-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColony.java
More file actions
79 lines (70 loc) · 1.52 KB
/
Copy pathColony.java
File metadata and controls
79 lines (70 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main;
import java.util.ArrayList;
public class Colony {
private ArrayList<Ant> ants;
private int numPassed = 0;
private int numDead = 0;
public Colony(){
ants = new ArrayList<Ant>();
}
/**
* Run a specified number of ants through the graph
* @return the path of the last ant
*/
public ArrayList<Edge> swarm(Graph g, int numAnts){
Ant a = new Ant(g,g.getEnd());
for(int i = 0; i < numAnts; i++){
a = new Ant(g, g.getEnd());
ants.add(a);
int status = 0;
while(status == 0){
status = a.move();
}
if(status == -1){
numDead++;
}
else{
numPassed++;
}
}
return a.getPath();
}
/**
* Run an unlimited number of ants through the graph until the same path is found a specified number of times in a row
* @return the optimized path
*/
public ArrayList<Edge> swarmUntilStatic(Graph g, int threshold){
ArrayList<Edge> path = new ArrayList<Edge>();
int numConsecutiveSamePaths = 0;
while(numConsecutiveSamePaths < threshold){
Ant a = new Ant(g, g.getEnd());
int status = a.walk();
if(status == -1){
numDead++;
numConsecutiveSamePaths = 0;
path = null;
}
else{
if(path == null || !a.getPath().equals(path)){
numConsecutiveSamePaths = 0;
}
else{
numConsecutiveSamePaths++;
}
numPassed++;
path = a.getPath();
}
g.evaporate();
}
return path;
}
public int getNumDead(){
return numDead;
}
public int getNumPassed(){
return numPassed;
}
public int getNumRuns(){
return numDead + numPassed;
}
}