mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
introduce HardConstraints
This commit is contained in:
parent
63270cd557
commit
93ead2edce
13 changed files with 151 additions and 22 deletions
|
|
@ -46,6 +46,14 @@ final class CalculatesServiceInsertion implements JobInsertionCalculator{
|
||||||
|
|
||||||
private End end;
|
private End end;
|
||||||
|
|
||||||
|
private HardConstraint hardConstraint = new HardConstraint() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean fulfilled(InsertionScenario iScenario) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private Neighborhood neighborhood = new Neighborhood() {
|
private Neighborhood neighborhood = new Neighborhood() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -54,14 +62,16 @@ final class CalculatesServiceInsertion implements JobInsertionCalculator{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void setHardConstraint(HardConstraint hardConstraint){
|
||||||
|
this.hardConstraint = hardConstraint;
|
||||||
|
}
|
||||||
|
|
||||||
public void setNeighborhood(Neighborhood neighborhood) {
|
public void setNeighborhood(Neighborhood neighborhood) {
|
||||||
this.neighborhood = neighborhood;
|
this.neighborhood = neighborhood;
|
||||||
logger.info("initialise neighborhood " + neighborhood);
|
logger.info("initialise neighborhood " + neighborhood);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setActivityStates(StatesContainer activityStates2){
|
public void setStates(StatesContainer activityStates2){
|
||||||
this.states = activityStates2;
|
this.states = activityStates2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,14 +97,15 @@ final class CalculatesServiceInsertion implements JobInsertionCalculator{
|
||||||
if(jobToInsert == null) throw new IllegalStateException("jobToInsert is missing.");
|
if(jobToInsert == null) throw new IllegalStateException("jobToInsert is missing.");
|
||||||
if(newVehicle == null || newVehicle instanceof NoVehicle) throw new IllegalStateException("newVehicle is missing.");
|
if(newVehicle == null || newVehicle instanceof NoVehicle) throw new IllegalStateException("newVehicle is missing.");
|
||||||
|
|
||||||
|
InsertionFacts iFacts = new InsertionFacts(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
|
||||||
|
if(!hardConstraint.fulfilled(new InsertionScenario(iFacts, null))){
|
||||||
|
return InsertionData.noInsertionFound();
|
||||||
|
}
|
||||||
|
|
||||||
TourActivities tour = currentRoute.getTourActivities();
|
TourActivities tour = currentRoute.getTourActivities();
|
||||||
double bestCost = bestKnownCosts;
|
double bestCost = bestKnownCosts;
|
||||||
Service service = (Service)jobToInsert;
|
Service service = (Service)jobToInsert;
|
||||||
|
|
||||||
int currentLoad = (int) states.getRouteState(currentRoute, StateTypes.LOAD).toDouble();
|
|
||||||
if(currentLoad + service.getCapacityDemand() > newVehicle.getCapacity()){
|
|
||||||
return InsertionData.noInsertionFound();
|
|
||||||
}
|
|
||||||
int insertionIndex = InsertionData.NO_INDEX;
|
int insertionIndex = InsertionData.NO_INDEX;
|
||||||
|
|
||||||
TourActivity deliveryAct2Insert = ServiceActivity.newInstance(service);
|
TourActivity deliveryAct2Insert = ServiceActivity.newInstance(service);
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ final class CalculatesServiceInsertionOnRouteLevel implements JobInsertionCalcul
|
||||||
logger.info("initialise " + this);
|
logger.info("initialise " + this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setActivityStates(StatesContainer activityStates2){
|
public void setStates(StatesContainer activityStates2){
|
||||||
this.states = activityStates2;
|
this.states = activityStates2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,9 @@ final class CalculatesServiceInsertionWithTriangleInequality implements JobInser
|
||||||
private HardConstraint hardConstraint = new HardConstraint() {
|
private HardConstraint hardConstraint = new HardConstraint() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean fulfilled() { return true; }
|
public boolean fulfilled(InsertionScenario iScenario) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public void setHardConstraint(HardConstraint hardConstraint){
|
public void setHardConstraint(HardConstraint hardConstraint){
|
||||||
|
|
|
||||||
|
|
@ -223,12 +223,10 @@ class CalculatorBuilder {
|
||||||
|
|
||||||
private CalculatorPlusListeners createStandardLocal(VehicleRoutingProblem vrp, StatesContainer activityStates2){
|
private CalculatorPlusListeners createStandardLocal(VehicleRoutingProblem vrp, StatesContainer activityStates2){
|
||||||
JobInsertionCalculator standardServiceInsertion = new CalculatesServiceInsertion(vrp.getTransportCosts(), vrp.getActivityCosts());
|
JobInsertionCalculator standardServiceInsertion = new CalculatesServiceInsertion(vrp.getTransportCosts(), vrp.getActivityCosts());
|
||||||
// JobInsertionCalculator standardServiceInsertion = new CalculatesServiceInsertionWithTriangleInequality(vrp.getTransportCosts(), vrp.getActivityCosts());
|
|
||||||
// ((CalculatesServiceInsertionWithTriangleInequality) standardServiceInsertion).setActivityStates(activityStates2);
|
((CalculatesServiceInsertion) standardServiceInsertion).setStates(activityStates2);
|
||||||
// ((CalculatesServiceInsertionWithTriangleInequality) standardServiceInsertion).setNeighborhood(vrp.getNeighborhood());
|
|
||||||
//
|
|
||||||
((CalculatesServiceInsertion) standardServiceInsertion).setActivityStates(activityStates2);
|
|
||||||
((CalculatesServiceInsertion) standardServiceInsertion).setNeighborhood(vrp.getNeighborhood());
|
((CalculatesServiceInsertion) standardServiceInsertion).setNeighborhood(vrp.getNeighborhood());
|
||||||
|
((CalculatesServiceInsertion) standardServiceInsertion).setHardConstraint(new HardConstraints.HardLoadConstraint(activityStates2));
|
||||||
CalculatorPlusListeners calcPlusListeners = new CalculatorPlusListeners(standardServiceInsertion);
|
CalculatorPlusListeners calcPlusListeners = new CalculatorPlusListeners(standardServiceInsertion);
|
||||||
|
|
||||||
return calcPlusListeners;
|
return calcPlusListeners;
|
||||||
|
|
@ -248,7 +246,7 @@ class CalculatorBuilder {
|
||||||
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setNuOfActsForwardLooking(after);
|
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setNuOfActsForwardLooking(after);
|
||||||
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setMemorySize(solutionMemory);
|
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setMemorySize(solutionMemory);
|
||||||
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setNeighborhood(vrp.getNeighborhood());
|
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setNeighborhood(vrp.getNeighborhood());
|
||||||
((CalculatesServiceInsertionOnRouteLevel) jobInsertionCalculator).setActivityStates(activityStates2);
|
((CalculatesServiceInsertionOnRouteLevel) jobInsertionCalculator).setStates(activityStates2);
|
||||||
CalculatorPlusListeners calcPlusListener = new CalculatorPlusListeners(jobInsertionCalculator);
|
CalculatorPlusListeners calcPlusListener = new CalculatorPlusListeners(jobInsertionCalculator);
|
||||||
return calcPlusListener;
|
return calcPlusListener;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,6 @@ package algorithms;
|
||||||
|
|
||||||
interface HardConstraint {
|
interface HardConstraint {
|
||||||
|
|
||||||
public boolean fulfilled();
|
public boolean fulfilled(InsertionScenario iScenario);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
jsprit-core/src/main/java/algorithms/HardConstraints.java
Normal file
28
jsprit-core/src/main/java/algorithms/HardConstraints.java
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
package algorithms;
|
||||||
|
|
||||||
|
import basics.Service;
|
||||||
|
|
||||||
|
class HardConstraints {
|
||||||
|
|
||||||
|
static class HardLoadConstraint implements HardConstraint{
|
||||||
|
|
||||||
|
private StatesContainer states;
|
||||||
|
|
||||||
|
public HardLoadConstraint(StatesContainer states) {
|
||||||
|
super();
|
||||||
|
this.states = states;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean fulfilled(InsertionScenario iScenario) {
|
||||||
|
int currentLoad = (int) states.getRouteState(iScenario.getiFacts().getRoute(), StateTypes.LOAD).toDouble();
|
||||||
|
Service service = (Service) iScenario.getiFacts().getJob();
|
||||||
|
if(currentLoad + service.getCapacityDemand() > iScenario.getiFacts().getNewVehicle().getCapacity()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
63
jsprit-core/src/main/java/algorithms/InsertionFacts.java
Normal file
63
jsprit-core/src/main/java/algorithms/InsertionFacts.java
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
package algorithms;
|
||||||
|
|
||||||
|
import basics.Job;
|
||||||
|
import basics.route.Driver;
|
||||||
|
import basics.route.Vehicle;
|
||||||
|
import basics.route.VehicleRoute;
|
||||||
|
|
||||||
|
class InsertionFacts {
|
||||||
|
|
||||||
|
private VehicleRoute route;
|
||||||
|
private Job job;
|
||||||
|
private Vehicle newVehicle;
|
||||||
|
private Driver newDriver;
|
||||||
|
private double newDepTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the route
|
||||||
|
*/
|
||||||
|
public VehicleRoute getRoute() {
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the job
|
||||||
|
*/
|
||||||
|
public Job getJob() {
|
||||||
|
return job;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the newVehicle
|
||||||
|
*/
|
||||||
|
public Vehicle getNewVehicle() {
|
||||||
|
return newVehicle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the newDriver
|
||||||
|
*/
|
||||||
|
public Driver getNewDriver() {
|
||||||
|
return newDriver;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the newDepTime
|
||||||
|
*/
|
||||||
|
public double getNewDepTime() {
|
||||||
|
return newDepTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InsertionFacts(VehicleRoute route, Job job, Vehicle newVehicle,
|
||||||
|
Driver newDriver, double newDepTime) {
|
||||||
|
super();
|
||||||
|
this.route = route;
|
||||||
|
this.job = job;
|
||||||
|
this.newVehicle = newVehicle;
|
||||||
|
this.newDriver = newDriver;
|
||||||
|
this.newDepTime = newDepTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
27
jsprit-core/src/main/java/algorithms/InsertionScenario.java
Normal file
27
jsprit-core/src/main/java/algorithms/InsertionScenario.java
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
package algorithms;
|
||||||
|
|
||||||
|
class InsertionScenario {
|
||||||
|
|
||||||
|
InsertionFacts iFacts;
|
||||||
|
InsertionData iData;
|
||||||
|
public InsertionScenario(InsertionFacts iFacts, InsertionData iData) {
|
||||||
|
super();
|
||||||
|
this.iFacts = iFacts;
|
||||||
|
this.iData = iData;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the iFacts
|
||||||
|
*/
|
||||||
|
public InsertionFacts getiFacts() {
|
||||||
|
return iFacts;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the iData
|
||||||
|
*/
|
||||||
|
public InsertionData getiData() {
|
||||||
|
return iData;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -152,7 +152,7 @@ public class GendreauPostOptTest {
|
||||||
activityCosts = new ExampleActivityCostFunction();
|
activityCosts = new ExampleActivityCostFunction();
|
||||||
|
|
||||||
CalculatesServiceInsertion standardServiceInsertion = new CalculatesServiceInsertion(cost, activityCosts);
|
CalculatesServiceInsertion standardServiceInsertion = new CalculatesServiceInsertion(cost, activityCosts);
|
||||||
standardServiceInsertion.setActivityStates(states);
|
standardServiceInsertion.setStates(states);
|
||||||
CalculatesServiceInsertionConsideringFixCost withFixCost = new CalculatesServiceInsertionConsideringFixCost(standardServiceInsertion, states);
|
CalculatesServiceInsertionConsideringFixCost withFixCost = new CalculatesServiceInsertionConsideringFixCost(standardServiceInsertion, states);
|
||||||
withFixCost.setWeightOfFixCost(1.2);
|
withFixCost.setWeightOfFixCost(1.2);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -157,7 +157,7 @@ public class TestCalculatesServiceInsertion {
|
||||||
ExampleActivityCostFunction activityCosts = new ExampleActivityCostFunction();
|
ExampleActivityCostFunction activityCosts = new ExampleActivityCostFunction();
|
||||||
|
|
||||||
serviceInsertion = new CalculatesServiceInsertion(costs, activityCosts);
|
serviceInsertion = new CalculatesServiceInsertion(costs, activityCosts);
|
||||||
serviceInsertion.setActivityStates(states);
|
serviceInsertion.setStates(states);
|
||||||
|
|
||||||
stateUpdater = new UpdateStates(states, costs, activityCosts);
|
stateUpdater = new UpdateStates(states, costs, activityCosts);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -148,7 +148,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
|
||||||
ExampleActivityCostFunction activityCosts = new ExampleActivityCostFunction();
|
ExampleActivityCostFunction activityCosts = new ExampleActivityCostFunction();
|
||||||
serviceInsertion = new CalculatesServiceInsertionOnRouteLevel(costs,activityCosts);
|
serviceInsertion = new CalculatesServiceInsertionOnRouteLevel(costs,activityCosts);
|
||||||
serviceInsertion.setNuOfActsForwardLooking(4);
|
serviceInsertion.setNuOfActsForwardLooking(4);
|
||||||
serviceInsertion.setActivityStates(states);
|
serviceInsertion.setStates(states);
|
||||||
|
|
||||||
updateStates = new UpdateStates(states, costs, activityCosts);
|
updateStates = new UpdateStates(states, costs, activityCosts);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@
|
||||||
|
|
||||||
<construction>
|
<construction>
|
||||||
<insertion name="bestInsertion">
|
<insertion name="bestInsertion">
|
||||||
<level forwardLooking="2" memory="1">route</level>
|
<!-- <level forwardLooking="2" memory="1">route</level> -->
|
||||||
</insertion>
|
</insertion>
|
||||||
</construction>
|
</construction>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,8 +76,8 @@ public class SolomonExample {
|
||||||
*
|
*
|
||||||
* 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);
|
VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
|
||||||
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_solomon.xml");
|
// VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_solomon.xml");
|
||||||
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png"));
|
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png"));
|
||||||
/*
|
/*
|
||||||
* Solve the problem.
|
* Solve the problem.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue