update of statistic information needs to be done before update of pheromone information
[aco.git] / Coordinate.java
blobc931aead5c556b08d2e34dabeb41ff61ff5fd340
1 import java.util.HashMap;
3 class Coordinate {
5 protected Graph graph;
6 protected final HashMap<Integer, CoordinatePair<Integer, Integer>> Coordinates;
8 Coordinate(Graph graph) {
9 this.graph = graph;
10 this.Coordinates = new HashMap<Integer, CoordinatePair<Integer, Integer>>(graph.getNumOfCities());
13 Coordinate(Graph graph, TSPLibReader tsplibreader) {
14 this.graph = graph;
15 this.Coordinates = tsplibreader.getCoordinates();
16 graph.setNumOfCities(Coordinates.size());
19 public int[][] computeDistances() {
21 int[][] Distance = new int[graph.getNumOfCities()][graph.getNumOfCities()];
23 for (int k1 : Coordinates.keySet()) {
24 for (int k2 : Coordinates.keySet()) {
25 if (k1 == k2)
26 Distance[k1][k2] = graph.getINFINITY();
27 else
28 Distance[k1][k2] = computeDistance(getCoordinates(k1), getCoordinates(k2));
32 return Distance;
35 public int computeDistance(CoordinatePair<Integer, Integer> CityOne,
36 CoordinatePair<Integer, Integer> CityTwo) {
37 /* pythagoras: a^2 + b^2 = c^2 => c = sqrt( a^2 + b^2 ) */
38 return (int)Math.sqrt(
39 Math.pow((double)(CityOne.getFirst() - CityTwo.getFirst()), 2) +
40 Math.pow( (double)(CityOne.getSecond() - CityTwo.getSecond()), 2 ) );
42 /* ATT */
44 double xd = (double)(CityOne.getFirst() - CityTwo.getFirst());
45 double yd = (double)(CityOne.getSecond() - CityTwo.getSecond());
46 double rij = Math.sqrt( (xd*xd + yd*yd) / 10.0 );
47 return (int)Math.round(rij);
51 public CoordinatePair<Integer, Integer> getCoordinates(int City) {
52 return Coordinates.get(City);