From 2c0366135cf47efe41ceb7c6e643a3afe0725912 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Sun, 17 Nov 2013 17:44:14 +0100 Subject: [PATCH] add state-updaters and constraints --- .../java/algorithms/ConstraintManager.java | 5 +- .../algorithms/ServiceBackhaulConstraint.java | 13 ++-- .../ServiceLoadActivityLevelConstraint.java | 33 +++++---- .../java/algorithms/UpdateFuturePickups.java | 39 ----------- .../src/main/java/algorithms/UpdateLoads.java | 14 ++++ .../main/java/algorithms/UpdateMaxLoad.java | 61 ++++++----------- .../main/java/algorithms/UpdateMaxLoad_.java | 67 +++++++++++++++++++ .../algorithms/UpdateOccuredDeliveries.java | 38 ----------- .../java/algorithms/UpdatePrevMaxLoad.java | 42 ++++++++++++ 9 files changed, 170 insertions(+), 142 deletions(-) delete mode 100644 jsprit-core/src/main/java/algorithms/UpdateFuturePickups.java create mode 100644 jsprit-core/src/main/java/algorithms/UpdateMaxLoad_.java delete mode 100644 jsprit-core/src/main/java/algorithms/UpdateOccuredDeliveries.java create mode 100644 jsprit-core/src/main/java/algorithms/UpdatePrevMaxLoad.java diff --git a/jsprit-core/src/main/java/algorithms/ConstraintManager.java b/jsprit-core/src/main/java/algorithms/ConstraintManager.java index 1bc7c3b9..e13be088 100644 --- a/jsprit-core/src/main/java/algorithms/ConstraintManager.java +++ b/jsprit-core/src/main/java/algorithms/ConstraintManager.java @@ -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; } } diff --git a/jsprit-core/src/main/java/algorithms/ServiceBackhaulConstraint.java b/jsprit-core/src/main/java/algorithms/ServiceBackhaulConstraint.java index 548c6395..7ebf7669 100644 --- a/jsprit-core/src/main/java/algorithms/ServiceBackhaulConstraint.java +++ b/jsprit-core/src/main/java/algorithms/ServiceBackhaulConstraint.java @@ -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; } diff --git a/jsprit-core/src/main/java/algorithms/ServiceLoadActivityLevelConstraint.java b/jsprit-core/src/main/java/algorithms/ServiceLoadActivityLevelConstraint.java index 5ce70594..9e23f68c 100644 --- a/jsprit-core/src/main/java/algorithms/ServiceLoadActivityLevelConstraint.java +++ b/jsprit-core/src/main/java/algorithms/ServiceLoadActivityLevelConstraint.java @@ -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; - } - + } } \ No newline at end of file diff --git a/jsprit-core/src/main/java/algorithms/UpdateFuturePickups.java b/jsprit-core/src/main/java/algorithms/UpdateFuturePickups.java deleted file mode 100644 index fa3d0850..00000000 --- a/jsprit-core/src/main/java/algorithms/UpdateFuturePickups.java +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/jsprit-core/src/main/java/algorithms/UpdateLoads.java b/jsprit-core/src/main/java/algorithms/UpdateLoads.java index 9b17f35b..ebd6076d 100644 --- a/jsprit-core/src/main/java/algorithms/UpdateLoads.java +++ b/jsprit-core/src/main/java/algorithms/UpdateLoads.java @@ -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()); +// +// } + } \ No newline at end of file diff --git a/jsprit-core/src/main/java/algorithms/UpdateMaxLoad.java b/jsprit-core/src/main/java/algorithms/UpdateMaxLoad.java index 9bd3b800..7f33bf3f 100644 --- a/jsprit-core/src/main/java/algorithms/UpdateMaxLoad.java +++ b/jsprit-core/src/main/java/algorithms/UpdateMaxLoad.java @@ -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. - * - *

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. - * - *

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. - * - *

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. - * - *

Thus it DEPENDS on StateTypes.LOAD_AT_DEPOT - * - * - * - *

The loads can be retrieved by
- * stateManager.getActivityState(activity,StateTypes.LOAD); - * - * - * @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); } } \ No newline at end of file diff --git a/jsprit-core/src/main/java/algorithms/UpdateMaxLoad_.java b/jsprit-core/src/main/java/algorithms/UpdateMaxLoad_.java new file mode 100644 index 00000000..382d4811 --- /dev/null +++ b/jsprit-core/src/main/java/algorithms/UpdateMaxLoad_.java @@ -0,0 +1,67 @@ +package algorithms; + +import basics.route.ActivityVisitor; +import basics.route.TourActivity; +import basics.route.VehicleRoute; + +/** + * Updates load at activity level. + * + *

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. + * + *

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. + * + *

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. + * + *

Thus it DEPENDS on StateTypes.LOAD_AT_DEPOT + * + * + * + *

The loads can be retrieved by
+ * stateManager.getActivityState(activity,StateTypes.LOAD); + * + * + * @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; + } +} \ No newline at end of file diff --git a/jsprit-core/src/main/java/algorithms/UpdateOccuredDeliveries.java b/jsprit-core/src/main/java/algorithms/UpdateOccuredDeliveries.java deleted file mode 100644 index d4022c4f..00000000 --- a/jsprit-core/src/main/java/algorithms/UpdateOccuredDeliveries.java +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/jsprit-core/src/main/java/algorithms/UpdatePrevMaxLoad.java b/jsprit-core/src/main/java/algorithms/UpdatePrevMaxLoad.java new file mode 100644 index 00000000..1536f1bb --- /dev/null +++ b/jsprit-core/src/main/java/algorithms/UpdatePrevMaxLoad.java @@ -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); + } +} \ No newline at end of file