1 package aco
.strategy
.as
.asrank
;
3 import java
.util
.TreeMap
;
4 import java
.util
.SortedMap
;
8 import aco
.mediator
.as
.*;
9 import aco
.mediator
.as
.asrank
.*;
11 public class ASRankPheromoneStrategy
12 extends PheromoneStrategy
{
14 public ASRankPheromoneStrategy(ASRankMediator acom
) {
15 super((ASMediator
)acom
);
18 public void pheromoneUpdate(Ant
[] ants
) {
21 this.applyRankPheromones(ants
);
22 this.enforceGlobalBest();
24 acom
.computeChoiceInformation();
27 protected void applyRankPheromones(Ant
[] ants
) {
28 SortedMap
<Integer
, Ant
> antmap
= new TreeMap
<Integer
, Ant
>();
30 int max
= 0, slots
= 0;
31 for (Ant ant
: ants
) {
32 if (ant
.getTourLength() == acom
.getGlobalBestTourLength())
34 if (slots
++ < (int)((ASRankMediator
)acom
).getW()) {
35 antmap
.put(ant
.getTourLength(), ant
);
36 } else if (ant
.getTourLength() < antmap
.lastKey()) {
37 for (int i
: antmap
.keySet()) {
42 antmap
.put(ant
.getTourLength(), ant
);
47 int w
= (int)((ASRankMediator
)acom
).getW();
48 int r
= 1, j
= 0, l
= 0;
50 for (int i
: antmap
.keySet()) {
51 dt
= (double)(w
- (r
++)) * (1.0/(double)antmap
.get(i
).getTourLength());
52 for (int n
= 0; n
< acom
.getNumOfCities(); n
++) {
53 j
= antmap
.get(i
).getTour(n
);
54 l
= antmap
.get(i
).getTour(n
+1);
55 acom
.setPheromone(j
, l
, (acom
.getPheromone(j
, l
) + dt
) );
60 protected void enforceGlobalBest() {
61 /* enforcement of global best route */
63 for (int i
= 0; i
< acom
.getNumOfCities(); i
++) {
64 j
= acom
.getGlobalBestTour(i
);
65 l
= acom
.getGlobalBestTour(i
+1);
66 acom
.setPheromone( j
, l
, (acom
.getPheromone(j
,l
) +
67 ((ASRankMediator
)acom
).getW()/(double)acom
.getGlobalBestTourLength()) );
71 protected void depositPheromone(Ant ant
) {
72 double dt
= 1.0/(double)ant
.getTourLength();
75 for (int i
= 0; i
< acom
.getNumOfCities(); i
++) {
78 /* setPheromone will update j,l and l,j */
79 acom
.setPheromone( j
, l
, (acom
.getPheromone(j
,l
) + dt
) );
83 protected void evaporate() {
84 for (int i
= 1; i
< acom
.getNumOfCities(); i
++) {
85 for (int j
= 1; j
< acom
.getNumOfCities(); j
++) {
86 acom
.setPheromone(i
, j
,
87 (1.0 - acom
.getRoh()) * acom
.getPheromone(i
, j
) );