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

merged master into multiple tw branch

This commit is contained in:
oblonski 2015-12-11 17:40:41 +01:00
parent 0b3b07a7de
commit 02c3c96e9c
523 changed files with 77426 additions and 74576 deletions

13
.editorconfig Normal file
View file

@ -0,0 +1,13 @@
root = true
[*]
indent_style = space
indent_size = 4
insert_final_newline = true
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
max_line_length = 120
[.travis.yml]
indent_size = 2

View file

@ -1,6 +1,18 @@
Change-log
==========
**v1.6.1** @ 2015-08-10
- feature [#165](https://github.com/jsprit/jsprit/issues/156)
- feature [#169](https://github.com/jsprit/jsprit/issues/159)
- bugfix [#154](https://github.com/jsprit/jsprit/issues/154)
- bugfix [#155](https://github.com/jsprit/jsprit/issues/155)
- bugfix [#158](https://github.com/jsprit/jsprit/issues/158)
- bugfix [#164](https://github.com/jsprit/jsprit/issues/164)
- bugfix [#165](https://github.com/jsprit/jsprit/issues/165)
- [detailed changelog ](https://rawgit.com/jsprit/misc-rep/master/changelog_1.6_to_1.6.1.html)
**v1.6** @ 2015-03-12
<b>! Break change !</b>

View file

@ -40,7 +40,7 @@ This software is released under [LGPL](http://opensource.org/licenses/LGPL-3.0).
##Contact
####Mailing List:
In the [mailing list](https://groups.google.com/group/jsprit-mailing-list) you can discuss jsprit related issues and you will probably get answers to your questions.
In the [mailing list](https://discuss.graphhopper.com/) ([old mailing list](https://groups.google.com/group/jsprit-mailing-list)) you can discuss jsprit related issues and you will probably get answers to your questions.
####Stackoverflow:
You can also use [stackoverflow](http://stackoverflow.com/questions/tagged/jsprit) to discuss your issues. Tag it with <em>jsprit</em> then it is easier to keep track of your topic.

View file

@ -1,6 +1,11 @@
WHATS NEW
==========
------------------------------
<b>2015-08-10</b> new release **v1.6.1**
Jsprit results are now reproducible since every time the algorithm runs, a unique random number generator (always starting with a
predefined seed) is invoked. If one does not want this behaviour, one can always specify custom random number generators.
<b>2015-03-12</b> new release **v1.6**
When reviewing the feedback from our users, we realized that jsprit cannot solve certain kinds of problems adequately.

View file

@ -1,8 +1,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>jsprit</groupId>
<artifactId>jsprit</artifactId>
<version>1.6.1-SNAPSHOT</version>
<version>1.6.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jsprit-analysis</artifactId>
@ -28,7 +29,7 @@
</goals>
</pluginExecutionFilter>
<action>
<ignore />
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
@ -43,7 +44,7 @@
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.14</version>
<version>1.0.19</version>
<scope>compile</scope>
</dependency>

View file

@ -89,11 +89,10 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
File dgsFile = new File(dgsFileLocation);
fos = new FileOutputStream(dgsFile);
fileSink = new FileSinkDGS();
if(dgsFile.getName().endsWith("gz")){
if (dgsFile.getName().endsWith("gz")) {
gzipOs = new GZIPOutputStream(fos);
fileSink.begin(gzipOs);
}
else{
} else {
fileSink.begin(fos);
}
graph.addSink(fileSink);
@ -109,37 +108,37 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
new AlgorithmEventsRecorder(vrp, dgsFileLocation);
}
public void setRecordingRange(int startIteration, int endIteration){
public void setRecordingRange(int startIteration, int endIteration) {
this.start_recording_at = startIteration;
this.end_recording_at = endIteration;
}
@Override
public void ruinStarts(Collection<VehicleRoute> routes) {
if(!record()) return;
fileSink.stepBegins(graph.getId(),0,BEFORE_RUIN_RENDER_SOLUTION);
if (!record()) return;
fileSink.stepBegins(graph.getId(), 0, BEFORE_RUIN_RENDER_SOLUTION);
markAllNodesAsInserted();
addRoutes(routes);
fileSink.stepBegins(graph.getId(),0,RUIN);
fileSink.stepBegins(graph.getId(), 0, RUIN);
}
private void markAllNodesAsInserted() {
for(Job j : vrp.getJobs().values()){
for (Job j : vrp.getJobs().values()) {
markInserted(j);
}
}
private void addRoutes(Collection<VehicleRoute> routes) {
for(VehicleRoute route : routes){
for (VehicleRoute route : routes) {
String prevNode = makeStartId(route.getVehicle());
for(TourActivity act : route.getActivities()){
for (TourActivity act : route.getActivities()) {
String actNodeId = getNodeId(act);
addEdge(prevNode+"_"+actNodeId,prevNode,actNodeId);
addEdge(prevNode + "_" + actNodeId, prevNode, actNodeId);
prevNode = actNodeId;
}
if(route.getVehicle().isReturnToDepot()) {
if (route.getVehicle().isReturnToDepot()) {
String lastNode = makeEndId(route.getVehicle());
addEdge(prevNode+"_"+lastNode,prevNode,lastNode);
addEdge(prevNode + "_" + lastNode, prevNode, lastNode);
}
}
@ -147,13 +146,12 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
private String getNodeId(TourActivity act) {
String nodeId = null;
if(act instanceof TourActivity.JobActivity){
if (act instanceof TourActivity.JobActivity) {
Job job = ((TourActivity.JobActivity) act).getJob();
if(job instanceof Service){
if (job instanceof Service) {
nodeId = job.getId();
}
else if(job instanceof Shipment){
if(act.getName().equals("pickupShipment")) nodeId = getFromNodeId((Shipment) job);
} else if (job instanceof Shipment) {
if (act.getName().equals("pickupShipment")) nodeId = getFromNodeId((Shipment) job);
else nodeId = getToNodeId((Shipment) job);
}
}
@ -171,28 +169,29 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
@Override
public void removed(Job job, VehicleRoute fromRoute) {
if(!record()) return;
if(job instanceof Service) removeService(job, fromRoute);
else if(job instanceof Shipment) removeShipment(job,fromRoute);
if (!record()) return;
if (job instanceof Service) removeService(job, fromRoute);
else if (job instanceof Shipment) removeShipment(job, fromRoute);
}
private void removeShipment(Job job, VehicleRoute fromRoute) {
Shipment shipment = (Shipment)job;
Shipment shipment = (Shipment) job;
String fromNodeId = getFromNodeId(shipment);
String toNodeId = getToNodeId(shipment);
// removeNodeAndBelongingEdges(fromNodeId,fromRoute);
// removeNodeAndBelongingEdges(toNodeId,fromRoute);
Edge enteringToNode = getEnteringEdge(toNodeId);
if(enteringToNode.getNode0().getId().equals(fromNodeId)){
if (enteringToNode.getNode0().getId().equals(fromNodeId)) {
markRemoved(graph.getNode(fromNodeId));
markRemoved(graph.getNode(toNodeId));
// i -> from -> to -> j: rem(i,from), rem(from,to), rem(to,j), add(i,j)
Edge enteringFromNode = getEnteringEdge(fromNodeId);
removeEdge(enteringFromNode.getId());
removeEdge(enteringToNode.getId());
if(graph.getNode(toNodeId).getLeavingEdgeSet().isEmpty()){
if(fromRoute.getVehicle().isReturnToDepot()) throw new IllegalStateException("leaving edge is missing");
if (graph.getNode(toNodeId).getLeavingEdgeSet().isEmpty()) {
if (fromRoute.getVehicle().isReturnToDepot())
throw new IllegalStateException("leaving edge is missing");
return;
}
@ -200,22 +199,23 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
removeEdge(leavingToNode.getId());
Node from = enteringFromNode.getNode0();
Node to = leavingToNode.getNode1();
if(!fromRoute.getActivities().isEmpty()){
addEdge(makeEdgeId(from,to),from.getId(),to.getId());
if (!fromRoute.getActivities().isEmpty()) {
addEdge(makeEdgeId(from, to), from.getId(), to.getId());
}
}
else{
removeNodeAndBelongingEdges(fromNodeId,fromRoute);
removeNodeAndBelongingEdges(toNodeId,fromRoute);
} else {
removeNodeAndBelongingEdges(fromNodeId, fromRoute);
removeNodeAndBelongingEdges(toNodeId, fromRoute);
}
}
private Edge getLeavingEdge(String toNodeId) {
Collection<Edge> edges = graph.getNode(toNodeId).getLeavingEdgeSet();
if(edges.size()==1) return edges.iterator().next();
else{
for(Edge e : edges){
if(e.getId().startsWith("shipment")){ continue; }
if (edges.size() == 1) return edges.iterator().next();
else {
for (Edge e : edges) {
if (e.getId().startsWith("shipment")) {
continue;
}
return e;
}
}
@ -224,10 +224,12 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
private Edge getEnteringEdge(String toNodeId) {
Collection<Edge> enteringEdges = graph.getNode(toNodeId).getEnteringEdgeSet();
if(enteringEdges.size()==1) return enteringEdges.iterator().next();
else{
for(Edge e : enteringEdges){
if(e.getId().startsWith("shipment")){ continue; }
if (enteringEdges.size() == 1) return enteringEdges.iterator().next();
else {
for (Edge e : enteringEdges) {
if (e.getId().startsWith("shipment")) {
continue;
}
return e;
}
}
@ -253,8 +255,8 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
Edge entering = getEnteringEdge(nodeId);
removeEdge(entering.getId());
if(node.getLeavingEdgeSet().isEmpty()){
if(fromRoute.getVehicle().isReturnToDepot()) throw new IllegalStateException("leaving edge is missing");
if (node.getLeavingEdgeSet().isEmpty()) {
if (fromRoute.getVehicle().isReturnToDepot()) throw new IllegalStateException("leaving edge is missing");
return;
}
@ -262,13 +264,13 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
removeEdge((leaving.getId()));
Node from = entering.getNode0();
Node to = leaving.getNode1();
if(!fromRoute.getActivities().isEmpty()){
addEdge(makeEdgeId(from,to),from.getId(),to.getId());
if (!fromRoute.getActivities().isEmpty()) {
addEdge(makeEdgeId(from, to), from.getId(), to.getId());
}
}
private void markRemoved(Node node) {
node.setAttribute("ui.class","removed");
node.setAttribute("ui.class", "removed");
}
private String makeEdgeId(Node from, Node to) {
@ -278,7 +280,7 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
@Override
public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);
fileSink.stepBegins(graph.getId(),0,BEFORE_RUIN_RENDER_SOLUTION);
fileSink.stepBegins(graph.getId(), 0, BEFORE_RUIN_RENDER_SOLUTION);
addRoutes(solution.getRoutes());
finish();
}
@ -287,7 +289,7 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
try {
fileSink.end();
fos.close();
if(gzipOs != null) gzipOs.close();
if (gzipOs != null) gzipOs.close();
} catch (IOException e) {
e.printStackTrace();
}
@ -299,28 +301,27 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
}
private void initialiseGraph(VehicleRoutingProblem problem) {
for(Vehicle vehicle : problem.getVehicles()){
for (Vehicle vehicle : problem.getVehicles()) {
addVehicle(vehicle);
}
for(Job job : problem.getJobs().values()){
for (Job job : problem.getJobs().values()) {
addJob(job);
}
}
private void addJob(Job job) {
if(job instanceof Service){
Service service = (Service)job;
if (job instanceof Service) {
Service service = (Service) job;
addNode(service.getId(), service.getLocation().getCoordinate());
markService(service);
}
else if(job instanceof Shipment){
Shipment shipment = (Shipment)job;
} else if (job instanceof Shipment) {
Shipment shipment = (Shipment) job;
String fromNodeId = getFromNodeId(shipment);
addNode(fromNodeId, shipment.getPickupLocation().getCoordinate());
String toNodeId = getToNodeId(shipment);
addNode(toNodeId,shipment.getDeliveryLocation().getCoordinate());
addNode(toNodeId, shipment.getDeliveryLocation().getCoordinate());
markShipment(shipment);
if(renderShipments) {
if (renderShipments) {
Edge e = graph.addEdge("shipment_" + fromNodeId + "_" + toNodeId, fromNodeId, toNodeId, true);
e.addAttribute("ui.class", "shipment");
}
@ -333,35 +334,34 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
}
private void markService(Service service) {
if(service instanceof Delivery){
if (service instanceof Delivery) {
markDelivery(service.getId());
}
else {
} else {
markPickup(service.getId());
}
}
private void markPickup(String id) {
graph.getNode(id).addAttribute("ui.class","pickup");
graph.getNode(id).addAttribute("ui.class", "pickup");
}
private void markDelivery(String id) {
graph.getNode(id).addAttribute("ui.class","delivery");
graph.getNode(id).addAttribute("ui.class", "delivery");
}
private void addVehicle(Vehicle vehicle) {
String startId = makeStartId(vehicle);
Node node = graph.addNode(startId);
node.addAttribute("x",vehicle.getStartLocation().getCoordinate().getX());
node.addAttribute("x", vehicle.getStartLocation().getCoordinate().getX());
node.addAttribute("y", vehicle.getStartLocation().getCoordinate().getY());
node.addAttribute("ui.class","depot");
node.addAttribute("ui.class", "depot");
String endId = makeEndId(vehicle);
if(!startId.equals(endId)){
if (!startId.equals(endId)) {
Node endNode = graph.addNode(endId);
endNode.addAttribute("x", vehicle.getEndLocation().getCoordinate().getX());
endNode.addAttribute("y", vehicle.getEndLocation().getCoordinate().getY());
endNode.addAttribute("ui.class","depot");
endNode.addAttribute("ui.class", "depot");
}
}
@ -370,7 +370,7 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
}
private String makeEndId(Vehicle vehicle) {
if(vehicle.getStartLocation().getId().equals(vehicle.getEndLocation().getId())) return makeStartId(vehicle);
if (vehicle.getStartLocation().getId().equals(vehicle.getEndLocation().getId())) return makeStartId(vehicle);
return vehicle.getId() + "_end";
}
@ -382,20 +382,20 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
@Override
public void informInsertionEnds(Collection<VehicleRoute> vehicleRoutes) {
if(!record()) return;
fileSink.stepBegins(graph.getId(),0,CLEAR_SOLUTION);
if (!record()) return;
fileSink.stepBegins(graph.getId(), 0, CLEAR_SOLUTION);
removeRoutes(vehicleRoutes);
}
private void removeRoutes(Collection<VehicleRoute> vehicleRoutes) {
for(VehicleRoute route : vehicleRoutes){
for (VehicleRoute route : vehicleRoutes) {
String prevNode = makeStartId(route.getVehicle());
for(TourActivity act : route.getActivities()){
for (TourActivity act : route.getActivities()) {
String actNode = getNodeId(act);
removeEdge(prevNode + "_" + actNode);
prevNode = actNode;
}
if(route.getVehicle().isReturnToDepot()) {
if (route.getVehicle().isReturnToDepot()) {
String lastNode = makeEndId(route.getVehicle());
removeEdge(prevNode + "_" + lastNode);
}
@ -404,25 +404,25 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
@Override
public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route) {
if(!record()) return;
if (!record()) return;
markInserted(job);
handleVehicleSwitch(data, route);
insertJob(job, data, route);
}
private void insertJob(Job job, InsertionData data, VehicleRoute route) {
if(job instanceof Service) insertService(job,data,route);
else if(job instanceof Shipment) insertShipment(job,data,route);
if (job instanceof Service) insertService(job, data, route);
else if (job instanceof Shipment) insertShipment(job, data, route);
}
private void insertShipment(Job job, InsertionData data, VehicleRoute route) {
String fromNodeId = getFromNodeId((Shipment) job);
String toNodeId = getToNodeId((Shipment) job);
insertNode(toNodeId,data.getDeliveryInsertionIndex(),data,route);
insertNode(toNodeId, data.getDeliveryInsertionIndex(), data, route);
List<AbstractActivity> del = vrp.getActivities(job);
VehicleRoute copied = VehicleRoute.copyOf(route);
copied.getTourActivities().addActivity(data.getDeliveryInsertionIndex(),del.get(1));
copied.getTourActivities().addActivity(data.getDeliveryInsertionIndex(), del.get(1));
insertNode(fromNodeId, data.getPickupInsertionIndex(), data, copied);
}
@ -436,19 +436,17 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
String node_i;
if(isFirst(insertionIndex)) {
if (isFirst(insertionIndex)) {
node_i = makeStartId(data.getSelectedVehicle());
}
else {
} else {
TourActivity.JobActivity jobActivity = (TourActivity.JobActivity) route.getActivities().get(insertionIndex - 1);
node_i = getNodeId(jobActivity);
}
String edgeId_1 = node_i + "_" + nodeId;
String node_j;
if(isLast(insertionIndex,route)) {
if (isLast(insertionIndex, route)) {
node_j = makeEndId(data.getSelectedVehicle());
}
else {
} else {
TourActivity.JobActivity jobActivity = (TourActivity.JobActivity) route.getActivities().get(insertionIndex);
node_j = getNodeId(jobActivity);
}
@ -456,7 +454,7 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
addEdge(edgeId_1, node_i, nodeId);
if(!(isLast(insertionIndex,route) && !data.getSelectedVehicle().isReturnToDepot())) {
if (!(isLast(insertionIndex, route) && !data.getSelectedVehicle().isReturnToDepot())) {
addEdge(edgeId_2, nodeId, node_j);
if (!route.getActivities().isEmpty()) {
removeEdge(node_i + "_" + node_j);
@ -466,38 +464,37 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
private void handleVehicleSwitch(InsertionData data, VehicleRoute route) {
boolean vehicleSwitch = false;
if(!(route.getVehicle() instanceof VehicleImpl.NoVehicle)) {
if (!(route.getVehicle() instanceof VehicleImpl.NoVehicle)) {
if (!route.getVehicle().getId().equals(data.getSelectedVehicle().getId())) {
vehicleSwitch = true;
}
}
if(vehicleSwitch && !route.getActivities().isEmpty()){
if (vehicleSwitch && !route.getActivities().isEmpty()) {
String oldStart = makeStartId(route.getVehicle());
String firstAct = ((TourActivity.JobActivity)route.getActivities().get(0)).getJob().getId();
String firstAct = ((TourActivity.JobActivity) route.getActivities().get(0)).getJob().getId();
String oldEnd = makeEndId(route.getVehicle());
String lastAct = ((TourActivity.JobActivity)route.getActivities().get(route.getActivities().size()-1)).getJob().getId();
String lastAct = ((TourActivity.JobActivity) route.getActivities().get(route.getActivities().size() - 1)).getJob().getId();
removeEdge(oldStart + "_" + firstAct);
if(route.getVehicle().isReturnToDepot()) {
if (route.getVehicle().isReturnToDepot()) {
removeEdge(lastAct + "_" + oldEnd);
}
String newStart = makeStartId(data.getSelectedVehicle());
String newEnd = makeEndId(data.getSelectedVehicle());
addEdge(newStart + "_" + firstAct,newStart,firstAct);
addEdge(newStart + "_" + firstAct, newStart, firstAct);
if(data.getSelectedVehicle().isReturnToDepot()) {
if (data.getSelectedVehicle().isReturnToDepot()) {
addEdge(lastAct + "_" + newEnd, lastAct, newEnd);
}
}
}
private void markInserted(Job job) {
if(job instanceof Service){
if (job instanceof Service) {
markService((Service) job);
}
else{
markShipment((Shipment)job);
} else {
markShipment((Shipment) job);
}
}
@ -507,7 +504,7 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
}
private void markEdgeRemoved(String edgeId) {
graph.getEdge(edgeId).addAttribute("ui.class","removed");
graph.getEdge(edgeId).addAttribute("ui.class", "removed");
}
private boolean isFirst(int index) {
@ -519,18 +516,18 @@ public class AlgorithmEventsRecorder implements RuinListener, IterationStartsLis
}
private void addEdge(String edgeId, String fromNode, String toNode) {
graph.addEdge(edgeId,fromNode,toNode,true);
graph.addEdge(edgeId, fromNode, toNode, true);
markEdgeInserted(edgeId);
}
private void markEdgeInserted(String edgeId) {
graph.getEdge(edgeId).addAttribute("ui.class","inserted");
graph.getEdge(edgeId).addAttribute("ui.class", "inserted");
graph.getEdge(edgeId).removeAttribute("ui.class");
}
@Override
public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) {
if(!record()) return;
fileSink.stepBegins(graph.getId(),0,RECREATE);
if (!record()) return;
fileSink.stepBegins(graph.getId(), 0, RECREATE);
}
}

View file

@ -47,7 +47,7 @@ public class AlgorithmEventsViewer {
this.delayContainer = delayContainer;
}
public void setRuinDelay(long ruinDelay){
public void setRuinDelay(long ruinDelay) {
this.ruinDelay = ruinDelay;
}
@ -131,16 +131,14 @@ public class AlgorithmEventsViewer {
@Override
public void stepBegins(String sourceId, long timeId, double step) {
if(step == AlgorithmEventsRecorder.RECREATE) {
if (step == AlgorithmEventsRecorder.RECREATE) {
delayContainer.delay = recreateDelay;
}
if(step == AlgorithmEventsRecorder.RUIN){
if (step == AlgorithmEventsRecorder.RUIN) {
delayContainer.delay = ruinDelay;
}
else if(step == AlgorithmEventsRecorder.CLEAR_SOLUTION){
} else if (step == AlgorithmEventsRecorder.CLEAR_SOLUTION) {
delayContainer.delay = delay;
}
else if(step == AlgorithmEventsRecorder.BEFORE_RUIN_RENDER_SOLUTION){
} else if (step == AlgorithmEventsRecorder.BEFORE_RUIN_RENDER_SOLUTION) {
delayContainer.delay = delay;
}
}
@ -156,15 +154,15 @@ public class AlgorithmEventsViewer {
private long delay = 2;
public void setRecreationDelay(long delay_in_ms){
public void setRecreationDelay(long delay_in_ms) {
this.delayRecreation = delay_in_ms;
}
public void setRuinDelay(long delay_in_ms){
public void setRuinDelay(long delay_in_ms) {
this.delayRuin = delay_in_ms;
}
public void display(String dgsFile){
public void display(String dgsFile) {
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph graph = GraphStreamViewer.createMultiGraph("g", GraphStreamViewer.StyleSheets.BLUE_FOREST);
Viewer viewer = graph.display();
@ -185,13 +183,13 @@ public class AlgorithmEventsViewer {
while (fs.nextEvents()) {
sleep(delayContainer.delay);
}
} catch( IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
try {
fs.end();
} catch( IOException e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
fs.removeSink(graph);

View file

@ -30,11 +30,10 @@ import java.util.Collection;
/**
* VehicleRoutingAlgorithm-Listener to record the solution-search-progress.
*
* <p/>
* <p>Register this listener in VehicleRoutingAlgorithm.
*
* @author stefan schroeder
*
*/
public class AlgorithmSearchProgressChartListener implements IterationEndsListener, AlgorithmEndsListener, AlgorithmStartsListener {
@ -53,14 +52,14 @@ public class AlgorithmSearchProgressChartListener implements IterationEndsListen
public AlgorithmSearchProgressChartListener(String pngFileName) {
super();
this.filename = pngFileName;
if(!this.filename.endsWith("png")){
if (!this.filename.endsWith("png")) {
this.filename += ".png";
}
}
@Override
public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
log.info("create chart " + filename);
log.info("create chart {}", filename);
XYLineChartBuilder.saveChartAsPNG(chartBuilder.build(), filename);
}
@ -69,19 +68,19 @@ public class AlgorithmSearchProgressChartListener implements IterationEndsListen
double worst = 0.0;
double best = Double.MAX_VALUE;
double sum = 0.0;
for(VehicleRoutingProblemSolution sol : solutions){
if(sol.getCost() > worst) worst = Math.min(sol.getCost(),Double.MAX_VALUE);
if(sol.getCost() < best) best = sol.getCost();
sum += Math.min(sol.getCost(),Double.MAX_VALUE);
for (VehicleRoutingProblemSolution sol : solutions) {
if (sol.getCost() > worst) worst = Math.min(sol.getCost(), Double.MAX_VALUE);
if (sol.getCost() < best) best = sol.getCost();
sum += Math.min(sol.getCost(), Double.MAX_VALUE);
}
chartBuilder.addData("best", i, best);
chartBuilder.addData("worst", i, worst);
chartBuilder.addData("avg", i, sum/(double)solutions.size());
chartBuilder.addData("avg", i, sum / (double) solutions.size());
}
@Override
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm,Collection<VehicleRoutingProblemSolution> solutions) {
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
chartBuilder = XYLineChartBuilder.newInstance("search-progress", "iterations", "results");
}

View file

@ -39,14 +39,13 @@ public class ComputationalLaboratory {
/**
* Listener-interface to listen to calculation.
*
* <p/>
* <p>Note that calculations are run concurrently, i.e. a unique task that is distributed to an available thread is
* {algorithm, instance, run}.
*
* @author schroeder
*
*/
public static interface CalculationListener extends LabListener{
public static interface CalculationListener extends LabListener {
public void calculationStarts(final BenchmarkInstance p, final String algorithmName, final VehicleRoutingAlgorithm algorithm, final int run);
@ -65,7 +64,6 @@ public class ComputationalLaboratory {
* Collects whatever indicators you require by algorithmName, instanceName, run and indicator.
*
* @author schroeder
*
*/
public static class DataCollector {
@ -75,13 +73,14 @@ public class ComputationalLaboratory {
private int run;
private String indicatorName;
public Key(String instanceName, String algorithmName, int run,String indicatorName) {
public Key(String instanceName, String algorithmName, int run, String indicatorName) {
super();
this.instanceName = instanceName;
this.algorithmName = algorithmName;
this.run = run;
this.indicatorName = indicatorName;
}
@Override
public int hashCode() {
final int prime = 31;
@ -100,6 +99,7 @@ public class ComputationalLaboratory {
result = prime * result + run;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
@ -128,22 +128,26 @@ public class ComputationalLaboratory {
return false;
return true;
}
public String getInstanceName() {
return instanceName;
}
public String getAlgorithmName() {
return algorithmName;
}
public int getRun() {
return run;
}
public String getIndicatorName() {
return indicatorName;
}
@Override
public String toString() {
return "[algorithm="+algorithmName+"][instance="+instanceName+"][run="+run+"][indicator="+indicatorName+"]";
return "[algorithm=" + algorithmName + "][instance=" + instanceName + "][run=" + run + "][indicator=" + indicatorName + "]";
}
}
@ -153,6 +157,7 @@ public class ComputationalLaboratory {
private ConcurrentHashMap<Key, Double> data = new ConcurrentHashMap<ComputationalLaboratory.DataCollector.Key, Double>();
private ConcurrentHashMap<Key, VehicleRoutingProblemSolution> solutions = new ConcurrentHashMap<ComputationalLaboratory.DataCollector.Key, VehicleRoutingProblemSolution>();
/**
* Adds a single date by instanceName, algorithmName, run and indicatorName.
* <p>If there is already an entry for this instance, algorithm, run and indicatorName, it is overwritten.
@ -163,14 +168,15 @@ public class ComputationalLaboratory {
* @param indicatorName
* @param value
*/
public void addDate(String instanceName, String algorithmName, int run, String indicatorName, double value){
if(indicatorName.equals(SOLUTION_INDICATOR_NAME)) throw new IllegalArgumentException(indicatorName + " is already used internally. please choose another indicator-name.");
Key key = new Key(instanceName,algorithmName,run,indicatorName);
public void addDate(String instanceName, String algorithmName, int run, String indicatorName, double value) {
if (indicatorName.equals(SOLUTION_INDICATOR_NAME))
throw new IllegalArgumentException(indicatorName + " is already used internally. please choose another indicator-name.");
Key key = new Key(instanceName, algorithmName, run, indicatorName);
data.put(key, value);
}
public void addSolution(String instanceName, String algorithmName, int run, VehicleRoutingProblemSolution solution){
Key key = new Key(instanceName,algorithmName,run,SOLUTION_INDICATOR_NAME);
public void addSolution(String instanceName, String algorithmName, int run, VehicleRoutingProblemSolution solution) {
Key key = new Key(instanceName, algorithmName, run, SOLUTION_INDICATOR_NAME);
solutions.put(key, solution);
}
@ -182,10 +188,10 @@ public class ComputationalLaboratory {
* @param indicator
* @return
*/
public Collection<Double> getData(String instanceName, String algorithmName, String indicator){
public Collection<Double> getData(String instanceName, String algorithmName, String indicator) {
List<Double> values = new ArrayList<Double>();
for(Key key : data.keySet()){
if(key.getAlgorithmName().equals(algorithmName) && key.getInstanceName().equals(instanceName) && key.getIndicatorName().equals(indicator)){
for (Key key : data.keySet()) {
if (key.getAlgorithmName().equals(algorithmName) && key.getInstanceName().equals(instanceName) && key.getIndicatorName().equals(indicator)) {
values.add(data.get(key));
}
}
@ -201,12 +207,12 @@ public class ComputationalLaboratory {
* @param indicator
* @return
*/
public Double getDate(String instanceName, String algorithmName, int run, String indicator){
return data.get(new Key(instanceName,algorithmName,run,indicator));
public Double getDate(String instanceName, String algorithmName, int run, String indicator) {
return data.get(new Key(instanceName, algorithmName, run, indicator));
}
public VehicleRoutingProblemSolution getSolution(String instanceName, String algorithmName, int run){
return solutions.get(new Key(instanceName,algorithmName,run,"solution"));
public VehicleRoutingProblemSolution getSolution(String instanceName, String algorithmName, int run) {
return solutions.get(new Key(instanceName, algorithmName, run, "solution"));
}
/**
@ -214,19 +220,19 @@ public class ComputationalLaboratory {
*
* @return
*/
public Set<Key> getDataKeySet(){
public Set<Key> getDataKeySet() {
return data.keySet();
}
public Set<Key> getSolutionKeySet(){
public Set<Key> getSolutionKeySet() {
return solutions.keySet();
}
public VehicleRoutingProblemSolution getSolution(Key solutionKey){
public VehicleRoutingProblemSolution getSolution(Key solutionKey) {
return solutions.get(solutionKey);
}
public Collection<VehicleRoutingProblemSolution> getSolutions(){
public Collection<VehicleRoutingProblemSolution> getSolutions() {
return solutions.values();
}
@ -236,7 +242,7 @@ public class ComputationalLaboratory {
* @param key
* @return
*/
public Double getData(Key key){
public Double getData(Key key) {
return data.get(key);
}
@ -284,9 +290,10 @@ public class ComputationalLaboratory {
* @param factory
* @throws IllegalStateException if there is already an algorithmFactory with the same name
*/
public void addAlgorithmFactory(String name, VehicleRoutingAlgorithmFactory factory){
if(algorithmNames.contains(name)) throw new IllegalStateException("there is already a algorithmFactory with the same name (algorithmName="+name+"). unique names are required.");
algorithms.add(new Algorithm(name,factory));
public void addAlgorithmFactory(String name, VehicleRoutingAlgorithmFactory factory) {
if (algorithmNames.contains(name))
throw new IllegalStateException("there is already a algorithmFactory with the same name (algorithmName=" + name + "). unique names are required.");
algorithms.add(new Algorithm(name, factory));
algorithmNames.add(name);
}
@ -294,7 +301,7 @@ public class ComputationalLaboratory {
return algorithmNames;
}
public Collection<String> getInstanceNames(){
public Collection<String> getInstanceNames() {
return instanceNames;
}
@ -305,9 +312,10 @@ public class ComputationalLaboratory {
* @param problem
* @throws IllegalStateException if there is already an instance with the same name.
*/
public void addInstance(String name, VehicleRoutingProblem problem){
if(benchmarkInstances.contains(name)) throw new IllegalStateException("there is already an instance with the same name (instanceName="+name+"). unique names are required.");
benchmarkInstances.add(new BenchmarkInstance(name,problem,null,null));
public void addInstance(String name, VehicleRoutingProblem problem) {
if (benchmarkInstances.contains(name))
throw new IllegalStateException("there is already an instance with the same name (instanceName=" + name + "). unique names are required.");
benchmarkInstances.add(new BenchmarkInstance(name, problem, null, null));
instanceNames.add(name);
}
@ -317,8 +325,9 @@ public class ComputationalLaboratory {
* @param instance the instance to be added
* @throws IllegalStateException if there is already an instance with the same name.
*/
public void addInstance(BenchmarkInstance instance){
if(benchmarkInstances.contains(instance.name)) throw new IllegalStateException("there is already an instance with the same name (instanceName="+instance.name+"). unique names are required.");
public void addInstance(BenchmarkInstance instance) {
if (benchmarkInstances.contains(instance.name))
throw new IllegalStateException("there is already an instance with the same name (instanceName=" + instance.name + "). unique names are required.");
benchmarkInstances.add(instance);
instanceNames.add(instance.name);
}
@ -329,8 +338,8 @@ public class ComputationalLaboratory {
* @param instances collection of instances to be added
* @throws IllegalStateException if there is already an instance with the same name.
*/
public void addAllInstances(Collection<BenchmarkInstance> instances){
for(BenchmarkInstance i : instances){
public void addAllInstances(Collection<BenchmarkInstance> instances) {
for (BenchmarkInstance i : instances) {
addInstance(i);
}
}
@ -342,8 +351,8 @@ public class ComputationalLaboratory {
* @param problem
* @throws IllegalStateException if there is already an instance with the same name.
*/
public void addInstance(String name, VehicleRoutingProblem problem, Double bestKnownResult, Double bestKnownVehicles){
addInstance(new BenchmarkInstance(name,problem,bestKnownResult,bestKnownVehicles));
public void addInstance(String name, VehicleRoutingProblem problem, Double bestKnownResult, Double bestKnownVehicles) {
addInstance(new BenchmarkInstance(name, problem, bestKnownResult, bestKnownVehicles));
}
/**
@ -351,11 +360,11 @@ public class ComputationalLaboratory {
*
* @param listener
*/
public void addListener(LabListener listener){
if(listener instanceof CalculationListener) {
public void addListener(LabListener listener) {
if (listener instanceof CalculationListener) {
listeners.add((CalculationListener) listener);
}
if(listener instanceof LabStartsAndEndsListener){
if (listener instanceof LabStartsAndEndsListener) {
startsAndEndslisteners.add((LabStartsAndEndsListener) listener);
}
}
@ -366,13 +375,13 @@ public class ComputationalLaboratory {
*
* @param runs
*/
public void setNuOfRuns(int runs){
public void setNuOfRuns(int runs) {
this.runs = runs;
}
/**
* Runs experiments.
*
* <p/>
* <p>If nuThreads > 1 it runs them concurrently, i.e. individual runs are distributed to available threads. Therefore
* a unique task is defined by its algorithmName, instanceName and its runNumber.
* <p>If you have one algorithm called "myAlgorithm" and one instance called "myInstance", and you need to run "myAlgorithm" on "myInstance" three times
@ -380,34 +389,36 @@ public class ComputationalLaboratory {
* <p>You can register whatever analysisTool you require by implementing and registering CalculationListener. Then your tool is informed just
* before a calculation starts as well as just after a calculation has been finished.
*
* @see CalculationListener
* @throws IllegalStateException if either no algorithm or no instance has been specified
* @see CalculationListener
*/
public void run(){
if(algorithms.isEmpty()){
public void run() {
if (algorithms.isEmpty()) {
throw new IllegalStateException("no algorithm specified. at least one algorithm needs to be specified.");
}
if(benchmarkInstances.isEmpty()){
if (benchmarkInstances.isEmpty()) {
throw new IllegalStateException("no instance specified. at least one instance needs to be specified.");
}
informStart();
System.out.println("start benchmarking [nuAlgorithms="+algorithms.size()+"][nuInstances=" + benchmarkInstances.size() + "][runsPerInstance=" + runs + "]");
System.out.println("start benchmarking [nuAlgorithms=" + algorithms.size() + "][nuInstances=" + benchmarkInstances.size() + "][runsPerInstance=" + runs + "]");
double startTime = System.currentTimeMillis();
ExecutorService executor = Executors.newFixedThreadPool(threads);
for(final Algorithm algorithm : algorithms){
for(final BenchmarkInstance p : benchmarkInstances){
for(int run=0;run<runs;run++){
for (final Algorithm algorithm : algorithms) {
for (final BenchmarkInstance p : benchmarkInstances) {
for (int run = 0; run < runs; run++) {
final int r = run;
// runAlgorithm(p, algorithm, r+1);
executor.submit(new Runnable(){
try {
executor.submit(new Runnable() {
@Override
public void run() {
runAlgorithm(p, algorithm, r+1);
runAlgorithm(p, algorithm, r + 1);
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
@ -416,21 +427,21 @@ public class ComputationalLaboratory {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
System.out.println("benchmarking done [time="+(System.currentTimeMillis()-startTime)/1000 + "sec]");
System.out.println("benchmarking done [time=" + (System.currentTimeMillis() - startTime) / 1000 + "sec]");
informEnd();
}
private void informEnd() {
for(LabStartsAndEndsListener l : startsAndEndslisteners){
for (LabStartsAndEndsListener l : startsAndEndslisteners) {
l.labEnds();
}
}
private void informStart() {
for(LabStartsAndEndsListener l : startsAndEndslisteners){
l.labStarts(benchmarkInstances, algorithms.size(),runs);
for (LabStartsAndEndsListener l : startsAndEndslisteners) {
l.labStarts(benchmarkInstances, algorithms.size(), runs);
}
}
@ -445,21 +456,21 @@ public class ComputationalLaboratory {
}
private void runAlgorithm(BenchmarkInstance p, Algorithm algorithm, int run) {
System.out.println("[algorithm=" + algorithm.name + "][instance="+p.name+"][run="+run+"][status=start]");
System.out.println("[algorithm=" + algorithm.name + "][instance=" + p.name + "][run=" + run + "][status=start]");
VehicleRoutingAlgorithm vra = algorithm.factory.createAlgorithm(p.vrp);
informCalculationStarts(p, algorithm.name, vra, run);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
System.out.println("[algorithm=" + algorithm.name + "][instance="+p.name+"][run="+run+"][status=finished]");
System.out.println("[algorithm=" + algorithm.name + "][instance=" + p.name + "][run=" + run + "][status=finished]");
informCalculationsEnds(p, algorithm.name, vra, run, solutions);
}
private void informCalculationStarts(BenchmarkInstance p, String name, VehicleRoutingAlgorithm vra, int run) {
for(CalculationListener l : listeners) l.calculationStarts(p, name, vra, run);
for (CalculationListener l : listeners) l.calculationStarts(p, name, vra, run);
}
private void informCalculationsEnds(BenchmarkInstance p, String name, VehicleRoutingAlgorithm vra, int run,
Collection<VehicleRoutingProblemSolution> solutions) {
for(CalculationListener l : listeners) l.calculationEnds(p, name, vra, run, solutions);
for (CalculationListener l : listeners) l.calculationEnds(p, name, vra, run, solutions);
}
}

View file

@ -41,7 +41,6 @@ public class ConcurrentBenchmarker {
}
private String algorithmConfig = null;
private List<BenchmarkInstance> benchmarkInstances = new ArrayList<BenchmarkInstance>();
@ -52,7 +51,7 @@ public class ConcurrentBenchmarker {
private Collection<BenchmarkResult> results = new ArrayList<BenchmarkResult>();
private Cost cost = new Cost(){
private Cost cost = new Cost() {
@Override
public double getCost(VehicleRoutingProblemSolution sol) {
@ -63,7 +62,9 @@ public class ConcurrentBenchmarker {
private VehicleRoutingAlgorithmFactory algorithmFactory;
public void setCost(Cost cost){ this.cost = cost; }
public void setCost(Cost cost) {
this.cost = cost;
}
public ConcurrentBenchmarker(String algorithmConfig) {
super();
@ -71,28 +72,28 @@ public class ConcurrentBenchmarker {
// LogManager.getRootLogger().setLevel(Level.ERROR);
}
public ConcurrentBenchmarker(VehicleRoutingAlgorithmFactory algorithmFactory){
public ConcurrentBenchmarker(VehicleRoutingAlgorithmFactory algorithmFactory) {
this.algorithmFactory = algorithmFactory;
}
public void addBenchmarkWriter(BenchmarkWriter writer){
public void addBenchmarkWriter(BenchmarkWriter writer) {
writers.add(writer);
}
public void addInstance(String name, VehicleRoutingProblem problem){
benchmarkInstances.add(new BenchmarkInstance(name,problem,null,null));
public void addInstance(String name, VehicleRoutingProblem problem) {
benchmarkInstances.add(new BenchmarkInstance(name, problem, null, null));
}
public void addInstane(BenchmarkInstance instance){
public void addInstane(BenchmarkInstance instance) {
benchmarkInstances.add(instance);
}
public void addAllInstances(Collection<BenchmarkInstance> instances){
public void addAllInstances(Collection<BenchmarkInstance> instances) {
benchmarkInstances.addAll(instances);
}
public void addInstance(String name, VehicleRoutingProblem problem, Double bestKnownResult, Double bestKnownVehicles){
benchmarkInstances.add(new BenchmarkInstance(name,problem,bestKnownResult,bestKnownVehicles));
public void addInstance(String name, VehicleRoutingProblem problem, Double bestKnownResult, Double bestKnownVehicles) {
benchmarkInstances.add(new BenchmarkInstance(name, problem, bestKnownResult, bestKnownVehicles));
}
/**
@ -101,18 +102,18 @@ public class ConcurrentBenchmarker {
*
* @param runs
*/
public void setNuOfRuns(int runs){
public void setNuOfRuns(int runs) {
this.runs = runs;
}
public void run(){
public void run() {
System.out.println("start benchmarking [nuOfInstances=" + benchmarkInstances.size() + "][runsPerInstance=" + runs + "]");
double startTime = System.currentTimeMillis();
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()+1);
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
List<Future<BenchmarkResult>> futures = new ArrayList<Future<BenchmarkResult>>();
for(final BenchmarkInstance p : benchmarkInstances){
for (final BenchmarkInstance p : benchmarkInstances) {
Future<BenchmarkResult> futureResult = executor.submit(new Callable<BenchmarkResult>(){
Future<BenchmarkResult> futureResult = executor.submit(new Callable<BenchmarkResult>() {
@Override
public BenchmarkResult call() throws Exception {
@ -125,9 +126,9 @@ public class ConcurrentBenchmarker {
}
try {
int count = 1;
for(Future<BenchmarkResult> f : futures){
for (Future<BenchmarkResult> f : futures) {
BenchmarkResult r = f.get();
print(r,count);
print(r, count);
results.add(f.get());
count++;
}
@ -140,7 +141,7 @@ public class ConcurrentBenchmarker {
}
executor.shutdown();
print(results);
System.out.println("done [time="+(System.currentTimeMillis()-startTime)/1000 + "sec]");
System.out.println("done [time=" + (System.currentTimeMillis() - startTime) / 1000 + "sec]");
}
private BenchmarkResult runAlgoAndGetResult(BenchmarkInstance p) {
@ -148,10 +149,10 @@ public class ConcurrentBenchmarker {
double[] results = new double[runs];
double[] times = new double[runs];
for(int run=0;run<runs;run++){
for (int run = 0; run < runs; run++) {
VehicleRoutingAlgorithm vra = createAlgorithm(p);
StopWatch stopwatch = new StopWatch();
vra.getAlgorithmListeners().addListener(stopwatch,Priority.HIGH);
vra.getAlgorithmListeners().addListener(stopwatch, Priority.HIGH);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoutingProblemSolution best = Solutions.bestOf(solutions);
vehicles[run] = best.getRoutes().size();
@ -163,25 +164,24 @@ public class ConcurrentBenchmarker {
}
private VehicleRoutingAlgorithm createAlgorithm(BenchmarkInstance p) {
if(algorithmConfig != null){
if (algorithmConfig != null) {
return VehicleRoutingAlgorithms.readAndCreateAlgorithm(p.vrp, algorithmConfig);
}
else{
} else {
return algorithmFactory.createAlgorithm(p.vrp);
}
}
private void print(Collection<BenchmarkResult> results) {
double sumTime=0.0;
double sumResult=0.0;
for(BenchmarkResult r : results){
sumTime+=r.getTimesStats().getMean();
sumResult+=r.getResultStats().getMean();
double sumTime = 0.0;
double sumResult = 0.0;
for (BenchmarkResult r : results) {
sumTime += r.getTimesStats().getMean();
sumResult += r.getResultStats().getMean();
// print(r);
}
System.out.println("[avgTime="+round(sumTime/(double)results.size(),2)+"][avgResult="+round(sumResult/(double)results.size(),2)+"]");
for(BenchmarkWriter writer : writers){
System.out.println("[avgTime=" + round(sumTime / (double) results.size(), 2) + "][avgResult=" + round(sumResult / (double) results.size(), 2) + "]");
for (BenchmarkWriter writer : writers) {
writer.write(results);
}
}
@ -190,30 +190,30 @@ public class ConcurrentBenchmarker {
Double avgDelta = null;
Double bestDelta = null;
Double worstDelta = null;
if(r.instance.bestKnownResult != null){
if (r.instance.bestKnownResult != null) {
avgDelta = (r.getResultStats().getMean() / r.instance.bestKnownResult - 1) * 100;
bestDelta = (r.getResultStats().getMin() / r.instance.bestKnownResult - 1) * 100;
worstDelta = (r.getResultStats().getMax() / r.instance.bestKnownResult - 1) * 100;
}
System.out.println("("+count+"/"+benchmarkInstances.size() +")"+ "\t[instance="+r.instance.name+
"][avgTime="+round(r.getTimesStats().getMean(),2)+"]" +
System.out.println("(" + count + "/" + benchmarkInstances.size() + ")" + "\t[instance=" + r.instance.name +
"][avgTime=" + round(r.getTimesStats().getMean(), 2) + "]" +
"[Result=" + getString(r.getResultStats()) + "]" +
"[Vehicles=" + getString(r.getVehicleStats()) + "]" +
"[Delta[%]=" + getString(bestDelta,avgDelta,worstDelta) + "]");
"[Delta[%]=" + getString(bestDelta, avgDelta, worstDelta) + "]");
}
private String getString(Double bestDelta, Double avgDelta,Double worstDelta) {
return "[best="+round(bestDelta,2)+"][avg="+round(avgDelta,2)+"][worst="+round(worstDelta,2)+"]";
private String getString(Double bestDelta, Double avgDelta, Double worstDelta) {
return "[best=" + round(bestDelta, 2) + "][avg=" + round(avgDelta, 2) + "][worst=" + round(worstDelta, 2) + "]";
}
private String getString(DescriptiveStatistics stats){
return "[best="+round(stats.getMin(),2)+"][avg="+round(stats.getMean(),2)+"][worst="+round(stats.getMax(),2)+"][stdDev=" + round(stats.getStandardDeviation(),2)+"]";
private String getString(DescriptiveStatistics stats) {
return "[best=" + round(stats.getMin(), 2) + "][avg=" + round(stats.getMean(), 2) + "][worst=" + round(stats.getMax(), 2) + "][stdDev=" + round(stats.getStandardDeviation(), 2) + "]";
}
private Double round(Double value, int i) {
if(value==null) return null;
long roundedVal = Math.round(value*Math.pow(10, i));
return (double)roundedVal/(double)(Math.pow(10, i));
if (value == null) return null;
long roundedVal = Math.round(value * Math.pow(10, i));
return (double) roundedVal / (double) (Math.pow(10, i));
}
}

View file

@ -110,8 +110,7 @@ public class GraphStreamViewer {
"edge.shipment {" +
" fill-color: #999;" +
" arrow-size: 6px,3px;" +
"}" ;
"}";
@SuppressWarnings("UnusedDeclaration")
@ -165,11 +164,11 @@ public class GraphStreamViewer {
"edge.shipment {" +
" fill-color: #999;" +
" arrow-size: 6px,3px;" +
"}" ;
"}";
}
public static Graph createMultiGraph(String name, String style){
public static Graph createMultiGraph(String name, String style) {
Graph g = new MultiGraph(name);
g.addAttribute("ui.quality");
g.addAttribute("ui.antialias");
@ -177,8 +176,8 @@ public class GraphStreamViewer {
return g;
}
public static ViewPanel createEmbeddedView(Graph graph, double scaling){
Viewer viewer = new Viewer(graph,Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
public static ViewPanel createEmbeddedView(Graph graph, double scaling) {
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
ViewPanel view = viewer.addDefaultView(false);
view.setPreferredSize(new Dimension((int) (698 * scaling), (int) (440 * scaling)));
return view;
@ -234,7 +233,7 @@ public class GraphStreamViewer {
"edge.shipment {" +
" fill-color: #999;" +
" arrow-size: 6px,3px;" +
"}" ;
"}";
public static enum Label {
NO_LABEL, ID, JOB_NAME, ARRIVAL_TIME, DEPARTURE_TIME, ACTIVITY
@ -280,13 +279,13 @@ public class GraphStreamViewer {
this.solution = solution;
}
public GraphStreamViewer labelWith(Label label){
this.label=label;
public GraphStreamViewer labelWith(Label label) {
this.label = label;
return this;
}
public GraphStreamViewer setRenderDelay(long ms){
this.renderDelay_in_ms=ms;
public GraphStreamViewer setRenderDelay(long ms) {
this.renderDelay_in_ms = ms;
return this;
}
@ -295,20 +294,20 @@ public class GraphStreamViewer {
return this;
}
public GraphStreamViewer setRenderShipments(boolean renderShipments){
public GraphStreamViewer setRenderShipments(boolean renderShipments) {
this.renderShipments = renderShipments;
return this;
}
public GraphStreamViewer setGraphStreamFrameScalingFactor(double factor){
this.scaling=factor;
public GraphStreamViewer setGraphStreamFrameScalingFactor(double factor) {
this.scaling = factor;
return this;
}
/**
* Sets the camera-view. Center describes the center-focus of the camera and zoomFactor its
* zoomFactor.
*
* <p/>
* <p>a zoomFactor < 1 zooms in and > 1 out.
*
* @param centerX x coordinate of center
@ -316,20 +315,20 @@ public class GraphStreamViewer {
* @param zoomFactor zoom factor
* @return the viewer
*/
public GraphStreamViewer setCameraView(double centerX, double centerY, double zoomFactor){
center = new Center(centerX,centerY);
public GraphStreamViewer setCameraView(double centerX, double centerY, double zoomFactor) {
center = new Center(centerX, centerY);
this.zoomFactor = zoomFactor;
return this;
}
public void display(){
public void display() {
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph g = createMultiGraph("g");
ViewPanel view = createEmbeddedView(g,scaling);
ViewPanel view = createEmbeddedView(g, scaling);
createJFrame(view,scaling);
createJFrame(view, scaling);
render(g, view);
}
@ -345,11 +344,11 @@ public class GraphStreamViewer {
JPanel graphStreamPanel = new JPanel();
graphStreamPanel.setPreferredSize(new Dimension((int)(800*scaling),(int)(460*scaling)));
graphStreamPanel.setPreferredSize(new Dimension((int) (800 * scaling), (int) (460 * scaling)));
graphStreamPanel.setBackground(Color.WHITE);
JPanel graphStreamBackPanel = new JPanel();
graphStreamBackPanel.setPreferredSize(new Dimension((int)(700*scaling),(int)(450*scaling)));
graphStreamBackPanel.setPreferredSize(new Dimension((int) (700 * scaling), (int) (450 * scaling)));
graphStreamBackPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
graphStreamBackPanel.setBackground(Color.WHITE);
@ -365,7 +364,7 @@ public class GraphStreamViewer {
jframe.add(basicPanel);
//conf jframe
jframe.setSize((int)(800*scaling),(int)(580*scaling));
jframe.setSize((int) (800 * scaling), (int) (580 * scaling));
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jframe.setVisible(true);
@ -375,34 +374,33 @@ public class GraphStreamViewer {
}
private Graph createMultiGraph(String name) {
return GraphStreamViewer.createMultiGraph(name,STYLESHEET);
return GraphStreamViewer.createMultiGraph(name, STYLESHEET);
}
private void render(Graph g, ViewPanel view) {
if(center != null){
if (center != null) {
view.resizeFrame(view.getWidth(), view.getHeight());
alignCamera(view);
}
for(Vehicle vehicle : vrp.getVehicles()){
renderVehicle(g,vehicle,label);
for (Vehicle vehicle : vrp.getVehicles()) {
renderVehicle(g, vehicle, label);
sleep(renderDelay_in_ms);
}
for(Job j : vrp.getJobs().values()){
if(j instanceof Service){
renderService(g,(Service)j,label);
}
else if(j instanceof Shipment){
renderShipment(g,(Shipment)j,label,renderShipments);
for (Job j : vrp.getJobs().values()) {
if (j instanceof Service) {
renderService(g, (Service) j, label);
} else if (j instanceof Shipment) {
renderShipment(g, (Shipment) j, label, renderShipments);
}
sleep(renderDelay_in_ms);
}
if(solution != null){
if (solution != null) {
int routeId = 1;
for(VehicleRoute route : solution.getRoutes()){
renderRoute(g,route,routeId,renderDelay_in_ms,label);
for (VehicleRoute route : solution.getRoutes()) {
renderRoute(g, route, routeId, renderDelay_in_ms, label);
sleep(renderDelay_in_ms);
routeId++;
}
@ -417,7 +415,7 @@ public class GraphStreamViewer {
private JLabel createEmptyLabel() {
JLabel emptyLabel1 = new JLabel();
emptyLabel1.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling)));
emptyLabel1.setPreferredSize(new Dimension((int) (40 * scaling), (int) (25 * scaling)));
return emptyLabel1;
}
@ -426,43 +424,43 @@ public class GraphStreamViewer {
int height = 50;
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension((int)(width*scaling),(int)(height*scaling)));
panel.setPreferredSize(new Dimension((int) (width * scaling), (int) (height * scaling)));
panel.setBackground(Color.WHITE);
JPanel subpanel = new JPanel();
subpanel.setLayout(new FlowLayout());
subpanel.setPreferredSize(new Dimension((int)(700*scaling),(int)(40*scaling)));
subpanel.setPreferredSize(new Dimension((int) (700 * scaling), (int) (40 * scaling)));
subpanel.setBackground(Color.WHITE);
subpanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,1));
subpanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
Font font = Font.decode("couriernew");
JLabel jobs = new JLabel("jobs");
jobs.setFont(font);
jobs.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling)));
jobs.setPreferredSize(new Dimension((int) (40 * scaling), (int) (25 * scaling)));
int noJobs = 0;
if(this.vrp != null) noJobs = this.vrp.getJobs().values().size();
if (this.vrp != null) noJobs = this.vrp.getJobs().values().size();
JFormattedTextField nJobs = new JFormattedTextField(noJobs);
nJobs.setFont(font);
nJobs.setEditable(false);
nJobs.setBorder(BorderFactory.createEmptyBorder());
nJobs.setBackground(new Color(230,230,230));
nJobs.setBackground(new Color(230, 230, 230));
JLabel costs = new JLabel("costs");
costs.setFont(font);
costs.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling)));
costs.setPreferredSize(new Dimension((int) (40 * scaling), (int) (25 * scaling)));
JFormattedTextField costsVal = new JFormattedTextField(getSolutionCosts());
costsVal.setFont(font);
costsVal.setEditable(false);
costsVal.setBorder(BorderFactory.createEmptyBorder());
costsVal.setBackground(new Color(230,230,230));
costsVal.setBackground(new Color(230, 230, 230));
JLabel vehicles = new JLabel("routes");
vehicles.setFont(font);
vehicles.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling)));
vehicles.setPreferredSize(new Dimension((int) (40 * scaling), (int) (25 * scaling)));
// vehicles.setForeground(Color.DARK_GRAY);
JFormattedTextField vehVal = new JFormattedTextField(getNoRoutes());
@ -470,11 +468,11 @@ public class GraphStreamViewer {
vehVal.setEditable(false);
vehVal.setBorder(BorderFactory.createEmptyBorder());
// vehVal.setForeground(Color.DARK_GRAY);
vehVal.setBackground(new Color(230,230,230));
vehVal.setBackground(new Color(230, 230, 230));
//platzhalter
JLabel placeholder1 = new JLabel();
placeholder1.setPreferredSize(new Dimension((int)(60*scaling),(int)(25*scaling)));
placeholder1.setPreferredSize(new Dimension((int) (60 * scaling), (int) (25 * scaling)));
JLabel emptyLabel1 = createEmptyLabel();
@ -498,32 +496,32 @@ public class GraphStreamViewer {
}
private Integer getNoRoutes() {
if(solution!=null) return solution.getRoutes().size();
if (solution != null) return solution.getRoutes().size();
return 0;
}
private Double getSolutionCosts() {
if(solution!=null) return solution.getCost();
if (solution != null) return solution.getCost();
return 0.0;
}
private void renderShipment(Graph g, Shipment shipment, Label label, boolean renderShipments) {
Node n1 = g.addNode(makeId(shipment.getId(),shipment.getPickupLocation().getId()));
if(label.equals(Label.ID)) n1.addAttribute("ui.label", shipment.getId());
Node n1 = g.addNode(makeId(shipment.getId(), shipment.getPickupLocation().getId()));
if (label.equals(Label.ID)) n1.addAttribute("ui.label", shipment.getId());
n1.addAttribute("x", shipment.getPickupLocation().getCoordinate().getX());
n1.addAttribute("y", shipment.getPickupLocation().getCoordinate().getY());
n1.setAttribute("ui.class", "pickup");
Node n2 = g.addNode(makeId(shipment.getId(),shipment.getDeliveryLocation().getId()));
if(label.equals(Label.ID)) n2.addAttribute("ui.label", shipment.getId());
Node n2 = g.addNode(makeId(shipment.getId(), shipment.getDeliveryLocation().getId()));
if (label.equals(Label.ID)) n2.addAttribute("ui.label", shipment.getId());
n2.addAttribute("x", shipment.getDeliveryLocation().getCoordinate().getX());
n2.addAttribute("y", shipment.getDeliveryLocation().getCoordinate().getY());
n2.setAttribute("ui.class", "delivery");
if(renderShipments){
Edge s = g.addEdge(shipment.getId(), makeId(shipment.getId(),shipment.getPickupLocation().getId()),
makeId(shipment.getId(),shipment.getDeliveryLocation().getId()), true);
if (renderShipments) {
Edge s = g.addEdge(shipment.getId(), makeId(shipment.getId(), shipment.getPickupLocation().getId()),
makeId(shipment.getId(), shipment.getDeliveryLocation().getId()), true);
s.addAttribute("ui.class", "shipment");
}
@ -539,12 +537,12 @@ public class GraphStreamViewer {
}
private void renderService(Graph g, Service service, Label label) {
Node n = g.addNode(makeId(service.getId(),service.getLocation().getId()));
if(label.equals(Label.ID)) n.addAttribute("ui.label", service.getId());
Node n = g.addNode(makeId(service.getId(), service.getLocation().getId()));
if (label.equals(Label.ID)) n.addAttribute("ui.label", service.getId());
n.addAttribute("x", service.getLocation().getCoordinate().getX());
n.addAttribute("y", service.getLocation().getCoordinate().getY());
if(service.getType().equals("pickup")) n.setAttribute("ui.class", "pickup");
if(service.getType().equals("delivery")) n.setAttribute("ui.class", "delivery");
if (service.getType().equals("pickup")) n.setAttribute("ui.class", "pickup");
if (service.getType().equals("delivery")) n.setAttribute("ui.class", "delivery");
}
private String makeId(String id, String locationId) {
@ -552,17 +550,17 @@ public class GraphStreamViewer {
}
private void renderVehicle(Graph g, Vehicle vehicle, Label label) {
String nodeId = makeId(vehicle.getId(),vehicle.getStartLocation().getId());
String nodeId = makeId(vehicle.getId(), vehicle.getStartLocation().getId());
Node vehicleStart = g.addNode(nodeId);
if(label.equals(Label.ID)) vehicleStart.addAttribute("ui.label", "depot");
if (label.equals(Label.ID)) vehicleStart.addAttribute("ui.label", "depot");
// if(label.equals(Label.ACTIVITY)) n.addAttribute("ui.label", "start");
vehicleStart.addAttribute("x", vehicle.getStartLocation().getCoordinate().getX());
vehicleStart.addAttribute("y", vehicle.getStartLocation().getCoordinate().getY());
vehicleStart.setAttribute("ui.class", "depot");
if(!vehicle.getStartLocation().getId().equals(vehicle.getEndLocation().getId())){
Node vehicleEnd = g.addNode(makeId(vehicle.getId(),vehicle.getEndLocation().getId()));
if(label.equals(Label.ID)) vehicleEnd.addAttribute("ui.label", "depot");
if (!vehicle.getStartLocation().getId().equals(vehicle.getEndLocation().getId())) {
Node vehicleEnd = g.addNode(makeId(vehicle.getId(), vehicle.getEndLocation().getId()));
if (label.equals(Label.ID)) vehicleEnd.addAttribute("ui.label", "depot");
vehicleEnd.addAttribute("x", vehicle.getEndLocation().getCoordinate().getX());
vehicleEnd.addAttribute("y", vehicle.getEndLocation().getCoordinate().getY());
vehicleEnd.setAttribute("ui.class", "depot");
@ -572,40 +570,38 @@ public class GraphStreamViewer {
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().getStartLocation().getId());
if(label.equals(Label.ACTIVITY) || label.equals(Label.JOB_NAME)){
String prevIdentifier = makeId(route.getVehicle().getId(), route.getVehicle().getStartLocation().getId());
if (label.equals(Label.ACTIVITY) || label.equals(Label.JOB_NAME)) {
Node n = g.getNode(prevIdentifier);
n.addAttribute("ui.label", "start");
}
for(TourActivity act : route.getActivities()){
for (TourActivity act : route.getActivities()) {
Job job = ((JobActivity) act).getJob();
String currIdentifier = makeId(job.getId(),act.getLocation().getId());
if(label.equals(Label.ACTIVITY)){
String currIdentifier = makeId(job.getId(), act.getLocation().getId());
if (label.equals(Label.ACTIVITY)) {
Node actNode = g.getNode(currIdentifier);
actNode.addAttribute("ui.label", act.getName());
}
else if(label.equals(Label.JOB_NAME)){
} else if (label.equals(Label.JOB_NAME)) {
Node actNode = g.getNode(currIdentifier);
actNode.addAttribute("ui.label", job.getName());
}
else if(label.equals(Label.ARRIVAL_TIME)){
} else if (label.equals(Label.ARRIVAL_TIME)) {
Node actNode = g.getNode(currIdentifier);
actNode.addAttribute("ui.label", Time.parseSecondsToTime(act.getArrTime()));
}
else if(label.equals(Label.DEPARTURE_TIME)){
} else if (label.equals(Label.DEPARTURE_TIME)) {
Node actNode = g.getNode(currIdentifier);
actNode.addAttribute("ui.label", Time.parseSecondsToTime(act.getEndTime()));
}
g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, currIdentifier, true);
if(act instanceof PickupActivity) g.getNode(currIdentifier).addAttribute("ui.class", "pickupInRoute");
else if (act instanceof DeliveryActivity) g.getNode(currIdentifier).addAttribute("ui.class", "deliveryInRoute");
g.addEdge(makeEdgeId(routeId, vehicle_edgeId), prevIdentifier, currIdentifier, true);
if (act instanceof PickupActivity) g.getNode(currIdentifier).addAttribute("ui.class", "pickupInRoute");
else if (act instanceof DeliveryActivity)
g.getNode(currIdentifier).addAttribute("ui.class", "deliveryInRoute");
prevIdentifier = currIdentifier;
vehicle_edgeId++;
sleep(renderDelay_in_ms);
}
if(route.getVehicle().isReturnToDepot()){
String lastIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getEndLocation().getId());
g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, lastIdentifier, true);
if (route.getVehicle().isReturnToDepot()) {
String lastIdentifier = makeId(route.getVehicle().getId(), route.getVehicle().getEndLocation().getId());
g.addEdge(makeEdgeId(routeId, vehicle_edgeId), prevIdentifier, lastIdentifier, true);
}
}

View file

@ -18,7 +18,7 @@
******************************************************************************/
package jsprit.analysis.toolbox;
class NoLocationFoundException extends Exception{
class NoLocationFoundException extends Exception {
/**
*

View file

@ -53,7 +53,6 @@ import java.util.*;
* <p>Note that every item to be rendered need to have coordinates.
*
* @author schroeder
*
*/
public class Plotter {
@ -74,12 +73,12 @@ public class Plotter {
private XYSeriesCollection seriesCollection;
private Map<XYDataItem,Activity> activities;
private Map<XYDataItem, Activity> activities;
private Set<XYDataItem> firstActivities;
public MyActivityRenderer(XYSeriesCollection seriesCollection,Map<XYDataItem, Activity> activities,Set<XYDataItem> firstActivities) {
super(false,true);
public MyActivityRenderer(XYSeriesCollection seriesCollection, Map<XYDataItem, Activity> activities, Set<XYDataItem> firstActivities) {
super(false, true);
this.seriesCollection = seriesCollection;
this.activities = activities;
this.firstActivities = firstActivities;
@ -90,7 +89,7 @@ public class Plotter {
@Override
public Shape getItemShape(int seriesIndex, int itemIndex) {
XYDataItem dataItem = seriesCollection.getSeries(seriesIndex).getDataItem(itemIndex);
if(firstActivities.contains(dataItem)){
if (firstActivities.contains(dataItem)) {
return ShapeUtilities.createUpTriangle(4.0f);
}
return ELLIPSE;
@ -99,7 +98,7 @@ public class Plotter {
@Override
public Paint getItemOutlinePaint(int seriesIndex, int itemIndex) {
XYDataItem dataItem = seriesCollection.getSeries(seriesIndex).getDataItem(itemIndex);
if(firstActivities.contains(dataItem)){
if (firstActivities.contains(dataItem)) {
return Color.BLACK;
}
return super.getItemOutlinePaint(seriesIndex, itemIndex);
@ -109,12 +108,12 @@ public class Plotter {
public Paint getItemPaint(int seriesIndex, int itemIndex) {
XYDataItem dataItem = seriesCollection.getSeries(seriesIndex).getDataItem(itemIndex);
Activity activity = activities.get(dataItem);
if(activity.equals(Activity.PICKUP)) return PICKUP_COLOR;
if(activity.equals(Activity.DELIVERY)) return DELIVERY_COLOR;
if(activity.equals(Activity.SERVICE)) return SERVICE_COLOR;
if(activity.equals(Activity.START)) return START_COLOR;
if(activity.equals(Activity.END)) return END_COLOR;
throw new IllegalStateException("activity at "+dataItem.toString()+" cannot be assigned to a color");
if (activity.equals(Activity.PICKUP)) return PICKUP_COLOR;
if (activity.equals(Activity.DELIVERY)) return DELIVERY_COLOR;
if (activity.equals(Activity.SERVICE)) return SERVICE_COLOR;
if (activity.equals(Activity.START)) return START_COLOR;
if (activity.equals(Activity.END)) return END_COLOR;
throw new IllegalStateException("activity at " + dataItem.toString() + " cannot be assigned to a color");
}
}
@ -124,6 +123,7 @@ public class Plotter {
double minY;
double maxX;
double maxY;
public BoundingBox(double minX, double minY, double maxX, double maxY) {
super();
this.minX = minX;
@ -143,8 +143,8 @@ public class Plotter {
/**
* Label to label ID (=jobId), SIZE (=jobSize=jobCapacityDimensions)
* @author schroeder
*
* @author schroeder
*/
public static enum Label {
ID, SIZE, @SuppressWarnings("UnusedDeclaration")NO_LABEL
@ -162,9 +162,9 @@ public class Plotter {
private BoundingBox boundingBox = null;
private Map<XYDataItem,Activity> activitiesByDataItem = new HashMap<XYDataItem, Plotter.Activity>();
private Map<XYDataItem, Activity> activitiesByDataItem = new HashMap<XYDataItem, Plotter.Activity>();
private Map<XYDataItem,String> labelsByDataItem = new HashMap<XYDataItem, String>();
private Map<XYDataItem, String> labelsByDataItem = new HashMap<XYDataItem, String>();
private XYSeries activities;
@ -217,22 +217,23 @@ public class Plotter {
}
@SuppressWarnings("UnusedDeclaration")
public Plotter setScalingFactor(double scalingFactor){
this.scalingFactor=scalingFactor;
public Plotter setScalingFactor(double scalingFactor) {
this.scalingFactor = scalingFactor;
return this;
}
/**
* Sets a label.
*
* @param label of jobs
* @return plotter
*/
public Plotter setLabel(Label label){
public Plotter setLabel(Label label) {
this.label = label;
return this;
}
public Plotter invertCoordinates(boolean invert){
public Plotter invertCoordinates(boolean invert) {
this.invert = invert;
return this;
}
@ -247,8 +248,8 @@ public class Plotter {
* @return
*/
@SuppressWarnings("UnusedDeclaration")
public Plotter setBoundingBox(double minX, double minY, double maxX, double maxY){
boundingBox = new BoundingBox(minX,minY,maxX,maxY);
public Plotter setBoundingBox(double minX, double minY, double maxX, double maxY) {
boundingBox = new BoundingBox(minX, minY, maxX, maxY);
return this;
}
@ -269,22 +270,20 @@ public class Plotter {
* @param pngFileName - path and filename
* @param plotTitle - title that appears on top of image
*/
public void plot(String pngFileName, String plotTitle){
public void plot(String pngFileName, String plotTitle) {
String filename = pngFileName;
if(!pngFileName.endsWith(".png")) filename += ".png";
if(plotSolutionAsWell){
if (!pngFileName.endsWith(".png")) filename += ".png";
if (plotSolutionAsWell) {
plot(vrp, routes, filename, plotTitle);
}
else if(!(vrp.getInitialVehicleRoutes().isEmpty())){
} else if (!(vrp.getInitialVehicleRoutes().isEmpty())) {
plot(vrp, vrp.getInitialVehicleRoutes(), filename, plotTitle);
}
else{
} else {
plot(vrp, null, filename, plotTitle);
}
}
private void plot(VehicleRoutingProblem vrp, final Collection<VehicleRoute> routes, String pngFile, String title){
log.info("plot to " + pngFile);
private void plot(VehicleRoutingProblem vrp, final Collection<VehicleRoute> routes, String pngFile, String title) {
log.info("plot to {}", pngFile);
XYSeriesCollection problem;
XYSeriesCollection solution = null;
final XYSeriesCollection shipments;
@ -292,7 +291,7 @@ public class Plotter {
retrieveActivities(vrp);
problem = new XYSeriesCollection(activities);
shipments = makeShipmentSeries(vrp.getJobs().values());
if(routes!=null) solution = makeSolutionSeries(vrp, routes);
if (routes != null) solution = makeSolutionSeries(vrp, routes);
} catch (NoLocationFoundException e) {
log.warn("cannot plot vrp, since coord is missing");
return;
@ -304,11 +303,11 @@ public class Plotter {
chart.removeLegend();
chart.addLegend(legend);
save(chart,pngFile);
save(chart, pngFile);
}
private LegendTitle createLegend(final Collection<VehicleRoute> routes,final XYSeriesCollection shipments, final XYPlot plot) {
private LegendTitle createLegend(final Collection<VehicleRoute> routes, final XYSeriesCollection shipments, final XYPlot plot) {
LegendItemSource lis = new LegendItemSource() {
@Override
@ -318,26 +317,26 @@ public class Plotter {
vehLoc.setShape(ELLIPSE);
vehLoc.setShapeVisible(true);
lic.add(vehLoc);
if(containsServiceAct){
if (containsServiceAct) {
LegendItem item = new LegendItem("service", Color.BLUE);
item.setShape(ELLIPSE);
item.setShapeVisible(true);
lic.add(item);
}
if(containsPickupAct){
if (containsPickupAct) {
LegendItem item = new LegendItem("pickup", Color.GREEN);
item.setShape(ELLIPSE);
item.setShapeVisible(true);
lic.add(item);
}
if(containsDeliveryAct){
if (containsDeliveryAct) {
LegendItem item = new LegendItem("delivery", Color.BLUE);
item.setShape(ELLIPSE);
item.setShapeVisible(true);
lic.add(item);
}
if(routes!=null){
LegendItem item = new LegendItem("firstActivity",Color.BLACK);
if (routes != null) {
LegendItem item = new LegendItem("firstActivity", Color.BLACK);
Shape upTriangle = ShapeUtilities.createUpTriangle(3.0f);
item.setShape(upTriangle);
item.setOutlinePaint(Color.BLACK);
@ -348,10 +347,10 @@ public class Plotter {
lic.add(item);
}
if(!shipments.getSeries().isEmpty()){
if (!shipments.getSeries().isEmpty()) {
lic.add(plot.getRenderer(1).getLegendItem(1, 0));
}
if(routes!=null){
if (routes != null) {
lic.addAll(plot.getRenderer(2).getLegendItems());
}
return lic;
@ -365,18 +364,18 @@ public class Plotter {
private XYItemRenderer getShipmentRenderer(XYSeriesCollection shipments) {
XYItemRenderer shipmentsRenderer = new XYLineAndShapeRenderer(true, false); // Shapes only
for(int i=0;i<shipments.getSeriesCount();i++){
for (int i = 0; i < shipments.getSeriesCount(); i++) {
shipmentsRenderer.setSeriesPaint(i, Color.DARK_GRAY);
shipmentsRenderer.setSeriesStroke(i, new BasicStroke(
1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.f, new float[] {4.0f, 4.0f}, 0.0f
1.f, new float[]{4.0f, 4.0f}, 0.0f
));
}
return shipmentsRenderer;
}
private MyActivityRenderer getProblemRenderer(final XYSeriesCollection problem) {
MyActivityRenderer problemRenderer = new MyActivityRenderer(problem, activitiesByDataItem,firstActivities);
MyActivityRenderer problemRenderer = new MyActivityRenderer(problem, activitiesByDataItem, firstActivities);
problemRenderer.setBaseItemLabelGenerator(new XYItemLabelGenerator() {
@Override
@ -393,12 +392,12 @@ public class Plotter {
}
private Range getRange(final XYSeriesCollection seriesCol) {
if(this.boundingBox==null) return seriesCol.getRangeBounds(false);
if (this.boundingBox == null) return seriesCol.getRangeBounds(false);
else return new Range(boundingBox.minY, boundingBox.maxY);
}
private Range getDomainRange(final XYSeriesCollection seriesCol) {
if(this.boundingBox == null) return seriesCol.getDomainBounds(true);
if (this.boundingBox == null) return seriesCol.getDomainBounds(true);
else return new Range(boundingBox.minX, boundingBox.maxX);
}
@ -416,7 +415,7 @@ public class Plotter {
plot.setDataset(1, shipments);
plot.setRenderer(1, shipmentsRenderer);
if(solution != null){
if (solution != null) {
XYItemRenderer solutionRenderer = getRouteRenderer(solution);
plot.setDataset(2, solution);
plot.setRenderer(2, solutionRenderer);
@ -425,11 +424,10 @@ public class Plotter {
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
if(boundingBox == null){
if (boundingBox == null) {
xAxis.setRangeWithMargins(getDomainRange(problem));
yAxis.setRangeWithMargins(getRange(problem));
}
else{
} else {
xAxis.setRangeWithMargins(new Range(boundingBox.minX, boundingBox.maxX));
yAxis.setRangeWithMargins(new Range(boundingBox.minY, boundingBox.maxY));
}
@ -442,7 +440,7 @@ public class Plotter {
private XYItemRenderer getRouteRenderer(XYSeriesCollection solutionColl) {
XYItemRenderer solutionRenderer = new XYLineAndShapeRenderer(true, false); // Lines only
for(int i=0;i<solutionColl.getSeriesCount();i++){
for (int i = 0; i < solutionColl.getSeriesCount(); i++) {
XYSeries s = solutionColl.getSeries(i);
XYDataItem firstCustomer = s.getDataItem(1);
firstActivities.add(firstCustomer);
@ -460,24 +458,24 @@ public class Plotter {
}
}
private XYSeriesCollection makeSolutionSeries(VehicleRoutingProblem vrp, Collection<VehicleRoute> routes) throws NoLocationFoundException{
private XYSeriesCollection makeSolutionSeries(VehicleRoutingProblem vrp, Collection<VehicleRoute> routes) throws NoLocationFoundException {
Locations locations = retrieveLocations(vrp);
XYSeriesCollection coll = new XYSeriesCollection();
int counter = 1;
for(VehicleRoute route : routes){
if(route.isEmpty()) continue;
for (VehicleRoute route : routes) {
if (route.isEmpty()) continue;
XYSeries series = new XYSeries(counter, false, true);
Coordinate startCoord = getCoordinate(locations.getCoord(route.getStart().getLocation().getId()));
series.add(startCoord.getX()*scalingFactor, startCoord.getY()*scalingFactor);
series.add(startCoord.getX() * scalingFactor, startCoord.getY() * scalingFactor);
for(TourActivity act : route.getTourActivities().getActivities()){
for (TourActivity act : route.getTourActivities().getActivities()) {
Coordinate coord = getCoordinate(locations.getCoord(act.getLocation().getId()));
series.add(coord.getX()*scalingFactor, coord.getY()*scalingFactor);
series.add(coord.getX() * scalingFactor, coord.getY() * scalingFactor);
}
Coordinate endCoord = getCoordinate(locations.getCoord(route.getEnd().getLocation().getId()));
series.add(endCoord.getX()*scalingFactor, endCoord.getY()*scalingFactor);
series.add(endCoord.getX() * scalingFactor, endCoord.getY() * scalingFactor);
coll.addSeries(series);
counter++;
@ -485,89 +483,83 @@ public class Plotter {
return coll;
}
private XYSeriesCollection makeShipmentSeries(Collection<Job> jobs) throws NoLocationFoundException{
private XYSeriesCollection makeShipmentSeries(Collection<Job> jobs) throws NoLocationFoundException {
XYSeriesCollection coll = new XYSeriesCollection();
if(!plotShipments) return coll;
if (!plotShipments) return coll;
int sCounter = 1;
String ship = "shipment";
boolean first = true;
for(Job job : jobs){
if(!(job instanceof Shipment)){
for (Job job : jobs) {
if (!(job instanceof Shipment)) {
continue;
}
Shipment shipment = (Shipment)job;
Shipment shipment = (Shipment) job;
XYSeries shipmentSeries;
if(first){
if (first) {
first = false;
shipmentSeries = new XYSeries(ship, false, true);
}
else{
} else {
shipmentSeries = new XYSeries(sCounter, false, true);
sCounter++;
}
Coordinate pickupCoordinate = getCoordinate(shipment.getPickupLocation().getCoordinate());
Coordinate delCoordinate = getCoordinate(shipment.getDeliveryLocation().getCoordinate());
shipmentSeries.add(pickupCoordinate.getX()*scalingFactor, pickupCoordinate.getY()*scalingFactor);
shipmentSeries.add(delCoordinate.getX()*scalingFactor, delCoordinate.getY()*scalingFactor);
shipmentSeries.add(pickupCoordinate.getX() * scalingFactor, pickupCoordinate.getY() * scalingFactor);
shipmentSeries.add(delCoordinate.getX() * scalingFactor, delCoordinate.getY() * scalingFactor);
coll.addSeries(shipmentSeries);
}
return coll;
}
private void addJob(XYSeries activities, Job job) {
if(job instanceof Shipment){
Shipment s = (Shipment)job;
if (job instanceof Shipment) {
Shipment s = (Shipment) job;
Coordinate pickupCoordinate = getCoordinate(s.getPickupLocation().getCoordinate());
XYDataItem dataItem = new XYDataItem(pickupCoordinate.getX()*scalingFactor, pickupCoordinate.getY()*scalingFactor);
XYDataItem dataItem = new XYDataItem(pickupCoordinate.getX() * scalingFactor, pickupCoordinate.getY() * scalingFactor);
activities.add(dataItem);
addLabel(s, dataItem);
markItem(dataItem,Activity.PICKUP);
markItem(dataItem, Activity.PICKUP);
containsPickupAct = true;
Coordinate deliveryCoordinate = getCoordinate(s.getDeliveryLocation().getCoordinate());
XYDataItem dataItem2 = new XYDataItem(deliveryCoordinate.getX()*scalingFactor, deliveryCoordinate.getY()*scalingFactor);
XYDataItem dataItem2 = new XYDataItem(deliveryCoordinate.getX() * scalingFactor, deliveryCoordinate.getY() * scalingFactor);
activities.add(dataItem2);
addLabel(s, dataItem2);
markItem(dataItem2,Activity.DELIVERY);
markItem(dataItem2, Activity.DELIVERY);
containsDeliveryAct = true;
}
else if(job instanceof Pickup){
Pickup service = (Pickup)job;
} else if (job instanceof Pickup) {
Pickup service = (Pickup) job;
Coordinate coord = getCoordinate(service.getLocation().getCoordinate());
XYDataItem dataItem = new XYDataItem(coord.getX()*scalingFactor, coord.getY()*scalingFactor);
XYDataItem dataItem = new XYDataItem(coord.getX() * scalingFactor, coord.getY() * scalingFactor);
activities.add(dataItem);
addLabel(service, dataItem);
markItem(dataItem, Activity.PICKUP);
containsPickupAct = true;
}
else if(job instanceof Delivery){
Delivery service = (Delivery)job;
} else if (job instanceof Delivery) {
Delivery service = (Delivery) job;
Coordinate coord = getCoordinate(service.getLocation().getCoordinate());
XYDataItem dataItem = new XYDataItem(coord.getX()*scalingFactor, coord.getY()*scalingFactor);
XYDataItem dataItem = new XYDataItem(coord.getX() * scalingFactor, coord.getY() * scalingFactor);
activities.add(dataItem);
addLabel(service, dataItem);
markItem(dataItem, Activity.DELIVERY);
containsDeliveryAct = true;
}
else if(job instanceof Service){
Service service = (Service)job;
} else if (job instanceof Service) {
Service service = (Service) job;
Coordinate coord = getCoordinate(service.getLocation().getCoordinate());
XYDataItem dataItem = new XYDataItem(coord.getX()*scalingFactor, coord.getY()*scalingFactor);
XYDataItem dataItem = new XYDataItem(coord.getX() * scalingFactor, coord.getY() * scalingFactor);
activities.add(dataItem);
addLabel(service, dataItem);
markItem(dataItem, Activity.SERVICE);
containsServiceAct = true;
}
else{
} else {
throw new IllegalStateException("job instanceof " + job.getClass().toString() + ". this is not supported.");
}
}
private void addLabel(Job job, XYDataItem dataItem) {
if(this.label.equals(Label.SIZE)){
if (this.label.equals(Label.SIZE)) {
labelsByDataItem.put(dataItem, getSizeString(job));
}
else if(this.label.equals(Label.ID)){
} else if (this.label.equals(Label.ID)) {
labelsByDataItem.put(dataItem, String.valueOf(job.getId()));
}
}
@ -576,12 +568,11 @@ public class Plotter {
StringBuilder builder = new StringBuilder();
builder.append("(");
boolean firstDim = true;
for(int i=0;i<job.getSize().getNuOfDimensions();i++){
if(firstDim) {
for (int i = 0; i < job.getSize().getNuOfDimensions(); i++) {
if (firstDim) {
builder.append(String.valueOf(job.getSize().get(i)));
firstDim = false;
}
else {
} else {
builder.append(",");
builder.append(String.valueOf(job.getSize().get(i)));
}
@ -590,42 +581,42 @@ public class Plotter {
return builder.toString();
}
private Coordinate getCoordinate(Coordinate coordinate){
if(invert){
return Coordinate.newInstance(coordinate.getY(),coordinate.getX());
private Coordinate getCoordinate(Coordinate coordinate) {
if (invert) {
return Coordinate.newInstance(coordinate.getY(), coordinate.getX());
}
return coordinate;
}
private void retrieveActivities(VehicleRoutingProblem vrp) throws NoLocationFoundException{
private void retrieveActivities(VehicleRoutingProblem vrp) throws NoLocationFoundException {
activities = new XYSeries("activities", false, true);
for(Vehicle v : vrp.getVehicles()){
for (Vehicle v : vrp.getVehicles()) {
Coordinate start_coordinate = getCoordinate(v.getStartLocation().getCoordinate());
if(start_coordinate == null) throw new NoLocationFoundException();
XYDataItem item = new XYDataItem(start_coordinate.getX()*scalingFactor, start_coordinate.getY()*scalingFactor);
markItem(item,Activity.START);
if (start_coordinate == null) throw new NoLocationFoundException();
XYDataItem item = new XYDataItem(start_coordinate.getX() * scalingFactor, start_coordinate.getY() * scalingFactor);
markItem(item, Activity.START);
activities.add(item);
if(!v.getStartLocation().getId().equals(v.getEndLocation().getId())){
if (!v.getStartLocation().getId().equals(v.getEndLocation().getId())) {
Coordinate end_coordinate = getCoordinate(v.getEndLocation().getCoordinate());
if(end_coordinate == null) throw new NoLocationFoundException();
XYDataItem end_item = new XYDataItem(end_coordinate.getX()*scalingFactor,end_coordinate.getY()*scalingFactor);
markItem(end_item,Activity.END);
if (end_coordinate == null) throw new NoLocationFoundException();
XYDataItem end_item = new XYDataItem(end_coordinate.getX() * scalingFactor, end_coordinate.getY() * scalingFactor);
markItem(end_item, Activity.END);
activities.add(end_item);
}
}
for(Job job : vrp.getJobs().values()){
for (Job job : vrp.getJobs().values()) {
addJob(activities, job);
}
for(VehicleRoute r : vrp.getInitialVehicleRoutes()){
for(Job job : r.getTourActivities().getJobs()){
for (VehicleRoute r : vrp.getInitialVehicleRoutes()) {
for (Job job : r.getTourActivities().getJobs()) {
addJob(activities, job);
}
}
}
private void markItem(XYDataItem item, Activity activity) {
activitiesByDataItem.put(item,activity);
activitiesByDataItem.put(item, activity);
}
private Locations retrieveLocations(VehicleRoutingProblem vrp) throws NoLocationFoundException {

View file

@ -27,7 +27,7 @@ import org.apache.logging.log4j.Logger;
import java.util.Collection;
public class StopWatch implements AlgorithmStartsListener, AlgorithmEndsListener{
public class StopWatch implements AlgorithmStartsListener, AlgorithmEndsListener {
private static Logger log = LogManager.getLogger(StopWatch.class);
@ -42,25 +42,25 @@ public class StopWatch implements AlgorithmStartsListener, AlgorithmEndsListener
start();
}
public double getCompTimeInSeconds(){
return (ran)/1000.0;
public double getCompTimeInSeconds() {
return (ran) / 1000.0;
}
@Override
public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
stop();
log.info("computation time [in sec]: " + getCompTimeInSeconds());
log.info("computation time [in sec]: {}", getCompTimeInSeconds());
}
public void stop(){
public void stop() {
ran += System.currentTimeMillis() - startTime;
}
public void start(){
public void start() {
startTime = System.currentTimeMillis();
}
public void reset(){
public void reset() {
startTime = 0;
ran = 0;
}
@ -71,7 +71,7 @@ public class StopWatch implements AlgorithmStartsListener, AlgorithmEndsListener
}
public double getCurrTimeInSeconds() {
return (System.currentTimeMillis()-startTime)/1000.0;
return (System.currentTimeMillis() - startTime) / 1000.0;
}
}

View file

@ -32,9 +32,7 @@ import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author schroeder
*
*/
public class XYLineChartBuilder {
@ -44,7 +42,7 @@ public class XYLineChartBuilder {
* @param chart
* @param pngFilename
*/
public static void saveChartAsPNG(JFreeChart chart, String pngFilename){
public static void saveChartAsPNG(JFreeChart chart, String pngFilename) {
try {
ChartUtilities.saveChartAsPNG(new File(pngFilename), chart, 1000, 600);
} catch (IOException e) {
@ -58,14 +56,13 @@ public class XYLineChartBuilder {
* @param chartTitle appears on top of the XYLineChart
* @param xDomainName appears below the xAxis
* @param yDomainName appears beside the yAxis
*
* @return the builder
*/
public static XYLineChartBuilder newInstance(String chartTitle, String xDomainName, String yDomainName){
public static XYLineChartBuilder newInstance(String chartTitle, String xDomainName, String yDomainName) {
return new XYLineChartBuilder(chartTitle, xDomainName, yDomainName);
}
private ConcurrentHashMap<String,XYSeries> seriesMap = new ConcurrentHashMap<String, XYSeries>();
private ConcurrentHashMap<String, XYSeries> seriesMap = new ConcurrentHashMap<String, XYSeries>();
private final String xDomain;
@ -74,9 +71,9 @@ public class XYLineChartBuilder {
private final String chartName;
private XYLineChartBuilder(String chartName, String xDomainName, String yDomainName) {
this.xDomain=xDomainName;
this.yDomain=yDomainName;
this.chartName=chartName;
this.xDomain = xDomainName;
this.yDomain = yDomainName;
this.chartName = chartName;
}
/**
@ -86,20 +83,21 @@ public class XYLineChartBuilder {
* @param xVal
* @param yVal
*/
public void addData(String seriesName, double xVal, double yVal){
if(!seriesMap.containsKey(seriesName)){
seriesMap.put(seriesName, new XYSeries(seriesName,true,true));
public void addData(String seriesName, double xVal, double yVal) {
if (!seriesMap.containsKey(seriesName)) {
seriesMap.put(seriesName, new XYSeries(seriesName, true, true));
}
seriesMap.get(seriesName).add(xVal, yVal);
}
/**
* Builds and returns JFreeChart.
*
* @return
*/
public JFreeChart build(){
public JFreeChart build() {
XYSeriesCollection collection = new XYSeriesCollection();
for(XYSeries s : seriesMap.values()){
for (XYSeries s : seriesMap.values()) {
collection.addSeries(s);
}
JFreeChart chart = ChartFactory.createXYLineChart(chartName, xDomain, yDomain, collection, PlotOrientation.VERTICAL, true, true, false);

View file

@ -16,10 +16,10 @@
******************************************************************************/
package jsprit.analysis.util;
import java.util.Collection;
import jsprit.core.util.BenchmarkResult;
import java.util.Collection;
public interface BenchmarkWriter {
public void write(Collection<BenchmarkResult> results);
}

View file

@ -16,15 +16,15 @@
******************************************************************************/
package jsprit.analysis.util;
import jsprit.core.util.BenchmarkResult;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import jsprit.core.util.BenchmarkResult;
public class HtmlBenchmarkTableWriter implements BenchmarkWriter{
public class HtmlBenchmarkTableWriter implements BenchmarkWriter {
private String filename;
@ -43,8 +43,8 @@ public class HtmlBenchmarkTableWriter implements BenchmarkWriter{
writer.write(head("inst") + newline());
writer.write(head("runs") + newline());
writer.write(head("&Oslash; time [sec]") + newline());
writer.write(head("results",4));
writer.write(head("vehicles",4));
writer.write(head("results", 4));
writer.write(head("vehicles", 4));
writer.write(head("res*") + newline());
writer.write(head("veh*") + newline());
writer.write(closeRow() + newline());
@ -78,40 +78,40 @@ public class HtmlBenchmarkTableWriter implements BenchmarkWriter{
double sum_dev_veh = 0.0;
Integer runs = null;
Double sum_res_star=null;
Double sum_veh_star=null;
Double sum_res_star = null;
Double sum_veh_star = null;
for(BenchmarkResult result : results){
if(runs==null) runs=result.runs;
for (BenchmarkResult result : results) {
if (runs == null) runs = result.runs;
writer.write(openRow() + newline());
writer.write(date(result.instance.name) + newline());
writer.write(date(Integer.valueOf(result.runs).toString()) + newline());
Double avg_time = round(result.getTimesStats().getMean(),2);
Double avg_time = round(result.getTimesStats().getMean(), 2);
writer.write(date(Double.valueOf(avg_time).toString()) + newline());
//bestRes
Double best_result = round(result.getResultStats().getMin(),2);
Double best_result = round(result.getResultStats().getMin(), 2);
writer.write(date(Double.valueOf(best_result).toString()) + newline());
//avgRes
Double avg_result = round(result.getResultStats().getMean(),2);
Double avg_result = round(result.getResultStats().getMean(), 2);
writer.write(date(Double.valueOf(avg_result).toString()) + newline());
//worstRes
Double worst_result = round(result.getResultStats().getMax(),2);
Double worst_result = round(result.getResultStats().getMax(), 2);
writer.write(date(Double.valueOf(worst_result).toString()) + newline());
//stdevRes
Double std_result = round(result.getResultStats().getStandardDeviation(),2);
Double std_result = round(result.getResultStats().getStandardDeviation(), 2);
writer.write(date(Double.valueOf(std_result).toString()) + newline());
//bestVeh
Double best_vehicle = round(result.getVehicleStats().getMin(),2);
Double best_vehicle = round(result.getVehicleStats().getMin(), 2);
writer.write(date(Double.valueOf(best_vehicle).toString()) + newline());
//avgVeh
Double avg_vehicle = round(result.getVehicleStats().getMean(),2);
Double avg_vehicle = round(result.getVehicleStats().getMean(), 2);
writer.write(date(Double.valueOf(avg_vehicle).toString()) + newline());
//worstVeh
Double worst_vehicle = round(result.getVehicleStats().getMax(),2);
Double worst_vehicle = round(result.getVehicleStats().getMax(), 2);
writer.write(date(Double.valueOf(worst_vehicle).toString()) + newline());
//stdevVeh
Double std_vehicle = round(result.getVehicleStats().getStandardDeviation(),2);
Double std_vehicle = round(result.getVehicleStats().getStandardDeviation(), 2);
writer.write(date(Double.valueOf(std_vehicle).toString()) + newline());
//bestKnownRes
writer.write(date("" + result.instance.bestKnownResult + newline()));
@ -119,71 +119,69 @@ public class HtmlBenchmarkTableWriter implements BenchmarkWriter{
writer.write(date("" + result.instance.bestKnownVehicles + newline()));
writer.write(closeRow() + newline());
sum_avg_time+=avg_time;
sum_best_result+=best_result;
sum_avg_result+=avg_result;
sum_worst_result+=worst_result;
sum_dev_result+=std_result;
sum_avg_time += avg_time;
sum_best_result += best_result;
sum_avg_result += avg_result;
sum_worst_result += worst_result;
sum_dev_result += std_result;
sum_best_veh+=best_vehicle;
sum_avg_veh+=avg_vehicle;
sum_worst_veh+=worst_vehicle;
sum_dev_veh+=std_vehicle;
sum_best_veh += best_vehicle;
sum_avg_veh += avg_vehicle;
sum_worst_veh += worst_vehicle;
sum_dev_veh += std_vehicle;
if(result.instance.bestKnownResult != null){
if(sum_res_star==null) sum_res_star=result.instance.bestKnownResult;
else sum_res_star+=result.instance.bestKnownResult;
if (result.instance.bestKnownResult != null) {
if (sum_res_star == null) sum_res_star = result.instance.bestKnownResult;
else sum_res_star += result.instance.bestKnownResult;
}
if(result.instance.bestKnownVehicles != null){
if(sum_veh_star==null) sum_veh_star=result.instance.bestKnownVehicles;
else sum_veh_star+=result.instance.bestKnownVehicles;
if (result.instance.bestKnownVehicles != null) {
if (sum_veh_star == null) sum_veh_star = result.instance.bestKnownVehicles;
else sum_veh_star += result.instance.bestKnownVehicles;
}
}
writer.write(openRow() + newline());
writer.write(date("&Oslash;") + newline());
writer.write(date(""+runs) + newline());
writer.write(date("" + runs) + newline());
Double average_time = round(sum_avg_time/(double)results.size(),2);
Double average_time = round(sum_avg_time / (double) results.size(), 2);
writer.write(date(Double.valueOf(average_time).toString()) + newline());
//bestRes
writer.write(date(Double.valueOf(round(sum_best_result/(double)results.size(),2)).toString()) + newline());
writer.write(date(Double.valueOf(round(sum_best_result / (double) results.size(), 2)).toString()) + newline());
//avgRes
Double average_result = round(sum_avg_result/(double)results.size(),2);
Double average_result = round(sum_avg_result / (double) results.size(), 2);
writer.write(date(Double.valueOf(average_result).toString()) + newline());
//worstRes
writer.write(date(Double.valueOf(round(sum_worst_result/(double)results.size(),2)).toString()) + newline());
writer.write(date(Double.valueOf(round(sum_worst_result / (double) results.size(), 2)).toString()) + newline());
//stdevRes
writer.write(date(Double.valueOf(round(sum_dev_result/(double)results.size(),2)).toString()) + newline());
writer.write(date(Double.valueOf(round(sum_dev_result / (double) results.size(), 2)).toString()) + newline());
//bestVeh
writer.write(date(Double.valueOf(round(sum_best_veh/(double)results.size(),2)).toString()) + newline());
writer.write(date(Double.valueOf(round(sum_best_veh / (double) results.size(), 2)).toString()) + newline());
//avgVeh
Double average_vehicles = round(sum_avg_veh/(double)results.size(),2);
Double average_vehicles = round(sum_avg_veh / (double) results.size(), 2);
writer.write(date(Double.valueOf(average_vehicles).toString()) + newline());
//worstVeh
writer.write(date(Double.valueOf(round(sum_worst_veh/(double)results.size(),2)).toString()) + newline());
writer.write(date(Double.valueOf(round(sum_worst_veh / (double) results.size(), 2)).toString()) + newline());
//stdevVeh
writer.write(date(Double.valueOf(round(sum_dev_veh/(double)results.size(),2)).toString()) + newline());
writer.write(date(Double.valueOf(round(sum_dev_veh / (double) results.size(), 2)).toString()) + newline());
//bestKnownRes
Double delta_res = null;
if(sum_res_star != null){
writer.write(date(Double.valueOf(round(sum_res_star.doubleValue()/(double)results.size(),2)).toString()) + newline());
delta_res = (sum_avg_result/sum_res_star - 1)*100;
}
else writer.write(date("null") + newline());
if (sum_res_star != null) {
writer.write(date(Double.valueOf(round(sum_res_star.doubleValue() / (double) results.size(), 2)).toString()) + newline());
delta_res = (sum_avg_result / sum_res_star - 1) * 100;
} else writer.write(date("null") + newline());
//bestKnownVeh
Double delta_veh = null;
if(sum_veh_star != null){
writer.write(date(Double.valueOf(round(sum_veh_star.doubleValue()/(double)results.size(),2)).toString()) + newline());
delta_veh = (sum_avg_veh - sum_veh_star)/(double)results.size();
}
else writer.write(date("null") + newline());
if (sum_veh_star != null) {
writer.write(date(Double.valueOf(round(sum_veh_star.doubleValue() / (double) results.size(), 2)).toString()) + newline());
delta_veh = (sum_avg_veh - sum_veh_star) / (double) results.size();
} else writer.write(date("null") + newline());
writer.write(closeRow() + newline());
writer.write(closeTable() + newline());
writer.write("avg. percentage deviation to best-known result: " + round(delta_res,2) + newline() + newline());
writer.write("avg. absolute deviation to best-known vehicles: " + round(delta_veh,2) + newline());
writer.write("avg. percentage deviation to best-known result: " + round(delta_res, 2) + newline() + newline());
writer.write("avg. absolute deviation to best-known vehicles: " + round(delta_veh, 2) + newline());
writer.write(openTable() + newline());
writer.write(openRow() + newline());
@ -191,15 +189,15 @@ public class HtmlBenchmarkTableWriter implements BenchmarkWriter{
writer.write(date("") + newline());
writer.write(date("") + newline());
writer.write(date("") + newline());
writer.write(date(Double.valueOf(average_time).toString(),"align=\"right\"") + newline());
writer.write(date(Double.valueOf(average_result).toString(),"align=\"right\"") + newline());
writer.write(date(Double.valueOf(average_vehicles).toString(),"align=\"right\"") + newline());
if(delta_res != null){
writer.write(date(Double.valueOf(round(delta_res,2)).toString(),"align=\"right\"") + newline());
}else writer.write(date("n.a.") + newline());
if(delta_veh != null){
writer.write(date(Double.valueOf(round(delta_veh,2)).toString(),"align=\"right\"") + newline());
}else writer.write(date("n.a.") + newline());
writer.write(date(Double.valueOf(average_time).toString(), "align=\"right\"") + newline());
writer.write(date(Double.valueOf(average_result).toString(), "align=\"right\"") + newline());
writer.write(date(Double.valueOf(average_vehicles).toString(), "align=\"right\"") + newline());
if (delta_res != null) {
writer.write(date(Double.valueOf(round(delta_res, 2)).toString(), "align=\"right\"") + newline());
} else writer.write(date("n.a.") + newline());
if (delta_veh != null) {
writer.write(date(Double.valueOf(round(delta_veh, 2)).toString(), "align=\"right\"") + newline());
} else writer.write(date("n.a.") + newline());
writer.write(closeRow() + newline());
writer.write(closeTable() + newline());
@ -213,17 +211,17 @@ public class HtmlBenchmarkTableWriter implements BenchmarkWriter{
}
private String head(String string, int i) {
return "<th colspan=\""+i+"\">"+string+"</th>";
return "<th colspan=\"" + i + "\">" + string + "</th>";
}
private Double round(Double value, int i) {
if(value==null) return null;
long roundedVal = Math.round(value*Math.pow(10, i));
return (double)roundedVal/(double)(Math.pow(10, i));
if (value == null) return null;
long roundedVal = Math.round(value * Math.pow(10, i));
return (double) roundedVal / (double) (Math.pow(10, i));
}
private String head(String head) {
return "<th>"+head+"</th>";
return "<th>" + head + "</th>";
}
private String closeTable() {
@ -239,11 +237,11 @@ public class HtmlBenchmarkTableWriter implements BenchmarkWriter{
}
private String date(String date) {
return "<td>"+date+"</td>";
return "<td>" + date + "</td>";
}
private String date(String date, String metaData) {
return "<td " + metaData + ">"+date+"</td>";
return "<td " + metaData + ">" + date + "</td>";
}
private String newline() {
@ -255,5 +253,4 @@ public class HtmlBenchmarkTableWriter implements BenchmarkWriter{
}
}

View file

@ -1,4 +1,3 @@
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright (C) 2014 Stefan Schroeder
~
@ -16,17 +15,18 @@
~ License along with this library. If not, see <http://www.gnu.org/licenses />.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>jsprit</groupId>
<artifactId>jsprit</artifactId>
<version>1.6.1-SNAPSHOT</version>
<version>1.6.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jsprit-core</artifactId>
<name>jsprit-core</name>
<build>
<build>
<pluginManagement>
<plugins>
<plugin>
@ -46,7 +46,7 @@
</goals>
</pluginExecutionFilter>
<action>
<ignore />
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
@ -55,8 +55,37 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<excludedGroups>jsprit.core.IntegrationTest</excludedGroups>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/*.java</include>
</includes>
<groups>jsprit.core.IntegrationTest</groups>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
@ -77,8 +106,8 @@
<dependency>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
<version>2.4.0</version>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
<scope>compile</scope>
</dependency>

View file

@ -25,6 +25,7 @@ import jsprit.core.problem.solution.InitialSolutionFactory;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.Vehicle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -60,7 +61,11 @@ public final class InsertionInitialSolutionFactory implements InitialSolutionFac
}
private List<Job> getUnassignedJobs(VehicleRoutingProblem vrp) {
return new ArrayList<Job>(vrp.getJobs().values());
ArrayList<Job> jobs = new ArrayList<Job>(vrp.getJobs().values());
for (Vehicle v : vrp.getVehicles()) {
if (v.getBreak() != null) jobs.add(v.getBreak());
}
return jobs;
}
}

View file

@ -25,6 +25,7 @@ import jsprit.core.algorithm.recreate.VehicleSwitched;
import jsprit.core.algorithm.state.*;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.SwitchNotFeasible;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
@ -36,8 +37,8 @@ import jsprit.core.util.ActivityTimeTracker;
import java.util.*;
/**
* Created by schroeder on 10.12.14.
*/
* Created by schroeder on 10.12.14.
*/
public class PrettyAlgorithmBuilder {
private final VehicleRoutingProblem vrp;
@ -56,11 +57,11 @@ public class PrettyAlgorithmBuilder {
private boolean coreStuff = false;
public static PrettyAlgorithmBuilder newInstance(VehicleRoutingProblem vrp, VehicleFleetManager fleetManager, StateManager stateManager, ConstraintManager constraintManager){
return new PrettyAlgorithmBuilder(vrp,fleetManager,stateManager,constraintManager);
public static PrettyAlgorithmBuilder newInstance(VehicleRoutingProblem vrp, VehicleFleetManager fleetManager, StateManager stateManager, ConstraintManager constraintManager) {
return new PrettyAlgorithmBuilder(vrp, fleetManager, stateManager, constraintManager);
}
PrettyAlgorithmBuilder(VehicleRoutingProblem vrp, VehicleFleetManager fleetManager, StateManager stateManager, ConstraintManager constraintManager){
PrettyAlgorithmBuilder(VehicleRoutingProblem vrp, VehicleFleetManager fleetManager, StateManager stateManager, ConstraintManager constraintManager) {
this.vrp = vrp;
this.fleetManager = fleetManager;
this.stateManager = stateManager;
@ -68,40 +69,41 @@ public class PrettyAlgorithmBuilder {
this.searchStrategyManager = new SearchStrategyManager();
}
public PrettyAlgorithmBuilder setRandom(Random random){
public PrettyAlgorithmBuilder setRandom(Random random) {
searchStrategyManager.setRandom(random);
return this;
}
public PrettyAlgorithmBuilder withStrategy(SearchStrategy strategy, double weight){
searchStrategyManager.addStrategy(strategy,weight);
public PrettyAlgorithmBuilder withStrategy(SearchStrategy strategy, double weight) {
searchStrategyManager.addStrategy(strategy, weight);
return this;
}
public PrettyAlgorithmBuilder constructInitialSolutionWith(InsertionStrategy insertionStrategy, SolutionCostCalculator objFunction){
public PrettyAlgorithmBuilder constructInitialSolutionWith(InsertionStrategy insertionStrategy, SolutionCostCalculator objFunction) {
this.iniInsertionStrategy = insertionStrategy;
this.iniObjFunction = objFunction;
return this;
}
public VehicleRoutingAlgorithm build(){
if(coreStuff){
public VehicleRoutingAlgorithm build() {
if (coreStuff) {
constraintManager.addTimeWindowConstraint();
constraintManager.addLoadConstraint();
constraintManager.addSkillsConstraint();
constraintManager.addConstraint(new SwitchNotFeasible(stateManager));
stateManager.updateLoadStates();
stateManager.updateTimeWindowStates();
UpdateVehicleDependentPracticalTimeWindows tw_updater = new UpdateVehicleDependentPracticalTimeWindows(stateManager, vrp.getTransportCosts());
tw_updater.setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
UpdateVehicleDependentPracticalTimeWindows twUpdater = new UpdateVehicleDependentPracticalTimeWindows(stateManager, vrp.getTransportCosts());
twUpdater.setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
Map<VehicleTypeKey,Vehicle> uniqueTypes = new HashMap<VehicleTypeKey,Vehicle>();
Map<VehicleTypeKey, Vehicle> uniqueTypes = new HashMap<VehicleTypeKey, Vehicle>();
@Override
public Collection<Vehicle> get(VehicleRoute vehicleRoute) {
if(uniqueTypes.isEmpty()){
for( Vehicle v : vrp.getVehicles()){
if(!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())){
uniqueTypes.put(v.getVehicleTypeIdentifier(),v);
if (uniqueTypes.isEmpty()) {
for (Vehicle v : vrp.getVehicles()) {
if (!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())) {
uniqueTypes.put(v.getVehicleTypeIdentifier(), v);
}
}
}
@ -109,14 +111,16 @@ public class PrettyAlgorithmBuilder {
vehicles.addAll(uniqueTypes.values());
return vehicles;
}
});
stateManager.addStateUpdater(tw_updater);
stateManager.addStateUpdater(twUpdater);
stateManager.updateSkillStates();
stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen());
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(), ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS));
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
stateManager.addStateUpdater(new UpdateFutureWaitingTimes(stateManager, vrp.getTransportCosts()));
}
VehicleRoutingAlgorithm vra = new VehicleRoutingAlgorithm(vrp,searchStrategyManager);
VehicleRoutingAlgorithm vra = new VehicleRoutingAlgorithm(vrp, searchStrategyManager);
vra.addListener(stateManager);
RemoveEmptyVehicles removeEmptyVehicles = new RemoveEmptyVehicles(fleetManager);
ResetAndIniFleetManager resetAndIniFleetManager = new ResetAndIniFleetManager(fleetManager);
@ -124,7 +128,7 @@ public class PrettyAlgorithmBuilder {
vra.addListener(removeEmptyVehicles);
vra.addListener(resetAndIniFleetManager);
vra.addListener(vehicleSwitched);
if(iniInsertionStrategy != null) {
if (iniInsertionStrategy != null) {
if (!iniInsertionStrategy.getListeners().contains(removeEmptyVehicles))
iniInsertionStrategy.addListener(removeEmptyVehicles);
if (!iniInsertionStrategy.getListeners().contains(resetAndIniFleetManager))
@ -136,8 +140,10 @@ public class PrettyAlgorithmBuilder {
vra.addListener(new AlgorithmStartsListener() {
@Override
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
if (solutions.isEmpty()) {
solutions.add(new InsertionInitialSolutionFactory(iniInsertionStrategy, iniObjFunction).createSolution(vrp));
}
}
});
}
addArbitraryListener(vra);
@ -150,10 +156,10 @@ public class PrettyAlgorithmBuilder {
private void searchSchrimpfAndRegister(VehicleRoutingAlgorithm vra) {
boolean schrimpfAdded = false;
for(SearchStrategy strategy : vra.getSearchStrategyManager().getStrategies()){
for (SearchStrategy strategy : vra.getSearchStrategyManager().getStrategies()) {
SolutionAcceptor acceptor = strategy.getSolutionAcceptor();
if(acceptor instanceof SchrimpfAcceptance){
if(!schrimpfAdded) {
if (acceptor instanceof SchrimpfAcceptance) {
if (!schrimpfAdded) {
vra.addListener((SchrimpfAcceptance) acceptor);
schrimpfAdded = true;
}

View file

@ -25,7 +25,7 @@ import java.util.Collection;
import java.util.List;
public class RemoveEmptyVehicles implements InsertionEndsListener{
public class RemoveEmptyVehicles implements InsertionEndsListener {
private VehicleFleetManager fleetManager;
@ -42,8 +42,8 @@ public class RemoveEmptyVehicles implements InsertionEndsListener{
@Override
public void informInsertionEnds(Collection<VehicleRoute> vehicleRoutes) {
List<VehicleRoute> routes = new ArrayList<VehicleRoute>(vehicleRoutes);
for(VehicleRoute route : routes){
if(route.isEmpty()) {
for (VehicleRoute route : routes) {
if (route.isEmpty()) {
fleetManager.unlock(route.getVehicle());
vehicleRoutes.remove(route);
}

View file

@ -25,7 +25,7 @@ import java.util.ArrayList;
import java.util.Collection;
public class ResetAndIniFleetManager implements InsertionStartsListener{
public class ResetAndIniFleetManager implements InsertionStartsListener {
private VehicleFleetManager vehicleFleetManager;
@ -38,7 +38,7 @@ public class ResetAndIniFleetManager implements InsertionStartsListener{
public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) {
vehicleFleetManager.unlockAll();
Collection<VehicleRoute> routes = new ArrayList<VehicleRoute>(vehicleRoutes);
for(VehicleRoute route : routes){
for (VehicleRoute route : routes) {
vehicleFleetManager.lock(route.getVehicle());
}
}

View file

@ -60,11 +60,13 @@ public class SearchStrategy {
return strategyId;
}
public String getStrategyId() { return strategyId; }
public String getStrategyId() {
return strategyId;
}
@Override
public String toString() {
return "[strategyId="+strategyId+"][solution="+solution+"][accepted="+accepted+"]";
return "[strategyId=" + strategyId + "][solution=" + solution + "][accepted=" + accepted + "]";
}
}
@ -83,12 +85,12 @@ public class SearchStrategy {
private String name;
public SearchStrategy(String id, SolutionSelector solutionSelector, SolutionAcceptor solutionAcceptor, SolutionCostCalculator solutionCostCalculator) {
if(id == null) throw new IllegalStateException("strategy id cannot be null");
if (id == null) throw new IllegalStateException("strategy id cannot be null");
this.solutionSelector = solutionSelector;
this.solutionAcceptor = solutionAcceptor;
this.solutionCostCalculator = solutionCostCalculator;
this.id = id;
logger.debug("initialise " + this);
logger.debug("initialise {}", this);
}
public String getId() {
@ -119,12 +121,12 @@ public class SearchStrategy {
@Override
public String toString() {
return "searchStrategy [#modules="+searchStrategyModules.size()+"][selector="+solutionSelector+"][acceptor="+solutionAcceptor+"]";
return "searchStrategy [#modules=" + searchStrategyModules.size() + "][selector=" + solutionSelector + "][acceptor=" + solutionAcceptor + "]";
}
/**
* Runs the search-strategy and its according modules, and returns DiscoveredSolution.
*
* <p/>
* <p>This involves three basic steps: 1) Selecting a solution from solutions (input parameter) according to {@link jsprit.core.algorithm.selector.SolutionSelector}, 2) running the modules
* ({@link jsprit.core.algorithm.SearchStrategyModule}) on the selectedSolution and 3) accepting the new solution according to {@link jsprit.core.algorithm.acceptor.SolutionAcceptor}.
* <p> Note that after 1) the selected solution is copied, thus the original solution is not modified.
@ -136,11 +138,11 @@ public class SearchStrategy {
* @throws java.lang.IllegalStateException if selector cannot select any solution
*/
@SuppressWarnings("UnusedParameters")
public DiscoveredSolution run(VehicleRoutingProblem vrp, Collection<VehicleRoutingProblemSolution> solutions){
public DiscoveredSolution run(VehicleRoutingProblem vrp, Collection<VehicleRoutingProblemSolution> solutions) {
VehicleRoutingProblemSolution solution = solutionSelector.selectSolution(solutions);
if(solution == null) throw new IllegalStateException(getErrMsg());
if (solution == null) throw new IllegalStateException(getErrMsg());
VehicleRoutingProblemSolution lastSolution = VehicleRoutingProblemSolution.copyOf(solution);
for(SearchStrategyModule module : searchStrategyModules){
for (SearchStrategyModule module : searchStrategyModules) {
lastSolution = module.runAndGetSolution(lastSolution);
}
double costs = solutionCostCalculator.getCosts(lastSolution);
@ -156,14 +158,14 @@ public class SearchStrategy {
}
public void addModule(SearchStrategyModule module){
if(module == null) throw new IllegalStateException("module to be added is null.");
public void addModule(SearchStrategyModule module) {
if (module == null) throw new IllegalStateException("module to be added is null.");
searchStrategyModules.add(module);
logger.debug("module added [module=" + module + "][#modules=" + searchStrategyModules.size() + "]");
logger.debug("module added [module={}][#modules={}]", module, searchStrategyModules.size());
}
public void addModuleListener(SearchStrategyModuleListener moduleListener) {
for(SearchStrategyModule module : searchStrategyModules){
for (SearchStrategyModule module : searchStrategyModules) {
module.addModuleListener(moduleListener);
}

View file

@ -35,7 +35,7 @@ public class SearchStrategyManager {
private List<Double> weights = new ArrayList<Double>();
private Map<String, Integer> id2index = new HashMap<String,Integer>();
private Map<String, Integer> id2index = new HashMap<String, Integer>();
private Random random = RandomNumberGeneration.getRandom();
@ -55,6 +55,7 @@ public class SearchStrategyManager {
* Returns the probabilities.
* [schroeder (2014.11.21): Now they are actually no propabilities anymore but weights. The resulting probabilities
* are calculated here with the sum of weights]
*
* @return list of probabilities
*/
@Deprecated
@ -62,36 +63,39 @@ public class SearchStrategyManager {
return Collections.unmodifiableList(weights);
}
public List<Double> getWeights(){ return Collections.unmodifiableList(weights); }
public List<Double> getWeights() {
return Collections.unmodifiableList(weights);
}
public double getWeight(String strategyId){
public double getWeight(String strategyId) {
return weights.get(id2index.get(strategyId));
}
/**
* adds a new search strategy with a certain weight.
*
* @param strategy strategy to be added
* @param weight of corresponding strategy to be added
* @throws java.lang.IllegalStateException if strategy is null OR weight < 0
*/
public void addStrategy(SearchStrategy strategy, double weight){
if(strategy == null){
public void addStrategy(SearchStrategy strategy, double weight) {
if (strategy == null) {
throw new IllegalStateException("strategy is null. make sure adding a valid strategy.");
}
if(id2index.keySet().contains(strategy.getId())){
if (id2index.keySet().contains(strategy.getId())) {
throw new IllegalStateException("strategyId " + strategy.getId() + " already in use. replace strateId in your config file or code with a unique strategy id");
}
if(weight < 0.0){
if (weight < 0.0) {
throw new IllegalStateException("weight is lower than zero.");
}
id2index.put(strategy.getId(),strategyIndex);
id2index.put(strategy.getId(), strategyIndex);
strategyIndex++;
strategies.add(strategy);
weights.add(weight);
sumWeights += weight;
}
public void informStrategyWeightChanged(String strategyId, double weight){
public void informStrategyWeightChanged(String strategyId, double weight) {
int strategyIndex = id2index.get(strategyId);
weights.set(strategyIndex, weight);
updateSumWeights();
@ -99,7 +103,7 @@ public class SearchStrategyManager {
private void updateSumWeights() {
double sum = 0.;
for(double w : weights){
for (double w : weights) {
sum += w;
}
sumWeights = sum;
@ -112,7 +116,8 @@ public class SearchStrategyManager {
* @throws java.lang.IllegalStateException if randomNumberGenerator is null OR no search strategy can be found
*/
public SearchStrategy getRandomStrategy() {
if(random == null) throw new IllegalStateException("randomizer is null. make sure you set random object correctly");
if (random == null)
throw new IllegalStateException("randomizer is null. make sure you set random object correctly");
double randomFig = random.nextDouble();
double sumProbabilities = 0.0;
for (int i = 0; i < weights.size(); i++) {
@ -124,12 +129,12 @@ public class SearchStrategyManager {
throw new IllegalStateException("no search-strategy found");
}
public void addSearchStrategyListener(SearchStrategyListener strategyListener){
public void addSearchStrategyListener(SearchStrategyListener strategyListener) {
searchStrategyListeners.add(strategyListener);
}
public void addSearchStrategyModuleListener(SearchStrategyModuleListener moduleListener){
for(SearchStrategy s : strategies){
public void addSearchStrategyModuleListener(SearchStrategyModuleListener moduleListener) {
for (SearchStrategy s : strategies) {
s.addModuleListener(moduleListener);
}
}

View file

@ -31,7 +31,6 @@ import jsprit.core.problem.vehicle.Vehicle;
* this objective function.
*
* @author schroeder
*
*/
public class VariablePlusFixedSolutionCostCalculatorFactory {
@ -42,13 +41,13 @@ public class VariablePlusFixedSolutionCostCalculatorFactory {
this.stateManager = stateManager;
}
public SolutionCostCalculator createCalculator(){
public SolutionCostCalculator createCalculator() {
return new SolutionCostCalculator() {
@Override
public double getCosts(VehicleRoutingProblemSolution solution) {
double c = 0.0;
for(VehicleRoute r : solution.getRoutes()){
for (VehicleRoute r : solution.getRoutes()) {
c += stateManager.getRouteState(r, InternalStates.COSTS, Double.class);
c += getFixedCosts(r.getVehicle());
}
@ -57,8 +56,8 @@ public class VariablePlusFixedSolutionCostCalculatorFactory {
}
private double getFixedCosts(Vehicle vehicle) {
if(vehicle == null) return 0.0;
if(vehicle.getType() == null) return 0.0;
if (vehicle == null) return 0.0;
if (vehicle.getType() == null) return 0.0;
return vehicle.getType().getVehicleCostParams().fix;
}
};

View file

@ -20,6 +20,7 @@ import jsprit.core.algorithm.SearchStrategy.DiscoveredSolution;
import jsprit.core.algorithm.listener.*;
import jsprit.core.algorithm.termination.PrematureAlgorithmTermination;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
@ -35,7 +36,6 @@ import java.util.Collection;
* Algorithm that solves a {@link VehicleRoutingProblem}.
*
* @author stefan schroeder
*
*/
public class VehicleRoutingAlgorithm {
@ -43,14 +43,14 @@ public class VehicleRoutingAlgorithm {
private Collection<PrematureAlgorithmTermination> terminationCriteria = new ArrayList<PrematureAlgorithmTermination>();
void addTermination(PrematureAlgorithmTermination termination){
void addTermination(PrematureAlgorithmTermination termination) {
terminationCriteria.add(termination);
}
@Override
public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) {
for(PrematureAlgorithmTermination termination : terminationCriteria){
if(termination.isPrematureBreak(discoveredSolution)){
for (PrematureAlgorithmTermination termination : terminationCriteria) {
if (termination.isPrematureBreak(discoveredSolution)) {
return true;
}
}
@ -72,14 +72,14 @@ public class VehicleRoutingAlgorithm {
long i = counter++;
long n = nextCounter;
if (i >= n) {
nextCounter=n*2;
nextCounter = n * 2;
log.info(this.name + n);
}
}
public void reset() {
counter=0;
nextCounter=1;
counter = 0;
nextCounter = 1;
}
}
@ -120,19 +120,19 @@ public class VehicleRoutingAlgorithm {
*
* @param solution the solution to be added
*/
public void addInitialSolution(VehicleRoutingProblemSolution solution){
public void addInitialSolution(VehicleRoutingProblemSolution solution) {
verify(solution);
initialSolutions.add(solution);
}
private void verify(VehicleRoutingProblemSolution solution) {
int nuJobs = 0;
for(VehicleRoute route : solution.getRoutes()){
for (VehicleRoute route : solution.getRoutes()) {
nuJobs += route.getTourActivities().getJobs().size();
if(route.getVehicle().getIndex() == 0)
if (route.getVehicle().getIndex() == 0)
throw new IllegalStateException("vehicle used in initial solution has no index. probably a vehicle is used that has not been added to the " +
" the VehicleRoutingProblem. only use vehicles that have already been added to the problem.");
for(TourActivity act : route.getActivities()) {
for (TourActivity act : route.getActivities()) {
if (act.getIndex() == 0) {
throw new IllegalStateException("act in initial solution has no index. activities are created and associated to their job in VehicleRoutingProblem\n." +
" thus if you build vehicle-routes use the jobActivityFactory from vehicle routing problem like that \n" +
@ -141,9 +141,9 @@ public class VehicleRoutingAlgorithm {
}
}
}
if(nuJobs != problem.getJobs().values().size()){
logger.warn("number of jobs in initial solution (" + nuJobs + ") is not equal nuJobs in vehicle routing problem (" + problem.getJobs().values().size() + ")" +
"\n this might yield unintended effects, e.g. initial solution cannot be improved anymore.");
if (nuJobs != problem.getJobs().values().size()) {
logger.warn("number of jobs in initial solution ({}) is not equal nuJobs in vehicle routing problem ({})" +
"\n this might yield unintended effects, e.g. initial solution cannot be improved anymore.", nuJobs, problem.getJobs().values().size());
}
}
@ -153,7 +153,7 @@ public class VehicleRoutingAlgorithm {
*
* @param prematureAlgorithmTermination the termination criterion
*/
public void setPrematureAlgorithmTermination(PrematureAlgorithmTermination prematureAlgorithmTermination){
public void setPrematureAlgorithmTermination(PrematureAlgorithmTermination prematureAlgorithmTermination) {
terminationManager = new TerminationManager();
terminationManager.addTermination(prematureAlgorithmTermination);
}
@ -164,7 +164,7 @@ public class VehicleRoutingAlgorithm {
*
* @param terminationCriterion the termination criterion
*/
public void addTerminationCriterion(PrematureAlgorithmTermination terminationCriterion){
public void addTerminationCriterion(PrematureAlgorithmTermination terminationCriterion) {
terminationManager.addTermination(terminationCriterion);
}
@ -179,7 +179,7 @@ public class VehicleRoutingAlgorithm {
/**
* Runs the vehicle routing algorithm and returns a number of generated solutions.
*
* <p/>
* <p>The algorithm runs as long as it is specified in nuOfIterations and prematureBreak. In each iteration it selects a searchStrategy according
* to searchStrategyManager and runs the strategy to improve solutions.
* <p>Note that clients are allowed to observe/listen the algorithm. See {@link VehicleRoutingAlgorithmListener} and its according listeners.
@ -187,51 +187,85 @@ public class VehicleRoutingAlgorithm {
* @return Collection<VehicleRoutingProblemSolution> the solutions
* @see {@link SearchStrategyManager}, {@link VehicleRoutingAlgorithmListener}, {@link AlgorithmStartsListener}, {@link AlgorithmEndsListener}, {@link IterationStartsListener}, {@link IterationEndsListener}
*/
public Collection<VehicleRoutingProblemSolution> searchSolutions(){
logger.info("algorithm starts: " + "[maxIterations=" + maxIterations + "]");
public Collection<VehicleRoutingProblemSolution> searchSolutions() {
logger.info("algorithm starts: [maxIterations={}]", maxIterations);
double now = System.currentTimeMillis();
int noIterationsThisAlgoIsRunning = maxIterations;
counter.reset();
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
algorithmStarts(problem,solutions);
algorithmStarts(problem, solutions);
bestEver = Solutions.bestOf(solutions);
if (logger.isTraceEnabled()) log(solutions);
logger.info("iterations start");
for(int i=0;i< maxIterations;i++){
iterationStarts(i+1,problem,solutions);
logger.debug("start iteration: " + i);
for (int i = 0; i < maxIterations; i++) {
iterationStarts(i + 1, problem, solutions);
logger.debug("start iteration: {}", i);
counter.incCounter();
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
DiscoveredSolution discoveredSolution = strategy.run(problem, solutions);
logger.trace("discovered solution: " + discoveredSolution);
if (logger.isTraceEnabled()) log(discoveredSolution);
memorizeIfBestEver(discoveredSolution);
selectedStrategy(discoveredSolution,problem,solutions);
if(terminationManager.isPrematureBreak(discoveredSolution)){
logger.info("premature algorithm termination at iteration "+ (i+1));
noIterationsThisAlgoIsRunning = (i+1);
selectedStrategy(discoveredSolution, problem, solutions);
if (terminationManager.isPrematureBreak(discoveredSolution)) {
logger.info("premature algorithm termination at iteration {}", (i + 1));
noIterationsThisAlgoIsRunning = (i + 1);
break;
}
iterationEnds(i+1,problem,solutions);
iterationEnds(i + 1, problem, solutions);
}
logger.info("iterations end at " + noIterationsThisAlgoIsRunning + " iterations");
logger.info("iterations end at {} iterations", noIterationsThisAlgoIsRunning);
addBestEver(solutions);
algorithmEnds(problem, solutions);
logger.info("took " + ((System.currentTimeMillis()-now)/1000.0) + " seconds");
logger.info("took {} seconds", ((System.currentTimeMillis() - now) / 1000.0));
return solutions;
}
private void addBestEver(Collection<VehicleRoutingProblemSolution> solutions) {
if(bestEver != null) solutions.add(bestEver);
if (bestEver != null) solutions.add(bestEver);
}
private void log(Collection<VehicleRoutingProblemSolution> solutions) {
for (VehicleRoutingProblemSolution sol : solutions) log(sol);
}
private void log(VehicleRoutingProblemSolution solution) {
logger.trace("solution costs: {}", solution.getCost());
for (VehicleRoute r : solution.getRoutes()) {
StringBuilder b = new StringBuilder();
b.append(r.getVehicle().getId()).append(" : ").append("[ ");
for (TourActivity act : r.getActivities()) {
if (act instanceof TourActivity.JobActivity) {
b.append(((TourActivity.JobActivity) act).getJob().getId()).append(" ");
}
}
b.append("]");
logger.trace(b.toString());
}
StringBuilder b = new StringBuilder();
b.append("unassigned : [ ");
for (Job j : solution.getUnassignedJobs()) {
b.append(j.getId()).append(" ");
}
b.append("]");
logger.trace(b.toString());
}
private void log(DiscoveredSolution discoveredSolution) {
logger.trace("discovered solution: {}", discoveredSolution);
log(discoveredSolution.getSolution());
}
private void memorizeIfBestEver(DiscoveredSolution discoveredSolution) {
if(discoveredSolution == null) return;
if(bestEver == null) bestEver = discoveredSolution.getSolution();
else if(discoveredSolution.getSolution().getCost() < bestEver.getCost()) bestEver = discoveredSolution.getSolution();
if (discoveredSolution == null) return;
if (bestEver == null) bestEver = discoveredSolution.getSolution();
else if (discoveredSolution.getSolution().getCost() < bestEver.getCost())
bestEver = discoveredSolution.getSolution();
}
private void selectedStrategy(DiscoveredSolution discoveredSolution, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
algoListeners.selectedStrategy(discoveredSolution,problem,solutions);
algoListeners.selectedStrategy(discoveredSolution, problem, solutions);
}
private void algorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
@ -242,14 +276,16 @@ public class VehicleRoutingAlgorithm {
return algoListeners;
}
public void addListener(VehicleRoutingAlgorithmListener l){
public void addListener(VehicleRoutingAlgorithmListener l) {
algoListeners.addListener(l);
if(l instanceof SearchStrategyListener) searchStrategyManager.addSearchStrategyListener((SearchStrategyListener) l);
if(l instanceof SearchStrategyModuleListener) searchStrategyManager.addSearchStrategyModuleListener((SearchStrategyModuleListener) l);
if (l instanceof SearchStrategyListener)
searchStrategyManager.addSearchStrategyListener((SearchStrategyListener) l);
if (l instanceof SearchStrategyModuleListener)
searchStrategyManager.addSearchStrategyModuleListener((SearchStrategyModuleListener) l);
}
private void iterationEnds(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
algoListeners.iterationEnds(i,problem, solutions);
algoListeners.iterationEnds(i, problem, solutions);
}
private void iterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
@ -267,7 +303,7 @@ public class VehicleRoutingAlgorithm {
*/
public void setMaxIterations(int maxIterations) {
this.maxIterations = maxIterations;
logger.debug("set maxIterations to " + this.maxIterations);
logger.debug("set maxIterations to {}", this.maxIterations);
}
/**

View file

@ -29,7 +29,6 @@ import jsprit.core.problem.solution.SolutionCostCalculator;
* Builder that builds a {@link VehicleRoutingAlgorithm}.
*
* @author schroeder
*
*/
public class VehicleRoutingAlgorithmBuilder {
@ -49,7 +48,7 @@ public class VehicleRoutingAlgorithmBuilder {
private ConstraintManager constraintManager;
private int nuOfThreads=0;
private int nuOfThreads = 0;
/**
* Constructs the builder with the problem and an algorithmConfigFile. Latter is to configure and specify the ruin-and-recreate meta-heuristic.
@ -58,9 +57,9 @@ public class VehicleRoutingAlgorithmBuilder {
* @param algorithmConfig config file of VehicleRoutingAlgorithm
*/
public VehicleRoutingAlgorithmBuilder(VehicleRoutingProblem problem, String algorithmConfig) {
this.vrp=problem;
this.algorithmConfigFile=algorithmConfig;
this.algorithmConfig=null;
this.vrp = problem;
this.algorithmConfigFile = algorithmConfig;
this.algorithmConfig = null;
}
/**
@ -70,14 +69,14 @@ public class VehicleRoutingAlgorithmBuilder {
* @param algorithmConfig config file of VehicleRoutingAlgorithm
*/
public VehicleRoutingAlgorithmBuilder(VehicleRoutingProblem problem, AlgorithmConfig algorithmConfig) {
this.vrp=problem;
this.algorithmConfigFile=null;
this.algorithmConfig=algorithmConfig;
this.vrp = problem;
this.algorithmConfigFile = null;
this.algorithmConfig = algorithmConfig;
}
/**
* Sets custom objective function.
*
* <p/>
* <p>If objective function is not set, a default function is applied (which basically minimizes
* fixed and variable transportation costs ({@link VariablePlusFixedSolutionCostCalculatorFactory}).
*
@ -95,18 +94,17 @@ public class VehicleRoutingAlgorithmBuilder {
* @see StateManager
*/
public void setStateManager(StateManager stateManager) {
this.stateManager=stateManager;
this.stateManager = stateManager;
}
/**
* Adds core constraints.
*
* <p/>
* <p>Thus, it adds vehicle-capacity, time-window and skills constraints and their
* required stateUpdater.
*
*/
public void addCoreConstraints() {
addCoreConstraints=true;
addCoreConstraints = true;
}
/**
@ -115,12 +113,12 @@ public class VehicleRoutingAlgorithmBuilder {
* By default, marginal transportation costs are calculated. Thus when inserting
* act_k between act_i and act_j, marginal (additional) transportation costs
* are basically c(act_i,act_k)+c(act_k,act_j)-c(act_i,act_j).
*
* <p/>
* <p>Do not use this method, if you plan to control the insertion heuristic
* entirely via hard- and soft-constraints.
*/
public void addDefaultCostCalculators() {
addDefaultCostCalculators=true;
addDefaultCostCalculators = true;
}
/**
@ -132,8 +130,8 @@ public class VehicleRoutingAlgorithmBuilder {
* @see ConstraintManager
*/
public void setStateAndConstraintManager(StateManager stateManager, ConstraintManager constraintManager) {
this.stateManager=stateManager;
this.constraintManager=constraintManager;
this.stateManager = stateManager;
this.constraintManager = constraintManager;
}
/**
@ -141,25 +139,25 @@ public class VehicleRoutingAlgorithmBuilder {
*
* @param nuOfThreads to be operated
*/
public void setNuOfThreads(int nuOfThreads){
this.nuOfThreads=nuOfThreads;
public void setNuOfThreads(int nuOfThreads) {
this.nuOfThreads = nuOfThreads;
}
/**
* Builds and returns the algorithm.
*
* <p/>
* <p>If algorithmConfigFile is set, it reads the configuration.
*
* @return the algorithm
*/
public VehicleRoutingAlgorithm build() {
if(stateManager == null) stateManager = new StateManager(vrp);
if(constraintManager == null) constraintManager = new ConstraintManager(vrp,stateManager);
if (stateManager == null) stateManager = new StateManager(vrp);
if (constraintManager == null) constraintManager = new ConstraintManager(vrp, stateManager);
//add core updater
stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen());
// stateManager.addStateUpdater(new OpenRouteStateVerifier());
if(addCoreConstraints){
if (addCoreConstraints) {
constraintManager.addLoadConstraint();
constraintManager.addTimeWindowConstraint();
constraintManager.addSkillsConstraint();
@ -167,7 +165,7 @@ public class VehicleRoutingAlgorithmBuilder {
stateManager.updateTimeWindowStates();
stateManager.updateSkillStates();
}
if(algorithmConfig==null){
if (algorithmConfig == null) {
algorithmConfig = new AlgorithmConfig();
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
xmlReader.read(algorithmConfigFile);

View file

@ -24,18 +24,18 @@ import java.util.Collection;
* @deprecated use GreedyAcceptance instead
*/
@Deprecated
public class AcceptNewRemoveFirst implements SolutionAcceptor{
public class AcceptNewRemoveFirst implements SolutionAcceptor {
private final int solutionMemory;
public AcceptNewRemoveFirst(int solutionMemory){
public AcceptNewRemoveFirst(int solutionMemory) {
this.solutionMemory = solutionMemory;
}
/**
* Accepts every solution if solution memory allows. If memory occupied, than accepts new solution only if better than the worst in memory.
* Consequently, the worst solution is removed from solutions, and the new solution added.
*
* <p/>
* <p>Note that this modifies Collection<VehicleRoutingProblemSolution> solutions.
*/
@Override
@ -53,6 +53,4 @@ public class AcceptNewRemoveFirst implements SolutionAcceptor{
}
}

View file

@ -35,7 +35,7 @@ import java.net.URL;
import java.util.Collection;
public class ExperimentalSchrimpfAcceptance implements SolutionAcceptor, IterationStartsListener, AlgorithmStartsListener{
public class ExperimentalSchrimpfAcceptance implements SolutionAcceptor, IterationStartsListener, AlgorithmStartsListener {
final static Logger logger = LogManager.getLogger(ExperimentalSchrimpfAcceptance.class.getName());
@ -57,11 +57,10 @@ public class ExperimentalSchrimpfAcceptance implements SolutionAcceptor, Iterati
this.alpha = alpha;
this.nOfRandomWalks = nOfWarmupIterations;
this.solutionMemory = solutionMemory;
logger.info("initialise " + this);
logger.info("initialise {}", this);
}
@Override
public boolean acceptSolution(Collection<VehicleRoutingProblemSolution> solutions, VehicleRoutingProblemSolution newSolution) {
boolean solutionAccepted = false;
@ -71,16 +70,15 @@ public class ExperimentalSchrimpfAcceptance implements SolutionAcceptor, Iterati
} else {
VehicleRoutingProblemSolution worst = null;
double threshold = getThreshold(currentIteration);
for(VehicleRoutingProblemSolution solutionInMemory : solutions){
if(worst == null) worst = solutionInMemory;
else if(solutionInMemory.getCost() > worst.getCost()) worst = solutionInMemory;
for (VehicleRoutingProblemSolution solutionInMemory : solutions) {
if (worst == null) worst = solutionInMemory;
else if (solutionInMemory.getCost() > worst.getCost()) worst = solutionInMemory;
}
if(newSolution.getRoutes().size() < worst.getRoutes().size()){
if (newSolution.getRoutes().size() < worst.getRoutes().size()) {
solutions.remove(worst);
solutions.add(newSolution);
solutionAccepted = true;
}
else if(newSolution.getRoutes().size() == worst.getRoutes().size() && newSolution.getCost() < worst.getCost() + threshold){
} else if (newSolution.getRoutes().size() == worst.getRoutes().size() && newSolution.getCost() < worst.getCost() + threshold) {
solutions.remove(worst);
solutions.add(newSolution);
solutionAccepted = true;
@ -91,12 +89,12 @@ public class ExperimentalSchrimpfAcceptance implements SolutionAcceptor, Iterati
@Override
public String toString() {
return "[name=schrimpfAcceptanceFunction][alpha="+alpha+"][warmup=" + nOfRandomWalks + "]";
return "[name=schrimpfAcceptanceFunction][alpha=" + alpha + "][warmup=" + nOfRandomWalks + "]";
}
private double getThreshold(int iteration) {
double scheduleVariable = (double) iteration / (double) nOfTotalIterations;
// logger.debug("iter="+iteration+" totalIter="+nOfTotalIterations+" scheduling="+scheduleVariable);
// logger.debug("iter={} totalIter={} scheduling={}", iteration, nOfTotalIterations, scheduleVariable);
double currentThreshold = initialThreshold * Math.exp(-Math.log(2) * scheduleVariable / alpha);
return currentThreshold;
}
@ -126,8 +124,8 @@ public class ExperimentalSchrimpfAcceptance implements SolutionAcceptor, Iterati
@Override
public void informIterationEnds(int iteration, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
double result = Solutions.bestOf(solutions).getCost();
// logger.info("result="+result);
results[iteration-1] = result;
// logger.info("result={}", result);
results[iteration - 1] = result;
}
});
@ -138,8 +136,8 @@ public class ExperimentalSchrimpfAcceptance implements SolutionAcceptor, Iterati
initialThreshold = standardDeviation / 2;
logger.info("warmup done");
logger.info("total time: " + ((System.currentTimeMillis()-now)/1000.0) + "s");
logger.info("initial threshold: " + initialThreshold);
logger.info("total time: {}s", ((System.currentTimeMillis() - now) / 1000.0));
logger.info("initial threshold: {}", initialThreshold);
logger.info("---------------------------------------------------------------------");
}

View file

@ -23,22 +23,22 @@ import java.util.Collection;
/**
* Acceptor that accepts solutions to be memorized only better solutions.
*
* <p/>
* <p>If there is enough memory, every solution will be accepted. If there is no memory anymore and the solution
* to be evaluated is better than the worst, the worst will be replaced by the new solution.</p>
*/
public class GreedyAcceptance implements SolutionAcceptor{
public class GreedyAcceptance implements SolutionAcceptor {
private final int solutionMemory;
public GreedyAcceptance(int solutionMemory){
public GreedyAcceptance(int solutionMemory) {
this.solutionMemory = solutionMemory;
}
/**
* Accepts every solution if solution memory allows. If memory occupied, than accepts new solution only if better than the worst in memory.
* Consequently, the worst solution is removed from solutions, and the new solution added.
*
* <p/>
* <p>Note that this modifies Collection<VehicleRoutingProblemSolution> solutions.
*/
@Override
@ -53,7 +53,7 @@ public class GreedyAcceptance implements SolutionAcceptor{
if (worstSolution == null) worstSolution = s;
else if (s.getCost() > worstSolution.getCost()) worstSolution = s;
}
if(newSolution.getCost() < worstSolution.getCost()){
if (newSolution.getCost() < worstSolution.getCost()) {
solutions.remove(worstSolution);
solutions.add(newSolution);
solutionAccepted = true;
@ -68,6 +68,4 @@ public class GreedyAcceptance implements SolutionAcceptor{
}
}

View file

@ -22,18 +22,18 @@ import java.util.Collection;
@Deprecated
public class GreedyAcceptance_minVehFirst implements SolutionAcceptor{
public class GreedyAcceptance_minVehFirst implements SolutionAcceptor {
private final int solutionMemory;
public GreedyAcceptance_minVehFirst(int solutionMemory){
public GreedyAcceptance_minVehFirst(int solutionMemory) {
this.solutionMemory = solutionMemory;
}
/**
* Accepts every solution if solution memory allows. If memory occupied, than accepts new solution only if better than the worst in memory.
* Consequently, the worst solution is removed from solutions, and the new solution added.
*
* <p/>
* <p>Note that this modifies Collection<VehicleRoutingProblemSolution> solutions.
*/
@Override
@ -48,12 +48,11 @@ public class GreedyAcceptance_minVehFirst implements SolutionAcceptor{
if (worstSolution == null) worstSolution = s;
else if (s.getRoutes().size() > worstSolution.getRoutes().size()) worstSolution = s;
}
if(newSolution.getRoutes().size() < worstSolution.getRoutes().size()){
if (newSolution.getRoutes().size() < worstSolution.getRoutes().size()) {
solutions.remove(worstSolution);
solutions.add(newSolution);
solutionAccepted = true;
}
else if(newSolution.getRoutes().size() == worstSolution.getRoutes().size() && newSolution.getCost() < worstSolution.getCost()){
} else if (newSolution.getRoutes().size() == worstSolution.getRoutes().size() && newSolution.getCost() < worstSolution.getCost()) {
solutions.remove(worstSolution);
solutions.add(newSolution);
solutionAccepted = true;
@ -68,6 +67,4 @@ public class GreedyAcceptance_minVehFirst implements SolutionAcceptor{
}
}

View file

@ -29,21 +29,21 @@ import java.util.Collection;
/**
* ThresholdAcceptance-Function defined by Schrimpf et al. (2000).
*
* <p/>
* <p>The <b>idea</b> can be described as follows: Most problems do not only have one unique minimum (maximum) but
* a number of local minima (maxima). To avoid to get stuck in a local minimum at the beginning of a search
* this threshold-acceptance function accepts also worse solution at the beginning (in contrary to a greedy
* approach which only accepts better solutions), and converges to a greedy approach at the end. <br>
* The difficulty is to define (i) an appropriate initial threshold and (ii) a corresponding function describing
* how the threshold converges to zero, i.e. the greedy threshold.
*
* <p/>
* <p>ad i) The initial threshold is determined by a random walk through the search space.
* The random walk currently runs with the following algorithm: src/main/resources/randomWalk.xml. It runs
* as long as it is specified in nuOfWarmupIterations. In the first iteration or walk respectively the algorithm generates a solution.
* This solution in turn is the basis of the next walk yielding to another solution value ... and so on.
* Each solution value is memorized since the initial threshold is essentially a function of the standard deviation of these solution values.
* To be more precise: initial threshold = stddev(solution values) / 2.
*
* <p/>
* <p>ad ii) The threshold of iteration i is determined as follows:
* threshold(i) = initialThreshold * Math.exp(-Math.log(2) * (i / nuOfTotalIterations) / alpha)
* To get a better understanding of the threshold-function go to Wolfram Alpha and plot the following line
@ -55,17 +55,15 @@ import java.util.Collection;
* alpha = 0.1<br>
* x corresponds to i iterations and<br>
* y to the threshold(i)
*
* <p/>
* <p>Gerhard Schrimpf, Johannes Schneider, Hermann Stamm- Wilbrandt, and Gunter Dueck (2000).
* Record breaking optimization results using the ruin and recreate principle.
* Journal of Computational Physics, 159(2):139 171, 2000. ISSN 0021-9991. doi: 10.1006/jcph.1999. 6413.
* URL http://www.sciencedirect.com/science/article/ pii/S0021999199964136
*
*
* @author schroeder
*
*/
public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsListener, AlgorithmStartsListener{
public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsListener, AlgorithmStartsListener {
private static Logger logger = LogManager.getLogger(SchrimpfAcceptance.class.getName());
@ -80,10 +78,10 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
private final int solutionMemory;
public SchrimpfAcceptance(int solutionMemory, double alpha){
public SchrimpfAcceptance(int solutionMemory, double alpha) {
this.alpha = alpha;
this.solutionMemory = solutionMemory;
logger.debug("initialise " + this);
logger.debug("initialise {}", this);
}
@Override
@ -95,15 +93,14 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
} else {
VehicleRoutingProblemSolution worst = null;
double threshold = getThreshold(currentIteration);
for(VehicleRoutingProblemSolution solutionInMemory : solutions){
if(worst == null) worst = solutionInMemory;
else if(solutionInMemory.getCost() > worst.getCost()) worst = solutionInMemory;
for (VehicleRoutingProblemSolution solutionInMemory : solutions) {
if (worst == null) worst = solutionInMemory;
else if (solutionInMemory.getCost() > worst.getCost()) worst = solutionInMemory;
}
if(worst == null){
if (worst == null) {
solutions.add(newSolution);
solutionAccepted = true;
}
else if(newSolution.getCost() < worst.getCost() + threshold){
} else if (newSolution.getCost() < worst.getCost() + threshold) {
solutions.remove(worst);
solutions.add(newSolution);
solutionAccepted = true;
@ -114,7 +111,7 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
@Override
public String toString() {
return "[name=SchrimpfAcceptance][alpha="+alpha+"]";
return "[name=SchrimpfAcceptance][alpha=" + alpha + "]";
}
private double getThreshold(int iteration) {
@ -124,7 +121,7 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
@SuppressWarnings("UnusedDeclaration")
public double getInitialThreshold(){
public double getInitialThreshold() {
return initialThreshold;
}

View file

@ -48,7 +48,7 @@ public class SchrimpfInitialThresholdGenerator implements AlgorithmStartsListene
}
@Override
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm,Collection<VehicleRoutingProblemSolution> solutions) {
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
logger.info("prepare schrimpfAcceptanceFunction, i.e. determine initial threshold");
double now = System.currentTimeMillis();
@ -67,8 +67,8 @@ public class SchrimpfInitialThresholdGenerator implements AlgorithmStartsListene
@Override
public void informIterationEnds(int iteration, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
double result = Solutions.bestOf(solutions).getCost();
// logger.info("result="+result);
results[iteration-1] = result;
// logger.info("result={}", result);
results[iteration - 1] = result;
}
});
@ -80,8 +80,8 @@ public class SchrimpfInitialThresholdGenerator implements AlgorithmStartsListene
schrimpfAcceptance.setInitialThreshold(initialThreshold);
logger.info("took " + ((System.currentTimeMillis()-now)/1000.0) + " seconds");
logger.debug("initial threshold: " + initialThreshold);
logger.info("took {} seconds", ((System.currentTimeMillis() - now) / 1000.0));
logger.debug("initial threshold: {}", initialThreshold);
logger.info("---------------------------------------------------------------------");
}

View file

@ -1,4 +1,3 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
@ -26,15 +25,13 @@ import java.util.Collection;
/**
* Acceptor that decides whether the newSolution is accepted or not.
*
*
* @author stefan
*
*/
public interface SolutionAcceptor {
/**
* Accepts solution or not, and returns true if a new solution has been accepted.
*
* <p/>
* <p>If the solution is accepted, it is added to solutions, i.e. the solutions-collections is modified.
*
* @param solutions collection of existing solutions

View file

@ -0,0 +1,61 @@
package jsprit.core.algorithm.box;
import jsprit.core.algorithm.listener.IterationStartsListener;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.SoftActivityConstraint;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.util.RandomNumberGeneration;
import java.util.Collection;
import java.util.Random;
/**
* Created by schroeder on 16/01/15.
*/
class ConcurrentInsertionNoiseMaker implements SoftActivityConstraint, IterationStartsListener {
private final double noiseProbability;
private boolean makeNoise = false;
private double noiseLevel = 0.1;
private Random random = RandomNumberGeneration.newInstance();
private Random[] randomArray;
private double maxCosts;
ConcurrentInsertionNoiseMaker(VehicleRoutingProblem vrp, double maxCosts, double noiseLevel, double noiseProbability) {
this.noiseLevel = noiseLevel;
this.noiseProbability = noiseProbability;
this.maxCosts = maxCosts;
randomArray = new Random[vrp.getNuActivities() + 2];
for (int i = 0; i < randomArray.length; i++) {
Random r = new Random();
r.setSeed(random.nextLong());
randomArray[i] = r;
}
}
@Override
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
if (random.nextDouble() < noiseProbability) {
makeNoise = true;
} else makeNoise = false;
}
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
if (makeNoise) {
return noiseLevel * maxCosts * randomArray[newAct.getIndex()].nextDouble();
}
return 0;
}
public void setRandom(Random random) {
this.random = random;
}
}

View file

@ -26,25 +26,23 @@ import jsprit.core.util.Resource;
import java.net.URL;
/**
* Factory that creates the {@link VehicleRoutingAlgorithm} as proposed by Schrimpf et al., 2000 with the following parameters:
*
* <p>
* <p/>
* <p/>
* R&R_random (prob=0.5, F=0.5);
* R&R_radial (prob=0.5, F=0.3);
* threshold-accepting with exponentialDecayFunction (alpha=0.1, warmup-iterations=100);
* nuOfIterations=2000
*
* <p/>
* <p>Gerhard Schrimpf, Johannes Schneider, Hermann Stamm- Wilbrandt, and Gunter Dueck.
* Record breaking optimization results using the ruin and recreate principle.
* Journal of Computational Physics, 159(2):139 171, 2000. ISSN 0021-9991. doi: 10.1006/jcph.1999. 6413.
* URL http://www.sciencedirect.com/science/article/ pii/S0021999199964136
*
* <p/>
* <p>algorithm-xml-config is available at src/main/resources/schrimpf.xml.
*
* @author stefan schroeder
*
*/
public class GreedySchrimpfFactory {
@ -54,7 +52,7 @@ public class GreedySchrimpfFactory {
* @param vrp
* @return algorithm
*/
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp){
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) {
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
URL resource = Resource.getAsURL("greedySchrimpf.xml");
new AlgorithmConfigXmlReader(algorithmConfig).read(resource);
@ -62,5 +60,4 @@ public class GreedySchrimpfFactory {
}
}

View file

@ -1,92 +1,60 @@
package jsprit.core.algorithm.box;
import jsprit.core.algorithm.listener.IterationStartsListener;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.SoftActivityConstraint;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.util.RandomNumberGeneration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
/**
* Created by schroeder on 16/01/15.
*/
* Created by schroeder on 16/01/15.
*/
class InsertionNoiseMaker implements SoftActivityConstraint, IterationStartsListener {
private final double noiseProbability;
private boolean makeNoise = false;
private VehicleRoutingProblem vrp;
double maxCosts = 0.;
private double noiseLevel = 0.1;
private Random random = RandomNumberGeneration.getRandom();
private Random random = RandomNumberGeneration.newInstance();
public InsertionNoiseMaker(VehicleRoutingProblem vrp, double noiseLevel, double noiseProbability) {
this.vrp = vrp;
// private Random[] randomArray;
private double maxCosts;
InsertionNoiseMaker(VehicleRoutingProblem vrp, double maxCosts, double noiseLevel, double noiseProbability) {
this.noiseLevel = noiseLevel;
this.noiseProbability = noiseProbability;
determineMaxCosts(vrp);
}
//@ToDo refactor determining max costs to allow skipping this
private void determineMaxCosts(VehicleRoutingProblem vrp) {
double max = 0.;
for(Job i : vrp.getJobs().values()){
List<Location> fromLocations = getLocations(i);
for(Job j : vrp.getJobs().values()){
List<Location> toLocations = getLocations(j);
for(Location iLoc : fromLocations){
for(Location jLoc : toLocations) {
max = Math.max(max, vrp.getTransportCosts().getTransportCost(iLoc, jLoc, 0, null, vrp.getVehicles().iterator().next()));
}
}
}
}
maxCosts = max;
}
private List<Location> getLocations(Job j) {
List<Location> locs = new ArrayList<Location>();
if(j instanceof Service) {
locs.add(((Service) j).getLocation());
}
else if(j instanceof Shipment){
locs.add(((Shipment) j).getPickupLocation());
locs.add(((Shipment) j).getDeliveryLocation());
}
return locs;
this.maxCosts = maxCosts;
// randomArray = new Random[vrp.getNuActivities() + 2];
// for (int i = 0; i < randomArray.length; i++) {
// Random r = new Random();
// r.setSeed(random.nextLong());
// randomArray[i] = r;
// }
}
@Override
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
if(random.nextDouble() < noiseProbability){
if (random.nextDouble() < noiseProbability) {
makeNoise = true;
}
else makeNoise = false;
} else makeNoise = false;
}
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
if(makeNoise) {
if (makeNoise) {
return noiseLevel * maxCosts * random.nextDouble();
}
return 0;
}
public void setRandom(Random random) {
this.random = random;
}

View file

@ -17,6 +17,7 @@ import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.BreakActivity;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.FiniteFleetManagerFactory;
import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
@ -34,17 +35,19 @@ import java.util.concurrent.Executors;
public class Jsprit {
private final ActivityInsertionCostsCalculator activityInsertion;
public enum Construction {
BEST_INSERTION("best_insertion"), REGRET_INSERTION("regret_insertion");
String name;
Construction(String name){
Construction(String name) {
this.name = name;
}
public String toString(){
public String toString() {
return name;
}
@ -63,11 +66,11 @@ public class Jsprit {
String strategyName;
Strategy(String strategyName){
Strategy(String strategyName) {
this.strategyName = strategyName;
}
public String toString(){
public String toString() {
return strategyName;
}
}
@ -93,21 +96,23 @@ public class Jsprit {
INSERTION_NOISE_PROB("insertion.noise_prob"),
RUIN_WORST_NOISE_LEVEL("worst.noise_level"),
RUIN_WORST_NOISE_PROB("worst.noise_prob"),
FAST_REGRET("regret.fast"),
MAX_TRANSPORT_COSTS("max_transport_costs"),
CONSTRUCTION("construction");
String paraName;
Parameter(String name){
Parameter(String name) {
this.paraName = name;
}
public String toString(){
public String toString() {
return paraName;
}
}
public static VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vehicleRoutingProblem){
public static VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vehicleRoutingProblem) {
return Jsprit.Builder.newInstance(vehicleRoutingProblem).buildAlgorithm();
}
@ -131,53 +136,56 @@ public class Jsprit {
private Random random = RandomNumberGeneration.newInstance();
public static Builder newInstance(VehicleRoutingProblem vrp){
private ActivityInsertionCostsCalculator activityInsertionCalculator;
public static Builder newInstance(VehicleRoutingProblem vrp) {
return new Builder(vrp);
}
private Builder(VehicleRoutingProblem vrp){
private Builder(VehicleRoutingProblem vrp) {
this.vrp = vrp;
properties = new Properties(createDefaultProperties());
}
private Properties createDefaultProperties() {
Properties defaults = new Properties();
defaults.put(Strategy.RADIAL_BEST.toString(),"0.");
defaults.put(Strategy.RADIAL_REGRET.toString(),".5");
defaults.put(Strategy.RANDOM_BEST.toString(),".5");
defaults.put(Strategy.RANDOM_REGRET.toString(),".5");
defaults.put(Strategy.WORST_BEST.toString(),"0.");
defaults.put(Strategy.WORST_REGRET.toString(),"1.");
defaults.put(Strategy.CLUSTER_BEST.toString(),"0.");
defaults.put(Strategy.CLUSTER_REGRET.toString(),"1.");
defaults.put(Parameter.FIXED_COST_PARAM.toString(),"0.");
defaults.put(Parameter.VEHICLE_SWITCH.toString(),"true");
defaults.put(Parameter.ITERATIONS.toString(),"2000");
defaults.put(Parameter.REGRET_DISTANCE_SCORER.toString(),".05");
defaults.put(Parameter.REGRET_TIME_WINDOW_SCORER.toString(),"-.1");
defaults.put(Parameter.THREADS.toString(),"1");
int minShare = (int)Math.min(20, Math.max(3,vrp.getJobs().size() * 0.05));
int maxShare = (int)Math.min(50, Math.max(5,vrp.getJobs().size() * 0.3));
defaults.put(Parameter.RADIAL_MIN_SHARE.toString(),String.valueOf(minShare));
defaults.put(Parameter.RADIAL_MAX_SHARE.toString(),String.valueOf(maxShare));
defaults.put(Parameter.WORST_MIN_SHARE.toString(),String.valueOf(minShare));
defaults.put(Parameter.WORST_MAX_SHARE.toString(),String.valueOf(maxShare));
defaults.put(Parameter.CLUSTER_MIN_SHARE.toString(),String.valueOf(minShare));
defaults.put(Parameter.CLUSTER_MAX_SHARE.toString(),String.valueOf(maxShare));
int minShare_ = (int)Math.min(70, Math.max(5,vrp.getJobs().size() * 0.5));
int maxShare_ = (int)Math.min(70, Math.max(5,vrp.getJobs().size() * 0.5));
defaults.put(Parameter.RANDOM_REGRET_MIN_SHARE.toString(),String.valueOf(minShare_));
defaults.put(Parameter.RANDOM_REGRET_MAX_SHARE.toString(),String.valueOf(maxShare_));
defaults.put(Parameter.RANDOM_BEST_MIN_SHARE.toString(),String.valueOf(minShare_));
defaults.put(Parameter.RANDOM_BEST_MAX_SHARE.toString(),String.valueOf(maxShare_));
defaults.put(Parameter.THRESHOLD_ALPHA.toString(),String.valueOf(0.15));
defaults.put(Parameter.THRESHOLD_INI.toString(),String.valueOf(0.03));
defaults.put(Parameter.INSERTION_NOISE_LEVEL.toString(),String.valueOf(0.15));
defaults.put(Parameter.INSERTION_NOISE_PROB.toString(),String.valueOf(0.2));
defaults.put(Parameter.RUIN_WORST_NOISE_LEVEL.toString(),String.valueOf(0.15));
defaults.put(Parameter.RUIN_WORST_NOISE_PROB.toString(),String.valueOf(0.2));
defaults.put(Parameter.VEHICLE_SWITCH.toString(),String.valueOf(true));
defaults.put(Parameter.CONSTRUCTION.toString(),Construction.REGRET_INSERTION.toString());
defaults.put(Strategy.RADIAL_BEST.toString(), "0.");
defaults.put(Strategy.RADIAL_REGRET.toString(), ".5");
defaults.put(Strategy.RANDOM_BEST.toString(), ".5");
defaults.put(Strategy.RANDOM_REGRET.toString(), ".5");
defaults.put(Strategy.WORST_BEST.toString(), "0.");
defaults.put(Strategy.WORST_REGRET.toString(), "1.");
defaults.put(Strategy.CLUSTER_BEST.toString(), "0.");
defaults.put(Strategy.CLUSTER_REGRET.toString(), "1.");
defaults.put(Parameter.FIXED_COST_PARAM.toString(), "0.");
defaults.put(Parameter.VEHICLE_SWITCH.toString(), "true");
defaults.put(Parameter.ITERATIONS.toString(), "2000");
defaults.put(Parameter.REGRET_DISTANCE_SCORER.toString(), ".05");
defaults.put(Parameter.REGRET_TIME_WINDOW_SCORER.toString(), "-.1");
defaults.put(Parameter.THREADS.toString(), "1");
int minShare = (int) Math.min(20, Math.max(3, vrp.getJobs().size() * 0.05));
int maxShare = (int) Math.min(50, Math.max(5, vrp.getJobs().size() * 0.3));
defaults.put(Parameter.RADIAL_MIN_SHARE.toString(), String.valueOf(minShare));
defaults.put(Parameter.RADIAL_MAX_SHARE.toString(), String.valueOf(maxShare));
defaults.put(Parameter.WORST_MIN_SHARE.toString(), String.valueOf(minShare));
defaults.put(Parameter.WORST_MAX_SHARE.toString(), String.valueOf(maxShare));
defaults.put(Parameter.CLUSTER_MIN_SHARE.toString(), String.valueOf(minShare));
defaults.put(Parameter.CLUSTER_MAX_SHARE.toString(), String.valueOf(maxShare));
int minShare_ = (int) Math.min(70, Math.max(5, vrp.getJobs().size() * 0.5));
int maxShare_ = (int) Math.min(70, Math.max(5, vrp.getJobs().size() * 0.5));
defaults.put(Parameter.RANDOM_REGRET_MIN_SHARE.toString(), String.valueOf(minShare_));
defaults.put(Parameter.RANDOM_REGRET_MAX_SHARE.toString(), String.valueOf(maxShare_));
defaults.put(Parameter.RANDOM_BEST_MIN_SHARE.toString(), String.valueOf(minShare_));
defaults.put(Parameter.RANDOM_BEST_MAX_SHARE.toString(), String.valueOf(maxShare_));
defaults.put(Parameter.THRESHOLD_ALPHA.toString(), String.valueOf(0.15));
defaults.put(Parameter.THRESHOLD_INI.toString(), String.valueOf(0.03));
defaults.put(Parameter.INSERTION_NOISE_LEVEL.toString(), String.valueOf(0.15));
defaults.put(Parameter.INSERTION_NOISE_PROB.toString(), String.valueOf(0.2));
defaults.put(Parameter.RUIN_WORST_NOISE_LEVEL.toString(), String.valueOf(0.15));
defaults.put(Parameter.RUIN_WORST_NOISE_PROB.toString(), String.valueOf(0.2));
defaults.put(Parameter.VEHICLE_SWITCH.toString(), String.valueOf(true));
defaults.put(Parameter.FAST_REGRET, String.valueOf(false));
defaults.put(Parameter.CONSTRUCTION.toString(), Construction.REGRET_INSERTION.toString());
return defaults;
}
@ -188,23 +196,23 @@ public class Jsprit {
return this;
}
public Builder setRandom(Random random){
public Builder setRandom(Random random) {
this.random = random;
return this;
}
public Builder setProperty(String key, String value){
properties.put(key,value);
public Builder setProperty(String key, String value) {
properties.put(key, value);
return this;
}
public Builder setProperty(Parameter parameter, String value){
setProperty(parameter.toString(),value);
public Builder setProperty(Parameter parameter, String value) {
setProperty(parameter.toString(), value);
return this;
}
public Builder setProperty(Strategy strategy, String value){
setProperty(strategy.toString(),value);
public Builder setProperty(Strategy strategy, String value) {
setProperty(strategy.toString(), value);
return this;
}
@ -224,7 +232,12 @@ public class Jsprit {
return this;
}
public VehicleRoutingAlgorithm buildAlgorithm(){
public Builder setActivityInsertionCalculator(ActivityInsertionCostsCalculator activityInsertionCalculator) {
this.activityInsertionCalculator = activityInsertionCalculator;
return this;
}
public VehicleRoutingAlgorithm buildAlgorithm() {
return new Jsprit(this).create(vrp);
}
@ -245,13 +258,15 @@ public class Jsprit {
}
public RuinShareFactoryImpl(int minShare, int maxShare) {
if(maxShare < minShare) throw new IllegalArgumentException("maxShare must be equal or greater than minShare");
if (maxShare < minShare)
throw new IllegalArgumentException("maxShare must be equal or greater than minShare");
this.minShare = minShare;
this.maxShare = maxShare;
}
public RuinShareFactoryImpl(int minShare, int maxShare, Random random) {
if(maxShare < minShare) throw new IllegalArgumentException("maxShare must be equal or greater than minShare");
if (maxShare < minShare)
throw new IllegalArgumentException("maxShare must be equal or greater than minShare");
this.minShare = minShare;
this.maxShare = maxShare;
this.random = random;
@ -291,32 +306,64 @@ public class Jsprit {
this.properties = builder.properties;
this.objectiveFunction = builder.objectiveFunction;
this.random = builder.random;
this.activityInsertion = builder.activityInsertionCalculator;
}
private VehicleRoutingAlgorithm create(final VehicleRoutingProblem vrp){
private VehicleRoutingAlgorithm create(final VehicleRoutingProblem vrp) {
VehicleFleetManager fm;
if(vrp.getFleetSize().equals(VehicleRoutingProblem.FleetSize.INFINITE)){
if (vrp.getFleetSize().equals(VehicleRoutingProblem.FleetSize.INFINITE)) {
fm = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
} else {
FiniteFleetManagerFactory finiteFleetManagerFactory = new FiniteFleetManagerFactory(vrp.getVehicles());
finiteFleetManagerFactory.setRandom(random);
fm = finiteFleetManagerFactory.createFleetManager();
}
else fm = new FiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
if(stateManager == null){
if (stateManager == null) {
stateManager = new StateManager(vrp);
}
if(constraintManager == null){
constraintManager = new ConstraintManager(vrp,stateManager);
if (constraintManager == null) {
constraintManager = new ConstraintManager(vrp, stateManager);
}
if (noThreads == null) {
noThreads = toInteger(getProperty(Parameter.THREADS.toString()));
}
if (noThreads > 1) {
if (es == null) {
setupExecutorInternally = true;
es = Executors.newFixedThreadPool(noThreads);
}
}
double noiseLevel = toDouble(getProperty(Parameter.INSERTION_NOISE_LEVEL.toString()));
double noiseProbability = toDouble(getProperty(Parameter.INSERTION_NOISE_PROB.toString()));
final InsertionNoiseMaker noiseMaker = new InsertionNoiseMaker(vrp, noiseLevel, noiseProbability);
noiseMaker.setRandom(random);
constraintManager.addConstraint(noiseMaker);
JobNeighborhoods jobNeighborhoods = new JobNeighborhoodsFactory().createNeighborhoods(vrp, new AvgServiceAndShipmentDistance(vrp.getTransportCosts()), (int) (vrp.getJobs().values().size() * 0.5));
jobNeighborhoods.initialise();
RuinRadial radial = new RuinRadial(vrp,vrp.getJobs().size(),jobNeighborhoods);
final double maxCosts;
if(properties.containsKey(Parameter.MAX_TRANSPORT_COSTS.toString())){
maxCosts = Double.parseDouble(getProperty(Parameter.MAX_TRANSPORT_COSTS.toString()));
}
else{
maxCosts = jobNeighborhoods.getMaxDistance();
}
IterationStartsListener noiseConfigurator;
if (noThreads > 1) {
ConcurrentInsertionNoiseMaker noiseMaker = new ConcurrentInsertionNoiseMaker(vrp, maxCosts, noiseLevel, noiseProbability);
noiseMaker.setRandom(random);
constraintManager.addConstraint(noiseMaker);
noiseConfigurator = noiseMaker;
} else {
InsertionNoiseMaker noiseMaker = new InsertionNoiseMaker(vrp, maxCosts, noiseLevel, noiseProbability);
noiseMaker.setRandom(random);
constraintManager.addConstraint(noiseMaker);
noiseConfigurator = noiseMaker;
}
RuinRadial radial = new RuinRadial(vrp, vrp.getJobs().size(), jobNeighborhoods);
radial.setRandom(random);
radial.setRuinShareFactory(new RuinShareFactoryImpl(
toInteger(properties.getProperty(Parameter.RADIAL_MIN_SHARE.toString())),
@ -324,7 +371,7 @@ public class Jsprit {
random)
);
final RuinRandom random_for_regret = new RuinRandom(vrp,0.5);
final RuinRandom random_for_regret = new RuinRandom(vrp, 0.5);
random_for_regret.setRandom(random);
random_for_regret.setRuinShareFactory(new RuinShareFactoryImpl(
toInteger(properties.getProperty(Parameter.RANDOM_REGRET_MIN_SHARE.toString())),
@ -332,7 +379,7 @@ public class Jsprit {
random)
);
final RuinRandom random_for_best = new RuinRandom(vrp,0.5);
final RuinRandom random_for_best = new RuinRandom(vrp, 0.5);
random_for_best.setRandom(random);
random_for_best.setRuinShareFactory(new RuinShareFactoryImpl(
toInteger(properties.getProperty(Parameter.RANDOM_BEST_MIN_SHARE.toString())),
@ -340,7 +387,7 @@ public class Jsprit {
random)
);
final RuinWorst worst = new RuinWorst(vrp, (int) (vrp.getJobs().values().size()*0.5));
final RuinWorst worst = new RuinWorst(vrp, (int) (vrp.getJobs().values().size() * 0.5));
worst.setRandom(random);
worst.setRuinShareFactory(new RuinShareFactoryImpl(
toInteger(properties.getProperty(Parameter.WORST_MIN_SHARE.toString())),
@ -353,17 +400,16 @@ public class Jsprit {
worst.setNoiseMaker(new NoiseMaker() {
public double makeNoise() {
if(random.nextDouble() < toDouble(getProperty(Parameter.RUIN_WORST_NOISE_PROB.toString()))) {
if (random.nextDouble() < toDouble(getProperty(Parameter.RUIN_WORST_NOISE_PROB.toString()))) {
return toDouble(getProperty(Parameter.RUIN_WORST_NOISE_LEVEL.toString()))
* noiseMaker.maxCosts * random.nextDouble();
}
else return 0.;
* maxCosts * random.nextDouble();
} else return 0.;
}
});
}
};
final RuinClusters clusters = new RuinClusters(vrp,(int) (vrp.getJobs().values().size()*0.5),jobNeighborhoods);
final RuinClusters clusters = new RuinClusters(vrp, (int) (vrp.getJobs().values().size() * 0.5), jobNeighborhoods);
clusters.setRandom(random);
clusters.setRuinShareFactory(new RuinShareFactoryImpl(
toInteger(properties.getProperty(Parameter.WORST_MIN_SHARE.toString())),
@ -372,99 +418,123 @@ public class Jsprit {
);
AbstractInsertionStrategy regret;
final RegretInsertion.DefaultScorer scorer;
if(noThreads == null){
noThreads = toInteger(getProperty(Parameter.THREADS.toString()));
}
if(noThreads > 1){
if(es == null){
setupExecutorInternally = true;
es = Executors.newFixedThreadPool(noThreads);
}
}
if(es != null) {
RegretInsertionConcurrent regretInsertion = (RegretInsertionConcurrent) new InsertionBuilder(vrp, fm, stateManager, constraintManager)
final DefaultScorer scorer;
boolean fastRegret = Boolean.parseBoolean(getProperty(Parameter.FAST_REGRET.toString()));
if (es != null) {
if(fastRegret){
RegretInsertionConcurrentFast regretInsertion = (RegretInsertionConcurrentFast) new InsertionBuilder(vrp, fm, stateManager, constraintManager)
.setInsertionStrategy(InsertionBuilder.Strategy.REGRET)
.setConcurrentMode(es, noThreads)
.setFastRegret(true)
.considerFixedCosts(toDouble(getProperty(Parameter.FIXED_COST_PARAM.toString())))
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
.setActivityInsertionCostCalculator(activityInsertion)
.build();
scorer = getRegretScorer(vrp);
regretInsertion.setScoringFunction(scorer);
regret = regretInsertion;
}
else {
RegretInsertion regretInsertion = (RegretInsertion) new InsertionBuilder(vrp, fm, stateManager, constraintManager)
RegretInsertionConcurrent regretInsertion = (RegretInsertionConcurrent) new InsertionBuilder(vrp, fm, stateManager, constraintManager)
.setInsertionStrategy(InsertionBuilder.Strategy.REGRET)
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
.setConcurrentMode(es, noThreads)
.considerFixedCosts(toDouble(getProperty(Parameter.FIXED_COST_PARAM.toString())))
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
.setActivityInsertionCostCalculator(activityInsertion)
.build();
scorer = getRegretScorer(vrp);
regretInsertion.setScoringFunction(scorer);
regret = regretInsertion;
}
} else {
if(fastRegret) {
RegretInsertionFast regretInsertion = (RegretInsertionFast) new InsertionBuilder(vrp, fm, stateManager, constraintManager)
.setInsertionStrategy(InsertionBuilder.Strategy.REGRET)
.setFastRegret(true)
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
.considerFixedCosts(toDouble(getProperty(Parameter.FIXED_COST_PARAM.toString())))
.setActivityInsertionCostCalculator(activityInsertion)
.build();
scorer = getRegretScorer(vrp);
regretInsertion.setScoringFunction(scorer);
regret = regretInsertion;
}
else{
RegretInsertion regretInsertion = (RegretInsertion) new InsertionBuilder(vrp, fm, stateManager, constraintManager)
.setInsertionStrategy(InsertionBuilder.Strategy.REGRET)
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
.considerFixedCosts(toDouble(getProperty(Parameter.FIXED_COST_PARAM.toString())))
.setActivityInsertionCostCalculator(activityInsertion)
.build();
scorer = getRegretScorer(vrp);
regretInsertion.setScoringFunction(scorer);
regret = regretInsertion;
}
}
regret.setRandom(random);
AbstractInsertionStrategy best;
if(vrp.getJobs().size() < 250 || es == null) {
if (vrp.getJobs().size() < 250 || es == null) {
BestInsertion bestInsertion = (BestInsertion) new InsertionBuilder(vrp, fm, stateManager, constraintManager)
.setInsertionStrategy(InsertionBuilder.Strategy.BEST)
.considerFixedCosts(Double.valueOf(properties.getProperty(Parameter.FIXED_COST_PARAM.toString())))
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
.setActivityInsertionCostCalculator(activityInsertion)
.build();
best = bestInsertion;
}
else{
} else {
BestInsertionConcurrent bestInsertion = (BestInsertionConcurrent) new InsertionBuilder(vrp, fm, stateManager, constraintManager)
.setInsertionStrategy(InsertionBuilder.Strategy.BEST)
.considerFixedCosts(Double.valueOf(properties.getProperty(Parameter.FIXED_COST_PARAM.toString())))
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
.setConcurrentMode(es, noThreads)
.setActivityInsertionCostCalculator(activityInsertion)
.build();
best = bestInsertion;
}
best.setRandom(random);
final SchrimpfAcceptance schrimpfAcceptance = new SchrimpfAcceptance(1,toDouble(getProperty(Parameter.THRESHOLD_ALPHA.toString())));
final SchrimpfAcceptance schrimpfAcceptance = new SchrimpfAcceptance(1, toDouble(getProperty(Parameter.THRESHOLD_ALPHA.toString())));
IterationStartsListener schrimpfThreshold = new IterationStartsListener() {
@Override
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
if(i == 1){
if (i == 1) {
double initialThreshold = Solutions.bestOf(solutions).getCost() * toDouble(getProperty(Parameter.THRESHOLD_INI.toString()));
schrimpfAcceptance.setInitialThreshold(initialThreshold);
}
}
};
SolutionCostCalculator objectiveFunction = getObjectiveFunction(vrp, noiseMaker.maxCosts);
SearchStrategy radial_regret = new SearchStrategy(Strategy.RADIAL_REGRET.toString(),new SelectBest(),schrimpfAcceptance, objectiveFunction);
SolutionCostCalculator objectiveFunction = getObjectiveFunction(vrp, maxCosts);
SearchStrategy radial_regret = new SearchStrategy(Strategy.RADIAL_REGRET.toString(), new SelectBest(), schrimpfAcceptance, objectiveFunction);
radial_regret.addModule(new RuinAndRecreateModule(Strategy.RADIAL_REGRET.toString(), regret, radial));
SearchStrategy radial_best = new SearchStrategy(Strategy.RADIAL_BEST.toString(),new SelectBest(),schrimpfAcceptance,objectiveFunction);
radial_best.addModule(new RuinAndRecreateModule(Strategy.RADIAL_BEST.toString(),best,radial));
SearchStrategy radial_best = new SearchStrategy(Strategy.RADIAL_BEST.toString(), new SelectBest(), schrimpfAcceptance, objectiveFunction);
radial_best.addModule(new RuinAndRecreateModule(Strategy.RADIAL_BEST.toString(), best, radial));
SearchStrategy random_best = new SearchStrategy(Strategy.RANDOM_BEST.toString(),new SelectBest(),schrimpfAcceptance,objectiveFunction);
random_best.addModule(new RuinAndRecreateModule(Strategy.RANDOM_BEST.toString(),best,random_for_best));
SearchStrategy random_best = new SearchStrategy(Strategy.RANDOM_BEST.toString(), new SelectBest(), schrimpfAcceptance, objectiveFunction);
random_best.addModule(new RuinAndRecreateModule(Strategy.RANDOM_BEST.toString(), best, random_for_best));
SearchStrategy random_regret = new SearchStrategy(Strategy.RANDOM_REGRET.toString(),new SelectBest(),schrimpfAcceptance,objectiveFunction);
random_regret.addModule(new RuinAndRecreateModule(Strategy.RANDOM_REGRET.toString(),regret,random_for_regret));
SearchStrategy random_regret = new SearchStrategy(Strategy.RANDOM_REGRET.toString(), new SelectBest(), schrimpfAcceptance, objectiveFunction);
random_regret.addModule(new RuinAndRecreateModule(Strategy.RANDOM_REGRET.toString(), regret, random_for_regret));
SearchStrategy worst_regret = new SearchStrategy(Strategy.WORST_REGRET.toString(),new SelectBest(),schrimpfAcceptance,objectiveFunction);
worst_regret.addModule(new RuinAndRecreateModule(Strategy.WORST_REGRET.toString(),regret,worst));
SearchStrategy worst_regret = new SearchStrategy(Strategy.WORST_REGRET.toString(), new SelectBest(), schrimpfAcceptance, objectiveFunction);
worst_regret.addModule(new RuinAndRecreateModule(Strategy.WORST_REGRET.toString(), regret, worst));
SearchStrategy worst_best = new SearchStrategy(Strategy.WORST_BEST.toString(),new SelectBest(),schrimpfAcceptance,objectiveFunction);
worst_best.addModule(new RuinAndRecreateModule(Strategy.WORST_BEST.toString(),best,worst));
SearchStrategy worst_best = new SearchStrategy(Strategy.WORST_BEST.toString(), new SelectBest(), schrimpfAcceptance, objectiveFunction);
worst_best.addModule(new RuinAndRecreateModule(Strategy.WORST_BEST.toString(), best, worst));
final SearchStrategy clusters_regret = new SearchStrategy(Strategy.CLUSTER_REGRET.toString(),new SelectBest(),schrimpfAcceptance,objectiveFunction);
clusters_regret.addModule(new RuinAndRecreateModule(Strategy.CLUSTER_REGRET.toString(),regret,clusters));
final SearchStrategy clusters_regret = new SearchStrategy(Strategy.CLUSTER_REGRET.toString(), new SelectBest(), schrimpfAcceptance, objectiveFunction);
clusters_regret.addModule(new RuinAndRecreateModule(Strategy.CLUSTER_REGRET.toString(), regret, clusters));
final SearchStrategy clusters_best = new SearchStrategy(Strategy.CLUSTER_BEST.toString(),new SelectBest(),schrimpfAcceptance,objectiveFunction);
clusters_best.addModule(new RuinAndRecreateModule(Strategy.CLUSTER_BEST.toString(),best,clusters));
final SearchStrategy clusters_best = new SearchStrategy(Strategy.CLUSTER_BEST.toString(), new SelectBest(), schrimpfAcceptance, objectiveFunction);
clusters_best.addModule(new RuinAndRecreateModule(Strategy.CLUSTER_BEST.toString(), best, clusters));
PrettyAlgorithmBuilder prettyBuilder = PrettyAlgorithmBuilder.newInstance(vrp, fm, stateManager, constraintManager);
prettyBuilder.setRandom(random);
if(addCoreConstraints){
if (addCoreConstraints) {
prettyBuilder.addCoreStateAndConstraintStuff();
}
prettyBuilder.withStrategy(radial_regret, toDouble(getProperty(Strategy.RADIAL_REGRET.toString())))
@ -473,38 +543,39 @@ public class Jsprit {
.withStrategy(random_regret, toDouble(getProperty(Strategy.RANDOM_REGRET.toString())))
.withStrategy(worst_best, toDouble(getProperty(Strategy.WORST_BEST.toString())))
.withStrategy(worst_regret, toDouble(getProperty(Strategy.WORST_REGRET.toString())))
.withStrategy(clusters_regret,toDouble(getProperty(Strategy.CLUSTER_REGRET.toString())))
.withStrategy(clusters_best,toDouble(getProperty(Strategy.CLUSTER_BEST.toString())));
if (getProperty(Parameter.CONSTRUCTION.toString()).equals(Construction.BEST_INSERTION.toString())){
.withStrategy(clusters_regret, toDouble(getProperty(Strategy.CLUSTER_REGRET.toString())))
.withStrategy(clusters_best, toDouble(getProperty(Strategy.CLUSTER_BEST.toString())));
if (getProperty(Parameter.CONSTRUCTION.toString()).equals(Construction.BEST_INSERTION.toString())) {
prettyBuilder.constructInitialSolutionWith(best, objectiveFunction);
}
else{
} else {
prettyBuilder.constructInitialSolutionWith(regret, objectiveFunction);
}
VehicleRoutingAlgorithm vra = prettyBuilder.build();
vra.addListener(schrimpfThreshold);
vra.addListener(noiseMaker);
vra.addListener(noiseConfigurator);
vra.addListener(noise);
vra.addListener(clusters);
vra.addListener(new RuinBreaks());
handleExecutorShutdown(vra);
vra.setMaxIterations(Integer.valueOf(properties.getProperty(Parameter.ITERATIONS.toString())));
return vra;
}
private RegretInsertion.DefaultScorer getRegretScorer(VehicleRoutingProblem vrp) {
RegretInsertion.DefaultScorer scorer;
scorer = new RegretInsertion.DefaultScorer(vrp);
private DefaultScorer getRegretScorer(VehicleRoutingProblem vrp) {
DefaultScorer scorer;
scorer = new DefaultScorer(vrp);
scorer.setTimeWindowParam(Double.valueOf(properties.getProperty(Parameter.REGRET_TIME_WINDOW_SCORER.toString())));
scorer.setDepotDistanceParam(Double.valueOf(properties.getProperty(Parameter.REGRET_TIME_WINDOW_SCORER.toString())));
scorer.setDepotDistanceParam(Double.valueOf(properties.getProperty(Parameter.REGRET_DISTANCE_SCORER.toString())));
return scorer;
}
private void handleExecutorShutdown(VehicleRoutingAlgorithm vra) {
if(setupExecutorInternally){
if (setupExecutorInternally) {
vra.addListener(new AlgorithmEndsListener() {
@Override
@ -514,7 +585,7 @@ public class Jsprit {
});
}
if(es != null) {
if (es != null) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
if (!es.isShutdown()) {
@ -526,7 +597,7 @@ public class Jsprit {
}
}
String getProperty(String key){
String getProperty(String key) {
return properties.getProperty(key);
}
@ -543,20 +614,34 @@ public class Jsprit {
}
private SolutionCostCalculator getObjectiveFunction(final VehicleRoutingProblem vrp, final double maxCosts) {
if(objectiveFunction != null) return objectiveFunction;
if (objectiveFunction != null) return objectiveFunction;
SolutionCostCalculator solutionCostCalculator = new SolutionCostCalculator() {
@Override
public double getCosts(VehicleRoutingProblemSolution solution) {
double costs = 0.;
for(VehicleRoute route : solution.getRoutes()){
for (VehicleRoute route : solution.getRoutes()) {
costs += route.getVehicle().getType().getVehicleCostParams().fix;
boolean hasBreak = false;
TourActivity prevAct = route.getStart();
for(TourActivity act : route.getActivities()){
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(),act.getLocation(),prevAct.getEndTime(),route.getDriver(),route.getVehicle());
for (TourActivity act : route.getActivities()) {
if (act instanceof BreakActivity) hasBreak = true;
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(), act.getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle());
costs += vrp.getActivityCosts().getActivityCost(act, act.getArrTime(), route.getDriver(), route.getVehicle());
prevAct = act;
}
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(),route.getEnd().getLocation(),prevAct.getEndTime(),route.getDriver(),route.getVehicle());
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(), route.getEnd().getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle());
if (route.getVehicle().getBreak() != null) {
if (!hasBreak) {
//break defined and required but not assigned penalty
if (route.getEnd().getArrTime() > route.getVehicle().getBreak().getTimeWindow().getEnd()) {
costs += maxCosts * 2 + route.getVehicle().getBreak().getServiceDuration() * route.getVehicle().getType().getVehicleCostParams().perServiceTimeUnit;
} else {
costs -= maxCosts * 2;
}
}
}
}
costs += solution.getUnassignedJobs().size() * maxCosts * 2;
return costs;
@ -565,4 +650,5 @@ public class Jsprit {
return solutionCostCalculator;
}
}

View file

@ -26,25 +26,23 @@ import jsprit.core.util.Resource;
import java.net.URL;
/**
* Factory that creates the {@link VehicleRoutingAlgorithm} as proposed by Schrimpf et al., 2000 with the following parameters:
*
* <p>
* <p/>
* <p/>
* R&R_random (prob=0.5, F=0.5);
* R&R_radial (prob=0.5, F=0.3);
* threshold-accepting with exponentialDecayFunction (alpha=0.1, warmup-iterations=100);
* nuOfIterations=2000
*
* <p/>
* <p>Gerhard Schrimpf, Johannes Schneider, Hermann Stamm- Wilbrandt, and Gunter Dueck.
* Record breaking optimization results using the ruin and recreate principle.
* Journal of Computational Physics, 159(2):139 171, 2000. ISSN 0021-9991. doi: 10.1006/jcph.1999. 6413.
* URL http://www.sciencedirect.com/science/article/ pii/S0021999199964136
*
* <p/>
* <p>algorithm-xml-config is available at src/main/resources/schrimpf.xml.
*
* @author stefan schroeder
*
*/
public class SchrimpfFactory {
@ -54,7 +52,7 @@ public class SchrimpfFactory {
* @param vrp the underlying vehicle routing problem
* @return algorithm
*/
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp){
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) {
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
URL resource = Resource.getAsURL("schrimpf.xml");
new AlgorithmConfigXmlReader(algorithmConfig).read(resource);
@ -62,5 +60,4 @@ public class SchrimpfFactory {
}
}

View file

@ -22,12 +22,12 @@ public class AlgorithmConfig {
private XMLConfiguration xmlConfig;
public AlgorithmConfig(){
public AlgorithmConfig() {
xmlConfig = new XMLConfiguration();
}
public XMLConfiguration getXMLConfiguration(){
public XMLConfiguration getXMLConfiguration() {
return xmlConfig;
}
}

View file

@ -45,19 +45,19 @@ public class AlgorithmConfigXmlReader {
return this;
}
public AlgorithmConfigXmlReader(AlgorithmConfig algorithmConfig){
public AlgorithmConfigXmlReader(AlgorithmConfig algorithmConfig) {
this.algorithmConfig = algorithmConfig;
}
public void read(URL url){
public void read(URL url) {
log.debug("read algorithm: " + url);
algorithmConfig.getXMLConfiguration().setURL(url);
algorithmConfig.getXMLConfiguration().setAttributeSplittingDisabled(true);
algorithmConfig.getXMLConfiguration().setDelimiterParsingDisabled(true);
if(schemaValidation){
if (schemaValidation) {
final InputStream resource = Resource.getAsInputStream("algorithm_schema.xsd");
if(resource != null) {
if (resource != null) {
EntityResolver resolver = new EntityResolver() {
@Override
@ -70,22 +70,19 @@ public class AlgorithmConfigXmlReader {
};
algorithmConfig.getXMLConfiguration().setEntityResolver(resolver);
algorithmConfig.getXMLConfiguration().setSchemaValidation(true);
}
else{
} else {
log.warn("cannot find schema-xsd file (algorithm_xml_schema.xsd). try to read xml without xml-file-validation.");
}
}
try {
algorithmConfig.getXMLConfiguration().load();
} catch (ConfigurationException e) {
log.error(e);
e.printStackTrace();
System.exit(1);
throw new RuntimeException(e);
}
}
public void read(String filename){
public void read(String filename) {
log.debug("read algorithm-config from file " + filename);
URL url = Resource.getAsURL(filename);
read(url);

View file

@ -37,77 +37,80 @@ class InsertionFactory {
@SuppressWarnings("deprecation")
public static InsertionStrategy createInsertion(VehicleRoutingProblem vrp, HierarchicalConfiguration config,
VehicleFleetManager vehicleFleetManager, StateManager stateManager, List<PrioritizedVRAListener> algorithmListeners, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators){
VehicleFleetManager vehicleFleetManager, StateManager stateManager, List<PrioritizedVRAListener> algorithmListeners, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators) {
if(config.containsKey("[@name]")){
if (config.containsKey("[@name]")) {
String insertionName = config.getString("[@name]");
if(!insertionName.equals("bestInsertion") && !insertionName.equals("regretInsertion")){
if (!insertionName.equals("bestInsertion") && !insertionName.equals("regretInsertion")) {
throw new IllegalStateException(insertionName + " is not supported. use either \"bestInsertion\" or \"regretInsertion\"");
}
InsertionBuilder iBuilder = new InsertionBuilder(vrp, vehicleFleetManager, stateManager, constraintManager);
if(executorService != null){
if (executorService != null) {
iBuilder.setConcurrentMode(executorService, nuOfThreads);
}
if(config.containsKey("level")){
if (config.containsKey("level")) {
String level = config.getString("level");
if(level.equals("local")){
if (level.equals("local")) {
iBuilder.setLocalLevel(addDefaultCostCalculators);
}
else if(level.equals("route")){
} else if (level.equals("route")) {
int forwardLooking = 0;
int memory = 1;
String forward = config.getString("level[@forwardLooking]");
String mem = config.getString("level[@memory]");
if(forward != null) forwardLooking = Integer.parseInt(forward);
else log.warn("parameter route[@forwardLooking] is missing. by default it is 0 which equals to local level");
if(mem != null) memory = Integer.parseInt(mem);
if (forward != null) forwardLooking = Integer.parseInt(forward);
else
log.warn("parameter route[@forwardLooking] is missing. by default it is 0 which equals to local level");
if (mem != null) memory = Integer.parseInt(mem);
else log.warn("parameter route[@memory] is missing. by default it is 1");
iBuilder.setRouteLevel(forwardLooking, memory, addDefaultCostCalculators);
}
else throw new IllegalStateException("level " + level + " is not known. currently it only knows \"local\" or \"route\"");
}
else iBuilder.setLocalLevel(addDefaultCostCalculators);
} else
throw new IllegalStateException("level " + level + " is not known. currently it only knows \"local\" or \"route\"");
} else iBuilder.setLocalLevel(addDefaultCostCalculators);
if(config.containsKey("considerFixedCosts") || config.containsKey("considerFixedCost")){
if(addDefaultCostCalculators){
if (config.containsKey("considerFixedCosts") || config.containsKey("considerFixedCost")) {
if (addDefaultCostCalculators) {
String val = config.getString("considerFixedCosts");
if(val == null) val = config.getString("considerFixedCost");
if(val.equals("true")){
if (val == null) val = config.getString("considerFixedCost");
if (val.equals("true")) {
double fixedCostWeight = 0.5;
String weight = config.getString("considerFixedCosts[@weight]");
if(weight == null) weight = config.getString("considerFixedCost[@weight]");
if(weight != null) fixedCostWeight = Double.parseDouble(weight);
else throw new IllegalStateException("fixedCostsParameter 'weight' must be set, e.g. <considerFixedCosts weight=1.0>true</considerFixedCosts>.\n" +
if (weight == null) weight = config.getString("considerFixedCost[@weight]");
if (weight != null) fixedCostWeight = Double.parseDouble(weight);
else
throw new IllegalStateException("fixedCostsParameter 'weight' must be set, e.g. <considerFixedCosts weight=1.0>true</considerFixedCosts>.\n" +
"this has to be changed in algorithm-config-xml-file.");
iBuilder.considerFixedCosts(fixedCostWeight);
}
else if(val.equals("false")){
} else if (val.equals("false")) {
}
else throw new IllegalStateException("considerFixedCosts must either be true or false, i.e. <considerFixedCosts weight=1.0>true</considerFixedCosts> or \n<considerFixedCosts weight=1.0>false</considerFixedCosts>. " +
} else
throw new IllegalStateException("considerFixedCosts must either be true or false, i.e. <considerFixedCosts weight=1.0>true</considerFixedCosts> or \n<considerFixedCosts weight=1.0>false</considerFixedCosts>. " +
"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]");
if(timeSliceString != null && neighbors != null){
iBuilder.experimentalTimeScheduler(Double.parseDouble(timeSliceString),Integer.parseInt(neighbors));
if (timeSliceString != null && neighbors != null) {
iBuilder.experimentalTimeScheduler(Double.parseDouble(timeSliceString), Integer.parseInt(neighbors));
}
String allowVehicleSwitch = config.getString("allowVehicleSwitch");
if(allowVehicleSwitch != null){
if (allowVehicleSwitch != null) {
iBuilder.setAllowVehicleSwitch(Boolean.parseBoolean(allowVehicleSwitch));
}
if(insertionName.equals("regretInsertion")){
if (insertionName.equals("regretInsertion")) {
iBuilder.setInsertionStrategy(InsertionBuilder.Strategy.REGRET);
String fastRegret = config.getString("fastRegret");
if (fastRegret != null) {
iBuilder.setFastRegret(Boolean.parseBoolean(fastRegret));
}
}
return iBuilder.build();
}
else throw new IllegalStateException("cannot create insertionStrategy, since it has no name.");
} else throw new IllegalStateException("cannot create insertionStrategy, since it has no name.");
}

View file

@ -42,6 +42,7 @@ import jsprit.core.algorithm.termination.VariationCoefficientTermination;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.SwitchNotFeasible;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
@ -70,7 +71,7 @@ public class VehicleRoutingAlgorithms {
Class<K> getType();
}
static class AcceptorKey implements AbstractKey<SolutionAcceptor>{
static class AcceptorKey implements AbstractKey<SolutionAcceptor> {
private ModKey modKey;
@ -115,7 +116,7 @@ public class VehicleRoutingAlgorithms {
}
static class SelectorKey implements AbstractKey<SolutionSelector>{
static class SelectorKey implements AbstractKey<SolutionSelector> {
private ModKey modKey;
@ -151,7 +152,6 @@ public class VehicleRoutingAlgorithms {
}
@Override
public Class<SolutionSelector> getType() {
return SolutionSelector.class;
@ -159,7 +159,7 @@ public class VehicleRoutingAlgorithms {
}
static class StrategyModuleKey implements AbstractKey<SearchStrategyModule>{
static class StrategyModuleKey implements AbstractKey<SearchStrategyModule> {
private ModKey modKey;
@ -195,7 +195,6 @@ public class VehicleRoutingAlgorithms {
}
@Override
public Class<SearchStrategyModule> getType() {
return SearchStrategyModule.class;
@ -203,7 +202,7 @@ public class VehicleRoutingAlgorithms {
}
static class RuinStrategyKey implements AbstractKey<RuinStrategy>{
static class RuinStrategyKey implements AbstractKey<RuinStrategy> {
private ModKey modKey;
@ -239,7 +238,6 @@ public class VehicleRoutingAlgorithms {
}
@Override
public Class<RuinStrategy> getType() {
return RuinStrategy.class;
@ -247,7 +245,7 @@ public class VehicleRoutingAlgorithms {
}
static class InsertionStrategyKey implements AbstractKey<InsertionStrategy>{
static class InsertionStrategyKey implements AbstractKey<InsertionStrategy> {
private ModKey modKey;
@ -283,7 +281,6 @@ public class VehicleRoutingAlgorithms {
}
@Override
public Class<InsertionStrategy> getType() {
return InsertionStrategy.class;
@ -294,7 +291,7 @@ public class VehicleRoutingAlgorithms {
private Map<AbstractKey<?>, Object> map = new HashMap<AbstractKey<?>, Object>();
public <T> T get(AbstractKey<T> key) {
if(map.get(key) == null) return null;
if (map.get(key) == null) return null;
return key.getType().cast(map.get(key));
}
@ -302,7 +299,7 @@ public class VehicleRoutingAlgorithms {
return key.getType().cast(map.put(key, key.getType().cast(value)));
}
public Set<AbstractKey<?>> keySet(){
public Set<AbstractKey<?>> keySet() {
return map.keySet();
}
}
@ -325,6 +322,7 @@ public class VehicleRoutingAlgorithms {
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
@ -351,7 +349,8 @@ public class VehicleRoutingAlgorithms {
private static Logger log = LogManager.getLogger(VehicleRoutingAlgorithms.class.getName());
private VehicleRoutingAlgorithms(){}
private VehicleRoutingAlgorithms() {
}
/**
* Creates a {@link jsprit.core.algorithm.VehicleRoutingAlgorithm} from a AlgorithConfig based on the input vrp.
@ -360,11 +359,12 @@ public class VehicleRoutingAlgorithms {
* @param algorithmConfig the algorithm config
* @return {@link jsprit.core.algorithm.VehicleRoutingAlgorithm}
*/
public static VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp, final AlgorithmConfig algorithmConfig){
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),0, null);
public static VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp, final AlgorithmConfig algorithmConfig) {
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null);
}
public static VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final AlgorithmConfig algorithmConfig){
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),nThreads, null);
public static VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final AlgorithmConfig algorithmConfig) {
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null);
}
/**
@ -374,17 +374,18 @@ public class VehicleRoutingAlgorithms {
* @param configURL config url
* @return {@link jsprit.core.algorithm.VehicleRoutingAlgorithm}
*/
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, final URL configURL){
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, final URL configURL) {
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
xmlReader.read(configURL);
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),0, null);
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null);
}
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final URL configURL){
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final URL configURL) {
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
xmlReader.read(configURL);
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),nThreads, null);
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null);
}
/**
@ -394,35 +395,35 @@ public class VehicleRoutingAlgorithms {
* @param configFileName the config filename (and location)
* @return {@link jsprit.core.algorithm.VehicleRoutingAlgorithm}
*/
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, final String configFileName){
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, final String configFileName) {
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
xmlReader.read(configFileName);
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),0, null);
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null);
}
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, final String configFileName, StateManager stateManager){
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, final String configFileName, StateManager stateManager) {
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
xmlReader.read(configFileName);
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),0, stateManager);
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, stateManager);
}
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final String configFileName, StateManager stateManager){
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final String configFileName, StateManager stateManager) {
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
xmlReader.read(configFileName);
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(), nThreads, stateManager);
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, stateManager);
}
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(VehicleRoutingProblem vrp, int nThreads, String configFileName) {
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
xmlReader.read(configFileName);
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),nThreads, null);
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null);
}
private static class OpenRouteStateVerifier implements StateUpdater, ReverseActivityVisitor{
private static class OpenRouteStateVerifier implements StateUpdater, ReverseActivityVisitor {
private End end;
@ -438,9 +439,9 @@ public class VehicleRoutingAlgorithms {
@Override
public void visit(TourActivity activity) {
if(firstAct){
firstAct=false;
if(!vehicle.isReturnToDepot()){
if (firstAct) {
firstAct = false;
if (!vehicle.isReturnToDepot()) {
assert activity.getLocation().getId().equals(end.getLocation().getId()) : "route end and last activity are not equal even route is open. this should not be.";
}
}
@ -454,13 +455,12 @@ public class VehicleRoutingAlgorithms {
}
private static VehicleRoutingAlgorithm createAlgo(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, StateManager stateMan){
private static VehicleRoutingAlgorithm createAlgo(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, StateManager stateMan) {
//create state-manager
final StateManager stateManager;
if(stateMan!=null) {
if (stateMan != null) {
stateManager = stateMan;
}
else{
} else {
stateManager = new StateManager(vrp);
}
stateManager.updateLoadStates();
@ -475,17 +475,18 @@ public class VehicleRoutingAlgorithms {
* define constraints
*/
//constraint manager
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
ConstraintManager constraintManager = new ConstraintManager(vrp, stateManager);
constraintManager.addTimeWindowConstraint();
constraintManager.addLoadConstraint();
constraintManager.addSkillsConstraint();
constraintManager.addConstraint(new SwitchNotFeasible(stateManager));
return readAndCreateAlgorithm(vrp, config, nuOfThreads, null, stateManager, constraintManager, true);
}
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, AlgorithmConfig config,
int nuOfThreads, SolutionCostCalculator solutionCostCalculator, final StateManager stateManager, ConstraintManager constraintManager, boolean addDefaultCostCalculators) {
return readAndCreateAlgorithm(vrp, config.getXMLConfiguration(),nuOfThreads, solutionCostCalculator, stateManager, constraintManager, addDefaultCostCalculators);
return readAndCreateAlgorithm(vrp, config.getXMLConfiguration(), nuOfThreads, solutionCostCalculator, stateManager, constraintManager, addDefaultCostCalculators);
}
private static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, XMLConfiguration config,
@ -502,13 +503,13 @@ public class VehicleRoutingAlgorithms {
//threading
final ExecutorService executorService;
if(nuOfThreads > 0){
if (nuOfThreads > 0) {
log.debug("setup executor-service with " + nuOfThreads + " threads");
executorService = Executors.newFixedThreadPool(nuOfThreads);
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, new AlgorithmEndsListener() {
@Override
public void informAlgorithmEnds(VehicleRoutingProblem problem,Collection<VehicleRoutingProblemSolution> solutions) {
public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
log.debug("shutdown executor-service");
executorService.shutdown();
}
@ -518,19 +519,17 @@ public class VehicleRoutingAlgorithms {
@Override
public void uncaughtException(Thread arg0, Throwable arg1) {
System.err.println(arg1.toString());
System.exit(0);
}
});
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
if(!executorService.isShutdown()){
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
if (!executorService.isShutdown()) {
System.err.println("shutdowHook shuts down executorService");
executorService.shutdown();
}
}
});
}
else executorService = null;
} else executorService = null;
//create fleetmanager
@ -538,22 +537,21 @@ public class VehicleRoutingAlgorithms {
String switchString = config.getString("construction.insertion.allowVehicleSwitch");
final boolean switchAllowed;
if(switchString != null){
if (switchString != null) {
switchAllowed = Boolean.parseBoolean(switchString);
}
else switchAllowed = true;
} else switchAllowed = true;
ActivityTimeTracker.ActivityPolicy activityPolicy;
if(stateManager.timeWindowUpdateIsActivated()){
UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows(stateManager,vrp.getTransportCosts());
if (stateManager.timeWindowUpdateIsActivated()) {
UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows(stateManager, vrp.getTransportCosts());
timeWindowUpdater.setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
Map<VehicleTypeKey,Vehicle> uniqueTypes = new HashMap<VehicleTypeKey,Vehicle>();
Map<VehicleTypeKey, Vehicle> uniqueTypes = new HashMap<VehicleTypeKey, Vehicle>();
@Override
public Collection<Vehicle> get(VehicleRoute vehicleRoute) {
if(uniqueTypes.isEmpty()){
for( Vehicle v : vrp.getVehicles()){
if(!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())){
uniqueTypes.put(v.getVehicleTypeIdentifier(),v);
if (uniqueTypes.isEmpty()) {
for (Vehicle v : vrp.getVehicles()) {
if (!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())) {
uniqueTypes.put(v.getVehicleTypeIdentifier(), v);
}
}
}
@ -564,56 +562,57 @@ public class VehicleRoutingAlgorithms {
});
stateManager.addStateUpdater(timeWindowUpdater);
activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;
}
else{
} else {
activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED;
}
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(),activityPolicy));
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy));
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy));
final SolutionCostCalculator costCalculator;
if(solutionCostCalculator==null) costCalculator = getDefaultCostCalculator(stateManager);
if (solutionCostCalculator == null) costCalculator = getDefaultCostCalculator(stateManager);
else costCalculator = solutionCostCalculator;
PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp,vehicleFleetManager,stateManager,constraintManager);
PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager, stateManager, constraintManager);
//construct initial solution creator
final InsertionStrategy initialInsertionStrategy = createInitialSolution(config,vrp,vehicleFleetManager,stateManager,algorithmListeners,definedClasses,executorService,nuOfThreads,costCalculator, constraintManager, addDefaultCostCalculators);
if(initialInsertionStrategy != null) prettyAlgorithmBuilder.constructInitialSolutionWith(initialInsertionStrategy,costCalculator);
final InsertionStrategy initialInsertionStrategy = createInitialSolution(config, vrp, vehicleFleetManager, stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, costCalculator, constraintManager, addDefaultCostCalculators);
if (initialInsertionStrategy != null)
prettyAlgorithmBuilder.constructInitialSolutionWith(initialInsertionStrategy, costCalculator);
//construct algorithm, i.e. search-strategies and its modules
int solutionMemory = config.getInt("strategy.memory");
List<HierarchicalConfiguration> strategyConfigs = config.configurationsAt("strategy.searchStrategies.searchStrategy");
for(HierarchicalConfiguration strategyConfig : strategyConfigs){
for (HierarchicalConfiguration strategyConfig : strategyConfigs) {
String name = getName(strategyConfig);
SolutionAcceptor acceptor = getAcceptor(strategyConfig,vrp,algorithmListeners,definedClasses,solutionMemory);
SolutionSelector selector = getSelector(strategyConfig,vrp,algorithmListeners,definedClasses);
SolutionAcceptor acceptor = getAcceptor(strategyConfig, vrp, algorithmListeners, definedClasses, solutionMemory);
SolutionSelector selector = getSelector(strategyConfig, vrp, algorithmListeners, definedClasses);
SearchStrategy strategy = new SearchStrategy(name, selector, acceptor, costCalculator);
strategy.setName(name);
List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module");
for(HierarchicalConfiguration moduleConfig : modulesConfig){
SearchStrategyModule module = buildModule(moduleConfig,vrp,vehicleFleetManager,stateManager,algorithmListeners,definedClasses,executorService,nuOfThreads, constraintManager, addDefaultCostCalculators);
for (HierarchicalConfiguration moduleConfig : modulesConfig) {
SearchStrategyModule module = buildModule(moduleConfig, vrp, vehicleFleetManager, stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, constraintManager, addDefaultCostCalculators);
strategy.addModule(module);
}
prettyAlgorithmBuilder.withStrategy(strategy,strategyConfig.getDouble("probability"));
prettyAlgorithmBuilder.withStrategy(strategy, strategyConfig.getDouble("probability"));
}
//construct algorithm
VehicleRoutingAlgorithm metaAlgorithm = prettyAlgorithmBuilder.build();
int maxIterations = getMaxIterations(config);
if(maxIterations > -1) metaAlgorithm.setMaxIterations(maxIterations);
if (maxIterations > -1) metaAlgorithm.setMaxIterations(maxIterations);
//define prematureBreak
PrematureAlgorithmTermination prematureAlgorithmTermination = getPrematureTermination(config, algorithmListeners);
if(prematureAlgorithmTermination != null) metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination);
else{
if (prematureAlgorithmTermination != null)
metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination);
else {
List<HierarchicalConfiguration> terminationCriteria = config.configurationsAt("terminationCriteria.termination");
for(HierarchicalConfiguration terminationConfig : terminationCriteria){
for (HierarchicalConfiguration terminationConfig : terminationCriteria) {
PrematureAlgorithmTermination termination = getTerminationCriterion(terminationConfig, algorithmListeners);
if(termination != null) metaAlgorithm.addTerminationCriterion(termination);
if (termination != null) metaAlgorithm.addTerminationCriterion(termination);
}
}
for(PrioritizedVRAListener l : algorithmListeners){
for (PrioritizedVRAListener l : algorithmListeners) {
metaAlgorithm.getAlgorithmListeners().add(l);
}
return metaAlgorithm;
@ -621,8 +620,8 @@ public class VehicleRoutingAlgorithms {
private static int getMaxIterations(XMLConfiguration config) {
String maxIterationsString = config.getString("iterations");
if(maxIterationsString == null) maxIterationsString = config.getString("maxIterations");
if(maxIterationsString != null) return (Integer.parseInt(maxIterationsString));
if (maxIterationsString == null) maxIterationsString = config.getString("maxIterations");
if (maxIterationsString != null) return (Integer.parseInt(maxIterationsString));
return -1;
}
@ -631,11 +630,10 @@ public class VehicleRoutingAlgorithms {
}
private static VehicleFleetManager createFleetManager(final VehicleRoutingProblem vrp) {
if(vrp.getFleetSize().equals(FleetSize.INFINITE)){
if (vrp.getFleetSize().equals(FleetSize.INFINITE)) {
return new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
}
else if(vrp.getFleetSize().equals(FleetSize.FINITE)){
} else if (vrp.getFleetSize().equals(FleetSize.FINITE)) {
return new FiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
}
throw new IllegalStateException("fleet size can only be infinite or finite. " +
@ -644,32 +642,32 @@ public class VehicleRoutingAlgorithms {
private static PrematureAlgorithmTermination getTerminationCriterion(HierarchicalConfiguration config, Set<PrioritizedVRAListener> algorithmListeners) {
String basedOn = config.getString("[@basedOn]");
if(basedOn == null){
if (basedOn == null) {
log.debug("set default prematureBreak, i.e. no premature break at all.");
return null;
}
if(basedOn.equals("iterations")){
if (basedOn.equals("iterations")) {
log.debug("set prematureBreak based on iterations");
String iter = config.getString("iterations");
if(iter == null) throw new IllegalStateException("iterations is missing");
if (iter == null) throw new IllegalStateException("iterations is missing");
int iterations = Integer.valueOf(iter);
return new IterationWithoutImprovementTermination(iterations);
}
if(basedOn.equals("time")){
if (basedOn.equals("time")) {
log.debug("set prematureBreak based on time");
String timeString = config.getString("time");
if(timeString == null) throw new IllegalStateException("time is missing");
if (timeString == null) throw new IllegalStateException("time is missing");
long time = Long.parseLong(timeString);
TimeTermination timeBreaker = new TimeTermination(time);
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, timeBreaker));
return timeBreaker;
}
if(basedOn.equals("variationCoefficient")){
if (basedOn.equals("variationCoefficient")) {
log.debug("set prematureBreak based on variation coefficient");
String thresholdString = config.getString("threshold");
String iterationsString = config.getString("iterations");
if(thresholdString == null) throw new IllegalStateException("threshold is missing");
if(iterationsString == null) throw new IllegalStateException("iterations is missing");
if (thresholdString == null) throw new IllegalStateException("threshold is missing");
if (iterationsString == null) throw new IllegalStateException("iterations is missing");
double threshold = Double.valueOf(thresholdString);
int iterations = Integer.valueOf(iterationsString);
VariationCoefficientTermination variationCoefficientBreaker = new VariationCoefficientTermination(iterations, threshold);
@ -681,32 +679,32 @@ public class VehicleRoutingAlgorithms {
private static PrematureAlgorithmTermination getPrematureTermination(XMLConfiguration config, Set<PrioritizedVRAListener> algorithmListeners) {
String basedOn = config.getString("prematureBreak[@basedOn]");
if(basedOn == null){
if (basedOn == null) {
log.debug("set default prematureBreak, i.e. no premature break at all.");
return null;
}
if(basedOn.equals("iterations")){
if (basedOn.equals("iterations")) {
log.debug("set prematureBreak based on iterations");
String iter = config.getString("prematureBreak.iterations");
if(iter == null) throw new IllegalStateException("prematureBreak.iterations is missing");
if (iter == null) throw new IllegalStateException("prematureBreak.iterations is missing");
int iterations = Integer.valueOf(iter);
return new IterationWithoutImprovementTermination(iterations);
}
if(basedOn.equals("time")){
if (basedOn.equals("time")) {
log.debug("set prematureBreak based on time");
String timeString = config.getString("prematureBreak.time");
if(timeString == null) throw new IllegalStateException("prematureBreak.time is missing");
if (timeString == null) throw new IllegalStateException("prematureBreak.time is missing");
long time = Long.parseLong(timeString);
TimeTermination timeBreaker = new TimeTermination(time);
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, timeBreaker));
return timeBreaker;
}
if(basedOn.equals("variationCoefficient")){
if (basedOn.equals("variationCoefficient")) {
log.debug("set prematureBreak based on variation coefficient");
String thresholdString = config.getString("prematureBreak.threshold");
String iterationsString = config.getString("prematureBreak.iterations");
if(thresholdString == null) throw new IllegalStateException("prematureBreak.threshold is missing");
if(iterationsString == null) throw new IllegalStateException("prematureBreak.iterations is missing");
if (thresholdString == null) throw new IllegalStateException("prematureBreak.threshold is missing");
if (iterationsString == null) throw new IllegalStateException("prematureBreak.iterations is missing");
double threshold = Double.valueOf(thresholdString);
int iterations = Integer.valueOf(iterationsString);
VariationCoefficientTermination variationCoefficientBreaker = new VariationCoefficientTermination(iterations, threshold);
@ -716,7 +714,8 @@ public class VehicleRoutingAlgorithms {
throw new IllegalStateException("prematureBreak basedOn " + basedOn + " is not defined");
}
private static String getName(HierarchicalConfiguration strategyConfig) { if(strategyConfig.containsKey("[@name]")){
private static String getName(HierarchicalConfiguration strategyConfig) {
if (strategyConfig.containsKey("[@name]")) {
return strategyConfig.getString("[@name]");
}
return "";
@ -724,43 +723,44 @@ public class VehicleRoutingAlgorithms {
private static InsertionStrategy createInitialSolution(XMLConfiguration config, final VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, final StateManager routeStates, Set<PrioritizedVRAListener> algorithmListeners, TypedMap definedClasses, ExecutorService executorService, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator, ConstraintManager constraintManager, boolean addDefaultCostCalculators) {
List<HierarchicalConfiguration> modConfigs = config.configurationsAt("construction.insertion");
if(modConfigs == null) return null;
if(modConfigs.isEmpty()) return null;
if(modConfigs.size() != 1) throw new IllegalStateException("#construction.modules != 1. 1 expected");
if (modConfigs == null) return null;
if (modConfigs.isEmpty()) return null;
if (modConfigs.size() != 1) throw new IllegalStateException("#construction.modules != 1. 1 expected");
HierarchicalConfiguration modConfig = modConfigs.get(0);
String insertionName = modConfig.getString("[@name]");
if(insertionName == null) throw new IllegalStateException("insertion[@name] is missing.");
if (insertionName == null) throw new IllegalStateException("insertion[@name] is missing.");
String insertionId = modConfig.getString("[@id]");
if(insertionId == null) insertionId = "noId";
ModKey modKey = makeKey(insertionName,insertionId);
if (insertionId == null) insertionId = "noId";
ModKey modKey = makeKey(insertionName, insertionId);
InsertionStrategyKey insertionStrategyKey = new InsertionStrategyKey(modKey);
InsertionStrategy insertionStrategy = definedClasses.get(insertionStrategyKey);
if(insertionStrategy == null){
if (insertionStrategy == null) {
List<PrioritizedVRAListener> prioListeners = new ArrayList<PrioritizedVRAListener>();
insertionStrategy = createInsertionStrategy(modConfig, vrp, vehicleFleetManager, routeStates, prioListeners, executorService, nuOfThreads, constraintManager, addDefaultCostCalculators);
algorithmListeners.addAll(prioListeners);
definedClasses.put(insertionStrategyKey,insertionStrategy);
definedClasses.put(insertionStrategyKey, insertionStrategy);
}
return insertionStrategy;
}
private static SolutionSelector getSelector(HierarchicalConfiguration strategyConfig, VehicleRoutingProblem vrp, Set<PrioritizedVRAListener> algorithmListeners, TypedMap definedSelectors) {
String selectorName = strategyConfig.getString("selector[@name]");
if(selectorName == null) throw new IllegalStateException("no solutionSelector defined. define either \"selectRandomly\" or \"selectBest\"");
if (selectorName == null)
throw new IllegalStateException("no solutionSelector defined. define either \"selectRandomly\" or \"selectBest\"");
String selectorId = strategyConfig.getString("selector[@id]");
if(selectorId == null) selectorId="noId";
ModKey modKey = makeKey(selectorName,selectorId);
if (selectorId == null) selectorId = "noId";
ModKey modKey = makeKey(selectorName, selectorId);
SelectorKey selectorKey = new SelectorKey(modKey);
SolutionSelector definedSelector = definedSelectors.get(selectorKey);
if(definedSelector != null) {
if (definedSelector != null) {
return definedSelector;
}
if(selectorName.equals("selectRandomly")){
if (selectorName.equals("selectRandomly")) {
SelectRandomly selector = SelectRandomly.getInstance();
definedSelectors.put(selectorKey, selector);
return selector;
}
if(selectorName.equals("selectBest")){
if (selectorName.equals("selectBest")) {
SelectBest selector = SelectBest.getInstance();
definedSelectors.put(selectorKey, selector);
return selector;
@ -768,47 +768,46 @@ public class VehicleRoutingAlgorithms {
throw new IllegalStateException("solutionSelector is not know. Currently, it only knows \"selectRandomly\" and \"selectBest\"");
}
private static ModKey makeKey(String name, String id){
private static ModKey makeKey(String name, String id) {
return new ModKey(name, id);
}
private static SolutionAcceptor getAcceptor(HierarchicalConfiguration strategyConfig, VehicleRoutingProblem vrp, Set<PrioritizedVRAListener> algorithmListeners, TypedMap typedMap, int solutionMemory) {
String acceptorName = strategyConfig.getString("acceptor[@name]");
if(acceptorName == null) throw new IllegalStateException("no solution acceptor is defined");
if (acceptorName == null) throw new IllegalStateException("no solution acceptor is defined");
String acceptorId = strategyConfig.getString("acceptor[@id]");
if(acceptorId == null) acceptorId = "noId";
AcceptorKey acceptorKey = new AcceptorKey(makeKey(acceptorName,acceptorId));
if (acceptorId == null) acceptorId = "noId";
AcceptorKey acceptorKey = new AcceptorKey(makeKey(acceptorName, acceptorId));
SolutionAcceptor definedAcceptor = typedMap.get(acceptorKey);
if(definedAcceptor != null) return definedAcceptor;
if(acceptorName.equals("acceptNewRemoveWorst")){
if (definedAcceptor != null) return definedAcceptor;
if (acceptorName.equals("acceptNewRemoveWorst")) {
GreedyAcceptance acceptor = new GreedyAcceptance(solutionMemory);
typedMap.put(acceptorKey, acceptor);
return acceptor;
}
if(acceptorName.equals("acceptNewRemoveFirst")){
if (acceptorName.equals("acceptNewRemoveFirst")) {
AcceptNewRemoveFirst acceptor = new AcceptNewRemoveFirst(solutionMemory);
typedMap.put(acceptorKey, acceptor);
return acceptor;
}
if(acceptorName.equals("greedyAcceptance")){
if (acceptorName.equals("greedyAcceptance")) {
GreedyAcceptance acceptor = new GreedyAcceptance(solutionMemory);
typedMap.put(acceptorKey, acceptor);
return acceptor;
}
if(acceptorName.equals("greedyAcceptance_minVehFirst")){
if (acceptorName.equals("greedyAcceptance_minVehFirst")) {
GreedyAcceptance_minVehFirst acceptor = new GreedyAcceptance_minVehFirst(solutionMemory);
typedMap.put(acceptorKey, acceptor);
return acceptor;
}
if(acceptorName.equals("schrimpfAcceptance")){
if (acceptorName.equals("schrimpfAcceptance")) {
String nuWarmupIterations = strategyConfig.getString("acceptor.warmup");
double alpha = strategyConfig.getDouble("acceptor.alpha");
SchrimpfAcceptance schrimpf = new SchrimpfAcceptance(solutionMemory, alpha);
if(nuWarmupIterations!=null){
if (nuWarmupIterations != null) {
SchrimpfInitialThresholdGenerator iniThresholdGenerator = new SchrimpfInitialThresholdGenerator(schrimpf, Integer.parseInt(nuWarmupIterations));
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, iniThresholdGenerator));
}
else{
} else {
double threshold = strategyConfig.getDouble("acceptor.initialThreshold");
schrimpf.setInitialThreshold(threshold);
}
@ -816,15 +815,14 @@ public class VehicleRoutingAlgorithms {
typedMap.put(acceptorKey, schrimpf);
return schrimpf;
}
if(acceptorName.equals("experimentalSchrimpfAcceptance")){
if (acceptorName.equals("experimentalSchrimpfAcceptance")) {
int iterOfSchrimpf = strategyConfig.getInt("acceptor.warmup");
double alpha = strategyConfig.getDouble("acceptor.alpha");
ExperimentalSchrimpfAcceptance schrimpf = new ExperimentalSchrimpfAcceptance(solutionMemory, alpha, iterOfSchrimpf);
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, schrimpf));
typedMap.put(acceptorKey, schrimpf);
return schrimpf;
}
else{
} else {
throw new IllegalStateException("solution acceptor " + acceptorName + " is not known");
}
}
@ -832,43 +830,43 @@ public class VehicleRoutingAlgorithms {
private static SearchStrategyModule buildModule(HierarchicalConfiguration moduleConfig, final VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager,
final StateManager routeStates, Set<PrioritizedVRAListener> algorithmListeners, TypedMap definedClasses, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators) {
String moduleName = moduleConfig.getString("[@name]");
if(moduleName == null) throw new IllegalStateException("module(-name) is missing.");
if (moduleName == null) throw new IllegalStateException("module(-name) is missing.");
String moduleId = moduleConfig.getString("[@id]");
if(moduleId == null) moduleId = "noId";
ModKey modKey = makeKey(moduleName,moduleId);
if (moduleId == null) moduleId = "noId";
ModKey modKey = makeKey(moduleName, moduleId);
StrategyModuleKey strategyModuleKey = new StrategyModuleKey(modKey);
SearchStrategyModule definedModule = definedClasses.get(strategyModuleKey);
if(definedModule != null) return definedModule;
if (definedModule != null) return definedModule;
if(moduleName.equals("ruin_and_recreate")){
if (moduleName.equals("ruin_and_recreate")) {
String ruin_name = moduleConfig.getString("ruin[@name]");
if(ruin_name == null) throw new IllegalStateException("module.ruin[@name] is missing.");
if (ruin_name == null) throw new IllegalStateException("module.ruin[@name] is missing.");
String ruin_id = moduleConfig.getString("ruin[@id]");
if(ruin_id == null) ruin_id = "noId";
if (ruin_id == null) ruin_id = "noId";
String shareToRuinString = moduleConfig.getString("ruin.share");
if(shareToRuinString == null) throw new IllegalStateException("module.ruin.share is missing.");
if (shareToRuinString == null) throw new IllegalStateException("module.ruin.share is missing.");
double shareToRuin = Double.valueOf(shareToRuinString);
final RuinStrategy ruin;
ModKey ruinKey = makeKey(ruin_name,ruin_id);
if(ruin_name.equals("randomRuin")){
ModKey ruinKey = makeKey(ruin_name, ruin_id);
if (ruin_name.equals("randomRuin")) {
ruin = getRandomRuin(vrp, routeStates, definedClasses, ruinKey, shareToRuin);
}
else if(ruin_name.equals("radialRuin")){
} else if (ruin_name.equals("radialRuin")) {
JobDistance jobDistance = new AvgServiceAndShipmentDistance(vrp.getTransportCosts());
ruin = getRadialRuin(vrp, routeStates, definedClasses, ruinKey, shareToRuin, jobDistance);
}
else throw new IllegalStateException("ruin[@name] " + ruin_name + " is not known. Use either randomRuin or radialRuin.");
} else
throw new IllegalStateException("ruin[@name] " + ruin_name + " is not known. Use either randomRuin or radialRuin.");
String insertionName = moduleConfig.getString("insertion[@name]");
if(insertionName == null) throw new IllegalStateException("module.insertion[@name] is missing. set it to \"regretInsertion\" or \"bestInsertion\"");
if (insertionName == null)
throw new IllegalStateException("module.insertion[@name] is missing. set it to \"regretInsertion\" or \"bestInsertion\"");
String insertionId = moduleConfig.getString("insertion[@id]");
if(insertionId == null) insertionId = "noId";
ModKey insertionKey = makeKey(insertionName,insertionId);
if (insertionId == null) insertionId = "noId";
ModKey insertionKey = makeKey(insertionName, insertionId);
InsertionStrategyKey insertionStrategyKey = new InsertionStrategyKey(insertionKey);
InsertionStrategy insertion = definedClasses.get(insertionStrategyKey);
if(insertion == null){
if (insertion == null) {
List<HierarchicalConfiguration> insertionConfigs = moduleConfig.configurationsAt("insertion");
if(insertionConfigs.size() != 1) throw new IllegalStateException("this should be 1");
if (insertionConfigs.size() != 1) throw new IllegalStateException("this should be 1");
List<PrioritizedVRAListener> prioListeners = new ArrayList<PrioritizedVRAListener>();
insertion = createInsertionStrategy(insertionConfigs.get(0), vrp, vehicleFleetManager, routeStates, prioListeners, executorService, nuOfThreads, constraintManager, addDefaultCostCalculators);
algorithmListeners.addAll(prioListeners);
@ -889,7 +887,7 @@ public class VehicleRoutingAlgorithms {
private static RuinStrategy getRadialRuin(final VehicleRoutingProblem vrp, final StateManager routeStates, TypedMap definedClasses, ModKey modKey, double shareToRuin, JobDistance jobDistance) {
RuinStrategyKey stratKey = new RuinStrategyKey(modKey);
RuinStrategy ruin = definedClasses.get(stratKey);
if(ruin == null){
if (ruin == null) {
ruin = new RadialRuinStrategyFactory(shareToRuin, jobDistance).createStrategy(vrp);
definedClasses.put(stratKey, ruin);
}
@ -899,18 +897,16 @@ public class VehicleRoutingAlgorithms {
private static RuinStrategy getRandomRuin(final VehicleRoutingProblem vrp, final StateManager routeStates, TypedMap definedClasses, ModKey modKey, double shareToRuin) {
RuinStrategyKey stratKey = new RuinStrategyKey(modKey);
RuinStrategy ruin = definedClasses.get(stratKey);
if(ruin == null){
if (ruin == null) {
ruin = new RandomRuinStrategyFactory(shareToRuin).createStrategy(vrp);
definedClasses.put(stratKey, ruin);
}
return ruin;
}
private static InsertionStrategy createInsertionStrategy(HierarchicalConfiguration moduleConfig, VehicleRoutingProblem vrp,VehicleFleetManager vehicleFleetManager, StateManager routeStates, List<PrioritizedVRAListener> algorithmListeners, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators) {
private static InsertionStrategy createInsertionStrategy(HierarchicalConfiguration moduleConfig, VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, StateManager routeStates, List<PrioritizedVRAListener> algorithmListeners, ExecutorService executorService, int nuOfThreads, ConstraintManager constraintManager, boolean addDefaultCostCalculators) {
return InsertionFactory.createInsertion(vrp, moduleConfig, vehicleFleetManager, routeStates, algorithmListeners, executorService, nuOfThreads, constraintManager, addDefaultCostCalculators);
}
}

View file

@ -22,9 +22,7 @@ import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import java.util.Collection;
public interface AlgorithmEndsListener extends VehicleRoutingAlgorithmListener{
public interface AlgorithmEndsListener extends VehicleRoutingAlgorithmListener {
void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions);

View file

@ -23,9 +23,7 @@ import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import java.util.Collection;
public interface AlgorithmStartsListener extends VehicleRoutingAlgorithmListener{
public interface AlgorithmStartsListener extends VehicleRoutingAlgorithmListener {
void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions);

View file

@ -22,10 +22,7 @@ import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import java.util.Collection;
public interface IterationEndsListener extends VehicleRoutingAlgorithmListener{
public interface IterationEndsListener extends VehicleRoutingAlgorithmListener {
public void informIterationEnds(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions);

View file

@ -22,9 +22,7 @@ import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import java.util.Collection;
public interface IterationStartsListener extends VehicleRoutingAlgorithmListener{
public interface IterationStartsListener extends VehicleRoutingAlgorithmListener {
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions);

View file

@ -16,6 +16,6 @@
******************************************************************************/
package jsprit.core.algorithm.listener;
public interface SearchStrategyListener extends VehicleRoutingAlgorithmListener{
public interface SearchStrategyListener extends VehicleRoutingAlgorithmListener {
}

View file

@ -16,6 +16,6 @@
******************************************************************************/
package jsprit.core.algorithm.listener;
public interface SearchStrategyModuleListener extends VehicleRoutingAlgorithmListener{
public interface SearchStrategyModuleListener extends VehicleRoutingAlgorithmListener {
}

View file

@ -23,9 +23,7 @@ import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import java.util.Collection;
public interface StrategySelectedListener extends VehicleRoutingAlgorithmListener{
public interface StrategySelectedListener extends VehicleRoutingAlgorithmListener {
void informSelectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem vehicleRoutingProblem, Collection<VehicleRoutingProblemSolution> vehicleRoutingProblemSolutions);

View file

@ -24,57 +24,27 @@ import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import java.util.*;
public class VehicleRoutingAlgorithmListeners {
public static class PrioritizedVRAListener {
Priority priority;
VehicleRoutingAlgorithmListener l;
public PrioritizedVRAListener(Priority priority, VehicleRoutingAlgorithmListener l) {
super();
this.priority = priority;
this.l = l;
}
public Priority getPriority() {
return priority;
}
public VehicleRoutingAlgorithmListener getListener() {
return l;
}
// @Override
// public int hashCode() {
// final int prime = 31;
// int result = 1;
// result = prime * result + ((l == null) ? 0 : l.hashCode());
// result = prime * result
// + ((priority == null) ? 0 : priority.hashCode());
// return result;
// }
// @Override
// public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
// PrioritizedVRAListener other = (PrioritizedVRAListener) obj;
// if (l == null) {
// if (other.l != null)
// return false;
// } else if (!l.equals(other.l))
// return false;
// if (priority == null) {
// if (other.priority != null)
// return false;
// } else if (!priority.equals(other.priority))
// return false;
// return true;
// }
}
public enum Priority {
@ -86,17 +56,14 @@ public class VehicleRoutingAlgorithmListeners {
@Override
public int compare(PrioritizedVRAListener o1, PrioritizedVRAListener o2) {
if(o1 == o2) return 0;
if(o1.getPriority() == Priority.HIGH && o2.getPriority() != Priority.HIGH){
if (o1 == o2) return 0;
if (o1.getPriority() == Priority.HIGH && o2.getPriority() != Priority.HIGH) {
return -1;
}
else if(o2.getPriority() == Priority.HIGH && o1.getPriority() != Priority.HIGH){
} else if (o2.getPriority() == Priority.HIGH && o1.getPriority() != Priority.HIGH) {
return 1;
}
else if(o1.getPriority() == Priority.MEDIUM && o2.getPriority() != Priority.MEDIUM){
} else if (o1.getPriority() == Priority.MEDIUM && o2.getPriority() != Priority.MEDIUM) {
return -1;
}
else if(o2.getPriority() == Priority.MEDIUM && o1.getPriority() != Priority.MEDIUM){
} else if (o2.getPriority() == Priority.MEDIUM && o1.getPriority() != Priority.MEDIUM) {
return 1;
}
return 1;
@ -106,76 +73,76 @@ public class VehicleRoutingAlgorithmListeners {
public Collection<VehicleRoutingAlgorithmListener> getAlgorithmListeners() {
List<VehicleRoutingAlgorithmListener> list = new ArrayList<VehicleRoutingAlgorithmListener>();
for(PrioritizedVRAListener l : algorithmListeners){
for (PrioritizedVRAListener l : algorithmListeners) {
list.add(l.getListener());
}
return Collections.unmodifiableCollection(list);
}
public void remove(PrioritizedVRAListener listener){
public void remove(PrioritizedVRAListener listener) {
boolean removed = algorithmListeners.remove(listener);
if(!removed){ throw new IllegalStateException("cannot remove listener"); }
if (!removed) {
throw new IllegalStateException("cannot remove listener");
}
}
public void addListener(VehicleRoutingAlgorithmListener listener, Priority priority){
public void addListener(VehicleRoutingAlgorithmListener listener, Priority priority) {
algorithmListeners.add(new PrioritizedVRAListener(priority, listener));
}
public void addListener(VehicleRoutingAlgorithmListener listener){
public void addListener(VehicleRoutingAlgorithmListener listener) {
addListener(listener, Priority.LOW);
}
public void algorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
for(PrioritizedVRAListener l : algorithmListeners){
if(l.getListener() instanceof AlgorithmEndsListener){
((AlgorithmEndsListener)l.getListener()).informAlgorithmEnds(problem, solutions);
for (PrioritizedVRAListener l : algorithmListeners) {
if (l.getListener() instanceof AlgorithmEndsListener) {
((AlgorithmEndsListener) l.getListener()).informAlgorithmEnds(problem, solutions);
}
}
}
public void iterationEnds(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
for(PrioritizedVRAListener l : algorithmListeners){
if(l.getListener() instanceof IterationEndsListener){
((IterationEndsListener)l.getListener()).informIterationEnds(i,problem, solutions);
for (PrioritizedVRAListener l : algorithmListeners) {
if (l.getListener() instanceof IterationEndsListener) {
((IterationEndsListener) l.getListener()).informIterationEnds(i, problem, solutions);
}
}
}
public void iterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
for(PrioritizedVRAListener l : algorithmListeners){
if(l.getListener() instanceof IterationStartsListener){
((IterationStartsListener)l.getListener()).informIterationStarts(i,problem, solutions);
for (PrioritizedVRAListener l : algorithmListeners) {
if (l.getListener() instanceof IterationStartsListener) {
((IterationStartsListener) l.getListener()).informIterationStarts(i, problem, solutions);
}
}
}
public void algorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
for(PrioritizedVRAListener l : algorithmListeners){
if(l.getListener() instanceof AlgorithmStartsListener){
((AlgorithmStartsListener)l.getListener()).informAlgorithmStarts(problem, algorithm, solutions);
for (PrioritizedVRAListener l : algorithmListeners) {
if (l.getListener() instanceof AlgorithmStartsListener) {
((AlgorithmStartsListener) l.getListener()).informAlgorithmStarts(problem, algorithm, solutions);
}
}
}
public void add(PrioritizedVRAListener l){
public void add(PrioritizedVRAListener l) {
algorithmListeners.add(l);
}
public void addAll(Collection<PrioritizedVRAListener> algorithmListeners) {
for(PrioritizedVRAListener l : algorithmListeners){
for (PrioritizedVRAListener l : algorithmListeners) {
this.algorithmListeners.add(l);
}
}
public void selectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
for(PrioritizedVRAListener l : algorithmListeners){
if(l.getListener() instanceof StrategySelectedListener){
((StrategySelectedListener)l.getListener()).informSelectedStrategy(discoveredSolution, problem, solutions);
for (PrioritizedVRAListener l : algorithmListeners) {
if (l.getListener() instanceof StrategySelectedListener) {
((StrategySelectedListener) l.getListener()).informSelectedStrategy(discoveredSolution, problem, solutions);
}
}
}

View file

@ -30,7 +30,7 @@ import java.util.HashSet;
import java.util.Set;
public class RuinAndRecreateModule implements SearchStrategyModule{
public class RuinAndRecreateModule implements SearchStrategyModule {
private InsertionStrategy insertion;
@ -65,15 +65,15 @@ public class RuinAndRecreateModule implements SearchStrategyModule{
@Override
public void addModuleListener(SearchStrategyModuleListener moduleListener) {
if(moduleListener instanceof InsertionListener){
if (moduleListener instanceof InsertionListener) {
InsertionListener iListener = (InsertionListener) moduleListener;
if(!insertion.getListeners().contains(iListener)){
if (!insertion.getListeners().contains(iListener)) {
insertion.addListener(iListener);
}
}
if(moduleListener instanceof RuinListener){
if (moduleListener instanceof RuinListener) {
RuinListener rListener = (RuinListener) moduleListener;
if(!ruin.getListeners().contains(rListener)){
if (!ruin.getListeners().contains(rListener)) {
ruin.addListener(rListener);
}
}

View file

@ -33,7 +33,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Random;
public abstract class AbstractInsertionStrategy implements InsertionStrategy{
public abstract class AbstractInsertionStrategy implements InsertionStrategy {
protected class Insertion {
@ -84,9 +84,9 @@ public abstract class AbstractInsertionStrategy implements InsertionStrategy{
}
@Override
public Collection<Job> insertJobs(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs){
public Collection<Job> insertJobs(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) {
insertionsListeners.informInsertionStarts(vehicleRoutes, unassignedJobs);
Collection<Job> badJobs = insertUnassignedJobs(vehicleRoutes,unassignedJobs);
Collection<Job> badJobs = insertUnassignedJobs(vehicleRoutes, unassignedJobs);
insertionsListeners.informInsertionEndsListeners(vehicleRoutes);
return badJobs;
}
@ -109,13 +109,13 @@ public abstract class AbstractInsertionStrategy implements InsertionStrategy{
}
protected void insertJob(Job unassignedJob, InsertionData iData, VehicleRoute inRoute){
logger.trace("insert: [jobId=" + unassignedJob.getId() + "]" + iData );
protected void insertJob(Job unassignedJob, InsertionData iData, VehicleRoute inRoute) {
logger.trace("insert: [jobId={}]{}", unassignedJob.getId(), iData);
insertionsListeners.informBeforeJobInsertion(unassignedJob, iData, inRoute);
if(!(inRoute.getVehicle().getId().equals(iData.getSelectedVehicle().getId()))){
if (!(inRoute.getVehicle().getId().equals(iData.getSelectedVehicle().getId()))) {
insertionsListeners.informVehicleSwitched(inRoute, inRoute.getVehicle(), iData.getSelectedVehicle());
}
for(Event e : iData.getEvents()){
for (Event e : iData.getEvents()) {
eventListeners.inform(e);
}
insertionsListeners.informJobInserted(unassignedJob, inRoute, iData.getInsertionCost(), iData.getAdditionalTime());

View file

@ -1,4 +1,3 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
@ -27,17 +26,20 @@ public interface ActivityInsertionCostsCalculator {
private double additionalCosts;
private double additionalTime;
public ActivityInsertionCosts(double additionalCosts, double additionalTime) {
super();
this.additionalCosts = additionalCosts;
this.additionalTime = additionalTime;
}
/**
* @return the additionalCosts
*/
public double getAdditionalCosts() {
return additionalCosts;
}
/**
* @return the additionalTime
*/
@ -46,7 +48,6 @@ public interface ActivityInsertionCostsCalculator {
}
}
public double getCosts(JobInsertionContext iContext, TourActivity prevAct, TourActivity nextAct, TourActivity newAct, double depTimeAtPrevAct);

View file

@ -25,11 +25,10 @@ import jsprit.core.problem.vehicle.Vehicle;
/**
* Estimates additional access/egress costs when operating route with a new vehicle that has different start/end-location.
*
* <p/>
* <p>If two vehicles have the same start/end-location and departure-time .getCosts(...) must return zero.
*
* @author schroeder
*
*/
class AdditionalAccessEgressCalculator {
@ -37,34 +36,33 @@ class AdditionalAccessEgressCalculator {
/**
* Constructs the estimator that estimates additional access/egress costs when operating route with a new vehicle that has different start/end-location.
*
* <p/>
* <p>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){
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()){
if (!currentRoute.isEmpty()) {
double accessTransportCostNew = routingCosts.getTransportCost(newVehicle.getStartLocation(), currentRoute.getActivities().get(0).getLocation(), newVehicleDepartureTime, newDriver, newVehicle);
double accessTransportCostOld = routingCosts.getTransportCost(currentRoute.getStart().getLocation(), currentRoute.getActivities().get(0).getLocation(), currentRoute.getDepartureTime(), currentRoute.getDriver(), currentRoute.getVehicle());
delta_access = accessTransportCostNew - accessTransportCostOld;
if(newVehicle.isReturnToDepot()){
TourActivity lastActivityBeforeEndOfRoute = currentRoute.getActivities().get(currentRoute.getActivities().size()-1);
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.getLocation(), newVehicle.getEndLocation() , lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime, newDriver, newVehicle);
double egressTransportCostNew = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocation(), newVehicle.getEndLocation(), lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime, newDriver, newVehicle);
double egressTransportCostOld = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocation(), currentRoute.getEnd().getLocation(), lastActivityEndTimeWithOldVehicleAndDepartureTime, currentRoute.getDriver(), currentRoute.getVehicle());
delta_egress = egressTransportCostNew - egressTransportCostOld;

View file

@ -27,7 +27,6 @@ import java.util.Iterator;
import java.util.List;
final class AuxilliaryCostCalculator {
private final VehicleRoutingTransportCosts routingCosts;
@ -41,15 +40,14 @@ final class AuxilliaryCostCalculator {
}
/**
*
* @param path activity path to get the costs for
* @param depTime departure time at first activity in path
* @param driver driver of vehicle
* @param vehicle vehicle running the path
* @return cost of path
*/
public double costOfPath(final List<TourActivity> path, final double depTime, final Driver driver, final Vehicle vehicle){
if(path.isEmpty()){
public double costOfPath(final List<TourActivity> path, final double depTime, final Driver driver, final Vehicle vehicle) {
if (path.isEmpty()) {
return 0.0;
}
double cost = 0.0;
@ -58,10 +56,10 @@ final class AuxilliaryCostCalculator {
double startCost = 0.0;
cost += startCost;
double departureTimePrevAct = depTime;
while(actIter.hasNext()){
while (actIter.hasNext()) {
TourActivity act = actIter.next();
if(act instanceof End){
if(!vehicle.isReturnToDepot()){
if (act instanceof End) {
if (!vehicle.isReturnToDepot()) {
return cost;
}
}

View file

@ -30,15 +30,12 @@ import java.util.Collections;
import java.util.List;
/**
* Best insertion that insert the job where additional costs are minimal.
*
* @author stefan schroeder
*
*/
public final class BestInsertion extends AbstractInsertionStrategy{
public final class BestInsertion extends AbstractInsertionStrategy {
private static Logger logger = LogManager.getLogger(BestInsertion.class);
@ -56,7 +53,7 @@ public final class BestInsertion extends AbstractInsertionStrategy{
public BestInsertion(JobInsertionCostsCalculator jobInsertionCalculator, VehicleRoutingProblem vehicleRoutingProblem) {
super(vehicleRoutingProblem);
bestInsertionCostCalculator = jobInsertionCalculator;
logger.debug("initialise " + this);
logger.debug("initialise {}", this);
}
@Override
@ -69,28 +66,28 @@ public final class BestInsertion extends AbstractInsertionStrategy{
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
List<Job> unassignedJobList = new ArrayList<Job>(unassignedJobs);
Collections.shuffle(unassignedJobList, random);
for(Job unassignedJob : unassignedJobList){
for (Job unassignedJob : unassignedJobList) {
Insertion bestInsertion = null;
double bestInsertionCost = Double.MAX_VALUE;
for(VehicleRoute vehicleRoute : vehicleRoutes){
for (VehicleRoute vehicleRoute : vehicleRoutes) {
InsertionData iData = bestInsertionCostCalculator.getInsertionData(vehicleRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost);
if(iData instanceof NoInsertionFound) {
if (iData instanceof NoInsertionFound) {
continue;
}
if(iData.getInsertionCost() < bestInsertionCost + noiseMaker.makeNoise()){
bestInsertion = new Insertion(vehicleRoute,iData);
if (iData.getInsertionCost() < bestInsertionCost + noiseMaker.makeNoise()) {
bestInsertion = new Insertion(vehicleRoute, iData);
bestInsertionCost = iData.getInsertionCost();
}
}
VehicleRoute newRoute = VehicleRoute.emptyRoute();
InsertionData newIData = bestInsertionCostCalculator.getInsertionData(newRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost);
if(!(newIData instanceof NoInsertionFound)){
if(newIData.getInsertionCost() < bestInsertionCost + noiseMaker.makeNoise()){
bestInsertion = new Insertion(newRoute,newIData);
if (!(newIData instanceof NoInsertionFound)) {
if (newIData.getInsertionCost() < bestInsertionCost + noiseMaker.makeNoise()) {
bestInsertion = new Insertion(newRoute, newIData);
vehicleRoutes.add(newRoute);
}
}
if(bestInsertion == null) badJobs.add(unassignedJob);
if (bestInsertion == null) badJobs.add(unassignedJob);
else insertJob(unassignedJob, bestInsertion.getInsertionData(), bestInsertion.getRoute());
}
return badJobs;

View file

@ -58,11 +58,11 @@ public class BestInsertionBuilder {
private int nNeighbors;
private boolean timeScheduling=false;
private boolean timeScheduling = false;
private boolean allowVehicleSwitch=true;
private boolean allowVehicleSwitch = true;
private boolean addDefaultCostCalc=true;
private boolean addDefaultCostCalc = true;
public BestInsertionBuilder(VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, StateManager stateManager, ConstraintManager constraintManager) {
super();
@ -72,25 +72,31 @@ public class BestInsertionBuilder {
this.fleetManager = vehicleFleetManager;
}
public BestInsertionBuilder setRouteLevel(int forwardLooking, int memory){
public BestInsertionBuilder setRouteLevel(int forwardLooking, int memory) {
local = false;
this.forwaredLooking = forwardLooking;
this.memory = memory;
return this;
};
}
public BestInsertionBuilder setRouteLevel(int forwardLooking, int memory, boolean addDefaultMarginalCostCalculation){
;
public BestInsertionBuilder setRouteLevel(int forwardLooking, int memory, boolean addDefaultMarginalCostCalculation) {
local = false;
this.forwaredLooking = forwardLooking;
this.memory = memory;
this.addDefaultCostCalc = addDefaultMarginalCostCalculation;
return this;
};
}
public BestInsertionBuilder setLocalLevel(){
;
public BestInsertionBuilder setLocalLevel() {
local = true;
return this;
};
}
;
/**
* If addDefaulMarginalCostCalculation is false, no calculator is set which implicitly assumes that marginal cost calculation
@ -99,24 +105,26 @@ public class BestInsertionBuilder {
* @param addDefaultMarginalCostCalculation
* @return
*/
public BestInsertionBuilder setLocalLevel(boolean addDefaultMarginalCostCalculation){
public BestInsertionBuilder setLocalLevel(boolean addDefaultMarginalCostCalculation) {
local = true;
addDefaultCostCalc = addDefaultMarginalCostCalculation;
return this;
}
public BestInsertionBuilder considerFixedCosts(double weightOfFixedCosts){
public BestInsertionBuilder considerFixedCosts(double weightOfFixedCosts) {
this.weightOfFixedCosts = weightOfFixedCosts;
this.considerFixedCosts = true;
return this;
}
public BestInsertionBuilder setActivityInsertionCostCalculator(ActivityInsertionCostsCalculator activityInsertionCostsCalculator){
public BestInsertionBuilder setActivityInsertionCostCalculator(ActivityInsertionCostsCalculator activityInsertionCostsCalculator) {
this.actInsertionCostsCalculator = activityInsertionCostsCalculator;
return this;
};
}
public BestInsertionBuilder setConcurrentMode(ExecutorService executor, int nuOfThreads){
;
public BestInsertionBuilder setConcurrentMode(ExecutorService executor, int nuOfThreads) {
this.executor = executor;
this.nuOfThreads = nuOfThreads;
return this;
@ -127,10 +135,9 @@ public class BestInsertionBuilder {
List<InsertionListener> iListeners = new ArrayList<InsertionListener>();
List<PrioritizedVRAListener> algorithmListeners = new ArrayList<PrioritizedVRAListener>();
JobInsertionCostsCalculatorBuilder calcBuilder = new JobInsertionCostsCalculatorBuilder(iListeners, algorithmListeners);
if(local){
if (local) {
calcBuilder.setLocalLevel(addDefaultCostCalc);
}
else {
} else {
calcBuilder.setRouteLevel(forwaredLooking, memory, addDefaultCostCalc);
}
calcBuilder.setConstraintManager(constraintManager);
@ -138,35 +145,34 @@ public class BestInsertionBuilder {
calcBuilder.setVehicleRoutingProblem(vrp);
calcBuilder.setVehicleFleetManager(fleetManager);
calcBuilder.setActivityInsertionCostsCalculator(actInsertionCostsCalculator);
if(considerFixedCosts) {
if (considerFixedCosts) {
calcBuilder.considerFixedCosts(weightOfFixedCosts);
}
if(timeScheduling){
if (timeScheduling) {
calcBuilder.experimentalTimeScheduler(timeSlice, nNeighbors);
}
calcBuilder.setAllowVehicleSwitch(allowVehicleSwitch);
JobInsertionCostsCalculator jobInsertions = calcBuilder.build();
InsertionStrategy bestInsertion;
if(executor == null){
bestInsertion = new BestInsertion(jobInsertions,vrp);
if (executor == null) {
bestInsertion = new BestInsertion(jobInsertions, vrp);
} else {
bestInsertion = new BestInsertionConcurrent(jobInsertions, executor, nuOfThreads, vrp);
}
else{
bestInsertion = new BestInsertionConcurrent(jobInsertions,executor,nuOfThreads,vrp);
}
for(InsertionListener l : iListeners) bestInsertion.addListener(l);
for (InsertionListener l : iListeners) bestInsertion.addListener(l);
return bestInsertion;
}
/**
* @deprecated this is experimental and can disappear.
* @param timeSlice the time slice
* @param nNeighbors number of neighbors
* @deprecated this is experimental and can disappear.
*/
@Deprecated
public void experimentalTimeScheduler(double timeSlice, int nNeighbors) {
this.timeSlice=timeSlice;
this.nNeighbors=nNeighbors;
timeScheduling=true;
this.timeSlice = timeSlice;
this.nNeighbors = nNeighbors;
timeScheduling = true;
}
public void setAllowVehicleSwitch(boolean allowVehicleSwitch) {
@ -174,6 +180,4 @@ public class BestInsertionBuilder {
}
}

View file

@ -30,15 +30,11 @@ import java.util.*;
import java.util.concurrent.*;
/**
*
* @author stefan schroeder
*
*/
public final class BestInsertionConcurrent extends AbstractInsertionStrategy{
public final class BestInsertionConcurrent extends AbstractInsertionStrategy {
static class Batch {
List<VehicleRoute> routes = new ArrayList<VehicleRoute>();
@ -94,7 +90,7 @@ public final class BestInsertionConcurrent extends AbstractInsertionStrategy{
this.nuOfBatches = nuOfBatches;
bestInsertionCostCalculator = jobInsertionCalculator;
completionService = new ExecutorCompletionService<Insertion>(executorService);
logger.debug("initialise " + this);
logger.debug("initialise {}", this);
}
@Override
@ -107,16 +103,16 @@ public final class BestInsertionConcurrent extends AbstractInsertionStrategy{
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
List<Job> unassignedJobList = new ArrayList<Job>(unassignedJobs);
Collections.shuffle(unassignedJobList, random);
List<Batch> batches = distributeRoutes(vehicleRoutes,nuOfBatches);
for(final Job unassignedJob : unassignedJobList){
List<Batch> batches = distributeRoutes(vehicleRoutes, nuOfBatches);
for (final Job unassignedJob : unassignedJobList) {
Insertion bestInsertion = null;
double bestInsertionCost = Double.MAX_VALUE;
for(final Batch batch : batches){
for (final Batch batch : batches) {
completionService.submit(new Callable<Insertion>() {
@Override
public Insertion call() throws Exception {
return getBestInsertion(batch,unassignedJob);
return getBestInsertion(batch, unassignedJob);
}
});
@ -131,22 +127,19 @@ public final class BestInsertionConcurrent extends AbstractInsertionStrategy{
bestInsertionCost = insertion.getInsertionData().getInsertionCost();
}
}
} catch(InterruptedException e){
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
catch (ExecutionException e) {
e.printStackTrace();
logger.error(e.getCause().toString());
System.exit(1);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
VehicleRoute newRoute = VehicleRoute.emptyRoute();
InsertionData newIData = bestInsertionCostCalculator.getInsertionData(newRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost);
if(newIData.getInsertionCost() < bestInsertionCost){
bestInsertion = new Insertion(newRoute,newIData);
if (newIData.getInsertionCost() < bestInsertionCost) {
bestInsertion = new Insertion(newRoute, newIData);
vehicleRoutes.add(newRoute);
batches.get(random.nextInt(batches.size())).routes.add(newRoute);
}
if(bestInsertion == null) badJobs.add(unassignedJob);
if (bestInsertion == null) badJobs.add(unassignedJob);
else insertJob(unassignedJob, bestInsertion.getInsertionData(), bestInsertion.getRoute());
}
return badJobs;
@ -155,13 +148,13 @@ public final class BestInsertionConcurrent extends AbstractInsertionStrategy{
private Insertion getBestInsertion(Batch batch, Job unassignedJob) {
Insertion bestInsertion = null;
double bestInsertionCost = Double.MAX_VALUE;
for(VehicleRoute vehicleRoute : batch.routes){
for (VehicleRoute vehicleRoute : batch.routes) {
InsertionData iData = bestInsertionCostCalculator.getInsertionData(vehicleRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost);
if(iData instanceof NoInsertionFound) {
if (iData instanceof NoInsertionFound) {
continue;
}
if(iData.getInsertionCost() < bestInsertionCost){
bestInsertion = new Insertion(vehicleRoute,iData);
if (iData.getInsertionCost() < bestInsertionCost) {
bestInsertion = new Insertion(vehicleRoute, iData);
bestInsertionCost = iData.getInsertionCost();
}
}
@ -170,26 +163,25 @@ public final class BestInsertionConcurrent extends AbstractInsertionStrategy{
private List<Batch> distributeRoutes(Collection<VehicleRoute> vehicleRoutes, int nuOfBatches) {
List<Batch> batches = new ArrayList<Batch>();
for(int i=0;i<nuOfBatches;i++) batches.add(new Batch());
for (int i = 0; i < nuOfBatches; i++) batches.add(new Batch());
/*
* if route.size < nuOfBatches add as much routes as empty batches are available
* else add one empty route anyway
*/
if(vehicleRoutes.size()<nuOfBatches){
int nOfNewRoutes = nuOfBatches-vehicleRoutes.size();
for(int i=0;i<nOfNewRoutes;i++){
if (vehicleRoutes.size() < nuOfBatches) {
int nOfNewRoutes = nuOfBatches - vehicleRoutes.size();
for (int i = 0; i < nOfNewRoutes; i++) {
vehicleRoutes.add(VehicleRoute.emptyRoute());
}
}
else{
} else {
vehicleRoutes.add(VehicleRoute.emptyRoute());
}
/*
* distribute routes to batches equally
*/
int count = 0;
for(VehicleRoute route : vehicleRoutes){
if(count == nuOfBatches) count=0;
for (VehicleRoute route : vehicleRoutes) {
if (count == nuOfBatches) count = 0;
batches.get(count).routes.add(route);
count++;
}

View file

@ -0,0 +1,179 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.JobActivityFactory;
import jsprit.core.problem.Location;
import jsprit.core.problem.constraint.*;
import jsprit.core.problem.constraint.HardActivityConstraint.ConstraintsStatus;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Break;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.BreakActivity;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.Start;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.util.CalculationUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Calculator that calculates the best insertion position for a {@link jsprit.core.problem.job.Service}.
*
* @author schroeder
*/
final class BreakInsertionCalculator implements JobInsertionCostsCalculator {
private static final Logger logger = LogManager.getLogger(BreakInsertionCalculator.class);
private HardRouteConstraint hardRouteLevelConstraint;
private HardActivityConstraint hardActivityLevelConstraint;
private SoftRouteConstraint softRouteConstraint;
private SoftActivityConstraint softActivityConstraint;
private VehicleRoutingTransportCosts transportCosts;
private ActivityInsertionCostsCalculator additionalTransportCostsCalculator;
private JobActivityFactory activityFactory;
private AdditionalAccessEgressCalculator additionalAccessEgressCalculator;
public BreakInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ActivityInsertionCostsCalculator additionalTransportCostsCalculator, ConstraintManager constraintManager) {
super();
this.transportCosts = routingCosts;
hardRouteLevelConstraint = constraintManager;
hardActivityLevelConstraint = constraintManager;
softActivityConstraint = constraintManager;
softRouteConstraint = constraintManager;
this.additionalTransportCostsCalculator = additionalTransportCostsCalculator;
additionalAccessEgressCalculator = new AdditionalAccessEgressCalculator(routingCosts);
logger.debug("initialise " + this);
}
public void setJobActivityFactory(JobActivityFactory jobActivityFactory) {
this.activityFactory = jobActivityFactory;
}
@Override
public String toString() {
return "[name=calculatesServiceInsertion]";
}
/**
* Calculates the marginal cost of inserting job i locally. This is based on the
* assumption that cost changes can entirely covered by only looking at the predecessor i-1 and its successor i+1.
*/
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownCosts) {
Break breakToInsert = (Break) jobToInsert;
if (newVehicle.getBreak() == null || newVehicle.getBreak() != breakToInsert) {
return InsertionData.createEmptyInsertionData();
}
if (currentRoute.isEmpty()) return InsertionData.createEmptyInsertionData();
JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
int insertionIndex = InsertionData.NO_INDEX;
BreakActivity breakAct2Insert = (BreakActivity) activityFactory.createActivities(breakToInsert).get(0);
insertionContext.getAssociatedActivities().add(breakAct2Insert);
/*
check hard constraints at route level
*/
if (!hardRouteLevelConstraint.fulfilled(insertionContext)) {
return InsertionData.createEmptyInsertionData();
}
/*
check soft constraints at route level
*/
double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);
double bestCost = bestKnownCosts;
additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);
/*
generate new start and end for new vehicle
*/
Start start = new Start(newVehicle.getStartLocation(), newVehicle.getEarliestDeparture(), Double.MAX_VALUE);
start.setEndTime(newVehicleDepartureTime);
End end = new End(newVehicle.getEndLocation(), 0.0, newVehicle.getLatestArrival());
Location bestLocation = null;
TourActivity prevAct = start;
double prevActStartTime = newVehicleDepartureTime;
int actIndex = 0;
Iterator<TourActivity> activityIterator = currentRoute.getActivities().iterator();
boolean tourEnd = false;
while (!tourEnd) {
TourActivity nextAct;
if (activityIterator.hasNext()) nextAct = activityIterator.next();
else {
nextAct = end;
tourEnd = true;
}
boolean breakThis = true;
List<Location> locations = Arrays.asList(prevAct.getLocation(), nextAct.getLocation());
for (Location location : locations) {
breakAct2Insert.setLocation(location);
ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, breakAct2Insert, nextAct, prevActStartTime);
if (status.equals(ConstraintsStatus.FULFILLED)) {
//from job2insert induced costs at activity level
double additionalICostsAtActLevel = softActivityConstraint.getCosts(insertionContext, prevAct, breakAct2Insert, nextAct, prevActStartTime);
double additionalTransportationCosts = additionalTransportCostsCalculator.getCosts(insertionContext, prevAct, nextAct, breakAct2Insert, prevActStartTime);
if (additionalICostsAtRouteLevel + additionalICostsAtActLevel + additionalTransportationCosts < bestCost) {
bestCost = additionalICostsAtRouteLevel + additionalICostsAtActLevel + additionalTransportationCosts;
insertionIndex = actIndex;
bestLocation = location;
}
breakThis = false;
} else if (status.equals(ConstraintsStatus.NOT_FULFILLED)) {
breakThis = false;
}
}
double nextActArrTime = prevActStartTime + transportCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevActStartTime, newDriver, newVehicle);
prevActStartTime = CalculationUtils.getActivityEndTime(nextActArrTime, nextAct);
prevAct = nextAct;
actIndex++;
if (breakThis) break;
}
if (insertionIndex == InsertionData.NO_INDEX) {
return InsertionData.createEmptyInsertionData();
}
InsertionData insertionData = new InsertionData(bestCost, InsertionData.NO_INDEX, insertionIndex, newVehicle, newDriver);
breakAct2Insert.setLocation(bestLocation);
insertionData.getEvents().add(new InsertBreak(currentRoute, newVehicle, breakAct2Insert, insertionIndex));
insertionData.getEvents().add(new SwitchVehicle(currentRoute, newVehicle, newVehicleDepartureTime));
insertionData.setVehicleDepartureTime(newVehicleDepartureTime);
return insertionData;
}
}

View file

@ -15,22 +15,24 @@ import java.util.List;
import java.util.Random;
class CalculatesServiceInsertionWithTimeScheduling implements JobInsertionCostsCalculator{
class CalculatesServiceInsertionWithTimeScheduling implements JobInsertionCostsCalculator {
public static class KnowledgeInjection implements InsertionStartsListener {
private CalculatesServiceInsertionWithTimeScheduling c;
public KnowledgeInjection(CalculatesServiceInsertionWithTimeScheduling c) {
super();
this.c = c;
}
@Override
public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes,Collection<Job> unassignedJobs) {
public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) {
List<Double> knowledge = new ArrayList<Double>();
if(vehicleRoutes.isEmpty()){
if (vehicleRoutes.isEmpty()) {
// System.out.println("hmm");
}
for(VehicleRoute route : vehicleRoutes){
for (VehicleRoute route : vehicleRoutes) {
// if(route.getDepartureTime() == 21600.){
// System.out.println("hu");
// }
@ -60,18 +62,17 @@ class CalculatesServiceInsertionWithTimeScheduling implements JobInsertionCostsC
@Override
public String toString() {
return "[name="+this.getClass().toString()+"]";
return "[name=" + this.getClass().toString() + "]";
}
@Override
public InsertionData getInsertionData(VehicleRoute currentRoute, Job jobToInsert, Vehicle newVehicle, double newVehicleDepartureTime, Driver newDriver, double bestKnownScore) {
double departureTime = newVehicleDepartureTime;
if(currentRoute.isEmpty()){
if(!departureTimeKnowledge.isEmpty()){
if (currentRoute.isEmpty()) {
if (!departureTimeKnowledge.isEmpty()) {
departureTime = departureTimeKnowledge.get(random.nextInt(departureTimeKnowledge.size()));
}
}
else if(!currentRoute.getVehicle().getId().equals(newVehicle.getId())){
} else if (!currentRoute.getVehicle().getId().equals(newVehicle.getId())) {
departureTime = currentRoute.getDepartureTime();
}
@ -82,7 +83,7 @@ class CalculatesServiceInsertionWithTimeScheduling implements JobInsertionCostsC
return insertionData;
}
public void setDepartureTimeKnowledge(List<Double> departureTimes){
departureTimeKnowledge=departureTimes;
public void setDepartureTimeKnowledge(List<Double> departureTimes) {
departureTimeKnowledge = departureTimes;
}
}

View file

@ -27,7 +27,7 @@ import java.util.ArrayList;
import java.util.List;
class CalculatesServiceInsertionWithTimeSchedulingInSlices implements JobInsertionCostsCalculator{
class CalculatesServiceInsertionWithTimeSchedulingInSlices implements JobInsertionCostsCalculator {
private static Logger log = LogManager.getLogger(CalculatesServiceInsertionWithTimeSchedulingInSlices.class);
@ -48,38 +48,37 @@ class CalculatesServiceInsertionWithTimeSchedulingInSlices implements JobInserti
@Override
public String toString() {
return "[name="+this.getClass().toString()+"][timeSlice="+timeSlice+"][#timeSlice="+nOfDepartureTimes+"]";
return "[name=" + this.getClass().toString() + "][timeSlice=" + timeSlice + "][#timeSlice=" + nOfDepartureTimes + "]";
}
@Override
public InsertionData getInsertionData(VehicleRoute currentRoute, Job jobToInsert, Vehicle newVehicle, double newVehicleDepartureTime, Driver newDriver, double bestKnownScore) {
List<Double> vehicleDepartureTimes = new ArrayList<Double>();
double currentStart;
if(currentRoute.getStart() == null){
if (currentRoute.getStart() == null) {
currentStart = newVehicleDepartureTime;
}
else currentStart = currentRoute.getStart().getEndTime();
} else currentStart = currentRoute.getStart().getEndTime();
vehicleDepartureTimes.add(currentStart);
// double earliestDeparture = newVehicle.getEarliestDeparture();
// double latestEnd = newVehicle.getLatestArrival();
for(int i=0;i<nOfDepartureTimes;i++){
double neighborStartTime_earlier = currentStart - (i+1)*timeSlice;
for (int i = 0; i < nOfDepartureTimes; i++) {
double neighborStartTime_earlier = currentStart - (i + 1) * timeSlice;
// if(neighborStartTime_earlier > earliestDeparture) {
vehicleDepartureTimes.add(neighborStartTime_earlier);
// }
double neighborStartTime_later = currentStart + (i+1)*timeSlice;
double neighborStartTime_later = currentStart + (i + 1) * timeSlice;
// if(neighborStartTime_later < latestEnd) {
vehicleDepartureTimes.add(neighborStartTime_later);
// }
}
InsertionData bestIData = null;
for(Double departureTime : vehicleDepartureTimes){
for (Double departureTime : vehicleDepartureTimes) {
InsertionData iData = jic.getInsertionData(currentRoute, jobToInsert, newVehicle, departureTime, newDriver, bestKnownScore);
if(bestIData == null) bestIData = iData;
else if(iData.getInsertionCost() < bestIData.getInsertionCost()){
if (bestIData == null) bestIData = iData;
else if (iData.getInsertionCost() < bestIData.getInsertionCost()) {
iData.setVehicleDepartureTime(departureTime);
bestIData = iData;
}

View file

@ -26,7 +26,7 @@ import jsprit.core.problem.solution.route.VehicleRoute;
import java.util.Collection;
final class ConfigureFixCostCalculator implements InsertionStartsListener, JobInsertedListener{
final class ConfigureFixCostCalculator implements InsertionStartsListener, JobInsertedListener {
VehicleRoutingProblem vrp;
@ -48,7 +48,7 @@ final class ConfigureFixCostCalculator implements InsertionStartsListener, JobIn
@Override
public void informInsertionStarts(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
this.nuOfJobsToRecreate = unassignedJobs.size();
double completenessRatio = (1-((double)nuOfJobsToRecreate/(double)vrp.getJobs().values().size()));
double completenessRatio = (1 - ((double) nuOfJobsToRecreate / (double) vrp.getJobs().values().size()));
calcConsideringFix.setSolutionCompletenessRatio(completenessRatio);
// log.debug("initialise completenessRatio to " + completenessRatio);
}
@ -56,7 +56,7 @@ final class ConfigureFixCostCalculator implements InsertionStartsListener, JobIn
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
nuOfJobsToRecreate--;
double completenessRatio = (1-((double)nuOfJobsToRecreate/(double)vrp.getJobs().values().size()));
double completenessRatio = (1 - ((double) nuOfJobsToRecreate / (double) vrp.getJobs().values().size()));
calcConsideringFix.setSolutionCompletenessRatio(completenessRatio);
// log.debug("set completenessRatio to " + completenessRatio);
}

View file

@ -0,0 +1,40 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.listener.InsertionStartsListener;
import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import java.util.Collection;
/**
* Created by schroeder on 22/07/15.
*/
public class ConfigureLocalActivityInsertionCalculator implements InsertionStartsListener, JobInsertedListener {
private VehicleRoutingProblem vrp;
private LocalActivityInsertionCostsCalculator localActivityInsertionCostsCalculator;
private int nuOfJobsToRecreate;
public ConfigureLocalActivityInsertionCalculator(VehicleRoutingProblem vrp, LocalActivityInsertionCostsCalculator localActivityInsertionCostsCalculator) {
this.vrp = vrp;
this.localActivityInsertionCostsCalculator = localActivityInsertionCostsCalculator;
}
@Override
public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) {
this.nuOfJobsToRecreate = unassignedJobs.size();
double completenessRatio = (1 - ((double) nuOfJobsToRecreate / (double) vrp.getJobs().values().size()));
localActivityInsertionCostsCalculator.setSolutionCompletenessRatio(Math.max(0.5, completenessRatio));
}
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
nuOfJobsToRecreate--;
double completenessRatio = (1 - ((double) nuOfJobsToRecreate / (double) vrp.getJobs().values().size()));
localActivityInsertionCostsCalculator.setSolutionCompletenessRatio(Math.max(0.5, completenessRatio));
}
}

View file

@ -0,0 +1,83 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
/**
* Created by schroeder on 15/10/15.
*/
public class DefaultScorer implements ScoringFunction {
private VehicleRoutingProblem vrp;
private double tw_param = -0.5;
private double depotDistance_param = +0.1;
private double minTimeWindowScore = -100000;
public DefaultScorer(VehicleRoutingProblem vrp) {
this.vrp = vrp;
}
public void setTimeWindowParam(double tw_param) {
this.tw_param = tw_param;
}
public void setDepotDistanceParam(double depotDistance_param) {
this.depotDistance_param = depotDistance_param;
}
@Override
public double score(InsertionData best, Job job) {
double score;
if (job instanceof Service) {
score = scoreService(best, job);
} else if (job instanceof Shipment) {
score = scoreShipment(best, job);
} else throw new IllegalStateException("not supported");
return score;
}
private double scoreShipment(InsertionData best, Job job) {
Shipment shipment = (Shipment) job;
double maxDepotDistance_1 = Math.max(
getDistance(best.getSelectedVehicle().getStartLocation(), shipment.getPickupLocation()),
getDistance(best.getSelectedVehicle().getStartLocation(), shipment.getDeliveryLocation())
);
double maxDepotDistance_2 = Math.max(
getDistance(best.getSelectedVehicle().getEndLocation(), shipment.getPickupLocation()),
getDistance(best.getSelectedVehicle().getEndLocation(), shipment.getDeliveryLocation())
);
double maxDepotDistance = Math.max(maxDepotDistance_1, maxDepotDistance_2);
double minTimeToOperate = Math.min(shipment.getPickupTimeWindow().getEnd() - shipment.getPickupTimeWindow().getStart(),
shipment.getDeliveryTimeWindow().getEnd() - shipment.getDeliveryTimeWindow().getStart());
return Math.max(tw_param * minTimeToOperate, minTimeWindowScore) + depotDistance_param * maxDepotDistance;
}
private double scoreService(InsertionData best, Job job) {
Location location = ((Service) job).getLocation();
double maxDepotDistance = 0;
if (location != null) {
maxDepotDistance = Math.max(
getDistance(best.getSelectedVehicle().getStartLocation(), location),
getDistance(best.getSelectedVehicle().getEndLocation(), location)
);
}
return Math.max(tw_param * (((Service) job).getTimeWindow().getEnd() - ((Service) job).getTimeWindow().getStart()), minTimeWindowScore) +
depotDistance_param * maxDepotDistance;
}
private double getDistance(Location loc1, Location loc2) {
return vrp.getTransportCosts().getTransportCost(loc1, loc2, 0., null, null);
}
@Override
public String toString() {
return "[name=defaultScorer][twParam=" + tw_param + "][depotDistanceParam=" + depotDistance_param + "]";
}
}

View file

@ -28,7 +28,7 @@ import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import java.util.Collection;
public class DellAmicoFixCostCalculator implements SoftRouteConstraint, InsertionStartsListener, JobInsertedListener{
public class DellAmicoFixCostCalculator implements SoftRouteConstraint, InsertionStartsListener, JobInsertedListener {
private int nuOfJobsToRecreate;
@ -38,7 +38,7 @@ public class DellAmicoFixCostCalculator implements SoftRouteConstraint, Insertio
public DellAmicoFixCostCalculator(final int nuOfJobs, final RouteAndActivityStateGetter stateGetter) {
super();
this.nuOfJobs=nuOfJobs;
this.nuOfJobs = nuOfJobs;
calculator = new JobInsertionConsideringFixCostsCalculator(null, stateGetter);
}
@ -50,14 +50,14 @@ public class DellAmicoFixCostCalculator implements SoftRouteConstraint, Insertio
@Override
public void informInsertionStarts(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
this.nuOfJobsToRecreate = unassignedJobs.size();
double completenessRatio = (1-((double)nuOfJobsToRecreate/(double)nuOfJobs));
double completenessRatio = (1 - ((double) nuOfJobsToRecreate / (double) nuOfJobs));
calculator.setSolutionCompletenessRatio(completenessRatio);
}
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
nuOfJobsToRecreate--;
double completenessRatio = (1-((double)nuOfJobsToRecreate/(double)nuOfJobs));
double completenessRatio = (1 - ((double) nuOfJobsToRecreate / (double) nuOfJobs));
calculator.setSolutionCompletenessRatio(completenessRatio);
System.out.println(completenessRatio);
}

View file

@ -13,10 +13,11 @@ class EventListeners {
public EventListeners() {
listeners.add(new InsertActivityListener());
listeners.add(new SwitchVehicleListener());
listeners.add(new InsertBreakListener());
}
public void inform(Event event){
for(EventListener l : listeners){
public void inform(Event event) {
for (EventListener l : listeners) {
l.inform(event);
}
}

View file

@ -7,14 +7,14 @@ class InsertActivityListener implements EventListener {
@Override
public void inform(Event event) {
if(event instanceof InsertActivity){
if (event instanceof InsertActivity) {
InsertActivity insertActivity = (InsertActivity) event;
if(!insertActivity.getNewVehicle().isReturnToDepot()){
if(insertActivity.getIndex()>=insertActivity.getVehicleRoute().getActivities().size()){
if (!insertActivity.getNewVehicle().isReturnToDepot()) {
if (insertActivity.getIndex() >= insertActivity.getVehicleRoute().getActivities().size()) {
insertActivity.getVehicleRoute().getEnd().setLocation(insertActivity.getActivity().getLocation());
}
}
insertActivity.getVehicleRoute().getTourActivities().addActivity(insertActivity.getIndex(),((InsertActivity) event).getActivity());
insertActivity.getVehicleRoute().getTourActivities().addActivity(insertActivity.getIndex(), ((InsertActivity) event).getActivity());
}
}

View file

@ -0,0 +1,42 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
/**
* Created by schroeder on 19/05/15.
*/
class InsertBreak implements Event {
private VehicleRoute vehicleRoute;
private Vehicle newVehicle;
private TourActivity activity;
private int index;
public InsertBreak(VehicleRoute vehicleRoute, Vehicle newVehicle, TourActivity activity, int index) {
this.vehicleRoute = vehicleRoute;
this.newVehicle = newVehicle;
this.activity = activity;
this.index = index;
}
public Vehicle getNewVehicle() {
return newVehicle;
}
public VehicleRoute getVehicleRoute() {
return vehicleRoute;
}
public TourActivity getActivity() {
return activity;
}
public int getIndex() {
return index;
}
}

View file

@ -0,0 +1,37 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.solution.route.VehicleRoute;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Created by schroeder on 19/05/15.
*/
class InsertBreakListener implements EventListener {
private static final Logger logger = LogManager.getLogger();
@Override
public void inform(Event event) {
if (event instanceof InsertBreak) {
InsertBreak insertActivity = (InsertBreak) event;
if (!insertActivity.getNewVehicle().isReturnToDepot()) {
if (insertActivity.getIndex() >= insertActivity.getVehicleRoute().getActivities().size()) {
insertActivity.getVehicleRoute().getEnd().setLocation(insertActivity.getActivity().getLocation());
}
}
VehicleRoute vehicleRoute = ((InsertBreak) event).getVehicleRoute();
if (!vehicleRoute.isEmpty()) {
if (vehicleRoute.getVehicle() != ((InsertBreak) event).getNewVehicle()) {
if (vehicleRoute.getVehicle().getBreak() != null) {
boolean removed = vehicleRoute.getTourActivities().removeJob(vehicleRoute.getVehicle().getBreak());
if (removed)
logger.trace("remove old break " + vehicleRoute.getVehicle().getBreak());
}
}
}
insertActivity.getVehicleRoute().getTourActivities().addActivity(insertActivity.getIndex(), ((InsertBreak) event).getActivity());
}
}
}

View file

@ -38,10 +38,10 @@ class Inserter {
}
class JobExceptionHandler implements JobInsertionHandler{
class JobExceptionHandler implements JobInsertionHandler {
@Override
public void handleJobInsertion(Job job, InsertionData iData,VehicleRoute route) {
public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
throw new IllegalStateException("job insertion is not supported. Do not know job type.");
}
@ -52,7 +52,7 @@ class Inserter {
}
class ServiceInsertionHandler implements JobInsertionHandler{
class ServiceInsertionHandler implements JobInsertionHandler {
private TourActivityFactory activityFactory = new DefaultTourActivityFactory();
@ -66,24 +66,23 @@ 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);
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);
}
}
TourActivity activity = vehicleRoutingProblem.copyAndGetActivities(job).get(0);
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), activity);
}
else delegator.handleJobInsertion(job, iData, route);
} else delegator.handleJobInsertion(job, iData, route);
}
private void setEndLocation(VehicleRoute route, Service service) {
route.getEnd().setLocation(service.getLocation());
}
public void setNextHandler(JobInsertionHandler jobInsertionHandler){
public void setNextHandler(JobInsertionHandler jobInsertionHandler) {
this.delegator = jobInsertionHandler;
}
@ -103,27 +102,26 @@ class Inserter {
@Override
public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
if(job instanceof Shipment){
if (job instanceof Shipment) {
List<AbstractActivity> acts = vehicleRoutingProblem.copyAndGetActivities(job);
TourActivity pickupShipment = acts.get(0);
TourActivity deliverShipment = acts.get(1);
route.setVehicleAndDepartureTime(iData.getSelectedVehicle(),iData.getVehicleDepartureTime());
if(!iData.getSelectedVehicle().isReturnToDepot()){
if(iData.getDeliveryInsertionIndex()>=route.getActivities().size()){
setEndLocation(route,(Shipment)job);
route.setVehicleAndDepartureTime(iData.getSelectedVehicle(), iData.getVehicleDepartureTime());
if (!iData.getSelectedVehicle().isReturnToDepot()) {
if (iData.getDeliveryInsertionIndex() >= route.getActivities().size()) {
setEndLocation(route, (Shipment) job);
}
}
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), deliverShipment);
route.getTourActivities().addActivity(iData.getPickupInsertionIndex(), pickupShipment);
}
else delegator.handleJobInsertion(job, iData, route);
} else delegator.handleJobInsertion(job, iData, route);
}
private void setEndLocation(VehicleRoute route, Shipment shipment) {
route.getEnd().setLocation(shipment.getDeliveryLocation());
}
public void setNextHandler(JobInsertionHandler jobInsertionHandler){
public void setNextHandler(JobInsertionHandler jobInsertionHandler) {
this.delegator = jobInsertionHandler;
}
@ -142,12 +140,13 @@ class Inserter {
jobInsertionHandler.setNextHandler(new ShipmentInsertionHandler(vehicleRoutingProblem));
}
public void insertJob(Job job, InsertionData insertionData, VehicleRoute vehicleRoute){
public void insertJob(Job job, InsertionData insertionData, VehicleRoute vehicleRoute) {
insertionListeners.informBeforeJobInsertion(job, insertionData, vehicleRoute);
if(insertionData == null || (insertionData instanceof NoInsertionFound)) throw new IllegalStateException("insertionData null. cannot insert job.");
if(job == null) throw new IllegalStateException("cannot insert null-job");
if(!(vehicleRoute.getVehicle().getId().equals(insertionData.getSelectedVehicle().getId()))){
if (insertionData == null || (insertionData instanceof NoInsertionFound))
throw new IllegalStateException("insertionData null. cannot insert job.");
if (job == null) throw new IllegalStateException("cannot insert null-job");
if (!(vehicleRoute.getVehicle().getId().equals(insertionData.getSelectedVehicle().getId()))) {
insertionListeners.informVehicleSwitched(vehicleRoute, vehicleRoute.getVehicle(), insertionData.getSelectedVehicle());
vehicleRoute.setVehicleAndDepartureTime(insertionData.getSelectedVehicle(), insertionData.getVehicleDepartureTime());
}

View file

@ -30,6 +30,9 @@ import java.util.concurrent.ExecutorService;
public class InsertionBuilder {
private boolean fastRegret;
public enum Strategy {
REGRET, BEST
}
@ -62,14 +65,16 @@ public class InsertionBuilder {
private int nNeighbors;
private boolean timeScheduling=false;
private boolean timeScheduling = false;
private boolean allowVehicleSwitch=true;
private boolean allowVehicleSwitch = true;
private boolean addDefaultCostCalc=true;
private boolean addDefaultCostCalc = true;
private Strategy strategy = Strategy.BEST;
private boolean isFastRegret = false;
public InsertionBuilder(VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, StateManager stateManager, ConstraintManager constraintManager) {
super();
this.vrp = vrp;
@ -78,19 +83,19 @@ public class InsertionBuilder {
this.fleetManager = vehicleFleetManager;
}
public InsertionBuilder setInsertionStrategy(Strategy strategy){
public InsertionBuilder setInsertionStrategy(Strategy strategy) {
this.strategy = strategy;
return this;
}
public InsertionBuilder setRouteLevel(int forwardLooking, int memory){
public InsertionBuilder setRouteLevel(int forwardLooking, int memory) {
local = false;
this.forwaredLooking = forwardLooking;
this.memory = memory;
return this;
}
public InsertionBuilder setRouteLevel(int forwardLooking, int memory, boolean addDefaultMarginalCostCalculation){
public InsertionBuilder setRouteLevel(int forwardLooking, int memory, boolean addDefaultMarginalCostCalculation) {
local = false;
this.forwaredLooking = forwardLooking;
this.memory = memory;
@ -98,7 +103,13 @@ public class InsertionBuilder {
return this;
}
public InsertionBuilder setLocalLevel(){
public InsertionBuilder setFastRegret(boolean fastRegret) {
this.isFastRegret = fastRegret;
return this;
}
public InsertionBuilder setLocalLevel() {
local = true;
return this;
}
@ -110,24 +121,24 @@ public class InsertionBuilder {
* @param addDefaultMarginalCostCalculation
* @return
*/
public InsertionBuilder setLocalLevel(boolean addDefaultMarginalCostCalculation){
public InsertionBuilder setLocalLevel(boolean addDefaultMarginalCostCalculation) {
local = true;
addDefaultCostCalc = addDefaultMarginalCostCalculation;
return this;
}
public InsertionBuilder considerFixedCosts(double weightOfFixedCosts){
public InsertionBuilder considerFixedCosts(double weightOfFixedCosts) {
this.weightOfFixedCosts = weightOfFixedCosts;
this.considerFixedCosts = true;
return this;
}
public InsertionBuilder setActivityInsertionCostCalculator(ActivityInsertionCostsCalculator activityInsertionCostsCalculator){
public InsertionBuilder setActivityInsertionCostCalculator(ActivityInsertionCostsCalculator activityInsertionCostsCalculator) {
this.actInsertionCostsCalculator = activityInsertionCostsCalculator;
return this;
}
public InsertionBuilder setConcurrentMode(ExecutorService executor, int nuOfThreads){
public InsertionBuilder setConcurrentMode(ExecutorService executor, int nuOfThreads) {
this.executor = executor;
this.nuOfThreads = nuOfThreads;
return this;
@ -138,10 +149,9 @@ public class InsertionBuilder {
List<InsertionListener> iListeners = new ArrayList<InsertionListener>();
List<PrioritizedVRAListener> algorithmListeners = new ArrayList<PrioritizedVRAListener>();
JobInsertionCostsCalculatorBuilder calcBuilder = new JobInsertionCostsCalculatorBuilder(iListeners, algorithmListeners);
if(local){
if (local) {
calcBuilder.setLocalLevel(addDefaultCostCalc);
}
else {
} else {
calcBuilder.setRouteLevel(forwaredLooking, memory, addDefaultCostCalc);
}
calcBuilder.setConstraintManager(constraintManager);
@ -149,46 +159,61 @@ public class InsertionBuilder {
calcBuilder.setVehicleRoutingProblem(vrp);
calcBuilder.setVehicleFleetManager(fleetManager);
calcBuilder.setActivityInsertionCostsCalculator(actInsertionCostsCalculator);
if(considerFixedCosts) {
if (considerFixedCosts) {
calcBuilder.considerFixedCosts(weightOfFixedCosts);
}
if(timeScheduling){
if (timeScheduling) {
calcBuilder.experimentalTimeScheduler(timeSlice, nNeighbors);
}
calcBuilder.setAllowVehicleSwitch(allowVehicleSwitch);
JobInsertionCostsCalculator costCalculator = calcBuilder.build();
InsertionStrategy insertion;
if(strategy.equals(Strategy.BEST)) {
if (strategy.equals(Strategy.BEST)) {
if (executor == null) {
insertion = new BestInsertion(costCalculator, vrp);
} else {
insertion = new BestInsertionConcurrent(costCalculator, executor, nuOfThreads, vrp);
}
}
else if(strategy.equals(Strategy.REGRET)){
} else if (strategy.equals(Strategy.REGRET)) {
if (executor == null) {
insertion = new RegretInsertion(costCalculator, vrp);
if(isFastRegret){
RegretInsertionFast regret = new RegretInsertionFast(costCalculator, vrp, fleetManager);
regret.setSwitchAllowed(allowVehicleSwitch);
insertion = regret;
}
else {
insertion = new RegretInsertionConcurrent(costCalculator,vrp,executor);
RegretInsertion regret = new RegretInsertion(costCalculator, vrp);
insertion = regret;
}
} else {
if(isFastRegret){
RegretInsertionConcurrentFast regret = new RegretInsertionConcurrentFast(costCalculator, vrp, executor, fleetManager);
regret.setSwitchAllowed(allowVehicleSwitch);
insertion = regret;
}
else throw new IllegalStateException("you should never get here");
for(InsertionListener l : iListeners) insertion.addListener(l);
else{
RegretInsertionConcurrent regret = new RegretInsertionConcurrent(costCalculator, vrp, executor);
insertion = regret;
}
}
} else throw new IllegalStateException("you should never get here");
for (InsertionListener l : iListeners) insertion.addListener(l);
return insertion;
}
/**
* @deprecated this is experimental and can disappear.
* @param timeSlice the time slice
* @param nNeighbors number of neighbors
* @deprecated this is experimental and can disappear.
*/
@Deprecated
public void experimentalTimeScheduler(double timeSlice, int nNeighbors) {
this.timeSlice=timeSlice;
this.nNeighbors=nNeighbors;
timeScheduling=true;
this.timeSlice = timeSlice;
this.nNeighbors = nNeighbors;
timeScheduling = true;
}
public InsertionBuilder setAllowVehicleSwitch(boolean allowVehicleSwitch) {
@ -197,6 +222,4 @@ public class InsertionBuilder {
}
}

View file

@ -27,11 +27,10 @@ import java.util.List;
* and departureTime of vehicle at vehicle's start location (e.g. depot).
*
* @author stefan
*
*/
public class InsertionData {
public static class NoInsertionFound extends InsertionData{
public static class NoInsertionFound extends InsertionData {
public NoInsertionFound() {
super(Double.MAX_VALUE, NO_INDEX, NO_INDEX, null, null);
@ -49,7 +48,7 @@ public class InsertionData {
*
* @return
*/
public static InsertionData createEmptyInsertionData(){
public static InsertionData createEmptyInsertionData() {
return noInsertion;
}
@ -71,7 +70,7 @@ public class InsertionData {
private List<Event> events = new ArrayList<Event>();
List<Event> getEvents(){
List<Event> getEvents() {
return events;
}
@ -89,7 +88,7 @@ public class InsertionData {
this.additionalTime = additionalTime;
}
public InsertionData(double insertionCost, int pickupInsertionIndex, int deliveryInsertionIndex, Vehicle vehicle, Driver driver){
public InsertionData(double insertionCost, int pickupInsertionIndex, int deliveryInsertionIndex, Vehicle vehicle, Driver driver) {
this.insertionCost = insertionCost;
this.pickupInsertionIndex = pickupInsertionIndex;
this.deliveryInsertionIndex = deliveryInsertionIndex;
@ -99,7 +98,7 @@ public class InsertionData {
@Override
public String toString() {
return "[iCost="+insertionCost+"][pickupIndex="+pickupInsertionIndex+"][deliveryIndex="+deliveryInsertionIndex+"][depTime="+departureTime+"][vehicle="+selectedVehicle+"][driver="+selectedDriver+"]";
return "[iCost=" + insertionCost + "][pickupIndex=" + pickupInsertionIndex + "][deliveryIndex=" + deliveryInsertionIndex + "][depTime=" + departureTime + "][vehicle=" + selectedVehicle + "][driver=" + selectedDriver + "]";
}
/**
@ -107,7 +106,7 @@ public class InsertionData {
*
* @return
*/
public int getDeliveryInsertionIndex(){
public int getDeliveryInsertionIndex() {
return deliveryInsertionIndex;
}
@ -116,7 +115,7 @@ public class InsertionData {
*
* @return
*/
public int getPickupInsertionIndex(){
public int getPickupInsertionIndex() {
return pickupInsertionIndex;
}
@ -143,7 +142,7 @@ public class InsertionData {
*
* @return
*/
public Driver getSelectedDriver(){
public Driver getSelectedDriver() {
return selectedDriver;
}
@ -162,5 +161,4 @@ public class InsertionData {
}
}

View file

@ -0,0 +1,157 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleFleetManager;
import jsprit.core.problem.vehicle.VehicleImpl;
import java.util.*;
/**
* Created by schroeder on 15/10/15.
*/
class InsertionDataUpdater {
static boolean update(boolean addAllAvailable, Set<String> initialVehicleIds, VehicleFleetManager fleetManager, JobInsertionCostsCalculator insertionCostsCalculator, TreeSet<VersionedInsertionData> insertionDataSet, int updateRound, Job unassignedJob, Collection<VehicleRoute> routes) {
for(VehicleRoute route : routes) {
Collection<Vehicle> relevantVehicles = new ArrayList<Vehicle>();
if (!(route.getVehicle() instanceof VehicleImpl.NoVehicle)) {
relevantVehicles.add(route.getVehicle());
if(addAllAvailable && !initialVehicleIds.contains(route.getVehicle().getId())){
relevantVehicles.addAll(fleetManager.getAvailableVehicles(route.getVehicle()));
}
} else relevantVehicles.addAll(fleetManager.getAvailableVehicles());
for (Vehicle v : relevantVehicles) {
double depTime = v.getEarliestDeparture();
InsertionData iData = insertionCostsCalculator.getInsertionData(route, unassignedJob, v, depTime, route.getDriver(), Double.MAX_VALUE);
if (iData instanceof InsertionData.NoInsertionFound) {
continue;
}
insertionDataSet.add(new VersionedInsertionData(iData, updateRound, route));
}
}
return true;
}
static VehicleRoute findRoute(Collection<VehicleRoute> routes, Job job) {
for(VehicleRoute r : routes){
if(r.getVehicle().getBreak() == job) return r;
}
return null;
}
static Comparator<VersionedInsertionData> getComparator(){
return new Comparator<VersionedInsertionData>() {
@Override
public int compare(VersionedInsertionData o1, VersionedInsertionData o2) {
if(o1.getiData().getInsertionCost() < o2.getiData().getInsertionCost()) return -1;
return 1;
}
};
}
static ScoredJob getBest(boolean switchAllowed, Set<String> initialVehicleIds, VehicleFleetManager fleetManager, JobInsertionCostsCalculator insertionCostsCalculator, ScoringFunction scoringFunction, TreeSet<VersionedInsertionData>[] priorityQueues, Map<VehicleRoute, Integer> updates, List<Job> unassignedJobList, List<Job> badJobs) {
ScoredJob bestScoredJob = null;
for(Job j : unassignedJobList){
VehicleRoute bestRoute = null;
InsertionData best = null;
InsertionData secondBest = null;
TreeSet<VersionedInsertionData> priorityQueue = priorityQueues[j.getIndex()];
Iterator<VersionedInsertionData> iterator = priorityQueue.iterator();
while(iterator.hasNext()){
VersionedInsertionData versionedIData = iterator.next();
if(bestRoute != null){
if(versionedIData.getRoute() == bestRoute){
continue;
}
}
if(versionedIData.getiData() instanceof InsertionData.NoInsertionFound) continue;
if(!(versionedIData.getRoute().getVehicle() instanceof VehicleImpl.NoVehicle)) {
if (versionedIData.getiData().getSelectedVehicle() != versionedIData.getRoute().getVehicle()) {
if (!switchAllowed) continue;
if (initialVehicleIds.contains(versionedIData.getRoute().getVehicle().getId())) continue;
}
}
if(versionedIData.getiData().getSelectedVehicle() != versionedIData.getRoute().getVehicle()) {
if (fleetManager.isLocked(versionedIData.getiData().getSelectedVehicle())) {
Vehicle available = fleetManager.getAvailableVehicle(versionedIData.getiData().getSelectedVehicle().getVehicleTypeIdentifier());
if (available != null) {
InsertionData oldData = versionedIData.getiData();
InsertionData newData = new InsertionData(oldData.getInsertionCost(), oldData.getPickupInsertionIndex(),
oldData.getDeliveryInsertionIndex(), available, oldData.getSelectedDriver());
newData.setVehicleDepartureTime(oldData.getVehicleDepartureTime());
for(Event e : oldData.getEvents()){
if(e instanceof SwitchVehicle){
newData.getEvents().add(new SwitchVehicle(versionedIData.getRoute(),available,oldData.getVehicleDepartureTime()));
}
else newData.getEvents().add(e);
}
versionedIData = new VersionedInsertionData(newData, versionedIData.getVersion(), versionedIData.getRoute());
} else continue;
}
}
int currentDataVersion = updates.get(versionedIData.getRoute());
if(versionedIData.getVersion() == currentDataVersion){
if(best == null) {
best = versionedIData.getiData();
bestRoute = versionedIData.getRoute();
}
else {
secondBest = versionedIData.getiData();
break;
}
}
}
VehicleRoute emptyRoute = VehicleRoute.emptyRoute();
InsertionData iData = insertionCostsCalculator.getInsertionData(emptyRoute, j, null, -1, null, Double.MAX_VALUE);
if(!(iData instanceof InsertionData.NoInsertionFound)){
if (best == null) {
best = iData;
bestRoute = emptyRoute;
} else if (iData.getInsertionCost() < best.getInsertionCost()) {
secondBest = best;
best = iData;
bestRoute = emptyRoute;
} else if (secondBest == null || (iData.getInsertionCost() < secondBest.getInsertionCost())) {
secondBest = iData;
}
}
if (best == null) {
badJobs.add(j);
continue;
}
double score = score(j, best, secondBest, scoringFunction);
ScoredJob scoredJob;
if (bestRoute == emptyRoute) {
scoredJob = new ScoredJob(j, score, best, bestRoute, true);
} else scoredJob = new ScoredJob(j, score, best, bestRoute, false);
if(bestScoredJob == null){
bestScoredJob = scoredJob;
}
else if(scoredJob.getScore() > bestScoredJob.getScore()){
bestScoredJob = scoredJob;
}
}
return bestScoredJob;
}
static double score(Job unassignedJob, InsertionData best, InsertionData secondBest, ScoringFunction scoringFunction) {
if (best == null) {
throw new IllegalStateException("cannot insert job " + unassignedJob.getId());
}
double score;
if (secondBest == null) { //either there is only one vehicle or there are more vehicles, but they cannot load unassignedJob
//if only one vehicle, I want the job to be inserted with min iCosts
//if there are more vehicles, I want this job to be prioritized since there are no alternatives
score = Integer.MAX_VALUE - best.getInsertionCost() + scoringFunction.score(best, unassignedJob);
} else {
score = (secondBest.getInsertionCost() - best.getInsertionCost()) + scoringFunction.score(best, unassignedJob);
}
return score;
}
}

View file

@ -23,18 +23,17 @@ import jsprit.core.problem.solution.route.VehicleRoute;
import java.util.Collection;
/**
* Basic interface for insertion strategies
*
* @author stefan schroeder
*
*/
public interface InsertionStrategy {
/**
* Inserts unassigned jobs into vehicle routes.
*
* @param vehicleRoutes existing vehicle routes
* @param unassignedJobs jobs to be inserted
*/

View file

@ -27,17 +27,17 @@ import java.util.HashMap;
import java.util.Map;
class JobCalculatorSwitcher implements JobInsertionCostsCalculator{
class JobCalculatorSwitcher implements JobInsertionCostsCalculator {
private Map<Class<? extends Job>,JobInsertionCostsCalculator> calcMap = new HashMap<Class<? extends Job>, JobInsertionCostsCalculator>();
private Map<Class<? extends Job>, JobInsertionCostsCalculator> calcMap = new HashMap<Class<? extends Job>, JobInsertionCostsCalculator>();
void put(Class<? extends Job> jobClass, JobInsertionCostsCalculator jic){
void put(Class<? extends Job> jobClass, JobInsertionCostsCalculator jic) {
calcMap.put(jobClass, jic);
}
public InsertionData getInsertionData(VehicleRoute currentRoute, Job jobToInsert, Vehicle newVehicle, double newVehicleDepartureTime, Driver newDriver, double bestKnownScore){
public InsertionData getInsertionData(VehicleRoute currentRoute, Job jobToInsert, Vehicle newVehicle, double newVehicleDepartureTime, Driver newDriver, double bestKnownScore) {
JobInsertionCostsCalculator jic = calcMap.get(jobToInsert.getClass());
if(jic==null) throw new IllegalStateException("cannot find calculator for " + jobToInsert.getClass());
if (jic == null) throw new IllegalStateException("cannot find calculator for " + jobToInsert.getClass());
return jic.getInsertionData(currentRoute, jobToInsert, newVehicle, newVehicleDepartureTime, newDriver, bestKnownScore);
}

View file

@ -30,7 +30,7 @@ import jsprit.core.problem.vehicle.VehicleImpl.NoVehicle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
final class JobInsertionConsideringFixCostsCalculator implements JobInsertionCostsCalculator, SoftRouteConstraint{
final class JobInsertionConsideringFixCostsCalculator implements JobInsertionCostsCalculator, SoftRouteConstraint {
private static final Logger logger = LogManager.getLogger(JobInsertionConsideringFixCostsCalculator.class);
@ -46,17 +46,17 @@ final class JobInsertionConsideringFixCostsCalculator implements JobInsertionCos
super();
this.standardServiceInsertion = standardInsertionCalculator;
this.stateGetter = stateGetter;
logger.debug("inialise " + this);
logger.debug("inialise {}", this);
}
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownPrice) {
double fixcost_contribution = getFixCostContribution(currentRoute,jobToInsert, newVehicle);
if(fixcost_contribution > bestKnownPrice){
double fixcost_contribution = getFixCostContribution(currentRoute, jobToInsert, newVehicle);
if (fixcost_contribution > bestKnownPrice) {
return InsertionData.createEmptyInsertionData();
}
InsertionData iData = standardServiceInsertion.getInsertionData(currentRoute, jobToInsert, newVehicle, newVehicleDepartureTime, newDriver, bestKnownPrice);
if(iData instanceof NoInsertionFound){
if (iData instanceof NoInsertionFound) {
return iData;
}
double totalInsertionCost = iData.getInsertionCost() + fixcost_contribution;
@ -66,65 +66,61 @@ final class JobInsertionConsideringFixCostsCalculator implements JobInsertionCos
return insertionData;
}
private double getFixCostContribution(final VehicleRoute currentRoute,
final Job jobToInsert, final Vehicle newVehicle) {
double relFixCost = getDeltaRelativeFixCost(currentRoute, newVehicle, jobToInsert);
double absFixCost = getDeltaAbsoluteFixCost(currentRoute, newVehicle, jobToInsert);
double deltaFixCost = (1-solution_completeness_ratio)*relFixCost + solution_completeness_ratio*absFixCost;
double fixcost_contribution = weight_deltaFixCost*solution_completeness_ratio*deltaFixCost;
private double getFixCostContribution(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle) {
Capacity currentMaxLoadInRoute = getCurrentMaxLoadInRoute(currentRoute);
double relFixCost = getDeltaRelativeFixCost(currentRoute, newVehicle, jobToInsert,currentMaxLoadInRoute);
double absFixCost = getDeltaAbsoluteFixCost(currentRoute, newVehicle, jobToInsert,currentMaxLoadInRoute);
double deltaFixCost = (1 - solution_completeness_ratio) * relFixCost + solution_completeness_ratio * absFixCost;
double fixcost_contribution = weight_deltaFixCost * solution_completeness_ratio * deltaFixCost;
return fixcost_contribution;
}
public void setWeightOfFixCost(double weight){
public void setWeightOfFixCost(double weight) {
weight_deltaFixCost = weight;
logger.debug("set weightOfFixCostSaving to " + weight);
logger.debug("set weightOfFixCostSaving to {}", weight);
}
@Override
public String toString() {
return "[name=calculatesServiceInsertionConsideringFixCost][weightOfFixedCostSavings="+weight_deltaFixCost+"]";
return "[name=calculatesServiceInsertionConsideringFixCost][weightOfFixedCostSavings=" + weight_deltaFixCost + "]";
}
public void setSolutionCompletenessRatio(double ratio){
public void setSolutionCompletenessRatio(double ratio) {
solution_completeness_ratio = ratio;
}
private double getDeltaAbsoluteFixCost(VehicleRoute route, Vehicle newVehicle, Job job) {
Capacity load = Capacity.addup(getCurrentMaxLoadInRoute(route), job.getSize());
// double load = getCurrentMaxLoadInRoute(route) + job.getCapacityDemand();
private double getDeltaAbsoluteFixCost(VehicleRoute route, Vehicle newVehicle, Job job, Capacity currentMaxLoadInRoute) {
Capacity load = Capacity.addup(currentMaxLoadInRoute, job.getSize());
double currentFix = 0.0;
if(route.getVehicle() != null){
if(!(route.getVehicle() instanceof NoVehicle)){
if (route.getVehicle() != null) {
if (!(route.getVehicle() instanceof NoVehicle)) {
currentFix += route.getVehicle().getType().getVehicleCostParams().fix;
}
}
if(!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)){
if (!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)) {
return Double.MAX_VALUE;
}
return newVehicle.getType().getVehicleCostParams().fix - currentFix;
}
private double getDeltaRelativeFixCost(VehicleRoute route, Vehicle newVehicle, Job job) {
Capacity currentLoad = getCurrentMaxLoadInRoute(route);
// int currentLoad = getCurrentMaxLoadInRoute(route);
private double getDeltaRelativeFixCost(VehicleRoute route, Vehicle newVehicle, Job job, Capacity currentLoad) {
Capacity load = Capacity.addup(currentLoad, job.getSize());
// double load = currentLoad + job.getCapacityDemand();
double currentRelFix = 0.0;
if(route.getVehicle() != null){
if(!(route.getVehicle() instanceof NoVehicle)){
if (route.getVehicle() != null) {
if (!(route.getVehicle() instanceof NoVehicle)) {
currentRelFix += route.getVehicle().getType().getVehicleCostParams().fix * Capacity.divide(currentLoad, route.getVehicle().getType().getCapacityDimensions());
}
}
if(!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)){
if (!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)) {
return Double.MAX_VALUE;
}
double relativeFixCost = newVehicle.getType().getVehicleCostParams().fix* (Capacity.divide(load, newVehicle.getType().getCapacityDimensions())) - currentRelFix;
double relativeFixCost = newVehicle.getType().getVehicleCostParams().fix * (Capacity.divide(load, newVehicle.getType().getCapacityDimensions())) - currentRelFix;
return relativeFixCost;
}
private Capacity getCurrentMaxLoadInRoute(VehicleRoute route) {
Capacity maxLoad = stateGetter.getRouteState(route, InternalStates.MAXLOAD, Capacity.class);
if(maxLoad == null) maxLoad = Capacity.Builder.newInstance().build();
if (maxLoad == null) maxLoad = Capacity.Builder.newInstance().build();
return maxLoad;
}

View file

@ -22,7 +22,7 @@ import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.Vehicle;
public interface JobInsertionCostsCalculator {
public interface JobInsertionCostsCalculator {
public InsertionData getInsertionData(VehicleRoute currentRoute, Job newJob, Vehicle newVehicle, double newVehicleDepartureTime, Driver newDriver, double bestKnownCosts);

View file

@ -32,8 +32,6 @@ import java.util.ArrayList;
import java.util.List;
public class JobInsertionCostsCalculatorBuilder {
private static class CalculatorPlusListeners {
@ -97,7 +95,7 @@ public class JobInsertionCostsCalculatorBuilder {
/**
* Constructs the builder.
*
* <p/>
* <p>Some calculators require information from the overall algorithm or the higher-level insertion procedure. Thus listeners inform them.
* These listeners are cached in the according list and can thus be added when its time to add them.
*
@ -112,11 +110,11 @@ public class JobInsertionCostsCalculatorBuilder {
/**
* Sets activityStates. MUST be set.
* @param stateManager
*
* @param stateManager
* @return
*/
public JobInsertionCostsCalculatorBuilder setStateManager(RouteAndActivityStateGetter stateManager){
public JobInsertionCostsCalculatorBuilder setStateManager(RouteAndActivityStateGetter stateManager) {
this.states = stateManager;
return this;
}
@ -127,7 +125,7 @@ public class JobInsertionCostsCalculatorBuilder {
* @param vehicleRoutingProblem
* @return
*/
public JobInsertionCostsCalculatorBuilder setVehicleRoutingProblem(VehicleRoutingProblem vehicleRoutingProblem){
public JobInsertionCostsCalculatorBuilder setVehicleRoutingProblem(VehicleRoutingProblem vehicleRoutingProblem) {
this.vrp = vehicleRoutingProblem;
return this;
}
@ -138,24 +136,25 @@ public class JobInsertionCostsCalculatorBuilder {
* @param fleetManager
* @return
*/
public JobInsertionCostsCalculatorBuilder setVehicleFleetManager(VehicleFleetManager fleetManager){
public JobInsertionCostsCalculatorBuilder setVehicleFleetManager(VehicleFleetManager fleetManager) {
this.fleetManager = fleetManager;
return this;
}
/**
* Sets a flag to build a calculator based on local calculations.
*
* <p/>
* <p>Insertion of a job and job-activity is evaluated based on the previous and next activity.
*
* @param addDefaultCostCalc
*/
public JobInsertionCostsCalculatorBuilder setLocalLevel(boolean addDefaultCostCalc){
public JobInsertionCostsCalculatorBuilder setLocalLevel(boolean addDefaultCostCalc) {
local = true;
this.addDefaultCostCalc = addDefaultCostCalc;
return this;
}
public JobInsertionCostsCalculatorBuilder setActivityInsertionCostsCalculator(ActivityInsertionCostsCalculator activityInsertionCostsCalculator){
public JobInsertionCostsCalculatorBuilder setActivityInsertionCostsCalculator(ActivityInsertionCostsCalculator activityInsertionCostsCalculator) {
this.activityInsertionCostCalculator = activityInsertionCostsCalculator;
return this;
}
@ -167,7 +166,7 @@ public class JobInsertionCostsCalculatorBuilder {
* @param memory
* @param addDefaultMarginalCostCalc
*/
public JobInsertionCostsCalculatorBuilder setRouteLevel(int forwardLooking, int memory, boolean addDefaultMarginalCostCalc){
public JobInsertionCostsCalculatorBuilder setRouteLevel(int forwardLooking, int memory, boolean addDefaultMarginalCostCalc) {
local = false;
this.forwardLooking = forwardLooking;
this.memory = memory;
@ -180,13 +179,13 @@ public class JobInsertionCostsCalculatorBuilder {
*
* @param weightOfFixedCosts
*/
public JobInsertionCostsCalculatorBuilder considerFixedCosts(double weightOfFixedCosts){
public JobInsertionCostsCalculatorBuilder considerFixedCosts(double weightOfFixedCosts) {
considerFixedCost = true;
this.weightOfFixedCost = weightOfFixedCosts;
return this;
}
public JobInsertionCostsCalculatorBuilder experimentalTimeScheduler(double timeSlice, int neighbors){
public JobInsertionCostsCalculatorBuilder experimentalTimeScheduler(double timeSlice, int neighbors) {
timeScheduling = true;
this.timeSlice = timeSlice;
this.neighbors = neighbors;
@ -199,31 +198,33 @@ public class JobInsertionCostsCalculatorBuilder {
* @return jobInsertionCalculator.
* @throws IllegalStateException if vrp == null or activityStates == null or fleetManager == null.
*/
public JobInsertionCostsCalculator build(){
if(vrp == null) throw new IllegalStateException("vehicle-routing-problem is null, but it must be set (this.setVehicleRoutingProblem(vrp))");
if(states == null) throw new IllegalStateException("states is null, but is must be set (this.setStateManager(states))");
if(fleetManager == null) throw new IllegalStateException("fleetManager is null, but it must be set (this.setVehicleFleetManager(fleetManager))");
public JobInsertionCostsCalculator build() {
if (vrp == null)
throw new IllegalStateException("vehicle-routing-problem is null, but it must be set (this.setVehicleRoutingProblem(vrp))");
if (states == null)
throw new IllegalStateException("states is null, but is must be set (this.setStateManager(states))");
if (fleetManager == null)
throw new IllegalStateException("fleetManager is null, but it must be set (this.setVehicleFleetManager(fleetManager))");
JobInsertionCostsCalculator baseCalculator = null;
CalculatorPlusListeners standardLocal = null;
if(local){
if (local) {
standardLocal = createStandardLocal(vrp, states);
}
else{
} else {
checkServicesOnly();
standardLocal = createStandardRoute(vrp, states,forwardLooking,memory);
standardLocal = createStandardRoute(vrp, states, forwardLooking, memory);
}
baseCalculator = standardLocal.getCalculator();
addAlgorithmListeners(standardLocal.getAlgorithmListener());
addInsertionListeners(standardLocal.getInsertionListener());
if(considerFixedCost){
if (considerFixedCost) {
CalculatorPlusListeners withFixed = createCalculatorConsideringFixedCosts(vrp, baseCalculator, states, weightOfFixedCost);
baseCalculator = withFixed.getCalculator();
addAlgorithmListeners(withFixed.getAlgorithmListener());
addInsertionListeners(withFixed.getInsertionListener());
}
if(timeScheduling){
if (timeScheduling) {
// baseCalculator = new CalculatesServiceInsertionWithTimeSchedulingInSlices(baseCalculator,timeSlice,neighbors);
CalculatesServiceInsertionWithTimeScheduling wts = new CalculatesServiceInsertionWithTimeScheduling(baseCalculator,timeSlice,neighbors);
CalculatesServiceInsertionWithTimeScheduling wts = new CalculatesServiceInsertionWithTimeScheduling(baseCalculator, timeSlice, neighbors);
CalculatorPlusListeners calcPlusListeners = new CalculatorPlusListeners(wts);
calcPlusListeners.getInsertionListener().add(new CalculatesServiceInsertionWithTimeScheduling.KnowledgeInjection(wts));
addInsertionListeners(calcPlusListeners.getInsertionListener());
@ -233,8 +234,8 @@ public class JobInsertionCostsCalculatorBuilder {
}
private void checkServicesOnly() {
for(Job j : vrp.getJobs().values()){
if(j instanceof Shipment){
for (Job j : vrp.getJobs().values()) {
if (j instanceof Shipment) {
throw new UnsupportedOperationException("currently the 'insert-on-route-level' option is only available for services (i.e. service, pickup, delivery), \n" +
"if you want to deal with shipments switch to option 'local-level' by either setting bestInsertionBuilder.setLocalLevel() or \n"
+ "by omitting the xml-tag '<level forwardLooking=2 memory=1>route</level>' when defining your insertionStrategy in algo-config.xml file");
@ -244,26 +245,27 @@ public class JobInsertionCostsCalculatorBuilder {
}
private void addInsertionListeners(List<InsertionListener> list) {
for(InsertionListener iL : list){
for (InsertionListener iL : list) {
insertionListeners.add(iL);
}
}
private void addAlgorithmListeners(List<PrioritizedVRAListener> list) {
for(PrioritizedVRAListener aL : list){
for (PrioritizedVRAListener aL : list) {
algorithmListeners.add(aL);
}
}
private CalculatorPlusListeners createStandardLocal(final VehicleRoutingProblem vrp, RouteAndActivityStateGetter statesManager){
if(constraintManager == null) throw new IllegalStateException("constraint-manager is null");
private CalculatorPlusListeners createStandardLocal(final VehicleRoutingProblem vrp, RouteAndActivityStateGetter statesManager) {
if (constraintManager == null) throw new IllegalStateException("constraint-manager is null");
ActivityInsertionCostsCalculator actInsertionCalc;
if(activityInsertionCostCalculator == null && addDefaultCostCalc){
actInsertionCalc = new LocalActivityInsertionCostsCalculator(vrp.getTransportCosts(), vrp.getActivityCosts());
}
else if(activityInsertionCostCalculator == null && !addDefaultCostCalc){
actInsertionCalc = new ActivityInsertionCostsCalculator(){
ConfigureLocalActivityInsertionCalculator configLocal = null;
if (activityInsertionCostCalculator == null && addDefaultCostCalc) {
actInsertionCalc = new LocalActivityInsertionCostsCalculator(vrp.getTransportCosts(), vrp.getActivityCosts(), statesManager);
configLocal = new ConfigureLocalActivityInsertionCalculator(vrp, (LocalActivityInsertionCostsCalculator) actInsertionCalc);
} else if (activityInsertionCostCalculator == null && !addDefaultCostCalc) {
actInsertionCalc = new ActivityInsertionCostsCalculator() {
@Override
public double getCosts(JobInsertionContext iContext, TourActivity prevAct, TourActivity nextAct, TourActivity newAct,
@ -272,8 +274,7 @@ public class JobInsertionCostsCalculatorBuilder {
}
};
}
else{
} else {
actInsertionCalc = activityInsertionCostCalculator;
}
@ -290,16 +291,24 @@ public class JobInsertionCostsCalculatorBuilder {
ServiceInsertionCalculator serviceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager);
serviceInsertion.setJobActivityFactory(activityFactory);
BreakInsertionCalculator breakInsertionCalculator = new BreakInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager);
breakInsertionCalculator.setJobActivityFactory(activityFactory);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
switcher.put(Shipment.class, shipmentInsertion);
switcher.put(Service.class, serviceInsertion);
switcher.put(Pickup.class, serviceInsertion);
switcher.put(Delivery.class, serviceInsertion);
switcher.put(Break.class, breakInsertionCalculator);
return new CalculatorPlusListeners(switcher);
CalculatorPlusListeners calculatorPlusListeners = new CalculatorPlusListeners(switcher);
if (configLocal != null) {
calculatorPlusListeners.insertionListener.add(configLocal);
}
return calculatorPlusListeners;
}
private CalculatorPlusListeners createCalculatorConsideringFixedCosts(VehicleRoutingProblem vrp, JobInsertionCostsCalculator baseCalculator, RouteAndActivityStateGetter activityStates2, double weightOfFixedCosts){
private CalculatorPlusListeners createCalculatorConsideringFixedCosts(VehicleRoutingProblem vrp, JobInsertionCostsCalculator baseCalculator, RouteAndActivityStateGetter activityStates2, double weightOfFixedCosts) {
final JobInsertionConsideringFixCostsCalculator withFixCost = new JobInsertionConsideringFixCostsCalculator(baseCalculator, activityStates2);
withFixCost.setWeightOfFixCost(weightOfFixedCosts);
CalculatorPlusListeners calcPlusListeners = new CalculatorPlusListeners(withFixCost);
@ -307,17 +316,16 @@ public class JobInsertionCostsCalculatorBuilder {
return calcPlusListeners;
}
private CalculatorPlusListeners createStandardRoute(final VehicleRoutingProblem vrp, RouteAndActivityStateGetter activityStates2, int forwardLooking, int solutionMemory){
private CalculatorPlusListeners createStandardRoute(final VehicleRoutingProblem vrp, RouteAndActivityStateGetter activityStates2, int forwardLooking, int solutionMemory) {
ActivityInsertionCostsCalculator routeLevelCostEstimator;
if(activityInsertionCostCalculator == null && addDefaultCostCalc){
if (activityInsertionCostCalculator == null && addDefaultCostCalc) {
RouteLevelActivityInsertionCostsEstimator routeLevelActivityInsertionCostsEstimator = new RouteLevelActivityInsertionCostsEstimator(vrp.getTransportCosts(), vrp.getActivityCosts(), activityStates2);
routeLevelActivityInsertionCostsEstimator.setForwardLooking(forwardLooking);
routeLevelCostEstimator = routeLevelActivityInsertionCostsEstimator;
}
else if(activityInsertionCostCalculator == null && !addDefaultCostCalc){
routeLevelCostEstimator = new ActivityInsertionCostsCalculator(){
} else if (activityInsertionCostCalculator == null && !addDefaultCostCalc) {
routeLevelCostEstimator = new ActivityInsertionCostsCalculator() {
final ActivityInsertionCosts noInsertionCosts = new ActivityInsertionCosts(0.,0.);
final ActivityInsertionCosts noInsertionCosts = new ActivityInsertionCosts(0., 0.);
@Override
public double getCosts(JobInsertionContext iContext, TourActivity prevAct, TourActivity nextAct, TourActivity newAct,
@ -326,8 +334,7 @@ public class JobInsertionCostsCalculatorBuilder {
}
};
}
else{
} else {
routeLevelCostEstimator = activityInsertionCostCalculator;
}
ServiceInsertionOnRouteLevelCalculator jobInsertionCalculator = new ServiceInsertionOnRouteLevelCalculator(vrp.getTransportCosts(), vrp.getActivityCosts(), routeLevelCostEstimator, constraintManager, constraintManager);
@ -343,7 +350,7 @@ public class JobInsertionCostsCalculatorBuilder {
return new CalculatorPlusListeners(jobInsertionCalculator);
}
private JobInsertionCostsCalculator createFinalInsertion(VehicleFleetManager fleetManager, JobInsertionCostsCalculator baseCalc, RouteAndActivityStateGetter activityStates2){
private JobInsertionCostsCalculator createFinalInsertion(VehicleFleetManager fleetManager, JobInsertionCostsCalculator baseCalc, RouteAndActivityStateGetter activityStates2) {
VehicleTypeDependentJobInsertionCalculator vehicleTypeDependentJobInsertionCalculator = new VehicleTypeDependentJobInsertionCalculator(vrp, fleetManager, baseCalc);
vehicleTypeDependentJobInsertionCalculator.setVehicleSwitchAllowed(allowVehicleSwitch);
return vehicleTypeDependentJobInsertionCalculator;

View file

@ -44,17 +44,17 @@ public class JobInsertionCostsCalculatorLightFactory {
* @param constraintManager constraint manager
* @return insertion calculator
*/
public static JobInsertionCostsCalculatorLight createStandardCalculator(VehicleRoutingProblem vrp, VehicleFleetManager fleetManager, StateManager stateManager, ConstraintManager constraintManager){
public static JobInsertionCostsCalculatorLight createStandardCalculator(VehicleRoutingProblem vrp, VehicleFleetManager fleetManager, StateManager stateManager, ConstraintManager constraintManager) {
List<VehicleRoutingAlgorithmListeners.PrioritizedVRAListener> al = new ArrayList<VehicleRoutingAlgorithmListeners.PrioritizedVRAListener>();
List<InsertionListener> il = new ArrayList<InsertionListener>();
JobInsertionCostsCalculatorBuilder builder = new JobInsertionCostsCalculatorBuilder(il,al);
JobInsertionCostsCalculatorBuilder builder = new JobInsertionCostsCalculatorBuilder(il, al);
builder.setVehicleRoutingProblem(vrp).setConstraintManager(constraintManager).setStateManager(stateManager).setVehicleFleetManager(fleetManager);
final JobInsertionCostsCalculator calculator = builder.build();
return new JobInsertionCostsCalculatorLight() {
@Override
public InsertionData getInsertionData(Job unassignedJob, VehicleRoute route, double bestKnownCosts) {
return calculator.getInsertionData(route,unassignedJob,AbstractInsertionStrategy.NO_NEW_VEHICLE_YET,AbstractInsertionStrategy.NO_NEW_DEPARTURE_TIME_YET,AbstractInsertionStrategy.NO_NEW_DRIVER_YET,bestKnownCosts);
return calculator.getInsertionData(route, unassignedJob, AbstractInsertionStrategy.NO_NEW_VEHICLE_YET, AbstractInsertionStrategy.NO_NEW_DEPARTURE_TIME_YET, AbstractInsertionStrategy.NO_NEW_DRIVER_YET, bestKnownCosts);
}
};

View file

@ -1,4 +1,3 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
@ -18,34 +17,42 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.state.InternalStates;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.util.CalculationUtils;
/**
* Calculates activity insertion costs locally, i.e. by comparing the additional costs of insertion the new activity k between
* activity i (prevAct) and j (nextAct).
* Additional costs are then basically calculated as delta c = c_ik + c_kj - c_ij.
*
* <p/>
* <p>Note once time has an effect on costs this class requires activity endTimes.
*
* @author stefan
*
*/
class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCalculator{
class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCalculator {
private VehicleRoutingTransportCosts routingCosts;
private VehicleRoutingActivityCosts activityCosts;
private double activityCostsWeight = 1.;
public LocalActivityInsertionCostsCalculator(VehicleRoutingTransportCosts routingCosts, VehicleRoutingActivityCosts actCosts) {
private double solutionCompletenessRatio = 1.;
private RouteAndActivityStateGetter stateManager;
public LocalActivityInsertionCostsCalculator(VehicleRoutingTransportCosts routingCosts, VehicleRoutingActivityCosts actCosts, RouteAndActivityStateGetter stateManager) {
super();
this.routingCosts = routingCosts;
this.activityCosts = actCosts;
this.stateManager = stateManager;
}
@Override
@ -55,35 +62,49 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
double tp_time_prevAct_newAct = routingCosts.getTransportTime(prevAct.getLocation(), newAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
double newAct_arrTime = depTimeAtPrevAct + tp_time_prevAct_newAct;
double newAct_endTime = CalculationUtils.getActivityEndTime(newAct_arrTime, newAct);
double act_costs_newAct = activityCosts.getActivityCost(newAct, newAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
//open routes
if(nextAct instanceof End){
if(!iFacts.getNewVehicle().isReturnToDepot()){
return tp_costs_prevAct_newAct;
}
}
if (isEnd(nextAct) && !toDepot(iFacts.getNewVehicle())) return tp_costs_prevAct_newAct;
double tp_costs_newAct_nextAct = routingCosts.getTransportCost(newAct.getLocation(), nextAct.getLocation(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double tp_time_newAct_nextAct = routingCosts.getTransportTime(newAct.getLocation(), nextAct.getLocation(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double nextAct_arrTime = newAct_endTime + tp_time_newAct_nextAct;
double endTime_nextAct_new = CalculationUtils.getActivityEndTime(nextAct_arrTime, nextAct);
double act_costs_nextAct = activityCosts.getActivityCost(nextAct, nextAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct + act_costs_newAct + act_costs_nextAct;
double oldCosts;
if(iFacts.getRoute().isEmpty()){
double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct + solutionCompletenessRatio * activityCostsWeight * (act_costs_newAct + act_costs_nextAct);
double oldCosts = 0.;
if (iFacts.getRoute().isEmpty()) {
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
double arrTime_nextAct = routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
double actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
oldCosts = tp_costs_prevAct_nextAct + actCost_nextAct;
}
else{
oldCosts += tp_costs_prevAct_nextAct;
} else {
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
double arrTime_nextAct = routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
double arrTime_nextAct = depTimeAtPrevAct + routingCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
double endTime_nextAct_old = CalculationUtils.getActivityEndTime(arrTime_nextAct, nextAct);
double actCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct, iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
oldCosts = tp_costs_prevAct_nextAct + actCost_nextAct;
double endTimeDelay_nextAct = Math.max(0, endTime_nextAct_new - endTime_nextAct_old);
Double futureWaiting = stateManager.getActivityState(nextAct, iFacts.getRoute().getVehicle(), InternalStates.FUTURE_WAITING, Double.class);
if (futureWaiting == null) futureWaiting = 0.;
double waitingTime_savings_timeUnit = Math.min(futureWaiting, endTimeDelay_nextAct);
double waitingTime_savings = waitingTime_savings_timeUnit * iFacts.getRoute().getVehicle().getType().getVehicleCostParams().perWaitingTimeUnit;
oldCosts += solutionCompletenessRatio * activityCostsWeight * waitingTime_savings;
oldCosts += tp_costs_prevAct_nextAct + solutionCompletenessRatio * activityCostsWeight * actCost_nextAct;
}
return totalCosts - oldCosts;
}
private boolean toDepot(Vehicle newVehicle) {
return newVehicle.isReturnToDepot();
}
private boolean isEnd(TourActivity nextAct) {
return nextAct instanceof End;
}
public void setSolutionCompletenessRatio(double solutionCompletenessRatio) {
this.solutionCompletenessRatio = solutionCompletenessRatio;
}
}

View file

@ -18,7 +18,7 @@
******************************************************************************/
package jsprit.core.algorithm.recreate;
public class NoSolutionFoundException extends IllegalStateException{
public class NoSolutionFoundException extends IllegalStateException {
public NoSolutionFoundException(String errorMsg) {
super(errorMsg);

View file

@ -17,170 +17,33 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Break;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.VehicleRoute;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* Insertion based on regret approach.
*
* <p>Basically calculates the insertion cost of the firstBest and the secondBest alternative. The score is then calculated as difference
* between secondBest and firstBest, plus additional scoring variables that can defined in this.ScoringFunction.
* The idea is that if the cost of the secondBest alternative is way higher than the first best, it seems to be important to insert this
* customer immediatedly. If difference is not that high, it might not impact solution if this customer is inserted later.
*
* @author stefan schroeder
*
*/
* Insertion based on regret approach.
* <p/>
* <p>Basically calculates the insertion cost of the firstBest and the secondBest alternative. The score is then calculated as difference
* between secondBest and firstBest, plus additional scoring variables that can defined in this.ScoringFunction.
* The idea is that if the cost of the secondBest alternative is way higher than the first best, it seems to be important to insert this
* customer immediatedly. If difference is not that high, it might not impact solution if this customer is inserted later.
*
* @author stefan schroeder
*/
public class RegretInsertion extends AbstractInsertionStrategy {
static class ScoredJob {
private Job job;
private double score;
private InsertionData insertionData;
private VehicleRoute route;
private boolean newRoute;
ScoredJob(Job job, double score, InsertionData insertionData, VehicleRoute route, boolean isNewRoute) {
this.job = job;
this.score = score;
this.insertionData = insertionData;
this.route = route;
this.newRoute = isNewRoute;
}
public boolean isNewRoute() {
return newRoute;
}
public Job getJob() {
return job;
}
public double getScore() {
return score;
}
public InsertionData getInsertionData() {
return insertionData;
}
public VehicleRoute getRoute() {
return route;
}
}
static class BadJob extends ScoredJob {
BadJob(Job job) {
super(job, 0., null, null, false);
}
}
/**
* Scorer to include other impacts on score such as time-window length or distance to depot.
*
* @author schroeder
*
*/
static interface ScoringFunction {
public double score(InsertionData best, Job job);
}
/**
* Scorer that includes the length of the time-window when scoring a job. The wider the time-window, the lower the score.
*
* <p>This is the default scorer, i.e.: score = (secondBest - firstBest) + this.TimeWindowScorer.score(job)
*
* @author schroeder
*
*/
public static class DefaultScorer implements ScoringFunction {
private VehicleRoutingProblem vrp;
private double tw_param = - 0.5;
private double depotDistance_param = + 0.1;
private double minTimeWindowScore = - 100000;
public DefaultScorer(VehicleRoutingProblem vrp) {
this.vrp = vrp;
}
public void setTimeWindowParam(double tw_param){ this.tw_param = tw_param; }
public void setDepotDistanceParam(double depotDistance_param){ this.depotDistance_param = depotDistance_param; }
@Override
public double score(InsertionData best, Job job) {
double score;
if(job instanceof Service){
score = scoreService(best, job);
}
else if(job instanceof Shipment){
score = scoreShipment(best,job);
}
else throw new IllegalStateException("not supported");
return score;
}
private double scoreShipment(InsertionData best, Job job) {
Shipment shipment = (Shipment)job;
double maxDepotDistance_1 = Math.max(
getDistance(best.getSelectedVehicle().getStartLocation(),shipment.getPickupLocation()),
getDistance(best.getSelectedVehicle().getStartLocation(),shipment.getDeliveryLocation())
);
double maxDepotDistance_2 = Math.max(
getDistance(best.getSelectedVehicle().getEndLocation(),shipment.getPickupLocation()),
getDistance(best.getSelectedVehicle().getEndLocation(),shipment.getDeliveryLocation())
);
double maxDepotDistance = Math.max(maxDepotDistance_1,maxDepotDistance_2);
double minTimeToOperate = Math.min(shipment.getPickupTimeWindow().getEnd()-shipment.getPickupTimeWindow().getStart(),
shipment.getDeliveryTimeWindow().getEnd()-shipment.getDeliveryTimeWindow().getStart());
return Math.max(tw_param * minTimeToOperate,minTimeWindowScore) + depotDistance_param * maxDepotDistance;
}
private double scoreService(InsertionData best, Job job) {
double maxDepotDistance = Math.max(
getDistance(best.getSelectedVehicle().getStartLocation(), ((Service) job).getLocation()),
getDistance(best.getSelectedVehicle().getEndLocation(), ((Service) job).getLocation())
);
return Math.max(tw_param * (((Service)job).getTimeWindow().getEnd() - ((Service)job).getTimeWindow().getStart()),minTimeWindowScore) +
depotDistance_param * maxDepotDistance;
}
private double getDistance(Location loc1, Location loc2) {
return vrp.getTransportCosts().getTransportCost(loc1,loc2,0.,null,null);
}
@Override
public String toString() {
return "[name=defaultScorer][twParam="+tw_param+"][depotDistanceParam=" + depotDistance_param + "]";
}
}
private static Logger logger = LogManager.getLogger(RegretInsertion.class);
private static Logger logger = LogManager.getLogger(RegretInsertionFast.class);
private ScoringFunction scoringFunction;
@ -189,7 +52,7 @@ public class RegretInsertion extends AbstractInsertionStrategy {
/**
* Sets the scoring function.
*
* <p/>
* <p>By default, the this.TimeWindowScorer is used.
*
* @param scoringFunction to score
@ -203,38 +66,57 @@ public class RegretInsertion extends AbstractInsertionStrategy {
this.scoringFunction = new DefaultScorer(vehicleRoutingProblem);
this.insertionCostsCalculator = jobInsertionCalculator;
this.vrp = vehicleRoutingProblem;
logger.debug("initialise " + this);
logger.debug("initialise {}", this);
}
@Override
public String toString() {
return "[name=regretInsertion][additionalScorer="+scoringFunction+"]";
return "[name=regretInsertion][additionalScorer=" + scoringFunction + "]";
}
/**
* Runs insertion.
*
* <p/>
* <p>Before inserting a job, all unassigned jobs are scored according to its best- and secondBest-insertion plus additional scoring variables.
*
*/
@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
Iterator<Job> jobIterator = unassignedJobs.iterator();
while (jobIterator.hasNext()){
Job job = jobIterator.next();
if(job instanceof Break){
VehicleRoute route = findRoute(routes,job);
if(route == null){
badJobs.add(job);
}
else {
InsertionData iData = insertionCostsCalculator.getInsertionData(route, job, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, Double.MAX_VALUE);
if (iData instanceof InsertionData.NoInsertionFound) {
badJobs.add(job);
} else {
insertJob(job, iData, route);
}
}
jobIterator.remove();
}
}
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
while (!jobs.isEmpty()) {
List<Job> unassignedJobList = new ArrayList<Job>(jobs);
List<Job> badJobList = new ArrayList<Job>();
ScoredJob bestScoredJob = nextJob(routes, unassignedJobList, badJobList);
if(bestScoredJob != null){
if(bestScoredJob.isNewRoute()){
if (bestScoredJob != null) {
if (bestScoredJob.isNewRoute()) {
routes.add(bestScoredJob.getRoute());
}
insertJob(bestScoredJob.getJob(),bestScoredJob.getInsertionData(),bestScoredJob.getRoute());
insertJob(bestScoredJob.getJob(), bestScoredJob.getInsertionData(), bestScoredJob.getRoute());
jobs.remove(bestScoredJob.getJob());
}
for(Job bad : badJobList) {
for (Job bad : badJobList) {
jobs.remove(bad);
badJobs.add(bad);
}
@ -242,18 +124,29 @@ public class RegretInsertion extends AbstractInsertionStrategy {
return badJobs;
}
private ScoredJob nextJob(Collection<VehicleRoute> routes, List<Job> unassignedJobList, List<Job> badJobs) {
private VehicleRoute findRoute(Collection<VehicleRoute> routes, Job job) {
for(VehicleRoute r : routes){
if(r.getVehicle().getBreak() == job) return r;
}
return null;
}
private ScoredJob nextJob(Collection<VehicleRoute> routes, Collection<Job> unassignedJobList, List<Job> badJobs) {
ScoredJob bestScoredJob = null;
for (Job unassignedJob : unassignedJobList) {
ScoredJob scoredJob = getScoredJob(routes,unassignedJob,insertionCostsCalculator,scoringFunction);
if(scoredJob instanceof BadJob){
ScoredJob scoredJob = getScoredJob(routes, unassignedJob, insertionCostsCalculator, scoringFunction);
if (scoredJob instanceof ScoredJob.BadJob) {
badJobs.add(unassignedJob);
continue;
}
if(bestScoredJob == null) bestScoredJob = scoredJob;
else{
if(scoredJob.getScore() > bestScoredJob.getScore()){
if (bestScoredJob == null) bestScoredJob = scoredJob;
else {
if (scoredJob.getScore() > bestScoredJob.getScore()) {
bestScoredJob = scoredJob;
} else if (scoredJob.getScore() == bestScoredJob.getScore()) {
if (scoredJob.getJob().getId().compareTo(bestScoredJob.getJob().getId()) <= 0) {
bestScoredJob = scoredJob;
}
}
}
}
@ -298,31 +191,29 @@ public class RegretInsertion extends AbstractInsertionStrategy {
secondBest = iData;
}
}
if(best == null){
return new RegretInsertion.BadJob(unassignedJob);
if (best == null) {
return new ScoredJob.BadJob(unassignedJob);
}
double score = score(unassignedJob, best, secondBest, scoringFunction);
ScoredJob scoredJob;
if(bestRoute == emptyRoute){
if (bestRoute == emptyRoute) {
scoredJob = new ScoredJob(unassignedJob, score, best, bestRoute, true);
}
else scoredJob = new ScoredJob(unassignedJob, score, best, bestRoute, false);
} else scoredJob = new ScoredJob(unassignedJob, score, best, bestRoute, false);
return scoredJob;
}
static double score(Job unassignedJob, InsertionData best, InsertionData secondBest, ScoringFunction scoringFunction) {
if(best == null){
if (best == null) {
throw new IllegalStateException("cannot insert job " + unassignedJob.getId());
}
double score;
if(secondBest == null){ //either there is only one vehicle or there are more vehicles, but they cannot load unassignedJob
if (secondBest == null) { //either there is only one vehicle or there are more vehicles, but they cannot load unassignedJob
//if only one vehicle, I want the job to be inserted with min iCosts
//if there are more vehicles, I want this job to be prioritized since there are no alternatives
score = Integer.MAX_VALUE - best.getInsertionCost() + scoringFunction.score(best, unassignedJob);
}
else{
score = (secondBest.getInsertionCost()-best.getInsertionCost()) + scoringFunction.score(best, unassignedJob);
} else {
score = (secondBest.getInsertionCost() - best.getInsertionCost()) + scoringFunction.score(best, unassignedJob);
}
return score;
}

View file

@ -17,10 +17,8 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.RegretInsertion.DefaultScorer;
import jsprit.core.algorithm.recreate.RegretInsertion.ScoredJob;
import jsprit.core.algorithm.recreate.RegretInsertion.ScoringFunction;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Break;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import org.apache.logging.log4j.LogManager;
@ -28,24 +26,24 @@ import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.*;
/**
* Insertion based on regret approach.
*
* <p>Basically calculates the insertion cost of the firstBest and the secondBest alternative. The score is then calculated as difference
* between secondBest and firstBest, plus additional scoring variables that can defined in this.ScoringFunction.
* The idea is that if the cost of the secondBest alternative is way higher than the first best, it seems to be important to insert this
* customer immediatedly. If difference is not that high, it might not impact solution if this customer is inserted later.
*
* @author stefan schroeder
*
*/
* Insertion based on regret approach.
* <p/>
* <p>Basically calculates the insertion cost of the firstBest and the secondBest alternative. The score is then calculated as difference
* between secondBest and firstBest, plus additional scoring variables that can defined in this.ScoringFunction.
* The idea is that if the cost of the secondBest alternative is way higher than the first best, it seems to be important to insert this
* customer immediatedly. If difference is not that high, it might not impact solution if this customer is inserted later.
*
* @author stefan schroeder
*/
public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
private static Logger logger = LogManager.getLogger(RegretInsertionConcurrent.class);
private static Logger logger = LogManager.getLogger(RegretInsertionConcurrentFast.class);
private ScoringFunction scoringFunction;
@ -55,7 +53,7 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
/**
* Sets the scoring function.
*
* <p/>
* <p>By default, the this.TimeWindowScorer is used.
*
* @param scoringFunction to score
@ -75,33 +73,54 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
@Override
public String toString() {
return "[name=regretInsertion][additionalScorer="+scoringFunction+"]";
return "[name=regretInsertion][additionalScorer=" + scoringFunction + "]";
}
/**
* Runs insertion.
*
* <p/>
* <p>Before inserting a job, all unassigned jobs are scored according to its best- and secondBest-insertion plus additional scoring variables.
*
* @throws java.lang.RuntimeException if smth went wrong with thread execution
*/
@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
Iterator<Job> jobIterator = unassignedJobs.iterator();
while (jobIterator.hasNext()){
Job job = jobIterator.next();
if(job instanceof Break){
VehicleRoute route = findRoute(routes,job);
if(route == null){
badJobs.add(job);
}
else {
InsertionData iData = insertionCostsCalculator.getInsertionData(route, job, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, Double.MAX_VALUE);
if (iData instanceof InsertionData.NoInsertionFound) {
badJobs.add(job);
} else {
insertJob(job, iData, route);
}
}
jobIterator.remove();
}
}
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
while (!jobs.isEmpty()) {
List<Job> unassignedJobList = new ArrayList<Job>(jobs);
List<Job> badJobList = new ArrayList<Job>();
ScoredJob bestScoredJob = nextJob(routes, unassignedJobList, badJobList);
if(bestScoredJob != null){
if(bestScoredJob.isNewRoute()){
if (bestScoredJob != null) {
if (bestScoredJob.isNewRoute()) {
routes.add(bestScoredJob.getRoute());
}
insertJob(bestScoredJob.getJob(),bestScoredJob.getInsertionData(),bestScoredJob.getRoute());
insertJob(bestScoredJob.getJob(), bestScoredJob.getInsertionData(), bestScoredJob.getRoute());
jobs.remove(bestScoredJob.getJob());
}
for(Job j : badJobList) {
for (Job j : badJobList) {
jobs.remove(j);
badJobs.add(j);
}
@ -123,38 +142,39 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
});
}
try{
for(int i=0; i < unassignedJobList.size(); i++){
try {
for (int i = 0; i < unassignedJobList.size(); i++) {
Future<ScoredJob> fsj = completionService.take();
ScoredJob sJob = fsj.get();
if(sJob instanceof RegretInsertion.BadJob){
if (sJob instanceof ScoredJob.BadJob) {
badJobList.add(sJob.getJob());
continue;
}
if(bestScoredJob == null){
if (bestScoredJob == null) {
bestScoredJob = sJob;
}
else if(sJob.getScore() > bestScoredJob.getScore()){
} else if (sJob.getScore() > bestScoredJob.getScore()) {
bestScoredJob = sJob;
} else if (sJob.getScore() == bestScoredJob.getScore()) {
if (sJob.getJob().getId().compareTo(bestScoredJob.getJob().getId()) <= 0) {
bestScoredJob = sJob;
}
}
}
catch(InterruptedException e){
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
catch (ExecutionException e) {
e.printStackTrace();
logger.error(e.getCause().toString());
System.exit(1);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
return bestScoredJob;
}
private VehicleRoute findRoute(Collection<VehicleRoute> routes, Job job) {
for(VehicleRoute r : routes){
if(r.getVehicle().getBreak() == job) return r;
}
return null;
}
}

View file

@ -0,0 +1,196 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Break;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.VehicleFleetManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
/**
* Insertion based on regret approach.
* <p/>
* <p>Basically calculates the insertion cost of the firstBest and the secondBest alternative. The score is then calculated as difference
* between secondBest and firstBest, plus additional scoring variables that can defined in this.ScoringFunction.
* The idea is that if the cost of the secondBest alternative is way higher than the first best, it seems to be important to insert this
* customer immediatedly. If difference is not that high, it might not impact solution if this customer is inserted later.
*
* @author stefan schroeder
*/
public class RegretInsertionConcurrentFast extends AbstractInsertionStrategy {
private static Logger logger = LogManager.getLogger(RegretInsertionConcurrentFast.class);
private ScoringFunction scoringFunction;
private final JobInsertionCostsCalculator insertionCostsCalculator;
private final ExecutorService executor;
private VehicleFleetManager fleetManager;
private Set<String> initialVehicleIds;
private boolean switchAllowed = true;
/**
* Sets the scoring function.
* <p/>
* <p>By default, the this.TimeWindowScorer is used.
*
* @param scoringFunction to score
*/
public void setScoringFunction(ScoringFunction scoringFunction) {
this.scoringFunction = scoringFunction;
}
public RegretInsertionConcurrentFast(JobInsertionCostsCalculator jobInsertionCalculator, VehicleRoutingProblem vehicleRoutingProblem, ExecutorService executorService, VehicleFleetManager fleetManager) {
super(vehicleRoutingProblem);
this.scoringFunction = new DefaultScorer(vehicleRoutingProblem);
this.insertionCostsCalculator = jobInsertionCalculator;
this.vrp = vehicleRoutingProblem;
this.executor = executorService;
this.fleetManager = fleetManager;
this.initialVehicleIds = getInitialVehicleIds(vehicleRoutingProblem);
logger.debug("initialise " + this);
}
@Override
public String toString() {
return "[name=regretInsertion][additionalScorer=" + scoringFunction + "]";
}
public void setSwitchAllowed(boolean switchAllowed) {
this.switchAllowed = switchAllowed;
}
private Set<String> getInitialVehicleIds(VehicleRoutingProblem vehicleRoutingProblem) {
Set<String> ids = new HashSet<String>();
for(VehicleRoute r : vehicleRoutingProblem.getInitialVehicleRoutes()){
ids.add(r.getVehicle().getId());
}
return ids;
}
/**
* Runs insertion.
* <p/>
* <p>Before inserting a job, all unassigned jobs are scored according to its best- and secondBest-insertion plus additional scoring variables.
*
* @throws java.lang.RuntimeException if smth went wrong with thread execution
*/
@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
Iterator<Job> jobIterator = unassignedJobs.iterator();
while (jobIterator.hasNext()){
Job job = jobIterator.next();
if(job instanceof Break){
VehicleRoute route = InsertionDataUpdater.findRoute(routes, job);
if(route == null){
badJobs.add(job);
}
else {
InsertionData iData = insertionCostsCalculator.getInsertionData(route, job, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, Double.MAX_VALUE);
if (iData instanceof InsertionData.NoInsertionFound) {
badJobs.add(job);
} else {
insertJob(job, iData, route);
}
}
jobIterator.remove();
}
}
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
TreeSet<VersionedInsertionData>[] priorityQueues = new TreeSet[vrp.getJobs().values().size() + 2];
VehicleRoute lastModified = null;
boolean firstRun = true;
int updateRound = 0;
Map<VehicleRoute,Integer> updates = new HashMap<VehicleRoute, Integer>();
while (!jobs.isEmpty()) {
List<Job> unassignedJobList = new ArrayList<Job>(jobs);
List<Job> badJobList = new ArrayList<Job>();
if(!firstRun && lastModified == null) throw new IllegalStateException("ho. this must not be.");
if(firstRun){
firstRun = false;
updateInsertionData(priorityQueues, routes, unassignedJobList, updateRound);
for(VehicleRoute r : routes) updates.put(r,updateRound);
}
else{
updateInsertionData(priorityQueues, Arrays.asList(lastModified), unassignedJobList, updateRound);
updates.put(lastModified,updateRound);
}
updateRound++;
ScoredJob bestScoredJob = InsertionDataUpdater.getBest(switchAllowed,initialVehicleIds,fleetManager, insertionCostsCalculator, scoringFunction, priorityQueues, updates, unassignedJobList, badJobList);
if (bestScoredJob != null) {
if (bestScoredJob.isNewRoute()) {
routes.add(bestScoredJob.getRoute());
}
insertJob(bestScoredJob.getJob(), bestScoredJob.getInsertionData(), bestScoredJob.getRoute());
jobs.remove(bestScoredJob.getJob());
lastModified = bestScoredJob.getRoute();
}
else lastModified = null;
for (Job bad : badJobList) {
jobs.remove(bad);
badJobs.add(bad);
}
}
return badJobs;
}
private void updateInsertionData(final TreeSet<VersionedInsertionData>[] priorityQueues, final Collection<VehicleRoute> routes, List<Job> unassignedJobList, final int updateRound) {
List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
for (final Job unassignedJob : unassignedJobList) {
if(priorityQueues[unassignedJob.getIndex()] == null){
priorityQueues[unassignedJob.getIndex()] = new TreeSet<VersionedInsertionData>(InsertionDataUpdater.getComparator());
}
final TreeSet<VersionedInsertionData> priorityQueue = priorityQueues[unassignedJob.getIndex()];
tasks.add(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return InsertionDataUpdater.update(switchAllowed,initialVehicleIds,fleetManager, insertionCostsCalculator, priorityQueue, updateRound, unassignedJob, routes);
}
});
}
try {
List<Future<Boolean>> futures = executor.invokeAll(tasks);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,173 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Break;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.VehicleFleetManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.*;
/**
* Insertion based on regret approach.
* <p/>
* <p>Basically calculates the insertion cost of the firstBest and the secondBest alternative. The score is then calculated as difference
* between secondBest and firstBest, plus additional scoring variables that can defined in this.ScoringFunction.
* The idea is that if the cost of the secondBest alternative is way higher than the first best, it seems to be important to insert this
* customer immediatedly. If difference is not that high, it might not impact solution if this customer is inserted later.
*
* @author stefan schroeder
*/
public class RegretInsertionFast extends AbstractInsertionStrategy {
private static Logger logger = LogManager.getLogger(RegretInsertionFast.class);
private ScoringFunction scoringFunction;
private JobInsertionCostsCalculator insertionCostsCalculator;
private VehicleFleetManager fleetManager;
private Set<String> initialVehicleIds;
private boolean switchAllowed = true;
public RegretInsertionFast(JobInsertionCostsCalculator jobInsertionCalculator, VehicleRoutingProblem vehicleRoutingProblem, VehicleFleetManager fleetManager) {
super(vehicleRoutingProblem);
this.scoringFunction = new DefaultScorer(vehicleRoutingProblem);
this.insertionCostsCalculator = jobInsertionCalculator;
this.fleetManager = fleetManager;
this.vrp = vehicleRoutingProblem;
this.initialVehicleIds = getInitialVehicleIds(vehicleRoutingProblem);
logger.debug("initialise {}", this);
}
/**
* Sets the scoring function.
* <p/>
* <p>By default, the this.TimeWindowScorer is used.
*
* @param scoringFunction to score
*/
public void setScoringFunction(ScoringFunction scoringFunction) {
this.scoringFunction = scoringFunction;
}
public void setSwitchAllowed(boolean switchAllowed) {
this.switchAllowed = switchAllowed;
}
private Set<String> getInitialVehicleIds(VehicleRoutingProblem vehicleRoutingProblem) {
Set<String> ids = new HashSet<String>();
for(VehicleRoute r : vehicleRoutingProblem.getInitialVehicleRoutes()){
ids.add(r.getVehicle().getId());
}
return ids;
}
@Override
public String toString() {
return "[name=regretInsertion][additionalScorer=" + scoringFunction + "]";
}
/**
* Runs insertion.
* <p/>
* <p>Before inserting a job, all unassigned jobs are scored according to its best- and secondBest-insertion plus additional scoring variables.
*/
@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
Iterator<Job> jobIterator = unassignedJobs.iterator();
while (jobIterator.hasNext()){
Job job = jobIterator.next();
if(job instanceof Break){
VehicleRoute route = InsertionDataUpdater.findRoute(routes, job);
if(route == null){
badJobs.add(job);
}
else {
InsertionData iData = insertionCostsCalculator.getInsertionData(route, job, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, Double.MAX_VALUE);
if (iData instanceof InsertionData.NoInsertionFound) {
badJobs.add(job);
} else {
insertJob(job, iData, route);
}
}
jobIterator.remove();
}
}
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
TreeSet<VersionedInsertionData>[] priorityQueues = new TreeSet[vrp.getJobs().values().size() + 2];
VehicleRoute lastModified = null;
boolean firstRun = true;
int updateRound = 0;
Map<VehicleRoute,Integer> updates = new HashMap<VehicleRoute, Integer>();
while (!jobs.isEmpty()) {
List<Job> unassignedJobList = new ArrayList<Job>(jobs);
List<Job> badJobList = new ArrayList<Job>();
if(!firstRun && lastModified == null) throw new IllegalStateException("fooo");
if(firstRun){
firstRun = false;
updateInsertionData(priorityQueues, routes, unassignedJobList, updateRound);
for(VehicleRoute r : routes) updates.put(r,updateRound);
}
else{
updateInsertionData(priorityQueues, Arrays.asList(lastModified), unassignedJobList, updateRound);
updates.put(lastModified,updateRound);
}
updateRound++;
ScoredJob bestScoredJob = InsertionDataUpdater.getBest(switchAllowed,initialVehicleIds,fleetManager,insertionCostsCalculator,scoringFunction,priorityQueues,updates,unassignedJobList,badJobList);
if (bestScoredJob != null) {
if (bestScoredJob.isNewRoute()) {
routes.add(bestScoredJob.getRoute());
}
insertJob(bestScoredJob.getJob(), bestScoredJob.getInsertionData(), bestScoredJob.getRoute());
jobs.remove(bestScoredJob.getJob());
lastModified = bestScoredJob.getRoute();
}
else lastModified = null;
for (Job bad : badJobList) {
jobs.remove(bad);
badJobs.add(bad);
}
}
return badJobs;
}
private void updateInsertionData(TreeSet<VersionedInsertionData>[] priorityQueues, Collection<VehicleRoute> routes, List<Job> unassignedJobList, int updateRound) {
for (Job unassignedJob : unassignedJobList) {
if(priorityQueues[unassignedJob.getIndex()] == null){
priorityQueues[unassignedJob.getIndex()] = new TreeSet<VersionedInsertionData>(InsertionDataUpdater.getComparator());
}
InsertionDataUpdater.update(switchAllowed, initialVehicleIds,fleetManager,insertionCostsCalculator, priorityQueues[unassignedJob.getIndex()], updateRound, unassignedJob, routes);
}
}
}

View file

@ -1,4 +1,3 @@
/*******************************************************************************
* Copyright (C) 2014 Stefan Schroeder
*
@ -32,7 +31,7 @@ import java.util.ArrayList;
import java.util.List;
class RouteLevelActivityInsertionCostsEstimator implements ActivityInsertionCostsCalculator{
class RouteLevelActivityInsertionCostsEstimator implements ActivityInsertionCostsCalculator {
private VehicleRoutingActivityCosts activityCosts;
@ -52,28 +51,31 @@ class RouteLevelActivityInsertionCostsEstimator implements ActivityInsertionCost
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity nextAct, TourActivity newAct, double depTimeAtPrevAct) {
List<TourActivity> path = new ArrayList<TourActivity>();
path.add(prevAct); path.add(newAct); path.add(nextAct);
path.add(prevAct);
path.add(newAct);
path.add(nextAct);
int actIndex;
if(prevAct instanceof Start) actIndex = 0;
if (prevAct instanceof Start) actIndex = 0;
else actIndex = iFacts.getRoute().getTourActivities().getActivities().indexOf(nextAct);
if(nuOfActivities2LookForward > 0 && !(nextAct instanceof End)){ path.addAll(getForwardLookingPath(iFacts.getRoute(),actIndex)); }
if (nuOfActivities2LookForward > 0 && !(nextAct instanceof End)) {
path.addAll(getForwardLookingPath(iFacts.getRoute(), actIndex));
}
/*
* calculates the path costs with new vehicle, c(forwardPath,newVehicle).
*/
double forwardPathCost_newVehicle = auxilliaryPathCostCalculator.costOfPath(path, depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
return forwardPathCost_newVehicle - (actCostsOld(iFacts.getRoute(), path.get(path.size()-1)) - actCostsOld(iFacts.getRoute(), prevAct));
return forwardPathCost_newVehicle - (actCostsOld(iFacts.getRoute(), path.get(path.size() - 1)) - actCostsOld(iFacts.getRoute(), prevAct));
}
private double actCostsOld(VehicleRoute vehicleRoute, TourActivity act) {
Double cost_at_act;
if(act instanceof End){
if (act instanceof End) {
cost_at_act = stateManager.getRouteState(vehicleRoute, InternalStates.COSTS, Double.class);
}
else{
} else {
cost_at_act = stateManager.getActivityState(act, InternalStates.COSTS, Double.class);
}
if(cost_at_act == null) cost_at_act = 0.;
if (cost_at_act == null) cost_at_act = 0.;
return cost_at_act;
}
@ -81,12 +83,12 @@ class RouteLevelActivityInsertionCostsEstimator implements ActivityInsertionCost
List<TourActivity> forwardLookingPath = new ArrayList<TourActivity>();
int nuOfActsInPath = 0;
int index = actIndex + 1;
while(index < route.getTourActivities().getActivities().size() && nuOfActsInPath < nuOfActivities2LookForward){
while (index < route.getTourActivities().getActivities().size() && nuOfActsInPath < nuOfActivities2LookForward) {
forwardLookingPath.add(route.getTourActivities().getActivities().get(index));
index++;
nuOfActsInPath++;
}
if(nuOfActsInPath < nuOfActivities2LookForward){
if (nuOfActsInPath < nuOfActivities2LookForward) {
forwardLookingPath.add(route.getEnd());
}
return forwardLookingPath;

View file

@ -0,0 +1,57 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
/**
* Created by schroeder on 15/10/15.
*/
class ScoredJob {
static class BadJob extends ScoredJob {
BadJob(Job job) {
super(job, 0., null, null, false);
}
}
private Job job;
private double score;
private InsertionData insertionData;
private VehicleRoute route;
private boolean newRoute;
ScoredJob(Job job, double score, InsertionData insertionData, VehicleRoute route, boolean isNewRoute) {
this.job = job;
this.score = score;
this.insertionData = insertionData;
this.route = route;
this.newRoute = isNewRoute;
}
public boolean isNewRoute() {
return newRoute;
}
public Job getJob() {
return job;
}
public double getScore() {
return score;
}
public InsertionData getInsertionData() {
return insertionData;
}
public VehicleRoute getRoute() {
return route;
}
}

View file

@ -0,0 +1,12 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.job.Job;
/**
* Created by schroeder on 15/10/15.
*/
public interface ScoringFunction {
public double score(InsertionData best, Job job);
}

View file

@ -41,9 +41,8 @@ import java.util.Iterator;
* Calculator that calculates the best insertion position for a {@link Service}.
*
* @author schroeder
*
*/
final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
final class ServiceInsertionCalculator implements JobInsertionCostsCalculator {
private static final Logger logger = LogManager.getLogger(ServiceInsertionCalculator.class);
@ -72,10 +71,10 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
softRouteConstraint = constraintManager;
this.additionalTransportCostsCalculator = additionalTransportCostsCalculator;
additionalAccessEgressCalculator = new AdditionalAccessEgressCalculator(routingCosts);
logger.debug("initialise " + this);
logger.debug("initialise {}", this);
}
public void setJobActivityFactory(JobActivityFactory jobActivityFactory){
public void setJobActivityFactory(JobActivityFactory jobActivityFactory) {
this.activityFactory = jobActivityFactory;
}
@ -87,12 +86,11 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
/**
* Calculates the marginal cost of inserting job i locally. This is based on the
* assumption that cost changes can entirely covered by only looking at the predecessor i-1 and its successor i+1.
*
*/
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownCosts) {
JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
Service service = (Service)jobToInsert;
Service service = (Service) jobToInsert;
int insertionIndex = InsertionData.NO_INDEX;
TourActivity deliveryAct2Insert = activityFactory.createActivities(service).get(0);
@ -101,7 +99,7 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
/*
check hard constraints at route level
*/
if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
if (!hardRouteLevelConstraint.fulfilled(insertionContext)) {
return InsertionData.createEmptyInsertionData();
}
@ -168,7 +166,7 @@ final class ServiceInsertionCalculator implements JobInsertionCostsCalculator{
InsertionData insertionData = new InsertionData(bestCost, InsertionData.NO_INDEX, insertionIndex, newVehicle, newDriver);
deliveryAct2Insert.setTheoreticalEarliestOperationStartTime(bestTimeWindow.getStart());
deliveryAct2Insert.setTheoreticalLatestOperationStartTime(bestTimeWindow.getEnd());
insertionData.getEvents().add(new InsertActivity(currentRoute,newVehicle,deliveryAct2Insert,insertionIndex));
insertionData.getEvents().add(new InsertActivity(currentRoute, newVehicle, deliveryAct2Insert, insertionIndex));
insertionData.getEvents().add(new SwitchVehicle(currentRoute,newVehicle,newVehicleDepartureTime));
insertionData.setVehicleDepartureTime(newVehicleDepartureTime);
return insertionData;

View file

@ -45,9 +45,7 @@ import java.util.List;
import java.util.PriorityQueue;
final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsCalculator{
final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsCalculator {
private static final Logger logger = LogManager.getLogger(ServiceInsertionOnRouteLevelCalculator.class);
@ -68,20 +66,20 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
private ActivityInsertionCostsCalculator activityInsertionCostsCalculator;
private int nuOfActsForwardLooking = 0;
//
//
private int memorySize = 2;
private Start start;
private End end;
public void setJobActivityFactory(JobActivityFactory jobActivityFactory){
this.activityFactory=jobActivityFactory;
public void setJobActivityFactory(JobActivityFactory jobActivityFactory) {
this.activityFactory = jobActivityFactory;
}
public void setMemorySize(int memorySize) {
this.memorySize = memorySize;
logger.debug("set [solutionMemory="+memorySize+"]");
logger.debug("set [solutionMemory={}]", memorySize);
}
public ServiceInsertionOnRouteLevelCalculator(VehicleRoutingTransportCosts vehicleRoutingCosts, VehicleRoutingActivityCosts costFunc, ActivityInsertionCostsCalculator activityInsertionCostsCalculator, HardRouteConstraint hardRouteLevelConstraint, HardActivityConstraint hardActivityLevelConstraint) {
@ -92,22 +90,22 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
this.hardRouteLevelConstraint = hardRouteLevelConstraint;
this.hardActivityLevelConstraint = hardActivityLevelConstraint;
auxilliaryPathCostCalculator = new AuxilliaryCostCalculator(transportCosts, activityCosts);
logger.debug("initialise " + this);
logger.debug("initialise {}", this);
}
public void setStates(RouteAndActivityStateGetter stateManager){
public void setStates(RouteAndActivityStateGetter stateManager) {
this.stateManager = stateManager;
}
void setNuOfActsForwardLooking(int nOfActsForwardLooking) {
this.nuOfActsForwardLooking = nOfActsForwardLooking;
logger.debug("set [forwardLooking="+nOfActsForwardLooking+"]");
logger.debug("set [forwardLooking={}]", nOfActsForwardLooking);
}
@Override
public String toString() {
return "[name=calculatesServiceInsertionOnRouteLevel][solutionMemory="+memorySize+"][forwardLooking="+nuOfActsForwardLooking+"]";
return "[name=calculatesServiceInsertionOnRouteLevel][solutionMemory=" + memorySize + "][forwardLooking=" + nuOfActsForwardLooking + "]";
}
/**
@ -115,16 +113,16 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
* have local effects but affects the entire route).
* Calculation is conducted by two steps. In the first step, promising insertion positions are identified by appromiximating their
* marginal insertion cost. In the second step, marginal cost of the best M positions are calculated exactly.
*
*
*/
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double best_known_insertion_costs) {
if(jobToInsert == null) throw new IllegalStateException("job is null. cannot calculate the insertion of a null-job.");
if(newVehicle == null || newVehicle instanceof NoVehicle) throw new IllegalStateException("no vehicle given. set para vehicle!");
if (jobToInsert == null)
throw new IllegalStateException("job is null. cannot calculate the insertion of a null-job.");
if (newVehicle == null || newVehicle instanceof NoVehicle)
throw new IllegalStateException("no vehicle given. set para vehicle!");
JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
if (!hardRouteLevelConstraint.fulfilled(insertionContext)) {
return InsertionData.createEmptyInsertionData();
}
@ -140,7 +138,7 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
TourActivities tour = currentRoute.getTourActivities();
double best_insertion_costs = best_known_insertion_costs;
Service service = (Service)jobToInsert;
Service service = (Service) jobToInsert;
/**
@ -163,9 +161,9 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
* j=nextAct
* k=serviceAct2Insert
*/
for(TourActivity nextAct : tour.getActivities()){
for (TourActivity nextAct : tour.getActivities()) {
ConstraintsStatus hardActivityConstraintsStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, serviceAct2Insert, nextAct, prevActDepTime_newVehicle);
if(hardActivityConstraintsStatus.equals(ConstraintsStatus.FULFILLED)){
if (hardActivityConstraintsStatus.equals(ConstraintsStatus.FULFILLED)) {
/**
* builds a path on this route forwardPath={i,k,j,j+1,j+2,...,j+nuOfActsForwardLooking}
*/
@ -174,16 +172,15 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
/**
* insertion_cost_approximation = c({0,1,...,i},newVehicle) + c({i,k,j,j+1,j+2,...,j+nuOfActsForwardLooking},newVehicle) - c({0,1,...,i,j,j+1,...,j+nuOfActsForwardLooking},oldVehicle)
*/
double insertion_cost_approximation = sumOf_prevCosts_newVehicle - sumOf_prevCosts_oldVehicle(currentRoute,prevAct) + actInsertionCosts;
double insertion_cost_approximation = sumOf_prevCosts_newVehicle - sumOf_prevCosts_oldVehicle(currentRoute, prevAct) + actInsertionCosts;
/**
* memorize it in insertion-queue
*/
if(insertion_cost_approximation < best_known_insertion_costs){
if (insertion_cost_approximation < best_known_insertion_costs) {
bestInsertionsQueue.add(new InsertionData(insertion_cost_approximation, InsertionData.NO_INDEX, actIndex, newVehicle, newDriver));
}
}
else if(hardActivityConstraintsStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)){
} else if (hardActivityConstraintsStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)) {
loopBroken = true;
break;
}
@ -216,21 +213,21 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
actIndex++;
}
if(!loopBroken){
if (!loopBroken) {
End nextAct = end;
ConstraintsStatus hardActivityConstraintsStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, serviceAct2Insert, nextAct, prevActDepTime_newVehicle);
if(hardActivityConstraintsStatus.equals(ConstraintsStatus.FULFILLED)){
if (hardActivityConstraintsStatus.equals(ConstraintsStatus.FULFILLED)) {
double actInsertionCosts = activityInsertionCostsCalculator.getCosts(insertionContext, prevAct, nextAct, serviceAct2Insert, prevActDepTime_newVehicle);
/**
* insertion_cost_approximation = c({0,1,...,i},newVehicle) + c({i,k,j,j+1,j+2,...,j+nuOfActsForwardLooking},newVehicle) - c({0,1,...,i,j,j+1,...,j+nuOfActsForwardLooking},oldVehicle)
*/
double insertion_cost_approximation = sumOf_prevCosts_newVehicle - sumOf_prevCosts_oldVehicle(currentRoute,prevAct) + actInsertionCosts;
double insertion_cost_approximation = sumOf_prevCosts_newVehicle - sumOf_prevCosts_oldVehicle(currentRoute, prevAct) + actInsertionCosts;
/**
* memorize it in insertion-queue
*/
if(insertion_cost_approximation < best_known_insertion_costs){
if (insertion_cost_approximation < best_known_insertion_costs) {
bestInsertionsQueue.add(new InsertionData(insertion_cost_approximation, InsertionData.NO_INDEX, actIndex, newVehicle, newDriver));
}
@ -244,18 +241,17 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
*
*/
if(memorySize==0){ // return bestInsertion
if (memorySize == 0) { // return bestInsertion
InsertionData insertion = bestInsertionsQueue.poll();
if(insertion != null){
if (insertion != null) {
best_insertion_index = insertion.getDeliveryInsertionIndex();
best_insertion_costs = insertion.getInsertionCost();
}
}
else{
} else {
for(int i=0;i<memorySize;i++){
for (int i = 0; i < memorySize; i++) {
InsertionData data = bestInsertionsQueue.poll();
if(data == null){
if (data == null) {
continue;
}
/**
@ -265,46 +261,44 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
wholeTour.add(start);
wholeTour.addAll(currentRoute.getTourActivities().getActivities());
wholeTour.add(end);
wholeTour.add(data.getDeliveryInsertionIndex()+1, serviceAct2Insert);
wholeTour.add(data.getDeliveryInsertionIndex() + 1, serviceAct2Insert);
/**
* compute cost-diff of tour with and without new activity --> insertion_costs
*/
Double currentRouteCosts = stateManager.getRouteState(currentRoute, InternalStates.COSTS, Double.class);
if(currentRouteCosts == null) currentRouteCosts = 0.;
if (currentRouteCosts == null) currentRouteCosts = 0.;
double insertion_costs = auxilliaryPathCostCalculator.costOfPath(wholeTour, start.getEndTime(), newDriver, newVehicle) - currentRouteCosts;
/**
* if better than best known, make it the best known
*/
if(insertion_costs < best_insertion_costs){
if (insertion_costs < best_insertion_costs) {
best_insertion_index = data.getDeliveryInsertionIndex();
best_insertion_costs = insertion_costs;
}
}
}
if(best_insertion_index == InsertionData.NO_INDEX) return InsertionData.createEmptyInsertionData();
if (best_insertion_index == InsertionData.NO_INDEX) return InsertionData.createEmptyInsertionData();
InsertionData insertionData = new InsertionData(best_insertion_costs, InsertionData.NO_INDEX, best_insertion_index, newVehicle, newDriver);
insertionData.setVehicleDepartureTime(start.getEndTime());
return insertionData;
}
private void initialiseStartAndEnd(final Vehicle newVehicle, double newVehicleDepartureTime) {
if(start == null){
if (start == null) {
start = new Start(newVehicle.getStartLocation(), newVehicle.getEarliestDeparture(), Double.MAX_VALUE);
start.setEndTime(newVehicleDepartureTime);
}
else{
} else {
start.setLocation(Location.newInstance(newVehicle.getStartLocation().getId()));
start.setTheoreticalEarliestOperationStartTime(newVehicle.getEarliestDeparture());
start.setTheoreticalLatestOperationStartTime(Double.MAX_VALUE);
start.setEndTime(newVehicleDepartureTime);
}
if(end == null){
if (end == null) {
end = new End(newVehicle.getEndLocation(), 0.0, newVehicle.getLatestArrival());
}
else{
} else {
end.setLocation(Location.newInstance(newVehicle.getEndLocation().getId()));
end.setTheoreticalEarliestOperationStartTime(0.0);
end.setTheoreticalLatestOperationStartTime(newVehicle.getLatestArrival());
@ -313,11 +307,10 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
private double sumOf_prevCosts_oldVehicle(VehicleRoute vehicleRoute, TourActivity act) {
Double prevCost;
if(act instanceof End){
prevCost = stateManager.getRouteState(vehicleRoute, InternalStates.COSTS,Double.class);
}
else prevCost = stateManager.getActivityState(act, InternalStates.COSTS,Double.class);
if(prevCost == null) prevCost = 0.;
if (act instanceof End) {
prevCost = stateManager.getRouteState(vehicleRoute, InternalStates.COSTS, Double.class);
} else prevCost = stateManager.getActivityState(act, InternalStates.COSTS, Double.class);
if (prevCost == null) prevCost = 0.;
return prevCost;
}
@ -326,10 +319,9 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
@Override
public int compare(InsertionData o1, InsertionData o2) {
if(o1.getInsertionCost() < o2.getInsertionCost()){
if (o1.getInsertionCost() < o2.getInsertionCost()) {
return -1;
}
else {
} else {
return 1;
}

View file

@ -37,9 +37,7 @@ import org.apache.logging.log4j.Logger;
import java.util.List;
final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator {
private static final Logger logger = LogManager.getLogger(ShipmentInsertionCalculator.class);
@ -68,10 +66,10 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
this.softRouteConstraint = constraintManager;
this.transportCosts = routingCosts;
additionalAccessEgressCalculator = new AdditionalAccessEgressCalculator(routingCosts);
logger.debug("initialise " + this);
logger.debug("initialise {}", this);
}
public void setJobActivityFactory(JobActivityFactory activityFactory){
public void setJobActivityFactory(JobActivityFactory activityFactory) {
this.activityFactory = activityFactory;
}
@ -83,12 +81,11 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
/**
* Calculates the marginal cost of inserting job i locally. This is based on the
* assumption that cost changes can entirely covered by only looking at the predecessor i-1 and its successor i+1.
*
*/
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownCosts) {
JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
Shipment shipment = (Shipment)jobToInsert;
Shipment shipment = (Shipment) jobToInsert;
TourActivity pickupShipment = activityFactory.createActivities(shipment).get(0);
TourActivity deliverShipment = activityFactory.createActivities(shipment).get(1);
insertionContext.getAssociatedActivities().add(pickupShipment);
@ -97,7 +94,7 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
/*
check hard route constraints
*/
if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
if (!hardRouteLevelConstraint.fulfilled(insertionContext)) {
return InsertionData.createEmptyInsertionData();
}
/*
@ -127,29 +124,27 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
//pickupShipmentLoop
List<TourActivity> activities = currentRoute.getTourActivities().getActivities();
while(!tourEnd){
while (!tourEnd) {
TourActivity nextAct;
if(i < activities.size()){
if (i < activities.size()) {
nextAct = activities.get(i);
}
else{
} else {
nextAct = end;
tourEnd = true;
}
// logger.info("activity: " + i + ", act-size: " + activities.size());
// logger.info("activity: {}, act-size: {}", i, activities.size());
ConstraintsStatus pickupShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, pickupShipment, nextAct, prevActEndTime);
if(pickupShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED)){
if (pickupShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED)) {
double nextActArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevActEndTime, newDriver, newVehicle);
prevActEndTime = CalculationUtils.getActivityEndTime(nextActArrTime, nextAct);
prevAct = nextAct;
i++;
continue;
}
else if(pickupShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)){
} else if (pickupShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)) {
break;
}
double additionalPickupICosts = softActivityConstraint.getCosts(insertionContext, prevAct, pickupShipment, nextAct, prevActEndTime);
double pickupAIC = calculate(insertionContext,prevAct,pickupShipment, nextAct,prevActEndTime);
double pickupAIC = calculate(insertionContext, prevAct, pickupShipment, nextAct, prevActEndTime);
TourActivity prevAct_deliveryLoop = pickupShipment;
double shipmentPickupArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocation(), pickupShipment.getLocation(), prevActEndTime, newDriver, newVehicle);
@ -168,31 +163,29 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
//deliverShipmentLoop
int j = i;
boolean tourEnd_deliveryLoop = false;
while(!tourEnd_deliveryLoop){
while (!tourEnd_deliveryLoop) {
// for(int j=i;j<activities.size();j++){
TourActivity nextAct_deliveryLoop;
if(j < activities.size()) {
if (j < activities.size()) {
nextAct_deliveryLoop = activities.get(j);
}
else{
} else {
nextAct_deliveryLoop = end;
tourEnd_deliveryLoop = true;
}
ConstraintsStatus deliverShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct_deliveryLoop, deliverShipment, nextAct_deliveryLoop, prevActEndTime_deliveryLoop);
if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
if (deliverShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)) {
double additionalDeliveryICosts = softActivityConstraint.getCosts(insertionContext, prevAct_deliveryLoop, deliverShipment, nextAct_deliveryLoop, prevActEndTime_deliveryLoop);
double deliveryAIC = calculate(insertionContext,prevAct_deliveryLoop,deliverShipment, nextAct_deliveryLoop,prevActEndTime_deliveryLoop);
double deliveryAIC = calculate(insertionContext, prevAct_deliveryLoop, deliverShipment, nextAct_deliveryLoop, prevActEndTime_deliveryLoop);
double totalActivityInsertionCosts = pickupAIC + deliveryAIC
+ additionalICostsAtRouteLevel + additionalPickupICosts + additionalDeliveryICosts;
if(totalActivityInsertionCosts < bestCost){
if (totalActivityInsertionCosts < bestCost) {
bestCost = totalActivityInsertionCosts;
pickupInsertionIndex = i;
deliveryInsertionIndex = j;
}
}
else if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)){
} else if (deliverShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)) {
break;
}
//update prevAct and endTime
@ -207,14 +200,14 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
prevAct = nextAct;
i++;
}
if(pickupInsertionIndex == InsertionData.NO_INDEX) {
if (pickupInsertionIndex == InsertionData.NO_INDEX) {
return InsertionData.createEmptyInsertionData();
}
InsertionData insertionData = new InsertionData(bestCost, pickupInsertionIndex, deliveryInsertionIndex, newVehicle, newDriver);
insertionData.setVehicleDepartureTime(newVehicleDepartureTime);
insertionData.getEvents().add(new InsertActivity(currentRoute,newVehicle,deliverShipment,deliveryInsertionIndex));
insertionData.getEvents().add(new InsertActivity(currentRoute,newVehicle,pickupShipment,pickupInsertionIndex));
insertionData.getEvents().add(new SwitchVehicle(currentRoute,newVehicle,newVehicleDepartureTime));
insertionData.getEvents().add(new InsertActivity(currentRoute, newVehicle, deliverShipment, deliveryInsertionIndex));
insertionData.getEvents().add(new InsertActivity(currentRoute, newVehicle, pickupShipment, pickupInsertionIndex));
insertionData.getEvents().add(new SwitchVehicle(currentRoute, newVehicle, newVehicleDepartureTime));
return insertionData;
}

View file

@ -1,15 +1,33 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.job.Break;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Created by schroeder on 19/05/15.
*/
class SwitchVehicleListener implements EventListener{
class SwitchVehicleListener implements EventListener {
private static final Logger logger = LogManager.getLogger();
@Override
public void inform(Event event) {
if(event instanceof SwitchVehicle){
if (event instanceof SwitchVehicle) {
SwitchVehicle switchVehicle = (SwitchVehicle) event;
switchVehicle.getRoute().setVehicleAndDepartureTime(switchVehicle.getVehicle(),((SwitchVehicle) event).getDepartureTime());
if (vehiclesDifferent((SwitchVehicle) event)) {
logger.trace("switch vehicle ({} to {})",((SwitchVehicle) event).getRoute().getVehicle().getId(),((SwitchVehicle) event).getVehicle().getId());
Break aBreak = ((SwitchVehicle) event).getRoute().getVehicle().getBreak();
if (aBreak != null) {
boolean removed = ((SwitchVehicle) event).getRoute().getTourActivities().removeJob(aBreak);
if (removed) logger.trace("remove {}",aBreak.getId());
}
}
switchVehicle.getRoute().setVehicleAndDepartureTime(switchVehicle.getVehicle(), ((SwitchVehicle) event).getDepartureTime());
}
}
private boolean vehiclesDifferent(SwitchVehicle event) {
return !event.getRoute().getVehicle().getId().equals(event.getVehicle().getId());
}
}

View file

@ -23,7 +23,7 @@ import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.util.CalculationUtils;
public class VariableTransportCostCalculator implements SoftActivityConstraint{
public class VariableTransportCostCalculator implements SoftActivityConstraint {
private VehicleRoutingTransportCosts routingCosts;
@ -33,7 +33,7 @@ public class VariableTransportCostCalculator implements SoftActivityConstraint{
}
@Override
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct,TourActivity newAct, TourActivity nextAct, double depTimeAtPrevAct) {
public double getCosts(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double depTimeAtPrevAct) {
double tp_costs_prevAct_newAct = routingCosts.getTransportCost(prevAct.getLocation(), newAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
double tp_time_prevAct_newAct = routingCosts.getTransportTime(prevAct.getLocation(), newAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
@ -41,8 +41,8 @@ public class VariableTransportCostCalculator implements SoftActivityConstraint{
double newAct_endTime = CalculationUtils.getActivityEndTime(newAct_arrTime, newAct);
//open routes
if(nextAct instanceof End){
if(!iFacts.getNewVehicle().isReturnToDepot()){
if (nextAct instanceof End) {
if (!iFacts.getNewVehicle().isReturnToDepot()) {
return tp_costs_prevAct_newAct;
}
}
@ -51,11 +51,10 @@ public class VariableTransportCostCalculator implements SoftActivityConstraint{
double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct;
double oldCosts;
if(iFacts.getRoute().isEmpty()){
if (iFacts.getRoute().isEmpty()) {
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
oldCosts = tp_costs_prevAct_nextAct;
}
else{
} else {
double tp_costs_prevAct_nextAct = routingCosts.getTransportCost(prevAct.getLocation(), nextAct.getLocation(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle());
oldCosts = tp_costs_prevAct_nextAct;
}

View file

@ -22,11 +22,11 @@ import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleFleetManager;
public class VehicleSwitched implements VehicleSwitchedListener{
public class VehicleSwitched implements VehicleSwitchedListener {
private VehicleFleetManager fleetManager;
public VehicleSwitched(VehicleFleetManager fleetManager){
public VehicleSwitched(VehicleFleetManager fleetManager) {
this.fleetManager = fleetManager;
}

View file

@ -33,7 +33,7 @@ import java.util.HashSet;
import java.util.Set;
final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCostsCalculator{
final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCostsCalculator {
private Logger logger = LogManager.getLogger(VehicleTypeDependentJobInsertionCalculator.class);
@ -47,11 +47,10 @@ final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCo
/**
* true if a vehicle(-type) is allowed to take over the whole route that was previously served by another vehicle
*
* <p/>
* <p>vehicleSwitch allowed makes sense if fleet consists of vehicles with different capacities such that one
* can start with a small vehicle, but as the number of customers grows bigger vehicles can be operated, i.e.
* bigger vehicles can take over the route that was previously served by a small vehicle.
*
*/
private boolean vehicleSwitchAllowed = false;
@ -65,7 +64,7 @@ final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCo
private void getInitialVehicleIds() {
Collection<VehicleRoute> initialVehicleRoutes = vrp.getInitialVehicleRoutes();
for(VehicleRoute initialRoute : initialVehicleRoutes){
for (VehicleRoute initialRoute : initialVehicleRoutes) {
initialVehicleIds.add(initialRoute.getVehicle().getId());
}
}
@ -94,29 +93,31 @@ final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCo
}
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle vehicle, double newVehicleDepartureTime, final Driver driver, final double bestKnownCost) {
if(vehicle != null){
return insertionCalculator.getInsertionData(currentRoute, jobToInsert, vehicle, newVehicleDepartureTime, driver, bestKnownCost);
}
Vehicle selectedVehicle = currentRoute.getVehicle();
Driver selectedDriver = currentRoute.getDriver();
InsertionData bestIData = InsertionData.createEmptyInsertionData();
double bestKnownCost_ = bestKnownCost;
Collection<Vehicle> relevantVehicles = new ArrayList<Vehicle>();
if(!(selectedVehicle instanceof NoVehicle)) {
if (!(selectedVehicle instanceof NoVehicle)) {
relevantVehicles.add(selectedVehicle);
if(vehicleSwitchAllowed && !isVehicleWithInitialRoute(selectedVehicle)){
if (vehicleSwitchAllowed && !isVehicleWithInitialRoute(selectedVehicle)) {
relevantVehicles.addAll(fleetManager.getAvailableVehicles(selectedVehicle));
}
}
else{ //if no vehicle has been assigned, i.e. it is an empty route
} else { //if no vehicle has been assigned, i.e. it is an empty route
relevantVehicles.addAll(fleetManager.getAvailableVehicles());
}
for(Vehicle v : relevantVehicles){
for (Vehicle v : relevantVehicles) {
double depTime;
if(v == selectedVehicle) depTime = currentRoute.getDepartureTime();
if (v == selectedVehicle) depTime = currentRoute.getDepartureTime();
else depTime = v.getEarliestDeparture();
InsertionData iData = insertionCalculator.getInsertionData(currentRoute, jobToInsert, v, depTime, selectedDriver, bestKnownCost_);
if(iData instanceof NoInsertionFound) {
if (iData instanceof NoInsertionFound) {
continue;
}
if(iData.getInsertionCost() < bestKnownCost_){
if (iData.getInsertionCost() < bestKnownCost_) {
bestIData = iData;
bestKnownCost_ = iData.getInsertionCost();
}
@ -124,6 +125,10 @@ final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCo
return bestIData;
}
VehicleFleetManager getFleetManager(){
return fleetManager;
}
private boolean isVehicleWithInitialRoute(Vehicle selectedVehicle) {
return initialVehicleIds.contains(selectedVehicle.getId());
}

View file

@ -0,0 +1,33 @@
package jsprit.core.algorithm.recreate;
import jsprit.core.problem.solution.route.VehicleRoute;
/**
* Created by schroeder on 15/10/15.
*/
class VersionedInsertionData {
private InsertionData iData;
private VehicleRoute route;
private int version;
public VersionedInsertionData(InsertionData iData, int version, VehicleRoute route) {
this.iData = iData;
this.version = version;
this.route = route;
}
public InsertionData getiData() {
return iData;
}
public int getVersion() {
return version;
}
public VehicleRoute getRoute() {
return route;
}
}

View file

@ -20,7 +20,7 @@ import jsprit.core.algorithm.recreate.InsertionData;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
public interface BeforeJobInsertionListener extends InsertionListener{
public interface BeforeJobInsertionListener extends InsertionListener {
public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route);

View file

@ -21,7 +21,6 @@ import jsprit.core.problem.solution.route.VehicleRoute;
import java.util.Collection;
public interface InsertionEndsListener extends InsertionListener {
public void informInsertionEnds(Collection<VehicleRoute> vehicleRoutes);

View file

@ -19,11 +19,7 @@ package jsprit.core.algorithm.recreate.listener;
import jsprit.core.algorithm.listener.SearchStrategyModuleListener;
public interface InsertionListener extends SearchStrategyModuleListener{
public interface InsertionListener extends SearchStrategyModuleListener {
}

Some files were not shown because too many files have changed in this diff Show more