import java.awt.*; import java.util.*; import java.applet.*; import drasys.or.graph.*; import drasys.or.graph.sp.*; class Node { int x, y; Node(int X, int Y) { x = X; y = Y; } } class Street { double speed; double length; Street(double len, double spd) { speed = spd; length = len; } } class SpProperties extends PropertiesAdapter { public double getEdgeCost(EdgeI edge, boolean reverse) { Street street = (Street)edge.getValue(); return (street.length/street.speed) * 60; } public double getVertexCost(VertexI v) { if("S1E3".equals(v.getKey())) return 1.0; if("N3E2".equals(v.getKey())) return 1.0; return 0.0; } public boolean isConnectionRestricted(EdgeI in, VertexI v, EdgeI out) { if(in == out) return true; // No u-turns Object inKey = in.getKey(); Object outKey = out.getKey(); if("N1".equals(inKey) && "E1".equals(outKey)) return true; if("N2".equals(inKey) && "E1".equals(outKey)) return true; return false; } } public class Tutorial_2 extends Applet { Image image; String to; String from; SparseGraph graph; SingleVertexI algorithm; public void paint(Graphics g) { if(image != null) g.drawImage(image,0,0,this); } public void init() { super.init(); graph = new SparseGraph(); image = this.getImage(this.getDocumentBase(), "Tutorial_2.gif"); to = "Depot"; from = "Depot"; buildGraph(); algorithm = new Connections(graph); algorithm.setProperties(new SpProperties()); generateSinglePath(from,to); //{{INIT_CONTROLS setLayout(null); addNotify(); resize(249,317); label1 = new java.awt.Label("1. Path from"); label1.reshape(12,204,70,27); add(label1); fromChoice = new java.awt.Choice(); fromChoice.addItem("Depot"); fromChoice.addItem("A"); fromChoice.addItem("B"); fromChoice.addItem("C"); fromChoice.addItem("D"); fromChoice.addItem("E"); fromChoice.addItem("F"); fromChoice.addItem("G"); fromChoice.addItem("H"); fromChoice.addItem("I"); fromChoice.addItem("J"); add(fromChoice); fromChoice.reshape(96,204,56,21); label2 = new java.awt.Label("to",Label.CENTER); label2.reshape(156,204,24,24); add(label2); toChoice = new java.awt.Choice(); toChoice.addItem("Depot"); toChoice.addItem("A"); toChoice.addItem("B"); toChoice.addItem("C"); toChoice.addItem("D"); toChoice.addItem("E"); toChoice.addItem("F"); toChoice.addItem("G"); toChoice.addItem("H"); toChoice.addItem("I"); toChoice.addItem("J"); add(toChoice); toChoice.reshape(180,204,56,21); label3 = new java.awt.Label("2."); label3.reshape(12,240,12,24); add(label3); numFromChoice = new java.awt.Choice(); numFromChoice.addItem("1"); numFromChoice.addItem("2"); numFromChoice.addItem("3"); numFromChoice.addItem("4"); numFromChoice.addItem("5"); numFromChoice.addItem("6"); numFromChoice.addItem("7"); numFromChoice.addItem("8"); numFromChoice.addItem("9"); numFromChoice.addItem("10"); add(numFromChoice); numFromChoice.reshape(36,240,42,21); label4 = new java.awt.Label("paths FROM depot."); label4.reshape(84,240,112,24); add(label4); label5 = new java.awt.Label("3."); label5.reshape(12,276,24,24); add(label5); numToChoice = new java.awt.Choice(); numToChoice.addItem("1"); numToChoice.addItem("2"); numToChoice.addItem("3"); numToChoice.addItem("4"); numToChoice.addItem("5"); numToChoice.addItem("6"); numToChoice.addItem("7"); numToChoice.addItem("8"); numToChoice.addItem("9"); numToChoice.addItem("10"); add(numToChoice); numToChoice.reshape(36,276,42,21); label6 = new java.awt.Label("paths TO depot."); label6.reshape(84,276,112,24); add(label6); //}} } public boolean handleEvent(Event event) { if (event.id == Event.ACTION_EVENT && event.target == numToChoice) { selectedNumToChoice(); return true; } else if (event.id == Event.ACTION_EVENT && event.target == numFromChoice) { selectedNumFromChoice(); return true; } else if (event.id == Event.ACTION_EVENT && event.target == toChoice) { selectedToChoice(); return true; } else if (event.id == Event.ACTION_EVENT && event.target == fromChoice) { selectedFromChoice(); return true; } return super.handleEvent(event); } //{{DECLARE_CONTROLS java.awt.Label label1; java.awt.Choice fromChoice; java.awt.Label label2; java.awt.Choice toChoice; java.awt.Label label3; java.awt.Choice numFromChoice; java.awt.Label label4; java.awt.Label label5; java.awt.Choice numToChoice; java.awt.Label label6; //}} public void selectedFromChoice() { from = fromChoice.getSelectedItem(); generateSinglePath(from,to); } public void selectedToChoice() { to = toChoice.getSelectedItem(); generateSinglePath(from,to); } public void selectedNumFromChoice() { generateMultiplePaths(Integer.valueOf(numFromChoice.getSelectedItem()).intValue(), true); } public void selectedNumToChoice() { generateMultiplePaths(Integer.valueOf(numToChoice.getSelectedItem()).intValue(), false); } private void buildGraph() { try { // Add Intersections // graph.addVertex("N1E1", new Node( 44, 37)); graph.addVertex("N1E2", new Node( 44, 79)); graph.addVertex("N1E3", new Node( 44, 115)); graph.addVertex("N1E4", new Node( 44, 148)); graph.addVertex("N2E1", new Node(119, 37)); graph.addVertex("N2E2", new Node(119, 79)); graph.addVertex("N2W1", new Node(119, 102)); graph.addVertex("N3E1", new Node(149, 37)); graph.addVertex("N3E2", new Node(149, 79)); graph.addVertex("N3W1", new Node(149, 102)); graph.addVertex("N4E1", new Node(183, 37)); graph.addVertex("N4E2", new Node(183, 79)); graph.addVertex("N4W1", new Node(183, 102)); graph.addVertex("N4E4", new Node(183, 148)); graph.addVertex("N5E1", new Node(208, 37)); graph.addVertex("N5E2", new Node(208, 79)); graph.addVertex("S1E3", new Node( 79, 115)); graph.addVertex("S1E4", new Node( 79, 148)); graph.addVertex("S2E3", new Node(102, 115)); graph.addVertex("S2E4", new Node(102, 148)); // Add Customers // graph.addVertex("A", new Node(135, 37)); graph.addVertex("B", new Node(166, 79)); graph.addVertex("C", new Node(208, 54)); graph.addVertex("D", new Node( 60, 115)); graph.addVertex("E", new Node(163, 148)); graph.addVertex("F", new Node(102, 131)); graph.addVertex("G", new Node( 79, 37)); graph.addVertex("H", new Node(203, 102)); graph.addVertex("I", new Node(183, 54)); graph.addVertex("J", new Node( 79, 133)); // Add Depot // graph.addVertex("Depot", new Node( 79, 79)); // Add oneway street graph.addEdge("N2W1","N3W1",new Street(1.0,30),true); // Add Right Turn Restricted Edges graph.addEdge("N1E1","G",new Street(0.5,30),false,"E1"); graph.addEdge("N1E1","N1E2",new Street(1.0,30),false,"N1"); // Add Left Turn Restricted Edges graph.addEdge("G","N2E1",new Street(0.5,30),false,"E1"); graph.addEdge("N2E1","N2E2",new Street(1.0,30),false,"N2"); // Add Other Edges // graph.addEdge("N1E2","Depot",new Street(0.5,20)); graph.addEdge("Depot","N2E2",new Street(0.5,20)); graph.addEdge("N2E1","A",new Street(0.5,20)); graph.addEdge("A","N3E1",new Street(0.5,20)); graph.addEdge("N3E2","B",new Street(0.5,20)); graph.addEdge("B","N4E2",new Street(0.5,20)); graph.addEdge("N5E1","C",new Street(0.5,20)); graph.addEdge("C","N5E2",new Street(0.5,20)); graph.addEdge("N1E3","D",new Street(0.5,20)); graph.addEdge("D","S1E3",new Street(0.5,20)); graph.addEdge("S2E4","E",new Street(0.6,20)); graph.addEdge("E","N4E4",new Street(0.4,20)); graph.addEdge("S2E3","F",new Street(0.5,20)); graph.addEdge("F","S2E4",new Street(0.5,20)); graph.addEdge("N4W1","H",new Street(0.5,20)); graph.addEdge("N4E1","I",new Street(0.5,20)); graph.addEdge("I","N4E2",new Street(0.5,20)); graph.addEdge("S1E3","J",new Street(0.5,20)); graph.addEdge("J","S1E4",new Street(0.5,20)); graph.addEdge("N3E1","N4E1",new Street(1.0,20)); graph.addEdge("N4E1","N5E1",new Street(1.0,20)); graph.addEdge("N2E2","N3E2",new Street(1.0,20)); graph.addEdge("N4E2","N5E2",new Street(1.0,20)); graph.addEdge("N3W1","N4W1",new Street(1.0,20)); graph.addEdge("S1E3","S2E3",new Street(1.0,20)); graph.addEdge("N1E4","S1E4",new Street(1.0,20)); graph.addEdge("S1E4","S2E4",new Street(1.0,20)); graph.addEdge("N1E2","N1E3",new Street(1.0,20)); graph.addEdge("N1E3","N1E4",new Street(1.0,20)); graph.addEdge("N2E2","N2W1",new Street(1.0,20)); graph.addEdge("N3E1","N3E2",new Street(1.0,20)); graph.addEdge("N3E2","N3W1",new Street(1.0,20)); graph.addEdge("N4E1","N4E2",new Street(1.0,20)); graph.addEdge("N4E2","N4W1",new Street(1.0,20)); graph.addEdge("N4W1","N4E4",new Street(1.0,20)); graph.addEdge("N5E1","N5E2",new Street(1.0,20)); } catch(DuplicateEdgeException e){} catch(VertexNotFoundException e){} catch(DuplicateVertexException e){} } public void generateSinglePath(Object src, Object dst) { try{ algorithm.setCandidate(false); algorithm.setCandidate(dst, true); algorithm.generatePathsFrom(src); } catch(VertexNotFoundException e){} catch(InvalidPropertyException e){} drawAllPaths(algorithm.candidates()); } public void generateMultiplePaths(int n, boolean isFrom) { try{ algorithm.setCandidate(false); algorithm.setCandidate("A", true); algorithm.setCandidate("B", true); algorithm.setCandidate("C", true); algorithm.setCandidate("D", true); algorithm.setCandidate("E", true); algorithm.setCandidate("F", true); algorithm.setCandidate("G", true); algorithm.setCandidate("H", true); algorithm.setCandidate("I", true); algorithm.setCandidate("J", true); algorithm.setMaxPaths(n); if(isFrom) algorithm.generatePathsFrom("Depot"); else algorithm.generatePathsTo("Depot"); } catch(VertexNotFoundException e){} catch(InvalidPropertyException e){} drawAllPaths(algorithm.candidates()); } public void drawAllPaths(Enumeration destinations) { Graphics g = this.getGraphics(); paint(g); g.setColor(Color.white); try{ while(destinations.hasMoreElements()){ VertexI dest = (VertexI)destinations.nextElement(); drawPath(g, dest); } } catch(VertexNotFoundException e){} } public void drawPath(Graphics g, VertexI dest) throws VertexNotFoundException { Enumeration path = algorithm.pathElements(dest); VertexI vertex = (VertexI)path.nextElement(); Node lastNode = (Node)vertex.getValue(); while(path.hasMoreElements()){ path.nextElement(); // Remove Edge vertex = (VertexI)path.nextElement(); Node node = (Node)vertex.getValue(); g.drawLine(lastNode.x,lastNode.y,node.x,node.y); lastNode = node; } } }