From 6575b6d48eddbdd5a8386f37c9663862ddae40c6 Mon Sep 17 00:00:00 2001
From: Stefan Schroeder <4sschroeder@gmail.com>
Date: Mon, 20 Jan 2014 18:05:07 +0100
Subject: [PATCH 01/22] add and test endLocationMethods to Vehicle and
VehicleImpl
Breaks code if Vehicle is implemented by another class than VehicleImpl
---
.../jsprit/core/problem/vehicle/Vehicle.java | 21 +++-
.../core/problem/vehicle/VehicleImpl.java | 114 +++++++++++++++++-
.../core/problem/vehicle/VehicleImplTest.java | 102 ++++++++++++++++
3 files changed, 235 insertions(+), 2 deletions(-)
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
index ba24cd92..bf1feb68 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
@@ -86,5 +86,24 @@ public interface Vehicle {
* @return true if isReturnToDepot
*/
public abstract boolean isReturnToDepot();
-
+
+ /**
+ * Returns the start-locationId of this vehicle.
+ */
+ public abstract String getStartLocationId();
+
+ /**
+ * Returns the start-locationCoord of this vehicle.
+ */
+ public abstract Coordinate getStartLocationCoordinate();
+
+ /**
+ * Returns the end-locationId of this vehicle.
+ */
+ public abstract String getEndLocationId();
+
+ /**
+ * Returns the end-locationCoord of this vehicle.
+ */
+ public abstract Coordinate getEndLocationCoordinate();
}
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
index 3f40b3f1..f79ec790 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
@@ -79,6 +79,12 @@ public class VehicleImpl implements Vehicle {
private double earliestStart = 0.0;
private double latestArrival = Double.MAX_VALUE;
+ private String startLocationId;
+ private Coordinate startLocationCoord;
+
+ private String endLocationId;
+ private Coordinate endLocationCoord;
+
private boolean returnToDepot = true;
private VehicleType type = VehicleTypeImpl.Builder.newInstance("default", 0).build();
@@ -109,6 +115,12 @@ public class VehicleImpl implements Vehicle {
/**
* Sets the flag whether the vehicle must return to depot or not.
*
+ *
If returnToDepot is true, the vehicle must return to specified end-location. If you
+ * omit specifying the end-location, vehicle returns to start-location (that must to be set). If
+ * you specify it, it returns to specified end-location.
+ *
+ *
If returnToDepot is false, the end-location of the vehicle is endogenous.
+ *
* @param returnToDepot
* @return this builder
*/
@@ -127,6 +139,7 @@ public class VehicleImpl implements Vehicle {
*/
public Builder setLocationId(String id){
this.locationId = id;
+ this.startLocationId = id;
return this;
}
@@ -140,6 +153,61 @@ public class VehicleImpl implements Vehicle {
*/
public Builder setLocationCoord(Coordinate coord){
this.locationCoord = coord;
+ this.startLocationCoord = coord;
+ return this;
+ }
+
+ /**
+ * Sets the start-location of this vehicle.
+ *
+ * @param startLocationId
+ * @return this builder
+ * @throws IllegalArgumentException if startLocationId is null
+ */
+ public Builder setStartLocationId(String startLocationId){
+ if(startLocationId == null) throw new IllegalArgumentException("startLocationId cannot be null");
+ this.startLocationId = startLocationId;
+ this.locationId = startLocationId;
+ return this;
+ }
+
+ /**
+ * Sets the start-coordinate of this vehicle.
+ *
+ * @param coord
+ * @return this builder
+ * @throws IllegalArgumentException if start-coordinate is null
+ */
+ public Builder setStartLocationCoordinate(Coordinate coord){
+ if(coord == null) throw new IllegalArgumentException("start-coordinate must not be null");
+ this.startLocationCoord = coord;
+ this.locationCoord = coord;
+ return this;
+ }
+
+ /**
+ * Sets the end-locationId of this vehicle.
+ *
+ * @param endLocationId
+ * @return this builder
+ * @throws IllegalArgumentException if endLocation is null
+ */
+ public Builder setEndLocationId(String endLocationId){
+ if(endLocationId == null) throw new IllegalArgumentException("end-locationId must not be null");
+ this.endLocationId = endLocationId;
+ return this;
+ }
+
+ /**
+ * Sets the end-coordinate of this vehicle.
+ *
+ * @param coord
+ * @return this builder
+ * @throws IllegalArgumentException if coord is null
+ */
+ public Builder setEndLocationCoordinate(Coordinate coord){
+ if(coord == null) throw new IllegalArgumentException("end-coordinate must not be null");
+ this.endLocationCoord = coord;
return this;
}
@@ -175,9 +243,21 @@ public class VehicleImpl implements Vehicle {
* @throw IllegalStateException if both locationId and locationCoord is not set
*/
public VehicleImpl build(){
- if(locationId == null && locationCoord != null) locationId = locationCoord.toString();
+ if(locationId == null && locationCoord != null) {
+ locationId = locationCoord.toString();
+ startLocationId = locationCoord.toString();
+ }
if(locationId == null && locationCoord == null) throw new IllegalStateException("locationId and locationCoord is missing.");
if(locationCoord == null) log.warn("locationCoord for vehicle " + id + " is missing.");
+ if(endLocationCoord != null && returnToDepot == false) throw new IllegalStateException("this must not be. you specified both endLocationCoord and open-routes. this is contradictory. " +
+ "if you set endLocation, returnToDepot must be true. if returnToDepot is false, endLocationCoord must not be specified.");
+ if(endLocationId != null && returnToDepot == false) throw new IllegalStateException("this must not be. you specified both endLocationId and open-routes. this is contradictory. " +
+ "if you set endLocation, returnToDepot must be true. if returnToDepot is false, endLocationCoord must not be specified.");
+ if(endLocationId == null && endLocationCoord != null) endLocationId = endLocationCoord.toString();
+ if(endLocationId == null && endLocationCoord == null) {
+ endLocationId = startLocationId;
+ endLocationCoord = startLocationCoord;
+ }
return new VehicleImpl(this);
}
@@ -216,6 +296,14 @@ public class VehicleImpl implements Vehicle {
private final boolean returnToDepot;
+ private final Coordinate endLocationCoord;
+
+ private final String endLocationId;
+
+ private final Coordinate startLocationCoord;
+
+ private final String startLocationId;
+
private VehicleImpl(Builder builder){
id = builder.id;
type = builder.type;
@@ -224,6 +312,10 @@ public class VehicleImpl implements Vehicle {
earliestDeparture = builder.earliestStart;
latestArrival = builder.latestArrival;
returnToDepot = builder.returnToDepot;
+ startLocationId = builder.startLocationId;
+ startLocationCoord = builder.startLocationCoord;
+ endLocationId = builder.endLocationId;
+ endLocationCoord = builder.endLocationCoord;
}
/**
@@ -273,5 +365,25 @@ public class VehicleImpl implements Vehicle {
public boolean isReturnToDepot() {
return returnToDepot;
}
+
+ @Override
+ public String getStartLocationId() {
+ return this.startLocationId;
+ }
+
+ @Override
+ public Coordinate getStartLocationCoordinate() {
+ return this.startLocationCoord;
+ }
+
+ @Override
+ public String getEndLocationId() {
+ return this.endLocationId;
+ }
+
+ @Override
+ public Coordinate getEndLocationCoordinate() {
+ return this.endLocationCoord;
+ }
}
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java
index 5f3fa4cd..6fd2fb2b 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java
@@ -98,5 +98,107 @@ public class VehicleImplTest {
assertEquals("noVehicle",v.getId());
}
+ @Test
+ public void whenStartLocationIsSet_itIsDoneCorrectly(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("startLoc").build();
+ assertEquals("startLoc", v.getLocationId());
+ assertEquals("startLoc", v.getStartLocationId());
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void whenStartLocationIsNull_itThrowsException(){
+ @SuppressWarnings("unused")
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId(null).build();
+ }
+
+ @Test
+ public void whenStartLocationCoordIsSet_itIsDoneCorrectly(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(1, 2)).build();
+ assertEquals(1.0, v.getStartLocationCoordinate().getX(),0.01);
+ assertEquals(2.0, v.getStartLocationCoordinate().getY(),0.01);
+
+ assertEquals(1.0, v.getCoord().getX(),0.01);
+ assertEquals(2.0, v.getCoord().getY(),0.01);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void whenStartLocationCoordIsNull_itThrowsException(){
+ @SuppressWarnings("unused")
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(null).build();
+ }
+
+ @Test
+ public void whenEndLocationIsSet_itIsDoneCorrectly(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("startLoc").setEndLocationId("endLoc").build();
+ assertEquals("startLoc", v.getStartLocationId());
+ assertEquals("endLoc", v.getEndLocationId());
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void whenEndLocationIsNull_itThrowsException(){
+ @SuppressWarnings("unused")
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setEndLocationId(null).build();
+ }
+
+ @Test
+ public void whenEndLocationCoordIsSet_itIsDoneCorrectly(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("startLoc").setEndLocationCoordinate(Coordinate.newInstance(1, 2)).build();
+ assertEquals(1.0, v.getEndLocationCoordinate().getX(),0.01);
+ assertEquals(2.0, v.getEndLocationCoordinate().getY(),0.01);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void whenEndLocationCoordIsNull_itThrowsException(){
+ @SuppressWarnings("unused")
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setEndLocationCoordinate(null).build();
+ }
+
+ @Test
+ public void whenNeitherEndLocationIdNorEndLocationCoordAreSet_endLocationIdMustBeEqualToStartLocationId(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("startLoc").build();
+ assertEquals("startLoc", v.getEndLocationId());
+ }
+
+ @Test
+ public void whenNeitherEndLocationIdNorEndLocationCoordAreSet_endLocationCoordMustBeEqualToStartLocationCoord(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("startLoc").build();
+ assertEquals(v.getEndLocationCoordinate(), v.getStartLocationCoordinate());
+ }
+
+ @Test
+ public void whenNeitherEndLocationIdNorEndLocationCoordAreSet_endLocationCoordMustBeEqualToStartLocationCoordV2(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(1.0, 2.0)).build();
+ assertEquals(v.getEndLocationCoordinate(), v.getStartLocationCoordinate());
+ }
+
+ @Test
+ public void whenEndLocationCoordinateIsSetButNoId_idMustBeCoordToString(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(1.0, 2.0)).setEndLocationCoordinate(Coordinate.newInstance(3.0, 4.0)).build();
+ assertEquals(v.getEndLocationCoordinate().toString(), v.getEndLocationId());
+ }
+
+ @Test(expected=IllegalStateException.class)
+ public void whenEndLocationIdIsSpecifiedANDReturnToDepotIsFalse_itShouldThrowException(){
+ @SuppressWarnings("unused")
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(1.0, 2.0)).setEndLocationId("endLoc").setReturnToDepot(false).build();
+ }
+
+ @Test(expected=IllegalStateException.class)
+ public void whenEndLocationCoordIsSpecifiedANDReturnToDepotIsFalse_itShouldThrowException(){
+ @SuppressWarnings("unused")
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(1.0, 2.0)).setEndLocationCoordinate(Coordinate.newInstance(3, 4)).setReturnToDepot(false).build();
+ }
+
+ @Test
+ public void whenEndLocationCoordIsNotSpecifiedANDReturnToDepotIsFalse_endLocationCoordMustBeStartLocationCoord(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(1.0, 2.0)).setReturnToDepot(false).build();
+ assertEquals(v.getStartLocationCoordinate(),v.getEndLocationCoordinate());
+ }
+
+ @Test
+ public void whenEndLocationIdIsNotSpecifiedANDReturnToDepotIsFalse_endLocationIdMustBeStartLocationId(){
+ Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(1.0, 2.0)).setReturnToDepot(false).build();
+ assertEquals(v.getStartLocationCoordinate().toString(),v.getEndLocationId());
+ }
}
From 63060f3b350e5c4462089804b52b02ad6d9cbae0 Mon Sep 17 00:00:00 2001
From: Stefan Schroeder <4sschroeder@gmail.com>
Date: Wed, 22 Jan 2014 17:02:31 +0100
Subject: [PATCH 02/22] modify problem reader/writer to deal vehicles that can
start AND end at specified locations
---
.../jsprit/core/problem/io/VrpXMLReader.java | 32 ++++-
.../jsprit/core/problem/io/VrpXMLWriter.java | 13 +-
.../jsprit/core/problem/vehicle/Vehicle.java | 1 +
.../core/problem/vehicle/VehicleImpl.java | 15 ++-
.../src/main/resources/vrp_xml_schema.xsd | 26 ++--
.../core/problem/io/VrpReaderV2Test.java | 126 +++++++++++++++++-
.../core/problem/io/VrpWriterV2Test.java | 73 ++++++++--
.../core/problem/vehicle/VehicleImplTest.java | 13 ++
.../resources/finiteVrpForReaderV2Test.xml | 33 +++++
.../test/resources/infiniteWriterV2Test.xml | 16 ++-
10 files changed, 310 insertions(+), 38 deletions(-)
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java
index 4b8dfe2f..a6895d41 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java
@@ -472,20 +472,44 @@ public class VrpXMLReader{
if(type == null) throw new IllegalStateException("vehicleType with typeId " + typeId + " is missing.");
builder.setType(type);
String locationId = vehicleConfig.getString("location.id");
+ if(locationId == null) {
+ locationId = vehicleConfig.getString("startLocation.id");
+ }
if(locationId == null) throw new IllegalStateException("location.id is missing.");
- builder.setLocationId(locationId);
+ builder.setStartLocationId(locationId);
String coordX = vehicleConfig.getString("location.coord[@x]");
String coordY = vehicleConfig.getString("location.coord[@y]");
if(coordX == null || coordY == null) {
+ coordX = vehicleConfig.getString("startLocation.coord[@x]");
+ coordY = vehicleConfig.getString("startLocation.coord[@y]");
+ }
+ if(coordX == null || coordY == null) {
+ if(!doNotWarnAgain) {
+ logger.warn("location.coord is missing. will not warn you again.");
+ doNotWarnAgain = true;
+ }
+ }
+ else{
+ Coordinate coordinate = Coordinate.newInstance(Double.parseDouble(coordX), Double.parseDouble(coordY));
+ builder.setStartLocationCoordinate(coordinate);
+ }
+
+ String endLocationId = vehicleConfig.getString("endLocation.id");
+ if(endLocationId != null) builder.setEndLocationId(endLocationId);
+ String endCoordX = vehicleConfig.getString("endLocation.coord[@x]");
+ String endCoordY = vehicleConfig.getString("endLocation.coord[@y]");
+ if(endCoordX == null || endCoordY == null) {
if(!doNotWarnAgain) {
- logger.warn("location.coord is missing. do not warn you again.");
+ logger.warn("endLocation.coord is missing. will not warn you again.");
doNotWarnAgain = true;
}
}
else{
- Coordinate coordinate = Coordinate.newInstance(Double.parseDouble(coordX), Double.parseDouble(coordY));
- builder.setLocationCoord(coordinate);
+ Coordinate coordinate = Coordinate.newInstance(Double.parseDouble(endCoordX), Double.parseDouble(endCoordY));
+ builder.setEndLocationCoordinate(coordinate);
}
+
+
String start = vehicleConfig.getString("timeSchedule.start");
String end = vehicleConfig.getString("timeSchedule.end");
if(start != null) builder.setEarliestStart(Double.parseDouble(start));
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLWriter.java b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLWriter.java
index 231779d1..01bba935 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLWriter.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLWriter.java
@@ -236,10 +236,15 @@ public class VrpXMLWriter {
}
xmlConfig.setProperty(vehiclePathString + "("+counter+").id", vehicle.getId());
xmlConfig.setProperty(vehiclePathString + "("+counter+").typeId", vehicle.getType().getTypeId());
- xmlConfig.setProperty(vehiclePathString + "("+counter+").location.id", vehicle.getLocationId());
- if(vehicle.getCoord() != null){
- xmlConfig.setProperty(vehiclePathString + "("+counter+").location.coord[@x]", vehicle.getCoord().getX());
- xmlConfig.setProperty(vehiclePathString + "("+counter+").location.coord[@y]", vehicle.getCoord().getY());
+ xmlConfig.setProperty(vehiclePathString + "("+counter+").startLocation.id", vehicle.getStartLocationId());
+ if(vehicle.getStartLocationCoordinate() != null){
+ xmlConfig.setProperty(vehiclePathString + "("+counter+").startLocation.coord[@x]", vehicle.getStartLocationCoordinate().getX());
+ xmlConfig.setProperty(vehiclePathString + "("+counter+").startLocation.coord[@y]", vehicle.getStartLocationCoordinate().getY());
+ }
+ xmlConfig.setProperty(vehiclePathString + "("+counter+").endLocation.id", vehicle.getEndLocationId());
+ if(vehicle.getEndLocationCoordinate() != null){
+ xmlConfig.setProperty(vehiclePathString + "("+counter+").endLocation.coord[@x]", vehicle.getEndLocationCoordinate().getX());
+ xmlConfig.setProperty(vehiclePathString + "("+counter+").endLocation.coord[@y]", vehicle.getEndLocationCoordinate().getY());
}
xmlConfig.setProperty(vehiclePathString + "("+counter+").timeSchedule.start", vehicle.getEarliestDeparture());
xmlConfig.setProperty(vehiclePathString + "("+counter+").timeSchedule.end", vehicle.getLatestArrival());
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
index bf1feb68..6176910c 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
@@ -99,6 +99,7 @@ public interface Vehicle {
/**
* Returns the end-locationId of this vehicle.
+ *
*/
public abstract String getEndLocationId();
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
index f79ec790..90ac6325 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
@@ -239,8 +239,15 @@ public class VehicleImpl implements Vehicle {
*
if {@link VehicleType} is not set, default vehicle-type is set with id="default" and
* capacity=0
*
+ *
if startLocationId || locationId is null (=> startLocationCoordinate || locationCoordinate must be set) then startLocationId=startLocationCoordinate.toString()
+ * and locationId=locationCoordinate.toString() [coord.toString() --> [x=x_val][y=y_val])
+ *
if endLocationId is null and endLocationCoordinate is set then endLocationId=endLocationCoordinate.toString()
+ *
This implies the following:
* if start and end are null, new start and end activities are created.
- *
startActivity is initialized with the location of the specified vehicle. the time-window of this activity is initialized
- * as follows: [time-window.start = vehicle.getEarliestDeparture()][time-window.end = vehicle.getLatestArrival()]
- *
endActivity is initialized with the location of the specified vehicle as well. time-window of this activity:[time-window.start = vehicle.getEarliestDeparture()][time-window.end = vehicle.getLatestArrival()]
- *
start.endTime is set to the specified departureTime
- *
Note that start end end-locations are always initialized with the location of the specified vehicle. (this will change soon, then there will be start and end location of vehicle which can be different, 23.01.14)
+ *
startActivity is initialized with the start-location of the specified vehicle (vehicle.getStartLocationId()). the time-window of this activity is initialized
+ * such that [startActivity.getTheoreticalEarliestOperationStartTime() = vehicle.getEarliestDeparture()][startActivity.getTheoreticalLatestOperationStartTime() = vehicle.getLatestArrival()]
+ *
endActivity is initialized with the end-location of the specified vehicle (vehicle.getEndLocationId()). The time-window of the
+ * endActivity is initialized such that [endActivity.getTheoreticalEarliestOperationStartTime() = vehicle.getEarliestDeparture()][endActivity.getTheoreticalLatestOperationStartTime() = vehicle.getLatestArrival()]
+ *
startActivity.endTime (startActivity.getEndTime()) is set to max{vehicle.getEarliestDeparture(), vehicleDepTime}.
+ * thus, vehicle.getEarliestDeparture() is a physical constraint that has to be met.
*
* @param vehicle
* @param vehicleDepTime
*/
+ public void setVehicleAndDepartureTime(Vehicle vehicle, double vehicleDepTime){
+ this.vehicle = vehicle;
+ if(start == null && end == null){
+ start = Start.newInstance(vehicle.getStartLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
+ end = End.newInstance(vehicle.getEndLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
+ }
+ start.setEndTime(Math.max(vehicleDepTime, vehicle.getEarliestDeparture()));
+ start.setTheoreticalEarliestOperationStartTime(vehicle.getEarliestDeparture());
+ start.setTheoreticalLatestOperationStartTime(vehicle.getLatestArrival());
+ start.setLocationId(vehicle.getLocationId());
+ end.setLocationId(vehicle.getLocationId());
+ end.setTheoreticalEarliestOperationStartTime(vehicle.getEarliestDeparture());
+ end.setTheoreticalLatestOperationStartTime(vehicle.getLatestArrival());
+ }
+
+ /**
+ * Sets the vehicle and its departureTime from vehicle.getStartLocationId().
+ *
+ *
This implies the following:
+ * if start and end are null, new start and end activities are created.
+ *
startActivity is initialized with the start-location of the specified vehicle (vehicle.getStartLocationId()). the time-window of this activity is initialized
+ * such that [startActivity.getTheoreticalEarliestOperationStartTime() = vehicle.getEarliestDeparture()][startActivity.getTheoreticalLatestOperationStartTime() = vehicle.getLatestArrival()]
+ *
endActivity is initialized with the end-location of the specified vehicle (vehicle.getEndLocationId()). The time-window of the
+ * endActivity is initialized such that [endActivity.getTheoreticalEarliestOperationStartTime() = vehicle.getEarliestDeparture()][endActivity.getTheoreticalLatestOperationStartTime() = vehicle.getLatestArrival()]
+ *
startActivity.endTime (startActivity.getEndTime()) is set to max{vehicle.getEarliestDeparture(), vehicleDepTime}.
+ * thus, vehicle.getEarliestDeparture() is a physical constraint that has to be met.
+ *
+ * @param vehicle
+ * @param vehicleDepTime
+ * @deprecated use .setVehicleAndDepartureTime(Vehicle vehicle, double vehicleDepTime) instead
+ */
+ @Deprecated
public void setVehicle(Vehicle vehicle, double vehicleDepTime){
this.vehicle = vehicle;
setStartAndEnd(vehicle, vehicleDepTime);
@@ -393,14 +426,14 @@ public class VehicleRoute {
private void setStartAndEnd(Vehicle vehicle, double vehicleDepTime) {
if(!(vehicle instanceof NoVehicle)){
if(start == null && end == null){
- start = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
- end = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
+ start = Start.newInstance(vehicle.getStartLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
+ end = End.newInstance(vehicle.getEndLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
}
- start.setEndTime(vehicleDepTime);
+ start.setEndTime(Math.max(vehicleDepTime, vehicle.getEarliestDeparture()));
start.setTheoreticalEarliestOperationStartTime(vehicle.getEarliestDeparture());
start.setTheoreticalLatestOperationStartTime(vehicle.getLatestArrival());
- start.setLocationId(vehicle.getLocationId());
- end.setLocationId(vehicle.getLocationId());
+ start.setLocationId(vehicle.getStartLocationId());
+ end.setLocationId(vehicle.getEndLocationId());
end.setTheoreticalEarliestOperationStartTime(vehicle.getEarliestDeparture());
end.setTheoreticalLatestOperationStartTime(vehicle.getLatestArrival());
}
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
index afa57d79..135ee7ec 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
@@ -246,5 +246,9 @@ public class TestVehicleRoute {
assertEquals(2,count);
}
}
+
+ public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustBeBuiltCorrectly(){
+
+ }
}
From 6552632a70b4531fb784e73f2c19d1d5bd3ad74a Mon Sep 17 00:00:00 2001
From: Stefan Schroeder <4sschroeder@gmail.com>
Date: Mon, 27 Jan 2014 17:57:35 +0100
Subject: [PATCH 07/22] modify to remove warnings
---
.../main/java/jsprit/core/algorithm/recreate/Inserter.java | 2 +-
.../recreate/ServiceInsertionAndLoadConstraintsTest.java | 2 +-
.../algorithm/recreate/ShipmentInsertionCalculatorTest.java | 4 ++--
3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java
index c7f2a593..6c4d69d2 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java
@@ -133,7 +133,7 @@ class Inserter {
if(!(vehicleRoute.getVehicle().getId().toString().equals(insertionData.getSelectedVehicle().getId().toString()))){
insertionListeners.informVehicleSwitched(vehicleRoute, vehicleRoute.getVehicle(), insertionData.getSelectedVehicle());
// log.debug("vehicle switched from " + vehicleRoute.getVehicle().getId() + " to " + insertionData.getSelectedVehicle().getId());
- vehicleRoute.setVehicle(insertionData.getSelectedVehicle(), insertionData.getVehicleDepartureTime());
+ vehicleRoute.setVehicleAndDepartureTime(insertionData.getSelectedVehicle(), insertionData.getVehicleDepartureTime());
}
jobInsertionHandler.handleJobInsertion(job, insertionData, vehicleRoute);
insertionListeners.informJobInserted(job, vehicleRoute, insertionData.getInsertionCost(), insertionData.getAdditionalTime());
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
index 20093453..874264dc 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
@@ -109,7 +109,7 @@ public class ServiceInsertionAndLoadConstraintsTest {
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setLocationId("0,0").setType(type).build();
VehicleRoute route = VehicleRoute.emptyRoute();
- route.setVehicle(vehicle, 0.0);
+ route.setVehicleAndDepartureTime(vehicle, 0.0);
Inserter inserter = new Inserter(new InsertionListeners());
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
index 0937f9ee..655e754e 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
@@ -181,7 +181,7 @@ public class ShipmentInsertionCalculatorTest {
VehicleRoute route = VehicleRoute.emptyRoute();
- route.setVehicle(vehicle, 0.0);
+ route.setVehicleAndDepartureTime(vehicle, 0.0);
Inserter inserter = new Inserter(new InsertionListeners());
@@ -212,7 +212,7 @@ public class ShipmentInsertionCalculatorTest {
Shipment shipment = Shipment.Builder.newInstance("s", 1).setPickupLocation("0,10").setDeliveryLocation("0,0").build();
Shipment shipment2 = Shipment.Builder.newInstance("s2", 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build();
VehicleRoute route = VehicleRoute.emptyRoute();
- route.setVehicle(vehicle, 0.0);
+ route.setVehicleAndDepartureTime(vehicle, 0.0);
Inserter inserter = new Inserter(new InsertionListeners());
From e51b0c96a8c9dd57f28d89373742a1417fbaf528 Mon Sep 17 00:00:00 2001
From: Stefan Schroeder <4sschroeder@gmail.com>
Date: Tue, 28 Jan 2014 14:34:07 +0100
Subject: [PATCH 08/22] add tests for VehicleRoute due to adding diff start-
and end-locations deprecate VehicleRoute.newInstance(...) and remove
resulting warnings
---
.../core/algorithm/recreate/Inserter.java | 5 +-
.../problem/solution/route/VehicleRoute.java | 78 ++++--
.../core/problem/vehicle/VehicleImpl.java | 3 +
.../TestCalculatesServiceInsertion.java | 40 +--
...alculatesServiceInsertionOnRouteLevel.java | 16 +-
.../core/algorithm/recreate/TestInserter.java | 6 +
.../TestTourStateUpdaterWithService.java | 32 +--
.../solution/route/TestVehicleRoute.java | 240 ++++++++++--------
.../route/VehicleRouteBuilderTest.java | 1 +
9 files changed, 233 insertions(+), 188 deletions(-)
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java
index 6c4d69d2..2a2c5fd0 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java
@@ -60,13 +60,13 @@ class Inserter {
@Override
public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
if(job instanceof Service){
+ route.setVehicleAndDepartureTime(iData.getSelectedVehicle(),iData.getVehicleDepartureTime());
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);
}
@@ -92,6 +92,7 @@ class Inserter {
if(job instanceof Shipment){
TourActivity pickupShipment = this.activityFactory.createPickup((Shipment)job);
TourActivity deliverShipment = this.activityFactory.createDelivery((Shipment)job);
+ route.setVehicleAndDepartureTime(iData.getSelectedVehicle(),iData.getVehicleDepartureTime());
if(!iData.getSelectedVehicle().isReturnToDepot()){
if(iData.getDeliveryInsertionIndex()>=route.getActivities().size()){
setEndLocation(route,(Shipment)job);
@@ -99,7 +100,6 @@ class Inserter {
}
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), deliverShipment);
route.getTourActivities().addActivity(iData.getPickupInsertionIndex(), pickupShipment);
- route.setDepartureTime(iData.getVehicleDepartureTime());
}
else delegator.handleJobInsertion(job, iData, route);
}
@@ -132,7 +132,6 @@ class Inserter {
if(job == null) throw new IllegalStateException("cannot insert null-job");
if(!(vehicleRoute.getVehicle().getId().toString().equals(insertionData.getSelectedVehicle().getId().toString()))){
insertionListeners.informVehicleSwitched(vehicleRoute, vehicleRoute.getVehicle(), insertionData.getSelectedVehicle());
-// log.debug("vehicle switched from " + vehicleRoute.getVehicle().getId() + " to " + insertionData.getSelectedVehicle().getId());
vehicleRoute.setVehicleAndDepartureTime(insertionData.getSelectedVehicle(), insertionData.getVehicleDepartureTime());
}
jobInsertionHandler.handleJobInsertion(job, insertionData, vehicleRoute);
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java b/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java
index 835458a6..86e66106 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java
@@ -64,8 +64,10 @@ public class VehicleRoute {
* @param tour
* @param driver
* @param vehicle
- * @return
+ * @return VehicleRoute
+ * @deprecated use VehicleRoute.Builder instead
*/
+ @Deprecated
public static VehicleRoute newInstance(TourActivities tour, Driver driver, Vehicle vehicle) {
return new VehicleRoute(tour,driver,vehicle);
}
@@ -78,7 +80,7 @@ public class VehicleRoute {
* @return
*/
public static VehicleRoute emptyRoute() {
- return new VehicleRoute(TourActivities.emptyTour(), DriverImpl.noDriver(), VehicleImpl.createNoVehicle());
+ return Builder.newInstance(VehicleImpl.createNoVehicle(), DriverImpl.noDriver()).build();
}
/**
@@ -92,11 +94,19 @@ public class VehicleRoute {
/**
* Returns new instance of this builder.
*
+ *
earliestEnd == 0.0
+ *
* @param vehicle
* @param driver
* @return this builder
*/
public static Builder newInstance(Vehicle vehicle, Driver driver){
+ if(vehicle == null || driver == null) throw new IllegalArgumentException("null arguments not accepted. ini emptyRoute with VehicleImpl.createNoVehicle() and DriverImpl.noDriver()");
return new Builder(vehicle,driver);
}
@@ -141,9 +151,11 @@ public class VehicleRoute {
/**
* Constructs the route-builder.
*
- *
Default startLocation is vehicle.getLocationId()
- * Default departureTime is vehicle.getEarliestDeparture()
- * Default endLocation is either vehicle.getLocationId() or (if !vehicle.isReturnToDepot()) last specified activityLocation
+ *
earliestEnd == 0.0
* @param vehicle
* @param driver
*/
@@ -151,18 +163,22 @@ public class VehicleRoute {
super();
this.vehicle = vehicle;
this.driver = driver;
- start = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
+ start = Start.newInstance(vehicle.getStartLocationId(), vehicle.getEarliestDeparture(), Double.MAX_VALUE);
start.setEndTime(vehicle.getEarliestDeparture());
- end = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
+ end = End.newInstance(vehicle.getLocationId(), 0.0, vehicle.getLatestArrival());
}
/**
* Sets the departure-time of the route, i.e. which is the time the vehicle departs from start-location.
*
+ *
Note that departureTime cannot be lower than earliestDepartureTime of vehicle.
+ *
* @param departureTime
- * @return
+ * @return builder
+ * @throws IllegalArgumentException if departureTime < vehicle.getEarliestDeparture()
*/
public Builder setDepartureTime(double departureTime){
+ if(departureTime < start.getEndTime()) throw new IllegalArgumentException("departureTime < vehicle.getEarliestDepartureTime(). this must not be.");
start.setEndTime(departureTime);
return this;
}
@@ -172,8 +188,10 @@ public class VehicleRoute {
*
* @param endTime
* @return this builder
+ * @throws IllegalArgumentException if endTime > vehicle.getLatestArrival()
*/
public Builder setRouteEndArrivalTime(double endTime){
+ if(endTime > vehicle.getLatestArrival()) throw new IllegalArgumentException("endTime > vehicle.getLatestArrival(). this must not be.");
end.setArrTime(endTime);
return this;
}
@@ -306,6 +324,11 @@ public class VehicleRoute {
private End end;
+ /**
+ * Copy constructor copying a route.
+ *
+ * @param route
+ */
private VehicleRoute(VehicleRoute route){
this.start = Start.copyOf(route.getStart());
this.end = End.copyOf(route.getEnd());
@@ -314,6 +337,7 @@ public class VehicleRoute {
this.driver = route.getDriver();
}
+ @Deprecated
private VehicleRoute(TourActivities tour, Driver driver, Vehicle vehicle) {
super();
verify(tour, driver, vehicle);
@@ -323,7 +347,11 @@ public class VehicleRoute {
setStartAndEnd(vehicle, vehicle.getEarliestDeparture());
}
-
+ /**
+ * Constructs route.
+ *
+ * @param builder
+ */
private VehicleRoute(Builder builder){
this.tourActivities = builder.tourActivities;
this.vehicle = builder.vehicle;
@@ -332,6 +360,14 @@ public class VehicleRoute {
this.end = builder.end;
}
+ /**
+ *
+ * @param tour
+ * @param driver
+ * @param vehicle
+ * @deprecated verification is a task of VehicleRoute.Builder
+ */
+ @Deprecated
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." +
"\n\tor make it easier and use VehicleRoute.emptyRoute()");
@@ -349,6 +385,11 @@ public class VehicleRoute {
return Collections.unmodifiableList(tourActivities.getActivities());
}
+ /**
+ * Returns TourActivities.
+ *
+ * @return {@link TourActivities}
+ */
public TourActivities getTourActivities() {
return tourActivities;
}
@@ -388,17 +429,7 @@ public class VehicleRoute {
*/
public void setVehicleAndDepartureTime(Vehicle vehicle, double vehicleDepTime){
this.vehicle = vehicle;
- if(start == null && end == null){
- start = Start.newInstance(vehicle.getStartLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
- end = End.newInstance(vehicle.getEndLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
- }
- start.setEndTime(Math.max(vehicleDepTime, vehicle.getEarliestDeparture()));
- start.setTheoreticalEarliestOperationStartTime(vehicle.getEarliestDeparture());
- start.setTheoreticalLatestOperationStartTime(vehicle.getLatestArrival());
- start.setLocationId(vehicle.getLocationId());
- end.setLocationId(vehicle.getLocationId());
- end.setTheoreticalEarliestOperationStartTime(vehicle.getEarliestDeparture());
- end.setTheoreticalLatestOperationStartTime(vehicle.getLatestArrival());
+ setStartAndEnd(vehicle, vehicleDepTime);
}
/**
@@ -444,14 +475,17 @@ public class VehicleRoute {
* Sets departureTime of this route, i.e. the time the vehicle departs from its start-location.
*
* @param vehicleDepTime
+ * @deprecated use .setVehicleAndDepartureTime(...) instead (vehicle requires departureTime and the other way around, and earliestDepartureTime
+ * of a vehicle is a physical constraint of the vehicle and cannot be broken. Using this method might break this constraint.)
*/
+ @Deprecated
public void setDepartureTime(double vehicleDepTime){
if(start == null) throw new IllegalStateException("cannot set departureTime without having a vehicle on this route. use setVehicle(vehicle,departureTime) instead.");
start.setEndTime(vehicleDepTime);
}
/**
- * Returns the departureTime of this vehicle.
+ * Returns the departureTime of this vehicle in this route.
*
* @return departureTime
* @throws IllegalStateException if start is null
@@ -464,7 +498,7 @@ public class VehicleRoute {
/**
* Returns tour if tour-activity-sequence is empty, i.e. to activity on the tour yet.
*
- * @return
+ * @return true if route is empty
*/
public boolean isEmpty() {
return tourActivities.isEmpty();
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
index 90ac6325..6a534e7e 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
@@ -250,6 +250,9 @@ public class VehicleImpl implements Vehicle {
* or (endLocationId!=null AND returnToDepot=false)
*/
public VehicleImpl build(){
+ if((locationId == null && locationCoord == null) && (startLocationId == null && startLocationCoord == null)){
+ throw new IllegalStateException("vehicle requires startLocation. but neither locationId nor locationCoord nor startLocationId nor startLocationCoord has been set");
+ }
if(locationId == null && locationCoord != null) {
locationId = locationCoord.toString();
startLocationId = locationCoord.toString();
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
index 675bcb98..9e820e3f 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
@@ -34,9 +34,7 @@ import jsprit.core.problem.driver.DriverImpl.NoDriver;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.route.VehicleRoute;
-import jsprit.core.problem.solution.route.activity.ServiceActivity;
import jsprit.core.problem.solution.route.activity.TimeWindow;
-import jsprit.core.problem.solution.route.activity.TourActivities;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
@@ -184,12 +182,9 @@ public class TestCalculatesServiceInsertion {
@Test
public void whenInsertingTheFirstJobInAnEmptyTourWithVehicle_itCalculatesMarginalCostChanges(){
- TourActivities tour = new TourActivities();
-
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).build();
states.informInsertionStarts(Arrays.asList(route), null);
-// stateUpdater.update(route);
-
+
InsertionData iData = serviceInsertion.getInsertionData(route, first, vehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
assertEquals(20.0, iData.getInsertionCost(), 0.2);
assertEquals(0, iData.getDeliveryInsertionIndex());
@@ -197,10 +192,7 @@ public class TestCalculatesServiceInsertion {
@Test
public void whenInsertingTheSecondJobInAnNonEmptyTourWithVehicle_itCalculatesMarginalCostChanges(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(first));
-
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, second, vehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -210,11 +202,7 @@ public class TestCalculatesServiceInsertion {
@Test
public void whenInsertingThirdJobWithVehicle_itCalculatesMarginalCostChanges(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(first));
- tour.addActivity(ServiceActivity.newInstance(second));
-
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).addService(second).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, third, vehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -224,11 +212,7 @@ public class TestCalculatesServiceInsertion {
@Test
public void whenInsertingThirdJobWithNewVehicle_itCalculatesMarginalCostChanges(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(first));
- tour.addActivity(ServiceActivity.newInstance(second));
-
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).addService(second).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, third, newVehicle, newVehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -238,11 +222,7 @@ public class TestCalculatesServiceInsertion {
@Test
public void whenInsertingASecondJobWithAVehicle_itCalculatesLocalMarginalCostChanges(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(first));
- tour.addActivity(ServiceActivity.newInstance(third));
-
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).addService(third).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, second, vehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -252,13 +232,7 @@ public class TestCalculatesServiceInsertion {
@Test
public void whenInsertingASecondJobWithANewVehicle_itCalculatesLocalMarginalCostChanges(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(first));
- tour.addActivity(ServiceActivity.newInstance(third));
-
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
-// route.addActivity(states.getActivity(first,true));
-// route.addActivity(states.getActivity(third,true));
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).addService(third).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, second, newVehicle, newVehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertionOnRouteLevel.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertionOnRouteLevel.java
index b8e45474..98f82120 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertionOnRouteLevel.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertionOnRouteLevel.java
@@ -80,6 +80,8 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
vehicle = mock(Vehicle.class);
when(vehicle.getCapacity()).thenReturn(1000);
when(vehicle.getLocationId()).thenReturn("0,0");
+ when(vehicle.getStartLocationId()).thenReturn("0,0");
+ when(vehicle.getEndLocationId()).thenReturn("0,0");
when(vehicle.getEarliestDeparture()).thenReturn(0.0);
when(vehicle.getLatestArrival()).thenReturn(100.0);
when(vehicle.isReturnToDepot()).thenReturn(true);
@@ -87,6 +89,8 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
newVehicle = mock(Vehicle.class);
when(newVehicle.getCapacity()).thenReturn(1000);
when(newVehicle.getLocationId()).thenReturn("0,0");
+ when(newVehicle.getStartLocationId()).thenReturn("0,0");
+ when(newVehicle.getEndLocationId()).thenReturn("0,0");
when(newVehicle.getEarliestDeparture()).thenReturn(0.0);
when(newVehicle.getLatestArrival()).thenReturn(100.0);
when(newVehicle.isReturnToDepot()).thenReturn(true);
@@ -175,9 +179,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
@Test
public void whenInsertingTheFirstJobInAnEmptyTourWithVehicle_itCalculatesMarginalCostChanges(){
- TourActivities tour = new TourActivities();
-
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, first, vehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -191,7 +193,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
tour.addActivity(ServiceActivity.newInstance(first));
tour.addActivity(ServiceActivity.newInstance(second));
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).addService(second).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, third, vehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -205,7 +207,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
tour.addActivity(ServiceActivity.newInstance(first));
tour.addActivity(ServiceActivity.newInstance(second));
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).addService(second).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, third, newVehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -219,7 +221,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
tour.addActivity(ServiceActivity.newInstance(first));
tour.addActivity(ServiceActivity.newInstance(third));
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).addService(third).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, second, vehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -233,7 +235,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
tour.addActivity(ServiceActivity.newInstance(first));
tour.addActivity(ServiceActivity.newInstance(third));
- VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).addService(first).addService(third).build();
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, second, newVehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestInserter.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestInserter.java
index 00bbd067..672d3236 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestInserter.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestInserter.java
@@ -23,6 +23,8 @@ public class TestInserter {
Service service = mock(Service.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getLocationId()).thenReturn("vehLoc");
+ when(vehicle.getStartLocationId()).thenReturn("vehLoc");
+ when(vehicle.getEndLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(true);
when(vehicle.getId()).thenReturn("vehId");
@@ -48,6 +50,8 @@ public class TestInserter {
Service service = mock(Service.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getLocationId()).thenReturn("vehLoc");
+ when(vehicle.getStartLocationId()).thenReturn("vehLoc");
+ when(vehicle.getEndLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getId()).thenReturn("vehId");
@@ -73,6 +77,8 @@ public class TestInserter {
Shipment shipment = mock(Shipment.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getLocationId()).thenReturn("vehLoc");
+ when(vehicle.getStartLocationId()).thenReturn("vehLoc");
+ when(vehicle.getEndLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(true);
when(vehicle.getId()).thenReturn("vehId");
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/state/TestTourStateUpdaterWithService.java b/jsprit-core/src/test/java/jsprit/core/algorithm/state/TestTourStateUpdaterWithService.java
index 83ec3285..f659b405 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/state/TestTourStateUpdaterWithService.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/state/TestTourStateUpdaterWithService.java
@@ -59,6 +59,10 @@ public class TestTourStateUpdaterWithService {
private VehicleRoute vehicleRoute;
+ private ServiceActivity act1;
+
+ private ServiceActivity act2;
+
@Before
public void setUp() {
@@ -115,11 +119,12 @@ public class TestTourStateUpdaterWithService {
states.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), states));
states.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts()));
- tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(firstService));
- tour.addActivity(ServiceActivity.newInstance(secondService));
-
- vehicleRoute = VehicleRoute.newInstance(tour,DriverImpl.noDriver(),vehicle);
+ act1 = ServiceActivity.newInstance(firstService);
+ act2 = ServiceActivity.newInstance(secondService);
+
+ vehicleRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();//.newInstance(tour,DriverImpl.noDriver(),vehicle);
+ vehicleRoute.getTourActivities().addActivity(act1);
+ vehicleRoute.getTourActivities().addActivity(act2);
}
@Test
@@ -135,27 +140,24 @@ public class TestTourStateUpdaterWithService {
assertEquals(0.0, vehicleRoute.getStart().getEndTime(),0.05);
assertEquals(vehicleRoute.getVehicle().getLocationId(), vehicleRoute.getStart().getLocationId());
assertEquals(vehicleRoute.getVehicle().getEarliestDeparture(), vehicleRoute.getStart().getTheoreticalEarliestOperationStartTime(),0.05);
- assertEquals(vehicleRoute.getVehicle().getLatestArrival(), vehicleRoute.getStart().getTheoreticalLatestOperationStartTime(),0.05);
+ assertEquals(Double.MAX_VALUE, vehicleRoute.getStart().getTheoreticalLatestOperationStartTime(),0.05);
}
@Test
public void testStatesOfAct1(){
states.informInsertionStarts(Arrays.asList(vehicleRoute), null);
- assertEquals(10.0, states.getActivityState(tour.getActivities().get(0), StateFactory.COSTS).toDouble(),0.05);
- assertEquals(5.0, states.getActivityState(tour.getActivities().get(0), StateFactory.LOAD).toDouble(),0.05);
-// assertEquals(10.0, states.getActivityState(tour.getActivities().get(0), StateTypes.EARLIEST_OPERATION_START_TIME).toDouble(),0.05);
- assertEquals(20.0, states.getActivityState(tour.getActivities().get(0), StateFactory.LATEST_OPERATION_START_TIME).toDouble(),0.05);
+ assertEquals(10.0, states.getActivityState(act1, StateFactory.COSTS).toDouble(),0.05);
+ assertEquals(5.0, states.getActivityState(act1, StateFactory.LOAD).toDouble(),0.05);
+ assertEquals(20.0, states.getActivityState(act1, StateFactory.LATEST_OPERATION_START_TIME).toDouble(),0.05);
}
@Test
public void testStatesOfAct2(){
states.informInsertionStarts(Arrays.asList(vehicleRoute), null);
-
- assertEquals(30.0, states.getActivityState(tour.getActivities().get(1), StateFactory.COSTS).toDouble(),0.05);
- assertEquals(10.0, states.getActivityState(tour.getActivities().get(1), StateFactory.LOAD).toDouble(),0.05);
-// assertEquals(10.0, states.getActivityState(tour.getActivities().get(0), StateTypes.EARLIEST_OPERATION_START_TIME).toDouble(),0.05);
- assertEquals(40.0, states.getActivityState(tour.getActivities().get(1), StateFactory.LATEST_OPERATION_START_TIME).toDouble(),0.05);
+ assertEquals(30.0, states.getActivityState(act2, StateFactory.COSTS).toDouble(),0.05);
+ assertEquals(10.0, states.getActivityState(act2, StateFactory.LOAD).toDouble(),0.05);
+ assertEquals(40.0, states.getActivityState(act2, StateFactory.LATEST_OPERATION_START_TIME).toDouble(),0.05);
}
@Test
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
index 135ee7ec..6c78d14a 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
@@ -24,10 +24,7 @@ import java.util.Iterator;
import jsprit.core.problem.driver.DriverImpl;
import jsprit.core.problem.driver.DriverImpl.NoDriver;
import jsprit.core.problem.job.Service;
-import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.ServiceActivity;
-import jsprit.core.problem.solution.route.activity.Start;
-import jsprit.core.problem.solution.route.activity.TourActivities;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
@@ -47,12 +44,10 @@ public class TestVehicleRoute {
vehicle = VehicleImpl.Builder.newInstance("v").setLocationId("loc").setType(VehicleTypeImpl.Builder.newInstance("yo", 0).build()).build();
driver = DriverImpl.noDriver();
}
-
-
-
+
@Test
public void whenBuildingEmptyRouteCorrectly_go(){
- VehicleRoute route = VehicleRoute.newInstance(TourActivities.emptyTour(),DriverImpl.noDriver(),VehicleImpl.createNoVehicle());
+ VehicleRoute route = VehicleRoute.Builder.newInstance(VehicleImpl.createNoVehicle(),DriverImpl.noDriver()).build();
assertTrue(route!=null);
}
@@ -74,56 +69,17 @@ public class TestVehicleRoute {
assertEquals(0,count);
}
- @Test(expected=IllegalStateException.class)
- public void whenBuildingEmptyRoute_(){
+ @Test(expected=IllegalArgumentException.class)
+ public void whenBuildingRouteWithNulls_itThrowsException(){
@SuppressWarnings("unused")
- VehicleRoute route = VehicleRoute.newInstance(null,null,null);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(null, null).build();
}
- @Test(expected=IllegalStateException.class)
- public void whenBuildingRouteWithNonEmptyTour_throwException(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("jo", 10).build()));
- @SuppressWarnings("unused")
- VehicleRoute route = VehicleRoute.newInstance(tour,DriverImpl.noDriver(),VehicleImpl.createNoVehicle());
- }
-
- @Test
- public void whenBuildingEmptyTour_tourIterIteratesOverAnEmptyList(){
- TourActivities tour = new TourActivities();
- Vehicle v = VehicleImpl.Builder.newInstance("v").setLocationId("loc").setType(VehicleTypeImpl.Builder.newInstance("yo", 0).build()).build();
- VehicleRoute route = VehicleRoute.newInstance(tour,DriverImpl.noDriver(),v);
- Iterator iter = route.getTourActivities().iterator();
- int count = 0;
- while(iter.hasNext()){
- @SuppressWarnings("unused")
- TourActivity act = iter.next();
- count++;
- }
- assertEquals(0,count);
- }
-
- @Test
- public void whenBuildingANonEmptyTour_tourIterIteratesOverActivitiesCorrectly(){
- TourActivities tour = new TourActivities();
- tour.addActivity(Start.newInstance("", 0, 0));
- VehicleRoute route = VehicleRoute.newInstance(tour, driver, vehicle);
- Iterator iter = route.getTourActivities().iterator();
- int count = 0;
- while(iter.hasNext()){
- @SuppressWarnings("unused")
- TourActivity act = iter.next();
- count++;
- }
- assertEquals(1,count);
- }
-
-
@Test
public void whenBuildingANonEmptyTour2Times_tourIterIteratesOverActivitiesCorrectly(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("2", 30).setLocationId("1").build()));
- VehicleRoute route = VehicleRoute.newInstance(tour, driver, vehicle);
+ VehicleRoute.Builder routeBuilder = VehicleRoute.Builder.newInstance(vehicle, driver);
+ routeBuilder.addService(Service.Builder.newInstance("2", 30).setLocationId("1").build());
+ VehicleRoute route = routeBuilder.build();
{
Iterator iter = route.getTourActivities().iterator();
@@ -136,7 +92,7 @@ public class TestVehicleRoute {
assertEquals(1,count);
}
{
- tour.addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("3", 30).setLocationId("1").build()));
+ route.getTourActivities().addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("3", 30).setLocationId("1").build()));
Iterator iter = route.getTourActivities().iterator();
int count = 0;
while(iter.hasNext()){
@@ -150,8 +106,7 @@ public class TestVehicleRoute {
@Test
public void whenBuildingANonEmptyTour_tourReverseIterIteratesOverActivitiesCorrectly(){
- TourActivities tour = new TourActivities();
- VehicleRoute route = VehicleRoute.newInstance(tour, driver, vehicle);
+ VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).build();
Iterator iter = route.getTourActivities().reverseActivityIterator();
int count = 0;
while(iter.hasNext()){
@@ -164,9 +119,9 @@ public class TestVehicleRoute {
@Test
public void whenBuildingANonEmptyTourV2_tourReverseIterIteratesOverActivitiesCorrectly(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("2", 30).setLocationId("1").build()));
- VehicleRoute route = VehicleRoute.newInstance(tour, driver, vehicle);
+ VehicleRoute.Builder routeBuilder = VehicleRoute.Builder.newInstance(vehicle, driver);
+ routeBuilder.addService(Service.Builder.newInstance("2", 30).setLocationId("1").build());
+ VehicleRoute route = routeBuilder.build();
Iterator iter = route.getTourActivities().reverseActivityIterator();
int count = 0;
while(iter.hasNext()){
@@ -177,78 +132,147 @@ public class TestVehicleRoute {
assertEquals(1,count);
}
- @Test
- public void whenBuildingANonEmptyTourV3_tourReverseIterIteratesOverActivitiesCorrectly(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("2", 30).setLocationId("1").build()));
- ServiceActivity del = ServiceActivity.newInstance(Service.Builder.newInstance("3", 30).setLocationId("1").build());
- tour.addActivity(del);
- VehicleRoute route = VehicleRoute.newInstance(tour, driver, vehicle);
- Iterator iter = route.getTourActivities().reverseActivityIterator();
- int count = 0;
- TourActivity memAct = null;
- while(iter.hasNext()){
- TourActivity act = iter.next();
- if(count==0) memAct = act;
- count++;
- }
- assertEquals(memAct,del);
- }
-
- @Test
- public void whenBuildingANonEmptyTourV4_tourReverseIterIteratesOverActivitiesCorrectly(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("2", 30).setLocationId("1").build()));
- ServiceActivity del = ServiceActivity.newInstance(Service.Builder.newInstance("3", 30).setLocationId("1").build());
- tour.addActivity(del);
- VehicleRoute route = VehicleRoute.newInstance(tour, driver, vehicle);
- Iterator iter = route.getTourActivities().reverseActivityIterator();
- int count = 0;
- TourActivity memAct = null;
- while(iter.hasNext()){
- TourActivity act = iter.next();
- if(count==0) memAct = act;
- count++;
- }
- assertEquals(memAct,del);
- assertEquals(2,count);
- }
-
@Test
public void whenBuildingANonEmptyTour2Times_tourReverseIterIteratesOverActivitiesCorrectly(){
- TourActivities tour = new TourActivities();
- tour.addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("2", 30).setLocationId("1").build()));
- ServiceActivity del = ServiceActivity.newInstance(Service.Builder.newInstance("3", 30).setLocationId("1").build());
- tour.addActivity(del);
- VehicleRoute route = VehicleRoute.newInstance(tour, driver, vehicle);
+ VehicleRoute.Builder routeBuilder = VehicleRoute.Builder.newInstance(vehicle, driver);
+ routeBuilder.addService(Service.Builder.newInstance("2", 30).setLocationId("1").build());
+ routeBuilder.addService(Service.Builder.newInstance("3", 30).setLocationId("2").build());
+ VehicleRoute route = routeBuilder.build();
{
Iterator iter = route.getTourActivities().reverseActivityIterator();
int count = 0;
- TourActivity memAct = null;
while(iter.hasNext()){
TourActivity act = iter.next();
- if(count==0) memAct = act;
+ if(count==0) {
+ assertEquals("2",act.getLocationId());
+ }
count++;
}
- assertEquals(memAct,del);
assertEquals(2,count);
}
{
Iterator secondIter = route.getTourActivities().reverseActivityIterator();
int count = 0;
- TourActivity memAct = null;
while(secondIter.hasNext()){
TourActivity act = secondIter.next();
- if(count==0) memAct = act;
+ if(count==0) {
+ assertEquals("2",act.getLocationId());
+ }
count++;
}
- assertEquals(memAct,del);
assertEquals(2,count);
}
}
- public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustBeBuiltCorrectly(){
-
+ public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustHaveCorrectStartLocation(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ assertTrue(vRoute.getStart().getLocationId().equals("start"));
+ }
+
+ public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustHaveCorrectEndLocation(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ assertTrue(vRoute.getEnd().getLocationId().equals("end"));
+ }
+
+ public void whenBuildingRouteWithVehicleThatHasSameStartAndEndLocation_routeMustHaveCorrectStartLocation(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("start").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ assertTrue(vRoute.getStart().getLocationId().equals("start"));
+ }
+
+ public void whenBuildingRouteWithVehicleThatHasSameStartAndEndLocation_routeMustHaveCorrectEndLocation(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("start").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ assertTrue(vRoute.getEnd().getLocationId().equals("start"));
+ }
+
+ public void whenBuildingRouteWithVehicleThatHasSameStartAndEndLocation_routeMustHaveCorrectStartLocationV2(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setLocationId("start").setEndLocationId("start").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ assertTrue(vRoute.getStart().getLocationId().equals("start"));
+ }
+
+ public void whenBuildingRouteWithVehicleThatHasSameStartAndEndLocation_routeMustHaveCorrectEndLocationV2(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setLocationId("start").setEndLocationId("start").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ assertTrue(vRoute.getEnd().getLocationId().equals("start"));
+ }
+
+ public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustHaveCorrectDepartureTime(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setStartLocationId("start").setEndLocationId("end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ assertEquals(vRoute.getDepartureTime(),100.0,0.01);
+ assertEquals(vRoute.getStart().getEndTime(),100.0,0.01);
+ }
+
+ public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustHaveCorrectEndTime(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ assertEquals(vRoute.getEnd().getArrTime(),100.0,0.01);
+ assertEquals(vRoute.getEnd().getTheoreticalLatestOperationStartTime(),100.0,0.01);
+ }
+
+ public void whenSettingDepartureTimeInBetweenEarliestStartAndLatestArr_routeMustHaveCorrectDepartureTime(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ vRoute.setVehicleAndDepartureTime(vehicle, 150.0);
+ assertEquals(vRoute.getStart().getEndTime(),150.0,0.01);
+ assertEquals(vRoute.getDepartureTime(),150.0,0.01);
+ }
+
+ public void whenSettingDepartureEarlierThanEarliestStart_routeMustHaveEarliestDepTimeAsDepTime(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ vRoute.setVehicleAndDepartureTime(vehicle, 50.0);
+ assertEquals(vRoute.getStart().getEndTime(),100.0,0.01);
+ assertEquals(vRoute.getDepartureTime(),100.0,0.01);
+ }
+
+ public void whenSettingDepartureTimeLaterThanLatestArrival_routeMustHaveThisDepTime(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ vRoute.setVehicleAndDepartureTime(vehicle, 50.0);
+ assertEquals(vRoute.getStart().getEndTime(),100.0,0.01);
+ assertEquals(vRoute.getDepartureTime(),100.0,0.01);
+ }
+
+ public void whenCreatingEmptyRoute_itMustReturnEmptyRoute(){
+ @SuppressWarnings("unused")
+ VehicleRoute route = VehicleRoute.emptyRoute();
+ assertTrue(true);
+ }
+
+ public void whenIniRouteWithNewVehicle_startLocationMustBeCorrect(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
+ Vehicle new_vehicle = VehicleImpl.Builder.newInstance("new_v").setEarliestStart(1000).setLatestArrival(2000).setStartLocationId("new_start").setEndLocationId("new_end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ vRoute.setVehicleAndDepartureTime(new_vehicle, 50.0);
+ assertEquals("new_start",vRoute.getStart().getLocationId());
+ }
+
+ public void whenIniRouteWithNewVehicle_endLocationMustBeCorrect(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
+ Vehicle new_vehicle = VehicleImpl.Builder.newInstance("new_v").setEarliestStart(1000).setLatestArrival(2000).setStartLocationId("new_start").setEndLocationId("new_end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ vRoute.setVehicleAndDepartureTime(new_vehicle, 50.0);
+ assertEquals("new_end",vRoute.getStart().getLocationId());
}
+ public void whenIniRouteWithNewVehicle_depTimeMustBeEarliestDepTimeOfNewVehicle(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
+ Vehicle new_vehicle = VehicleImpl.Builder.newInstance("new_v").setEarliestStart(1000).setLatestArrival(2000).setStartLocationId("new_start").setEndLocationId("new_end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ vRoute.setVehicleAndDepartureTime(new_vehicle, 50.0);
+ assertEquals(1000.0,vRoute.getDepartureTime(),0.01);
+ }
+
+ public void whenIniRouteWithNewVehicle_depTimeMustBeSetDepTime(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
+ Vehicle new_vehicle = VehicleImpl.Builder.newInstance("new_v").setEarliestStart(1000).setLatestArrival(2000).setStartLocationId("new_start").setEndLocationId("new_end").build();
+ VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
+ vRoute.setVehicleAndDepartureTime(new_vehicle, 1500.0);
+ assertEquals(1500.0,vRoute.getDepartureTime(),0.01);
+ }
}
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/VehicleRouteBuilderTest.java b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/VehicleRouteBuilderTest.java
index 3a584e17..900c5ec9 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/VehicleRouteBuilderTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/VehicleRouteBuilderTest.java
@@ -120,6 +120,7 @@ public class VehicleRouteBuilderTest {
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getLocationId()).thenReturn("vehLoc");
+ when(vehicle.getLatestArrival()).thenReturn(200.0);
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
From e5dabbdf6459f40563651b1c70954a8d4a742ce9 Mon Sep 17 00:00:00 2001
From: Stefan Schroeder <4sschroeder@gmail.com>
Date: Tue, 28 Jan 2014 14:45:11 +0100
Subject: [PATCH 09/22] mod
---
.../src/main/java/jsprit/core/problem/io/VrpXMLReader.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java
index 9bad6b39..40f5d2fb 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java
@@ -512,6 +512,7 @@ public class VrpXMLReader{
else{
Coordinate coordinate = Coordinate.newInstance(Double.parseDouble(coordX), Double.parseDouble(coordY));
builder.setStartLocationCoordinate(coordinate);
+
}
String endLocationId = vehicleConfig.getString("endLocation.id");
From a234bb54c928dd1d37a95c9bbafd7a80456d586a Mon Sep 17 00:00:00 2001
From: Stefan Schroeder <4sschroeder@gmail.com>
Date: Tue, 28 Jan 2014 17:40:13 +0100
Subject: [PATCH 10/22] shift from vehicle.getLocation() to
vehicle.getStartLocation() and vehicle.getCoord() to
vehicle.getStartLocationCoordinate()
---
.../analysis/toolbox/GraphStreamViewer.java | 24 +++++++++----
.../java/jsprit/analysis/toolbox/Plotter.java | 29 +++++++++++-----
.../analysis/toolbox/SolutionPlotter.java | 20 +++++++----
.../core/algorithm/io/InsertionFactory.java | 8 ++++-
.../io/VehicleRoutingAlgorithms.java | 3 --
.../recreate/ServiceInsertionCalculator.java | 4 +--
...erviceInsertionOnRouteLevelCalculator.java | 12 +++----
.../recreate/ShipmentInsertionCalculator.java | 4 +--
.../core/problem/VehicleRoutingProblem.java | 13 ++++---
.../problem/solution/route/VehicleRoute.java | 2 +-
.../jsprit/core/problem/vehicle/Vehicle.java | 4 +++
.../core/problem/vehicle/VehicleImpl.java | 14 ++++----
.../src/main/resources/algorithm_schema.xsd | 3 +-
...icleTypeDependentServiceInsertionTest.java | 4 +--
.../TestCalculatesServiceInsertion.java | 6 ++--
...alculatesServiceInsertionOnRouteLevel.java | 2 --
.../core/algorithm/recreate/TestInserter.java | 10 ++----
.../TestTourStateUpdaterWithService.java | 2 +-
.../problem/VehicleRoutingProblemTest.java | 34 ++++++++++++++-----
.../core/problem/io/VrpReaderV2Test.java | 14 ++++----
.../solution/route/TestVehicleRoute.java | 21 ++++++++++--
.../route/VehicleRouteBuilderTest.java | 11 +++---
.../core/problem/vehicle/VehicleImplTest.java | 19 +----------
jsprit-examples/input/algorithmConfig_fix.xml | 2 +-
.../jsprit/examples/BicycleMessenger.java | 2 +-
...nfigureAlgorithmInCodeInsteadOfPerXml.java | 4 +--
.../jsprit/examples/CostMatrixExample.java | 8 ++---
.../jsprit/examples/MultipleDepotExample.java | 7 ++--
...ltipleDepotExampleWithPenaltyVehicles.java | 7 ++--
.../examples/PickupAndDeliveryExample.java | 4 +--
.../examples/PickupAndDeliveryExample2.java | 4 +--
.../examples/SimpleExampleOpenRoutes.java | 6 ++--
.../java/jsprit/examples/SolomonExample.java | 7 ++--
.../jsprit/examples/SolomonOpenExample.java | 6 ++--
.../jsprit/examples/SolomonR101Example.java | 8 ++---
.../examples/VRPWithBackhaulsExample2.java | 4 +--
36 files changed, 195 insertions(+), 137 deletions(-)
diff --git a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java
index 16928de8..898213ab 100644
--- a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java
+++ b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java
@@ -385,17 +385,27 @@ public class GraphStreamViewer {
}
private void renderVehicle(Graph g, Vehicle vehicle, Label label) {
- Node n = g.addNode(makeId(vehicle.getId(),vehicle.getLocationId()));
- if(label.equals(Label.ID)) n.addAttribute("ui.label", "depot");
+ Node vehicleStart = g.addNode(makeId(vehicle.getId(),vehicle.getStartLocationId()));
+ if(label.equals(Label.ID)) vehicleStart.addAttribute("ui.label", "depot");
// if(label.equals(Label.ACTIVITY)) n.addAttribute("ui.label", "start");
- n.addAttribute("x", vehicle.getCoord().getX());
- n.addAttribute("y", vehicle.getCoord().getY());
- n.setAttribute("ui.class", "depot");
+ vehicleStart.addAttribute("x", vehicle.getStartLocationCoordinate().getX());
+ vehicleStart.addAttribute("y", vehicle.getStartLocationCoordinate().getY());
+ vehicleStart.setAttribute("ui.class", "depot");
+
+ if(!vehicle.getStartLocationId().equals(vehicle.getEndLocationId())){
+ Node vehicleEnd = g.addNode(makeId(vehicle.getId(),vehicle.getEndLocationId()));
+ if(label.equals(Label.ID)) vehicleEnd.addAttribute("ui.label", "depot");
+// if(label.equals(Label.ACTIVITY)) n.addAttribute("ui.label", "start");
+ vehicleEnd.addAttribute("x", vehicle.getEndLocationCoordinate().getX());
+ vehicleEnd.addAttribute("y", vehicle.getEndLocationCoordinate().getY());
+ vehicleEnd.setAttribute("ui.class", "depot");
+
+ }
}
private void renderRoute(Graph g, VehicleRoute route, int routeId, long renderDelay_in_ms, Label label) {
int vehicle_edgeId = 1;
- String prevIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getLocationId());
+ String prevIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getStartLocationId());
if(label.equals(Label.ACTIVITY)){
Node n = g.getNode(prevIdentifier);
n.addAttribute("ui.label", "start");
@@ -414,7 +424,7 @@ public class GraphStreamViewer {
sleep(renderDelay_in_ms);
}
if(route.getVehicle().isReturnToDepot()){
- String lastIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getLocationId());
+ String lastIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getEndLocationId());
g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, lastIdentifier, true);
}
}
diff --git a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/Plotter.java b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/Plotter.java
index 38e8fbcf..44535d05 100644
--- a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/Plotter.java
+++ b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/Plotter.java
@@ -404,9 +404,15 @@ public class Plotter {
XYSeriesCollection coll = new XYSeriesCollection();
XYSeries vehicleSeries = new XYSeries("depot", false, true);
for(Vehicle v : vehicles){
- Coordinate coord = v.getCoord();
- if(coord == null) throw new NoLocationFoundException();
- vehicleSeries.add(coord.getX(),coord.getY());
+ Coordinate startCoord = v.getStartLocationCoordinate();
+ if(startCoord == null) throw new NoLocationFoundException();
+ vehicleSeries.add(startCoord.getX(),startCoord.getY());
+
+ if(!v.getStartLocationId().equals(v.getEndLocationId())){
+ Coordinate endCoord = v.getEndLocationCoordinate();
+ if(endCoord == null) throw new NoLocationFoundException();
+ vehicleSeries.add(endCoord.getX(),endCoord.getY());
+ }
}
coll.addSeries(vehicleSeries);
@@ -473,11 +479,18 @@ public class Plotter {
private Locations retrieveLocations(VehicleRoutingProblem vrp) throws NoLocationFoundException {
final Map locs = new HashMap();
for(Vehicle v : vrp.getVehicles()){
- String locationId = v.getLocationId();
- if(locationId == null) throw new NoLocationFoundException();
- Coordinate coord = v.getCoord();
- if(coord == null) throw new NoLocationFoundException();
- locs.put(locationId, coord);
+ String startLocationId = v.getStartLocationId();
+ if(startLocationId == null) throw new NoLocationFoundException();
+ Coordinate startCoord = v.getStartLocationCoordinate();
+ if(startCoord == null) throw new NoLocationFoundException();
+ locs.put(startLocationId, startCoord);
+
+ String endLocationId = v.getEndLocationId();
+ if(!startLocationId.equals(endLocationId)){
+ Coordinate endCoord = v.getEndLocationCoordinate();
+ if(endCoord == null) throw new NoLocationFoundException();
+ locs.put(endLocationId, endCoord);
+ }
}
for(Job j : vrp.getJobs().values()){
if(j instanceof Service){
diff --git a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/SolutionPlotter.java b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/SolutionPlotter.java
index 141c8c22..c86a6266 100644
--- a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/SolutionPlotter.java
+++ b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/SolutionPlotter.java
@@ -75,8 +75,10 @@ public class SolutionPlotter {
* @param vrp
* @param pngFile target path with filename.
- * @see VehicleRoutingProblem, VehicleRoutingProblemSolution
+ * @see VehicleRoutingProblem, VehicleRoutingProblemSolution
+ * @deprecated use Plotter.java instead (this plotter is not maintained anymore and might plot incorrectly)
*/
+ @Deprecated
public static void plotVrpAsPNG(VehicleRoutingProblem vrp, String pngFile, String title){
String filename = pngFile;
if(!pngFile.endsWith(".png")) filename += ".png";
@@ -102,7 +104,9 @@ public class SolutionPlotter {
* @param pngFile target path with filename.
* @param plotTitle
* @see VehicleRoute
+ * @deprecated use Plotter.java instead (this plotter is not maintained anymore and might plot incorrectly)
*/
+ @Deprecated
public static void plotRoutesAsPNG(Collection routes, Locations locations, String pngFile, String title) {
String filename = pngFile;
if(!pngFile.endsWith(".png")) filename += ".png";
@@ -130,8 +134,10 @@ public class SolutionPlotter {
* @param vrp
* @param solution
* @param pngFile target path with filename.
- * @see VehicleRoutingProblem, VehicleRoutingProblemSolution
+ * @see VehicleRoutingProblem, VehicleRoutingProblemSolution
+ * @deprecated use Plotter.java instead (this plotter is not maintained anymore and might plot incorrectly)
*/
+ @Deprecated
public static void plotSolutionAsPNG(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, String pngFile, String title){
String filename = pngFile;
if(!pngFile.endsWith(".png")) filename += ".png";
@@ -294,7 +300,7 @@ public class SolutionPlotter {
XYSeriesCollection coll = new XYSeriesCollection();
XYSeries vehicleSeries = new XYSeries("depot", false, true);
for(Vehicle v : vehicles){
- Coordinate coord = v.getCoord();
+ Coordinate coord = v.getStartLocationCoordinate();
if(coord == null) throw new NoLocationFoundException();
vehicleSeries.add(coord.getX(),coord.getY());
}
@@ -353,11 +359,11 @@ public class SolutionPlotter {
private static Locations retrieveLocations(VehicleRoutingProblem vrp) throws NoLocationFoundException {
final Map locs = new HashMap();
for(Vehicle v : vrp.getVehicles()){
- String locationId = v.getLocationId();
- if(locationId == null) throw new NoLocationFoundException();
- Coordinate coord = v.getCoord();
+ String startLocationId = v.getStartLocationId();
+ if(startLocationId == null) throw new NoLocationFoundException();
+ Coordinate coord = v.getStartLocationCoordinate();
if(coord == null) throw new NoLocationFoundException();
- locs.put(locationId, coord);
+ locs.put(startLocationId, coord);
}
for(Job j : vrp.getJobs().values()){
if(j instanceof Service){
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/io/InsertionFactory.java b/jsprit-core/src/main/java/jsprit/core/algorithm/io/InsertionFactory.java
index 00a18fe3..9a4f49a5 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/io/InsertionFactory.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/io/InsertionFactory.java
@@ -86,9 +86,15 @@ class InsertionFactory {
String weight = config.getString("considerFixedCosts[@weight]");
if(weight == null) weight = config.getString("considerFixedCost[@weight]");
if(weight != null) fixedCostWeight = Double.parseDouble(weight);
- else log.warn("parameter considerFixedCosts[@weight] is missing. by default, it is 0.5.");
+ else throw new IllegalStateException("fixedCostsParameter 'weight' must be set, e.g. true.\n" +
+ "this has to be changed in algorithm-config-xml-file.");
iBuilder.considerFixedCosts(fixedCostWeight);
}
+ else if(val.equals("false")){
+
+ }
+ else throw new IllegalStateException("considerFixedCosts must either be true or false, i.e. true or \nfalse. " +
+ "if latter, you can also omit the tag. this has to be changed in algorithm-config-xml-file");
}
String timeSliceString = config.getString("experimental[@timeSlice]");
String neighbors = config.getString("experimental[@neighboringSlices]");
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java
index 0a636235..428f0276 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java
@@ -481,9 +481,6 @@ public class VehicleRoutingAlgorithms {
@Override
public void finish() {
- if(firstAct){
- assert vehicle.getLocationId() == end.getLocationId() : "route end and last activity are not equal even route is open. this should not be.";
- }
firstAct = true;
}
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
index 1a1ead9c..c07b6e44 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
@@ -106,9 +106,9 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
TourActivity deliveryAct2Insert = activityFactory.createActivity(service);
- Start start = Start.newInstance(newVehicle.getLocationId(), newVehicle.getEarliestDeparture(), newVehicle.getLatestArrival());
+ Start start = Start.newInstance(newVehicle.getStartLocationId(), newVehicle.getEarliestDeparture(), Double.MAX_VALUE);
start.setEndTime(newVehicleDepartureTime);
- End end = End.newInstance(newVehicle.getLocationId(), 0.0, newVehicle.getLatestArrival());
+ End end = End.newInstance(newVehicle.getEndLocationId(), 0.0, newVehicle.getLatestArrival());
TourActivity prevAct = start;
double prevActStartTime = newVehicleDepartureTime;
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionOnRouteLevelCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionOnRouteLevelCalculator.java
index f5dc6b79..272400cb 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionOnRouteLevelCalculator.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionOnRouteLevelCalculator.java
@@ -314,22 +314,22 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
*/
private void initialiseStartAndEnd(final Vehicle newVehicle, double newVehicleDepartureTime) {
if(start == null){
- start = Start.newInstance(newVehicle.getLocationId(), newVehicle.getEarliestDeparture(), newVehicle.getLatestArrival());
+ start = Start.newInstance(newVehicle.getStartLocationId(), newVehicle.getEarliestDeparture(), Double.MAX_VALUE);
start.setEndTime(newVehicleDepartureTime);
}
else{
- start.setLocationId(newVehicle.getLocationId());
+ start.setLocationId(newVehicle.getStartLocationId());
start.setTheoreticalEarliestOperationStartTime(newVehicle.getEarliestDeparture());
- start.setTheoreticalLatestOperationStartTime(newVehicle.getLatestArrival());
+ start.setTheoreticalLatestOperationStartTime(Double.MAX_VALUE);
start.setEndTime(newVehicleDepartureTime);
}
if(end == null){
- end = End.newInstance(newVehicle.getLocationId(), 0.0, newVehicle.getLatestArrival());
+ end = End.newInstance(newVehicle.getEndLocationId(), 0.0, newVehicle.getLatestArrival());
}
else{
- end.setLocationId(newVehicle.getLocationId());
- end.setTheoreticalEarliestOperationStartTime(newVehicleDepartureTime);
+ end.setLocationId(newVehicle.getEndLocationId());
+ end.setTheoreticalEarliestOperationStartTime(0.0);
end.setTheoreticalLatestOperationStartTime(newVehicle.getLatestArrival());
}
}
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculator.java
index 8a98d517..87377a8f 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculator.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculator.java
@@ -100,10 +100,10 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
int pickupInsertionIndex = InsertionData.NO_INDEX;
int deliveryInsertionIndex = InsertionData.NO_INDEX;
- Start start = Start.newInstance(newVehicle.getLocationId(), newVehicle.getEarliestDeparture(), newVehicle.getLatestArrival());
+ Start start = Start.newInstance(newVehicle.getStartLocationId(), newVehicle.getEarliestDeparture(), newVehicle.getLatestArrival());
start.setEndTime(newVehicleDepartureTime);
- End end = End.newInstance(newVehicle.getLocationId(), 0.0, newVehicle.getLatestArrival());
+ End end = End.newInstance(newVehicle.getEndLocationId(), 0.0, newVehicle.getLatestArrival());
TourActivity prevAct = start;
double prevActEndTime = newVehicleDepartureTime;
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java
index eb263d6f..86768bfe 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java
@@ -327,7 +327,11 @@ public class VehicleRoutingProblem {
if(!vehicleTypes.contains(vehicle.getType())){
vehicleTypes.add(vehicle.getType());
}
- coordinates.put(vehicle.getLocationId(), vehicle.getCoord());
+ String startLocationId = vehicle.getStartLocationId();
+ coordinates.put(startLocationId, vehicle.getStartLocationCoordinate());
+ if(!vehicle.getEndLocationId().equals(startLocationId)){
+ coordinates.put(vehicle.getEndLocationId(), vehicle.getEndLocationCoordinate());
+ }
return this;
}
@@ -373,7 +377,7 @@ public class VehicleRoutingProblem {
Set locTypeKeys = new HashSet();
List uniqueVehicles = new ArrayList();
for(Vehicle v : vehicles){
- LocTypeKey key = new LocTypeKey(v.getLocationId(),v.getType().getTypeId());
+ LocTypeKey key = new LocTypeKey(v.getStartLocationId(),v.getType().getTypeId());
if(!locTypeKeys.contains(key)){
uniqueVehicles.add(v);
locTypeKeys.add(key);
@@ -390,9 +394,10 @@ public class VehicleRoutingProblem {
.setFixedCost(fixed)
.build();
PenaltyVehicleType penType = new PenaltyVehicleType(t,penaltyFactor);
- String vehicleId = "penaltyVehicle_" + v.getLocationId() + "_" + t.getTypeId();
+ String vehicleId = "penaltyVehicle_" + v.getStartLocationId() + "_" + t.getTypeId();
Vehicle penVehicle = VehicleImpl.Builder.newInstance(vehicleId).setEarliestStart(v.getEarliestDeparture())
- .setLatestArrival(v.getLatestArrival()).setLocationCoord(v.getCoord()).setLocationId(v.getLocationId())
+ .setLatestArrival(v.getLatestArrival()).setStartLocationCoordinate(v.getStartLocationCoordinate()).setLocationId(v.getStartLocationId())
+ .setEndLocationId(v.getEndLocationId()).setEndLocationCoordinate(v.getEndLocationCoordinate())
.setReturnToDepot(v.isReturnToDepot()).setType(penType).build();
addVehicle(penVehicle);
}
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java b/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java
index 86e66106..2a099724 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java
@@ -165,7 +165,7 @@ public class VehicleRoute {
this.driver = driver;
start = Start.newInstance(vehicle.getStartLocationId(), vehicle.getEarliestDeparture(), Double.MAX_VALUE);
start.setEndTime(vehicle.getEarliestDeparture());
- end = End.newInstance(vehicle.getLocationId(), 0.0, vehicle.getLatestArrival());
+ end = End.newInstance(vehicle.getEndLocationId(), 0.0, vehicle.getLatestArrival());
}
/**
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
index 6176910c..c73cfb24 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java
@@ -47,7 +47,9 @@ public interface Vehicle {
*
Consequently, it should be the end-location of this vehicle, if returnToDepot is true.
*
* @return location-id of this vehicle
+ * @deprecated use getStartLocationId() instead
*/
+ @Deprecated
public abstract String getLocationId();
/**
@@ -56,7 +58,9 @@ public interface Vehicle {
*
Consequently, it should be the coordinate of the end-location, if returnToDepot is true.
*
* @return coordinate of this vehicle
+ * @deprecated use getStartLocationCoordinate() instead
*/
+ @Deprecated
public abstract Coordinate getCoord();
/**
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
index 6a534e7e..5a50813f 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
@@ -176,10 +176,8 @@ public class VehicleImpl implements Vehicle {
*
* @param coord
* @return this builder
- * @throws IllegalArgumentException if start-coordinate is null
*/
public Builder setStartLocationCoordinate(Coordinate coord){
- if(coord == null) throw new IllegalArgumentException("start-coordinate must not be null");
this.startLocationCoord = coord;
this.locationCoord = coord;
return this;
@@ -190,10 +188,8 @@ public class VehicleImpl implements Vehicle {
*
* @param endLocationId
* @return this builder
- * @throws IllegalArgumentException if endLocation is null
*/
public Builder setEndLocationId(String endLocationId){
- if(endLocationId == null) throw new IllegalArgumentException("end-locationId must not be null");
this.endLocationId = endLocationId;
return this;
}
@@ -203,10 +199,8 @@ public class VehicleImpl implements Vehicle {
*
* @param coord
* @return this builder
- * @throws IllegalArgumentException if coord is null
*/
public Builder setEndLocationCoordinate(Coordinate coord){
- if(coord == null) throw new IllegalArgumentException("end-coordinate must not be null");
this.endLocationCoord = coord;
return this;
}
@@ -336,6 +330,10 @@ public class VehicleImpl implements Vehicle {
return "[id="+id+"][type="+type+"][locationId="+locationId+"][coord=" + coord + "][isReturnToDepot=" + isReturnToDepot() + "]";
}
+ /**
+ * @deprecated use getStartLocationCoordinate() instead
+ */
+ @Deprecated
public Coordinate getCoord() {
return coord;
}
@@ -350,6 +348,10 @@ public class VehicleImpl implements Vehicle {
return latestArrival;
}
+ /**
+ * @deprecated use getStartLocationId() instead
+ */
+ @Deprecated
@Override
public String getLocationId() {
return locationId;
diff --git a/jsprit-core/src/main/resources/algorithm_schema.xsd b/jsprit-core/src/main/resources/algorithm_schema.xsd
index 6ea5d966..b2a80039 100644
--- a/jsprit-core/src/main/resources/algorithm_schema.xsd
+++ b/jsprit-core/src/main/resources/algorithm_schema.xsd
@@ -226,7 +226,8 @@
-
+
+
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/CalcVehicleTypeDependentServiceInsertionTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/CalcVehicleTypeDependentServiceInsertionTest.java
index b7d775ab..a1495182 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/CalcVehicleTypeDependentServiceInsertionTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/CalcVehicleTypeDependentServiceInsertionTest.java
@@ -53,8 +53,8 @@ public class CalcVehicleTypeDependentServiceInsertionTest {
veh2 = mock(Vehicle.class);
when(veh1.getType()).thenReturn(VehicleTypeImpl.Builder.newInstance("type1", 0).build());
when(veh2.getType()).thenReturn(VehicleTypeImpl.Builder.newInstance("type2", 0).build());
- when(veh1.getLocationId()).thenReturn("loc1");
- when(veh2.getLocationId()).thenReturn("loc2");
+ when(veh1.getStartLocationId()).thenReturn("loc1");
+ when(veh2.getStartLocationId()).thenReturn("loc2");
fleetManager = mock(VehicleFleetManager.class);
service = mock(Service.class);
vehicleRoute = mock(VehicleRoute.class);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
index 9e820e3f..ae238881 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
@@ -75,14 +75,16 @@ public class TestCalculatesServiceInsertion {
costs = mock(VehicleRoutingTransportCosts.class);
vehicle = mock(Vehicle.class);
when(vehicle.getCapacity()).thenReturn(1000);
- when(vehicle.getLocationId()).thenReturn("depot");
+ when(vehicle.getStartLocationId()).thenReturn("depot");
+ when(vehicle.getEndLocationId()).thenReturn("depot");
when(vehicle.getEarliestDeparture()).thenReturn(0.0);
when(vehicle.getLatestArrival()).thenReturn(100.0);
when(vehicle.isReturnToDepot()).thenReturn(true);
newVehicle = mock(Vehicle.class);
when(newVehicle.getCapacity()).thenReturn(1000);
- when(newVehicle.getLocationId()).thenReturn("depot");
+ when(newVehicle.getStartLocationId()).thenReturn("depot");
+ when(newVehicle.getEndLocationId()).thenReturn("depot");
when(newVehicle.getEarliestDeparture()).thenReturn(0.0);
when(newVehicle.getLatestArrival()).thenReturn(100.0);
when(newVehicle.isReturnToDepot()).thenReturn(true);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertionOnRouteLevel.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertionOnRouteLevel.java
index 98f82120..02d2119a 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertionOnRouteLevel.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertionOnRouteLevel.java
@@ -79,7 +79,6 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
costs = mock(VehicleRoutingTransportCosts.class);
vehicle = mock(Vehicle.class);
when(vehicle.getCapacity()).thenReturn(1000);
- when(vehicle.getLocationId()).thenReturn("0,0");
when(vehicle.getStartLocationId()).thenReturn("0,0");
when(vehicle.getEndLocationId()).thenReturn("0,0");
when(vehicle.getEarliestDeparture()).thenReturn(0.0);
@@ -88,7 +87,6 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
newVehicle = mock(Vehicle.class);
when(newVehicle.getCapacity()).thenReturn(1000);
- when(newVehicle.getLocationId()).thenReturn("0,0");
when(newVehicle.getStartLocationId()).thenReturn("0,0");
when(newVehicle.getEndLocationId()).thenReturn("0,0");
when(newVehicle.getEarliestDeparture()).thenReturn(0.0);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestInserter.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestInserter.java
index 672d3236..4a1d3228 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestInserter.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestInserter.java
@@ -22,7 +22,6 @@ public class TestInserter {
public void whenInsertingServiceAndRouteIsClosed_itInsertsCorrectly(){
Service service = mock(Service.class);
Vehicle vehicle = mock(Vehicle.class);
- when(vehicle.getLocationId()).thenReturn("vehLoc");
when(vehicle.getStartLocationId()).thenReturn("vehLoc");
when(vehicle.getEndLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(true);
@@ -42,14 +41,13 @@ public class TestInserter {
assertEquals(2,route.getTourActivities().getActivities().size());
assertEquals(route.getTourActivities().getActivities().get(1).getLocationId(),serviceToInsert.getLocationId());
- assertEquals(route.getEnd().getLocationId(),vehicle.getLocationId());
+ assertEquals(route.getEnd().getLocationId(),vehicle.getEndLocationId());
}
@Test
public void whenInsertingServiceAndRouteIsOpen_itInsertsCorrectlyAndSwitchesEndLocation(){
Service service = mock(Service.class);
Vehicle vehicle = mock(Vehicle.class);
- when(vehicle.getLocationId()).thenReturn("vehLoc");
when(vehicle.getStartLocationId()).thenReturn("vehLoc");
when(vehicle.getEndLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(false);
@@ -76,7 +74,6 @@ public class TestInserter {
public void whenInsertingShipmentAndRouteIsClosed_itInsertsCorrectly(){
Shipment shipment = mock(Shipment.class);
Vehicle vehicle = mock(Vehicle.class);
- when(vehicle.getLocationId()).thenReturn("vehLoc");
when(vehicle.getStartLocationId()).thenReturn("vehLoc");
when(vehicle.getEndLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(true);
@@ -98,14 +95,13 @@ public class TestInserter {
assertEquals(4,route.getTourActivities().getActivities().size());
assertEquals(route.getTourActivities().getActivities().get(2).getLocationId(),shipmentToInsert.getPickupLocation());
assertEquals(route.getTourActivities().getActivities().get(3).getLocationId(),shipmentToInsert.getDeliveryLocation());
- assertEquals(route.getEnd().getLocationId(),vehicle.getLocationId());
+ assertEquals(route.getEnd().getLocationId(),vehicle.getEndLocationId());
}
@Test
public void whenInsertingShipmentAndRouteIsOpen_itInsertsCorrectlyAndSwitchesEndLocation(){
Shipment shipment = mock(Shipment.class);
Vehicle vehicle = mock(Vehicle.class);
- when(vehicle.getLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getId()).thenReturn("vehId");
@@ -148,7 +144,7 @@ public class TestInserter {
Inserter inserter = new Inserter(mock(InsertionListeners.class));
inserter.insertJob(shipmentToInsert, iData, route);
- assertEquals(newVehicle.getLocationId(),route.getEnd().getLocationId());
+ assertEquals(route.getEnd().getLocationId(),newVehicle.getEndLocationId());
}
@Test
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/state/TestTourStateUpdaterWithService.java b/jsprit-core/src/test/java/jsprit/core/algorithm/state/TestTourStateUpdaterWithService.java
index f659b405..3498829a 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/state/TestTourStateUpdaterWithService.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/state/TestTourStateUpdaterWithService.java
@@ -138,7 +138,7 @@ public class TestTourStateUpdaterWithService {
public void testStatesOfAct0(){
states.informInsertionStarts(Arrays.asList(vehicleRoute), null);
assertEquals(0.0, vehicleRoute.getStart().getEndTime(),0.05);
- assertEquals(vehicleRoute.getVehicle().getLocationId(), vehicleRoute.getStart().getLocationId());
+ assertEquals(vehicleRoute.getVehicle().getStartLocationId(), vehicleRoute.getStart().getLocationId());
assertEquals(vehicleRoute.getVehicle().getEarliestDeparture(), vehicleRoute.getStart().getTheoreticalEarliestOperationStartTime(),0.05);
assertEquals(Double.MAX_VALUE, vehicleRoute.getStart().getTheoreticalLatestOperationStartTime(),0.05);
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java b/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java
index fb2ce5b2..62728eb7 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java
@@ -66,10 +66,10 @@ public class VehicleRoutingProblemTest {
public void whenBuildingWithFourVehicles_vrpShouldContainTheCorrectNuOfVehicles(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
- Vehicle v1 = mock(VehicleImpl.class);
- Vehicle v2 = mock(VehicleImpl.class);
- Vehicle v3 = mock(VehicleImpl.class);
- Vehicle v4 = mock(VehicleImpl.class);
+ Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").build();
+ Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").build();
+ Vehicle v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("start").build();
+ Vehicle v4 = VehicleImpl.Builder.newInstance("v4").setStartLocationId("start").build();
builder.addVehicle(v1).addVehicle(v2).addVehicle(v3).addVehicle(v4);
@@ -82,10 +82,10 @@ public class VehicleRoutingProblemTest {
public void whenAddingFourVehiclesAllAtOnce_vrpShouldContainTheCorrectNuOfVehicles(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
- Vehicle v1 = mock(VehicleImpl.class);
- Vehicle v2 = mock(VehicleImpl.class);
- Vehicle v3 = mock(VehicleImpl.class);
- Vehicle v4 = mock(VehicleImpl.class);
+ Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").build();
+ Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").build();
+ Vehicle v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("start").build();
+ Vehicle v4 = VehicleImpl.Builder.newInstance("v4").setStartLocationId("start").build();
builder.addAllVehicles(Arrays.asList(v1,v2,v3,v4));
@@ -471,4 +471,22 @@ public class VehicleRoutingProblemTest {
assertTrue(anotherPenVehInCollection);
}
+
+ @Test
+ public void whenAddingVehicleWithDiffStartAndEnd_startLocationMustBeRegisteredInLocationMap(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build();
+
+ VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
+ vrpBuilder.addVehicle(vehicle);
+ assertTrue(vrpBuilder.getLocationMap().containsKey("start"));
+ }
+
+ @Test
+ public void whenAddingVehicleWithDiffStartAndEnd_endLocationMustBeRegisteredInLocationMap(){
+ Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build();
+
+ VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
+ vrpBuilder.addVehicle(vehicle);
+ assertTrue(vrpBuilder.getLocationMap().containsKey("end"));
+ }
}
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/io/VrpReaderV2Test.java b/jsprit-core/src/test/java/jsprit/core/problem/io/VrpReaderV2Test.java
index aec2da95..19f29541 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/io/VrpReaderV2Test.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/io/VrpReaderV2Test.java
@@ -70,9 +70,9 @@ public class VrpReaderV2Test {
VehicleRoutingProblem vrp = builder.build();
Vehicle v1 = getVehicle("v1",vrp.getVehicles());
assertEquals(20,v1.getCapacity());
- assertEquals(100.0,v1.getCoord().getX(),0.01);
+ assertEquals(100.0,v1.getStartLocationCoordinate().getX(),0.01);
assertEquals(0.0,v1.getEarliestDeparture(),0.01);
- assertEquals("depotLoc2",v1.getLocationId());
+ assertEquals("depotLoc2",v1.getStartLocationId());
assertNotNull(v1.getType());
assertEquals("vehType", v1.getType().getTypeId());
assertEquals(1000.0,v1.getLatestArrival(),0.01);
@@ -229,8 +229,8 @@ public class VrpReaderV2Test {
new VrpXMLReader(builder, null).read(inFileName);
VehicleRoutingProblem vrp = builder.build();
Vehicle v3 = getVehicle("v3",vrp.getVehicles());
- assertEquals(10.0,v3.getCoord().getX(),0.01);
- assertEquals(100.0,v3.getCoord().getY(),0.01);
+ assertEquals(10.0,v3.getStartLocationCoordinate().getX(),0.01);
+ assertEquals(100.0,v3.getStartLocationCoordinate().getY(),0.01);
}
@Test
@@ -239,7 +239,7 @@ public class VrpReaderV2Test {
new VrpXMLReader(builder, null).read(inFileName);
VehicleRoutingProblem vrp = builder.build();
Vehicle v3 = getVehicle("v3",vrp.getVehicles());
- assertEquals("startLoc",v3.getLocationId());
+ assertEquals("startLoc",v3.getStartLocationId());
}
@Test
@@ -286,8 +286,8 @@ public class VrpReaderV2Test {
new VrpXMLReader(builder, null).read(inFileName);
VehicleRoutingProblem vrp = builder.build();
Vehicle v = getVehicle("v4",vrp.getVehicles());
- assertEquals(10.0,v.getCoord().getX(),0.01);
- assertEquals(100.0,v.getCoord().getY(),0.01);
+ assertEquals(10.0,v.getStartLocationCoordinate().getX(),0.01);
+ assertEquals(100.0,v.getStartLocationCoordinate().getY(),0.01);
}
@Test
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
index 6c78d14a..15047e17 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/TestVehicleRoute.java
@@ -164,42 +164,49 @@ public class TestVehicleRoute {
}
}
+ @Test
public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustHaveCorrectStartLocation(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
assertTrue(vRoute.getStart().getLocationId().equals("start"));
}
+ @Test
public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustHaveCorrectEndLocation(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
assertTrue(vRoute.getEnd().getLocationId().equals("end"));
}
+ @Test
public void whenBuildingRouteWithVehicleThatHasSameStartAndEndLocation_routeMustHaveCorrectStartLocation(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("start").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
assertTrue(vRoute.getStart().getLocationId().equals("start"));
}
+ @Test
public void whenBuildingRouteWithVehicleThatHasSameStartAndEndLocation_routeMustHaveCorrectEndLocation(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("start").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
assertTrue(vRoute.getEnd().getLocationId().equals("start"));
}
+ @Test
public void whenBuildingRouteWithVehicleThatHasSameStartAndEndLocation_routeMustHaveCorrectStartLocationV2(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setLocationId("start").setEndLocationId("start").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
assertTrue(vRoute.getStart().getLocationId().equals("start"));
}
+ @Test
public void whenBuildingRouteWithVehicleThatHasSameStartAndEndLocation_routeMustHaveCorrectEndLocationV2(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setLocationId("start").setEndLocationId("start").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
assertTrue(vRoute.getEnd().getLocationId().equals("start"));
}
+ @Test
public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustHaveCorrectDepartureTime(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setStartLocationId("start").setEndLocationId("end").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
@@ -207,13 +214,14 @@ public class TestVehicleRoute {
assertEquals(vRoute.getStart().getEndTime(),100.0,0.01);
}
+ @Test
public void whenBuildingRouteWithVehicleThatHasDifferentStartAndEndLocation_routeMustHaveCorrectEndTime(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
- assertEquals(vRoute.getEnd().getArrTime(),100.0,0.01);
- assertEquals(vRoute.getEnd().getTheoreticalLatestOperationStartTime(),100.0,0.01);
+ assertEquals(200.0,vRoute.getEnd().getTheoreticalLatestOperationStartTime(),0.01);
}
+ @Test
public void whenSettingDepartureTimeInBetweenEarliestStartAndLatestArr_routeMustHaveCorrectDepartureTime(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
@@ -222,6 +230,7 @@ public class TestVehicleRoute {
assertEquals(vRoute.getDepartureTime(),150.0,0.01);
}
+ @Test
public void whenSettingDepartureEarlierThanEarliestStart_routeMustHaveEarliestDepTimeAsDepTime(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
@@ -230,6 +239,7 @@ public class TestVehicleRoute {
assertEquals(vRoute.getDepartureTime(),100.0,0.01);
}
+ @Test
public void whenSettingDepartureTimeLaterThanLatestArrival_routeMustHaveThisDepTime(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
@@ -238,12 +248,14 @@ public class TestVehicleRoute {
assertEquals(vRoute.getDepartureTime(),100.0,0.01);
}
+ @Test
public void whenCreatingEmptyRoute_itMustReturnEmptyRoute(){
@SuppressWarnings("unused")
VehicleRoute route = VehicleRoute.emptyRoute();
assertTrue(true);
}
+ @Test
public void whenIniRouteWithNewVehicle_startLocationMustBeCorrect(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
Vehicle new_vehicle = VehicleImpl.Builder.newInstance("new_v").setEarliestStart(1000).setLatestArrival(2000).setStartLocationId("new_start").setEndLocationId("new_end").build();
@@ -252,14 +264,16 @@ public class TestVehicleRoute {
assertEquals("new_start",vRoute.getStart().getLocationId());
}
+ @Test
public void whenIniRouteWithNewVehicle_endLocationMustBeCorrect(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
Vehicle new_vehicle = VehicleImpl.Builder.newInstance("new_v").setEarliestStart(1000).setLatestArrival(2000).setStartLocationId("new_start").setEndLocationId("new_end").build();
VehicleRoute vRoute = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build();
vRoute.setVehicleAndDepartureTime(new_vehicle, 50.0);
- assertEquals("new_end",vRoute.getStart().getLocationId());
+ assertEquals("new_end",vRoute.getEnd().getLocationId());
}
+ @Test
public void whenIniRouteWithNewVehicle_depTimeMustBeEarliestDepTimeOfNewVehicle(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
Vehicle new_vehicle = VehicleImpl.Builder.newInstance("new_v").setEarliestStart(1000).setLatestArrival(2000).setStartLocationId("new_start").setEndLocationId("new_end").build();
@@ -268,6 +282,7 @@ public class TestVehicleRoute {
assertEquals(1000.0,vRoute.getDepartureTime(),0.01);
}
+ @Test
public void whenIniRouteWithNewVehicle_depTimeMustBeSetDepTime(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setEarliestStart(100).setLatestArrival(200).setStartLocationId("start").setEndLocationId("end").build();
Vehicle new_vehicle = VehicleImpl.Builder.newInstance("new_v").setEarliestStart(1000).setLatestArrival(2000).setStartLocationId("new_start").setEndLocationId("new_end").build();
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/VehicleRouteBuilderTest.java b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/VehicleRouteBuilderTest.java
index 900c5ec9..389527a7 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/solution/route/VehicleRouteBuilderTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/solution/route/VehicleRouteBuilderTest.java
@@ -65,14 +65,15 @@ public class VehicleRouteBuilderTest {
Shipment s2 = mock(Shipment.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(true);
- when(vehicle.getLocationId()).thenReturn("vehLoc");
+ when(vehicle.getStartLocationId()).thenReturn("vehLoc");
+ when(vehicle.getEndLocationId()).thenReturn("vehLoc");
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
builder.addDelivery(s);
builder.addDelivery(s2);
VehicleRoute route = builder.build();
- assertEquals(route.getEnd().getLocationId(), vehicle.getLocationId());
+ assertEquals("vehLoc",route.getEnd().getLocationId());
}
@Test
@@ -82,7 +83,7 @@ public class VehicleRouteBuilderTest {
when(s2.getDeliveryLocation()).thenReturn("delLoc");
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(false);
- when(vehicle.getLocationId()).thenReturn("vehLoc");
+ when(vehicle.getStartLocationId()).thenReturn("vehLoc");
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
@@ -99,7 +100,7 @@ public class VehicleRouteBuilderTest {
when(s2.getDeliveryLocation()).thenReturn("delLoc");
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(false);
- when(vehicle.getLocationId()).thenReturn("vehLoc");
+ when(vehicle.getStartLocationId()).thenReturn("vehLoc");
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
@@ -119,7 +120,7 @@ public class VehicleRouteBuilderTest {
when(s2.getDeliveryLocation()).thenReturn("delLoc");
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(false);
- when(vehicle.getLocationId()).thenReturn("vehLoc");
+ when(vehicle.getStartLocationId()).thenReturn("vehLoc");
when(vehicle.getLatestArrival()).thenReturn(200.0);
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java
index 6578892a..adb5344a 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/vehicle/VehicleImplTest.java
@@ -121,12 +121,6 @@ public class VehicleImplTest {
assertEquals(2.0, v.getCoord().getY(),0.01);
}
- @Test(expected=IllegalArgumentException.class)
- public void whenStartLocationCoordIsNull_itThrowsException(){
- @SuppressWarnings("unused")
- Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(null).build();
- }
-
@Test
public void whenEndLocationIsSet_itIsDoneCorrectly(){
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("startLoc").setEndLocationId("endLoc").build();
@@ -134,12 +128,6 @@ public class VehicleImplTest {
assertEquals("endLoc", v.getEndLocationId());
}
- @Test(expected=IllegalArgumentException.class)
- public void whenEndLocationIsNull_itThrowsException(){
- @SuppressWarnings("unused")
- Vehicle v = VehicleImpl.Builder.newInstance("v").setEndLocationId(null).build();
- }
-
@Test
public void whenEndLocationCoordIsSet_itIsDoneCorrectly(){
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("startLoc").setEndLocationCoordinate(Coordinate.newInstance(1, 2)).build();
@@ -147,12 +135,7 @@ public class VehicleImplTest {
assertEquals(2.0, v.getEndLocationCoordinate().getY(),0.01);
}
- @Test(expected=IllegalArgumentException.class)
- public void whenEndLocationCoordIsNull_itThrowsException(){
- @SuppressWarnings("unused")
- Vehicle v = VehicleImpl.Builder.newInstance("v").setEndLocationCoordinate(null).build();
- }
-
+
@Test
public void whenNeitherEndLocationIdNorEndLocationCoordAreSet_endLocationIdMustBeEqualToStartLocationId(){
Vehicle v = VehicleImpl.Builder.newInstance("v").setStartLocationId("startLoc").build();
diff --git a/jsprit-examples/input/algorithmConfig_fix.xml b/jsprit-examples/input/algorithmConfig_fix.xml
index b7ffdd9c..7d01a4f3 100755
--- a/jsprit-examples/input/algorithmConfig_fix.xml
+++ b/jsprit-examples/input/algorithmConfig_fix.xml
@@ -7,7 +7,7 @@
- 1.0
+ true
diff --git a/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java b/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
index 14b67a24..e3a13739 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
@@ -318,7 +318,7 @@ public class BicycleMessenger {
static double getTimeOfDirectRoute(Job job, Vehicle v, VehicleRoutingTransportCosts routingCosts) {
Shipment envelope = (Shipment) job;
- double direct = routingCosts.getTransportTime(v.getLocationId(), envelope.getPickupLocation(), 0.0, DriverImpl.noDriver(), v) +
+ double direct = routingCosts.getTransportTime(v.getStartLocationId(), envelope.getPickupLocation(), 0.0, DriverImpl.noDriver(), v) +
routingCosts.getTransportTime(envelope.getPickupLocation(), envelope.getDeliveryLocation(), 0.0, DriverImpl.noDriver(), v);
return direct;
}
diff --git a/jsprit-examples/src/main/java/jsprit/examples/ConfigureAlgorithmInCodeInsteadOfPerXml.java b/jsprit-examples/src/main/java/jsprit/examples/ConfigureAlgorithmInCodeInsteadOfPerXml.java
index 339e1ecb..c81a58a7 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/ConfigureAlgorithmInCodeInsteadOfPerXml.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/ConfigureAlgorithmInCodeInsteadOfPerXml.java
@@ -19,7 +19,7 @@ package jsprit.examples;
import java.io.File;
import java.util.Collection;
-import jsprit.analysis.toolbox.SolutionPlotter;
+import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.AlgorithmConfig;
@@ -106,7 +106,7 @@ public class ConfigureAlgorithmInCodeInsteadOfPerXml {
/*
* plot
*/
- SolutionPlotter.plotSolutionAsPNG(problem, bestSolution, "output/solution.png", "solution");
+ new Plotter(problem,bestSolution).plot("output/solution.png", "solution");
}
private static AlgorithmConfig getAlgorithmConfig() {
diff --git a/jsprit-examples/src/main/java/jsprit/examples/CostMatrixExample.java b/jsprit-examples/src/main/java/jsprit/examples/CostMatrixExample.java
index 0357b9ef..e03bb64a 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/CostMatrixExample.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/CostMatrixExample.java
@@ -19,7 +19,7 @@ package jsprit.examples;
import java.io.File;
import java.util.Collection;
-import jsprit.analysis.toolbox.SolutionPlotter;
+import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
@@ -110,9 +110,9 @@ public class CostMatrixExample {
Collection solutions = vra.searchSolutions();
SolutionPrinter.print(Solutions.bestOf(solutions));
-
- SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.bestOf(solutions), "output/yo.png", "po");
-
+
+ new Plotter(vrp, Solutions.bestOf(solutions)).plot("output/yo.png", "po");
+
}
}
diff --git a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java
index 14071e2b..c26c8027 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java
@@ -22,7 +22,7 @@ import java.util.Collection;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer;
-import jsprit.analysis.toolbox.SolutionPlotter;
+import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.StopWatch;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
@@ -112,8 +112,9 @@ public class MultipleDepotExample {
Collection solutions = vra.searchSolutions();
SolutionPrinter.print(Solutions.bestOf(solutions));
- SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.bestOf(solutions), "output/p01_solution.png", "p01");
-
+
+ new Plotter(vrp, Solutions.bestOf(solutions)).plot("output/p01_solution.png", "p01");
+
new GraphStreamViewer(vrp, Solutions.bestOf(solutions)).setRenderDelay(100).display();
}
diff --git a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExampleWithPenaltyVehicles.java b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExampleWithPenaltyVehicles.java
index 0224cb4d..9881ba15 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExampleWithPenaltyVehicles.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExampleWithPenaltyVehicles.java
@@ -22,10 +22,10 @@ import java.util.Collection;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer;
-import jsprit.analysis.toolbox.SolutionPlotter;
+import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter;
-import jsprit.analysis.toolbox.StopWatch;
import jsprit.analysis.toolbox.SolutionPrinter.Print;
+import jsprit.analysis.toolbox.StopWatch;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners.Priority;
@@ -140,8 +140,9 @@ public class MultipleDepotExampleWithPenaltyVehicles {
Collection solutions = vra.searchSolutions();
SolutionPrinter.print(vrp,Solutions.bestOf(solutions),Print.VERBOSE);
- SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.bestOf(solutions), "output/p08_solution.png", "p08");
+ new Plotter(vrp, Solutions.bestOf(solutions)).plot("output/p08_solution.png", "p08");
+
new GraphStreamViewer(vrp,Solutions.bestOf(solutions)).setRenderDelay(50).display();
}
diff --git a/jsprit-examples/src/main/java/jsprit/examples/PickupAndDeliveryExample.java b/jsprit-examples/src/main/java/jsprit/examples/PickupAndDeliveryExample.java
index 04aafdaa..69097990 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/PickupAndDeliveryExample.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/PickupAndDeliveryExample.java
@@ -22,7 +22,6 @@ import java.util.Collection;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.Plotter.Label;
-import jsprit.analysis.toolbox.SolutionPlotter;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
@@ -65,7 +64,8 @@ public class PickupAndDeliveryExample {
VehicleRoutingProblem vrp = vrpBuilder.build();
- SolutionPlotter.plotVrpAsPNG(vrp, "output/pd_solomon_r101.png", "pd_r101");
+ new Plotter(vrp).plot("output/pd_solomon_r101.png", "pd_r101");
+
/*
* Define the required vehicle-routing algorithms to solve the above problem.
diff --git a/jsprit-examples/src/main/java/jsprit/examples/PickupAndDeliveryExample2.java b/jsprit-examples/src/main/java/jsprit/examples/PickupAndDeliveryExample2.java
index c235a8ae..cef1fe44 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/PickupAndDeliveryExample2.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/PickupAndDeliveryExample2.java
@@ -23,7 +23,6 @@ import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.Plotter.Label;
-import jsprit.analysis.toolbox.SolutionPlotter;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
@@ -71,7 +70,8 @@ public class PickupAndDeliveryExample2 {
VehicleRoutingProblem vrp = vrpBuilder.build();
- SolutionPlotter.plotVrpAsPNG(vrp, "output/pd_christophides_vrpnc1.png", "pd_vrpnc1");
+ new Plotter(vrp).plot("output/pd_christophides_vrpnc1.png", "pd_vrpnc1");
+
/*
* Define the required vehicle-routing algorithms to solve the above problem.
diff --git a/jsprit-examples/src/main/java/jsprit/examples/SimpleExampleOpenRoutes.java b/jsprit-examples/src/main/java/jsprit/examples/SimpleExampleOpenRoutes.java
index d7b8ba24..97c34da2 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/SimpleExampleOpenRoutes.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/SimpleExampleOpenRoutes.java
@@ -19,7 +19,7 @@ package jsprit.examples;
import java.io.File;
import java.util.Collection;
-import jsprit.analysis.toolbox.SolutionPlotter;
+import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
@@ -105,7 +105,9 @@ public class SimpleExampleOpenRoutes {
/*
* plot
*/
- SolutionPlotter.plotSolutionAsPNG(problem, bestSolution, "output/solution.png", "solution");
+
+ new Plotter(problem, bestSolution).plot("output/solution.png", "solution");
+
}
}
diff --git a/jsprit-examples/src/main/java/jsprit/examples/SolomonExample.java b/jsprit-examples/src/main/java/jsprit/examples/SolomonExample.java
index 25c2cacd..34cee239 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/SolomonExample.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/SolomonExample.java
@@ -20,10 +20,9 @@ import java.io.File;
import java.util.Collection;
import jsprit.analysis.toolbox.GraphStreamViewer;
-import jsprit.analysis.toolbox.Plotter;
-import jsprit.analysis.toolbox.SolutionPlotter;
-import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.GraphStreamViewer.Label;
+import jsprit.analysis.toolbox.Plotter;
+import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.SolutionPrinter.Print;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
@@ -64,7 +63,7 @@ public class SolomonExample {
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
- SolutionPlotter.plotVrpAsPNG(vrp, "output/solomon_C101.png", "C101");
+ new Plotter(vrp).plot("output/solomon_C101.png", "C101");
/*
* Define the required vehicle-routing algorithms to solve the above problem.
diff --git a/jsprit-examples/src/main/java/jsprit/examples/SolomonOpenExample.java b/jsprit-examples/src/main/java/jsprit/examples/SolomonOpenExample.java
index 8f35317c..1604e74b 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/SolomonOpenExample.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/SolomonOpenExample.java
@@ -20,9 +20,9 @@ import java.io.File;
import java.util.Collection;
import jsprit.analysis.toolbox.GraphStreamViewer;
-import jsprit.analysis.toolbox.SolutionPlotter;
-import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.GraphStreamViewer.Label;
+import jsprit.analysis.toolbox.Plotter;
+import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.selector.SelectBest;
@@ -62,7 +62,7 @@ public class SolomonOpenExample {
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
- SolutionPlotter.plotVrpAsPNG(vrp, "output/solomon_C101_open.png", "C101");
+ new Plotter(vrp).plot("output/solomon_C101_open.png", "C101");
/*
* Define the required vehicle-routing algorithms to solve the above problem.
diff --git a/jsprit-examples/src/main/java/jsprit/examples/SolomonR101Example.java b/jsprit-examples/src/main/java/jsprit/examples/SolomonR101Example.java
index f89d2171..f1251299 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/SolomonR101Example.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/SolomonR101Example.java
@@ -19,7 +19,7 @@ package jsprit.examples;
import java.io.File;
import java.util.Collection;
-import jsprit.analysis.toolbox.SolutionPlotter;
+import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
@@ -60,7 +60,7 @@ public class SolomonR101Example {
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
- SolutionPlotter.plotVrpAsPNG(vrp, "output/solomon_R101.png", "R101");
+ new Plotter(vrp).plot("output/solomon_R101.png", "R101");
/*
* Define the required vehicle-routing algorithms to solve the above problem.
@@ -91,9 +91,7 @@ public class SolomonR101Example {
/*
* Plot solution.
*/
- SolutionPlotter.plotSolutionAsPNG(vrp, solution, "output/solomon_R101_solution.png","R101");
-
-
+ new Plotter(vrp,solution).plot( "output/solomon_R101_solution.png","R101");
}
diff --git a/jsprit-examples/src/main/java/jsprit/examples/VRPWithBackhaulsExample2.java b/jsprit-examples/src/main/java/jsprit/examples/VRPWithBackhaulsExample2.java
index 7deb9c71..b2b8bdb3 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/VRPWithBackhaulsExample2.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/VRPWithBackhaulsExample2.java
@@ -21,7 +21,6 @@ import java.util.Collection;
import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.Plotter.Label;
-import jsprit.analysis.toolbox.SolutionPlotter;
import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
@@ -69,7 +68,8 @@ public class VRPWithBackhaulsExample2 {
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
- SolutionPlotter.plotVrpAsPNG(vrp, "output/vrpwbh_christophides_vrpnc1.png", "pd_vrpnc1");
+ new Plotter(vrp).plot("output/vrpwbh_christophides_vrpnc1.png", "pd_vrpnc1");
+
/*
* Define the required vehicle-routing algorithms to solve the above problem.
From 974021cf1b4d2688f5c6c23b615e5efcd5c8c24c Mon Sep 17 00:00:00 2001
From: Stefan Schroeder <4sschroeder@gmail.com>
Date: Wed, 29 Jan 2014 19:09:44 +0100
Subject: [PATCH 11/22] shift to start/endLocationId and
start/endLocationCoordinate
---
...leTypeDependentJobInsertionCalculator.java | 2 +-
.../core/problem/VehicleRoutingProblem.java | 2 +-
.../problem/vehicle/InfiniteVehicles.java | 20 +++++-
.../problem/vehicle/VehicleFleetManager.java | 10 +++
.../vehicle/VehicleFleetManagerImpl.java | 28 +++++++--
.../core/problem/vehicle/VehicleImpl.java | 4 ++
.../core/problem/vehicle/VehicleTypeKey.java | 40 +++++++++---
.../jsprit/core/util/NeighborhoodImpl.java | 4 +-
.../java/jsprit/core/util/VrpVerifier.java | 52 +++++++++-------
.../core/algorithm/RefuseCollection_IT.java | 2 +-
.../recreate/CalcWithTimeSchedulingTest.java | 2 +-
...erviceInsertionAndLoadConstraintsTest.java | 4 +-
.../ShipmentInsertionCalculatorTest.java | 2 +-
.../recreate/TestDepartureTimeOpt.java | 12 ++--
.../core/algorithm/recreate/TestInserter.java | 12 ++--
...erviceAndShipmentsProblemOnRouteLevel.java | 4 +-
.../TestTourStateUpdaterWithService.java | 2 +-
.../problem/VehicleRoutingProblemTest.java | 38 ++++++------
.../core/problem/io/VrpWriterV2Test.java | 62 +++++++++----------
.../solution/route/TestVehicleRoute.java | 6 +-
.../vehicle/TestVehicleFleetManagerImpl.java | 14 ++---
.../core/problem/vehicle/VehicleImplTest.java | 2 +-
.../jsprit/examples/BicycleMessenger.java | 2 +-
...nfigureAlgorithmInCodeInsteadOfPerXml.java | 2 +-
.../jsprit/examples/CostMatrixExample.java | 2 +-
.../java/jsprit/examples/HVRPExample.java | 8 +--
.../jsprit/examples/MultipleDepotExample.java | 2 +-
...ltipleDepotExampleWithPenaltyVehicles.java | 4 +-
.../examples/RefuseCollectionExample.java | 2 +-
...eDepotBoundedPickupAndDeliveryExample.java | 2 +-
...SimpleEnRoutePickupAndDeliveryExample.java | 2 +-
...utePickupAndDeliveryOpenRoutesExample.java | 2 +-
...veryWithDepotBoundedDeliveriesExample.java | 2 +-
.../java/jsprit/examples/SimpleExample.java | 2 +-
.../examples/SimpleExampleOpenRoutes.java | 2 +-
.../SimpleVRPWithBackhaulsExample.java | 2 +-
36 files changed, 220 insertions(+), 140 deletions(-)
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/VehicleTypeDependentJobInsertionCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/VehicleTypeDependentJobInsertionCalculator.java
index 11be2212..76c64d94 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/VehicleTypeDependentJobInsertionCalculator.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/VehicleTypeDependentJobInsertionCalculator.java
@@ -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
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java
index 86768bfe..94b5c947 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java
@@ -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);
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/InfiniteVehicles.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/InfiniteVehicles.java
index 6cba846f..f46d6ce3 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/InfiniteVehicles.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/InfiniteVehicles.java
@@ -45,7 +45,7 @@ class InfiniteVehicles implements VehicleFleetManager{
private void extractTypes(Collection 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 getAvailableVehicles(String withoutThisType, String locationId) {
Collection vehicles = new ArrayList();
- 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 getAvailableVehicles(Vehicle withoutThisType) {
+ Collection vehicles = new ArrayList();
+ 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));
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleFleetManager.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleFleetManager.java
index 6d545e42..6eeee1ae 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleFleetManager.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleFleetManager.java
@@ -31,6 +31,16 @@ public interface VehicleFleetManager {
public abstract Collection getAvailableVehicles();
+ /**
+ *
+ * @param withoutThisType
+ * @param locationId
+ * @return
+ * @deprecated use .getAvailableVehicles(Vehicle without) instead. this might ignore withoutType and returns all available vehicles
+ */
+ @Deprecated
public Collection getAvailableVehicles(String withoutThisType, String locationId);
+
+ public Collection getAvailableVehicles(Vehicle withoutThisType);
}
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleFleetManagerImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleFleetManagerImpl.java
index 31ee75c4..8e1d303e 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleFleetManagerImpl.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleFleetManagerImpl.java
@@ -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 getAvailableVehicles(String withoutThisType, String withThisLocationId) {
List vehicles = new ArrayList();
- 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 getAvailableVehicles(Vehicle withoutThisType) {
+ List vehicles = new ArrayList();
+ 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)
*/
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
index 5a50813f..92e69358 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java
@@ -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;
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeKey.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeKey.java
index a39907df..7a9e82dd 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeKey.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeKey.java
@@ -1,26 +1,45 @@
package jsprit.core.problem.vehicle;
+/**
+ * Key to identify different vehicles
+ *
+ *
Two vehicles are equal if they share the same type and location.
+ *
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;
}
-
+
}
\ No newline at end of file
diff --git a/jsprit-core/src/main/java/jsprit/core/util/NeighborhoodImpl.java b/jsprit-core/src/main/java/jsprit/core/util/NeighborhoodImpl.java
index 393289dc..61fdbaf9 100644
--- a/jsprit-core/src/main/java/jsprit/core/util/NeighborhoodImpl.java
+++ b/jsprit-core/src/main/java/jsprit/core/util/NeighborhoodImpl.java
@@ -62,7 +62,7 @@ public class NeighborhoodImpl implements Neighborhood{
for(Service i : services){
Set neigh = new HashSet();
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 vehicles) {
for(Vehicle v : vehicles){
- neighborsToAll.add(v.getLocationId());
+ neighborsToAll.add(v.getStartLocationId());
}
}
diff --git a/jsprit-core/src/main/java/jsprit/core/util/VrpVerifier.java b/jsprit-core/src/main/java/jsprit/core/util/VrpVerifier.java
index 783e428b..3e6b9309 100644
--- a/jsprit-core/src/main/java/jsprit/core/util/VrpVerifier.java
+++ b/jsprit-core/src/main/java/jsprit/core/util/VrpVerifier.java
@@ -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.
+ *
+ *
It is calculated at local level, i.e. the additional costs of inserting act_new between act_i and act_j is c(act_i,act_new,newVehicle)+c(act_new,act_j,newVehicle)-c(act_i,act_j,oldVehicle)
+ *
If newVehicle.isReturnToDepot == false then the additional costs of inserting act_new between act_i and end is c(act_i,act_new) [since act_new is then the new end-of-route]
+ *
+ * @param routingCosts
+ */
+ public AdditionalTransportationCosts(VehicleRoutingTransportCosts routingCosts) {
+ super();
+ this.routingCosts = routingCosts;
+ }
+
+ /**
+ * Returns additional transportation costs induced by inserting newAct.
+ *
+ *
It is calculated at local level, i.e. the additional costs of inserting act_new between act_i and act_j is c(act_i,act_new,newVehicle)+c(act_new,act_j,newVehicle)-c(act_i,act_j,oldVehicle)
+ *
If newVehicle.isReturnToDepot == false then the additional costs of inserting act_new between act_i and end is c(act_i,act_new) [since act_new is then the new end-of-route]
+ */
+ @Override
+ public double getCosts(JobInsertionContext iFacts, TourActivity prevAct,TourActivity newAct, TourActivity nextAct, double depTimeAtPrevAct) {
+ double tp_costs_prevAct_newAct = routingCosts.getTransportCost(prevAct.getLocationId(), newAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
+ double tp_time_prevAct_newAct = routingCosts.getTransportTime(prevAct.getLocationId(), newAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
+
+ double newAct_arrTime = depTimeAtPrevAct + tp_time_prevAct_newAct;
+ double newAct_endTime = CalculationUtils.getActivityEndTime(newAct_arrTime, newAct);
+
+ //open routes
+ if(nextAct instanceof End){
+ if(!iFacts.getNewVehicle().isReturnToDepot()){
+ return tp_costs_prevAct_newAct;
+ }
+ }
+
+ double tp_costs_newAct_nextAct = routingCosts.getTransportCost(newAct.getLocationId(), nextAct.getLocationId(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
+ double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct;
+
+ double oldCosts;
+ if(iFacts.getRoute().isEmpty()){
+ double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocationId(), nextAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
+ oldCosts = tp_costs_prevAct_nextAct;
+ }
+ else{
+ double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocationId(), nextAct.getLocationId(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
+ oldCosts = tp_costs_prevAct_nextAct;
+ }
+
+ double additionalCosts = totalCosts - oldCosts;
+ return additionalCosts;
+ }
+
+}
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/constraint/ConstraintManager.java b/jsprit-core/src/main/java/jsprit/core/problem/constraint/ConstraintManager.java
index 6c2dc332..fd6545c4 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/constraint/ConstraintManager.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/constraint/ConstraintManager.java
@@ -14,7 +14,7 @@ import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import org.apache.log4j.Logger;
@SuppressWarnings("deprecation")
-public class ConstraintManager implements HardActivityStateLevelConstraint, HardRouteStateLevelConstraint{
+public class ConstraintManager implements HardActivityStateLevelConstraint, HardRouteStateLevelConstraint, SoftActivityConstraint, SoftRouteConstraint{
public static enum Priority {
CRITICAL, HIGH, LOW
@@ -26,6 +26,10 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
private HardRouteLevelConstraintManager routeLevelConstraintManager = new HardRouteLevelConstraintManager();
+ private SoftActivityConstraintManager softActivityConstraintManager = new SoftActivityConstraintManager();
+
+ private SoftRouteConstraintManager softRouteConstraintManager = new SoftRouteConstraintManager();
+
private VehicleRoutingProblem vrp;
private RouteAndActivityStateGetter stateManager;
@@ -56,6 +60,14 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
routeLevelConstraintManager.addConstraint((HardRouteStateLevelConstraint) c);
constraintTypeKnown = true;
}
+ if(c instanceof SoftRouteConstraint){
+ softRouteConstraintManager.addConstraint((SoftRouteConstraint)c);
+ constraintTypeKnown = true;
+ }
+ if(c instanceof SoftActivityConstraint){
+ softActivityConstraintManager.addConstraint((SoftActivityConstraint)c);
+ constraintTypeKnown = true;
+ }
if(!constraintTypeKnown){
log.warn("constraint " + c + " unknown thus ignores the constraint. currently, a constraint must implement either HardActivityStateLevelConstraint or HardRouteStateLevelConstraint");
}
@@ -82,6 +94,8 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
}
}
+// public void add
+
public void addConstraint(HardActivityStateLevelConstraint actLevelConstraint, Priority priority){
actLevelConstraintManager.addConstraint(actLevelConstraint,priority);
}
@@ -90,6 +104,14 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
routeLevelConstraintManager.addConstraint(routeLevelConstraint);
}
+ public void addConstraint(SoftActivityConstraint softActivityConstraint){
+ softActivityConstraintManager.addConstraint(softActivityConstraint);
+ }
+
+ public void addConstraint(SoftRouteConstraint softRouteConstraint){
+ softRouteConstraintManager.addConstraint(softRouteConstraint);
+ }
+
@Override
public boolean fulfilled(JobInsertionContext insertionContext) {
return routeLevelConstraintManager.fulfilled(insertionContext);
@@ -104,7 +126,19 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
List constraints = new ArrayList();
constraints.addAll(actLevelConstraintManager.getAllConstraints());
constraints.addAll(routeLevelConstraintManager.getConstraints());
+ constraints.addAll(softActivityConstraintManager.getConstraints());
+ constraints.addAll(softRouteConstraintManager.getConstraints());
return Collections.unmodifiableCollection(constraints);
}
+
+ @Override
+ public double getCosts(JobInsertionContext insertionContext) {
+ return softRouteConstraintManager.getCosts(insertionContext);
+ }
+
+ @Override
+ public double getCosts(JobInsertionContext iFacts, TourActivity prevAct,TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
+ return softActivityConstraintManager.getCosts(iFacts, prevAct, newAct, nextAct, prevActDepTime);
+ }
}
\ No newline at end of file
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftActivityConstraint.java b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftActivityConstraint.java
new file mode 100644
index 00000000..8817b2ad
--- /dev/null
+++ b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftActivityConstraint.java
@@ -0,0 +1,10 @@
+package jsprit.core.problem.constraint;
+
+import jsprit.core.problem.misc.JobInsertionContext;
+import jsprit.core.problem.solution.route.activity.TourActivity;
+
+public interface SoftActivityConstraint extends SoftConstraint{
+
+ public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime);
+
+}
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftActivityConstraintManager.java b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftActivityConstraintManager.java
new file mode 100644
index 00000000..bb8a1106
--- /dev/null
+++ b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftActivityConstraintManager.java
@@ -0,0 +1,29 @@
+package jsprit.core.problem.constraint;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import jsprit.core.problem.misc.JobInsertionContext;
+import jsprit.core.problem.solution.route.activity.TourActivity;
+
+class SoftActivityConstraintManager implements SoftActivityConstraint{
+
+ private Collection softConstraints = new ArrayList();
+
+ public void addConstraint(SoftActivityConstraint constraint){
+ softConstraints.add(constraint);
+ }
+
+ Collection getConstraints(){ return Collections.unmodifiableCollection(softConstraints); }
+
+ @Override
+ public double getCosts(JobInsertionContext iFacts, TourActivity prevAct,TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
+ double sumCosts = 0.0;
+ for(SoftActivityConstraint c : softConstraints){
+ sumCosts += c.getCosts(iFacts, prevAct, newAct, nextAct, prevActDepTime);
+ }
+ return sumCosts;
+ }
+
+}
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftRouteConstraint.java b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftRouteConstraint.java
new file mode 100644
index 00000000..1cc1d6e5
--- /dev/null
+++ b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftRouteConstraint.java
@@ -0,0 +1,9 @@
+package jsprit.core.problem.constraint;
+
+import jsprit.core.problem.misc.JobInsertionContext;
+
+public interface SoftRouteConstraint extends SoftConstraint{
+
+ public double getCosts(JobInsertionContext insertionContext);
+
+}
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftRouteConstraintManager.java b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftRouteConstraintManager.java
new file mode 100644
index 00000000..311ac857
--- /dev/null
+++ b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftRouteConstraintManager.java
@@ -0,0 +1,28 @@
+package jsprit.core.problem.constraint;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import jsprit.core.problem.misc.JobInsertionContext;
+
+class SoftRouteConstraintManager implements SoftRouteConstraint{
+
+ private Collection softConstraints = new ArrayList();
+
+ public void addConstraint(SoftRouteConstraint constraint){
+ softConstraints.add(constraint);
+ }
+
+ Collection getConstraints(){ return Collections.unmodifiableCollection(softConstraints); }
+
+ @Override
+ public double getCosts(JobInsertionContext insertionContext) {
+ double sumCosts = 0.0;
+ for(SoftRouteConstraint c : softConstraints){
+ sumCosts += c.getCosts(insertionContext);
+ }
+ return sumCosts;
+ }
+
+}
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java
index 9c3a63da..944999c0 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java
@@ -32,6 +32,7 @@ import jsprit.core.algorithm.selector.SelectBest;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateVariableCosts;
import jsprit.core.problem.VehicleRoutingProblem;
+import jsprit.core.problem.constraint.AdditionalTransportationCosts;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.SolutionCostCalculator;
@@ -67,6 +68,7 @@ public class BuildCVRPAlgoFromScratch_IT {
ConstraintManager cManager = new ConstraintManager(vrp, stateManager);
cManager.addLoadConstraint();
cManager.addTimeWindowConstraint();
+ cManager.addConstraint(new AdditionalTransportationCosts(vrp.getTransportCosts()));
VehicleFleetManager fleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
@@ -102,11 +104,7 @@ public class BuildCVRPAlgoFromScratch_IT {
vra = new VehicleRoutingAlgorithm(vrp, strategyManager);
vra.addListener(stateManager);
vra.addListener(new RemoveEmptyVehicles(fleetManager));
-
-// vra.getAlgorithmListeners().addListener(stateManager);
-// vra.getSearchStrategyManager().addSearchStrategyModuleListener(stateManager);
-// vra.getSearchStrategyManager().addSearchStrategyModuleListener(new RemoveEmptyVehicles(fleetManager));
-
+
VehicleRoutingProblemSolution iniSolution = new InsertionInitialSolutionFactory(bestInsertion, solutionCostCalculator).createSolution(vrp);
vra.addInitialSolution(iniSolution);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
index 20093453..ce3dae97 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
@@ -136,7 +136,7 @@ public class ServiceInsertionAndLoadConstraintsTest {
stateManager.informInsertionStarts(Arrays.asList(route), null);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
- ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
+ ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
switcher.put(Pickup.class, serviceInsertionCalc);
switcher.put(Delivery.class, serviceInsertionCalc);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
index 0937f9ee..0401f981 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
@@ -240,7 +240,7 @@ public class ShipmentInsertionCalculatorTest {
stateManager.informInsertionStarts(Arrays.asList(route), null);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
- ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
+ ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
switcher.put(Pickup.class, serviceInsertionCalc);
switcher.put(Shipment.class, insertionCalculator);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
index 675bcb98..82bd6c2c 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
@@ -27,6 +27,7 @@ import java.util.Collection;
import jsprit.core.algorithm.ExampleActivityCostFunction;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem;
+import jsprit.core.problem.constraint.AdditionalTransportationCosts;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.DriverImpl;
@@ -165,11 +166,9 @@ public class TestCalculatesServiceInsertion {
ConstraintManager cManager = new ConstraintManager(vrp,states);
cManager.addLoadConstraint();
cManager.addTimeWindowConstraint();
+ cManager.addConstraint(new AdditionalTransportationCosts(costs));
- ExampleActivityCostFunction activityCosts = new ExampleActivityCostFunction();
-
-
- serviceInsertion = new ServiceInsertionCalculator(costs, new LocalActivityInsertionCostsCalculator(costs, activityCosts), cManager, cManager);
+ serviceInsertion = new ServiceInsertionCalculator(costs, cManager);
// stateUpdater = new UpdateStates(states, costs, activityCosts);
diff --git a/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java b/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java
new file mode 100644
index 00000000..64f5aa9d
--- /dev/null
+++ b/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java
@@ -0,0 +1,219 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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 .
+ *
+ * Contributors:
+ * Stefan Schroeder - initial API and implementation
+ ******************************************************************************/
+package jsprit.examples;
+
+import java.util.Collection;
+
+import jsprit.analysis.toolbox.SolutionPrinter;
+import jsprit.core.algorithm.InsertionInitialSolutionFactory;
+import jsprit.core.algorithm.RemoveEmptyVehicles;
+import jsprit.core.algorithm.SearchStrategy;
+import jsprit.core.algorithm.SearchStrategyManager;
+import jsprit.core.algorithm.VariablePlusFixedSolutionCostCalculatorFactory;
+import jsprit.core.algorithm.VehicleRoutingAlgorithm;
+import jsprit.core.algorithm.acceptor.GreedyAcceptance;
+import jsprit.core.algorithm.module.RuinAndRecreateModule;
+import jsprit.core.algorithm.recreate.BestInsertionBuilder;
+import jsprit.core.algorithm.recreate.InsertionStrategy;
+import jsprit.core.algorithm.ruin.RadialRuinStrategyFactory;
+import jsprit.core.algorithm.ruin.RandomRuinStrategyFactory;
+import jsprit.core.algorithm.ruin.RuinStrategy;
+import jsprit.core.algorithm.ruin.distance.AvgServiceAndShipmentDistance;
+import jsprit.core.algorithm.selector.SelectBest;
+import jsprit.core.algorithm.state.StateManager;
+import jsprit.core.algorithm.state.UpdateVariableCosts;
+import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
+import jsprit.core.problem.VehicleRoutingProblem;
+import jsprit.core.problem.constraint.AdditionalTransportationCosts;
+import jsprit.core.problem.constraint.ConstraintManager;
+import jsprit.core.problem.solution.SolutionCostCalculator;
+import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
+import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
+import jsprit.core.problem.vehicle.VehicleFleetManager;
+import jsprit.core.util.Solutions;
+import jsprit.instance.reader.SolomonReader;
+import jsprit.util.Examples;
+
+public class BuildAlgorithmFromScratchWithHardAndSoftConstraints {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ /*
+ * some preparation - create output folder
+ */
+ Examples.createOutputFolder();
+
+ /*
+ * Build the problem.
+ *
+ * But define a problem-builder first.
+ */
+ VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
+
+ /*
+ * A solomonReader reads solomon-instance files, and stores the required information in the builder.
+ */
+ new SolomonReader(vrpBuilder).read("input/C101_solomon.txt");
+
+ /*
+ * Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
+ */
+ VehicleRoutingProblem vrp = vrpBuilder.build();
+
+ /*
+ * Build algorithm
+ */
+ VehicleRoutingAlgorithm vra = buildAlgorithmFromScratch(vrp);
+
+ /*
+ * search solution
+ */
+ Collection solutions = vra.searchSolutions();
+
+ /*
+ * print result
+ */
+ SolutionPrinter.print(Solutions.bestOf(solutions));
+
+ }
+
+ private static VehicleRoutingAlgorithm buildAlgorithmFromScratch(VehicleRoutingProblem vrp) {
+
+ /*
+ * manages route and activity states.
+ */
+ StateManager stateManager = new StateManager(vrp);
+ /*
+ * tells stateManager to update load states
+ */
+ stateManager.updateLoadStates();
+ /*
+ * tells stateManager to update time-window states
+ */
+ stateManager.updateTimeWindowStates();
+ /*
+ * stateManager.addStateUpdater(updater);
+ * lets you register your own stateUpdater
+ */
+
+ /*
+ * updates variable costs once a vehicleRoute has changed (by removing or adding a customer)
+ */
+ stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
+
+ /*
+ * constructs a constraintManager that manages the various hardConstraints (and soon also softConstraints)
+ */
+ ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
+ /*
+ * tells constraintManager to add timeWindowConstraints
+ */
+ constraintManager.addTimeWindowConstraint();
+ /*
+ * tells constraintManager to add loadConstraints
+ */
+ constraintManager.addLoadConstraint();
+ /*
+ * add an arbitrary number of hardConstraints by
+ * constraintManager.addConstraint(...)
+ */
+ constraintManager.addConstraint(new AdditionalTransportationCosts(vrp.getTransportCosts()));
+
+ /*
+ * define a fleetManager, here infinite vehicles can be used
+ */
+ VehicleFleetManager fleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
+
+ /*
+ * define ruin-and-recreate strategies
+ *
+ */
+ /*
+ * first, define an insertion-strategy, i.e. bestInsertion
+ */
+ BestInsertionBuilder iBuilder = new BestInsertionBuilder(vrp, fleetManager, stateManager, constraintManager);
+ /*
+ * no need to set further options
+ */
+ InsertionStrategy iStrategy = iBuilder.build();
+
+ /*
+ * second, define random-ruin that ruins 50-percent of the selected solution
+ */
+ RuinStrategy randomRuin = new RandomRuinStrategyFactory(0.5).createStrategy(vrp);
+
+ /*
+ * third, define radial-ruin that ruins 30-percent of the selected solution
+ * the second para defines the distance between two jobs.
+ */
+ RuinStrategy radialRuin = new RadialRuinStrategyFactory(0.3, new AvgServiceAndShipmentDistance(vrp.getTransportCosts())).createStrategy(vrp);
+
+ /*
+ * now define a strategy
+ */
+ /*
+ * but before define how a generated solution is evaluated
+ * here: the VariablePlusFixed.... comes out of the box and it does what its name suggests
+ */
+ SolutionCostCalculator solutionCostCalculator = new VariablePlusFixedSolutionCostCalculatorFactory(stateManager).createCalculator();
+
+ SearchStrategy firstStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator);
+ firstStrategy.addModule(new RuinAndRecreateModule("randomRuinAndBestInsertion", iStrategy, randomRuin));
+
+ SearchStrategy secondStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator);
+ secondStrategy.addModule(new RuinAndRecreateModule("radialRuinAndBestInsertion", iStrategy, radialRuin));
+
+ /*
+ * put both strategies together, each with the prob of 0.5 to be selected
+ */
+ SearchStrategyManager searchStrategyManager = new SearchStrategyManager();
+ searchStrategyManager.addStrategy(firstStrategy, 0.5);
+ searchStrategyManager.addStrategy(secondStrategy, 0.5);
+
+ /*
+ * construct the algorithm
+ */
+ VehicleRoutingAlgorithm vra = new VehicleRoutingAlgorithm(vrp, searchStrategyManager);
+ //do not forgett to add the stateManager listening to the algorithm-stages
+ vra.addListener(stateManager);
+ //remove empty vehicles after insertion has finished
+ vra.addListener(new RemoveEmptyVehicles(fleetManager));
+
+ /*
+ * Do not forget to add an initial solution by vra.addInitialSolution(solution);
+ * or
+ */
+ vra.addInitialSolution(new InsertionInitialSolutionFactory(iStrategy, solutionCostCalculator).createSolution(vrp));
+
+ /*
+ * define the nIterations (by default nIteration=100)
+ */
+ vra.setNuOfIterations(1000);
+
+ /*
+ * optionally define a premature termination criterion (by default: not criterion is set)
+ */
+ vra.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(100));
+
+ return vra;
+ }
+
+}
From a2552ba11619ccf22a334ca89514ea7cbb3ab237 Mon Sep 17 00:00:00 2001
From: jsprit
Date: Mon, 10 Feb 2014 10:19:24 +0100
Subject: [PATCH 15/22] Update README.md
---
README.md | 2 --
1 file changed, 2 deletions(-)
diff --git a/README.md b/README.md
index a97f6834..52b4ed68 100644
--- a/README.md
+++ b/README.md
@@ -59,5 +59,3 @@ It is mainly inspired by my research group at [KIT-ECON](http://netze.econ.kit.e
If you have questions or if you use jsprit, it would be great you give feedback and let me know your experience:
Email: jsprit.vehicle.routing@gmail.com
-
-[](http://githalytics.com/jsprit/jsprit)
From 9a471a58bedf9c48ae498faffb1007662c7a47eb Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Mon, 10 Feb 2014 10:22:02 +0100
Subject: [PATCH 16/22] added soft constraints and tests
---
.../analysis/toolbox/GraphStreamViewer.java | 1 -
.../algorithm/recreate/CalculatorBuilder.java | 2 +-
.../recreate/ServiceInsertionCalculator.java | 22 +++++---
.../AdditionalTransportationCosts.java | 2 +-
.../BuildCVRPAlgoFromScratch_IT.java | 3 +-
...erviceInsertionAndLoadConstraintsTest.java | 2 +-
.../ShipmentInsertionCalculatorTest.java | 2 +-
.../TestCalculatesServiceInsertion.java | 9 ++--
.../SoftActivityConstraintManagerTest.java | 50 +++++++++++++++++++
.../SoftRouteConstraintManagerTest.java | 46 +++++++++++++++++
.../constraint/TestConstraintManager.java | 42 +++++++++++++++-
11 files changed, 159 insertions(+), 22 deletions(-)
create mode 100644 jsprit-core/src/test/java/jsprit/core/problem/constraint/SoftActivityConstraintManagerTest.java
create mode 100644 jsprit-core/src/test/java/jsprit/core/problem/constraint/SoftRouteConstraintManagerTest.java
diff --git a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java
index 16928de8..b37af700 100644
--- a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java
+++ b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java
@@ -214,7 +214,6 @@ public class GraphStreamViewer {
//start rendering graph
render(g,view);
-
}
private void render(Graph g, View view) {
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java
index aedae439..02828d77 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java
@@ -253,7 +253,7 @@ class CalculatorBuilder {
}
ShipmentInsertionCalculator shipmentInsertion = new ShipmentInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager, constraintManager);
- ServiceInsertionCalculator serviceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), constraintManager);
+ ServiceInsertionCalculator serviceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
switcher.put(Shipment.class, shipmentInsertion);
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
index c8000ac3..289fe9fd 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
@@ -16,6 +16,7 @@
******************************************************************************/
package jsprit.core.algorithm.recreate;
+import jsprit.core.algorithm.recreate.ActivityInsertionCostsCalculator.ActivityInsertionCosts;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint.ConstraintsStatus;
@@ -59,15 +60,18 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
private VehicleRoutingTransportCosts transportCosts;
+ private ActivityInsertionCostsCalculator additionalTransportCostsCalculator;
+
private TourActivityFactory activityFactory;
- public ServiceInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ConstraintManager constraintManager) {
+ public ServiceInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ActivityInsertionCostsCalculator additionalTransportCostsCalculator, ConstraintManager constraintManager) {
super();
this.transportCosts = routingCosts;
hardRouteLevelConstraint = constraintManager;
hardActivityLevelConstraint = constraintManager;
softActivityConstraint = constraintManager;
softRouteConstraint = constraintManager;
+ this.additionalTransportCostsCalculator = additionalTransportCostsCalculator;
activityFactory = new DefaultTourActivityFactory();
logger.info("initialise " + this);
}
@@ -95,7 +99,7 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
double bestCost = bestKnownCosts;
//from job2insert induced costs at route level
- double routeICosts = softRouteConstraint.getCosts(insertionContext);
+ double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);
Service service = (Service)jobToInsert;
int insertionIndex = InsertionData.NO_INDEX;
@@ -114,9 +118,10 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
if(status.equals(ConstraintsStatus.FULFILLED)){
//from job2insert induced costs at activity level
- double activityICosts = softActivityConstraint.getCosts(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
- if(routeICosts + activityICosts < bestCost){
- bestCost = routeICosts + activityICosts;
+ double additionalICostsAtActLevel = softActivityConstraint.getCosts(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
+ ActivityInsertionCosts additionalTransportationCosts = additionalTransportCostsCalculator.getCosts(insertionContext, prevAct, nextAct, deliveryAct2Insert, prevActStartTime);
+ if(additionalICostsAtRouteLevel + additionalICostsAtActLevel + additionalTransportationCosts.getAdditionalCosts() < bestCost){
+ bestCost = additionalICostsAtRouteLevel + additionalICostsAtActLevel + additionalTransportationCosts.getAdditionalCosts();
insertionIndex = actIndex;
}
}
@@ -134,9 +139,10 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
if(!loopBroken){
ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
if(status.equals(ConstraintsStatus.FULFILLED)){
- double activityICosts = softActivityConstraint.getCosts(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
- if(routeICosts + activityICosts < bestCost){
- bestCost = routeICosts + activityICosts;
+ double additionalICostsAtActLevel = softActivityConstraint.getCosts(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
+ ActivityInsertionCosts additionalTransportationCosts = additionalTransportCostsCalculator.getCosts(insertionContext, prevAct, nextAct, deliveryAct2Insert, prevActStartTime);
+ if(additionalICostsAtRouteLevel + additionalICostsAtActLevel + additionalTransportationCosts.getAdditionalCosts() < bestCost){
+ bestCost = additionalICostsAtRouteLevel + additionalICostsAtActLevel + additionalTransportationCosts.getAdditionalCosts();
insertionIndex = actIndex;
}
}
diff --git a/jsprit-core/src/main/java/jsprit/core/problem/constraint/AdditionalTransportationCosts.java b/jsprit-core/src/main/java/jsprit/core/problem/constraint/AdditionalTransportationCosts.java
index f2a4451a..b11885c0 100644
--- a/jsprit-core/src/main/java/jsprit/core/problem/constraint/AdditionalTransportationCosts.java
+++ b/jsprit-core/src/main/java/jsprit/core/problem/constraint/AdditionalTransportationCosts.java
@@ -12,7 +12,7 @@ import jsprit.core.util.CalculationUtils;
* @author schroeder
*
*/
-public class AdditionalTransportationCosts implements SoftActivityConstraint{
+class AdditionalTransportationCosts implements SoftActivityConstraint{
private VehicleRoutingTransportCosts routingCosts;
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java
index 944999c0..c1094738 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java
@@ -32,7 +32,6 @@ import jsprit.core.algorithm.selector.SelectBest;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateVariableCosts;
import jsprit.core.problem.VehicleRoutingProblem;
-import jsprit.core.problem.constraint.AdditionalTransportationCosts;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.SolutionCostCalculator;
@@ -68,7 +67,7 @@ public class BuildCVRPAlgoFromScratch_IT {
ConstraintManager cManager = new ConstraintManager(vrp, stateManager);
cManager.addLoadConstraint();
cManager.addTimeWindowConstraint();
- cManager.addConstraint(new AdditionalTransportationCosts(vrp.getTransportCosts()));
+
VehicleFleetManager fleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
index ce3dae97..6e83633c 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ServiceInsertionAndLoadConstraintsTest.java
@@ -136,7 +136,7 @@ public class ServiceInsertionAndLoadConstraintsTest {
stateManager.informInsertionStarts(Arrays.asList(route), null);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
- ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, constraintManager);
+ ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, activityInsertionCostsCalculator, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
switcher.put(Pickup.class, serviceInsertionCalc);
switcher.put(Delivery.class, serviceInsertionCalc);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
index 0401f981..22ef73e3 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculatorTest.java
@@ -240,7 +240,7 @@ public class ShipmentInsertionCalculatorTest {
stateManager.informInsertionStarts(Arrays.asList(route), null);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
- ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, constraintManager);
+ ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, activityInsertionCostsCalculator, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
switcher.put(Pickup.class, serviceInsertionCalc);
switcher.put(Shipment.class, insertionCalculator);
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
index 82bd6c2c..0baaae61 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
@@ -24,11 +24,10 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
-import jsprit.core.algorithm.ExampleActivityCostFunction;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem;
-import jsprit.core.problem.constraint.AdditionalTransportationCosts;
import jsprit.core.problem.constraint.ConstraintManager;
+import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.DriverImpl;
import jsprit.core.problem.driver.DriverImpl.NoDriver;
@@ -166,12 +165,10 @@ public class TestCalculatesServiceInsertion {
ConstraintManager cManager = new ConstraintManager(vrp,states);
cManager.addLoadConstraint();
cManager.addTimeWindowConstraint();
- cManager.addConstraint(new AdditionalTransportationCosts(costs));
- serviceInsertion = new ServiceInsertionCalculator(costs, cManager);
-
+ VehicleRoutingActivityCosts actCosts = mock(VehicleRoutingActivityCosts.class);
-// stateUpdater = new UpdateStates(states, costs, activityCosts);
+ serviceInsertion = new ServiceInsertionCalculator(costs, new LocalActivityInsertionCostsCalculator(costs, actCosts), cManager);
}
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/constraint/SoftActivityConstraintManagerTest.java b/jsprit-core/src/test/java/jsprit/core/problem/constraint/SoftActivityConstraintManagerTest.java
new file mode 100644
index 00000000..2f8b486f
--- /dev/null
+++ b/jsprit-core/src/test/java/jsprit/core/problem/constraint/SoftActivityConstraintManagerTest.java
@@ -0,0 +1,50 @@
+package jsprit.core.problem.constraint;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import jsprit.core.problem.misc.JobInsertionContext;
+import jsprit.core.problem.solution.route.activity.TourActivity;
+
+import org.junit.Test;
+
+public class SoftActivityConstraintManagerTest {
+
+ @Test
+ public void whenAddingSoftConstraint_managerShouldHaveIt(){
+ SoftActivityConstraint c = mock(SoftActivityConstraint.class);
+ SoftActivityConstraintManager man = new SoftActivityConstraintManager();
+ assertEquals(0,man.getConstraints().size());
+ man.addConstraint(c);
+ assertEquals(1,man.getConstraints().size());
+ }
+
+ @Test
+ public void whenAddingTwoSoftConstraints_managerShouldHaveIt(){
+ SoftActivityConstraint c1 = mock(SoftActivityConstraint.class);
+ SoftActivityConstraint c2 = mock(SoftActivityConstraint.class);
+ SoftActivityConstraintManager man = new SoftActivityConstraintManager();
+ assertEquals(0,man.getConstraints().size());
+ man.addConstraint(c1);
+ man.addConstraint(c2);
+ assertEquals(2,man.getConstraints().size());
+ }
+
+ @Test
+ public void whenAddingTwoSoftConstrainta_managerShouldSumCostsCorrectly(){
+ SoftActivityConstraint c1 = mock(SoftActivityConstraint.class);
+ JobInsertionContext iContext = mock(JobInsertionContext.class);
+ TourActivity act_i = mock(TourActivity.class);
+ TourActivity act_k = mock(TourActivity.class);
+ TourActivity act_j = mock(TourActivity.class);
+ when(c1.getCosts(iContext,act_i,act_k,act_j,0.0)).thenReturn(1.0);
+ SoftActivityConstraint c2 = mock(SoftActivityConstraint.class);
+ when(c2.getCosts(iContext,act_i,act_k,act_j,0.0)).thenReturn(2.0);
+
+ SoftActivityConstraintManager man = new SoftActivityConstraintManager();
+
+ man.addConstraint(c1);
+ man.addConstraint(c2);
+ assertEquals(3.0,man.getCosts(iContext,act_i,act_k,act_j,0.0),0.01);
+ }
+}
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/constraint/SoftRouteConstraintManagerTest.java b/jsprit-core/src/test/java/jsprit/core/problem/constraint/SoftRouteConstraintManagerTest.java
new file mode 100644
index 00000000..75dab6b6
--- /dev/null
+++ b/jsprit-core/src/test/java/jsprit/core/problem/constraint/SoftRouteConstraintManagerTest.java
@@ -0,0 +1,46 @@
+package jsprit.core.problem.constraint;
+
+import jsprit.core.problem.misc.JobInsertionContext;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SoftRouteConstraintManagerTest {
+
+ @Test
+ public void whenAddingSoftRouteConstraint_managerShouldHaveIt(){
+ SoftRouteConstraint c = mock(SoftRouteConstraint.class);
+ SoftRouteConstraintManager man = new SoftRouteConstraintManager();
+ assertEquals(0,man.getConstraints().size());
+ man.addConstraint(c);
+ assertEquals(1,man.getConstraints().size());
+ }
+
+ @Test
+ public void whenAddingTwoSoftRouteConstraint_managerShouldHaveIt(){
+ SoftRouteConstraint c1 = mock(SoftRouteConstraint.class);
+ SoftRouteConstraint c2 = mock(SoftRouteConstraint.class);
+ SoftRouteConstraintManager man = new SoftRouteConstraintManager();
+ assertEquals(0,man.getConstraints().size());
+ man.addConstraint(c1);
+ man.addConstraint(c2);
+ assertEquals(2,man.getConstraints().size());
+ }
+
+ @Test
+ public void whenAddingTwoSoftRouteConstraint_managerShouldSumCostsCorrectly(){
+ SoftRouteConstraint c1 = mock(SoftRouteConstraint.class);
+ JobInsertionContext iContext = mock(JobInsertionContext.class);
+ when(c1.getCosts(iContext)).thenReturn(1.0);
+ SoftRouteConstraint c2 = mock(SoftRouteConstraint.class);
+ when(c2.getCosts(iContext)).thenReturn(2.0);
+ SoftRouteConstraintManager man = new SoftRouteConstraintManager();
+
+ man.addConstraint(c1);
+ man.addConstraint(c2);
+ assertEquals(3.0,man.getCosts(iContext),0.01);
+ }
+}
diff --git a/jsprit-core/src/test/java/jsprit/core/problem/constraint/TestConstraintManager.java b/jsprit-core/src/test/java/jsprit/core/problem/constraint/TestConstraintManager.java
index 58c55f22..6f8ec250 100644
--- a/jsprit-core/src/test/java/jsprit/core/problem/constraint/TestConstraintManager.java
+++ b/jsprit-core/src/test/java/jsprit/core/problem/constraint/TestConstraintManager.java
@@ -1,6 +1,6 @@
package jsprit.core.problem.constraint;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import java.util.ArrayList;
@@ -30,5 +30,45 @@ public class TestConstraintManager {
ConstraintManager cManager = new ConstraintManager(mock(VehicleRoutingProblem.class),mock(RouteAndActivityStateGetter.class),constraints);
assertEquals(1,cManager.getConstraints().size());
}
+
+ @Test
+ public void whenAddingSoftRouteConstraint_managerShouldHaveIt(){
+ SoftRouteConstraint c = mock(SoftRouteConstraint.class);
+ ConstraintManager man = new ConstraintManager(mock(VehicleRoutingProblem.class),mock(RouteAndActivityStateGetter.class));
+ assertEquals(0,man.getConstraints().size());
+ man.addConstraint(c);
+ assertEquals(1,man.getConstraints().size());
+ }
+
+ @Test
+ public void whenAddingTwoSoftRouteConstraint_managerShouldHaveIt(){
+ SoftRouteConstraint c1 = mock(SoftRouteConstraint.class);
+ SoftRouteConstraint c2 = mock(SoftRouteConstraint.class);
+ ConstraintManager man = new ConstraintManager(mock(VehicleRoutingProblem.class),mock(RouteAndActivityStateGetter.class));
+ assertEquals(0,man.getConstraints().size());
+ man.addConstraint(c1);
+ man.addConstraint(c2);
+ assertEquals(2,man.getConstraints().size());
+ }
+
+ @Test
+ public void whenAddingSoftActivityConstraint_managerShouldHaveIt(){
+ SoftActivityConstraint c = mock(SoftActivityConstraint.class);
+ ConstraintManager man = new ConstraintManager(mock(VehicleRoutingProblem.class),mock(RouteAndActivityStateGetter.class));
+ assertEquals(0,man.getConstraints().size());
+ man.addConstraint(c);
+ assertEquals(1,man.getConstraints().size());
+ }
+
+ @Test
+ public void whenAddingTwoSoftActivityConstraints_managerShouldHaveIt(){
+ SoftActivityConstraint c1 = mock(SoftActivityConstraint.class);
+ SoftActivityConstraint c2 = mock(SoftActivityConstraint.class);
+ ConstraintManager man = new ConstraintManager(mock(VehicleRoutingProblem.class),mock(RouteAndActivityStateGetter.class));
+ assertEquals(0,man.getConstraints().size());
+ man.addConstraint(c1);
+ man.addConstraint(c2);
+ assertEquals(2,man.getConstraints().size());
+ }
}
From dc24411802a3099acede61b968b667316be8f0bf Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Mon, 10 Feb 2014 16:23:42 +0100
Subject: [PATCH 17/22] added egress/access calc
---
.../AdditionalAccessEgressCalculator.java | 58 +++++++++++++++++
.../recreate/ServiceInsertionCalculator.java | 6 +-
.../TestCalculatesServiceInsertion.java | 62 ++++++++++++++++++-
jsprit-examples/input/algorithmConfig.xml | 12 ++--
...FromScratchWithHardAndSoftConstraints.java | 3 +-
.../jsprit/examples/MultipleDepotExample.java | 2 +-
...ltipleDepotExampleWithPenaltyVehicles.java | 26 ++------
7 files changed, 135 insertions(+), 34 deletions(-)
create mode 100644 jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AdditionalAccessEgressCalculator.java
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AdditionalAccessEgressCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AdditionalAccessEgressCalculator.java
new file mode 100644
index 00000000..164a3186
--- /dev/null
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AdditionalAccessEgressCalculator.java
@@ -0,0 +1,58 @@
+package jsprit.core.algorithm.recreate;
+
+import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
+import jsprit.core.problem.driver.Driver;
+import jsprit.core.problem.misc.JobInsertionContext;
+import jsprit.core.problem.solution.route.VehicleRoute;
+import jsprit.core.problem.solution.route.activity.TourActivity;
+import jsprit.core.problem.vehicle.Vehicle;
+
+/**
+ * Estimates additional access/egress costs when operating route with a new vehicle that has different start/end-location.
+ *
+ *
If two vehicles have the same start/end-location and departure-time .getCosts(...) must return zero.
+ *
+ * @author schroeder
+ *
+ */
+class AdditionalAccessEgressCalculator {
+
+ private VehicleRoutingTransportCosts routingCosts;
+
+ /**
+ * Constructs the estimator that estimates additional access/egress costs when operating route with a new vehicle that has different start/end-location.
+ *
+ *
If two vehicles have the same start/end-location and departure-time .getCosts(...) must return zero.
+ *
+ * @author schroeder
+ *
+ */
+ public AdditionalAccessEgressCalculator(VehicleRoutingTransportCosts routingCosts) {
+ this.routingCosts = routingCosts;
+ }
+
+ public double getCosts(JobInsertionContext insertionContext){
+ double delta_access = 0.0;
+ double delta_egress = 0.0;
+ VehicleRoute currentRoute = insertionContext.getRoute();
+ Vehicle newVehicle = insertionContext.getNewVehicle();
+ Driver newDriver = insertionContext.getNewDriver();
+ double newVehicleDepartureTime = insertionContext.getNewDepTime();
+ if(!currentRoute.isEmpty()){
+ double accessTransportCostNew = routingCosts.getTransportCost(newVehicle.getLocationId(), currentRoute.getActivities().get(0).getLocationId(), newVehicleDepartureTime, newDriver, newVehicle);
+ double accessTransportCostOld = routingCosts.getTransportCost(currentRoute.getStart().getLocationId(), currentRoute.getActivities().get(0).getLocationId(), currentRoute.getDepartureTime(), currentRoute.getDriver(), currentRoute.getVehicle());
+
+ delta_access = accessTransportCostNew - accessTransportCostOld;
+
+ TourActivity lastActivityBeforeEndOfRoute = currentRoute.getActivities().get(currentRoute.getActivities().size()-1);
+ double lastActivityEndTimeWithOldVehicleAndDepartureTime = lastActivityBeforeEndOfRoute.getEndTime();
+ double lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime = Math.max(0.0, lastActivityEndTimeWithOldVehicleAndDepartureTime + (newVehicleDepartureTime - currentRoute.getDepartureTime()));
+ double egressTransportCostNew = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocationId(), newVehicle.getLocationId() , lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime, newDriver, newVehicle);
+ double egressTransportCostOld = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocationId(), currentRoute.getEnd().getLocationId(), lastActivityEndTimeWithOldVehicleAndDepartureTime, currentRoute.getDriver(), currentRoute.getVehicle());
+
+ delta_egress = egressTransportCostNew - egressTransportCostOld;
+ }
+ return delta_access + delta_egress;
+ }
+
+}
\ No newline at end of file
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
index 289fe9fd..72265579 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ServiceInsertionCalculator.java
@@ -47,7 +47,7 @@ import org.apache.log4j.Logger;
*
*/
final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
-
+
private static final Logger logger = Logger.getLogger(ServiceInsertionCalculator.class);
private HardRouteStateLevelConstraint hardRouteLevelConstraint;
@@ -63,6 +63,8 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
private ActivityInsertionCostsCalculator additionalTransportCostsCalculator;
private TourActivityFactory activityFactory;
+
+ private AdditionalAccessEgressCalculator additionalAccessEgressCalculator;
public ServiceInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ActivityInsertionCostsCalculator additionalTransportCostsCalculator, ConstraintManager constraintManager) {
super();
@@ -73,6 +75,7 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
softRouteConstraint = constraintManager;
this.additionalTransportCostsCalculator = additionalTransportCostsCalculator;
activityFactory = new DefaultTourActivityFactory();
+ additionalAccessEgressCalculator = new AdditionalAccessEgressCalculator(routingCosts);
logger.info("initialise " + this);
}
@@ -100,6 +103,7 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
//from job2insert induced costs at route level
double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);
+ additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);
Service service = (Service)jobToInsert;
int insertionIndex = InsertionData.NO_INDEX;
diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
index 0baaae61..7b957c22 100644
--- a/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
+++ b/jsprit-core/src/test/java/jsprit/core/algorithm/recreate/TestCalculatesServiceInsertion.java
@@ -23,22 +23,30 @@ import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
+import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
+import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.driver.DriverImpl;
import jsprit.core.problem.driver.DriverImpl.NoDriver;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
+import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.ServiceActivity;
import jsprit.core.problem.solution.route.activity.TimeWindow;
import jsprit.core.problem.solution.route.activity.TourActivities;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
+import jsprit.core.problem.vehicle.VehicleImpl;
+import jsprit.core.util.Coordinate;
+import jsprit.core.util.EuclideanDistanceCalculator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
@@ -184,7 +192,6 @@ public class TestCalculatesServiceInsertion {
VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
states.informInsertionStarts(Arrays.asList(route), null);
-// stateUpdater.update(route);
InsertionData iData = serviceInsertion.getInsertionData(route, first, vehicle, vehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
assertEquals(20.0, iData.getInsertionCost(), 0.2);
@@ -253,8 +260,6 @@ public class TestCalculatesServiceInsertion {
tour.addActivity(ServiceActivity.newInstance(third));
VehicleRoute route = VehicleRoute.newInstance(tour,driver,vehicle);
-// route.addActivity(states.getActivity(first,true));
-// route.addActivity(states.getActivity(third,true));
states.informInsertionStarts(Arrays.asList(route), null);
InsertionData iData = serviceInsertion.getInsertionData(route, second, newVehicle, newVehicle.getEarliestDeparture(), null, Double.MAX_VALUE);
@@ -262,6 +267,57 @@ public class TestCalculatesServiceInsertion {
assertEquals(2, iData.getDeliveryInsertionIndex());
}
+ @Test
+ public void whenInsertingJobAndCurrRouteIsEmpty_accessEggressCalcShouldReturnZero(){
+ VehicleRoute route = VehicleRoute.Builder.newInstance(VehicleImpl.createNoVehicle(), DriverImpl.noDriver()).build();
+ AdditionalAccessEgressCalculator accessEgressCalc = new AdditionalAccessEgressCalculator(costs);
+ Job job = Service.Builder.newInstance("1", 0).setLocationId("1").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
+ JobInsertionContext iContex = new JobInsertionContext(route, job, newVehicle, mock(Driver.class), 0.0);
+ assertEquals(0.0, accessEgressCalc.getCosts(iContex),0.01);
+ }
+ @Test
+ public void whenInsertingJobAndCurrRouteAndVehicleHaveTheSameLocation_accessEggressCalcShouldReturnZero(){
+ VehicleRoute route = VehicleRoute.Builder.newInstance(newVehicle, DriverImpl.noDriver())
+ .addService(Service.Builder.newInstance("1", 0).setLocationId("1").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build())
+ .build();
+
+ AdditionalAccessEgressCalculator accessEgressCalc = new AdditionalAccessEgressCalculator(costs);
+ Job job = Service.Builder.newInstance("1", 0).setLocationId("1").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
+ JobInsertionContext iContex = new JobInsertionContext(route, job, newVehicle, mock(Driver.class), 0.0);
+ assertEquals(0.0, accessEgressCalc.getCosts(iContex),0.01);
+ }
+ @Test
+ public void whenInsertingJobAndCurrRouteAndNewVehicleHaveDifferentLocations_accessEggressCostsMustBeCorrect(){
+ final Map coords = new HashMap();
+ coords.put("oldV", Coordinate.newInstance(1, 0));
+ coords.put("newV", Coordinate.newInstance(5, 0));
+ coords.put("service", Coordinate.newInstance(0, 0));
+
+ AbstractForwardVehicleRoutingTransportCosts routingCosts = new AbstractForwardVehicleRoutingTransportCosts() {
+
+ @Override
+ public double getTransportTime(String fromId, String toId,double departureTime, Driver driver, Vehicle vehicle) {
+ return getTransportCost(fromId, toId, departureTime, driver, vehicle);
+ }
+
+ @Override
+ public double getTransportCost(String fromId, String toId,double departureTime, Driver driver, Vehicle vehicle) {
+ return EuclideanDistanceCalculator.calculateDistance(coords.get(fromId), coords.get(toId));
+ }
+ };
+ Vehicle oldVehicle = VehicleImpl.Builder.newInstance("oldV").setLocationId("oldV").build();
+
+ VehicleRoute route = VehicleRoute.Builder.newInstance(oldVehicle, DriverImpl.noDriver())
+ .addService(Service.Builder.newInstance("service", 0).setLocationId("service").build())
+ .build();
+
+ Vehicle newVehicle = VehicleImpl.Builder.newInstance("newV").setLocationId("newV").build();
+
+ AdditionalAccessEgressCalculator accessEgressCalc = new AdditionalAccessEgressCalculator(routingCosts);
+ Job job = Service.Builder.newInstance("service2", 0).setLocationId("service").build();
+ JobInsertionContext iContex = new JobInsertionContext(route, job, newVehicle, mock(Driver.class), 0.0);
+ assertEquals(8.0, accessEgressCalc.getCosts(iContex),0.01);
+ }
}
diff --git a/jsprit-examples/input/algorithmConfig.xml b/jsprit-examples/input/algorithmConfig.xml
index 410727cd..917e27bd 100755
--- a/jsprit-examples/input/algorithmConfig.xml
+++ b/jsprit-examples/input/algorithmConfig.xml
@@ -26,7 +26,7 @@
- route
+
@@ -35,8 +35,10 @@
-
-
+
+ 0.05
+ 20
+
@@ -51,7 +53,7 @@
-
+
@@ -67,7 +69,7 @@
-
+
diff --git a/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java b/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java
index 64f5aa9d..fa62669f 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java
@@ -40,7 +40,6 @@ import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateVariableCosts;
import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
import jsprit.core.problem.VehicleRoutingProblem;
-import jsprit.core.problem.constraint.AdditionalTransportationCosts;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
@@ -135,7 +134,7 @@ public class BuildAlgorithmFromScratchWithHardAndSoftConstraints {
* add an arbitrary number of hardConstraints by
* constraintManager.addConstraint(...)
*/
- constraintManager.addConstraint(new AdditionalTransportationCosts(vrp.getTransportCosts()));
+
/*
* define a fleetManager, here infinite vehicles can be used
diff --git a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java
index af082fcd..2fc6cf24 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExample.java
@@ -101,7 +101,7 @@ public class MultipleDepotExample {
/*
* solve the problem
*/
- VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig.xml");
+ VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfigWithSchrimpfAcceptance.xml");
vra.getAlgorithmListeners().addListener(new StopWatch(),Priority.HIGH);
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
Collection solutions = vra.searchSolutions();
diff --git a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExampleWithPenaltyVehicles.java b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExampleWithPenaltyVehicles.java
index 9d2b5efd..206b0f77 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExampleWithPenaltyVehicles.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/MultipleDepotExampleWithPenaltyVehicles.java
@@ -32,7 +32,6 @@ import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
-import jsprit.core.problem.vehicle.PenaltyVehicleType;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
@@ -85,30 +84,13 @@ public class MultipleDepotExampleWithPenaltyVehicles {
Vehicle vehicle = vehicleBuilder.build();
vrpBuilder.addVehicle(vehicle);
}
- /*
- * define penalty-type with the same id, but other higher fixed and variable costs
- */
- VehicleType penaltyType = VehicleTypeImpl.Builder.newInstance(depotCounter + "_type", capacity).setFixedCost(50).setCostPerDistance(3.0).build();
- /*
- * to mark the penalty-type as penalty-type, wrap it with PenaltyVehicleType(Wrapper)
- * this is to tell the fleetManager that this is not a regular but a penalty vehicle
- */
- PenaltyVehicleType penaltyVehicleType = new PenaltyVehicleType(penaltyType,3);
- String vehicleId = depotCounter + "_vehicle#penalty";
- VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance(vehicleId);
- vehicleBuilder.setLocationCoord(depotCoord);
- /*
- * set PenaltyVehicleType
- */
- vehicleBuilder.setType(penaltyVehicleType);
- vehicleBuilder.setLatestArrival(maxDuration);
- Vehicle penaltyVehicle = vehicleBuilder.build();
- vrpBuilder.addVehicle(penaltyVehicle);
-
depotCounter++;
}
-
+ /*
+ * define penalty-type with the same id, but other higher fixed and variable costs
+ */
+ vrpBuilder.addPenaltyVehicles(3, 50);
/*
* define problem with finite fleet
From 941e4ae29bc2dade71a4f0d7d01ce0c28902fbf1 Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Mon, 10 Feb 2014 16:28:02 +0100
Subject: [PATCH 18/22] added example
---
...pAndDeliveryWithMultipleDepotsExample.java | 146 ++++++++++++++++++
1 file changed, 146 insertions(+)
create mode 100644 jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsExample.java
diff --git a/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsExample.java b/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsExample.java
new file mode 100644
index 00000000..dfd69fd2
--- /dev/null
+++ b/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsExample.java
@@ -0,0 +1,146 @@
+/*******************************************************************************
+ * Copyright (C) 2013 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 .
+ ******************************************************************************/
+package jsprit.examples;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import jsprit.analysis.toolbox.GraphStreamViewer;
+import jsprit.analysis.toolbox.GraphStreamViewer.Label;
+import jsprit.analysis.toolbox.Plotter;
+import jsprit.analysis.toolbox.SolutionPrinter;
+import jsprit.core.algorithm.VehicleRoutingAlgorithm;
+import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
+import jsprit.core.problem.VehicleRoutingProblem;
+import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
+import jsprit.core.problem.io.VrpXMLWriter;
+import jsprit.core.problem.job.Shipment;
+import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
+import jsprit.core.problem.vehicle.Vehicle;
+import jsprit.core.problem.vehicle.VehicleImpl;
+import jsprit.core.problem.vehicle.VehicleImpl.Builder;
+import jsprit.core.problem.vehicle.VehicleType;
+import jsprit.core.problem.vehicle.VehicleTypeImpl;
+import jsprit.core.util.Coordinate;
+import jsprit.core.util.Solutions;
+import jsprit.util.Examples;
+
+
+public class EnRoutePickupAndDeliveryWithMultipleDepotsExample {
+
+ public static void main(String[] args) {
+ /*
+ * some preparation - create output folder
+ */
+ Examples.createOutputFolder();
+
+ /*
+ * get a vehicle type-builder and build a type with the typeId "vehicleType" and a capacity of 2
+ */
+ VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType", 2);
+ vehicleTypeBuilder.setCostPerDistance(1.0);
+ VehicleType vehicleType = vehicleTypeBuilder.build();
+
+ /*
+ * define two depots, i.e. two vehicle locations ([10,10],[50,50]) and equip them with an infinite number of vehicles of type 'vehicleType'
+ */
+ Builder vehicleBuilder1 = VehicleImpl.Builder.newInstance("vehicles@[10,10]");
+ vehicleBuilder1.setLocationCoord(Coordinate.newInstance(10, 10));
+ vehicleBuilder1.setType(vehicleType);
+ Vehicle vehicle1 = vehicleBuilder1.build();
+
+ Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("vehicles@[50,50]");
+ vehicleBuilder2.setLocationCoord(Coordinate.newInstance(50, 50));
+ vehicleBuilder2.setType(vehicleType);
+ Vehicle vehicle2 = vehicleBuilder2.build();
+
+
+ /*
+ * build shipments at the required locations, each with a capacity-demand of 1.
+ * 4 shipments
+ * 1: (5,7)->(6,9)
+ * 2: (5,13)->(6,11)
+ * 3: (15,7)->(14,9)
+ * 4: (15,13)->(14,11)
+ */
+
+ Shipment shipment1 = Shipment.Builder.newInstance("1", 1).setPickupCoord(Coordinate.newInstance(5, 7)).setDeliveryCoord(Coordinate.newInstance(6, 9)).build();
+ Shipment shipment2 = Shipment.Builder.newInstance("2", 1).setPickupCoord(Coordinate.newInstance(5, 13)).setDeliveryCoord(Coordinate.newInstance(6, 11)).build();
+
+ Shipment shipment3 = Shipment.Builder.newInstance("3", 1).setPickupCoord(Coordinate.newInstance(15, 7)).setDeliveryCoord(Coordinate.newInstance(14, 9)).build();
+ Shipment shipment4 = Shipment.Builder.newInstance("4", 1).setPickupCoord(Coordinate.newInstance(15, 13)).setDeliveryCoord(Coordinate.newInstance(14, 11)).build();
+
+ Shipment shipment5 = Shipment.Builder.newInstance("5", 1).setPickupCoord(Coordinate.newInstance(55, 57)).setDeliveryCoord(Coordinate.newInstance(56, 59)).build();
+ Shipment shipment6 = Shipment.Builder.newInstance("6", 1).setPickupCoord(Coordinate.newInstance(55, 63)).setDeliveryCoord(Coordinate.newInstance(56, 61)).build();
+
+ Shipment shipment7 = Shipment.Builder.newInstance("7", 1).setPickupCoord(Coordinate.newInstance(65, 57)).setDeliveryCoord(Coordinate.newInstance(64, 59)).build();
+ Shipment shipment8 = Shipment.Builder.newInstance("8", 1).setPickupCoord(Coordinate.newInstance(65, 63)).setDeliveryCoord(Coordinate.newInstance(64, 61)).build();
+
+
+ VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
+ vrpBuilder.addVehicle(vehicle1).addVehicle(vehicle2);
+ vrpBuilder.addJob(shipment1).addJob(shipment2).addJob(shipment3).addJob(shipment4);
+ vrpBuilder.addJob(shipment5).addJob(shipment6).addJob(shipment7).addJob(shipment8);
+
+ vrpBuilder.setFleetSize(FleetSize.FINITE);
+ VehicleRoutingProblem problem = vrpBuilder.build();
+
+ /*
+ * get the algorithm out-of-the-box.
+ */
+ VehicleRoutingAlgorithm algorithm = VehicleRoutingAlgorithms.readAndCreateAlgorithm(problem, "input/algorithmConfig_noVehicleSwitch.xml");
+ algorithm.setNuOfIterations(30000);
+ /*
+ * and search a solution
+ */
+ Collection solutions = algorithm.searchSolutions();
+
+ /*
+ * get the best
+ */
+ VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);
+
+ /*
+ * write out problem and solution to xml-file
+ */
+ new VrpXMLWriter(problem, solutions).write("output/shipment-problem-with-solution.xml");
+
+ /*
+ * print nRoutes and totalCosts of bestSolution
+ */
+ SolutionPrinter.print(bestSolution);
+
+ /*
+ * plot problem without solution
+ */
+ Plotter problemPlotter = new Plotter(problem);
+ problemPlotter.plotShipments(true);
+ problemPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_problem.png", "en-route pickup and delivery");
+
+ /*
+ * plot problem with solution
+ */
+ Plotter solutionPlotter = new Plotter(problem,Arrays.asList(Solutions.bestOf(solutions).getRoutes().iterator().next()));
+ solutionPlotter.plotShipments(true);
+ solutionPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_solution.png", "en-route pickup and delivery");
+
+ new GraphStreamViewer(problem,Solutions.bestOf(solutions)).labelWith(Label.ACTIVITY).setRenderDelay(100).setRenderShipments(true).display();
+
+ }
+
+}
+
From 330fa937a000f3859d93b6d91d47ff608af1b0df Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Mon, 10 Feb 2014 17:44:36 +0100
Subject: [PATCH 19/22] added soft constraints to shipment calc
---
.../algorithm/recreate/CalculatorBuilder.java | 2 +-
.../recreate/ShipmentInsertionCalculator.java | 40 +++--
...erviceInsertionAndLoadConstraintsTest.java | 7 +-
.../ShipmentInsertionCalculatorTest.java | 9 +-
.../TestCalculatesServiceInsertion.java | 107 +++++--------
.../jsprit/examples/BicycleMessenger.java | 2 +-
...pAndDeliveryWithMultipleDepotsExample.java | 41 +++--
...rvicePickupsWithMultipleDepotsExample.java | 145 ++++++++++++++++++
8 files changed, 257 insertions(+), 96 deletions(-)
create mode 100644 jsprit-examples/src/main/java/jsprit/examples/ServicePickupsWithMultipleDepotsExample.java
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java
index 02828d77..bbac87d2 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/CalculatorBuilder.java
@@ -252,7 +252,7 @@ class CalculatorBuilder {
actInsertionCalc = activityInsertionCostCalculator;
}
- ShipmentInsertionCalculator shipmentInsertion = new ShipmentInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager, constraintManager);
+ ShipmentInsertionCalculator shipmentInsertion = new ShipmentInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager);
ServiceInsertionCalculator serviceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculator.java
index 8a98d517..4c21eeb3 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculator.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/ShipmentInsertionCalculator.java
@@ -19,7 +19,10 @@ package jsprit.core.algorithm.recreate;
import java.util.List;
import jsprit.core.algorithm.recreate.ActivityInsertionCostsCalculator.ActivityInsertionCosts;
+import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint;
+import jsprit.core.problem.constraint.SoftActivityConstraint;
+import jsprit.core.problem.constraint.SoftRouteConstraint;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint.ConstraintsStatus;
import jsprit.core.problem.constraint.HardRouteStateLevelConstraint;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
@@ -36,7 +39,6 @@ import jsprit.core.problem.solution.route.activity.TourShipmentActivityFactory;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl.NoVehicle;
import jsprit.core.util.CalculationUtils;
-import jsprit.core.util.Neighborhood;
import org.apache.log4j.Logger;
@@ -51,23 +53,28 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
private HardActivityStateLevelConstraint hardActivityLevelConstraint;
+ private SoftRouteConstraint softRouteConstraint;
+
+ private SoftActivityConstraint softActivityConstraint;
+
private ActivityInsertionCostsCalculator activityInsertionCostsCalculator;
private VehicleRoutingTransportCosts transportCosts;
private TourShipmentActivityFactory activityFactory;
- public void setNeighborhood(Neighborhood neighborhood) {
- logger.info("initialise neighborhood " + neighborhood);
- }
+ private AdditionalAccessEgressCalculator additionalAccessEgressCalculator;
- public ShipmentInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ActivityInsertionCostsCalculator activityInsertionCostsCalculator, HardRouteStateLevelConstraint hardRouteLevelConstraint, HardActivityStateLevelConstraint hardActivityLevelConstraint) {
+ public ShipmentInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ActivityInsertionCostsCalculator activityInsertionCostsCalculator, ConstraintManager constraintManager) {
super();
this.activityInsertionCostsCalculator = activityInsertionCostsCalculator;
- this.hardRouteLevelConstraint = hardRouteLevelConstraint;
- this.hardActivityLevelConstraint = hardActivityLevelConstraint;
+ this.hardRouteLevelConstraint = constraintManager;
+ this.hardActivityLevelConstraint = constraintManager;
+ this.softActivityConstraint = constraintManager;
+ this.softRouteConstraint = constraintManager;
this.transportCosts = routingCosts;
activityFactory = new DefaultShipmentActivityFactory();
+ additionalAccessEgressCalculator = new AdditionalAccessEgressCalculator(routingCosts);
logger.info("initialise " + this);
}
@@ -93,6 +100,10 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
}
double bestCost = bestKnownCosts;
+
+ double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);
+ additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);
+
Shipment shipment = (Shipment)jobToInsert;
TourActivity pickupShipment = activityFactory.createPickup(shipment);
TourActivity deliverShipment = activityFactory.createDelivery(shipment);
@@ -122,6 +133,7 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
pickupShipmentLoopBroken = true;
break;
}
+ double additionalPickupICosts = softActivityConstraint.getCosts(insertionContext, prevAct, pickupShipment, activities.get(i), prevActEndTime);
ActivityInsertionCosts pickupAIC = calculate(insertionContext,prevAct,pickupShipment,activities.get(i),prevActEndTime);
TourActivity prevAct_deliveryLoop = pickupShipment;
double shipmentPickupArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), pickupShipment.getLocationId(), prevActEndTime, newDriver, newVehicle);
@@ -132,8 +144,10 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
for(int j=i;j jobs = new ArrayList();
jobs.add(first);
jobs.add(second);
@@ -279,12 +249,11 @@ public class TestCalculatesServiceInsertion {
@Test
public void whenInsertingJobAndCurrRouteAndVehicleHaveTheSameLocation_accessEggressCalcShouldReturnZero(){
VehicleRoute route = VehicleRoute.Builder.newInstance(newVehicle, DriverImpl.noDriver())
- .addService(Service.Builder.newInstance("1", 0).setLocationId("1").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build())
+ .addService(first)
.build();
AdditionalAccessEgressCalculator accessEgressCalc = new AdditionalAccessEgressCalculator(costs);
- Job job = Service.Builder.newInstance("1", 0).setLocationId("1").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
- JobInsertionContext iContex = new JobInsertionContext(route, job, newVehicle, mock(Driver.class), 0.0);
+ JobInsertionContext iContex = new JobInsertionContext(route, first, newVehicle, mock(Driver.class), 0.0);
assertEquals(0.0, accessEgressCalc.getCosts(iContex),0.01);
}
diff --git a/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java b/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
index 8ee6c648..fbcc7ca9 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
@@ -239,7 +239,7 @@ public class BicycleMessenger {
//if you want, terminate it after 1000 iterations with no change
// algorithm.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(1000));
// algorithm.addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
- algorithm.setNuOfIterations(200);
+ algorithm.setNuOfIterations(2000);
Collection solutions = algorithm.searchSolutions();
//this is just to ensure that solution meet the above constraints
diff --git a/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsExample.java b/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsExample.java
index dfd69fd2..476346b6 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsExample.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsExample.java
@@ -63,11 +63,20 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsExample {
vehicleBuilder1.setType(vehicleType);
Vehicle vehicle1 = vehicleBuilder1.build();
- Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("vehicles@[50,50]");
- vehicleBuilder2.setLocationCoord(Coordinate.newInstance(50, 50));
+ Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("vehicles@[30,30]");
+ vehicleBuilder2.setLocationCoord(Coordinate.newInstance(30, 30));
vehicleBuilder2.setType(vehicleType);
Vehicle vehicle2 = vehicleBuilder2.build();
+ Builder vehicleBuilder3 = VehicleImpl.Builder.newInstance("vehicles@[10,30]");
+ vehicleBuilder3.setLocationCoord(Coordinate.newInstance(10, 30));
+ vehicleBuilder3.setType(vehicleType);
+ Vehicle vehicle3 = vehicleBuilder3.build();
+
+ Builder vehicleBuilder4 = VehicleImpl.Builder.newInstance("vehicles@[30,10]");
+ vehicleBuilder4.setLocationCoord(Coordinate.newInstance(30, 10));
+ vehicleBuilder4.setType(vehicleType);
+ Vehicle vehicle4 = vehicleBuilder4.build();
/*
* build shipments at the required locations, each with a capacity-demand of 1.
@@ -84,17 +93,31 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsExample {
Shipment shipment3 = Shipment.Builder.newInstance("3", 1).setPickupCoord(Coordinate.newInstance(15, 7)).setDeliveryCoord(Coordinate.newInstance(14, 9)).build();
Shipment shipment4 = Shipment.Builder.newInstance("4", 1).setPickupCoord(Coordinate.newInstance(15, 13)).setDeliveryCoord(Coordinate.newInstance(14, 11)).build();
- Shipment shipment5 = Shipment.Builder.newInstance("5", 1).setPickupCoord(Coordinate.newInstance(55, 57)).setDeliveryCoord(Coordinate.newInstance(56, 59)).build();
- Shipment shipment6 = Shipment.Builder.newInstance("6", 1).setPickupCoord(Coordinate.newInstance(55, 63)).setDeliveryCoord(Coordinate.newInstance(56, 61)).build();
+ Shipment shipment5 = Shipment.Builder.newInstance("5", 1).setPickupCoord(Coordinate.newInstance(25, 27)).setDeliveryCoord(Coordinate.newInstance(26, 29)).build();
+ Shipment shipment6 = Shipment.Builder.newInstance("6", 1).setPickupCoord(Coordinate.newInstance(25, 33)).setDeliveryCoord(Coordinate.newInstance(26, 31)).build();
- Shipment shipment7 = Shipment.Builder.newInstance("7", 1).setPickupCoord(Coordinate.newInstance(65, 57)).setDeliveryCoord(Coordinate.newInstance(64, 59)).build();
- Shipment shipment8 = Shipment.Builder.newInstance("8", 1).setPickupCoord(Coordinate.newInstance(65, 63)).setDeliveryCoord(Coordinate.newInstance(64, 61)).build();
+ Shipment shipment7 = Shipment.Builder.newInstance("7", 1).setPickupCoord(Coordinate.newInstance(35, 27)).setDeliveryCoord(Coordinate.newInstance(34, 29)).build();
+ Shipment shipment8 = Shipment.Builder.newInstance("8", 1).setPickupCoord(Coordinate.newInstance(35, 33)).setDeliveryCoord(Coordinate.newInstance(34, 31)).build();
+
+ Shipment shipment9 = Shipment.Builder.newInstance("9", 1).setPickupCoord(Coordinate.newInstance(5, 27)).setDeliveryCoord(Coordinate.newInstance(6, 29)).build();
+ Shipment shipment10 = Shipment.Builder.newInstance("10", 1).setPickupCoord(Coordinate.newInstance(5, 33)).setDeliveryCoord(Coordinate.newInstance(6, 31)).build();
+
+ Shipment shipment11 = Shipment.Builder.newInstance("11", 1).setPickupCoord(Coordinate.newInstance(15, 27)).setDeliveryCoord(Coordinate.newInstance(14, 29)).build();
+ Shipment shipment12 = Shipment.Builder.newInstance("12", 1).setPickupCoord(Coordinate.newInstance(15, 33)).setDeliveryCoord(Coordinate.newInstance(14, 31)).build();
+
+ Shipment shipment13 = Shipment.Builder.newInstance("13", 1).setPickupCoord(Coordinate.newInstance(25, 7)).setDeliveryCoord(Coordinate.newInstance(26, 9)).build();
+ Shipment shipment14 = Shipment.Builder.newInstance("14", 1).setPickupCoord(Coordinate.newInstance(25, 13)).setDeliveryCoord(Coordinate.newInstance(26, 11)).build();
+
+ Shipment shipment15 = Shipment.Builder.newInstance("15", 1).setPickupCoord(Coordinate.newInstance(35, 7)).setDeliveryCoord(Coordinate.newInstance(34, 9)).build();
+ Shipment shipment16 = Shipment.Builder.newInstance("16", 1).setPickupCoord(Coordinate.newInstance(35, 13)).setDeliveryCoord(Coordinate.newInstance(34, 11)).build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
- vrpBuilder.addVehicle(vehicle1).addVehicle(vehicle2);
+ vrpBuilder.addVehicle(vehicle1).addVehicle(vehicle2).addVehicle(vehicle3).addVehicle(vehicle4);
vrpBuilder.addJob(shipment1).addJob(shipment2).addJob(shipment3).addJob(shipment4);
vrpBuilder.addJob(shipment5).addJob(shipment6).addJob(shipment7).addJob(shipment8);
+ vrpBuilder.addJob(shipment9).addJob(shipment10).addJob(shipment11).addJob(shipment12);
+ vrpBuilder.addJob(shipment13).addJob(shipment14).addJob(shipment15).addJob(shipment16);
vrpBuilder.setFleetSize(FleetSize.FINITE);
VehicleRoutingProblem problem = vrpBuilder.build();
@@ -102,8 +125,8 @@ public class EnRoutePickupAndDeliveryWithMultipleDepotsExample {
/*
* get the algorithm out-of-the-box.
*/
- VehicleRoutingAlgorithm algorithm = VehicleRoutingAlgorithms.readAndCreateAlgorithm(problem, "input/algorithmConfig_noVehicleSwitch.xml");
- algorithm.setNuOfIterations(30000);
+ VehicleRoutingAlgorithm algorithm = VehicleRoutingAlgorithms.readAndCreateAlgorithm(problem, "input/algorithmConfig.xml");
+// algorithm.setNuOfIterations(30000);
/*
* and search a solution
*/
diff --git a/jsprit-examples/src/main/java/jsprit/examples/ServicePickupsWithMultipleDepotsExample.java b/jsprit-examples/src/main/java/jsprit/examples/ServicePickupsWithMultipleDepotsExample.java
new file mode 100644
index 00000000..4e5dadd1
--- /dev/null
+++ b/jsprit-examples/src/main/java/jsprit/examples/ServicePickupsWithMultipleDepotsExample.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * Copyright (C) 2013 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 .
+ ******************************************************************************/
+package jsprit.examples;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import jsprit.analysis.toolbox.GraphStreamViewer;
+import jsprit.analysis.toolbox.GraphStreamViewer.Label;
+import jsprit.analysis.toolbox.Plotter;
+import jsprit.analysis.toolbox.SolutionPrinter;
+import jsprit.core.algorithm.VehicleRoutingAlgorithm;
+import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
+import jsprit.core.problem.VehicleRoutingProblem;
+import jsprit.core.problem.io.VrpXMLWriter;
+import jsprit.core.problem.job.Service;
+import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
+import jsprit.core.problem.vehicle.Vehicle;
+import jsprit.core.problem.vehicle.VehicleImpl;
+import jsprit.core.problem.vehicle.VehicleImpl.Builder;
+import jsprit.core.problem.vehicle.VehicleType;
+import jsprit.core.problem.vehicle.VehicleTypeImpl;
+import jsprit.core.util.Coordinate;
+import jsprit.core.util.Solutions;
+import jsprit.util.Examples;
+
+
+public class ServicePickupsWithMultipleDepotsExample {
+
+ public static void main(String[] args) {
+ /*
+ * some preparation - create output folder
+ */
+ Examples.createOutputFolder();
+
+ /*
+ * get a vehicle type-builder and build a type with the typeId "vehicleType" and a capacity of 2
+ */
+ VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType", 8);
+ vehicleTypeBuilder.setCostPerDistance(1.0);
+ VehicleType vehicleType = vehicleTypeBuilder.build();
+
+ /*
+ * define two depots, i.e. two vehicle locations ([10,10],[50,50]) and equip them with an infinite number of vehicles of type 'vehicleType'
+ */
+ Builder vehicleBuilder1 = VehicleImpl.Builder.newInstance("vehicles@[10,10]");
+ vehicleBuilder1.setLocationCoord(Coordinate.newInstance(10, 10));
+ vehicleBuilder1.setType(vehicleType);
+ Vehicle vehicle1 = vehicleBuilder1.build();
+
+ Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("vehicles@[50,50]");
+ vehicleBuilder2.setLocationCoord(Coordinate.newInstance(50, 50));
+ vehicleBuilder2.setType(vehicleType);
+ Vehicle vehicle2 = vehicleBuilder2.build();
+
+
+ /*
+ * build shipments at the required locations, each with a capacity-demand of 1.
+ * 4 shipments
+ * 1: (5,7)->(6,9)
+ * 2: (5,13)->(6,11)
+ * 3: (15,7)->(14,9)
+ * 4: (15,13)->(14,11)
+ */
+
+ Service shipment1 = Service.Builder.newInstance("1", 1).setCoord(Coordinate.newInstance(5, 7)).build();
+ Service shipment2 = Service.Builder.newInstance("2", 1).setCoord(Coordinate.newInstance(5, 13)).build();
+
+ Service shipment3 = Service.Builder.newInstance("3", 1).setCoord(Coordinate.newInstance(15, 7)).build();
+ Service shipment4 = Service.Builder.newInstance("4", 1).setCoord(Coordinate.newInstance(15, 13)).build();
+
+ Service shipment5 = Service.Builder.newInstance("5", 1).setCoord(Coordinate.newInstance(55, 57)).build();
+ Service shipment6 = Service.Builder.newInstance("6", 1).setCoord(Coordinate.newInstance(55, 63)).build();
+
+ Service shipment7 = Service.Builder.newInstance("7", 1).setCoord(Coordinate.newInstance(65, 57)).build();
+ Service shipment8 = Service.Builder.newInstance("8", 1).setCoord(Coordinate.newInstance(65, 63)).build();
+
+
+ VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
+ vrpBuilder.addVehicle(vehicle1).addVehicle(vehicle2);
+ vrpBuilder.addJob(shipment1).addJob(shipment2).addJob(shipment3).addJob(shipment4);
+ vrpBuilder.addJob(shipment5).addJob(shipment6).addJob(shipment7).addJob(shipment8);
+
+// vrpBuilder.setFleetSize(FleetSize.FINITE);
+ VehicleRoutingProblem problem = vrpBuilder.build();
+
+ /*
+ * get the algorithm out-of-the-box.
+ */
+ VehicleRoutingAlgorithm algorithm = VehicleRoutingAlgorithms.readAndCreateAlgorithm(problem, "input/algorithmConfig.xml");
+ algorithm.setNuOfIterations(10);
+
+ /*
+ * and search a solution
+ */
+ Collection solutions = algorithm.searchSolutions();
+
+ /*
+ * get the best
+ */
+ VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);
+
+ /*
+ * write out problem and solution to xml-file
+ */
+ new VrpXMLWriter(problem, solutions).write("output/shipment-problem-with-solution.xml");
+
+ /*
+ * print nRoutes and totalCosts of bestSolution
+ */
+ SolutionPrinter.print(bestSolution);
+
+ /*
+ * plot problem without solution
+ */
+ Plotter problemPlotter = new Plotter(problem);
+ problemPlotter.plotShipments(true);
+ problemPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_problem.png", "en-route pickup and delivery");
+
+ /*
+ * plot problem with solution
+ */
+ Plotter solutionPlotter = new Plotter(problem,Arrays.asList(Solutions.bestOf(solutions).getRoutes().iterator().next()));
+ solutionPlotter.plotShipments(true);
+ solutionPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_solution.png", "en-route pickup and delivery");
+
+ new GraphStreamViewer(problem,Solutions.bestOf(solutions)).labelWith(Label.ACTIVITY).setRenderDelay(100).setRenderShipments(true).display();
+
+ }
+
+}
From 97fa2a7639cd0004d8dbefca0b63b1b709f22f73 Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Mon, 10 Feb 2014 18:01:23 +0100
Subject: [PATCH 20/22] some mod
---
jsprit-examples/input/algorithmConfig_open.xml | 10 +++++-----
.../main/java/jsprit/examples/BicycleMessenger.java | 7 ++++---
2 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/jsprit-examples/input/algorithmConfig_open.xml b/jsprit-examples/input/algorithmConfig_open.xml
index 25c78f00..031f78f0 100755
--- a/jsprit-examples/input/algorithmConfig_open.xml
+++ b/jsprit-examples/input/algorithmConfig_open.xml
@@ -22,11 +22,11 @@
- 1000
+ 20000
- false
+ true
@@ -36,8 +36,8 @@
- 0.1
- 40
+ 0.05
+ 20
@@ -56,7 +56,7 @@
-
+ 0.3
diff --git a/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java b/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
index fbcc7ca9..cf66ee66 100644
--- a/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
+++ b/jsprit-examples/src/main/java/jsprit/examples/BicycleMessenger.java
@@ -8,6 +8,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.GraphStreamViewer.Label;
import jsprit.analysis.toolbox.Plotter;
@@ -226,7 +227,7 @@ public class BicycleMessenger {
problemBuilder.addConstraint(new ThreeTimesLessThanBestDirectRouteConstraint(nearestMessengers, routingCosts, stateManager));
problemBuilder.addConstraint(new IgnoreMessengerThatCanNeverMeetTimeRequirements(nearestMessengers, routingCosts));
- problemBuilder.addPenaltyVehicles(10.0,50000);
+ problemBuilder.addPenaltyVehicles(20.0,50000);
//finally build the problem
VehicleRoutingProblem bicycleMessengerProblem = problemBuilder.build();
@@ -238,8 +239,8 @@ public class BicycleMessenger {
VehicleRoutingAlgorithm algorithm = VehicleRoutingAlgorithms.readAndCreateAlgorithm(bicycleMessengerProblem,"input/algorithmConfig_open.xml", stateManager);
//if you want, terminate it after 1000 iterations with no change
// algorithm.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(1000));
-// algorithm.addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
- algorithm.setNuOfIterations(2000);
+ algorithm.addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
+// algorithm.setNuOfIterations(2000);
Collection solutions = algorithm.searchSolutions();
//this is just to ensure that solution meet the above constraints
From db14ea481c873004aafb7918330c510fa6bcbcc9 Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Mon, 10 Feb 2014 18:18:44 +0100
Subject: [PATCH 21/22] modified access/egress calc to reflect open routes
---
.../AdditionalAccessEgressCalculator.java | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AdditionalAccessEgressCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AdditionalAccessEgressCalculator.java
index 164a3186..bd11546e 100644
--- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AdditionalAccessEgressCalculator.java
+++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AdditionalAccessEgressCalculator.java
@@ -44,13 +44,15 @@ class AdditionalAccessEgressCalculator {
delta_access = accessTransportCostNew - accessTransportCostOld;
- TourActivity lastActivityBeforeEndOfRoute = currentRoute.getActivities().get(currentRoute.getActivities().size()-1);
- double lastActivityEndTimeWithOldVehicleAndDepartureTime = lastActivityBeforeEndOfRoute.getEndTime();
- double lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime = Math.max(0.0, lastActivityEndTimeWithOldVehicleAndDepartureTime + (newVehicleDepartureTime - currentRoute.getDepartureTime()));
- double egressTransportCostNew = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocationId(), newVehicle.getLocationId() , lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime, newDriver, newVehicle);
- double egressTransportCostOld = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocationId(), currentRoute.getEnd().getLocationId(), lastActivityEndTimeWithOldVehicleAndDepartureTime, currentRoute.getDriver(), currentRoute.getVehicle());
-
- delta_egress = egressTransportCostNew - egressTransportCostOld;
+ if(newVehicle.isReturnToDepot()){
+ TourActivity lastActivityBeforeEndOfRoute = currentRoute.getActivities().get(currentRoute.getActivities().size()-1);
+ double lastActivityEndTimeWithOldVehicleAndDepartureTime = lastActivityBeforeEndOfRoute.getEndTime();
+ double lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime = Math.max(0.0, lastActivityEndTimeWithOldVehicleAndDepartureTime + (newVehicleDepartureTime - currentRoute.getDepartureTime()));
+ double egressTransportCostNew = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocationId(), newVehicle.getLocationId() , lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime, newDriver, newVehicle);
+ double egressTransportCostOld = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocationId(), currentRoute.getEnd().getLocationId(), lastActivityEndTimeWithOldVehicleAndDepartureTime, currentRoute.getDriver(), currentRoute.getVehicle());
+
+ delta_egress = egressTransportCostNew - egressTransportCostOld;
+ }
}
return delta_access + delta_egress;
}
From 5d80a4caf00db7ee2bac0cf8b850ce4b64c077e1 Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Mon, 10 Feb 2014 21:39:45 +0100
Subject: [PATCH 22/22] added another example
---
...potsAndVehicleAccessConstraintExample.java | 225 ++++++++++++++++++
1 file changed, 225 insertions(+)
create mode 100644 jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintExample.java
diff --git a/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintExample.java b/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintExample.java
new file mode 100644
index 00000000..ae5a2749
--- /dev/null
+++ b/jsprit-examples/src/main/java/jsprit/examples/EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintExample.java
@@ -0,0 +1,225 @@
+/*******************************************************************************
+ * Copyright (C) 2013 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 .
+ ******************************************************************************/
+package jsprit.examples;
+
+import java.util.Collection;
+
+import jsprit.analysis.toolbox.GraphStreamViewer;
+import jsprit.analysis.toolbox.GraphStreamViewer.Label;
+import jsprit.analysis.toolbox.SolutionPrinter;
+import jsprit.core.algorithm.VehicleRoutingAlgorithm;
+import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
+import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
+import jsprit.core.algorithm.state.StateManager;
+import jsprit.core.algorithm.state.StateUpdater;
+import jsprit.core.problem.VehicleRoutingProblem;
+import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
+import jsprit.core.problem.constraint.HardRouteStateLevelConstraint;
+import jsprit.core.problem.job.Job;
+import jsprit.core.problem.job.Shipment;
+import jsprit.core.problem.misc.JobInsertionContext;
+import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
+import jsprit.core.problem.solution.route.VehicleRoute;
+import jsprit.core.problem.vehicle.Vehicle;
+import jsprit.core.problem.vehicle.VehicleImpl;
+import jsprit.core.problem.vehicle.VehicleImpl.Builder;
+import jsprit.core.problem.vehicle.VehicleType;
+import jsprit.core.problem.vehicle.VehicleTypeImpl;
+import jsprit.core.util.Coordinate;
+import jsprit.core.util.Solutions;
+import jsprit.util.Examples;
+
+
+public class EnRoutePickupAndDeliveryWithMultipleDepotsAndVehicleAccessConstraintExample {
+
+ static class ClusterMem implements StateUpdater, JobInsertedListener {
+
+ private StateManager stateManager;
+
+ public ClusterMem(StateManager stateManager) {
+ super();
+ this.stateManager = stateManager;
+ }
+
+ @Override
+ public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
+
+ }
+
+
+ }
+
+ public static void main(String[] args) {
+ /*
+ * some preparation - create output folder
+ */
+ Examples.createOutputFolder();
+
+ /*
+ * get a vehicle type-builder and build a type with the typeId "vehicleType" and a capacity of 2
+ */
+ VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType", 2);
+ vehicleTypeBuilder.setCostPerDistance(1.0);
+ VehicleType vehicleType = vehicleTypeBuilder.build();
+
+ /*
+ * define two depots, i.e. two vehicle locations ([10,10],[50,50]) and equip them with an infinite number of vehicles of type 'vehicleType'
+ */
+ Builder vehicleBuilder1 = VehicleImpl.Builder.newInstance("v1");
+ vehicleBuilder1.setLocationCoord(Coordinate.newInstance(10, 10));
+ vehicleBuilder1.setType(vehicleType);
+ Vehicle vehicle1 = vehicleBuilder1.build();
+
+ Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("v2");
+ vehicleBuilder2.setLocationCoord(Coordinate.newInstance(30, 30));
+ vehicleBuilder2.setType(vehicleType);
+ Vehicle vehicle2 = vehicleBuilder2.build();
+
+ Builder vehicleBuilder3 = VehicleImpl.Builder.newInstance("v3");
+ vehicleBuilder3.setLocationCoord(Coordinate.newInstance(10, 30));
+ vehicleBuilder3.setType(vehicleType);
+ Vehicle vehicle3 = vehicleBuilder3.build();
+
+ Builder vehicleBuilder4 = VehicleImpl.Builder.newInstance("v4");
+ vehicleBuilder4.setLocationCoord(Coordinate.newInstance(30, 10));
+ vehicleBuilder4.setType(vehicleType);
+ Vehicle vehicle4 = vehicleBuilder4.build();
+
+ /*
+ * build shipments at the required locations, each with a capacity-demand of 1.
+ * 4 shipments
+ * 1: (5,7)->(6,9)
+ * 2: (5,13)->(6,11)
+ * 3: (15,7)->(14,9)
+ * 4: (15,13)->(14,11)
+ */
+
+ Shipment shipment1 = Shipment.Builder.newInstance("1", 1).setPickupCoord(Coordinate.newInstance(5, 7)).setDeliveryCoord(Coordinate.newInstance(6, 9)).build();
+ Shipment shipment2 = Shipment.Builder.newInstance("2", 1).setPickupCoord(Coordinate.newInstance(5, 13)).setDeliveryCoord(Coordinate.newInstance(6, 11)).build();
+
+ Shipment shipment3 = Shipment.Builder.newInstance("3", 1).setPickupCoord(Coordinate.newInstance(15, 7)).setDeliveryCoord(Coordinate.newInstance(14, 9)).build();
+ Shipment shipment4 = Shipment.Builder.newInstance("4", 1).setPickupCoord(Coordinate.newInstance(15, 13)).setDeliveryCoord(Coordinate.newInstance(14, 11)).build();
+
+ Shipment shipment5 = Shipment.Builder.newInstance("5", 1).setPickupCoord(Coordinate.newInstance(25, 27)).setDeliveryCoord(Coordinate.newInstance(26, 29)).build();
+ Shipment shipment6 = Shipment.Builder.newInstance("6", 1).setPickupCoord(Coordinate.newInstance(25, 33)).setDeliveryCoord(Coordinate.newInstance(26, 31)).build();
+
+ Shipment shipment7 = Shipment.Builder.newInstance("7", 1).setPickupCoord(Coordinate.newInstance(35, 27)).setDeliveryCoord(Coordinate.newInstance(34, 29)).build();
+ Shipment shipment8 = Shipment.Builder.newInstance("8", 1).setPickupCoord(Coordinate.newInstance(35, 33)).setDeliveryCoord(Coordinate.newInstance(34, 31)).build();
+
+ Shipment shipment9 = Shipment.Builder.newInstance("9", 1).setPickupCoord(Coordinate.newInstance(5, 27)).setDeliveryCoord(Coordinate.newInstance(6, 29)).build();
+ Shipment shipment10 = Shipment.Builder.newInstance("10", 1).setPickupCoord(Coordinate.newInstance(5, 33)).setDeliveryCoord(Coordinate.newInstance(6, 31)).build();
+
+ Shipment shipment11 = Shipment.Builder.newInstance("11", 1).setPickupCoord(Coordinate.newInstance(15, 27)).setDeliveryCoord(Coordinate.newInstance(14, 29)).build();
+ Shipment shipment12 = Shipment.Builder.newInstance("12", 1).setPickupCoord(Coordinate.newInstance(15, 33)).setDeliveryCoord(Coordinate.newInstance(14, 31)).build();
+
+ Shipment shipment13 = Shipment.Builder.newInstance("13", 1).setPickupCoord(Coordinate.newInstance(25, 7)).setDeliveryCoord(Coordinate.newInstance(26, 9)).build();
+ Shipment shipment14 = Shipment.Builder.newInstance("14", 1).setPickupCoord(Coordinate.newInstance(25, 13)).setDeliveryCoord(Coordinate.newInstance(26, 11)).build();
+
+ Shipment shipment15 = Shipment.Builder.newInstance("15", 1).setPickupCoord(Coordinate.newInstance(35, 7)).setDeliveryCoord(Coordinate.newInstance(34, 9)).build();
+ Shipment shipment16 = Shipment.Builder.newInstance("16", 1).setPickupCoord(Coordinate.newInstance(35, 13)).setDeliveryCoord(Coordinate.newInstance(34, 11)).build();
+
+ Shipment shipment17 = Shipment.Builder.newInstance("17", 1).setPickupCoord(Coordinate.newInstance(5, 14)).setDeliveryCoord(Coordinate.newInstance(6, 16)).build();
+ Shipment shipment18 = Shipment.Builder.newInstance("18", 1).setPickupCoord(Coordinate.newInstance(5, 20)).setDeliveryCoord(Coordinate.newInstance(6, 18)).build();
+
+ Shipment shipment19 = Shipment.Builder.newInstance("19", 1).setPickupCoord(Coordinate.newInstance(15, 14)).setDeliveryCoord(Coordinate.newInstance(14, 16)).build();
+ Shipment shipment20 = Shipment.Builder.newInstance("20", 1).setPickupCoord(Coordinate.newInstance(15, 20)).setDeliveryCoord(Coordinate.newInstance(14, 18)).build();
+
+ VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
+ vrpBuilder.addVehicle(vehicle1).addVehicle(vehicle2);
+// vrpBuilder.addVehicle(vehicle1).addVehicle(vehicle2).addVehicle(vehicle3).addVehicle(vehicle4);
+ vrpBuilder.addJob(shipment1).addJob(shipment2).addJob(shipment3).addJob(shipment4);
+ vrpBuilder.addJob(shipment5).addJob(shipment6).addJob(shipment7).addJob(shipment8);
+ vrpBuilder.addJob(shipment9).addJob(shipment10).addJob(shipment11).addJob(shipment12);
+ vrpBuilder.addJob(shipment13).addJob(shipment14).addJob(shipment15).addJob(shipment16);
+ vrpBuilder.addJob(shipment17).addJob(shipment18).addJob(shipment19).addJob(shipment20);
+
+ vrpBuilder.setFleetSize(FleetSize.FINITE);
+
+ //vehicle1 cannot go to x>15 and vehicle2 cannot go to x<15
+ /*
+ * switch off the geoConstraints to show the effects without
+ */
+ HardRouteStateLevelConstraint geoClusterConstraint = new HardRouteStateLevelConstraint() {
+
+ @Override
+ public boolean fulfilled(JobInsertionContext insertionContext) {
+ Shipment shipment2insert = ((Shipment)insertionContext.getJob());
+ if(insertionContext.getNewVehicle().getId().equals("v2")){
+ if(shipment2insert.getPickupCoord().getX() > 15. || shipment2insert.getDeliveryCoord().getX() > 15.){
+ return false;
+ }
+ }
+ if(insertionContext.getNewVehicle().getId().equals("v1")){
+ if(shipment2insert.getPickupCoord().getX() < 15. || shipment2insert.getDeliveryCoord().getX() < 15.){
+ return false;
+ }
+ }
+ return true;
+ }
+ };
+ vrpBuilder.addConstraint(geoClusterConstraint);
+
+ VehicleRoutingProblem problem = vrpBuilder.build();
+
+// StateManager stateManager = new StateManager(problem);
+
+
+ /*
+ * get the algorithm out-of-the-box.
+ */
+ VehicleRoutingAlgorithm algorithm = VehicleRoutingAlgorithms.readAndCreateAlgorithm(problem, "input/algorithmConfig_noVehicleSwitch.xml");
+// algorithm.setNuOfIterations(30000);
+ /*
+ * and search a solution
+ */
+ Collection solutions = algorithm.searchSolutions();
+
+ /*
+ * get the best
+ */
+ VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);
+
+ /*
+ * write out problem and solution to xml-file
+ */
+// new VrpXMLWriter(problem, solutions).write("output/shipment-problem-with-solution.xml");
+
+ /*
+ * print nRoutes and totalCosts of bestSolution
+ */
+ SolutionPrinter.print(bestSolution);
+
+ /*
+ * plot problem without solution
+ */
+// Plotter problemPlotter = new Plotter(problem);
+// problemPlotter.plotShipments(true);
+// problemPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_problem.png", "en-route pickup and delivery");
+//
+// /*
+// * plot problem with solution
+// */
+// Plotter solutionPlotter = new Plotter(problem,Arrays.asList(Solutions.bestOf(solutions).getRoutes().iterator().next()));
+// solutionPlotter.plotShipments(true);
+// solutionPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_solution.png", "en-route pickup and delivery");
+
+ new GraphStreamViewer(problem,Solutions.bestOf(solutions)).labelWith(Label.ACTIVITY).setRenderDelay(100).setRenderShipments(true).display();
+
+ }
+
+}
+