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

Merge branch 'master' into constraints

This commit is contained in:
oblonski 2013-12-02 21:00:07 +01:00
commit 3335f235a3
30 changed files with 4851 additions and 58 deletions

View file

@ -22,6 +22,7 @@ import java.util.List;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
@ -33,10 +34,10 @@ final class AuxilliaryCostCalculator {
private final VehicleRoutingActivityCosts activityCosts;
public AuxilliaryCostCalculator(final VehicleRoutingTransportCosts routingCosts, final VehicleRoutingActivityCosts costFunction) {
public AuxilliaryCostCalculator(final VehicleRoutingTransportCosts routingCosts, final VehicleRoutingActivityCosts actCosts) {
super();
this.routingCosts = routingCosts;
this.activityCosts = costFunction;
this.activityCosts = actCosts;
}
/**
@ -59,6 +60,11 @@ final class AuxilliaryCostCalculator {
double departureTimePrevAct = depTime;
while(actIter.hasNext()){
TourActivity act = actIter.next();
if(act instanceof End){
if(!vehicle.isReturnToDepot()){
return cost;
}
}
double transportCost = routingCosts.getTransportCost(prevAct.getLocationId(), act.getLocationId(), departureTimePrevAct, driver, vehicle);
double transportTime = routingCosts.getTransportTime(prevAct.getLocationId(), act.getLocationId(), departureTimePrevAct, driver, vehicle);
cost += transportCost;
@ -72,37 +78,5 @@ final class AuxilliaryCostCalculator {
return cost;
}
public double costOfPath(String startLocationId, final double startTime, final List<TourActivity> path, String endLocationId, final Driver driver, final Vehicle vehicle){
if(path.isEmpty()){
return 0.0;
}
double cost = 0.0;
// Iterator<TourActivity> actIter = path.iterator();
String prevActLocation = startLocationId;
// TourActivity prevAct = actIter.next();
double startCost = 0.0;
cost += startCost;
double departureTimePrevAct = startTime;
for(TourActivity act : path){
// TourActivity act = actIter.next();
double transportCost = routingCosts.getTransportCost(prevActLocation, act.getLocationId(), departureTimePrevAct, driver, vehicle);
double transportTime = routingCosts.getTransportTime(prevActLocation, act.getLocationId(), departureTimePrevAct, driver, vehicle);
cost += transportCost;
double actStartTime = departureTimePrevAct + transportTime;
double earliestOperationStartTime = Math.max(actStartTime, act.getTheoreticalEarliestOperationStartTime());
double actEndTime = earliestOperationStartTime + act.getOperationTime();
departureTimePrevAct = actEndTime;
cost += activityCosts.getActivityCost(act, actStartTime, driver, vehicle);
prevActLocation = act.getLocationId();
}
/*
*!!! ENDLOCATION
=> Start u. End können primitiv sein.
*/
return cost;
}
}

View file

@ -60,12 +60,21 @@ class Inserter {
@Override
public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
if(job instanceof Service){
if(!iData.getSelectedVehicle().isReturnToDepot()){
if(iData.getDeliveryInsertionIndex()>=route.getTourActivities().getActivities().size()){
setEndLocation(route,(Service)job);
}
}
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), this.activityFactory.createActivity((Service)job));
route.setDepartureTime(iData.getVehicleDepartureTime());
}
else delegator.handleJobInsertion(job, iData, route);
}
private void setEndLocation(VehicleRoute route, Service service) {
route.getEnd().setLocationId(service.getLocationId());
}
public void setNextHandler(JobInsertionHandler jobInsertionHandler){
this.delegator = jobInsertionHandler;
}
@ -83,6 +92,11 @@ class Inserter {
if(job instanceof Shipment){
TourActivity pickupShipment = this.activityFactory.createPickup((Shipment)job);
TourActivity deliverShipment = this.activityFactory.createDelivery((Shipment)job);
if(!iData.getSelectedVehicle().isReturnToDepot()){
if(iData.getDeliveryInsertionIndex()>=route.getTourActivities().getActivities().size()){
setEndLocation(route,(Shipment)job);
}
}
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), deliverShipment);
route.getTourActivities().addActivity(iData.getPickupInsertionIndex(), pickupShipment);
route.setDepartureTime(iData.getVehicleDepartureTime());
@ -90,6 +104,10 @@ class Inserter {
else delegator.handleJobInsertion(job, iData, route);
}
private void setEndLocation(VehicleRoute route, Shipment shipment) {
route.getEnd().setLocationId(shipment.getDeliveryLocation());
}
public void setNextHandler(JobInsertionHandler jobInsertionHandler){
this.delegator = jobInsertionHandler;
}

View file

@ -23,6 +23,7 @@ package jsprit.core.algorithm.recreate;
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.util.CalculationUtils;
@ -61,6 +62,13 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
double act_costs_newAct = activityCosts.getActivityCost(newAct, newAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
//open routes
if(nextAct instanceof End){
if(!iFacts.getNewVehicle().isReturnToDepot()){
return new ActivityInsertionCosts(tp_costs_prevAct_newAct, tp_time_prevAct_newAct);
}
}
double tp_costs_newAct_nextAct = routingCosts.getTransportCost(newAct.getLocationId(), nextAct.getLocationId(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double tp_time_newAct_nextAct = routingCosts.getTransportTime(newAct.getLocationId(), nextAct.getLocationId(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());

View file

@ -230,14 +230,19 @@ public class VrpXMLReader{
if(vehicle == null) throw new IllegalStateException("vehicle is missing.");
String start = routeConfig.getString("start");
if(start == null) throw new IllegalStateException("route start-time is missing.");
double departureTime = Double.parseDouble(start);
String end = routeConfig.getString("end");
if(end == null) throw new IllegalStateException("route end-time is missing.");
Start startAct = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
startAct.setEndTime(Double.parseDouble(start));
// Start startAct = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
// startAct.setEndTime(Double.parseDouble(start));
End endAct = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
endAct.setArrTime(Double.parseDouble(end));
VehicleRoute.Builder routeBuilder = VehicleRoute.Builder.newInstance(vehicle, driver);
routeBuilder.setDepartureTime(departureTime);
routeBuilder.setRouteEndArrivalTime(Double.parseDouble(end));
List<HierarchicalConfiguration> actConfigs = routeConfig.configurationsAt("act");
for(HierarchicalConfiguration actConfig : actConfigs){
String type = actConfig.getString("[@type]");
@ -466,6 +471,10 @@ public class VrpXMLReader{
String end = vehicleConfig.getString("timeSchedule.end");
if(start != null) builder.setEarliestStart(Double.parseDouble(start));
if(end != null) builder.setLatestArrival(Double.parseDouble(end));
String returnToDepot = vehicleConfig.getString("returnToDepot");
if(returnToDepot != null){
builder.setReturnToDepot(vehicleConfig.getBoolean("returnToDepot"));
}
VehicleImpl vehicle = builder.build();
vrpBuilder.addVehicle(vehicle);
vehicleMap.put(vehicleId, vehicle);

View file

@ -234,6 +234,7 @@ public class VrpXMLWriter {
xmlConfig.setProperty(vehiclePathString + "("+counter+").timeSchedule.start", vehicle.getEarliestDeparture());
xmlConfig.setProperty(vehiclePathString + "("+counter+").timeSchedule.end", vehicle.getLatestArrival());
xmlConfig.setProperty(vehiclePathString + "("+counter+").returnToDepot", vehicle.isReturnToDepot());
counter++;
}

View file

@ -61,6 +61,8 @@ public class VehicleRoute {
private Start start;
private End end;
private TourActivities tourActivities = new TourActivities();
private TourActivityFactory serviceActivityFactory = new DefaultTourActivityFactory();
@ -79,6 +81,10 @@ public class VehicleRoute {
/**
* Constructs the route-builder.
*
* <p>Default startLocation is vehicle.getLocationId()<br>
* Default departureTime is vehicle.getEarliestDeparture()<br>
* Default endLocation is either vehicle.getLocationId() or (if !vehicle.isReturnToDepot()) last specified activityLocation
* @param vehicle
* @param driver
*/
@ -88,7 +94,7 @@ public class VehicleRoute {
this.driver = driver;
start = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
start.setEndTime(vehicle.getEarliestDeparture());
End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
end = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
}
/**
@ -102,6 +108,11 @@ public class VehicleRoute {
return this;
}
public Builder setRouteEndArrivalTime(double endTime){
end.setArrTime(endTime);
return this;
}
public Builder addService(Service service){
addService(service,0.0,0.0);
return this;
@ -185,7 +196,12 @@ public class VehicleRoute {
if(!openShipments.isEmpty()){
throw new IllegalStateException("there are still shipments that have not been delivered yet.");
}
VehicleRoute route = VehicleRoute.newInstance(tourActivities, driver, vehicle);
if(!vehicle.isReturnToDepot()){
if(!tourActivities.isEmpty()){
end.setLocationId(tourActivities.getActivities().get(tourActivities.getActivities().size()-1).getLocationId());
}
}
VehicleRoute route = new VehicleRoute(this);
return route;
}
@ -218,13 +234,14 @@ public class VehicleRoute {
setStartAndEnd(vehicle, vehicle.getEarliestDeparture());
}
// private VehicleRoute(Builder builder){
// this.tourActivities = builder.tour;
// this.vehicle = builder.vehicle;
// this.driver = builder.driver;
// this.start = builder.start;
// this.end = builder.end;
// }
private VehicleRoute(Builder builder){
this.tourActivities = builder.tourActivities;
this.vehicle = builder.vehicle;
this.driver = builder.driver;
this.start = builder.start;
this.end = builder.end;
}
private void verify(TourActivities tour, Driver driver, Vehicle vehicle) {
if(tour == null || driver == null || vehicle == null) throw new IllegalStateException("null is not allowed for tour, driver or vehicle. use emptyRoute. use Tour.emptyTour, DriverImpl.noDriver() and VehicleImpl.noVehicle() instead." +

View file

@ -35,11 +35,11 @@ public final class End implements TourActivity {
private Coordinate coordinate;
public Coordinate getCoordinate() {
Coordinate getCoordinate() {
return coordinate;
}
public void setCoordinate(Coordinate coordinate) {
void setCoordinate(Coordinate coordinate) {
this.coordinate = coordinate;
}

View file

@ -65,11 +65,11 @@ public final class Start implements TourActivity {
public Coordinate getCoordinate() {
Coordinate getCoordinate() {
return coordinate;
}
public void setCoordinate(Coordinate coordinate) {
void setCoordinate(Coordinate coordinate) {
this.coordinate = coordinate;
}

View file

@ -35,5 +35,7 @@ public interface Vehicle {
public abstract String getId();
public abstract int getCapacity();
public abstract boolean isReturnToDepot();
}

View file

@ -45,6 +45,14 @@ public class VehicleImpl implements Vehicle {
}
/**
* builds the vehicle.
*
* <p>by default, it returns to the depot.
*
* @author stefan
*
*/
public static class Builder {
static Logger log = Logger.getLogger(Builder.class);
private String id;
@ -54,6 +62,8 @@ public class VehicleImpl implements Vehicle {
private double earliestStart = 0.0;
private double latestArrival = Double.MAX_VALUE;
private boolean returnToDepot = true;
private VehicleType type = VehicleTypeImpl.Builder.newInstance("default", 0).build();
private Builder(String id) {
@ -66,6 +76,11 @@ public class VehicleImpl implements Vehicle {
return this;
}
public Builder setReturnToDepot(boolean returnToDepot){
this.returnToDepot = returnToDepot;
return this;
}
public Builder setLocationId(String id){
this.locationId = id;
return this;
@ -113,6 +128,8 @@ public class VehicleImpl implements Vehicle {
private final double earliestDeparture;
private final double latestArrival;
private boolean returnToDepot;
private VehicleImpl(Builder builder){
id = builder.id;
@ -121,6 +138,7 @@ public class VehicleImpl implements Vehicle {
locationId = builder.locationId;
earliestDeparture = builder.earliestStart;
latestArrival = builder.latestArrival;
returnToDepot = builder.returnToDepot;
}
@ -193,5 +211,16 @@ public class VehicleImpl implements Vehicle {
public int getCapacity() {
return type.getCapacity();
}
/**
* @return the returnToDepot
*/
public boolean isReturnToDepot() {
return returnToDepot;
}
}

View file

@ -53,7 +53,7 @@
</xs:element>
<xs:element name="typeId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="timeSchedule" type="timeWindowType"/>
<xs:element name="returnToDepot" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:element>