| Home | OR-Objects | Tutorials | Prev | Next |
In this tutorial you will build on what you learned in Tutorial-1
and use OR-Objects to construct a street network like the one above. Then
you will customize a shortest path algorithm and use it to find customers
nearest to the
. The
customers are labeled 'A' through 'J'
and the nearness of a customer is measured by travel time. The
symbols are traffic lights which require one minute to cross. The 'L' and
'R' mark corners where left and right turns are not allowed.
For simplicity, some of the applet details will be skipped. If you would like all the details then view the complete source.
SparseGraph
graph;
.............
graph = new SparseGraph();
class Street
{
double speed;
double length;
Street(double len, double spd)
{
speed = spd;
length = len;
}
}
Node(int X, int Y)
{
x = X;
y = Y;
}
}
private void
buildGraph()
{
try {
// Add Intersections
//
graph.addVertex("N1E1",
new Node( 44, 37));
graph.addVertex("N1E2",
new Node( 44, 79));
............................................
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("I",
new Node(183, 54));
graph.addVertex("J",
new Node( 79, 133));
// Add Depot //
graph.addVertex("Depot",
new Node( 79, 79));
The next step is to add the edges. Notice that the edges adjacent to the restricted right and left turns use the street name as a key. These names are used in Properties.isConnectionRestricted to prevent wrong turns. The one-way street is added as a directed edge.
// Add one-way
street section
graph.addEdge("N2W1","N3W1",new
Street(1.0,20),true);
// Add Right Turn Restricted
Edges
graph.addEdge("N1E1","G",new
Street(0.5,20),false,"E1");
graph.addEdge("N1E1","N1E2",new
Street(1.0,20),false,"N1");
// Add Left Turn Restricted
Edges
graph.addEdge("G","N2E1",new
Street(0.5,20),false,"E1");
graph.addEdge("N2E1","N2E2",new
Street(1.0,20),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("N3E1","N4E1",new
Street(1.0,20));
graph.addEdge("N4E1","N5E1",new
Street(1.0,20));
................................................
class Properties
extends PropertiesAdapter
{
public double
getEdgeCost(EdgeI edge, boolean reverse)
{
Street street = (Street)edge.getValue();
return (street.length/street.speed) * 60.0;
}
The algorithm calls getVertexCost
to obtain the cost of crossing a vertex. In this tutorial the cost of crossing
a traffic light is one minute and zero for all other vertices.
buildGraph();
algorithm = new Connections(graph);
algorithm.setProperties(new
Properties());
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("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){}
}
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;
}
}
![]() |
|
![]() |