1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00
This commit is contained in:
oblonski 2014-09-17 20:47:58 +02:00
parent 800e03d48c
commit f961fa8971
7 changed files with 142 additions and 51 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014 Stefan Schroeder.
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -8,13 +8,11 @@
*
* 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.
* 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.core.algorithm;
@ -22,9 +20,7 @@ import jsprit.core.algorithm.io.AlgorithmConfig;
import jsprit.core.algorithm.io.AlgorithmConfigXmlReader;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateActivityTimes;
import jsprit.core.algorithm.state.UpdateEndLocationIfRouteIsOpen;
import jsprit.core.algorithm.state.UpdateVariableCosts;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.solution.SolutionCostCalculator;
@ -162,8 +158,7 @@ public class VehicleRoutingAlgorithmBuilder {
//add core updater
stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen());
// stateManager.addStateUpdater(new OpenRouteStateVerifier());
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts()));
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
if(addCoreConstraints){
constraintManager.addLoadConstraint();
constraintManager.addTimeWindowConstraint();

View file

@ -54,6 +54,7 @@ import jsprit.core.problem.vehicle.FiniteFleetManagerFactory;
import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleFleetManager;
import jsprit.core.util.ActivityTimeTracker;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.logging.log4j.LogManager;
@ -546,6 +547,7 @@ public class VehicleRoutingAlgorithms {
switchAllowed = Boolean.parseBoolean(switchString);
}
else switchAllowed = true;
ActivityTimeTracker.ActivityPolicy activityPolicy;
if(stateManager.timeWindowUpdateIsActivated()){
UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows(stateManager,vrp.getTransportCosts());
timeWindowUpdater.setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
@ -562,7 +564,13 @@ public class VehicleRoutingAlgorithms {
});
stateManager.addStateUpdater(timeWindowUpdater);
activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;
}
else{
activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED;
}
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(),activityPolicy));
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy));
SolutionCostCalculator costCalculator;
if(solutionCostCalculator==null) costCalculator = getDefaultCostCalculator(stateManager);

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014 Stefan Schroeder.
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -8,13 +8,11 @@
*
* 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.
* 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.core.algorithm.state;
@ -35,7 +33,9 @@ import jsprit.core.util.ActivityTimeTracker;
*/
public class UpdateActivityTimes implements ActivityVisitor, StateUpdater{
private ActivityTimeTracker timeTracker;
private ActivityTimeTracker timeTracker;
private VehicleRoute route;
@ -48,7 +48,7 @@ public class UpdateActivityTimes implements ActivityVisitor, StateUpdater{
* <code>activity.getArrTime()</code> and
* <code>activity.getEndTime()</code>
*
* @author stefan
*
*
*/
public UpdateActivityTimes(ForwardTransportTime transportTime) {
@ -56,6 +56,10 @@ public class UpdateActivityTimes implements ActivityVisitor, StateUpdater{
timeTracker = new ActivityTimeTracker(transportTime);
}
public UpdateActivityTimes(ForwardTransportTime transportTime, ActivityTimeTracker.ActivityPolicy activityPolicy){
timeTracker = new ActivityTimeTracker(transportTime,activityPolicy);
}
@Override
public void begin(VehicleRoute route) {
timeTracker.begin(route);

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014 Stefan Schroeder.
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -8,13 +8,11 @@
*
* 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.
* 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.core.algorithm.state;
@ -71,7 +69,14 @@ public class UpdateVariableCosts implements ActivityVisitor,StateUpdater{
timeTracker = new ActivityTimeTracker(transportCost);
}
@Override
public UpdateVariableCosts(VehicleRoutingActivityCosts activityCosts, VehicleRoutingTransportCosts transportCosts, StateManager stateManager, ActivityTimeTracker.ActivityPolicy activityPolicy) {
this.activityCost = activityCosts;
this.transportCost = transportCosts;
this.states = stateManager;
timeTracker = new ActivityTimeTracker(transportCosts, activityPolicy);
}
@Override
public void begin(VehicleRoute route) {
vehicleRoute = route;
timeTracker.begin(route);

View file

@ -168,4 +168,5 @@ public class ConstraintManager implements HardActivityConstraint, HardRouteConst
return softActivityConstraintManager.getCosts(iFacts, prevAct, newAct, nextAct, prevActDepTime);
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -23,6 +23,12 @@ import jsprit.core.problem.solution.route.activity.TourActivity;
public class ActivityTimeTracker implements ActivityVisitor{
public static enum ActivityPolicy {
AS_SOON_AS_TIME_WINDOW_OPENS, AS_SOON_AS_ARRIVED
}
private ForwardTransportTime transportTime;
private TourActivity prevAct = null;
@ -37,11 +43,19 @@ public class ActivityTimeTracker implements ActivityVisitor{
private double actEndTime;
private ActivityPolicy activityPolicy = ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;
public ActivityTimeTracker(ForwardTransportTime transportTime) {
super();
this.transportTime = transportTime;
}
public ActivityTimeTracker(ForwardTransportTime transportTime, ActivityPolicy activityPolicy) {
super();
this.transportTime = transportTime;
this.activityPolicy = activityPolicy;
}
public double getActArrTime(){
return actArrTime;
}
@ -54,12 +68,9 @@ public class ActivityTimeTracker implements ActivityVisitor{
public void begin(VehicleRoute route) {
prevAct = route.getStart();
startAtPrevAct = prevAct.getEndTime();
actEndTime = startAtPrevAct;
this.route = route;
beginFirst = true;
actEndTime = startAtPrevAct;
this.route = route;
beginFirst = true;
}
@Override
@ -69,8 +80,16 @@ public class ActivityTimeTracker implements ActivityVisitor{
double arrivalTimeAtCurrAct = startAtPrevAct + transportTime;
actArrTime = arrivalTimeAtCurrAct;
double operationStartTime;
if(activityPolicy.equals(ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS)){
operationStartTime = Math.max(activity.getTheoreticalEarliestOperationStartTime(), arrivalTimeAtCurrAct);
}
else if(activityPolicy.equals(ActivityPolicy.AS_SOON_AS_ARRIVED)){
operationStartTime = actArrTime;
}
else operationStartTime = actArrTime;
double operationStartTime = Math.max(activity.getTheoreticalEarliestOperationStartTime(), arrivalTimeAtCurrAct);
double operationEndTime = operationStartTime + activity.getOperationTime();
actEndTime = operationEndTime;

View file

@ -18,7 +18,9 @@
package jsprit.core.algorithm;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
@ -48,4 +50,61 @@ public class DeactivateTimeWindowsTest {
VehicleRoute route = Solutions.bestOf(solutions).getRoutes().iterator().next();
Assert.assertEquals(20., route.getActivities().get(0).getEndTime(), 0.01);
}
@Test
public void whenNotActivatingViaStateManager_activityTimesShouldConsiderTimeWindows(){
Service service = Service.Builder.newInstance("s").setCoord(Coordinate.newInstance(20, 0))
.setTimeWindow(TimeWindow.newInstance(40,50)).build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(0,0)).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(service).addVehicle(vehicle).build();
VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(vrp,"src/test/resources/algorithmConfig.xml");
vraBuilder.addDefaultCostCalculators();
StateManager stateManager = new StateManager(vrp);
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
vraBuilder.setStateAndConstraintManager(stateManager,constraintManager);
VehicleRoutingAlgorithm vra = vraBuilder.build(); //this should ignore any constraints
vra.setMaxIterations(10);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoute route = Solutions.bestOf(solutions).getRoutes().iterator().next();
Assert.assertEquals(20., route.getActivities().get(0).getEndTime(), 0.01);
}
@Test
public void activityTimesShouldConsiderTimeWindows(){
Service service = Service.Builder.newInstance("s").setCoord(Coordinate.newInstance(20, 0))
.setTimeWindow(TimeWindow.newInstance(40,50)).build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(0,0)).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(service).addVehicle(vehicle).build();
VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(vrp,"src/test/resources/algorithmConfig.xml");
vraBuilder.addCoreConstraints();
vraBuilder.addDefaultCostCalculators();
VehicleRoutingAlgorithm vra = vraBuilder.build(); //this should ignore any constraints
vra.setMaxIterations(10);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoute route = Solutions.bestOf(solutions).getRoutes().iterator().next();
Assert.assertEquals(40., route.getActivities().get(0).getEndTime(), 0.01);
}
@Test
public void whenActivatingViaStateManager_activityTimesShouldConsiderTimeWindows(){
Service service = Service.Builder.newInstance("s").setCoord(Coordinate.newInstance(20, 0))
.setTimeWindow(TimeWindow.newInstance(40,50)).build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(0,0)).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(service).addVehicle(vehicle).build();
VehicleRoutingAlgorithmBuilder vraBuilder = new VehicleRoutingAlgorithmBuilder(vrp,"src/test/resources/algorithmConfig.xml");
vraBuilder.addDefaultCostCalculators();
StateManager stateManager = new StateManager(vrp);
stateManager.updateTimeWindowStates();
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
constraintManager.addTimeWindowConstraint();
vraBuilder.setStateAndConstraintManager(stateManager,constraintManager);
VehicleRoutingAlgorithm vra = vraBuilder.build(); //this should ignore any constraints
vra.setMaxIterations(10);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoute route = Solutions.bestOf(solutions).getRoutes().iterator().next();
Assert.assertEquals(40., route.getActivities().get(0).getEndTime(), 0.01);
}
}