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

added soft constraints

This commit is contained in:
oblonski 2014-02-10 09:02:50 +01:00
parent bb98df01f1
commit 86c168e428
13 changed files with 446 additions and 66 deletions

View file

@ -253,7 +253,7 @@ class CalculatorBuilder {
} }
ShipmentInsertionCalculator shipmentInsertion = new ShipmentInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager, constraintManager); ShipmentInsertionCalculator shipmentInsertion = new ShipmentInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager, constraintManager);
ServiceInsertionCalculator serviceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager, constraintManager); ServiceInsertionCalculator serviceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), constraintManager);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher(); JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
switcher.put(Shipment.class, shipmentInsertion); switcher.put(Shipment.class, shipmentInsertion);
@ -263,8 +263,6 @@ class CalculatorBuilder {
PenalyzeInsertionCostsWithPenaltyVehicle penalyzeInsertionCosts = new PenalyzeInsertionCostsWithPenaltyVehicle(switcher); PenalyzeInsertionCostsWithPenaltyVehicle penalyzeInsertionCosts = new PenalyzeInsertionCostsWithPenaltyVehicle(switcher);
// JobInsertionCostsCalculator standardServiceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager, constraintManager);
// ((ServiceInsertionCalculator) standardServiceInsertion).setNeighborhood(vrp.getNeighborhood());
CalculatorPlusListeners calcPlusListeners = new CalculatorPlusListeners(penalyzeInsertionCosts); CalculatorPlusListeners calcPlusListeners = new CalculatorPlusListeners(penalyzeInsertionCosts);
return calcPlusListeners; return calcPlusListeners;

View file

@ -16,10 +16,12 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.recreate; package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.ActivityInsertionCostsCalculator.ActivityInsertionCosts; import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint; import jsprit.core.problem.constraint.HardActivityStateLevelConstraint;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint.ConstraintsStatus; import jsprit.core.problem.constraint.HardActivityStateLevelConstraint.ConstraintsStatus;
import jsprit.core.problem.constraint.HardRouteStateLevelConstraint; import jsprit.core.problem.constraint.HardRouteStateLevelConstraint;
import jsprit.core.problem.constraint.SoftActivityConstraint;
import jsprit.core.problem.constraint.SoftRouteConstraint;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts; import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
@ -34,13 +36,15 @@ import jsprit.core.problem.solution.route.activity.TourActivityFactory;
import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl.NoVehicle; import jsprit.core.problem.vehicle.VehicleImpl.NoVehicle;
import jsprit.core.util.CalculationUtils; import jsprit.core.util.CalculationUtils;
import jsprit.core.util.Neighborhood;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/**
* Calculator that calculates the best insertion position for a {@link Service}.
*
* @author schroeder
*
*/
final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
private static final Logger logger = Logger.getLogger(ServiceInsertionCalculator.class); private static final Logger logger = Logger.getLogger(ServiceInsertionCalculator.class);
@ -49,32 +53,21 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
private HardActivityStateLevelConstraint hardActivityLevelConstraint; private HardActivityStateLevelConstraint hardActivityLevelConstraint;
private Neighborhood neighborhood = new Neighborhood() { private SoftRouteConstraint softRouteConstraint;
@Override private SoftActivityConstraint softActivityConstraint;
public boolean areNeighbors(String location1, String location2) {
return true;
}
};
private ActivityInsertionCostsCalculator activityInsertionCostsCalculator;
private VehicleRoutingTransportCosts transportCosts; private VehicleRoutingTransportCosts transportCosts;
private TourActivityFactory activityFactory; private TourActivityFactory activityFactory;
public void setNeighborhood(Neighborhood neighborhood) { public ServiceInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ConstraintManager constraintManager) {
this.neighborhood = neighborhood;
logger.info("initialise neighborhood " + neighborhood);
}
public ServiceInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ActivityInsertionCostsCalculator activityInsertionCostsCalculator, HardRouteStateLevelConstraint hardRouteLevelConstraint, HardActivityStateLevelConstraint hardActivityLevelConstraint) {
super(); super();
this.activityInsertionCostsCalculator = activityInsertionCostsCalculator;
this.hardRouteLevelConstraint = hardRouteLevelConstraint;
this.hardActivityLevelConstraint = hardActivityLevelConstraint;
this.transportCosts = routingCosts; this.transportCosts = routingCosts;
hardRouteLevelConstraint = constraintManager;
hardActivityLevelConstraint = constraintManager;
softActivityConstraint = constraintManager;
softRouteConstraint = constraintManager;
activityFactory = new DefaultTourActivityFactory(); activityFactory = new DefaultTourActivityFactory();
logger.info("initialise " + this); logger.info("initialise " + this);
} }
@ -100,7 +93,10 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
} }
double bestCost = bestKnownCosts; double bestCost = bestKnownCosts;
ActivityInsertionCosts bestMarginals = null;
//from job2insert induced costs at route level
double routeICosts = softRouteConstraint.getCosts(insertionContext);
Service service = (Service)jobToInsert; Service service = (Service)jobToInsert;
int insertionIndex = InsertionData.NO_INDEX; int insertionIndex = InsertionData.NO_INDEX;
@ -115,13 +111,12 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
int actIndex = 0; int actIndex = 0;
boolean loopBroken = false; boolean loopBroken = false;
for(TourActivity nextAct : currentRoute.getTourActivities().getActivities()){ for(TourActivity nextAct : currentRoute.getTourActivities().getActivities()){
if(neighborhood.areNeighbors(deliveryAct2Insert.getLocationId(), prevAct.getLocationId()) && neighborhood.areNeighbors(deliveryAct2Insert.getLocationId(), nextAct.getLocationId())){
ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime); ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
if(status.equals(ConstraintsStatus.FULFILLED)){ if(status.equals(ConstraintsStatus.FULFILLED)){
ActivityInsertionCosts mc = calculate(insertionContext, prevAct, nextAct, deliveryAct2Insert, prevActStartTime); //from job2insert induced costs at activity level
if(mc.getAdditionalCosts() < bestCost){ double activityICosts = softActivityConstraint.getCosts(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
bestCost = mc.getAdditionalCosts(); if(routeICosts + activityICosts < bestCost){
bestMarginals = mc; bestCost = routeICosts + activityICosts;
insertionIndex = actIndex; insertionIndex = actIndex;
} }
} }
@ -129,7 +124,6 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
loopBroken = true; loopBroken = true;
break; break;
} }
}
double nextActArrTime = prevActStartTime + transportCosts.getTransportTime(prevAct.getLocationId(), nextAct.getLocationId(), prevActStartTime, newDriver, newVehicle); double nextActArrTime = prevActStartTime + transportCosts.getTransportTime(prevAct.getLocationId(), nextAct.getLocationId(), prevActStartTime, newDriver, newVehicle);
double nextActEndTime = CalculationUtils.getActivityEndTime(nextActArrTime, nextAct); double nextActEndTime = CalculationUtils.getActivityEndTime(nextActArrTime, nextAct);
prevActStartTime = nextActEndTime; prevActStartTime = nextActEndTime;
@ -138,29 +132,21 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
} }
End nextAct = end; End nextAct = end;
if(!loopBroken){ if(!loopBroken){
if(neighborhood.areNeighbors(deliveryAct2Insert.getLocationId(), prevAct.getLocationId()) && neighborhood.areNeighbors(deliveryAct2Insert.getLocationId(), nextAct.getLocationId())){
ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime); ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
if(status.equals(ConstraintsStatus.FULFILLED)){ if(status.equals(ConstraintsStatus.FULFILLED)){
ActivityInsertionCosts mc = calculate(insertionContext, prevAct, nextAct, deliveryAct2Insert, prevActStartTime); double activityICosts = softActivityConstraint.getCosts(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
if(mc.getAdditionalCosts() < bestCost){ if(routeICosts + activityICosts < bestCost){
bestCost = mc.getAdditionalCosts(); bestCost = routeICosts + activityICosts;
bestMarginals = mc;
insertionIndex = actIndex; insertionIndex = actIndex;
} }
} }
} }
}
if(insertionIndex == InsertionData.NO_INDEX) { if(insertionIndex == InsertionData.NO_INDEX) {
return InsertionData.createEmptyInsertionData(); return InsertionData.createEmptyInsertionData();
} }
InsertionData insertionData = new InsertionData(bestCost, InsertionData.NO_INDEX, insertionIndex, newVehicle, newDriver); InsertionData insertionData = new InsertionData(bestCost, InsertionData.NO_INDEX, insertionIndex, newVehicle, newDriver);
insertionData.setVehicleDepartureTime(newVehicleDepartureTime); insertionData.setVehicleDepartureTime(newVehicleDepartureTime);
insertionData.setAdditionalTime(bestMarginals.getAdditionalTime());
return insertionData; return insertionData;
} }
public ActivityInsertionCosts calculate(JobInsertionContext iFacts, TourActivity prevAct, TourActivity nextAct, TourActivity newAct, double departureTimeAtPrevAct) {
return activityInsertionCostsCalculator.getCosts(iFacts, prevAct, nextAct, newAct, departureTimeAtPrevAct);
}
} }

View file

@ -0,0 +1,70 @@
package jsprit.core.problem.constraint;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.util.CalculationUtils;
/**
* Calculates additional transportation costs induced by inserting newAct.
*
* @author schroeder
*
*/
public class AdditionalTransportationCosts implements SoftActivityConstraint{
private VehicleRoutingTransportCosts routingCosts;
/**
* Constructs the calculator that calculates additional transportation costs induced by inserting new activity.
*
* <p>It is calculated at local level, i.e. the additional costs of inserting act_new between act_i and act_j is c(act_i,act_new,newVehicle)+c(act_new,act_j,newVehicle)-c(act_i,act_j,oldVehicle)
* <p>If newVehicle.isReturnToDepot == false then the additional costs of inserting act_new between act_i and end is c(act_i,act_new) [since act_new is then the new end-of-route]
*
* @param routingCosts
*/
public AdditionalTransportationCosts(VehicleRoutingTransportCosts routingCosts) {
super();
this.routingCosts = routingCosts;
}
/**
* Returns additional transportation costs induced by inserting newAct.
*
* <p>It is calculated at local level, i.e. the additional costs of inserting act_new between act_i and act_j is c(act_i,act_new,newVehicle)+c(act_new,act_j,newVehicle)-c(act_i,act_j,oldVehicle)
* <p>If newVehicle.isReturnToDepot == false then the additional costs of inserting act_new between act_i and end is c(act_i,act_new) [since act_new is then the new end-of-route]
*/
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct,TourActivity newAct, TourActivity nextAct, double depTimeAtPrevAct) {
double tp_costs_prevAct_newAct = routingCosts.getTransportCost(prevAct.getLocationId(), newAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
double tp_time_prevAct_newAct = routingCosts.getTransportTime(prevAct.getLocationId(), newAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
double newAct_arrTime = depTimeAtPrevAct + tp_time_prevAct_newAct;
double newAct_endTime = CalculationUtils.getActivityEndTime(newAct_arrTime, newAct);
//open routes
if(nextAct instanceof End){
if(!iFacts.getNewVehicle().isReturnToDepot()){
return tp_costs_prevAct_newAct;
}
}
double tp_costs_newAct_nextAct = routingCosts.getTransportCost(newAct.getLocationId(), nextAct.getLocationId(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct;
double oldCosts;
if(iFacts.getRoute().isEmpty()){
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocationId(), nextAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
oldCosts = tp_costs_prevAct_nextAct;
}
else{
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocationId(), nextAct.getLocationId(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
oldCosts = tp_costs_prevAct_nextAct;
}
double additionalCosts = totalCosts - oldCosts;
return additionalCosts;
}
}

View file

@ -14,7 +14,7 @@ import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class ConstraintManager implements HardActivityStateLevelConstraint, HardRouteStateLevelConstraint{ public class ConstraintManager implements HardActivityStateLevelConstraint, HardRouteStateLevelConstraint, SoftActivityConstraint, SoftRouteConstraint{
public static enum Priority { public static enum Priority {
CRITICAL, HIGH, LOW CRITICAL, HIGH, LOW
@ -26,6 +26,10 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
private HardRouteLevelConstraintManager routeLevelConstraintManager = new HardRouteLevelConstraintManager(); private HardRouteLevelConstraintManager routeLevelConstraintManager = new HardRouteLevelConstraintManager();
private SoftActivityConstraintManager softActivityConstraintManager = new SoftActivityConstraintManager();
private SoftRouteConstraintManager softRouteConstraintManager = new SoftRouteConstraintManager();
private VehicleRoutingProblem vrp; private VehicleRoutingProblem vrp;
private RouteAndActivityStateGetter stateManager; private RouteAndActivityStateGetter stateManager;
@ -56,6 +60,14 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
routeLevelConstraintManager.addConstraint((HardRouteStateLevelConstraint) c); routeLevelConstraintManager.addConstraint((HardRouteStateLevelConstraint) c);
constraintTypeKnown = true; constraintTypeKnown = true;
} }
if(c instanceof SoftRouteConstraint){
softRouteConstraintManager.addConstraint((SoftRouteConstraint)c);
constraintTypeKnown = true;
}
if(c instanceof SoftActivityConstraint){
softActivityConstraintManager.addConstraint((SoftActivityConstraint)c);
constraintTypeKnown = true;
}
if(!constraintTypeKnown){ if(!constraintTypeKnown){
log.warn("constraint " + c + " unknown thus ignores the constraint. currently, a constraint must implement either HardActivityStateLevelConstraint or HardRouteStateLevelConstraint"); log.warn("constraint " + c + " unknown thus ignores the constraint. currently, a constraint must implement either HardActivityStateLevelConstraint or HardRouteStateLevelConstraint");
} }
@ -82,6 +94,8 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
} }
} }
// public void add
public void addConstraint(HardActivityStateLevelConstraint actLevelConstraint, Priority priority){ public void addConstraint(HardActivityStateLevelConstraint actLevelConstraint, Priority priority){
actLevelConstraintManager.addConstraint(actLevelConstraint,priority); actLevelConstraintManager.addConstraint(actLevelConstraint,priority);
} }
@ -90,6 +104,14 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
routeLevelConstraintManager.addConstraint(routeLevelConstraint); routeLevelConstraintManager.addConstraint(routeLevelConstraint);
} }
public void addConstraint(SoftActivityConstraint softActivityConstraint){
softActivityConstraintManager.addConstraint(softActivityConstraint);
}
public void addConstraint(SoftRouteConstraint softRouteConstraint){
softRouteConstraintManager.addConstraint(softRouteConstraint);
}
@Override @Override
public boolean fulfilled(JobInsertionContext insertionContext) { public boolean fulfilled(JobInsertionContext insertionContext) {
return routeLevelConstraintManager.fulfilled(insertionContext); return routeLevelConstraintManager.fulfilled(insertionContext);
@ -104,7 +126,19 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
List<jsprit.core.problem.constraint.Constraint> constraints = new ArrayList<jsprit.core.problem.constraint.Constraint>(); List<jsprit.core.problem.constraint.Constraint> constraints = new ArrayList<jsprit.core.problem.constraint.Constraint>();
constraints.addAll(actLevelConstraintManager.getAllConstraints()); constraints.addAll(actLevelConstraintManager.getAllConstraints());
constraints.addAll(routeLevelConstraintManager.getConstraints()); constraints.addAll(routeLevelConstraintManager.getConstraints());
constraints.addAll(softActivityConstraintManager.getConstraints());
constraints.addAll(softRouteConstraintManager.getConstraints());
return Collections.unmodifiableCollection(constraints); return Collections.unmodifiableCollection(constraints);
} }
@Override
public double getCosts(JobInsertionContext insertionContext) {
return softRouteConstraintManager.getCosts(insertionContext);
}
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct,TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
return softActivityConstraintManager.getCosts(iFacts, prevAct, newAct, nextAct, prevActDepTime);
}
} }

View file

@ -0,0 +1,10 @@
package jsprit.core.problem.constraint;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.activity.TourActivity;
public interface SoftActivityConstraint extends SoftConstraint{
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime);
}

View file

@ -0,0 +1,29 @@
package jsprit.core.problem.constraint;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.activity.TourActivity;
class SoftActivityConstraintManager implements SoftActivityConstraint{
private Collection<SoftActivityConstraint> softConstraints = new ArrayList<SoftActivityConstraint>();
public void addConstraint(SoftActivityConstraint constraint){
softConstraints.add(constraint);
}
Collection<SoftActivityConstraint> getConstraints(){ return Collections.unmodifiableCollection(softConstraints); }
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct,TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
double sumCosts = 0.0;
for(SoftActivityConstraint c : softConstraints){
sumCosts += c.getCosts(iFacts, prevAct, newAct, nextAct, prevActDepTime);
}
return sumCosts;
}
}

View file

@ -0,0 +1,9 @@
package jsprit.core.problem.constraint;
import jsprit.core.problem.misc.JobInsertionContext;
public interface SoftRouteConstraint extends SoftConstraint{
public double getCosts(JobInsertionContext insertionContext);
}

View file

@ -0,0 +1,28 @@
package jsprit.core.problem.constraint;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import jsprit.core.problem.misc.JobInsertionContext;
class SoftRouteConstraintManager implements SoftRouteConstraint{
private Collection<SoftRouteConstraint> softConstraints = new ArrayList<SoftRouteConstraint>();
public void addConstraint(SoftRouteConstraint constraint){
softConstraints.add(constraint);
}
Collection<SoftRouteConstraint> getConstraints(){ return Collections.unmodifiableCollection(softConstraints); }
@Override
public double getCosts(JobInsertionContext insertionContext) {
double sumCosts = 0.0;
for(SoftRouteConstraint c : softConstraints){
sumCosts += c.getCosts(insertionContext);
}
return sumCosts;
}
}

View file

@ -32,6 +32,7 @@ import jsprit.core.algorithm.selector.SelectBest;
import jsprit.core.algorithm.state.StateManager; import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateVariableCosts; import jsprit.core.algorithm.state.UpdateVariableCosts;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.AdditionalTransportationCosts;
import jsprit.core.problem.constraint.ConstraintManager; import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.SolutionCostCalculator; import jsprit.core.problem.solution.SolutionCostCalculator;
@ -67,6 +68,7 @@ public class BuildCVRPAlgoFromScratch_IT {
ConstraintManager cManager = new ConstraintManager(vrp, stateManager); ConstraintManager cManager = new ConstraintManager(vrp, stateManager);
cManager.addLoadConstraint(); cManager.addLoadConstraint();
cManager.addTimeWindowConstraint(); cManager.addTimeWindowConstraint();
cManager.addConstraint(new AdditionalTransportationCosts(vrp.getTransportCosts()));
VehicleFleetManager fleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager(); VehicleFleetManager fleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
@ -103,10 +105,6 @@ public class BuildCVRPAlgoFromScratch_IT {
vra.addListener(stateManager); vra.addListener(stateManager);
vra.addListener(new RemoveEmptyVehicles(fleetManager)); vra.addListener(new RemoveEmptyVehicles(fleetManager));
// vra.getAlgorithmListeners().addListener(stateManager);
// vra.getSearchStrategyManager().addSearchStrategyModuleListener(stateManager);
// vra.getSearchStrategyManager().addSearchStrategyModuleListener(new RemoveEmptyVehicles(fleetManager));
VehicleRoutingProblemSolution iniSolution = new InsertionInitialSolutionFactory(bestInsertion, solutionCostCalculator).createSolution(vrp); VehicleRoutingProblemSolution iniSolution = new InsertionInitialSolutionFactory(bestInsertion, solutionCostCalculator).createSolution(vrp);
vra.addInitialSolution(iniSolution); vra.addInitialSolution(iniSolution);

View file

@ -136,7 +136,7 @@ public class ServiceInsertionAndLoadConstraintsTest {
stateManager.informInsertionStarts(Arrays.asList(route), null); stateManager.informInsertionStarts(Arrays.asList(route), null);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher(); JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager); ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager); ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
switcher.put(Pickup.class, serviceInsertionCalc); switcher.put(Pickup.class, serviceInsertionCalc);
switcher.put(Delivery.class, serviceInsertionCalc); switcher.put(Delivery.class, serviceInsertionCalc);

View file

@ -240,7 +240,7 @@ public class ShipmentInsertionCalculatorTest {
stateManager.informInsertionStarts(Arrays.asList(route), null); stateManager.informInsertionStarts(Arrays.asList(route), null);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher(); JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager); ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager); ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
switcher.put(Pickup.class, serviceInsertionCalc); switcher.put(Pickup.class, serviceInsertionCalc);
switcher.put(Shipment.class, insertionCalculator); switcher.put(Shipment.class, insertionCalculator);

View file

@ -27,6 +27,7 @@ import java.util.Collection;
import jsprit.core.algorithm.ExampleActivityCostFunction; import jsprit.core.algorithm.ExampleActivityCostFunction;
import jsprit.core.algorithm.state.StateManager; import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.AdditionalTransportationCosts;
import jsprit.core.problem.constraint.ConstraintManager; import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts; import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.DriverImpl; import jsprit.core.problem.driver.DriverImpl;
@ -165,11 +166,9 @@ public class TestCalculatesServiceInsertion {
ConstraintManager cManager = new ConstraintManager(vrp,states); ConstraintManager cManager = new ConstraintManager(vrp,states);
cManager.addLoadConstraint(); cManager.addLoadConstraint();
cManager.addTimeWindowConstraint(); cManager.addTimeWindowConstraint();
cManager.addConstraint(new AdditionalTransportationCosts(costs));
ExampleActivityCostFunction activityCosts = new ExampleActivityCostFunction(); serviceInsertion = new ServiceInsertionCalculator(costs, cManager);
serviceInsertion = new ServiceInsertionCalculator(costs, new LocalActivityInsertionCostsCalculator(costs, activityCosts), cManager, cManager);
// stateUpdater = new UpdateStates(states, costs, activityCosts); // stateUpdater = new UpdateStates(states, costs, activityCosts);

View file

@ -0,0 +1,219 @@
/*******************************************************************************
* Copyright (c) 2013 Stefan Schroeder.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/
package jsprit.examples;
import java.util.Collection;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.InsertionInitialSolutionFactory;
import jsprit.core.algorithm.RemoveEmptyVehicles;
import jsprit.core.algorithm.SearchStrategy;
import jsprit.core.algorithm.SearchStrategyManager;
import jsprit.core.algorithm.VariablePlusFixedSolutionCostCalculatorFactory;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.acceptor.GreedyAcceptance;
import jsprit.core.algorithm.module.RuinAndRecreateModule;
import jsprit.core.algorithm.recreate.BestInsertionBuilder;
import jsprit.core.algorithm.recreate.InsertionStrategy;
import jsprit.core.algorithm.ruin.RadialRuinStrategyFactory;
import jsprit.core.algorithm.ruin.RandomRuinStrategyFactory;
import jsprit.core.algorithm.ruin.RuinStrategy;
import jsprit.core.algorithm.ruin.distance.AvgServiceAndShipmentDistance;
import jsprit.core.algorithm.selector.SelectBest;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateVariableCosts;
import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.AdditionalTransportationCosts;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
import jsprit.core.problem.vehicle.VehicleFleetManager;
import jsprit.core.util.Solutions;
import jsprit.instance.reader.SolomonReader;
import jsprit.util.Examples;
public class BuildAlgorithmFromScratchWithHardAndSoftConstraints {
/**
* @param args
*/
public static void main(String[] args) {
/*
* some preparation - create output folder
*/
Examples.createOutputFolder();
/*
* Build the problem.
*
* But define a problem-builder first.
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
/*
* A solomonReader reads solomon-instance files, and stores the required information in the builder.
*/
new SolomonReader(vrpBuilder).read("input/C101_solomon.txt");
/*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
/*
* Build algorithm
*/
VehicleRoutingAlgorithm vra = buildAlgorithmFromScratch(vrp);
/*
* search solution
*/
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
/*
* print result
*/
SolutionPrinter.print(Solutions.bestOf(solutions));
}
private static VehicleRoutingAlgorithm buildAlgorithmFromScratch(VehicleRoutingProblem vrp) {
/*
* manages route and activity states.
*/
StateManager stateManager = new StateManager(vrp);
/*
* tells stateManager to update load states
*/
stateManager.updateLoadStates();
/*
* tells stateManager to update time-window states
*/
stateManager.updateTimeWindowStates();
/*
* stateManager.addStateUpdater(updater);
* lets you register your own stateUpdater
*/
/*
* updates variable costs once a vehicleRoute has changed (by removing or adding a customer)
*/
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
/*
* constructs a constraintManager that manages the various hardConstraints (and soon also softConstraints)
*/
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
/*
* tells constraintManager to add timeWindowConstraints
*/
constraintManager.addTimeWindowConstraint();
/*
* tells constraintManager to add loadConstraints
*/
constraintManager.addLoadConstraint();
/*
* add an arbitrary number of hardConstraints by
* constraintManager.addConstraint(...)
*/
constraintManager.addConstraint(new AdditionalTransportationCosts(vrp.getTransportCosts()));
/*
* define a fleetManager, here infinite vehicles can be used
*/
VehicleFleetManager fleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
/*
* define ruin-and-recreate strategies
*
*/
/*
* first, define an insertion-strategy, i.e. bestInsertion
*/
BestInsertionBuilder iBuilder = new BestInsertionBuilder(vrp, fleetManager, stateManager, constraintManager);
/*
* no need to set further options
*/
InsertionStrategy iStrategy = iBuilder.build();
/*
* second, define random-ruin that ruins 50-percent of the selected solution
*/
RuinStrategy randomRuin = new RandomRuinStrategyFactory(0.5).createStrategy(vrp);
/*
* third, define radial-ruin that ruins 30-percent of the selected solution
* the second para defines the distance between two jobs.
*/
RuinStrategy radialRuin = new RadialRuinStrategyFactory(0.3, new AvgServiceAndShipmentDistance(vrp.getTransportCosts())).createStrategy(vrp);
/*
* now define a strategy
*/
/*
* but before define how a generated solution is evaluated
* here: the VariablePlusFixed.... comes out of the box and it does what its name suggests
*/
SolutionCostCalculator solutionCostCalculator = new VariablePlusFixedSolutionCostCalculatorFactory(stateManager).createCalculator();
SearchStrategy firstStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator);
firstStrategy.addModule(new RuinAndRecreateModule("randomRuinAndBestInsertion", iStrategy, randomRuin));
SearchStrategy secondStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator);
secondStrategy.addModule(new RuinAndRecreateModule("radialRuinAndBestInsertion", iStrategy, radialRuin));
/*
* put both strategies together, each with the prob of 0.5 to be selected
*/
SearchStrategyManager searchStrategyManager = new SearchStrategyManager();
searchStrategyManager.addStrategy(firstStrategy, 0.5);
searchStrategyManager.addStrategy(secondStrategy, 0.5);
/*
* construct the algorithm
*/
VehicleRoutingAlgorithm vra = new VehicleRoutingAlgorithm(vrp, searchStrategyManager);
//do not forgett to add the stateManager listening to the algorithm-stages
vra.addListener(stateManager);
//remove empty vehicles after insertion has finished
vra.addListener(new RemoveEmptyVehicles(fleetManager));
/*
* Do not forget to add an initial solution by vra.addInitialSolution(solution);
* or
*/
vra.addInitialSolution(new InsertionInitialSolutionFactory(iStrategy, solutionCostCalculator).createSolution(vrp));
/*
* define the nIterations (by default nIteration=100)
*/
vra.setNuOfIterations(1000);
/*
* optionally define a premature termination criterion (by default: not criterion is set)
*/
vra.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(100));
return vra;
}
}