diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/VehicleRoutingAlgorithmBuilder.java b/jsprit-core/src/main/java/jsprit/core/algorithm/VehicleRoutingAlgorithmBuilder.java index 27db5d8e..de3f0e56 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/VehicleRoutingAlgorithmBuilder.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/VehicleRoutingAlgorithmBuilder.java @@ -158,7 +158,7 @@ public class VehicleRoutingAlgorithmBuilder { */ public VehicleRoutingAlgorithm build() { if(stateManager == null) stateManager = new StateManager(vrp); - if(constraintManager == null) constraintManager = new ConstraintManager(vrp,stateManager,vrp.getConstraints()); + if(constraintManager == null) constraintManager = new ConstraintManager(vrp,stateManager); //add core updater stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen()); // stateManager.addStateUpdater(new OpenRouteStateVerifier()); diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java index b46479d9..acd27a77 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java @@ -471,7 +471,7 @@ public class VehicleRoutingAlgorithms { * define constraints */ //constraint manager - ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager,vrp.getConstraints()); + ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager); constraintManager.addTimeWindowConstraint(); constraintManager.addLoadConstraint(); constraintManager.addSkillsConstraint(); diff --git a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java index 19e57adb..328e2a29 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java @@ -102,9 +102,6 @@ public class VehicleRoutingProblem { private Set uniqueVehicles = new HashSet(); - @Deprecated - private Collection constraints = new ArrayList(); - private JobActivityFactory jobActivityFactory = new JobActivityFactory() { @Override @@ -500,20 +497,6 @@ public class VehicleRoutingProblem { return Collections.unmodifiableCollection(vehicleTypes); } - /** - * Adds constraint to problem. - * - * @param constraint constraint to be added - * @return this builder - * @deprecated use ConstraintManager instead - */ - @Deprecated - public Builder addConstraint(jsprit.core.problem.constraint.Constraint constraint){ - //noinspection deprecation - constraints.add(constraint); - return this; - } - /** * Adds penaltyVehicles, i.e. for every unique vehicle-location and type combination a penalty-vehicle is constructed having penaltyFactor times higher fixed and variable costs * (see .addPenaltyVehicles(double penaltyFactor, double penaltyFixedCosts) if fixed costs = 0.0). @@ -615,12 +598,7 @@ public class VehicleRoutingProblem { * An enum that indicates type of fleetSize. By default, it is INFINTE */ private final FleetSize fleetSize; - - /** - * contains all constraints - */ - private final Collection constraints; - + private final Locations locations; private Map> activityMap; @@ -644,8 +622,6 @@ public class VehicleRoutingProblem { this.initialVehicleRoutes = builder.initialRoutes; this.transportCosts = builder.transportCosts; this.activityCosts = builder.activityCosts; - //noinspection deprecation - this.constraints = builder.constraints; this.locations = builder.getLocations(); this.activityMap = builder.activityMap; this.nuActivities = builder.activityIndexCounter; @@ -719,18 +695,7 @@ public class VehicleRoutingProblem { public VehicleRoutingActivityCosts getActivityCosts(){ return activityCosts; } - - /** - * Returns an unmodifiable collection of constraints. - * - * @return collection of constraints - * @deprecated use ConstraintManager instead - */ - @Deprecated - public Collection getConstraints(){ - return Collections.unmodifiableCollection(constraints); - } - + public Locations getLocations(){ return locations; } @@ -743,14 +708,12 @@ public class VehicleRoutingProblem { public JobActivityFactory getJobActivityFactory(){ return jobActivityFactory; - }; + } public List copyAndGetActivities(Job job){ List acts = new ArrayList(); if(activityMap.containsKey(job)) { - for (AbstractActivity act : activityMap.get(job)) { - acts.add((AbstractActivity) act.duplicate()); - } + for (AbstractActivity act : activityMap.get(job)) acts.add((AbstractActivity) act.duplicate()); } return acts; } diff --git a/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java b/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java index da91acd2..2bd7c3ef 100644 --- a/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java +++ b/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java @@ -17,7 +17,6 @@ package jsprit.core.problem; import jsprit.core.problem.VehicleRoutingProblem.FleetSize; -import jsprit.core.problem.constraint.Constraint; import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; import jsprit.core.problem.cost.VehicleRoutingActivityCosts; import jsprit.core.problem.driver.Driver; @@ -98,10 +97,10 @@ public class VehicleRoutingProblemTest { VehicleTypeImpl type1 = mock(VehicleTypeImpl.class); VehicleTypeImpl type2 = mock(VehicleTypeImpl.class); - Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("yo").setType(type1).build(); - Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("yo").setType(type1).build(); - Vehicle v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("yo").setType(type2).build(); - Vehicle v4 = VehicleImpl.Builder.newInstance("v4").setStartLocationId("yo").setType(type2).build(); + VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("yo").setType(type1).build(); + VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("yo").setType(type1).build(); + VehicleImpl v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("yo").setType(type2).build(); + VehicleImpl v4 = VehicleImpl.Builder.newInstance("v4").setStartLocationId("yo").setType(type2).build(); builder.addVehicle(v1).addVehicle(v2).addVehicle(v3).addVehicle(v4); @@ -234,16 +233,7 @@ public class VehicleRoutingProblemTest { assertEquals(s2,vrp.getJobs().get("s2")); } - @SuppressWarnings("deprecation") - @Test - public void whenConstraintsAdded_vrpShouldContainThem(){ - Constraint c1 = mock(Constraint.class); - Constraint c2 = mock(Constraint.class); - VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); - builder.addConstraint(c1).addConstraint(c2); - VehicleRoutingProblem problem = builder.build(); - assertEquals(2,problem.getConstraints().size()); - } + @Test public void whenSettingActivityCosts_vrpShouldContainIt(){ diff --git a/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintAndSpecifiedVehicleEndLocationsExample.java b/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintAndSpecifiedVehicleEndLocationsExample.java index 7e656516..df566130 100644 --- a/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintAndSpecifiedVehicleEndLocationsExample.java +++ b/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintAndSpecifiedVehicleEndLocationsExample.java @@ -16,21 +16,20 @@ ******************************************************************************/ package jsprit.examples; -import java.util.Collection; - import jsprit.analysis.toolbox.GraphStreamViewer; import jsprit.analysis.toolbox.GraphStreamViewer.Label; import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.core.algorithm.VehicleRoutingAlgorithm; -import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; +import jsprit.core.algorithm.VehicleRoutingAlgorithmBuilder; +import jsprit.core.algorithm.state.StateManager; import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem.FleetSize; +import jsprit.core.problem.constraint.ConstraintManager; import jsprit.core.problem.constraint.HardRouteStateLevelConstraint; import jsprit.core.problem.job.Shipment; import jsprit.core.problem.misc.JobInsertionContext; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; -import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl.Builder; import jsprit.core.problem.vehicle.VehicleType; @@ -39,6 +38,8 @@ import jsprit.core.util.Coordinate; import jsprit.core.util.Solutions; import jsprit.util.Examples; +import java.util.Collection; + public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintAndSpecifiedVehicleEndLocationsExample { @@ -66,12 +67,12 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstrain Builder vehicleBuilder1 = VehicleImpl.Builder.newInstance("v1"); vehicleBuilder1.setStartLocationCoordinate(Coordinate.newInstance(10, 10)); vehicleBuilder1.setType(vehicleType); - Vehicle vehicle1 = vehicleBuilder1.build(); + VehicleImpl vehicle1 = vehicleBuilder1.build(); Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("v2"); vehicleBuilder2.setStartLocationCoordinate(Coordinate.newInstance(30, 30)).setEndLocationCoordinate(Coordinate.newInstance(30, 19)); vehicleBuilder2.setType(vehicleType); - Vehicle vehicle2 = vehicleBuilder2.build(); + VehicleImpl vehicle2 = vehicleBuilder2.build(); /* @@ -119,45 +120,55 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstrain //you only have two vehicles vrpBuilder.setFleetSize(FleetSize.FINITE); - /* - * add a geographic constraint determining that vehicle1 cannot go to x>15 and vehicle2 cannot go to x<15 - * - * switch off the geoConstraints to see the impact of this constraint on routes, or just exchange v1 and v2 to reverse the geo-constraint. - */ - HardRouteStateLevelConstraint geoClusterConstraint = new HardRouteStateLevelConstraint() { - - @Override - public boolean fulfilled(JobInsertionContext insertionContext) { - Shipment shipment2insert = ((Shipment)insertionContext.getJob()); - if(insertionContext.getNewVehicle().getId().equals("v1")){ - if(shipment2insert.getPickupCoord().getX() > 15. || shipment2insert.getDeliveryCoord().getX() > 15.){ - return false; - } - } - if(insertionContext.getNewVehicle().getId().equals("v2")){ - if(shipment2insert.getPickupCoord().getX() < 15. || shipment2insert.getDeliveryCoord().getX() < 15.){ - return false; - } - } - return true; - } - }; - //add the constraint to the problem - vrpBuilder.addConstraint(geoClusterConstraint); + + //build the problem VehicleRoutingProblem problem = vrpBuilder.build(); - /* - * get a sample algorithm. - * + * add a geographic constraint determining that vehicle1 cannot go to x>15 and vehicle2 cannot go to x<15 + * + * switch off the geoConstraints to see the impact of this constraint on routes, or just exchange v1 and v2 to reverse the geo-constraint. + */ + HardRouteStateLevelConstraint geoClusterConstraint = new HardRouteStateLevelConstraint() { + + @Override + public boolean fulfilled(JobInsertionContext insertionContext) { + Shipment shipment2insert = ((Shipment)insertionContext.getJob()); + if(insertionContext.getNewVehicle().getId().equals("v1")){ + if(shipment2insert.getPickupCoord().getX() > 15. || shipment2insert.getDeliveryCoord().getX() > 15.){ + return false; + } + } + if(insertionContext.getNewVehicle().getId().equals("v2")){ + if(shipment2insert.getPickupCoord().getX() < 15. || shipment2insert.getDeliveryCoord().getX() < 15.){ + return false; + } + } + return true; + } + }; + + /* + * get a sample algorithm. + * * Note that you need to make sure to prohibit vehicle-switching by adding the insertion-tag false. * This way you make sure that no vehicle can take over a route that is employed by another. Allowing this might make sense when dealing with - * a heterogeneous fleet and you want to employ a bigger vehicle on a still existing route. However, allowing it makes constraint-checking + * a heterogeneous fleet and you want to employ a bigger vehicle on a still existing route. However, allowing it makes constraint-checking * bit more complicated and you cannot just add the above hard-constraint. Latter will be covered in another example. - * + * */ - VehicleRoutingAlgorithm algorithm = VehicleRoutingAlgorithms.readAndCreateAlgorithm(problem, "input/algorithmConfig_noVehicleSwitch.xml"); + VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(problem,"input/algorithmConfig_noVehicleSwitch.xml"); + vraBuilder.addCoreConstraints(); + vraBuilder.addDefaultCostCalculators(); + + StateManager stateManager = new StateManager(problem); + ConstraintManager constraintManager = new ConstraintManager(problem,stateManager); + constraintManager.addConstraint(geoClusterConstraint); + + vraBuilder.setStateAndConstraintManager(stateManager,constraintManager); + VehicleRoutingAlgorithm algorithm = vraBuilder.build(); + algorithm.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(100)); // algorithm.setNuOfIterations(30000); /* diff --git a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java index e494cf1f..782babb0 100644 --- a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java +++ b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java @@ -16,14 +16,7 @@ ******************************************************************************/ package jsprit.examples; -import java.util.Arrays; -import java.util.Collection; - -import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener; -import jsprit.analysis.toolbox.GraphStreamViewer; -import jsprit.analysis.toolbox.Plotter; -import jsprit.analysis.toolbox.SolutionPrinter; -import jsprit.analysis.toolbox.StopWatch; +import jsprit.analysis.toolbox.*; import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners.Priority; @@ -31,19 +24,18 @@ import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem.FleetSize; import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; -import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleTypeImpl; import jsprit.core.util.Coordinate; import jsprit.core.util.Solutions; import jsprit.util.Examples; +import java.util.Arrays; +import java.util.Collection; + public class MultipleDepotExample { - /** - * @param args - */ public static void main(String[] args) { /* * some preparation - create output folder @@ -77,7 +69,7 @@ public class MultipleDepotExample { for(Coordinate depotCoord : Arrays.asList(firstDepotCoord,second,third,fourth)){ for(int i=0;i