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

add getAddedServices(), getAddedVehicles() to

VehicleRoutingProblem.Builder to get a snapshot of what has already been
built
This commit is contained in:
Stefan Schroeder 2013-06-11 14:00:09 +02:00
parent 611d71f272
commit 885eab5eff
2 changed files with 28 additions and 19 deletions

View file

@ -24,7 +24,6 @@ import util.Coordinate;
import util.CrowFlyCosts; import util.CrowFlyCosts;
import util.Locations; import util.Locations;
import util.Neighborhood; import util.Neighborhood;
import util.NeighborhoodImpl;
import basics.costs.DefaultVehicleRoutingActivityCosts; import basics.costs.DefaultVehicleRoutingActivityCosts;
import basics.costs.VehicleRoutingActivityCosts; import basics.costs.VehicleRoutingActivityCosts;
import basics.costs.VehicleRoutingTransportCosts; import basics.costs.VehicleRoutingTransportCosts;
@ -69,6 +68,8 @@ public class VehicleRoutingProblem {
private VehicleRoutingActivityCosts activityCosts = new DefaultVehicleRoutingActivityCosts(); private VehicleRoutingActivityCosts activityCosts = new DefaultVehicleRoutingActivityCosts();
private Map<String,Job> jobs; private Map<String,Job> jobs;
private Collection<Service> services;
private Collection<Vehicle> vehicles; private Collection<Vehicle> vehicles;
@ -96,6 +97,7 @@ public class VehicleRoutingProblem {
vehicles = new ArrayList<Vehicle>(); vehicles = new ArrayList<Vehicle>();
coordinates = new HashMap<String, Coordinate>(); coordinates = new HashMap<String, Coordinate>();
vehicleTypes = new ArrayList<VehicleImpl.VehicleType>(); vehicleTypes = new ArrayList<VehicleImpl.VehicleType>();
services = new ArrayList<Service>();
} }
/** /**
@ -193,6 +195,7 @@ public class VehicleRoutingProblem {
coordinates.put(service.getLocationId(), service.getCoord()); coordinates.put(service.getLocationId(), service.getCoord());
if(jobs.containsKey(service.getId())){ logger.warn("service " + service + " already in job list. overrides existing job."); } if(jobs.containsKey(service.getId())){ logger.warn("service " + service + " already in job list. overrides existing job."); }
jobs.put(service.getId(),service); jobs.put(service.getId(),service);
services.add(service);
return this; return this;
} }
@ -285,6 +288,15 @@ public class VehicleRoutingProblem {
} }
return this; return this;
} }
public Collection<Vehicle> getAddedVehicles(){
return Collections.unmodifiableCollection(vehicles);
}
public Collection<Service> getAddedServices(){
return Collections.unmodifiableCollection(services);
}
} }
/** /**
@ -348,7 +360,7 @@ public class VehicleRoutingProblem {
this.vehicleTypes = builder.vehicleTypes; this.vehicleTypes = builder.vehicleTypes;
this.transportCosts = builder.transportCosts; this.transportCosts = builder.transportCosts;
this.activityCosts = builder.activityCosts; this.activityCosts = builder.activityCosts;
this.neighborhood = new NeighborhoodImpl(this); this.neighborhood = builder.neighborhood;
log.info("initialise " + this); log.info("initialise " + this);
} }

View file

@ -25,11 +25,10 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import basics.Job;
import basics.Service; import basics.Service;
import basics.VehicleRoutingProblem;
import basics.route.Vehicle; import basics.route.Vehicle;
@ -44,34 +43,35 @@ public class NeighborhoodImpl implements Neighborhood{
private boolean initialised = false; private boolean initialised = false;
private VehicleRoutingProblem vrp;
public void setThreshold(double threshold) { public void setThreshold(double threshold) {
this.threshold = threshold; this.threshold = threshold;
log.info("set threshold to " + threshold); log.info("set threshold to " + threshold);
} }
private Map<String,Set<String>> neighbors; private Map<String,Set<String>> neighbors;
private Collection<Vehicle> vehicles;
private Collection<Service> services;
public NeighborhoodImpl(VehicleRoutingProblem vrp) { public NeighborhoodImpl(Collection<Vehicle> vehicles, Collection<Service> services) {
neighborsToAll = new HashSet<String>(); neighborsToAll = new HashSet<String>();
this.vrp = vrp; this.vehicles = vehicles;
this.services = services;
neighbors = new HashMap<String, Set<String>>(); neighbors = new HashMap<String, Set<String>>();
// makeNeighbors();
} }
private void makeNeighbors() { private void makeNeighbors() {
for(Job i : vrp.getJobs().values()){ for(Service i : services){
Set<String> neigh = new HashSet<String>(); Set<String> neigh = new HashSet<String>();
for(Vehicle v : vrp.getVehicles()){ for(Vehicle v : vehicles){
double dist2depot = EuclideanDistanceCalculator.calculateDistance(v.getCoord(), ((Service)i).getCoord()); double dist2depot = EuclideanDistanceCalculator.calculateDistance(v.getCoord(), i.getCoord());
if(dist2depot <= threshold){ if(dist2depot <= threshold){
neighborsToAll.add(((Service)i).getLocationId()); neighborsToAll.add(((Service)i).getLocationId());
} }
} }
for(Job j : vrp.getJobs().values()){ for(Service j : services){
double crowFlyDistance = EuclideanDistanceCalculator.calculateDistance(((Service)i).getCoord(), ((Service)j).getCoord()); double crowFlyDistance = EuclideanDistanceCalculator.calculateDistance(i.getCoord(), j.getCoord());
if(crowFlyDistance <= threshold) { if(crowFlyDistance <= threshold) {
neigh.add(((Service)j).getLocationId()); neigh.add(((Service)j).getLocationId());
} }
@ -85,14 +85,11 @@ public class NeighborhoodImpl implements Neighborhood{
for(Vehicle v : vehicles){ for(Vehicle v : vehicles){
neighborsToAll.add(v.getLocationId()); neighborsToAll.add(v.getLocationId());
} }
// for(Job j : vrp.getJobs().values()){
//
// }
} }
public void initialise(){ public void initialise(){
log.info("initialise neighboorhood [threshold="+ this.threshold + "]"); log.info("initialise neighboorhood [threshold="+ this.threshold + "]");
makeNeighborsToAll(vrp.getVehicles()); makeNeighborsToAll(vehicles);
makeNeighbors(); makeNeighbors();
initialised = true; initialised = true;
} }