| Home | OR-Objects | Tutorials | Prev | Next |
In this tutorial you will learn to use OR-Objects to model the problem pictured to above. A supply service is located at the depot and uses a helicopter to make deliveries. The problem is to design a tour which connects all the selected customers to the depot while minimizing the flight distance.
For simplicity, some of the applet details will be skipped. If you would like all the details then view the complete source.
class Customer
extends drasys.or.geom.geo.Point
{
String key;
boolean isSelected;
drasys.or.geom.rect2.Point screenCoord;
Customer(String id, double longitude, double latitude, int x, int y)
{
super(longitude, latitude);
key = id;
isSelected = false;
screenCoord = new drasys.or.geom.rect2.Point(x,y);
}
}
public static final Customer[] customers = {
new Customer("Depot", 161.325, -6.355, 132, 135),
new Customer("A", 160.215, -5.155, 21, 15),
new Customer("B", 161.015, -5.195, 101, 19),
new Customer("C", 162.105, -5.225, 210, 22),
new Customer("D", 160.565, -5.465, 56, 46),
new Customer("E", 161.375, -5.465, 137, 46),
new Customer("F", 161.025, -5.585, 102, 58),
new Customer("H", 160.205, -5.845, 20, 84),
new Customer("I", 161.035, -5.975, 103, 97),
new Customer("J", 162.005, -5.835, 200, 83),
new Customer("K", 160.325, -6.455, 32, 145),
new Customer("L", 162.195, -6.505, 219, 150)
};
PointIndexI index;
...
index = new KDTree();
for(int i=1; i<customers.length; i++){
index.put(customers[i].screenCoord, customers[i]);
}
public void
mouseClick(int x, int y)
{
PointI clickPoint = new drasys.or.geom.rect2.Point(x,y);
PairI pair = index.getNearestNeighborTo(clickPoint);
Customer Customer = (Customer)pair.getSecond();
Customer.isSelected = Customer.isSelected ? false : true;
paintcustomers(getGraphics());
}
The Composite class is a generic implementation of a composite TSP algorithm. It is passed the construction and improvement algorithms when it is created. If null is passed for the improvement algorithm then only the construction algorithm will be used.
Composite tsp;
public void
newAlgorithm(String constructStr, String improveStr)
{
ConstructI construct = null;
if(constructStr.equals("Nearest Add")) construct = new NearestAddition();
else if(constructStr.equals("Nearest Ins")) construct = new NearestInsertion();
else if(constructStr.equals("Farthest Ins")) construct = new FarthestInsertion();
else if(constructStr.equals("Cheapest Ins")) construct = new CheapestInsertion();
else if(constructStr.equals("GENI-7")) construct = new Geni(7);
else if(constructStr.equals("Enumeration")) construct = new FullEnumeration();
else System.out.println("Can't find: "+constructStr);
ImproveI improve = null;
if(improveStr.equals("2-Opt")) improve = new TwoOpt();
else if(improveStr.equals("3-Opt")) improve = new ThreeOpt();
else if(improveStr.equals("US-5")) improve = new Us(5);
else if(improveStr.equals("None")) improve = null;
else System.out.println("Can't find: "+improveStr);
tsp = new Composite(construct, improve);
}
PointGraph graph = new PointGraph();
for(int i=0; i<customers.length; i++){
Customer Customer = customers[i];
if(Customer.isSelected)
graph.addVertex(Customer.key, Customer);
}
public void
buildTour(GraphI graph)
throws TourNotFoundException, VertexNotFoundException
{
tsp.setGraph(new MatrixGraph(graph, null));
if(tourType.equals("Closed"))
tsp.constructClosedTour();
else if(tourType.equals("Open"))
tsp.constructOpenTour();
else if(tourType.equals("Depot")){
tsp.constructOpenTourFrom("Depot");
}
}
Notice that the TSP algorithm is initialized with a 'MatrixGraph' constructed
from the graph argument instead of the argument dorectly. The reason is
that the argument is an instance of a 'PointGraph' and the distance calculations
for the geographic points is relatively expensive. The 'MatrixGraph' will
compute all of the distances just once and store the results so that the
TSP algorithm can run more efficiently.
public void
paintTour()
{
Graphics g = this.getGraphics();
g.setColor(Color.blue);
Enumeration e=tsp.getTour().elements();
e.nextElement(); // Skip Vertex
while(e.hasMoreElements()){
EdgeI edge = (EdgeI)e.nextElement();
Customer Customer1 = (Customer)edge.getToVertex().getValue();
Customer Customer2 = (Customer)edge.getFromVertex().getValue();
int x1 = (int)Customer1.screenCoord.x();
int y1 = (int)Customer1.screenCoord.y();
int x2 = (int)Customer2.screenCoord.x();
int y2 = (int)Customer2.screenCoord.y();
g.drawLine(x1, y1, x2, y2);
e.nextElement(); // Skip Vertex
}
}
![]() |
|
![]() |