-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ant.java
108 lines (91 loc) · 2.46 KB
/
Ant.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;
public class Ant extends Actor implements Comparable<Ant>
{
private static final int PATH_LENGTH = 32;
private static final double MUTATION_RATE = 0.05;
private String path;
private AntColony colony;
public Ant(AntColony c)
{
colony = c;
path = randomPath();
}
// define an ordering such that the Ant whose path get it closest
// to the food is greatest
public int compareTo(Ant other)
{
return other.distanceFromFood() - distanceFromFood();
}
// the number of steps it will take us to get to food from the end
// of our path. imagine that we can smell it or something
public int distanceFromFood()
{
Grid g = colony.getGrid();
// this casting thing is ugly but i don't see a way around it
// unless we put getDistanceToFood in this class which seems
// wrong. TBD: move the method here anyway.
if(!(g instanceof GeneticGrid)) return Integer.MAX_VALUE;
GeneticGrid gr = (GeneticGrid)g;
Location end = followPathFrom(colony.getLocation());
if(end == null) return Integer.MAX_VALUE;
if(!gr.isValid(end)) return Integer.MAX_VALUE;
return gr.getDistanceToFood(end);
}
public void setPath(String newPath)
{
path = newPath;
}
public String getPathWithMutations()
{
String mutatedPath = "";
if(path.length() < PATH_LENGTH && Math.random() < MUTATION_RATE)
path += (int)(Math.random()*4);
for(int i=0; i<path.length(); i++)
{
String c = path.substring(i,i+1);
if(Math.random() < MUTATION_RATE)
{
mutatedPath += (int)(Math.random()*4);
// a rare mutation causes shorter paths
if(Math.random() < MUTATION_RATE) break;
}
else
{
mutatedPath += c;
}
}
return mutatedPath;
}
public String randomPath()
{
String randPath = "";
for(int i=0; i<PATH_LENGTH; i++)
{
int r = (int)(Math.random()*4);
randPath += r;
}
return randPath;
}
public Location followPathFrom(Location start)
{
Grid gr = colony.getGrid();
Location end = new Location(start.getRow(), start.getCol());
for(int i=0; i<path.length(); i++)
{
int p = Integer.parseInt(path.substring(i,i+1));
end = end.getAdjacentLocation(p * 90);
if(!gr.isValid(end))
{
System.out.println("invalid path because " + end);
return null;
}
}
return end;
}
public String toString()
{
return "ANT (path "+path+")";
}
}