mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
Merge remote-tracking branch 'origin/master'
# Conflicts: # jsprit-core/src/main/java/jsprit/core/problem/constraint/ServiceLoadActivityLevelConstraint.java
This commit is contained in:
commit
b9c7dc3324
5 changed files with 90 additions and 43 deletions
|
|
@ -136,7 +136,9 @@ public class PrettyAlgorithmBuilder {
|
||||||
vra.addListener(new AlgorithmStartsListener() {
|
vra.addListener(new AlgorithmStartsListener() {
|
||||||
@Override
|
@Override
|
||||||
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||||
solutions.add(new InsertionInitialSolutionFactory(iniInsertionStrategy, iniObjFunction).createSolution(vrp));
|
if (solutions.isEmpty()) {
|
||||||
|
solutions.add(new InsertionInitialSolutionFactory(iniInsertionStrategy, iniObjFunction).createSolution(vrp));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -145,8 +145,6 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
catch (ExecutionException e) {
|
catch (ExecutionException e) {
|
||||||
e.printStackTrace();
|
|
||||||
logger.error("Exception", e);
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,49 +25,52 @@ import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensures load constraint for inserting ServiceActivity.
|
* Ensures load constraint for inserting ServiceActivity.
|
||||||
* <p/>
|
*
|
||||||
* <p>When using this, you need to use<br>
|
* <p>When using this, you need to use<br>
|
||||||
*
|
*
|
||||||
|
*
|
||||||
* @author schroeder
|
* @author schroeder
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
class ServiceLoadActivityLevelConstraint implements HardActivityConstraint {
|
public class ServiceLoadActivityLevelConstraint implements HardActivityConstraint {
|
||||||
|
|
||||||
private RouteAndActivityStateGetter stateManager;
|
private RouteAndActivityStateGetter stateManager;
|
||||||
|
|
||||||
private Capacity defaultValue;
|
private Capacity defaultValue;
|
||||||
|
|
||||||
public ServiceLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) {
|
public ServiceLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) {
|
||||||
super();
|
super();
|
||||||
this.stateManager = stateManager;
|
this.stateManager = stateManager;
|
||||||
defaultValue = Capacity.Builder.newInstance().build();
|
defaultValue = Capacity.Builder.newInstance().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
|
public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
|
||||||
Capacity futureMaxLoad;
|
Capacity futureMaxLoad;
|
||||||
Capacity prevMaxLoad;
|
Capacity prevMaxLoad;
|
||||||
if (prevAct instanceof Start) {
|
if(prevAct instanceof Start){
|
||||||
futureMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.MAXLOAD, Capacity.class);
|
futureMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.MAXLOAD, Capacity.class);
|
||||||
if (futureMaxLoad == null) futureMaxLoad = defaultValue;
|
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
|
||||||
prevMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.LOAD_AT_BEGINNING, Capacity.class);
|
prevMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.LOAD_AT_BEGINNING, Capacity.class);
|
||||||
if (prevMaxLoad == null) prevMaxLoad = defaultValue;
|
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
|
||||||
} else {
|
}
|
||||||
futureMaxLoad = stateManager.getActivityState(prevAct, InternalStates.FUTURE_MAXLOAD, Capacity.class);
|
else{
|
||||||
if (futureMaxLoad == null) futureMaxLoad = defaultValue;
|
futureMaxLoad = stateManager.getActivityState(prevAct, InternalStates.FUTURE_MAXLOAD, Capacity.class);
|
||||||
prevMaxLoad = stateManager.getActivityState(prevAct, InternalStates.PAST_MAXLOAD, Capacity.class);
|
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
|
||||||
if (prevMaxLoad == null) prevMaxLoad = defaultValue;
|
prevMaxLoad = stateManager.getActivityState(prevAct, InternalStates.PAST_MAXLOAD, Capacity.class);
|
||||||
|
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (newAct instanceof PickupService || newAct instanceof ServiceActivity) {
|
if(newAct instanceof PickupService || newAct instanceof ServiceActivity){
|
||||||
if (!Capacity.addup(newAct.getSize(), futureMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())) {
|
if(!Capacity.addup(newAct.getSize(), futureMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())){
|
||||||
return ConstraintsStatus.NOT_FULFILLED;
|
return ConstraintsStatus.NOT_FULFILLED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (newAct instanceof DeliverService) {
|
if(newAct instanceof DeliverService){
|
||||||
if (!Capacity.addup(Capacity.invert(newAct.getSize()), prevMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())) {
|
if(!Capacity.addup(Capacity.invert(newAct.getSize()), prevMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())){
|
||||||
return ConstraintsStatus.NOT_FULFILLED_BREAK;
|
return ConstraintsStatus.NOT_FULFILLED_BREAK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ConstraintsStatus.FULFILLED;
|
return ConstraintsStatus.FULFILLED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
|
||||||
*
|
*
|
||||||
* @author stefan
|
* @author stefan
|
||||||
*/
|
*/
|
||||||
class ServiceLoadRouteLevelConstraint implements HardRouteConstraint {
|
public class ServiceLoadRouteLevelConstraint implements HardRouteConstraint {
|
||||||
|
|
||||||
private RouteAndActivityStateGetter stateManager;
|
private RouteAndActivityStateGetter stateManager;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,27 @@
|
||||||
package jsprit.core.algorithm;
|
package jsprit.core.algorithm;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import jsprit.core.algorithm.box.GreedySchrimpfFactory;
|
import jsprit.core.algorithm.box.GreedySchrimpfFactory;
|
||||||
import jsprit.core.algorithm.box.Jsprit;
|
import jsprit.core.algorithm.box.Jsprit;
|
||||||
|
import jsprit.core.algorithm.box.Jsprit.Builder;
|
||||||
import jsprit.core.algorithm.box.SchrimpfFactory;
|
import jsprit.core.algorithm.box.SchrimpfFactory;
|
||||||
|
import jsprit.core.algorithm.state.StateManager;
|
||||||
|
import jsprit.core.algorithm.state.UpdateEndLocationIfRouteIsOpen;
|
||||||
|
import jsprit.core.algorithm.state.UpdateVariableCosts;
|
||||||
import jsprit.core.problem.AbstractActivity;
|
import jsprit.core.problem.AbstractActivity;
|
||||||
import jsprit.core.problem.Location;
|
import jsprit.core.problem.Location;
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
|
import jsprit.core.problem.constraint.ConstraintManager;
|
||||||
|
import jsprit.core.problem.constraint.ConstraintManager.Priority;
|
||||||
|
import jsprit.core.problem.constraint.ServiceLoadActivityLevelConstraint;
|
||||||
|
import jsprit.core.problem.constraint.ServiceLoadRouteLevelConstraint;
|
||||||
import jsprit.core.problem.io.VrpXMLReader;
|
import jsprit.core.problem.io.VrpXMLReader;
|
||||||
import jsprit.core.problem.job.Job;
|
import jsprit.core.problem.job.Job;
|
||||||
import jsprit.core.problem.job.Service;
|
import jsprit.core.problem.job.Service;
|
||||||
|
|
@ -38,13 +53,9 @@ import jsprit.core.problem.vehicle.VehicleTypeImpl;
|
||||||
import jsprit.core.reporting.SolutionPrinter;
|
import jsprit.core.reporting.SolutionPrinter;
|
||||||
import jsprit.core.util.Coordinate;
|
import jsprit.core.util.Coordinate;
|
||||||
import jsprit.core.util.Solutions;
|
import jsprit.core.util.Solutions;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
public class InitialRoutesTest {
|
public class InitialRoutesTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -395,4 +406,37 @@ public class InitialRoutesTest {
|
||||||
vra.searchSolutions();
|
vra.searchSolutions();
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void buildWithoutTimeConstraints() {
|
||||||
|
Service s1 = Service.Builder.newInstance("s1").setLocation(Location.newInstance(0,10)).addSizeDimension(0, 10).build();
|
||||||
|
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(10,20)).addSizeDimension(0, 12).build();
|
||||||
|
|
||||||
|
VehicleTypeImpl vt = VehicleTypeImpl.Builder.newInstance("vt").addCapacityDimension(0, 15).build();
|
||||||
|
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setType(vt).setStartLocation(Location.newInstance(0,0)).build();
|
||||||
|
|
||||||
|
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s1).addJob(s2).addVehicle(v).build();
|
||||||
|
Builder algBuilder = Jsprit.Builder.newInstance(vrp).addCoreStateAndConstraintStuff(false);
|
||||||
|
|
||||||
|
// only required constraints
|
||||||
|
StateManager stateManager = new StateManager(vrp);
|
||||||
|
ConstraintManager constraintManager = new ConstraintManager(vrp, stateManager);
|
||||||
|
constraintManager.addConstraint(new ServiceLoadRouteLevelConstraint(stateManager));
|
||||||
|
constraintManager.addConstraint(new ServiceLoadActivityLevelConstraint(stateManager), Priority.LOW);
|
||||||
|
stateManager.updateLoadStates();
|
||||||
|
stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen());
|
||||||
|
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
|
||||||
|
|
||||||
|
algBuilder.setStateAndConstraintManager(stateManager, constraintManager);
|
||||||
|
VehicleRoutingAlgorithm vra = algBuilder.buildAlgorithm();
|
||||||
|
vra.setMaxIterations(20);
|
||||||
|
Collection<VehicleRoutingProblemSolution> searchSolutions = vra.searchSolutions();
|
||||||
|
VehicleRoutingProblemSolution bestOf = Solutions.bestOf(searchSolutions);
|
||||||
|
|
||||||
|
//ensure 2 routes
|
||||||
|
assertEquals(2, bestOf.getRoutes().size());
|
||||||
|
|
||||||
|
//ensure no time information in first service of first route
|
||||||
|
assertEquals(0, bestOf.getRoutes().iterator().next().getActivities().iterator().next().getArrTime(), 0.001);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue