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 HardConstraint hardConstraint = new HardConstraint() {
|
||||
|
||||
@Override
|
||||
public boolean fulfilled(InsertionScenario iScenario) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private Neighborhood neighborhood = new Neighborhood() {
|
||||
|
||||
@Override
|
||||
|
|
@ -54,14 +62,16 @@ final class CalculatesServiceInsertion implements JobInsertionCalculator{
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
void setHardConstraint(HardConstraint hardConstraint){
|
||||
this.hardConstraint = hardConstraint;
|
||||
}
|
||||
|
||||
public void setNeighborhood(Neighborhood neighborhood) {
|
||||
this.neighborhood = neighborhood;
|
||||
logger.info("initialise neighborhood " + neighborhood);
|
||||
}
|
||||
|
||||
public void setActivityStates(StatesContainer activityStates2){
|
||||
public void setStates(StatesContainer activityStates2){
|
||||
this.states = activityStates2;
|
||||
}
|
||||
|
||||
|
|
@ -87,14 +97,15 @@ final class CalculatesServiceInsertion implements JobInsertionCalculator{
|
|||
if(jobToInsert == null) throw new IllegalStateException("jobToInsert 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();
|
||||
double bestCost = bestKnownCosts;
|
||||
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;
|
||||
|
||||
TourActivity deliveryAct2Insert = ServiceActivity.newInstance(service);
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ final class CalculatesServiceInsertionOnRouteLevel implements JobInsertionCalcul
|
|||
logger.info("initialise " + this);
|
||||
}
|
||||
|
||||
public void setActivityStates(StatesContainer activityStates2){
|
||||
public void setStates(StatesContainer activityStates2){
|
||||
this.states = activityStates2;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -73,7 +73,9 @@ final class CalculatesServiceInsertionWithTriangleInequality implements JobInser
|
|||
private HardConstraint hardConstraint = new HardConstraint() {
|
||||
|
||||
@Override
|
||||
public boolean fulfilled() { return true; }
|
||||
public boolean fulfilled(InsertionScenario iScenario) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
public void setHardConstraint(HardConstraint hardConstraint){
|
||||
|
|
|
|||
|
|
@ -223,12 +223,10 @@ class CalculatorBuilder {
|
|||
|
||||
private CalculatorPlusListeners createStandardLocal(VehicleRoutingProblem vrp, StatesContainer activityStates2){
|
||||
JobInsertionCalculator standardServiceInsertion = new CalculatesServiceInsertion(vrp.getTransportCosts(), vrp.getActivityCosts());
|
||||
// JobInsertionCalculator standardServiceInsertion = new CalculatesServiceInsertionWithTriangleInequality(vrp.getTransportCosts(), vrp.getActivityCosts());
|
||||
// ((CalculatesServiceInsertionWithTriangleInequality) standardServiceInsertion).setActivityStates(activityStates2);
|
||||
// ((CalculatesServiceInsertionWithTriangleInequality) standardServiceInsertion).setNeighborhood(vrp.getNeighborhood());
|
||||
//
|
||||
((CalculatesServiceInsertion) standardServiceInsertion).setActivityStates(activityStates2);
|
||||
|
||||
((CalculatesServiceInsertion) standardServiceInsertion).setStates(activityStates2);
|
||||
((CalculatesServiceInsertion) standardServiceInsertion).setNeighborhood(vrp.getNeighborhood());
|
||||
((CalculatesServiceInsertion) standardServiceInsertion).setHardConstraint(new HardConstraints.HardLoadConstraint(activityStates2));
|
||||
CalculatorPlusListeners calcPlusListeners = new CalculatorPlusListeners(standardServiceInsertion);
|
||||
|
||||
return calcPlusListeners;
|
||||
|
|
@ -248,7 +246,7 @@ class CalculatorBuilder {
|
|||
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setNuOfActsForwardLooking(after);
|
||||
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setMemorySize(solutionMemory);
|
||||
((CalculatesServiceInsertionOnRouteLevel)jobInsertionCalculator).setNeighborhood(vrp.getNeighborhood());
|
||||
((CalculatesServiceInsertionOnRouteLevel) jobInsertionCalculator).setActivityStates(activityStates2);
|
||||
((CalculatesServiceInsertionOnRouteLevel) jobInsertionCalculator).setStates(activityStates2);
|
||||
CalculatorPlusListeners calcPlusListener = new CalculatorPlusListeners(jobInsertionCalculator);
|
||||
return calcPlusListener;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@ package algorithms;
|
|||
|
||||
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();
|
||||
|
||||
CalculatesServiceInsertion standardServiceInsertion = new CalculatesServiceInsertion(cost, activityCosts);
|
||||
standardServiceInsertion.setActivityStates(states);
|
||||
standardServiceInsertion.setStates(states);
|
||||
CalculatesServiceInsertionConsideringFixCost withFixCost = new CalculatesServiceInsertionConsideringFixCost(standardServiceInsertion, states);
|
||||
withFixCost.setWeightOfFixCost(1.2);
|
||||
|
||||
|
|
|
|||
|
|
@ -157,7 +157,7 @@ public class TestCalculatesServiceInsertion {
|
|||
ExampleActivityCostFunction activityCosts = new ExampleActivityCostFunction();
|
||||
|
||||
serviceInsertion = new CalculatesServiceInsertion(costs, activityCosts);
|
||||
serviceInsertion.setActivityStates(states);
|
||||
serviceInsertion.setStates(states);
|
||||
|
||||
stateUpdater = new UpdateStates(states, costs, activityCosts);
|
||||
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
|
|||
ExampleActivityCostFunction activityCosts = new ExampleActivityCostFunction();
|
||||
serviceInsertion = new CalculatesServiceInsertionOnRouteLevel(costs,activityCosts);
|
||||
serviceInsertion.setNuOfActsForwardLooking(4);
|
||||
serviceInsertion.setActivityStates(states);
|
||||
serviceInsertion.setStates(states);
|
||||
|
||||
updateStates = new UpdateStates(states, costs, activityCosts);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue