mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add state-updaters and constraints
This commit is contained in:
parent
491d7a9738
commit
2c0366135c
9 changed files with 170 additions and 142 deletions
|
|
@ -46,8 +46,9 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
|
|||
UpdateLoads updateLoads = new UpdateLoads(stateManager);
|
||||
stateManager.addActivityVisitor(updateLoads);
|
||||
stateManager.addListener(updateLoads);
|
||||
stateManager.addActivityVisitor(new UpdateFuturePickups(stateManager));
|
||||
stateManager.addActivityVisitor(new UpdateOccuredDeliveries(stateManager));
|
||||
stateManager.addActivityVisitor(new UpdateMaxLoad(stateManager));
|
||||
stateManager.addActivityVisitor(new UpdateMaxLoad_(stateManager));
|
||||
stateManager.addActivityVisitor(new UpdatePrevMaxLoad(stateManager));
|
||||
loadConstraintsSet=true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,18 @@
|
|||
package algorithms;
|
||||
|
||||
import basics.route.DeliveryActivity;
|
||||
import basics.route.PickupActivity;
|
||||
import basics.route.DeliverService;
|
||||
import basics.route.PickupService;
|
||||
import basics.route.ServiceActivity;
|
||||
import basics.route.Start;
|
||||
import basics.route.TourActivity;
|
||||
|
||||
public class ServiceBackhaulConstraint implements HardActivityStateLevelConstraint {
|
||||
|
||||
@Override
|
||||
public ConstraintsStatus fulfilled(InsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
|
||||
if(newAct instanceof PickupActivity && nextAct instanceof DeliveryActivity){ return ConstraintsStatus.NOT_FULFILLED; }
|
||||
if(newAct instanceof ServiceActivity && nextAct instanceof DeliveryActivity){ return ConstraintsStatus.NOT_FULFILLED; }
|
||||
if(newAct instanceof DeliveryActivity && prevAct instanceof PickupActivity){ return ConstraintsStatus.NOT_FULFILLED; }
|
||||
if(newAct instanceof DeliveryActivity && prevAct instanceof ServiceActivity){ return ConstraintsStatus.NOT_FULFILLED; }
|
||||
if(newAct instanceof PickupService && nextAct instanceof DeliverService){ return ConstraintsStatus.NOT_FULFILLED; }
|
||||
if(newAct instanceof ServiceActivity && nextAct instanceof DeliverService){ return ConstraintsStatus.NOT_FULFILLED; }
|
||||
if(newAct instanceof DeliverService && prevAct instanceof PickupService){ return ConstraintsStatus.NOT_FULFILLED_BREAK; }
|
||||
if(newAct instanceof DeliverService && prevAct instanceof ServiceActivity){ return ConstraintsStatus.NOT_FULFILLED_BREAK; }
|
||||
return ConstraintsStatus.FULFILLED;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
package algorithms;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import basics.route.DeliverService;
|
||||
import basics.route.PickupService;
|
||||
import basics.route.ServiceActivity;
|
||||
import basics.route.Start;
|
||||
import basics.route.TourActivity;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
/**
|
||||
* Ensures load constraint for inserting ServiceActivity.
|
||||
|
|
@ -17,6 +20,8 @@ import basics.route.TourActivity;
|
|||
*/
|
||||
class ServiceLoadActivityLevelConstraint implements HardActivityStateLevelConstraint {
|
||||
|
||||
private static Logger log = Logger.getLogger(ServiceLoadActivityLevelConstraint.class);
|
||||
|
||||
private StateGetter stateManager;
|
||||
|
||||
public ServiceLoadActivityLevelConstraint(StateGetter stateManager) {
|
||||
|
|
@ -26,31 +31,31 @@ class ServiceLoadActivityLevelConstraint implements HardActivityStateLevelConstr
|
|||
|
||||
@Override
|
||||
public ConstraintsStatus fulfilled(InsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
|
||||
int loadAtPrevAct;
|
||||
int futurePicks;
|
||||
int pastDeliveries;
|
||||
int futureMaxLoad;
|
||||
int prevMaxLoad;
|
||||
if(prevAct instanceof Start){
|
||||
loadAtPrevAct = (int)stateManager.getRouteState(iFacts.getRoute(), StateFactory.LOAD_AT_BEGINNING).toDouble();
|
||||
futurePicks = (int)stateManager.getRouteState(iFacts.getRoute(), StateFactory.LOAD_AT_END).toDouble();
|
||||
pastDeliveries = 0;
|
||||
futureMaxLoad = (int)stateManager.getRouteState(iFacts.getRoute(), StateFactory.MAXLOAD).toDouble();
|
||||
prevMaxLoad = (int)stateManager.getRouteState(iFacts.getRoute(), StateFactory.LOAD_AT_BEGINNING).toDouble();
|
||||
}
|
||||
else{
|
||||
loadAtPrevAct = (int) stateManager.getActivityState(prevAct, StateFactory.LOAD).toDouble();
|
||||
futurePicks = (int) stateManager.getActivityState(prevAct, StateFactory.FUTURE_PICKS).toDouble();
|
||||
pastDeliveries = (int) stateManager.getActivityState(prevAct, StateFactory.PAST_DELIVERIES).toDouble();
|
||||
futureMaxLoad = (int) stateManager.getActivityState(prevAct, StateFactory.FUTURE_PICKS).toDouble();
|
||||
prevMaxLoad = (int) stateManager.getActivityState(prevAct, StateFactory.PAST_DELIVERIES).toDouble();
|
||||
|
||||
}
|
||||
if(newAct instanceof PickupService || newAct instanceof ServiceActivity){
|
||||
if(loadAtPrevAct + newAct.getCapacityDemand() + futurePicks > iFacts.getNewVehicle().getCapacity()){
|
||||
if(newAct.getCapacityDemand() + futureMaxLoad > iFacts.getNewVehicle().getCapacity()){
|
||||
// log.debug("insertionOf("+newAct+").BETWEEN("+prevAct+").AND("+nextAct+")=NOT_POSSIBLE");
|
||||
return ConstraintsStatus.NOT_FULFILLED;
|
||||
}
|
||||
}
|
||||
if(newAct instanceof DeliverService){
|
||||
if(loadAtPrevAct + Math.abs(newAct.getCapacityDemand()) + pastDeliveries > iFacts.getNewVehicle().getCapacity()){
|
||||
return ConstraintsStatus.NOT_FULFILLED;
|
||||
if(Math.abs(newAct.getCapacityDemand()) + prevMaxLoad > iFacts.getNewVehicle().getCapacity()){
|
||||
// log.debug("insertionOf("+newAct+").BETWEEN("+prevAct+").AND("+nextAct+")=NOT_POSSIBLE[break=neverBePossibleAnymore]");
|
||||
return ConstraintsStatus.NOT_FULFILLED_BREAK;
|
||||
}
|
||||
|
||||
}
|
||||
// log.debug("insertionOf("+newAct+").BETWEEN("+prevAct+").AND("+nextAct+")=POSSIBLE");
|
||||
return ConstraintsStatus.FULFILLED;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
package algorithms;
|
||||
|
||||
import basics.route.PickupService;
|
||||
import basics.route.ReverseActivityVisitor;
|
||||
import basics.route.ServiceActivity;
|
||||
import basics.route.TourActivity;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
class UpdateFuturePickups implements ReverseActivityVisitor, StateUpdater {
|
||||
private StateManager stateManager;
|
||||
private int futurePicks = 0;
|
||||
private VehicleRoute route;
|
||||
|
||||
public UpdateFuturePickups(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
this.route = route;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TourActivity act) {
|
||||
stateManager.putActivityState(act, StateFactory.FUTURE_PICKS, StateFactory.createState(futurePicks));
|
||||
if(act instanceof PickupService || act instanceof ServiceActivity){
|
||||
futurePicks += act.getCapacityDemand();
|
||||
}
|
||||
assert futurePicks <= route.getVehicle().getCapacity() : "sum of pickups must not be > vehicleCap";
|
||||
assert futurePicks >= 0 : "sum of pickups must not < 0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
futurePicks = 0;
|
||||
route = null;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@ package algorithms;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import basics.Delivery;
|
||||
import basics.Job;
|
||||
import basics.Pickup;
|
||||
|
|
@ -27,6 +29,7 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
|
|||
private StateManager stateManager;
|
||||
private int currentLoad = 0;
|
||||
private VehicleRoute route;
|
||||
private static Logger log = Logger.getLogger(UpdateLoads.class);
|
||||
|
||||
/**
|
||||
* Updates load at activity level.
|
||||
|
|
@ -89,6 +92,8 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
|
|||
|
||||
@Override
|
||||
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
|
||||
// log.debug("insert("+job2insert+").into("+inRoute+")");
|
||||
// log(inRoute);
|
||||
if(job2insert instanceof Delivery){
|
||||
int loadAtDepot = (int) stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING).toDouble();
|
||||
// log.info("loadAtDepot="+loadAtDepot);
|
||||
|
|
@ -101,4 +106,13 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
|
|||
}
|
||||
}
|
||||
|
||||
// private void log(VehicleRoute inRoute) {
|
||||
// log.debug(inRoute.getStart());
|
||||
// for(TourActivity act : inRoute.getTourActivities().getActivities()){
|
||||
// log.debug(act);
|
||||
// }
|
||||
// log.debug(inRoute.getEnd());
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
@ -1,67 +1,44 @@
|
|||
package algorithms;
|
||||
|
||||
import basics.route.ActivityVisitor;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import basics.route.ReverseActivityVisitor;
|
||||
import basics.route.TourActivity;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
/**
|
||||
* Updates load at activity level.
|
||||
*
|
||||
* <p>Note that this assumes that StateTypes.LOAD_AT_DEPOT is already updated, i.e. it starts by setting loadAtDepot to StateTypes.LOAD_AT_DEPOT.
|
||||
* If StateTypes.LOAD_AT_DEPOT is not set, it starts with 0 load at depot.
|
||||
*
|
||||
* <p>Thus it DEPENDS on StateTypes.LOAD_AT_DEPOT
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
class UpdateMaxLoad implements ActivityVisitor, StateUpdater {
|
||||
class UpdateMaxLoad implements ReverseActivityVisitor, StateUpdater {
|
||||
private static Logger log = Logger.getLogger(UpdateMaxLoad.class);
|
||||
private StateManager stateManager;
|
||||
private int currentLoad = 0;
|
||||
private VehicleRoute route;
|
||||
private int maxLoad = 0;
|
||||
private double maxLoad;
|
||||
private double currLoad;
|
||||
|
||||
/**
|
||||
* Updates load at activity level.
|
||||
*
|
||||
* <p>Note that this assumes that StateTypes.LOAD_AT_DEPOT is already updated, i.e. it starts by setting loadAtDepot to StateTypes.LOAD_AT_DEPOT.
|
||||
* If StateTypes.LOAD_AT_DEPOT is not set, it starts with 0 load at depot.
|
||||
*
|
||||
* <p>Thus it DEPENDS on StateTypes.LOAD_AT_DEPOT
|
||||
*
|
||||
*
|
||||
*
|
||||
* <p>The loads can be retrieved by <br>
|
||||
* <code>stateManager.getActivityState(activity,StateTypes.LOAD);</code>
|
||||
*
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
public UpdateMaxLoad(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
currentLoad = (int) stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING).toDouble();
|
||||
maxLoad = currentLoad;
|
||||
this.route = route;
|
||||
maxLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_END).toDouble();
|
||||
// currLoad = maxLoad;
|
||||
// log.debug("maxLoad@end="+maxLoad);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TourActivity act) {
|
||||
currentLoad += act.getCapacityDemand();
|
||||
maxLoad = Math.max(maxLoad, currentLoad);
|
||||
assert currentLoad <= route.getVehicle().getCapacity() : "currentLoad at activity must not be > vehicleCapacity";
|
||||
assert currentLoad >= 0 : "currentLoad at act must not be < 0";
|
||||
maxLoad = Math.max(maxLoad, stateManager.getActivityState(act, StateFactory.LOAD).toDouble());
|
||||
// currLoad -= act.getCapacityDemand();
|
||||
// log.debug("maxLoad@"+act+"="+maxLoad);
|
||||
stateManager.putActivityState(act, StateFactory.FUTURE_PICKS, StateFactory.createState(maxLoad));
|
||||
assert maxLoad <= route.getVehicle().getCapacity() : "maxLoad can never be bigger than vehicleCap";
|
||||
assert maxLoad >= 0 : "maxLoad can never be smaller than 0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
stateManager.putRouteState(route, StateFactory.MAXLOAD, StateFactory.createState(maxLoad));
|
||||
currentLoad = 0;
|
||||
maxLoad = 0;
|
||||
// stateManager.putRouteState(route, StateFactory.MAXLOAD, StateFactory.createState(maxLoad));
|
||||
// log.debug("maxLoad@start="+maxLoad);
|
||||
}
|
||||
}
|
||||
67
jsprit-core/src/main/java/algorithms/UpdateMaxLoad_.java
Normal file
67
jsprit-core/src/main/java/algorithms/UpdateMaxLoad_.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package algorithms;
|
||||
|
||||
import basics.route.ActivityVisitor;
|
||||
import basics.route.TourActivity;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
/**
|
||||
* Updates load at activity level.
|
||||
*
|
||||
* <p>Note that this assumes that StateTypes.LOAD_AT_DEPOT is already updated, i.e. it starts by setting loadAtDepot to StateTypes.LOAD_AT_DEPOT.
|
||||
* If StateTypes.LOAD_AT_DEPOT is not set, it starts with 0 load at depot.
|
||||
*
|
||||
* <p>Thus it DEPENDS on StateTypes.LOAD_AT_DEPOT
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
class UpdateMaxLoad_ implements ActivityVisitor, StateUpdater {
|
||||
private StateManager stateManager;
|
||||
private int currentLoad = 0;
|
||||
private VehicleRoute route;
|
||||
private int maxLoad = 0;
|
||||
|
||||
/**
|
||||
* Updates load at activity level.
|
||||
*
|
||||
* <p>Note that this assumes that StateTypes.LOAD_AT_DEPOT is already updated, i.e. it starts by setting loadAtDepot to StateTypes.LOAD_AT_DEPOT.
|
||||
* If StateTypes.LOAD_AT_DEPOT is not set, it starts with 0 load at depot.
|
||||
*
|
||||
* <p>Thus it DEPENDS on StateTypes.LOAD_AT_DEPOT
|
||||
*
|
||||
*
|
||||
*
|
||||
* <p>The loads can be retrieved by <br>
|
||||
* <code>stateManager.getActivityState(activity,StateTypes.LOAD);</code>
|
||||
*
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
public UpdateMaxLoad_(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
currentLoad = (int) stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING).toDouble();
|
||||
maxLoad = currentLoad;
|
||||
this.route = route;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TourActivity act) {
|
||||
currentLoad += act.getCapacityDemand();
|
||||
maxLoad = Math.max(maxLoad, currentLoad);
|
||||
assert currentLoad <= route.getVehicle().getCapacity() : "currentLoad at activity must not be > vehicleCapacity";
|
||||
assert currentLoad >= 0 : "currentLoad at act must not be < 0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
stateManager.putRouteState(route, StateFactory.MAXLOAD, StateFactory.createState(maxLoad));
|
||||
currentLoad = 0;
|
||||
maxLoad = 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
package algorithms;
|
||||
|
||||
import basics.route.ActivityVisitor;
|
||||
import basics.route.DeliverService;
|
||||
import basics.route.TourActivity;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
class UpdateOccuredDeliveries implements ActivityVisitor, StateUpdater {
|
||||
private StateManager stateManager;
|
||||
private int deliveries = 0;
|
||||
private VehicleRoute route;
|
||||
|
||||
public UpdateOccuredDeliveries(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
this.route = route;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TourActivity act) {
|
||||
if(act instanceof DeliverService){
|
||||
deliveries += Math.abs(act.getCapacityDemand());
|
||||
}
|
||||
stateManager.putActivityState(act, StateFactory.PAST_DELIVERIES, StateFactory.createState(deliveries));
|
||||
assert deliveries >= 0 : "deliveries < 0";
|
||||
assert deliveries <= route.getVehicle().getCapacity() : "deliveries > vehicleCap";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
deliveries = 0;
|
||||
route = null;
|
||||
}
|
||||
}
|
||||
42
jsprit-core/src/main/java/algorithms/UpdatePrevMaxLoad.java
Normal file
42
jsprit-core/src/main/java/algorithms/UpdatePrevMaxLoad.java
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package algorithms;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import basics.route.ActivityVisitor;
|
||||
import basics.route.TourActivity;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
class UpdatePrevMaxLoad implements ActivityVisitor, StateUpdater {
|
||||
private static Logger log = Logger.getLogger(UpdatePrevMaxLoad.class);
|
||||
private StateManager stateManager;
|
||||
private VehicleRoute route;
|
||||
private double currLoad;
|
||||
private double prevMaxLoad;
|
||||
|
||||
public UpdatePrevMaxLoad(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
this.route = route;
|
||||
currLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING).toDouble();
|
||||
prevMaxLoad = currLoad;
|
||||
// log.debug("prevMaxLoad@start="+prevMaxLoad);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TourActivity act) {
|
||||
prevMaxLoad = Math.max(prevMaxLoad, stateManager.getActivityState(act, StateFactory.LOAD).toDouble());
|
||||
// log.debug("prevMaxLoad@"+act+"="+prevMaxLoad);
|
||||
stateManager.putActivityState(act, StateFactory.PAST_DELIVERIES, StateFactory.createState(prevMaxLoad));
|
||||
assert prevMaxLoad >= 0 : "maxLoad can never be smaller than 0";
|
||||
assert prevMaxLoad <= route.getVehicle().getCapacity() : "maxLoad can never be bigger than vehicleCap";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
// log.debug("prevMaxLoad@end="+prevMaxLoad);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue