5 public class GraphStrategy
{
7 protected ACOMediator acom
;
9 public GraphStrategy(ACOMediator acom
) {
13 public void computeNearestNeighbourListDepth() {
14 if (acom
.getNearestNeighbourListDepth() == 0) {
15 if (acom
.getNumOfCities() <= 30)
16 acom
.setNearestNeighbourListDepth(acom
.getNumOfCities() - 1);
17 else if (acom
.getNumOfCities() > 30 && acom
.getNumOfCities() < 80)
18 acom
.setNearestNeighbourListDepth(acom
.getNumOfCities()/2);
20 acom
.setNearestNeighbourListDepth(40);
21 } else if (acom
.getNearestNeighbourListDepth() > acom
.getNumOfCities()) {
22 acom
.setNearestNeighbourListDepth(acom
.getNumOfCities());
26 public int nearestNeighbourHeuristicRandomStart() {
28 boolean[] visited
= new boolean[acom
.getNumOfCities()];
30 for (int i
= 0; i
< acom
.getNumOfCities(); i
++)
33 return nearestNeighbourHeuristic(
34 (int)( Math
.random() * acom
.getNumOfCities() ),
36 acom
.getNumOfCities());
39 public int nearestNeighbourHeuristic(int city
, boolean[] visited
, int remaining
) {
41 int nextmin
= acom
.getInfinity();
44 for (int i
= 0; i
< acom
.getNumOfCities(); i
++) {
47 (acom
.getDistance(i
, city
) < nextmin
) ) {
48 nextmin
= acom
.getDistance(i
, city
);
53 visited
[nextcity
] = true;
58 return nextmin
+ nearestNeighbourHeuristic(nextcity
, visited
, remaining
- 1);