mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
variable departure times and waiting time opt
This commit is contained in:
parent
552729d7ee
commit
f9c255cc08
24 changed files with 881 additions and 110 deletions
|
|
@ -20,6 +20,7 @@ import jsprit.core.algorithm.SearchStrategy.DiscoveredSolution;
|
|||
import jsprit.core.algorithm.listener.*;
|
||||
import jsprit.core.algorithm.termination.PrematureAlgorithmTermination;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.job.Job;
|
||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
|
|
@ -195,6 +196,7 @@ public class VehicleRoutingAlgorithm {
|
|||
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
|
||||
algorithmStarts(problem,solutions);
|
||||
bestEver = Solutions.bestOf(solutions);
|
||||
if(logger.isTraceEnabled()) log(solutions);
|
||||
logger.info("iterations start");
|
||||
for(int i=0;i< maxIterations;i++){
|
||||
iterationStarts(i+1,problem,solutions);
|
||||
|
|
@ -202,7 +204,7 @@ public class VehicleRoutingAlgorithm {
|
|||
counter.incCounter();
|
||||
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
|
||||
DiscoveredSolution discoveredSolution = strategy.run(problem, solutions);
|
||||
logger.trace("discovered solution: " + discoveredSolution);
|
||||
if(logger.isTraceEnabled()) log(discoveredSolution);
|
||||
memorizeIfBestEver(discoveredSolution);
|
||||
selectedStrategy(discoveredSolution,problem,solutions);
|
||||
if(terminationManager.isPrematureBreak(discoveredSolution)){
|
||||
|
|
@ -219,7 +221,38 @@ public class VehicleRoutingAlgorithm {
|
|||
return solutions;
|
||||
}
|
||||
|
||||
private void addBestEver(Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
private void log(Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
for(VehicleRoutingProblemSolution sol : solutions) log(sol);
|
||||
}
|
||||
|
||||
private void log(VehicleRoutingProblemSolution solution){
|
||||
logger.trace("solution costs: " + solution.getCost());
|
||||
for(VehicleRoute r : solution.getRoutes()){
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.append(r.getVehicle().getId()).append(" : ").append("[ ");
|
||||
for(TourActivity act : r.getActivities()){
|
||||
if(act instanceof TourActivity.JobActivity){
|
||||
b.append(((TourActivity.JobActivity) act).getJob().getId()).append(" ");
|
||||
}
|
||||
}
|
||||
b.append("]");
|
||||
logger.trace(b.toString());
|
||||
}
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.append("unassigned : [ ");
|
||||
for(Job j : solution.getUnassignedJobs()){
|
||||
b.append(j.getId()).append(" ");
|
||||
}
|
||||
b.append("]");
|
||||
logger.trace(b.toString());
|
||||
}
|
||||
|
||||
private void log(DiscoveredSolution discoveredSolution) {
|
||||
logger.trace("discovered solution: " + discoveredSolution);
|
||||
log(discoveredSolution.getSolution());
|
||||
}
|
||||
|
||||
private void addBestEver(Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
if(bestEver != null) solutions.add(bestEver);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -554,6 +554,7 @@ public class Jsprit {
|
|||
TourActivity prevAct = route.getStart();
|
||||
for(TourActivity act : route.getActivities()){
|
||||
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(),act.getLocation(),prevAct.getEndTime(),route.getDriver(),route.getVehicle());
|
||||
costs += vrp.getActivityCosts().getActivityCost(act,act.getArrTime(),route.getDriver(),route.getVehicle());
|
||||
prevAct = act;
|
||||
}
|
||||
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(),route.getEnd().getLocation(),prevAct.getEndTime(),route.getDriver(),route.getVehicle());
|
||||
|
|
|
|||
|
|
@ -28,13 +28,13 @@ public class ConfigureLocalActivityInsertionCalculator implements InsertionStart
|
|||
public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) {
|
||||
this.nuOfJobsToRecreate = unassignedJobs.size();
|
||||
double completenessRatio = (1-((double)nuOfJobsToRecreate/(double)vrp.getJobs().values().size()));
|
||||
localActivityInsertionCostsCalculator.setSolutionCompletenessRatio(completenessRatio);
|
||||
localActivityInsertionCostsCalculator.setSolutionCompletenessRatio(Math.max(0.5,completenessRatio));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
|
||||
nuOfJobsToRecreate--;
|
||||
double completenessRatio = (1-((double)nuOfJobsToRecreate/(double)vrp.getJobs().values().size()));
|
||||
localActivityInsertionCostsCalculator.setSolutionCompletenessRatio(completenessRatio);
|
||||
localActivityInsertionCostsCalculator.setSolutionCompletenessRatio(Math.max(0.5,completenessRatio));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -261,7 +261,7 @@ public class JobInsertionCostsCalculatorBuilder {
|
|||
ActivityInsertionCostsCalculator actInsertionCalc;
|
||||
ConfigureLocalActivityInsertionCalculator configLocal = null;
|
||||
if(activityInsertionCostCalculator == null && addDefaultCostCalc){
|
||||
actInsertionCalc = new LocalActivityInsertionCostsCalculator(vrp.getTransportCosts(), vrp.getActivityCosts());
|
||||
actInsertionCalc = new LocalActivityInsertionCostsCalculator(vrp.getTransportCosts(), vrp.getActivityCosts(), statesManager);
|
||||
configLocal = new ConfigureLocalActivityInsertionCalculator(vrp, (LocalActivityInsertionCostsCalculator) actInsertionCalc);
|
||||
}
|
||||
else if(activityInsertionCostCalculator == null && !addDefaultCostCalc){
|
||||
|
|
|
|||
|
|
@ -18,12 +18,15 @@
|
|||
|
||||
package jsprit.core.algorithm.recreate;
|
||||
|
||||
import jsprit.core.algorithm.state.InternalStates;
|
||||
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
|
||||
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.Start;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
|
||||
import jsprit.core.problem.vehicle.Vehicle;
|
||||
import jsprit.core.util.CalculationUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -46,56 +49,96 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
|
|||
|
||||
private double solutionCompletenessRatio = 1.;
|
||||
|
||||
public LocalActivityInsertionCostsCalculator(VehicleRoutingTransportCosts routingCosts, VehicleRoutingActivityCosts actCosts) {
|
||||
private double variableStartTimeFactor = 1.;
|
||||
|
||||
private RouteAndActivityStateGetter stateManager;
|
||||
|
||||
public LocalActivityInsertionCostsCalculator(VehicleRoutingTransportCosts routingCosts, VehicleRoutingActivityCosts actCosts, RouteAndActivityStateGetter stateManager) {
|
||||
super();
|
||||
this.routingCosts = routingCosts;
|
||||
this.activityCosts = actCosts;
|
||||
this.stateManager = stateManager;
|
||||
}
|
||||
|
||||
public void setVariableStartTimeFactor(double variableStartTimeFactor) {
|
||||
this.variableStartTimeFactor = variableStartTimeFactor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity nextAct, TourActivity newAct, double depTimeAtPrevAct) {
|
||||
|
||||
// double waiting = 0;
|
||||
// if(!isEnd(nextAct)){
|
||||
// waiting = stateManager.getActivityState(nextAct, InternalStates.WAITING,Double.class);
|
||||
// }
|
||||
|
||||
double tp_costs_prevAct_newAct = routingCosts.getTransportCost(prevAct.getLocation(), newAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
double tp_time_prevAct_newAct = routingCosts.getTransportTime(prevAct.getLocation(), newAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
double newAct_arrTime = depTimeAtPrevAct + tp_time_prevAct_newAct;
|
||||
double newAct_endTime = CalculationUtils.getActivityEndTime(newAct_arrTime, newAct);
|
||||
|
||||
|
||||
double act_costs_newAct = activityCosts.getActivityCost(newAct, newAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
if(prevAct instanceof Start) {
|
||||
if(iFacts.getNewVehicle().hasVariableDepartureTime()){
|
||||
act_costs_newAct = 0;
|
||||
}
|
||||
|
||||
double slack_time_new = 0;
|
||||
double slack_time_prev = 0;
|
||||
if(isStart(prevAct) && hasVariableDeparture(iFacts.getNewVehicle())) act_costs_newAct = 0;
|
||||
else if(hasVariableDeparture(iFacts.getNewVehicle())){
|
||||
Double slack_time_prev_ = stateManager.getActivityState(prevAct,iFacts.getNewVehicle(),InternalStates.TIME_SLACK,Double.class);
|
||||
if(slack_time_prev_ == null) slack_time_prev = 0.;
|
||||
else slack_time_prev = slack_time_prev_;
|
||||
act_costs_newAct = activityCosts.getActivityCost(newAct, newAct_arrTime + slack_time_prev, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
slack_time_new = Math.min(newAct.getTheoreticalLatestOperationStartTime() - newAct.getTheoreticalEarliestOperationStartTime(), Math.max(newAct_arrTime + slack_time_prev - newAct.getTheoreticalEarliestOperationStartTime(), 0));
|
||||
}
|
||||
|
||||
//open routes
|
||||
if(nextAct instanceof End){
|
||||
if(!iFacts.getNewVehicle().isReturnToDepot()){
|
||||
return tp_costs_prevAct_newAct;
|
||||
}
|
||||
}
|
||||
|
||||
if(isEnd(nextAct) && !toDepot(iFacts.getNewVehicle())) return tp_costs_prevAct_newAct;
|
||||
|
||||
double tp_costs_newAct_nextAct = routingCosts.getTransportCost(newAct.getLocation(), nextAct.getLocation(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
double tp_time_newAct_nextAct = routingCosts.getTransportTime(newAct.getLocation(), nextAct.getLocation(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
double nextAct_arrTime = newAct_endTime + tp_time_newAct_nextAct;
|
||||
double act_costs_nextAct = activityCosts.getActivityCost(nextAct, nextAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
|
||||
if(hasVariableDeparture(iFacts.getNewVehicle())){
|
||||
act_costs_nextAct = activityCosts.getActivityCost(nextAct, nextAct_arrTime + slack_time_new, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
}
|
||||
|
||||
double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct + solutionCompletenessRatio * activityCostsWeight * (act_costs_newAct + act_costs_nextAct);
|
||||
|
||||
double oldCosts;
|
||||
if(iFacts.getRoute().isEmpty()){
|
||||
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
double arrTime_nextAct = routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
double actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
oldCosts = tp_costs_prevAct_nextAct + solutionCompletenessRatio * activityCostsWeight * actCost_nextAct;
|
||||
oldCosts = tp_costs_prevAct_nextAct;
|
||||
}
|
||||
else{
|
||||
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
|
||||
double arrTime_nextAct = routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
|
||||
double arrTime_nextAct = prevAct.getEndTime() + routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
|
||||
double actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct, iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
|
||||
if(isStart(prevAct) && hasVariableDeparture(iFacts.getRoute().getVehicle())) {
|
||||
actCost_nextAct = 0;
|
||||
}
|
||||
else if(hasVariableDeparture(iFacts.getRoute().getVehicle())){
|
||||
actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct + slack_time_prev, iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
|
||||
}
|
||||
oldCosts = tp_costs_prevAct_nextAct + solutionCompletenessRatio * activityCostsWeight * actCost_nextAct;
|
||||
}
|
||||
return totalCosts - oldCosts;
|
||||
}
|
||||
|
||||
private boolean toDepot(Vehicle newVehicle) {
|
||||
return newVehicle.isReturnToDepot();
|
||||
}
|
||||
|
||||
private boolean isEnd(TourActivity nextAct) {
|
||||
return nextAct instanceof End;
|
||||
}
|
||||
|
||||
private boolean hasVariableDeparture(Vehicle newVehicle) {
|
||||
return newVehicle.hasVariableDepartureTime();
|
||||
}
|
||||
|
||||
private boolean isStart(TourActivity prevAct) {
|
||||
return prevAct instanceof Start;
|
||||
}
|
||||
|
||||
public void setActivityCostWeight(double weight){
|
||||
this.activityCostsWeight = weight;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,9 +109,9 @@ final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCo
|
|||
relevantVehicles.addAll(fleetManager.getAvailableVehicles());
|
||||
}
|
||||
for(Vehicle v : relevantVehicles){
|
||||
double depTime;
|
||||
if(v == selectedVehicle) depTime = currentRoute.getDepartureTime();
|
||||
else depTime = v.getEarliestDeparture();
|
||||
double depTime = v.getEarliestDeparture();
|
||||
// if(v == selectedVehicle) depTime = currentRoute.getDepartureTime();
|
||||
// else depTime = v.getEarliestDeparture();
|
||||
InsertionData iData = insertionCalculator.getInsertionData(currentRoute, jobToInsert, v, depTime, selectedDriver, bestKnownCost_);
|
||||
if(iData instanceof NoInsertionFound) {
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -42,4 +42,8 @@ public class InternalStates {
|
|||
public final static StateId PAST_MAXLOAD = new StateFactory.StateIdImpl("past_max_load", 9);
|
||||
|
||||
public static final StateId SKILLS = new StateFactory.StateIdImpl("skills", 10);
|
||||
|
||||
public static final StateId WAITING = new StateFactory.StateIdImpl("waiting",11);
|
||||
|
||||
public static final StateId TIME_SLACK = new StateFactory.StateIdImpl("time_slack",12);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,34 +21,86 @@ import jsprit.core.problem.solution.route.RouteVisitor;
|
|||
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
|
||||
public class UpdateDepartureTime implements StateUpdater, RouteVisitor{
|
||||
import java.util.Iterator;
|
||||
|
||||
private VehicleRoutingTransportCosts routingCosts;
|
||||
/**
|
||||
* Updates and memorizes latest operation start times at activities.
|
||||
*
|
||||
* @author schroeder
|
||||
*
|
||||
*/
|
||||
public class UpdateDepartureTime implements RouteVisitor, StateUpdater{
|
||||
|
||||
public UpdateDepartureTime(VehicleRoutingTransportCosts routingCosts) {
|
||||
this.routingCosts = routingCosts;
|
||||
|
||||
|
||||
private VehicleRoute route;
|
||||
|
||||
private VehicleRoutingTransportCosts transportCosts;
|
||||
|
||||
private double arrTimeAtPrevActWithoutWaiting;
|
||||
|
||||
private TourActivity prevAct;
|
||||
|
||||
private boolean hasEarliest = false;
|
||||
|
||||
private boolean hasVariableDepartureTime;
|
||||
|
||||
public UpdateDepartureTime(VehicleRoutingTransportCosts tpCosts) {
|
||||
super();
|
||||
this.transportCosts = tpCosts;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void begin(VehicleRoute route) {
|
||||
this.route = route;
|
||||
hasEarliest = false;
|
||||
prevAct = route.getEnd();
|
||||
hasVariableDepartureTime = route.getVehicle().hasVariableDepartureTime();
|
||||
}
|
||||
|
||||
|
||||
public void visit(TourActivity activity) {
|
||||
if(!hasVariableDepartureTime) return;
|
||||
if(hasEarliest){
|
||||
double potentialArrivalTimeAtCurrAct = arrTimeAtPrevActWithoutWaiting - transportCosts.getBackwardTransportTime(activity.getLocation(), prevAct.getLocation(), arrTimeAtPrevActWithoutWaiting, route.getDriver(),route.getVehicle()) - activity.getOperationTime();
|
||||
if(potentialArrivalTimeAtCurrAct < activity.getTheoreticalEarliestOperationStartTime()){
|
||||
arrTimeAtPrevActWithoutWaiting = activity.getTheoreticalEarliestOperationStartTime();
|
||||
}
|
||||
else if(potentialArrivalTimeAtCurrAct >= activity.getTheoreticalEarliestOperationStartTime() && potentialArrivalTimeAtCurrAct <= activity.getTheoreticalLatestOperationStartTime()){
|
||||
arrTimeAtPrevActWithoutWaiting = potentialArrivalTimeAtCurrAct;
|
||||
}
|
||||
else {
|
||||
arrTimeAtPrevActWithoutWaiting = activity.getTheoreticalLatestOperationStartTime();
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(activity.getTheoreticalEarliestOperationStartTime() > 0){
|
||||
hasEarliest = true;
|
||||
arrTimeAtPrevActWithoutWaiting = activity.getTheoreticalEarliestOperationStartTime();
|
||||
}
|
||||
}
|
||||
prevAct = activity;
|
||||
}
|
||||
|
||||
|
||||
public void finish() {
|
||||
if(!hasVariableDepartureTime) return;
|
||||
if(hasEarliest){
|
||||
double dep = arrTimeAtPrevActWithoutWaiting - transportCosts.getBackwardTransportTime(route.getStart().getLocation(), prevAct.getLocation(), arrTimeAtPrevActWithoutWaiting, route.getDriver(),route.getVehicle());
|
||||
double newDepartureTime = Math.max(route.getVehicle().getEarliestDeparture(), dep);
|
||||
route.setVehicleAndDepartureTime(route.getVehicle(),newDepartureTime);
|
||||
}
|
||||
else route.setVehicleAndDepartureTime(route.getVehicle(),route.getVehicle().getEarliestDeparture());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(VehicleRoute route) {
|
||||
if(route.getVehicle() != null){
|
||||
if(route.getVehicle().hasVariableDepartureTime()) {
|
||||
if (!route.isEmpty()) {
|
||||
TourActivity first = route.getActivities().get(0);
|
||||
double earliestStart = first.getTheoreticalEarliestOperationStartTime();
|
||||
double backwardTravelTime = routingCosts.getBackwardTransportTime(first.getLocation(), route.getStart().getLocation(),
|
||||
earliestStart, route.getDriver(), route.getVehicle());
|
||||
double newDepartureTime = Math.max(route.getStart().getEndTime(), earliestStart - backwardTravelTime);
|
||||
route.setVehicleAndDepartureTime(route.getVehicle(), newDepartureTime);
|
||||
}
|
||||
}
|
||||
begin(route);
|
||||
Iterator<TourActivity> revIterator = route.getTourActivities().reverseActivityIterator();
|
||||
while(revIterator.hasNext()){
|
||||
visit(revIterator.next());
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,81 @@
|
|||
/*******************************************************************************
|
||||
* 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 jsprit.core.algorithm.state;
|
||||
|
||||
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
|
||||
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import jsprit.core.problem.solution.route.activity.ActivityVisitor;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
|
||||
|
||||
/**
|
||||
* Updates arrival and end times of activities.
|
||||
*
|
||||
* <p>Note that this modifies arrTime and endTime of each activity in a route.
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
public class UpdateTimeSlack implements ActivityVisitor, StateUpdater{
|
||||
|
||||
private VehicleRoute route;
|
||||
|
||||
private StateManager stateManager;
|
||||
|
||||
private VehicleRoutingTransportCosts costs;
|
||||
|
||||
private double prevTimeSlack;
|
||||
|
||||
private double prevActDeparture;
|
||||
|
||||
private TourActivity prevAct;
|
||||
|
||||
public UpdateTimeSlack(StateManager stateManager, VehicleRoutingTransportCosts costs) {
|
||||
this.stateManager = stateManager;
|
||||
this.costs = costs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
if(route.isEmpty()) return;
|
||||
this.route = route;
|
||||
prevActDeparture = route.getVehicle().getEarliestDeparture();
|
||||
TourActivity first = route.getActivities().get(0);
|
||||
double latestArr = stateManager.getActivityState(first,route.getVehicle(),InternalStates.LATEST_OPERATION_START_TIME,Double.class);
|
||||
double latest = latestArr - costs.getBackwardTransportCost(first.getLocation(),route.getStart().getLocation(),latestArr,route.getDriver(),route.getVehicle());
|
||||
prevTimeSlack = latest - prevActDeparture;
|
||||
prevAct = route.getStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TourActivity activity) {
|
||||
double actArrTime = prevActDeparture + costs.getTransportTime(prevAct.getLocation(),activity.getLocation(),prevActDeparture,route.getDriver(),route.getVehicle());
|
||||
double actEarliestStart = Math.max(actArrTime,activity.getTheoreticalEarliestOperationStartTime());
|
||||
double actLatestStart = stateManager.getActivityState(activity,route.getVehicle(),InternalStates.LATEST_OPERATION_START_TIME,Double.class);
|
||||
double latest_minus_earliest = actLatestStart - actEarliestStart;
|
||||
double time_slack_ = Math.max(actArrTime + prevTimeSlack - activity.getTheoreticalEarliestOperationStartTime(),0);
|
||||
double time_slack = Math.min(latest_minus_earliest,time_slack_);
|
||||
stateManager.putInternalTypedActivityState(activity,route.getVehicle(), InternalStates.TIME_SLACK, time_slack);
|
||||
prevTimeSlack = time_slack;
|
||||
prevAct = activity;
|
||||
prevActDeparture = actEarliestStart + activity.getOperationTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {}
|
||||
|
||||
}
|
||||
|
|
@ -19,15 +19,26 @@ package jsprit.core.algorithm.state;
|
|||
|
||||
import jsprit.core.problem.Location;
|
||||
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
|
||||
import jsprit.core.problem.solution.route.RouteVisitor;
|
||||
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import jsprit.core.problem.solution.route.activity.ReverseActivityVisitor;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
import jsprit.core.problem.vehicle.Vehicle;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class UpdateVehicleDependentPracticalTimeWindows implements ReverseActivityVisitor, StateUpdater{
|
||||
public class UpdateVehicleDependentPracticalTimeWindows implements RouteVisitor, StateUpdater{
|
||||
|
||||
@Override
|
||||
public void visit(VehicleRoute route) {
|
||||
begin(route);
|
||||
Iterator<TourActivity> revIterator = route.getTourActivities().reverseActivityIterator();
|
||||
while(revIterator.hasNext()){
|
||||
visit(revIterator.next());
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
public static interface VehiclesToUpdate {
|
||||
|
||||
|
|
@ -68,7 +79,7 @@ public class UpdateVehicleDependentPracticalTimeWindows implements ReverseActivi
|
|||
this.vehiclesToUpdate = vehiclesToUpdate;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void begin(VehicleRoute route) {
|
||||
this.route = route;
|
||||
vehicles = vehiclesToUpdate.get(route);
|
||||
|
|
@ -78,7 +89,7 @@ public class UpdateVehicleDependentPracticalTimeWindows implements ReverseActivi
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void visit(TourActivity activity) {
|
||||
for(Vehicle vehicle : vehicles){
|
||||
double latestArrTimeAtPrevAct = latest_arrTimes_at_prevAct[vehicle.getVehicleTypeIdentifier().getIndex()];
|
||||
|
|
@ -92,7 +103,7 @@ public class UpdateVehicleDependentPracticalTimeWindows implements ReverseActivi
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void finish() {}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ package jsprit.core.problem;
|
|||
|
||||
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
|
||||
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
|
||||
import jsprit.core.problem.driver.Driver;
|
||||
import jsprit.core.problem.cost.WaitingTimeCosts;
|
||||
import jsprit.core.problem.job.Job;
|
||||
import jsprit.core.problem.job.Service;
|
||||
import jsprit.core.problem.job.Shipment;
|
||||
|
|
@ -75,19 +75,7 @@ public class VehicleRoutingProblem {
|
|||
|
||||
private VehicleRoutingTransportCosts transportCosts;
|
||||
|
||||
private VehicleRoutingActivityCosts activityCosts = new VehicleRoutingActivityCosts() {
|
||||
|
||||
@Override
|
||||
public double getActivityCost(TourActivity tourAct, double arrivalTime, Driver driver, Vehicle vehicle) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[name=defaultActivityCosts]";
|
||||
}
|
||||
|
||||
};
|
||||
private VehicleRoutingActivityCosts activityCosts = new WaitingTimeCosts();
|
||||
|
||||
private Map<String,Job> jobs = new LinkedHashMap<String, Job>();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package jsprit.core.problem.cost;
|
||||
|
||||
import jsprit.core.problem.driver.Driver;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
import jsprit.core.problem.vehicle.Vehicle;
|
||||
|
||||
/**
|
||||
* Created by schroeder on 23/07/15.
|
||||
*/
|
||||
public class WaitingTimeCosts implements VehicleRoutingActivityCosts {
|
||||
|
||||
@Override
|
||||
public double getActivityCost(TourActivity tourAct, double arrivalTime, Driver driver, Vehicle vehicle) {
|
||||
if(vehicle != null){
|
||||
return vehicle.getType().getVehicleCostParams().perWaitingTimeUnit * Math.max(0.,tourAct.getTheoreticalEarliestOperationStartTime()-arrivalTime);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -47,7 +47,7 @@ class InfiniteVehicles implements VehicleFleetManager{
|
|||
|
||||
private void extractTypes(Collection<Vehicle> vehicles) {
|
||||
for(Vehicle v : vehicles){
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocation().getId(),v.getEndLocation().getId(), v.getEarliestDeparture(), v.getLatestArrival(), v.getSkills());
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocation().getId(),v.getEndLocation().getId(), v.getEarliestDeparture(), v.getLatestArrival(), v.getSkills(), v.isReturnToDepot());
|
||||
types.put(typeKey,v);
|
||||
// sortedTypes.add(typeKey);
|
||||
}
|
||||
|
|
@ -83,7 +83,7 @@ class InfiniteVehicles implements VehicleFleetManager{
|
|||
@Override
|
||||
public Collection<Vehicle> getAvailableVehicles(Vehicle withoutThisType) {
|
||||
Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType.getType().getTypeId(), withoutThisType.getStartLocation().getId(), withoutThisType.getEndLocation().getId(), withoutThisType.getEarliestDeparture(), withoutThisType.getLatestArrival(), withoutThisType.getSkills());
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType.getType().getTypeId(), withoutThisType.getStartLocation().getId(), withoutThisType.getEndLocation().getId(), withoutThisType.getEarliestDeparture(), withoutThisType.getLatestArrival(), withoutThisType.getSkills(), withoutThisType.isReturnToDepot());
|
||||
for(VehicleTypeKey key : types.keySet()){
|
||||
if(!key.equals(thisKey)){
|
||||
vehicles.add(types.get(key));
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ class VehicleFleetManagerImpl implements VehicleFleetManager {
|
|||
if(v.getType() == null){
|
||||
throw new IllegalStateException("vehicle needs type");
|
||||
}
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocation().getId(), v.getEndLocation().getId(), v.getEarliestDeparture(), v.getLatestArrival(), v.getSkills());
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocation().getId(), v.getEndLocation().getId(), v.getEarliestDeparture(), v.getLatestArrival(), v.getSkills(),v.isReturnToDepot() );
|
||||
if(!typeMapOfAvailableVehicles.containsKey(typeKey)){
|
||||
typeMapOfAvailableVehicles.put(typeKey, new TypeContainer());
|
||||
}
|
||||
|
|
@ -105,7 +105,7 @@ class VehicleFleetManagerImpl implements VehicleFleetManager {
|
|||
}
|
||||
|
||||
private void removeVehicle(Vehicle v){
|
||||
VehicleTypeKey key = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocation().getId(), v.getEndLocation().getId(), v.getEarliestDeparture(), v.getLatestArrival(), v.getSkills());
|
||||
VehicleTypeKey key = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocation().getId(), v.getEndLocation().getId(), v.getEarliestDeparture(), v.getLatestArrival(), v.getSkills(),v.isReturnToDepot() );
|
||||
if(typeMapOfAvailableVehicles.containsKey(key)){
|
||||
typeMapOfAvailableVehicles.get(key).remove(v);
|
||||
}
|
||||
|
|
@ -137,7 +137,7 @@ class VehicleFleetManagerImpl implements VehicleFleetManager {
|
|||
@Override
|
||||
public Collection<Vehicle> getAvailableVehicles(Vehicle withoutThisType) {
|
||||
List<Vehicle> vehicles = new ArrayList<Vehicle>();
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType.getType().getTypeId(), withoutThisType.getStartLocation().getId(), withoutThisType.getEndLocation().getId(), withoutThisType.getEarliestDeparture(), withoutThisType.getLatestArrival(), withoutThisType.getSkills());
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType.getType().getTypeId(), withoutThisType.getStartLocation().getId(), withoutThisType.getEndLocation().getId(), withoutThisType.getEarliestDeparture(), withoutThisType.getLatestArrival(), withoutThisType.getSkills(),withoutThisType.isReturnToDepot() );
|
||||
for(VehicleTypeKey key : typeMapOfAvailableVehicles.keySet()){
|
||||
if(key.equals(thisKey)) continue;
|
||||
if(!typeMapOfAvailableVehicles.get(key).isEmpty()){
|
||||
|
|
|
|||
|
|
@ -298,7 +298,7 @@ public class VehicleImpl extends AbstractVehicle{
|
|||
endLocation = builder.endLocation;
|
||||
startLocation = builder.startLocation;
|
||||
hasVariableDepartureTime = builder.hasVariableDepartureTime;
|
||||
setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(),startLocation.getId(),endLocation.getId(),earliestDeparture,latestArrival,skills));
|
||||
setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(),startLocation.getId(),endLocation.getId(),earliestDeparture,latestArrival,skills, returnToDepot));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -42,19 +42,32 @@ public class VehicleTypeImpl implements VehicleType {
|
|||
}
|
||||
|
||||
public final double fix;
|
||||
@Deprecated
|
||||
public final double perTimeUnit;
|
||||
public final double perTransportTimeUnit;
|
||||
public final double perDistanceUnit;
|
||||
public final double perWaitingTimeUnit;
|
||||
|
||||
private VehicleCostParams(double fix, double perTimeUnit,double perDistanceUnit) {
|
||||
super();
|
||||
this.fix = fix;
|
||||
this.perTimeUnit = perTimeUnit;
|
||||
this.perTransportTimeUnit = perTimeUnit;
|
||||
this.perDistanceUnit = perDistanceUnit;
|
||||
this.perWaitingTimeUnit = 0.;
|
||||
}
|
||||
|
||||
|
||||
public VehicleCostParams(double fix, double perTimeUnit, double perDistanceUnit, double perWaitingTimeUnit) {
|
||||
this.fix = fix;
|
||||
this.perTimeUnit = perTimeUnit;
|
||||
this.perTransportTimeUnit = perTimeUnit;
|
||||
this.perDistanceUnit = perDistanceUnit;
|
||||
this.perWaitingTimeUnit = perWaitingTimeUnit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[fixed="+fix+"][perTime="+perTimeUnit+"][perDistance="+perDistanceUnit+"]";
|
||||
return "[fixed="+fix+"][perTime="+perTransportTimeUnit+"][perDistance="+perDistanceUnit+"][perWaitingTimeUnit="+perWaitingTimeUnit+"]";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +78,9 @@ public class VehicleTypeImpl implements VehicleType {
|
|||
*
|
||||
*/
|
||||
public static class Builder{
|
||||
|
||||
|
||||
|
||||
|
||||
public static VehicleTypeImpl.Builder newInstance(String id) {
|
||||
if(id==null) throw new IllegalStateException();
|
||||
return new Builder(id);
|
||||
|
|
@ -80,6 +95,7 @@ public class VehicleTypeImpl implements VehicleType {
|
|||
private double fixedCost = 0.0;
|
||||
private double perDistance = 1.0;
|
||||
private double perTime = 0.0;
|
||||
private double perWaitingTime = 0.0;
|
||||
|
||||
private String profile = "car";
|
||||
|
||||
|
|
@ -143,12 +159,46 @@ public class VehicleTypeImpl implements VehicleType {
|
|||
* @param perTime
|
||||
* @return this builder
|
||||
* @throws IllegalStateException if costPerTime is smaller than zero
|
||||
* @deprecated use .setCostPerTransportTime(..) instead
|
||||
*/
|
||||
@Deprecated
|
||||
public VehicleTypeImpl.Builder setCostPerTime(double perTime){
|
||||
if(perTime < 0.0) throw new IllegalStateException();
|
||||
this.perTime = perTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets cost per time unit, for instance € per second.
|
||||
*
|
||||
* <p>by default it is 0.0
|
||||
*
|
||||
* @param perTime
|
||||
* @return this builder
|
||||
* @throws IllegalStateException if costPerTime is smaller than zero
|
||||
*
|
||||
*/
|
||||
public VehicleTypeImpl.Builder setCostPerTransportTime(double perTime){
|
||||
if(perTime < 0.0) throw new IllegalStateException();
|
||||
this.perTime = perTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets cost per waiting time unit, for instance € per second.
|
||||
*
|
||||
* <p>by default it is 0.0
|
||||
*
|
||||
* @param perWaitingTime
|
||||
* @return this builder
|
||||
* @throws IllegalStateException if costPerTime is smaller than zero
|
||||
*
|
||||
*/
|
||||
public VehicleTypeImpl.Builder setCostPerWaitingTime(double perWaitingTime){
|
||||
if(perWaitingTime < 0.0) throw new IllegalStateException();
|
||||
this.perWaitingTime = perWaitingTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the vehicle-type.
|
||||
|
|
@ -256,7 +306,7 @@ public class VehicleTypeImpl implements VehicleType {
|
|||
typeId = builder.id;
|
||||
capacity = builder.capacity;
|
||||
maxVelocity = builder.maxVelo;
|
||||
vehicleCostParams = new VehicleCostParams(builder.fixedCost, builder.perTime, builder.perDistance);
|
||||
vehicleCostParams = new VehicleCostParams(builder.fixedCost, builder.perTime, builder.perDistance, builder.perWaitingTime);
|
||||
capacityDimensions = builder.capacityDimensions;
|
||||
profile = builder.profile;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,8 +37,9 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey{
|
|||
public final double earliestStart;
|
||||
public final double latestEnd;
|
||||
public final Skills skills;
|
||||
public final boolean returnToDepot;
|
||||
|
||||
public VehicleTypeKey(String typeId, String startLocationId, String endLocationId, double earliestStart, double latestEnd, Skills skills) {
|
||||
public VehicleTypeKey(String typeId, String startLocationId, String endLocationId, double earliestStart, double latestEnd, Skills skills, boolean returnToDepot) {
|
||||
super();
|
||||
this.type = typeId;
|
||||
this.startLocationId = startLocationId;
|
||||
|
|
@ -46,17 +47,19 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey{
|
|||
this.earliestStart = earliestStart;
|
||||
this.latestEnd = latestEnd;
|
||||
this.skills = skills;
|
||||
this.returnToDepot=returnToDepot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof VehicleTypeKey)) return false;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
VehicleTypeKey that = (VehicleTypeKey) o;
|
||||
|
||||
if (Double.compare(that.earliestStart, earliestStart) != 0) return false;
|
||||
if (Double.compare(that.latestEnd, latestEnd) != 0) return false;
|
||||
if (returnToDepot != that.returnToDepot) return false;
|
||||
if (!endLocationId.equals(that.endLocationId)) return false;
|
||||
if (!skills.equals(that.skills)) return false;
|
||||
if (!startLocationId.equals(that.startLocationId)) return false;
|
||||
|
|
@ -77,6 +80,7 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey{
|
|||
temp = Double.doubleToLongBits(latestEnd);
|
||||
result = 31 * result + (int) (temp ^ (temp >>> 32));
|
||||
result = 31 * result + skills.hashCode();
|
||||
result = 31 * result + (returnToDepot ? 1 : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue