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

improve visualization

This commit is contained in:
oblonski 2013-12-11 08:58:11 +01:00
parent 0717f5dd66
commit f0c6d15852
9 changed files with 294 additions and 210 deletions

View file

@ -1,199 +0,0 @@
package jsprit.analysis.toolbox;
import java.util.HashMap;
import java.util.Map;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.util.Coordinate;
import jsprit.core.util.Locations;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.DefaultGraph;
import org.graphstream.ui.swingViewer.Viewer;
public class GraphStream {
protected static String styleSheet =
"node {" +
" size: 10px, 10px;" +
" fill-color: orange;" +
" text-alignment: at-right;" +
" stroke-mode: plain;" +
" stroke-color: black;" +
"}" +
"node.depot {" +
" fill-color: red;" +
" size: 10px, 10px;" +
" shape: box;" +
"}" +
"edge {" +
" fill-color: black;" +
"}" ;
public static void display(VehicleRoutingProblem vrp, int renderDelay_in_ms) {
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph g = new DefaultGraph("g");
g.addAttribute("ui.quality");
g.addAttribute("ui.antialias");
g.addAttribute("ui.stylesheet", styleSheet);
Viewer viewer = g.display();
viewer.disableAutoLayout();
for(Vehicle vehicle : vrp.getVehicles()){
renderVehicle(g, vehicle);
sleep(renderDelay_in_ms);
}
for(Job j : vrp.getJobs().values()){
sleep(renderDelay_in_ms);
if(j instanceof Service){
renderService(g, j);
}
}
}
private static void sleep(int renderDelay_in_ms) {
try {
Thread.sleep(renderDelay_in_ms);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
private static void renderService(Graph g, Job j) {
Node n = g.addNode(j.getId());
n.addAttribute("ui.label", j.getId());
n.addAttribute("x", ((Service) j).getCoord().getX());
n.addAttribute("y", ((Service) j).getCoord().getY());
}
private static void renderVehicle(Graph g, Vehicle vehicle) {
Node n = g.addNode(vehicle.getId());
n.addAttribute("ui.label", "depot");
n.addAttribute("x", vehicle.getCoord().getX());
n.addAttribute("y", vehicle.getCoord().getY());
n.setAttribute("ui.class", "depot");
}
public static void display(VehicleRoutingProblem vrp) {
display(vrp,0);
}
public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, int renderDelay_in_ms, boolean enableAutoLayout) {
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph g = new DefaultGraph("g");
g.addAttribute("ui.quality");
g.addAttribute("ui.antialias");
g.addAttribute("ui.stylesheet", styleSheet);
Viewer viewer = g.display();
if(!enableAutoLayout) viewer.disableAutoLayout();
for(Vehicle vehicle : vrp.getVehicles()){
renderVehicle(g,vehicle);
sleep(renderDelay_in_ms);
}
for(Job j : vrp.getJobs().values()){
if(j instanceof Service){
renderService(g,(Service)j);
}
sleep(renderDelay_in_ms);
}
int routeId = 1;
for(VehicleRoute route : solution.getRoutes()){
renderRoute(g,route,routeId,renderDelay_in_ms);
sleep(renderDelay_in_ms);
routeId++;
}
}
public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, int renderDelay_in_ms) {
display(vrp,solution,renderDelay_in_ms,false);
}
public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution) {
display(vrp,solution,0,false);
}
private static Locations retrieveLocations(VehicleRoutingProblem vrp) throws NoLocationFoundException {
final Map<String, Coordinate> locs = new HashMap<String, Coordinate>();
for(Vehicle v : vrp.getVehicles()){
String locationId = v.getLocationId();
if(locationId == null) throw new NoLocationFoundException();
Coordinate coord = v.getCoord();
if(coord == null) throw new NoLocationFoundException();
locs.put(locationId, coord);
}
for(Job j : vrp.getJobs().values()){
if(j instanceof Service){
String locationId = ((Service) j).getLocationId();
if(locationId == null) throw new NoLocationFoundException();
Coordinate coord = ((Service) j).getCoord();
if(coord == null) throw new NoLocationFoundException();
locs.put(locationId, coord);
}
else if(j instanceof Shipment){
{
String locationId = ((Shipment) j).getPickupLocation();
if(locationId == null) throw new NoLocationFoundException();
Coordinate coord = ((Shipment) j).getPickupCoord();
if(coord == null) throw new NoLocationFoundException();
locs.put(locationId, coord);
}
{
String locationId = ((Shipment) j).getDeliveryLocation();
if(locationId == null) throw new NoLocationFoundException();
Coordinate coord = ((Shipment) j).getDeliveryCoord();
if(coord == null) throw new NoLocationFoundException();
locs.put(locationId, coord);
}
}
else{
throw new IllegalStateException("job is neither a service nor a shipment. this is not supported yet.");
}
}
return new Locations() {
@Override
public Coordinate getCoord(String id) {
return locs.get(id);
}
};
}
private static void renderRoute(Graph g, VehicleRoute route, int routeId, int renderDelay_in_ms) {
int vehicle_edgeId = 1;
String prevIdentifier = route.getVehicle().getId();
for(TourActivity act : route.getActivities()){
String currIdentifier = ((JobActivity)act).getJob().getId();
g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, currIdentifier, true);
prevIdentifier = currIdentifier;
vehicle_edgeId++;
sleep(renderDelay_in_ms);
}
if(route.getVehicle().isReturnToDepot()){
g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, route.getVehicle().getId(), true);
}
}
private static String makeEdgeId(int routeId, int vehicle_edgeId) {
return Integer.valueOf(routeId).toString() + "." + Integer.valueOf(vehicle_edgeId).toString();
}
}

View file

@ -0,0 +1,279 @@
package jsprit.analysis.toolbox;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity;
import jsprit.core.problem.vehicle.Vehicle;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.DefaultGraph;
import org.graphstream.ui.swingViewer.Viewer;
public class GraphStreamViewer {
protected static String styleSheet =
"node {" +
" size: 7px, 7px;" +
" fill-color: orange;" +
" text-alignment: at-right;" +
" stroke-mode: plain;" +
" stroke-color: black;" +
"}" +
"node.pickup {" +
" fill-color: green;" +
"}" +
"node.delivery {" +
" fill-color: blue;" +
"}" +
"node.depot {" +
" fill-color: red;" +
" size: 10px, 10px;" +
" shape: box;" +
"}" +
"edge {" +
" fill-color: black;" +
" arrow-size: 6px,3px;" +
"}" +
"edge.shipment {" +
" fill-color: blue;" +
" arrow-size: 6px,3px;" +
"}" ;
private static class View {
private long renderDelay = 0;
private Label label = Label.NO_LABEL;
private boolean renderShipments = false;
private boolean enableAutoDisplay = false;
private VehicleRoutingProblem vrp;
private VehicleRoutingProblemSolution solution;
public View(VehicleRoutingProblem vrp) {
super();
this.vrp = vrp;
}
public View(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution) {
super();
this.vrp = vrp;
this.solution = solution;
}
public void setEnableAutoDisplay(boolean enableAutoDisplay) {
this.enableAutoDisplay = enableAutoDisplay;
}
public void setRenderDelay(long renderDelay) {
this.renderDelay = renderDelay;
}
public void setLabel(Label label) {
this.label = label;
}
public void setRenderShipments(boolean renderShipments) {
this.renderShipments = renderShipments;
}
}
private static void display(View view){
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph g = new DefaultGraph("g");
g.addAttribute("ui.quality");
g.addAttribute("ui.antialias");
g.addAttribute("ui.stylesheet", styleSheet);
Viewer viewer = g.display();
if(!view.enableAutoDisplay) viewer.disableAutoLayout();
for(Vehicle vehicle : view.vrp.getVehicles()){
renderVehicle(g,vehicle,view.label);
sleep(view.renderDelay);
}
for(Job j : view.vrp.getJobs().values()){
if(j instanceof Service){
renderService(g,(Service)j,view.label);
}
else if(j instanceof Shipment){
renderShipment(g,(Shipment)j,view.label,view.renderShipments);
}
sleep(view.renderDelay);
}
if(view.solution != null){
int routeId = 1;
for(VehicleRoute route : view.solution.getRoutes()){
renderRoute(g,route,routeId,view.renderDelay);
sleep(view.renderDelay);
routeId++;
}
}
}
public static void display(VehicleRoutingProblem vrp, int renderDelay_in_ms) {
View builder = new View(vrp);
builder.setRenderDelay(renderDelay_in_ms);
display(builder);
}
private static void renderShipment(Graph g, Shipment shipment, Label label, boolean renderShipments) {
Node n1 = g.addNode(makeId(shipment.getId(),shipment.getPickupLocation()));
if(label.equals(Label.ID)) n1.addAttribute("ui.label", shipment.getId());
n1.addAttribute("x", shipment.getPickupCoord().getX());
n1.addAttribute("y", shipment.getPickupCoord().getY());
n1.setAttribute("ui.class", "pickup");
Node n2 = g.addNode(makeId(shipment.getId(),shipment.getDeliveryLocation()));
if(label.equals(Label.ID)) n2.addAttribute("ui.label", shipment.getId());
n2.addAttribute("x", shipment.getDeliveryCoord().getX());
n2.addAttribute("y", shipment.getDeliveryCoord().getY());
n2.setAttribute("ui.class", "delivery");
if(renderShipments){
}
}
private static void sleep(long renderDelay_in_ms2) {
try {
Thread.sleep(renderDelay_in_ms2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
private static void renderService(Graph g, Service service, Label label) {
Node n = g.addNode(makeId(service.getId(),service.getLocationId()));
if(label.equals(Label.ID)) n.addAttribute("ui.label", service.getId());
n.addAttribute("x", service.getCoord().getX());
n.addAttribute("y", service.getCoord().getY());
if(service.getType().equals("pickup")) n.setAttribute("ui.class", "pickup");
if(service.getType().equals("delivery")) n.setAttribute("ui.class", "delivery");
}
private static String makeId(String id, String locationId) {
return new StringBuffer().append(id).append("_").append(locationId).toString();
}
private static void renderVehicle(Graph g, Vehicle vehicle, Label label) {
Node n = g.addNode(makeId(vehicle.getId(),vehicle.getLocationId()));
if(label.equals(Label.ID)) n.addAttribute("ui.label", "depot");
n.addAttribute("x", vehicle.getCoord().getX());
n.addAttribute("y", vehicle.getCoord().getY());
n.setAttribute("ui.class", "depot");
}
public static void display(VehicleRoutingProblem vrp) {
display(new View(vrp));
}
public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, int renderDelay_in_ms, boolean enableAutoLayout) {
View view = new View(vrp,solution);
view.setEnableAutoDisplay(enableAutoLayout);
view.setRenderDelay(renderDelay_in_ms);
display(view);
}
public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, int renderDelay_in_ms) {
View view = new View(vrp,solution);
view.setRenderDelay(renderDelay_in_ms);
display(view);
}
public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution) {
display(new View(vrp,solution));
}
private static void renderRoute(Graph g, VehicleRoute route, int routeId, long renderDelay_in_ms) {
int vehicle_edgeId = 1;
String prevIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getLocationId());
for(TourActivity act : route.getActivities()){
String currIdentifier = makeId(((JobActivity)act).getJob().getId(),act.getLocationId());
g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, currIdentifier, true);
prevIdentifier = currIdentifier;
vehicle_edgeId++;
sleep(renderDelay_in_ms);
}
if(route.getVehicle().isReturnToDepot()){
g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, makeId(route.getVehicle().getId(),route.getVehicle().getLocationId()), true);
}
}
private static String makeEdgeId(int routeId, int vehicle_edgeId) {
return Integer.valueOf(routeId).toString() + "." + Integer.valueOf(vehicle_edgeId).toString();
}
public static enum Label {
NO_LABEL, ID
}
private Label label = Label.NO_LABEL;
private long renderDelay_in_ms = 0;
private boolean enableAutoLayout = false;
private boolean renderShipments = false;
private VehicleRoutingProblem vrp;
private VehicleRoutingProblemSolution solution;
public GraphStreamViewer(VehicleRoutingProblem vrp) {
super();
this.vrp = vrp;
}
public GraphStreamViewer(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution) {
super();
this.vrp = vrp;
this.solution = solution;
}
public GraphStreamViewer labelWith(Label label){
this.label=label;
return this;
}
public GraphStreamViewer setRenderDelay(long ms){
this.renderDelay_in_ms=ms;
return this;
}
public GraphStreamViewer setEnableAutoLayout(boolean enableAutoLayout) {
this.enableAutoLayout = enableAutoLayout;
return this;
}
public GraphStreamViewer setRenderShipments(boolean renderShipments){
this.renderShipments = renderShipments;
return this;
}
public void display(){
View view = new View(vrp,solution);
view.setEnableAutoDisplay(enableAutoLayout);
view.setLabel(label);
view.setRenderDelay(renderDelay_in_ms);
view.setRenderShipments(renderShipments);
display(view);
}
// public void saveAsPNG(String filename){
//
// }
}

View file

@ -9,6 +9,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener; import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.Plotter; import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.SolutionPrinter.Print; import jsprit.analysis.toolbox.SolutionPrinter.Print;
@ -264,6 +265,7 @@ public class BicycleMessenger {
SolutionPrinter.print(bicycleMessengerProblem, Solutions.bestOf(solutions), Print.VERBOSE); SolutionPrinter.print(bicycleMessengerProblem, Solutions.bestOf(solutions), Print.VERBOSE);
GraphStreamViewer.display(bicycleMessengerProblem, Solutions.bestOf(solutions), 100);
} }

View file

@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener; import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.SolutionPlotter; import jsprit.analysis.toolbox.SolutionPlotter;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.StopWatch; import jsprit.analysis.toolbox.StopWatch;
@ -141,6 +142,7 @@ public class MultipleDepotExampleWithPenaltyVehicles {
SolutionPrinter.print(vrp,Solutions.bestOf(solutions),Print.VERBOSE); SolutionPrinter.print(vrp,Solutions.bestOf(solutions),Print.VERBOSE);
SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.bestOf(solutions), "output/p08_solution.png", "p08"); SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.bestOf(solutions), "output/p08_solution.png", "p08");
new GraphStreamViewer(vrp,Solutions.bestOf(solutions)).setRenderDelay(50).display();
} }
} }

View file

@ -20,6 +20,7 @@ import java.io.File;
import java.util.Collection; import java.util.Collection;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener; import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.Plotter; import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.Plotter.Label; import jsprit.analysis.toolbox.Plotter.Label;
import jsprit.analysis.toolbox.SolutionPlotter; import jsprit.analysis.toolbox.SolutionPlotter;
@ -106,7 +107,7 @@ public class PickupAndDeliveryExample2 {
plotter.setShowFirstActivity(true); plotter.setShowFirstActivity(true);
plotter.plot("output/pd_christophides_vrpnc1_solution.png","pd_vrpnc1"); plotter.plot("output/pd_christophides_vrpnc1_solution.png","pd_vrpnc1");
GraphStreamViewer.display(vrp, solution, 100);
} }

View file

@ -20,6 +20,7 @@ import java.io.File;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.Plotter; import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.SolutionPrinter.Print; import jsprit.analysis.toolbox.SolutionPrinter.Print;
@ -128,6 +129,7 @@ public class SimpleEnRoutePickupAndDeliveryOpenRoutesExample {
solutionPlotter.plotShipments(true); solutionPlotter.plotShipments(true);
solutionPlotter.plot("output/simpleEnRoutePickupAndDeliveryExample_solution.png", "en-route pickup and delivery"); solutionPlotter.plot("output/simpleEnRoutePickupAndDeliveryExample_solution.png", "en-route pickup and delivery");
GraphStreamViewer.display(problem, bestSolution, 100);
} }
} }

View file

@ -19,7 +19,7 @@ package jsprit.examples;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
import jsprit.analysis.toolbox.GraphStream; import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.Plotter; import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPlotter; import jsprit.analysis.toolbox.SolutionPlotter;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
@ -101,7 +101,7 @@ public class SolomonExample {
// GraphStream.display(vrp,100); // GraphStream.display(vrp,100);
GraphStream.display(vrp,solution); GraphStreamViewer.display(vrp,solution);
} }

View file

@ -19,7 +19,7 @@ package jsprit.examples;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
import jsprit.analysis.toolbox.GraphStream; import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.SolutionPlotter; import jsprit.analysis.toolbox.SolutionPlotter;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.VehicleRoutingAlgorithm;
@ -95,7 +95,7 @@ public class SolomonOpenExample {
SolutionPlotter.plotSolutionAsPNG(vrp, solution, "output/solomon_C101_open_solution.png","C101"); SolutionPlotter.plotSolutionAsPNG(vrp, solution, "output/solomon_C101_open_solution.png","C101");
GraphStream.display(vrp, solution, 50, false); GraphStreamViewer.display(vrp, solution, 50, false);
} }

View file

@ -20,16 +20,13 @@ import java.io.File;
import java.util.Collection; import java.util.Collection;
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener; import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener;
import jsprit.analysis.toolbox.GraphStream; import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.Plotter.Label;
import jsprit.analysis.toolbox.SolutionPrinter.Print;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.SolutionPrinter.Print;
import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.selector.SelectBest; import jsprit.core.algorithm.selector.SelectBest;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.VehicleRoutingProblem.Constraint;
import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint; import jsprit.core.problem.constraint.ServiceDeliveriesFirstConstraint;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
@ -105,7 +102,7 @@ public class VRPWithBackhaulsExample {
// plotter.setShowFirstActivity(true); // plotter.setShowFirstActivity(true);
// plotter.plot("output/vrpwbh_solomon_r101_solution.png","vrpwbh_r101"); // plotter.plot("output/vrpwbh_solomon_r101_solution.png","vrpwbh_r101");
GraphStream.display(vrp, solution, 100, true); GraphStreamViewer.display(vrp, solution, 100);
} }