Skip to content

Commit

Permalink
flexible start time
Browse files Browse the repository at this point in the history
  • Loading branch information
braktar committed Jul 8, 2016
1 parent b5d31f8 commit ccbed2a
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;
import com.graphhopper.jsprit.core.problem.vehicle.*;
import com.graphhopper.jsprit.core.util.ActivityTimeTracker;
import com.graphhopper.jsprit.core.util.ReverseActivityTimeTracker;

import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.slf4j.Logger;
Expand Down Expand Up @@ -542,6 +544,7 @@ public void run() {
switchAllowed = Boolean.parseBoolean(switchString);
} else switchAllowed = true;
ActivityTimeTracker.ActivityPolicy activityPolicy;
ReverseActivityTimeTracker.ReverseActivityPolicy reverseActivityPolicy;
if (stateManager.timeWindowUpdateIsActivated()) {
UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows(stateManager, vrp.getTransportCosts(), vrp.getActivityCosts());
timeWindowUpdater.setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
Expand All @@ -563,10 +566,13 @@ public Collection<Vehicle> get(VehicleRoute vehicleRoute) {
});
stateManager.addStateUpdater(timeWindowUpdater);
activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;
reverseActivityPolicy = ReverseActivityTimeTracker.ReverseActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;
} else {
activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED;
reverseActivityPolicy = ReverseActivityTimeTracker.ReverseActivityPolicy.AS_SOON_AS_ARRIVED;
}
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy, vrp.getActivityCosts()));
stateManager.addStateUpdater(new UpdateReverseActivityTimes(vrp.getTransportCosts(), reverseActivityPolicy, vrp.getActivityCosts()));
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy));

final SolutionCostCalculator costCalculator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*******************************************************************************
* 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
* 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/>.
******************************************************************************/
package com.graphhopper.jsprit.core.algorithm.state;

import com.graphhopper.jsprit.core.problem.cost.TransportTime;
import com.graphhopper.jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
import com.graphhopper.jsprit.core.problem.solution.route.activity.ReverseActivityVisitor;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;
import com.graphhopper.jsprit.core.util.ReverseActivityTimeTracker;
import com.graphhopper.jsprit.core.util.ReverseActivityTimeTracker.ReverseActivityPolicy;


/**
* Updates arrival and end times of activities.
* <p>
* <p>Note that this modifies arrTime and endTime of each activity in a route.
*
* @author stefan
*/
public class UpdateReverseActivityTimes implements ReverseActivityVisitor, StateUpdater {

private ReverseActivityTimeTracker timeTracker;

private VehicleRoute route;

public UpdateReverseActivityTimes(TransportTime transportTime, VehicleRoutingActivityCosts activityCosts) {
super();
timeTracker = new ReverseActivityTimeTracker(transportTime,activityCosts);
}

public UpdateReverseActivityTimes(TransportTime transportTime, ReverseActivityPolicy activityPolicy, VehicleRoutingActivityCosts activityCosts) {
timeTracker = new ReverseActivityTimeTracker(transportTime, activityPolicy, activityCosts);
}

@Override
public void begin(VehicleRoute route) {
timeTracker.begin(route);
this.route = route;
}

@Override
public void visit(TourActivity activity) {
timeTracker.visit(activity);
}

@Override
public void finish() {
timeTracker.finish();
route.getStart().setEndTime(timeTracker.getActEndTime());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public double getActEndTime() {
@Override
public void begin(VehicleRoute route) {
prevAct = route.getStart();
startAtPrevAct = prevAct.getEndTime();
startAtPrevAct = route.getVehicle().getEarliestDeparture();
actEndTime = startAtPrevAct;
this.route = route;
beginFirst = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*******************************************************************************
* 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
* 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/>.
******************************************************************************/
package com.graphhopper.jsprit.core.util;

import com.graphhopper.jsprit.core.problem.cost.TransportTime;
import com.graphhopper.jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
import com.graphhopper.jsprit.core.problem.solution.route.activity.ReverseActivityVisitor;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;

public class ReverseActivityTimeTracker implements ReverseActivityVisitor {

public static enum ReverseActivityPolicy {

AS_SOON_AS_TIME_WINDOW_OPENS, AS_SOON_AS_ARRIVED

}

private final TransportTime transportTime;

private final VehicleRoutingActivityCosts activityCosts;

private TourActivity prevAct = null;

private double startAtPrevAct;

private VehicleRoute route;

private boolean beginFirst = false;

private double actArrTime;

private double actEndTime;

private ReverseActivityPolicy activityPolicy = ReverseActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;

public ReverseActivityTimeTracker(TransportTime transportTime, VehicleRoutingActivityCosts activityCosts) {
super();
this.transportTime = transportTime;
this.activityCosts = activityCosts;
}

public ReverseActivityTimeTracker(TransportTime transportTime, ReverseActivityPolicy activityPolicy, VehicleRoutingActivityCosts activityCosts) {
super();
this.transportTime = transportTime;
this.activityPolicy = activityPolicy;
this.activityCosts = activityCosts;
}

public double getActArrTime() {
return actArrTime;
}

public double getActEndTime() {
return actEndTime;
}

@Override
public void begin(VehicleRoute route) {
prevAct = route.getEnd();
startAtPrevAct = prevAct.getArrTime();
actArrTime = startAtPrevAct;
this.route = route;
beginFirst = true;
}

@Override
public void visit(TourActivity activity) {
if (!beginFirst) throw new IllegalStateException("never called begin. this however is essential here");
double transportTime = this.transportTime.getBackwardTransportTime(prevAct.getLocation(), activity.getLocation(), startAtPrevAct, route.getDriver(), route.getVehicle());
double arrivalTimeAtCurrAct = startAtPrevAct - transportTime;

actEndTime = arrivalTimeAtCurrAct;
double operationEndTime = arrivalTimeAtCurrAct;

double operationStartTime = operationEndTime - activityCosts.getActivityDuration(activity,actEndTime,route.getDriver(),route.getVehicle());

if (activityPolicy.equals(ReverseActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS)) {
actArrTime = Math.min(activity.getTheoreticalLatestOperationStartTime(), operationStartTime);
} else if (activityPolicy.equals(ReverseActivityPolicy.AS_SOON_AS_ARRIVED)) {
actArrTime = actEndTime;
} else operationEndTime = actEndTime;

prevAct = activity;
startAtPrevAct = actArrTime;

}

@Override
public void finish() {
double transportTime = this.transportTime.getBackwardTransportTime(prevAct.getLocation(), route.getStart().getLocation(), startAtPrevAct, route.getDriver(), route.getVehicle());
double arrivalTimeAtCurrAct = startAtPrevAct - transportTime;

actArrTime = arrivalTimeAtCurrAct;
actEndTime = arrivalTimeAtCurrAct;

beginFirst = false;
}


}

0 comments on commit ccbed2a

Please sign in to comment.