mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
issue #63 - very basic version of GraphStreamViewer
This commit is contained in:
parent
439ee29895
commit
8e862b524e
4 changed files with 134 additions and 249 deletions
|
|
@ -1,5 +1,11 @@
|
||||||
package jsprit.analysis.toolbox;
|
package jsprit.analysis.toolbox;
|
||||||
|
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
import javax.swing.JFormattedTextField;
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import jsprit.core.problem.job.Job;
|
import jsprit.core.problem.job.Job;
|
||||||
import jsprit.core.problem.job.Service;
|
import jsprit.core.problem.job.Service;
|
||||||
|
|
@ -18,132 +24,54 @@ import org.graphstream.ui.swingViewer.Viewer;
|
||||||
|
|
||||||
public class GraphStreamViewer {
|
public class GraphStreamViewer {
|
||||||
|
|
||||||
protected static String styleSheet =
|
protected static String styleSheet =
|
||||||
"node {" +
|
"node {" +
|
||||||
" size: 7px, 7px;" +
|
" size: 10px, 10px;" +
|
||||||
" fill-color: rgb(255,204,0);" +
|
" fill-color: #6CC644;" +
|
||||||
" text-alignment: at-right;" +
|
" text-alignment: at-right;" +
|
||||||
" stroke-mode: plain;" +
|
" stroke-mode: plain;" +
|
||||||
" stroke-color: black;" +
|
" stroke-color: #999;" +
|
||||||
" stroke-width: 1.0;" +
|
" stroke-width: 1.0;" +
|
||||||
"}" +
|
"}" +
|
||||||
"node.pickup {" +
|
"node.pickup {" +
|
||||||
" fill-color: rgb(255,204,0);" +
|
" fill-color: #6CC644;" +
|
||||||
"}" +
|
"}" +
|
||||||
"node.delivery {" +
|
"node.delivery {" +
|
||||||
" fill-color: rgb(0,102,153);" +
|
" fill-color: #f93;" +
|
||||||
"}" +
|
"}" +
|
||||||
"node.depot {" +
|
"node.depot {" +
|
||||||
" fill-color: red;" +
|
" fill-color: #BD2C00;" +
|
||||||
" size: 10px, 10px;" +
|
" size: 10px, 10px;" +
|
||||||
" shape: box;" +
|
" shape: box;" +
|
||||||
"}" +
|
"}" +
|
||||||
"edge {" +
|
"edge {" +
|
||||||
" fill-color: black;" +
|
" fill-color: #333;" +
|
||||||
" arrow-size: 6px,3px;" +
|
" arrow-size: 6px,3px;" +
|
||||||
"}" +
|
"}" +
|
||||||
"edge.shipment {" +
|
"edge.shipment {" +
|
||||||
" fill-color: grey;" +
|
" fill-color: #999;" +
|
||||||
" arrow-size: 6px,3px;" +
|
" 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 BoundingBox boundingBox;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param boundingBox the boundingBox to set
|
|
||||||
*/
|
|
||||||
public void setBoundingBox(BoundingBox boundingBox) {
|
|
||||||
this.boundingBox = boundingBox;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// public static void display(VehicleRoutingProblem vrp, int renderDelay_in_ms) {
|
|
||||||
// View builder = new View(vrp);
|
|
||||||
// builder.setRenderDelay(renderDelay_in_ms);
|
|
||||||
// display();
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// public static void display(VehicleRoutingProblem vrp) {
|
|
||||||
// display();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, int renderDelay_in_ms) {
|
|
||||||
// View view = new View(vrp,solution);
|
|
||||||
// view.setRenderDelay(renderDelay_in_ms);
|
|
||||||
// display();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution) {
|
|
||||||
// display();
|
|
||||||
// }
|
|
||||||
|
|
||||||
public static enum Label {
|
public static enum Label {
|
||||||
NO_LABEL, ID
|
NO_LABEL, ID
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class BoundingBox {
|
// private static class BoundingBox {
|
||||||
final double minX;
|
// final double minX;
|
||||||
final double minY;
|
// final double minY;
|
||||||
final double maxX;
|
// final double maxX;
|
||||||
final double maxY;
|
// final double maxY;
|
||||||
|
//
|
||||||
public BoundingBox(double minX, double minY, double maxX, double maxY) {
|
// public BoundingBox(double minX, double minY, double maxX, double maxY) {
|
||||||
super();
|
// super();
|
||||||
this.minX = minX;
|
// this.minX = minX;
|
||||||
this.minY = minY;
|
// this.minY = minY;
|
||||||
this.maxX = maxX;
|
// this.maxX = maxX;
|
||||||
this.maxY = maxY;
|
// this.maxY = maxY;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
|
|
||||||
private Label label = Label.NO_LABEL;
|
private Label label = Label.NO_LABEL;
|
||||||
|
|
||||||
|
|
@ -153,7 +81,7 @@ public class GraphStreamViewer {
|
||||||
|
|
||||||
private boolean renderShipments = false;
|
private boolean renderShipments = false;
|
||||||
|
|
||||||
private BoundingBox boundingBox;
|
// private BoundingBox boundingBox;
|
||||||
|
|
||||||
private VehicleRoutingProblem vrp;
|
private VehicleRoutingProblem vrp;
|
||||||
|
|
||||||
|
|
@ -190,100 +118,75 @@ public class GraphStreamViewer {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public GraphStreamViewer setBoundingBox(double minX, double minY, double maxX, double maxY){
|
|
||||||
// boundingBox = new BoundingBox(minX,minY,maxX,maxY);
|
|
||||||
// return this;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public void display(){
|
|
||||||
// display();
|
|
||||||
//// View view = new View(vrp,solution);
|
|
||||||
//// view.setEnableAutoDisplay(enableAutoLayout);
|
|
||||||
//// view.setLabel(label);
|
|
||||||
//// view.setRenderDelay(renderDelay_in_ms);
|
|
||||||
//// view.setRenderShipments(renderShipments);
|
|
||||||
//// view.setBoundingBox(boundingBox);
|
|
||||||
//// display(view);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void display(){
|
public void display(){
|
||||||
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
|
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
|
||||||
Graph g = new MultiGraph("g");
|
Graph g = new MultiGraph("g");
|
||||||
g.addAttribute("ui.quality");
|
g.addAttribute("ui.quality");
|
||||||
g.addAttribute("ui.antialias");
|
g.addAttribute("ui.antialias");
|
||||||
g.addAttribute("ui.stylesheet", styleSheet);
|
g.addAttribute("ui.stylesheet", styleSheet);
|
||||||
|
|
||||||
Viewer viewer = g.display();
|
Viewer viewer = g.display();
|
||||||
if(!enableAutoLayout) viewer.disableAutoLayout();
|
|
||||||
|
// MessageFormat format = new MessageFormat("{0,number,00}:{1,number,00}:{2,number,00}");
|
||||||
|
//
|
||||||
|
// JFormattedTextField textField = new JFormattedTextField(format);
|
||||||
|
// textField.setEditable(false);
|
||||||
|
// textField.setMaximumSize(new Dimension(100,30));
|
||||||
|
// textField.setMinimumSize(new Dimension(80,30));
|
||||||
|
// textField.setHorizontalAlignment(JTextField.CENTER);
|
||||||
|
// textField.setText("00:01:56");
|
||||||
|
// viewer.getDefaultView().add(textField);
|
||||||
|
|
||||||
|
if(!enableAutoLayout) viewer.disableAutoLayout();
|
||||||
|
|
||||||
|
|
||||||
for(Vehicle vehicle : vrp.getVehicles()){
|
for(Vehicle vehicle : vrp.getVehicles()){
|
||||||
renderVehicle(g,vehicle,label);
|
renderVehicle(g,vehicle,label);
|
||||||
sleep(renderDelay_in_ms);
|
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);
|
|
||||||
}
|
|
||||||
sleep(renderDelay_in_ms);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(solution != null){
|
|
||||||
int routeId = 1;
|
|
||||||
for(VehicleRoute route : solution.getRoutes()){
|
|
||||||
renderRoute(g,route,routeId,renderDelay_in_ms);
|
|
||||||
sleep(renderDelay_in_ms);
|
|
||||||
routeId++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if(view.boundingBox != null){
|
|
||||||
//// viewer.getDefaultView().getCamera().setViewPercent(0.5);
|
|
||||||
// System.out.println("metric="+viewer.getDefaultView().getCamera().getMetrics());
|
|
||||||
//// viewer.getDefaultView().getCamera().setViewCenter(15000, 50000, 0);
|
|
||||||
//// viewer.getDefaultView().getCamera().setViewPercent(0.5);
|
|
||||||
// viewer.getDefaultView().getCamera().setBounds(10000,40000, 0, 20000, 60000, 0);
|
|
||||||
// System.out.println("metric="+viewer.getDefaultView().getCamera().getMetrics());
|
|
||||||
//// viewer.getDefaultView().se
|
|
||||||
// viewer.getDefaultView().display(viewer.getGraphicGraph(), true);
|
|
||||||
//// viewer.getDefaultView().getCamera().setViewPercent(0.5);
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void display(VehicleRoutingProblem vrp, int renderDelay_in_ms) {
|
for(Job j : vrp.getJobs().values()){
|
||||||
// View builder = new View(vrp);
|
if(j instanceof Service){
|
||||||
// builder.setRenderDelay(renderDelay_in_ms);
|
renderService(g,(Service)j,label);
|
||||||
// display();
|
|
||||||
// }
|
|
||||||
|
|
||||||
private 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){
|
|
||||||
Edge s = g.addEdge(shipment.getId(), makeId(shipment.getId(),shipment.getPickupLocation()),
|
|
||||||
makeId(shipment.getId(),shipment.getDeliveryLocation()), true);
|
|
||||||
s.addAttribute("ui.class", "shipment");
|
|
||||||
}
|
}
|
||||||
|
else if(j instanceof Shipment){
|
||||||
|
renderShipment(g,(Shipment)j,label,renderShipments);
|
||||||
|
}
|
||||||
|
sleep(renderDelay_in_ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(solution != null){
|
||||||
|
int routeId = 1;
|
||||||
|
for(VehicleRoute route : solution.getRoutes()){
|
||||||
|
renderRoute(g,route,routeId,renderDelay_in_ms);
|
||||||
|
sleep(renderDelay_in_ms);
|
||||||
|
routeId++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private 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){
|
||||||
|
Edge s = g.addEdge(shipment.getId(), makeId(shipment.getId(),shipment.getPickupLocation()),
|
||||||
|
makeId(shipment.getId(),shipment.getDeliveryLocation()), true);
|
||||||
|
s.addAttribute("ui.class", "shipment");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void sleep(long renderDelay_in_ms2) {
|
private void sleep(long renderDelay_in_ms2) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(renderDelay_in_ms2);
|
Thread.sleep(renderDelay_in_ms2);
|
||||||
|
|
@ -315,27 +218,6 @@ public class GraphStreamViewer {
|
||||||
n.setAttribute("ui.class", "depot");
|
n.setAttribute("ui.class", "depot");
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void display(VehicleRoutingProblem vrp) {
|
|
||||||
// display();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, int renderDelay_in_ms) {
|
|
||||||
// View view = new View(vrp,solution);
|
|
||||||
// view.setRenderDelay(renderDelay_in_ms);
|
|
||||||
// display();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public static void display(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution) {
|
|
||||||
// display();
|
|
||||||
// }
|
|
||||||
|
|
||||||
private void renderRoute(Graph g, VehicleRoute route, int routeId, long renderDelay_in_ms) {
|
private void renderRoute(Graph g, VehicleRoute route, int routeId, long renderDelay_in_ms) {
|
||||||
int vehicle_edgeId = 1;
|
int vehicle_edgeId = 1;
|
||||||
String prevIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getLocationId());
|
String prevIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getLocationId());
|
||||||
|
|
@ -355,7 +237,7 @@ public class GraphStreamViewer {
|
||||||
return Integer.valueOf(routeId).toString() + "." + Integer.valueOf(vehicle_edgeId).toString();
|
return Integer.valueOf(routeId).toString() + "." + Integer.valueOf(vehicle_edgeId).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void saveAsPNG(String filename){
|
// public void saveAsPNG(String filename){
|
||||||
//
|
//
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@ public class PickupAndDeliveryExample2 {
|
||||||
plotter.plot("output/pd_christophides_vrpnc1_solution.png","pd_vrpnc1");
|
plotter.plot("output/pd_christophides_vrpnc1_solution.png","pd_vrpnc1");
|
||||||
|
|
||||||
|
|
||||||
new GraphStreamViewer(vrp, solution).setRenderDelay(100).labelWith(jsprit.analysis.toolbox.GraphStreamViewer.Label.ID).display();
|
new GraphStreamViewer(vrp, solution).setRenderDelay(100).display();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -129,7 +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");
|
||||||
|
|
||||||
new GraphStreamViewer(problem, bestSolution).setRenderDelay(100).display();
|
new GraphStreamViewer(problem, bestSolution).setRenderShipments(true).setRenderDelay(100).display();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +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.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.SolutionPrinter.Print;
|
import jsprit.analysis.toolbox.SolutionPrinter.Print;
|
||||||
|
|
@ -103,7 +104,9 @@ public class SimpleExample {
|
||||||
/*
|
/*
|
||||||
* plot
|
* plot
|
||||||
*/
|
*/
|
||||||
SolutionPlotter.plotSolutionAsPNG(problem, bestSolution, "output/solution.png", "solution");
|
// SolutionPlotter.plotSolutionAsPNG(problem, bestSolution, "output/solution.png", "solution");
|
||||||
|
|
||||||
|
new GraphStreamViewer(problem, bestSolution).display();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue