mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add infrastructure to easily add soft and hard constraints
This commit is contained in:
parent
e5e5783d67
commit
f1213994e4
8 changed files with 62 additions and 3 deletions
|
|
@ -492,7 +492,7 @@ public class VehicleRoutingAlgorithms {
|
|||
* define constraints
|
||||
*/
|
||||
//constraint manager
|
||||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
|
||||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager,vrp.getConstraints());
|
||||
constraintManager.addTimeWindowConstraint();
|
||||
constraintManager.addLoadConstraint();
|
||||
|
||||
|
|
|
|||
|
|
@ -118,6 +118,8 @@ public class VehicleRoutingProblem {
|
|||
|
||||
private Collection<Constraint> problemConstraints;
|
||||
|
||||
private Collection<jsprit.core.problem.constraint.Constraint> constraints;
|
||||
|
||||
/**
|
||||
* by default all locations are neighbors
|
||||
*/
|
||||
|
|
@ -136,6 +138,7 @@ public class VehicleRoutingProblem {
|
|||
vehicleTypes = new ArrayList<VehicleType>();
|
||||
services = new ArrayList<Service>();
|
||||
problemConstraints = new ArrayList<VehicleRoutingProblem.Constraint>();
|
||||
constraints = new ArrayList<jsprit.core.problem.constraint.Constraint>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -388,6 +391,10 @@ public class VehicleRoutingProblem {
|
|||
return Collections.unmodifiableCollection(vehicles);
|
||||
}
|
||||
|
||||
public void addConstraint(jsprit.core.problem.constraint.Constraint constraint){
|
||||
constraints.add(constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an unmodifiable collection of already added services.
|
||||
*
|
||||
|
|
@ -456,6 +463,8 @@ public class VehicleRoutingProblem {
|
|||
|
||||
private Collection<Constraint> problemConstraints;
|
||||
|
||||
private Collection<jsprit.core.problem.constraint.Constraint> constraints;
|
||||
|
||||
private VehicleRoutingProblem(Builder builder) {
|
||||
this.jobs = builder.jobs;
|
||||
this.fleetComposition = builder.fleetComposition;
|
||||
|
|
@ -466,6 +475,7 @@ public class VehicleRoutingProblem {
|
|||
this.activityCosts = builder.activityCosts;
|
||||
this.neighborhood = builder.neighborhood;
|
||||
this.problemConstraints = builder.problemConstraints;
|
||||
this.constraints = builder.constraints;
|
||||
logger.info("initialise " + this);
|
||||
}
|
||||
|
||||
|
|
@ -558,5 +568,9 @@ public class VehicleRoutingProblem {
|
|||
return activityCosts;
|
||||
}
|
||||
|
||||
public Collection<jsprit.core.problem.constraint.Constraint> getConstraints(){
|
||||
return Collections.unmodifiableCollection(constraints);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
package jsprit.core.problem.constraint;
|
||||
|
||||
public interface Constraint {
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,9 @@
|
|||
package jsprit.core.problem.constraint;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.VehicleRoutingProblem.Constraint;
|
||||
import jsprit.core.problem.misc.JobInsertionContext;
|
||||
|
|
@ -12,6 +16,8 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
|
|||
CRITICAL, HIGH, LOW
|
||||
}
|
||||
|
||||
private static Logger log = Logger.getLogger(ConstraintManager.class);
|
||||
|
||||
private HardActivityLevelConstraintManager actLevelConstraintManager = new HardActivityLevelConstraintManager();
|
||||
|
||||
private HardRouteLevelConstraintManager routeLevelConstraintManager = new HardRouteLevelConstraintManager();
|
||||
|
|
@ -29,6 +35,30 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
|
|||
this.stateManager = stateManager;
|
||||
}
|
||||
|
||||
public ConstraintManager(VehicleRoutingProblem vrp, RouteAndActivityStateGetter stateManager, Collection<jsprit.core.problem.constraint.Constraint> constraints) {
|
||||
this.vrp = vrp;
|
||||
this.stateManager = stateManager;
|
||||
resolveConstraints(constraints);
|
||||
}
|
||||
|
||||
private void resolveConstraints(Collection<jsprit.core.problem.constraint.Constraint> constraints) {
|
||||
for(jsprit.core.problem.constraint.Constraint c : constraints){
|
||||
boolean constraintTypeKnown = false;
|
||||
if(c instanceof HardActivityStateLevelConstraint) {
|
||||
actLevelConstraintManager.addConstraint((HardActivityStateLevelConstraint) c, Priority.HIGH);
|
||||
constraintTypeKnown = true;
|
||||
}
|
||||
if(c instanceof HardRouteStateLevelConstraint) {
|
||||
routeLevelConstraintManager.addConstraint((HardRouteStateLevelConstraint) c);
|
||||
constraintTypeKnown = true;
|
||||
}
|
||||
if(!constraintTypeKnown){
|
||||
log.warn("constraint " + c + " unknown thus ignores the constraint. currently, a constraint must implement either HardActivityStateLevelConstraint or HardRouteStateLevelConstraint");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void addTimeWindowConstraint(){
|
||||
if(!timeWindowConstraintsSet){
|
||||
addConstraint(new TimeWindowConstraint(stateManager, vrp.getTransportCosts()),Priority.HIGH);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package jsprit.core.problem.constraint;
|
|||
import jsprit.core.problem.misc.JobInsertionContext;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
|
||||
public interface HardActivityStateLevelConstraint {
|
||||
public interface HardActivityStateLevelConstraint extends HardConstraint{
|
||||
|
||||
static enum ConstraintsStatus {
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
package jsprit.core.problem.constraint;
|
||||
|
||||
public interface HardConstraint extends Constraint{
|
||||
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ package jsprit.core.problem.constraint;
|
|||
import jsprit.core.problem.misc.JobInsertionContext;
|
||||
|
||||
|
||||
public interface HardRouteStateLevelConstraint {
|
||||
public interface HardRouteStateLevelConstraint extends HardConstraint{
|
||||
|
||||
public boolean fulfilled(JobInsertionContext insertionContext);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
package jsprit.core.problem.constraint;
|
||||
|
||||
public interface SoftConstraint extends Constraint{
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue