1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

removed addConstraint in core.problem.VehicleRoutingProblem

This commit is contained in:
oblonski 2014-07-29 12:25:27 +02:00
parent 1f96eefc4d
commit b991793aba
10 changed files with 128 additions and 147 deletions

View file

@ -158,7 +158,7 @@ public class VehicleRoutingAlgorithmBuilder {
*/ */
public VehicleRoutingAlgorithm build() { public VehicleRoutingAlgorithm build() {
if(stateManager == null) stateManager = new StateManager(vrp); 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 //add core updater
stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen()); stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen());
// stateManager.addStateUpdater(new OpenRouteStateVerifier()); // stateManager.addStateUpdater(new OpenRouteStateVerifier());

View file

@ -471,7 +471,7 @@ public class VehicleRoutingAlgorithms {
* define constraints * define constraints
*/ */
//constraint manager //constraint manager
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager,vrp.getConstraints()); ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
constraintManager.addTimeWindowConstraint(); constraintManager.addTimeWindowConstraint();
constraintManager.addLoadConstraint(); constraintManager.addLoadConstraint();
constraintManager.addSkillsConstraint(); constraintManager.addSkillsConstraint();

View file

@ -102,9 +102,6 @@ public class VehicleRoutingProblem {
private Set<Vehicle> uniqueVehicles = new HashSet<Vehicle>(); private Set<Vehicle> uniqueVehicles = new HashSet<Vehicle>();
@Deprecated
private Collection<jsprit.core.problem.constraint.Constraint> constraints = new ArrayList<jsprit.core.problem.constraint.Constraint>();
private JobActivityFactory jobActivityFactory = new JobActivityFactory() { private JobActivityFactory jobActivityFactory = new JobActivityFactory() {
@Override @Override
@ -500,20 +497,6 @@ public class VehicleRoutingProblem {
return Collections.unmodifiableCollection(vehicleTypes); 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 * 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). * (see .addPenaltyVehicles(double penaltyFactor, double penaltyFixedCosts) if fixed costs = 0.0).
@ -616,11 +599,6 @@ public class VehicleRoutingProblem {
*/ */
private final FleetSize fleetSize; private final FleetSize fleetSize;
/**
* contains all constraints
*/
private final Collection<jsprit.core.problem.constraint.Constraint> constraints;
private final Locations locations; private final Locations locations;
private Map<Job,List<AbstractActivity>> activityMap; private Map<Job,List<AbstractActivity>> activityMap;
@ -644,8 +622,6 @@ public class VehicleRoutingProblem {
this.initialVehicleRoutes = builder.initialRoutes; this.initialVehicleRoutes = builder.initialRoutes;
this.transportCosts = builder.transportCosts; this.transportCosts = builder.transportCosts;
this.activityCosts = builder.activityCosts; this.activityCosts = builder.activityCosts;
//noinspection deprecation
this.constraints = builder.constraints;
this.locations = builder.getLocations(); this.locations = builder.getLocations();
this.activityMap = builder.activityMap; this.activityMap = builder.activityMap;
this.nuActivities = builder.activityIndexCounter; this.nuActivities = builder.activityIndexCounter;
@ -720,17 +696,6 @@ public class VehicleRoutingProblem {
return activityCosts; return activityCosts;
} }
/**
* Returns an unmodifiable collection of constraints.
*
* @return collection of constraints
* @deprecated use ConstraintManager instead
*/
@Deprecated
public Collection<jsprit.core.problem.constraint.Constraint> getConstraints(){
return Collections.unmodifiableCollection(constraints);
}
public Locations getLocations(){ public Locations getLocations(){
return locations; return locations;
} }
@ -743,14 +708,12 @@ public class VehicleRoutingProblem {
public JobActivityFactory getJobActivityFactory(){ public JobActivityFactory getJobActivityFactory(){
return jobActivityFactory; return jobActivityFactory;
}; }
public List<AbstractActivity> copyAndGetActivities(Job job){ public List<AbstractActivity> copyAndGetActivities(Job job){
List<AbstractActivity> acts = new ArrayList<AbstractActivity>(); List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
if(activityMap.containsKey(job)) { if(activityMap.containsKey(job)) {
for (AbstractActivity act : activityMap.get(job)) { for (AbstractActivity act : activityMap.get(job)) acts.add((AbstractActivity) act.duplicate());
acts.add((AbstractActivity) act.duplicate());
}
} }
return acts; return acts;
} }

View file

@ -17,7 +17,6 @@
package jsprit.core.problem; package jsprit.core.problem;
import jsprit.core.problem.VehicleRoutingProblem.FleetSize; import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import jsprit.core.problem.constraint.Constraint;
import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts; import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
@ -98,10 +97,10 @@ public class VehicleRoutingProblemTest {
VehicleTypeImpl type1 = mock(VehicleTypeImpl.class); VehicleTypeImpl type1 = mock(VehicleTypeImpl.class);
VehicleTypeImpl type2 = mock(VehicleTypeImpl.class); VehicleTypeImpl type2 = mock(VehicleTypeImpl.class);
Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("yo").setType(type1).build(); VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("yo").setType(type1).build();
Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("yo").setType(type1).build(); VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("yo").setType(type1).build();
Vehicle v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("yo").setType(type2).build(); VehicleImpl v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("yo").setType(type2).build();
Vehicle v4 = VehicleImpl.Builder.newInstance("v4").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); builder.addVehicle(v1).addVehicle(v2).addVehicle(v3).addVehicle(v4);
@ -234,16 +233,7 @@ public class VehicleRoutingProblemTest {
assertEquals(s2,vrp.getJobs().get("s2")); 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 @Test
public void whenSettingActivityCosts_vrpShouldContainIt(){ public void whenSettingActivityCosts_vrpShouldContainIt(){

View file

@ -16,21 +16,20 @@
******************************************************************************/ ******************************************************************************/
package jsprit.examples; package jsprit.examples;
import java.util.Collection;
import jsprit.analysis.toolbox.GraphStreamViewer; import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.GraphStreamViewer.Label; import jsprit.analysis.toolbox.GraphStreamViewer.Label;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm; 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.algorithm.termination.IterationWithoutImprovementTermination;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.VehicleRoutingProblem.FleetSize; import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.HardRouteStateLevelConstraint; import jsprit.core.problem.constraint.HardRouteStateLevelConstraint;
import jsprit.core.problem.job.Shipment; import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.misc.JobInsertionContext; import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleImpl.Builder; import jsprit.core.problem.vehicle.VehicleImpl.Builder;
import jsprit.core.problem.vehicle.VehicleType; import jsprit.core.problem.vehicle.VehicleType;
@ -39,6 +38,8 @@ import jsprit.core.util.Coordinate;
import jsprit.core.util.Solutions; import jsprit.core.util.Solutions;
import jsprit.util.Examples; import jsprit.util.Examples;
import java.util.Collection;
public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintAndSpecifiedVehicleEndLocationsExample { public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintAndSpecifiedVehicleEndLocationsExample {
@ -66,12 +67,12 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstrain
Builder vehicleBuilder1 = VehicleImpl.Builder.newInstance("v1"); Builder vehicleBuilder1 = VehicleImpl.Builder.newInstance("v1");
vehicleBuilder1.setStartLocationCoordinate(Coordinate.newInstance(10, 10)); vehicleBuilder1.setStartLocationCoordinate(Coordinate.newInstance(10, 10));
vehicleBuilder1.setType(vehicleType); vehicleBuilder1.setType(vehicleType);
Vehicle vehicle1 = vehicleBuilder1.build(); VehicleImpl vehicle1 = vehicleBuilder1.build();
Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("v2"); Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("v2");
vehicleBuilder2.setStartLocationCoordinate(Coordinate.newInstance(30, 30)).setEndLocationCoordinate(Coordinate.newInstance(30, 19)); vehicleBuilder2.setStartLocationCoordinate(Coordinate.newInstance(30, 30)).setEndLocationCoordinate(Coordinate.newInstance(30, 19));
vehicleBuilder2.setType(vehicleType); vehicleBuilder2.setType(vehicleType);
Vehicle vehicle2 = vehicleBuilder2.build(); VehicleImpl vehicle2 = vehicleBuilder2.build();
/* /*
@ -119,6 +120,11 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstrain
//you only have two vehicles //you only have two vehicles
vrpBuilder.setFleetSize(FleetSize.FINITE); vrpBuilder.setFleetSize(FleetSize.FINITE);
//build the problem
VehicleRoutingProblem problem = vrpBuilder.build();
/* /*
* add a geographic constraint determining that vehicle1 cannot go to x>15 and vehicle2 cannot go to x<15 * add a geographic constraint determining that vehicle1 cannot go to x>15 and vehicle2 cannot go to x<15
* *
@ -142,11 +148,6 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstrain
return true; return true;
} }
}; };
//add the constraint to the problem
vrpBuilder.addConstraint(geoClusterConstraint);
//build the problem
VehicleRoutingProblem problem = vrpBuilder.build();
/* /*
* get a sample algorithm. * get a sample algorithm.
@ -157,7 +158,17 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstrain
* bit more complicated and you cannot just add the above hard-constraint. Latter will be covered in another example. * 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.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(100));
// algorithm.setNuOfIterations(30000); // algorithm.setNuOfIterations(30000);
/* /*

View file

@ -16,14 +16,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.examples; package jsprit.examples;
import java.util.Arrays; import jsprit.analysis.toolbox.*;
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.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners.Priority; 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.VehicleRoutingProblem.FleetSize;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleTypeImpl; import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
import jsprit.core.util.Solutions; import jsprit.core.util.Solutions;
import jsprit.util.Examples; import jsprit.util.Examples;
import java.util.Arrays;
import java.util.Collection;
public class MultipleDepotExample { public class MultipleDepotExample {
/**
* @param args
*/
public static void main(String[] args) { public static void main(String[] args) {
/* /*
* some preparation - create output folder * some preparation - create output folder
@ -77,7 +69,7 @@ public class MultipleDepotExample {
for(Coordinate depotCoord : Arrays.asList(firstDepotCoord,second,third,fourth)){ for(Coordinate depotCoord : Arrays.asList(firstDepotCoord,second,third,fourth)){
for(int i=0;i<nuOfVehicles;i++){ for(int i=0;i<nuOfVehicles;i++){
VehicleTypeImpl vehicleType = VehicleTypeImpl.Builder.newInstance(depotCounter + "_type").addCapacityDimension(0, capacity).setCostPerDistance(1.0).build(); VehicleTypeImpl vehicleType = VehicleTypeImpl.Builder.newInstance(depotCounter + "_type").addCapacityDimension(0, capacity).setCostPerDistance(1.0).build();
Vehicle vehicle = VehicleImpl.Builder.newInstance(depotCounter + "_" + (i+1) + "_vehicle").setStartLocationCoordinate(depotCoord).setType(vehicleType).build(); VehicleImpl vehicle = VehicleImpl.Builder.newInstance(depotCounter + "_" + (i+1) + "_vehicle").setStartLocationCoordinate(depotCoord).setType(vehicleType).build();
vrpBuilder.addVehicle(vehicle); vrpBuilder.addVehicle(vehicle);
} }
depotCounter++; depotCounter++;

View file

@ -16,13 +16,13 @@
******************************************************************************/ ******************************************************************************/
package jsprit.examples; package jsprit.examples;
import java.util.Collection;
import jsprit.analysis.toolbox.Plotter; import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.VehicleRoutingAlgorithmBuilder;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint; import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint;
import jsprit.core.problem.io.VrpXMLWriter; import jsprit.core.problem.io.VrpXMLWriter;
import jsprit.core.problem.job.Delivery; import jsprit.core.problem.job.Delivery;
@ -37,6 +37,8 @@ import jsprit.core.util.Coordinate;
import jsprit.core.util.Solutions; import jsprit.core.util.Solutions;
import jsprit.util.Examples; import jsprit.util.Examples;
import java.util.Collection;
public class SimpleEnRoutePickupAndDeliveryWithDepotBoundedDeliveriesExample { public class SimpleEnRoutePickupAndDeliveryWithDepotBoundedDeliveriesExample {
@ -92,14 +94,19 @@ public class SimpleEnRoutePickupAndDeliveryWithDepotBoundedDeliveriesExample {
vrpBuilder.addJob(shipment1).addJob(shipment2).addJob(shipment3).addJob(shipment4) vrpBuilder.addJob(shipment1).addJob(shipment2).addJob(shipment3).addJob(shipment4)
.addJob(delivery1).addJob(delivery2).addJob(delivery3).addJob(delivery4).build(); .addJob(delivery1).addJob(delivery2).addJob(delivery3).addJob(delivery4).build();
vrpBuilder.addConstraint(new ServiceDeliveriesFirstConstraint());
VehicleRoutingProblem problem = vrpBuilder.build(); VehicleRoutingProblem problem = vrpBuilder.build();
/* /*
* get the algorithm out-of-the-box. * build the algorithm
*/ */
VehicleRoutingAlgorithm algorithm = new SchrimpfFactory().createAlgorithm(problem); VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(problem,"input/algorithmConfig.xml");
vraBuilder.addCoreConstraints();
vraBuilder.addDefaultCostCalculators();
StateManager stateManager = new StateManager(problem);
ConstraintManager constraintManager = new ConstraintManager(problem,stateManager);
constraintManager.addConstraint(new ServiceDeliveriesFirstConstraint(), ConstraintManager.Priority.CRITICAL);
vraBuilder.setStateAndConstraintManager(stateManager,constraintManager);
VehicleRoutingAlgorithm algorithm = vraBuilder.build();
/* /*
* and search a solution * and search a solution

View file

@ -16,14 +16,14 @@
******************************************************************************/ ******************************************************************************/
package jsprit.examples; package jsprit.examples;
import java.util.Collection;
import jsprit.analysis.toolbox.Plotter; import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.Plotter.Label; import jsprit.analysis.toolbox.Plotter.Label;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.VehicleRoutingAlgorithmBuilder;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint; import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint;
import jsprit.core.problem.io.VrpXMLWriter; import jsprit.core.problem.io.VrpXMLWriter;
import jsprit.core.problem.job.Delivery; import jsprit.core.problem.job.Delivery;
@ -38,6 +38,8 @@ import jsprit.core.util.Coordinate;
import jsprit.core.util.Solutions; import jsprit.core.util.Solutions;
import jsprit.util.Examples; import jsprit.util.Examples;
import java.util.Collection;
public class SimpleVRPWithBackhaulsExample { public class SimpleVRPWithBackhaulsExample {
@ -76,15 +78,16 @@ public class SimpleVRPWithBackhaulsExample {
vrpBuilder.addJob(pickup1).addJob(pickup2).addJob(delivery1).addJob(delivery2); vrpBuilder.addJob(pickup1).addJob(pickup2).addJob(delivery1).addJob(delivery2);
//
vrpBuilder.addConstraint(new ServiceDeliveriesFirstConstraint());
VehicleRoutingProblem problem = vrpBuilder.build(); VehicleRoutingProblem problem = vrpBuilder.build();
/* VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(problem,"input/algorithmConfig.xml");
* get the algorithm out-of-the-box. vraBuilder.addCoreConstraints();
*/ vraBuilder.addDefaultCostCalculators();
VehicleRoutingAlgorithm algorithm = new SchrimpfFactory().createAlgorithm(problem); StateManager stateManager = new StateManager(problem);
ConstraintManager constraintManager = new ConstraintManager(problem,stateManager);
constraintManager.addConstraint(new ServiceDeliveriesFirstConstraint(), ConstraintManager.Priority.CRITICAL);
vraBuilder.setStateAndConstraintManager(stateManager,constraintManager);
VehicleRoutingAlgorithm algorithm = vraBuilder.build();
/* /*
* and search a solution * and search a solution

View file

@ -16,21 +16,23 @@
******************************************************************************/ ******************************************************************************/
package jsprit.examples; package jsprit.examples;
import java.util.Collection;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener; import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer; import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.SolutionPrinter.Print; import jsprit.analysis.toolbox.SolutionPrinter.Print;
import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.VehicleRoutingAlgorithmBuilder;
import jsprit.core.algorithm.selector.SelectBest; import jsprit.core.algorithm.selector.SelectBest;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint; import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.util.Examples; import jsprit.util.Examples;
import java.util.Collection;
public class VRPWithBackhaulsExample { public class VRPWithBackhaulsExample {
@ -56,9 +58,7 @@ public class VRPWithBackhaulsExample {
/* /*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances). * Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
*/ */
// vrpBuilder.addProblemConstraint(Constraint.DELIVERIES_FIRST); //
vrpBuilder.addConstraint(new ServiceDeliveriesFirstConstraint());
VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
// SolutionPlotter.plotVrpAsPNG(vrp, "output/vrpwbh_solomon_r101.png", "pd_r101"); // SolutionPlotter.plotVrpAsPNG(vrp, "output/vrpwbh_solomon_r101.png", "pd_r101");
@ -68,8 +68,14 @@ public class VRPWithBackhaulsExample {
* *
* The algorithm can be defined and configured in an xml-file. * The algorithm can be defined and configured in an xml-file.
*/ */
// VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(vrp,"input/algorithmConfig_solomon.xml");
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_solomon.xml"); vraBuilder.addCoreConstraints();
vraBuilder.addDefaultCostCalculators();
StateManager stateManager = new StateManager(vrp);
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
constraintManager.addConstraint(new ServiceDeliveriesFirstConstraint(), ConstraintManager.Priority.CRITICAL);
vraBuilder.setStateAndConstraintManager(stateManager,constraintManager);
VehicleRoutingAlgorithm vra = vraBuilder.build();
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png")); vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png"));
/* /*
* Solve the problem. * Solve the problem.

View file

@ -16,20 +16,22 @@
******************************************************************************/ ******************************************************************************/
package jsprit.examples; package jsprit.examples;
import java.util.Collection;
import jsprit.analysis.toolbox.Plotter; import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.Plotter.Label; import jsprit.analysis.toolbox.Plotter.Label;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.VehicleRoutingAlgorithmBuilder;
import jsprit.core.algorithm.selector.SelectBest; import jsprit.core.algorithm.selector.SelectBest;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint; import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.util.Examples; import jsprit.util.Examples;
import java.util.Collection;
public class VRPWithBackhaulsExample2 { public class VRPWithBackhaulsExample2 {
@ -52,10 +54,6 @@ public class VRPWithBackhaulsExample2 {
*/ */
new VrpXMLReader(vrpBuilder).read("input/pd_christophides_vrpnc1_vcap50.xml"); new VrpXMLReader(vrpBuilder).read("input/pd_christophides_vrpnc1_vcap50.xml");
/*
* add the backhaul constraint to the problem
*/
vrpBuilder.addConstraint(new ServiceDeliveriesFirstConstraint());
/* /*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances). * Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
@ -70,7 +68,18 @@ public class VRPWithBackhaulsExample2 {
* *
* The algorithm can be defined and configured in an xml-file. * The algorithm can be defined and configured in an xml-file.
*/ */
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_solomon.xml"); // VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_solomon.xml");
VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(vrp,"input/algorithmConfig_solomon.xml");
vraBuilder.addDefaultCostCalculators();
vraBuilder.addCoreConstraints();
StateManager stateManager = new StateManager(vrp);
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
constraintManager.addConstraint(new ServiceDeliveriesFirstConstraint(), ConstraintManager.Priority.CRITICAL);
vraBuilder.setStateAndConstraintManager(stateManager,constraintManager);
VehicleRoutingAlgorithm vra = vraBuilder.build();
/* /*
* Solve the problem. * Solve the problem.