1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

add driver breaks

This commit is contained in:
oblonski 2015-08-05 14:39:05 +02:00
commit 2503a96237
25 changed files with 1020 additions and 66 deletions

View file

@ -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);
}

View file

@ -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());

View file

@ -0,0 +1,40 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.listener.InsertionStartsListener;
import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import java.util.Collection;
/**
* Created by schroeder on 22/07/15.
*/
public class ConfigureLocalActivityInsertionCalculator implements InsertionStartsListener, JobInsertedListener {
private VehicleRoutingProblem vrp;
private LocalActivityInsertionCostsCalculator localActivityInsertionCostsCalculator;
private int nuOfJobsToRecreate;
public ConfigureLocalActivityInsertionCalculator(VehicleRoutingProblem vrp, LocalActivityInsertionCostsCalculator localActivityInsertionCostsCalculator) {
this.vrp = vrp;
this.localActivityInsertionCostsCalculator = localActivityInsertionCostsCalculator;
}
@Override
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(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(Math.max(0.5,completenessRatio));
}
}

View file

@ -259,8 +259,10 @@ public class JobInsertionCostsCalculatorBuilder {
if(constraintManager == null) throw new IllegalStateException("constraint-manager is null");
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){
actInsertionCalc = new ActivityInsertionCostsCalculator(){
@ -300,7 +302,11 @@ public class JobInsertionCostsCalculatorBuilder {
switcher.put(Delivery.class, serviceInsertion);
switcher.put(Break.class, breakInsertionCalculator);
return new CalculatorPlusListeners(switcher);
CalculatorPlusListeners calculatorPlusListeners = new CalculatorPlusListeners(switcher);
if(configLocal != null){
calculatorPlusListeners.insertionListener.add(configLocal);
}
return calculatorPlusListeners;
}
private CalculatorPlusListeners createCalculatorConsideringFixedCosts(VehicleRoutingProblem vrp, JobInsertionCostsCalculator baseCalculator, RouteAndActivityStateGetter activityStates2, double weightOfFixedCosts){

View file

@ -18,11 +18,14 @@
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.TourActivity;
import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.util.CalculationUtils;
/**
@ -40,50 +43,71 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
private VehicleRoutingTransportCosts routingCosts;
private VehicleRoutingActivityCosts activityCosts;
public LocalActivityInsertionCostsCalculator(VehicleRoutingTransportCosts routingCosts, VehicleRoutingActivityCosts actCosts) {
private double activityCostsWeight = 1.;
private double solutionCompletenessRatio = 1.;
private RouteAndActivityStateGetter stateManager;
public LocalActivityInsertionCostsCalculator(VehicleRoutingTransportCosts routingCosts, VehicleRoutingActivityCosts actCosts, RouteAndActivityStateGetter stateManager) {
super();
this.routingCosts = routingCosts;
this.activityCosts = actCosts;
this.stateManager = stateManager;
}
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity nextAct, TourActivity newAct, double depTimeAtPrevAct) {
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());
//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 endTime_nextAct_new = CalculationUtils.getActivityEndTime(nextAct_arrTime, nextAct);
double act_costs_nextAct = activityCosts.getActivityCost(nextAct, nextAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct + act_costs_newAct + act_costs_nextAct;
double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct + solutionCompletenessRatio * activityCostsWeight * (act_costs_newAct + act_costs_nextAct);
double oldCosts;
double oldCosts = 0.;
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 + 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 = depTimeAtPrevAct + routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
double endTime_nextAct_old = CalculationUtils.getActivityEndTime(arrTime_nextAct,nextAct);
double actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct, iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
oldCosts = tp_costs_prevAct_nextAct + actCost_nextAct;
double endTimeDelay_nextAct = Math.max(0, endTime_nextAct_new - endTime_nextAct_old);
Double futureWaiting = stateManager.getActivityState(nextAct, iFacts.getRoute().getVehicle(), InternalStates.FUTURE_WAITING, Double.class);
if (futureWaiting == null) futureWaiting = 0.;
double waitingTime_savings_timeUnit = Math.min(futureWaiting, endTimeDelay_nextAct);
double waitingTime_savings = waitingTime_savings_timeUnit * iFacts.getRoute().getVehicle().getType().getVehicleCostParams().perWaitingTimeUnit;
oldCosts += solutionCompletenessRatio * activityCostsWeight * waitingTime_savings;
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;
}
public void setSolutionCompletenessRatio(double solutionCompletenessRatio) {
this.solutionCompletenessRatio = solutionCompletenessRatio;
}
}

View file

@ -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;

View file

@ -42,4 +42,12 @@ 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);
public static final StateId FUTURE_WAITING = new StateFactory.StateIdImpl("future_waiting",13);
public static final StateId EARLIEST_WITHOUT_WAITING = new StateFactory.StateIdImpl("earliest_without_waiting",14);
}

View file

@ -0,0 +1,60 @@
/*******************************************************************************
* 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.ReverseActivityVisitor;
import jsprit.core.problem.solution.route.activity.TourActivity;
/**
* Updates and memorizes latest operation start times at activities.
*
* @author schroeder
*
*/
public class UpdateFutureWaitingTimes implements ReverseActivityVisitor, StateUpdater{
private StateManager states;
private VehicleRoute route;
private VehicleRoutingTransportCosts transportCosts;
private double futureWaiting;
public UpdateFutureWaitingTimes(StateManager states, VehicleRoutingTransportCosts tpCosts) {
super();
this.states = states;
this.transportCosts = tpCosts;
}
@Override
public void begin(VehicleRoute route) {
this.route = route;
this.futureWaiting = 0.;
}
@Override
public void visit(TourActivity activity) {
states.putInternalTypedActivityState(activity,route.getVehicle(),InternalStates.FUTURE_WAITING,futureWaiting);
futureWaiting += Math.max(activity.getTheoreticalEarliestOperationStartTime() - activity.getArrTime(),0);
}
@Override
public void finish() {}
}

View file

@ -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() {}
}

View file

@ -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;
@ -76,19 +76,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>();

View file

@ -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;
}
}

View file

@ -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));

View file

@ -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()){

View file

@ -297,7 +297,8 @@ public class VehicleImpl extends AbstractVehicle{
endLocation = builder.endLocation;
startLocation = builder.startLocation;
aBreak = builder.aBreak;
setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(),startLocation.getId(),endLocation.getId(),earliestDeparture,latestArrival,skills));
// 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));
}
/**

View file

@ -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;
}

View file

@ -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;
}