mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
shift to start/endLocationId and start/endLocationCoordinate
This commit is contained in:
parent
a234bb54c9
commit
974021cf1b
36 changed files with 220 additions and 140 deletions
|
|
@ -85,7 +85,7 @@ final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCo
|
|||
if(!(selectedVehicle instanceof NoVehicle)) {
|
||||
relevantVehicles.add(selectedVehicle);
|
||||
if(vehicleSwitchAllowed){
|
||||
relevantVehicles.addAll(fleetManager.getAvailableVehicles(selectedVehicle.getType().getTypeId(),selectedVehicle.getLocationId()));
|
||||
relevantVehicles.addAll(fleetManager.getAvailableVehicles(selectedVehicle));
|
||||
}
|
||||
}
|
||||
else{ //if no vehicle has been assigned, i.e. it is an empty route
|
||||
|
|
|
|||
|
|
@ -396,7 +396,7 @@ public class VehicleRoutingProblem {
|
|||
PenaltyVehicleType penType = new PenaltyVehicleType(t,penaltyFactor);
|
||||
String vehicleId = "penaltyVehicle_" + v.getStartLocationId() + "_" + t.getTypeId();
|
||||
Vehicle penVehicle = VehicleImpl.Builder.newInstance(vehicleId).setEarliestStart(v.getEarliestDeparture())
|
||||
.setLatestArrival(v.getLatestArrival()).setStartLocationCoordinate(v.getStartLocationCoordinate()).setLocationId(v.getStartLocationId())
|
||||
.setLatestArrival(v.getLatestArrival()).setStartLocationCoordinate(v.getStartLocationCoordinate()).setStartLocationId(v.getStartLocationId())
|
||||
.setEndLocationId(v.getEndLocationId()).setEndLocationCoordinate(v.getEndLocationCoordinate())
|
||||
.setReturnToDepot(v.isReturnToDepot()).setType(penType).build();
|
||||
addVehicle(penVehicle);
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class InfiniteVehicles implements VehicleFleetManager{
|
|||
|
||||
private void extractTypes(Collection<Vehicle> vehicles) {
|
||||
for(Vehicle v : vehicles){
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(v.getType().getTypeId(),v.getLocationId());
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocationId(),v.getEndLocationId());
|
||||
types.put(typeKey,v);
|
||||
sortedTypes.add(typeKey);
|
||||
|
||||
|
|
@ -79,10 +79,26 @@ class InfiniteVehicles implements VehicleFleetManager{
|
|||
return types.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use getAvailableVehicles(Vehicle withoutThisType) instead
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public Collection<Vehicle> getAvailableVehicles(String withoutThisType, String locationId) {
|
||||
Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType,locationId);
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType, locationId,locationId);
|
||||
for(VehicleTypeKey key : types.keySet()){
|
||||
if(!key.equals(thisKey)){
|
||||
vehicles.add(types.get(key));
|
||||
}
|
||||
}
|
||||
return vehicles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Vehicle> getAvailableVehicles(Vehicle withoutThisType) {
|
||||
Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType.getType().getTypeId(), withoutThisType.getStartLocationId(), withoutThisType.getEndLocationId());
|
||||
for(VehicleTypeKey key : types.keySet()){
|
||||
if(!key.equals(thisKey)){
|
||||
vehicles.add(types.get(key));
|
||||
|
|
|
|||
|
|
@ -31,6 +31,16 @@ public interface VehicleFleetManager {
|
|||
|
||||
public abstract Collection<Vehicle> getAvailableVehicles();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param withoutThisType
|
||||
* @param locationId
|
||||
* @return
|
||||
* @deprecated use .getAvailableVehicles(Vehicle without) instead. this might ignore withoutType and returns all available vehicles
|
||||
*/
|
||||
@Deprecated
|
||||
public Collection<Vehicle> getAvailableVehicles(String withoutThisType, String locationId);
|
||||
|
||||
public Collection<Vehicle> getAvailableVehicles(Vehicle withoutThisType);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -132,11 +132,11 @@ class VehicleFleetManagerImpl implements VehicleFleetManager {
|
|||
}
|
||||
String typeId = v.getType().getTypeId();
|
||||
if(v.getType() instanceof PenaltyVehicleType){
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(typeId,v.getLocationId());
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(typeId, v.getStartLocationId(), v.getEndLocationId());
|
||||
penaltyVehicles.put(typeKey, v);
|
||||
}
|
||||
else{
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(v.getType().getTypeId(),v.getLocationId());
|
||||
VehicleTypeKey typeKey = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocationId(), v.getEndLocationId());
|
||||
if(!typeMapOfAvailableVehicles.containsKey(typeKey)){
|
||||
typeMapOfAvailableVehicles.put(typeKey, new TypeContainer(typeKey));
|
||||
}
|
||||
|
|
@ -147,7 +147,7 @@ class VehicleFleetManagerImpl implements VehicleFleetManager {
|
|||
private void removeVehicle(Vehicle v){
|
||||
//it might be better to introduce a class PenaltyVehicle
|
||||
if(!(v.getType() instanceof PenaltyVehicleType)){
|
||||
VehicleTypeKey key = new VehicleTypeKey(v.getType().getTypeId(),v.getLocationId());
|
||||
VehicleTypeKey key = new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocationId(), v.getEndLocationId());
|
||||
if(typeMapOfAvailableVehicles.containsKey(key)){
|
||||
typeMapOfAvailableVehicles.get(key).remove(v);
|
||||
}
|
||||
|
|
@ -186,11 +186,13 @@ class VehicleFleetManagerImpl implements VehicleFleetManager {
|
|||
* @param typeId to specify the typeId that should not be the returned collection
|
||||
* @param locationId to specify the locationId that should not be in the returned collection
|
||||
* @return collection of available vehicles without the vehicles that have the typeId 'withoutThisType' AND the locationId 'withThisLocation'.
|
||||
* @deprecated use .getAvailableVehicles(Vehicle without) instead - this might ignore withoutThisType and returns all available vehicles
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public Collection<Vehicle> getAvailableVehicles(String withoutThisType, String withThisLocationId) {
|
||||
List<Vehicle> vehicles = new ArrayList<Vehicle>();
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType,withThisLocationId);
|
||||
VehicleTypeKey thisKey = new VehicleTypeKey(withoutThisType, withThisLocationId, withThisLocationId);
|
||||
for(VehicleTypeKey key : typeMapOfAvailableVehicles.keySet()){
|
||||
if(key.equals(thisKey)) continue;
|
||||
if(!typeMapOfAvailableVehicles.get(key).isEmpty()){
|
||||
|
|
@ -207,6 +209,24 @@ 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.getStartLocationId(), withoutThisType.getEndLocationId());
|
||||
for(VehicleTypeKey key : typeMapOfAvailableVehicles.keySet()){
|
||||
if(key.equals(thisKey)) continue;
|
||||
if(!typeMapOfAvailableVehicles.get(key).isEmpty()){
|
||||
vehicles.add(typeMapOfAvailableVehicles.get(key).getVehicle());
|
||||
}
|
||||
else{
|
||||
if(penaltyVehicles.containsKey(key)){
|
||||
vehicles.add(penaltyVehicles.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
return vehicles;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.matsim.contrib.freight.vrp.basics.VehicleFleetManager#lock(org.matsim.contrib.freight.vrp.basics.Vehicle)
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -136,7 +136,9 @@ public class VehicleImpl implements Vehicle {
|
|||
*
|
||||
* @param id
|
||||
* @return this builder
|
||||
* @deprecated use setStartLocationId(..) instead
|
||||
*/
|
||||
@Deprecated
|
||||
public Builder setLocationId(String id){
|
||||
this.locationId = id;
|
||||
this.startLocationId = id;
|
||||
|
|
@ -150,7 +152,9 @@ public class VehicleImpl implements Vehicle {
|
|||
*
|
||||
* @param coord
|
||||
* @return this builder
|
||||
* @deprecated use setStartLocationCoordinate(...) instead
|
||||
*/
|
||||
@Deprecated
|
||||
public Builder setLocationCoord(Coordinate coord){
|
||||
this.locationCoord = coord;
|
||||
this.startLocationCoord = coord;
|
||||
|
|
|
|||
|
|
@ -1,26 +1,45 @@
|
|||
package jsprit.core.problem.vehicle;
|
||||
|
||||
/**
|
||||
* Key to identify different vehicles
|
||||
*
|
||||
* <p>Two vehicles are equal if they share the same type and location.
|
||||
* <p>Note that earliestStart and latestArrival are ignored by this key (this might change in future)
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
class VehicleTypeKey {
|
||||
|
||||
public final String type;
|
||||
public final String locationId;
|
||||
public final String startLocationId;
|
||||
public final String endLocationId;
|
||||
|
||||
VehicleTypeKey(String typeId, String locationId) {
|
||||
VehicleTypeKey(String typeId, String startLocationId, String endLocationId) {
|
||||
super();
|
||||
this.type = typeId;
|
||||
this.locationId = locationId;
|
||||
this.startLocationId = startLocationId;
|
||||
this.endLocationId = endLocationId;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result
|
||||
+ ((locationId == null) ? 0 : locationId.hashCode());
|
||||
+ ((endLocationId == null) ? 0 : endLocationId.hashCode());
|
||||
result = prime * result
|
||||
+ ((startLocationId == null) ? 0 : startLocationId.hashCode());
|
||||
result = prime * result + ((type == null) ? 0 : type.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
|
|
@ -30,10 +49,15 @@ class VehicleTypeKey {
|
|||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
VehicleTypeKey other = (VehicleTypeKey) obj;
|
||||
if (locationId == null) {
|
||||
if (other.locationId != null)
|
||||
if (endLocationId == null) {
|
||||
if (other.endLocationId != null)
|
||||
return false;
|
||||
} else if (!locationId.equals(other.locationId))
|
||||
} else if (!endLocationId.equals(other.endLocationId))
|
||||
return false;
|
||||
if (startLocationId == null) {
|
||||
if (other.startLocationId != null)
|
||||
return false;
|
||||
} else if (!startLocationId.equals(other.startLocationId))
|
||||
return false;
|
||||
if (type == null) {
|
||||
if (other.type != null)
|
||||
|
|
@ -43,6 +67,6 @@ class VehicleTypeKey {
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -62,7 +62,7 @@ public class NeighborhoodImpl implements Neighborhood{
|
|||
for(Service i : services){
|
||||
Set<String> neigh = new HashSet<String>();
|
||||
for(Vehicle v : vehicles){
|
||||
double dist2depot = EuclideanDistanceCalculator.calculateDistance(v.getCoord(), i.getCoord());
|
||||
double dist2depot = EuclideanDistanceCalculator.calculateDistance(v.getStartLocationCoordinate(), i.getCoord());
|
||||
if(dist2depot <= threshold){
|
||||
neighborsToAll.add(((Service)i).getLocationId());
|
||||
}
|
||||
|
|
@ -80,7 +80,7 @@ public class NeighborhoodImpl implements Neighborhood{
|
|||
|
||||
private void makeNeighborsToAll(Collection<Vehicle> vehicles) {
|
||||
for(Vehicle v : vehicles){
|
||||
neighborsToAll.add(v.getLocationId());
|
||||
neighborsToAll.add(v.getStartLocationId());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,15 +21,21 @@ import java.util.Collection;
|
|||
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
|
||||
import jsprit.core.algorithm.listener.AlgorithmStartsListener;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.driver.DriverImpl;
|
||||
import jsprit.core.problem.job.Job;
|
||||
import jsprit.core.problem.job.Service;
|
||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
import jsprit.core.problem.vehicle.Vehicle;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
|
||||
/**
|
||||
* Verifies whether vrp can be solved.
|
||||
*
|
||||
* <p>Checks<br>
|
||||
* - capacities, i.e. whether all job at least fit into the biggest vehicle
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
public class VrpVerifier implements AlgorithmStartsListener{
|
||||
|
||||
private static Logger log = Logger.getLogger(VrpVerifier.class);
|
||||
|
|
@ -47,26 +53,26 @@ public class VrpVerifier implements AlgorithmStartsListener{
|
|||
}
|
||||
}
|
||||
log.info("ok");
|
||||
log.info("check vehicles can manage shuttle tours ...");
|
||||
for(Job j : problem.getJobs().values()){
|
||||
Service s = (Service)j;
|
||||
boolean jobCanBeRoutedWithinTimeWindow = false;
|
||||
for(Vehicle v : problem.getVehicles()){
|
||||
double transportTime = problem.getTransportCosts().getTransportTime(v.getLocationId(), s.getLocationId(), v.getEarliestDeparture(), DriverImpl.noDriver(), v);
|
||||
if(transportTime+v.getEarliestDeparture() < s.getTimeWindow().getEnd()){
|
||||
jobCanBeRoutedWithinTimeWindow = true;
|
||||
break;
|
||||
}
|
||||
else{
|
||||
log.warn("vehicle " + v + " needs " + transportTime + " time-units to get to " + s.getLocationId() + ". latestOperationStartTime however is " + s.getTimeWindow().getEnd());
|
||||
}
|
||||
|
||||
}
|
||||
if(!jobCanBeRoutedWithinTimeWindow){
|
||||
throw new IllegalStateException("no vehicle is able to cover the distance from depot to " + s.getLocationId() + " to meet the time-window " + s.getTimeWindow() + ".");
|
||||
}
|
||||
}
|
||||
log.info("ok");
|
||||
// log.info("check vehicles can manage shuttle tours ...");
|
||||
// for(Job j : problem.getJobs().values()){
|
||||
// Service s = (Service)j;
|
||||
// boolean jobCanBeRoutedWithinTimeWindow = false;
|
||||
// for(Vehicle v : problem.getVehicles()){
|
||||
// double transportTime = problem.getTransportCosts().getTransportTime(v.getStartLocationId(), s.getLocationId(), v.getEarliestDeparture(), DriverImpl.noDriver(), v);
|
||||
// if(transportTime+v.getEarliestDeparture() < s.getTimeWindow().getEnd()){
|
||||
// jobCanBeRoutedWithinTimeWindow = true;
|
||||
// break;
|
||||
// }
|
||||
// else{
|
||||
// log.warn("vehicle " + v + " needs " + transportTime + " time-units to get to " + s.getLocationId() + ". latestOperationStartTime however is " + s.getTimeWindow().getEnd());
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// if(!jobCanBeRoutedWithinTimeWindow){
|
||||
// throw new IllegalStateException("no vehicle is able to cover the distance from depot to " + s.getLocationId() + " to meet the time-window " + s.getTimeWindow() + ".");
|
||||
// }
|
||||
// }
|
||||
// log.info("ok");
|
||||
log.info("verifying done");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue