documentation files
[aco.git] / protoas / Coordinate.java
blob28481cae41a5c19bb332dcb66128d8276c04f0b2
1 package protoas;
3 import java.util.HashMap;
5 class Coordinate {
7 protected Graph graph;
8 protected final HashMap<Integer, CoordinatePair<Integer, Integer>> Coordinates;
10 Coordinate(Graph graph) {
11 this.graph = graph;
12 this.Coordinates = new HashMap<Integer, CoordinatePair<Integer, Integer>>(graph.getNumOfCities());
15 Coordinate(Graph graph, TSPLibReader tsplibreader) {
16 this.graph = graph;
17 this.Coordinates = tsplibreader.getCoordinates();
18 graph.setNumOfCities(Coordinates.size());
21 public int[][] computeDistances() {
23 int[][] Distance = new int[graph.getNumOfCities()][graph.getNumOfCities()];
25 for (int k1 : Coordinates.keySet()) {
26 for (int k2 : Coordinates.keySet()) {
27 if (k1 == k2)
28 Distance[k1][k2] = graph.getINFINITY();
29 else
30 Distance[k1][k2] = computeDistance(getCoordinates(k1), getCoordinates(k2));
34 return Distance;
37 public int computeDistance(CoordinatePair<Integer, Integer> CityOne,
38 CoordinatePair<Integer, Integer> CityTwo) {
39 /* pythagoras: a^2 + b^2 = c^2 => c = sqrt( a^2 + b^2 ) */
40 return (int)Math.sqrt(
41 Math.pow((double)(CityOne.getFirst() - CityTwo.getFirst()), 2) +
42 Math.pow( (double)(CityOne.getSecond() - CityTwo.getSecond()), 2 ) );
44 /* ATT */
46 double xd = (double)(CityOne.getFirst() - CityTwo.getFirst());
47 double yd = (double)(CityOne.getSecond() - CityTwo.getSecond());
48 double rij = Math.sqrt( (xd*xd + yd*yd) / 10.0 );
49 return (int)Math.round(rij);
53 public CoordinatePair<Integer, Integer> getCoordinates(int City) {
54 return Coordinates.get(City);