-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathant.py
151 lines (136 loc) · 4.39 KB
/
ant.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# encoding: utf-8
"""
ant.py
CS266 Ant Sim
"""
import sys, random
from param import G
from error import *
def randomDiscrete(choices, probs):
assert len(choices) == len(probs)
k = float(sum(probs))
norm_probs = [p/k for p in probs]
cumulatives = [sum(norm_probs[:i+1]) for i in range(len(norm_probs))]
x = random.random()
for i,choice in enumerate(choices):
if x < cumulatives[i]:
return choice
assert False
def getNeighbors((x,y)):
neighbors = []
if x > 0:
neighbors.append((x-1, y))
if x < G.numBlocksX-1:
neighbors.append((x+1, y))
if y > 0:
neighbors.append((x, y-1))
if y < G.numBlocksY-1:
neighbors.append((x, y+1))
return neighbors
class Ant(object):
def __init__(self, id):
self.id = id
#self.x = random.choice(range(G.numBlocksX))
#self.x = random.choice(range(G.numBlocksX/2-3, G.numBlocksX/2+3))
self.x = G.numBlocksX/2
self.y = 0
self.pos = (self.x, self.y) #purely for syntax simplicity
self.settled = False
self.supportMode = False
self.moveRight = random.choice([True,False])
if G.verbose:
print >> G.outfile, "ant %d is moving" % (self.id)
def move(self):
if (not self.supportMode):
neighbors = [self.pos]
if G.antSenseRadius == 1:
neighbors += getNeighbors(self.pos)
for neighbor in neighbors:
if (G.state[neighbor]) == G.SHAKING:
self.supportMode = True
self.supportDirection = random.choice([1.0,-1.0])
break
try:
if not G.state[self.pos]:
newCoord = self.pos
else:
if (self.supportMode):
if (G.supportAlgo == G.HORIZONTAL_SUPPORT):
newCoord = (self.x+self.supportDirection, self.y)
if not newCoord in getNeighbors(self.pos):
raise SimulationError("OutOfBoundsError", "Ant cannot move any further in desired direction")
elif (G.supportAlgo == G.NONUNIFORM_SUPPORT):
horizontalProb = 2.0
verticalProb = 1.0
neighbors = []
probs = []
x, y = self.pos
if self.supportDirection == -1.0 and x > 0:
neighbors.append((x-1, y))
probs.append(horizontalProb)
if self.supportDirection == 1.0 and x < G.numBlocksX-1:
neighbors.append((x+1, y))
probs.append(horizontalProb)
neighbors.append((x, y+1))
probs.append(verticalProb)
newCoord = randomDiscrete(neighbors, probs)
if not newCoord in getNeighbors(self.pos):
raise SimulationError("OutOfBoundsError", "Ant cannot move any further in desired direction")
elif (G.supportAlgo == G.RANDOM_SUPPORT):
neighbors = [n for n in getNeighbors(self.pos)]
newCoord = random.choice(neighbors)
else:
if (G.baseMoveAlgo == G.RANDOM_WALK):
horizontalProb = 1.0
verticalProb = 1.0
neighbors = []
probs = []
x, y = self.pos
if not self.moveRight and x > 0:
neighbors.append((x-1, y))
probs.append(horizontalProb)
if self.moveRight and x < G.numBlocksX-1:
neighbors.append((x+1, y))
probs.append(horizontalProb)
neighbors.append((x, y+1))
probs.append(verticalProb)
newCoord = randomDiscrete(neighbors, probs)
elif (G.baseMoveAlgo == G.STRAIGHT_DOWN):
x, y = self.pos
newCoord = (x, y+1)
elif (G.baseMoveAlgo == G.NONUNIFORM):
horizontalProb = 1.0
verticalProb = 3.0
neighbors = []
probs = []
x, y = self.pos
if not self.moveRight and x > 0:
neighbors.append((x-1, y))
probs.append(horizontalProb)
if self.moveRight and x < G.numBlocksX-1:
neighbors.append((x+1, y))
probs.append(horizontalProb)
neighbors.append((x, y+1))
probs.append(verticalProb)
newCoord = randomDiscrete(neighbors, probs)
if not G.state[newCoord]:
x, y = newCoord
try:
#while (y > 0 and (G.state[(x-1,y-1)] == G.NOANT and G.state[(x,y-1)] == G.NOANT and G.state[(x+1,y-1)] == G.NOANT)):
while (y > 0 and G.state[(x,y-1)] == G.NOANT):
if (G.state[(x-1,y)]):
newCoord = (x,y-1)
elif (G.state[(x+1,y)]):
newCoord = (x,y-1)
else:
raise WeirdError("There are no ants around us or above us, yet here we are!")
x, y = newCoord
except IndexError as e:
raise SimulationError("OutOfBoundsError", "Ant cannot move any further in desired direction")
G.state[newCoord] = G.NORMAL
self.settled = True
(self.x, self.y) = newCoord
self.pos = newCoord
except Error as e:
raise e