-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.py
355 lines (296 loc) · 11.1 KB
/
robot.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from math import sqrt
from random import choice
from operator import add
import sys
def dist(p1, p2):
""" manhattan distance """
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
def log(s):
pass
class Robot:
sightRadius = 3
def __init__(self, name, position=(0, 0)):
self.name = name
self.neighbors = []
self.world = None
self.position = position
self.brain = RobotBrain()
self.state = WanderingState()
self.points = 0 # robot gets a point if it pushes box into hole
def addNeighbor(self, neighbor):
self.neighbors.append(neighbor)
def getPeers(self):
return self.neighbors
def getPoints(self):
return self.points
def setWorld(self, world):
self.world = world
def lookAround(self):
boxes, noboxes = self.world.getBoxesNear(self.position,
self.sightRadius)
holes = self.world.getHolesNear(self.position, self.sightRadius)
self.brain.updateHoles(holes)
self.brain.updateBoxes(boxes, noboxes)
self.exchangeInfo()
def exchangeInfo(self):
for neighbor in self.neighbors:
neighbor.updateBrain(self.brain)
def updateBrain(self, otherBrain):
self.brain.updateAssignments(otherBrain)
def wander(self):
size = self.world.size
x, y = self.position
dx, dy = choice([(0, 1), (1, 0), (0, -1), (-1, 0)])
newx, newy = x + dx, y + dy
while not (0 <= newx < size and 0 <= newy < size):
dx, dy = choice([(0, 1), (1, 0), (0, -1), (-1, 0)])
newx, newy = x + dx, y + dy
self.moveTo(newx, newy)
def moveTo(self, x, y):
size = self.world.size
if 0 <= x < size and 0 <= y < size:
self.position = (x, y)
else:
assert False, '%s tried to move to (%d, %d)' %(self.name, x, y)
def act(self):
self.brain.updateCachedChanges()
self.state.act(self)
def printBeliefs(self, stream=sys.stdout):
boxes = self.brain.boxes
holes = self.brain.holes
stream.write('robot %s\nboxes\n' %(self.name,))
for box, agent in boxes.items():
stream.write('\t%s assigned to %s\n' %(
box, agent is None and 'nobody' or agent.name))
stream.write('holes\n')
for hole, agent in holes.items():
stream.write('\t%s assigned to %s\n' %(
hole, agent is None and 'nobody' or agent.name))
stream.flush()
class MovingToBoxState:
def __init__(self, box):
self.destination = box
def act(self, robot):
x, y = robot.position
xdest, ydest = self.destination
if abs(x - xdest) == 0 and abs(y - ydest) == 0:
if robot.world.isBoxAt(x, y):
log('%s in MovingBoxState' %(robot.name,))
robot.state = MovingBoxState()
else:
log('%s in WanderingState' %(robot.name,))
robot.state = WanderingState()
elif abs(x - xdest) == 0:
dy = y > ydest and -1 or 1
robot.moveTo(x, y + dy)
else:
dx = x > xdest and -1 or 1
robot.moveTo(x + dx, y)
class MovingBoxState:
def __init__(self):
self.hole = None
def act(self, robot):
if not robot.world.isBoxAt(robot.position):
log('%s in WanderingState' %(robot.name,))
robot.state = WanderingState()
return
if self.hole is None: # find closest unassigned hole
holes = [ (hole, dist(robot.position, hole))
for hole in robot.brain.getUnassignedHoles() ]
if not holes:
oldbox = robot.position
robot.wander()
robot.points += robot.world.moveBox(oldbox, robot.position)
return
holes.sort(lambda x, y: cmp(x[1], y[1]))
myHole = holes[0][0]
self.hole = myHole
robot.brain.assignHole(myHole, robot)
robot.exchangeInfo()
else: # move box towards hole
x, y = robot.position
xdest, ydest = self.hole
if abs(x - xdest) == 0 and abs(y - ydest) == 0:
log('%s in WanderingState' %(robot.name,))
robot.state = WanderingState()
return
elif abs(x - xdest) == 0:
dy = y > ydest and -1 or 1
dx = 0
else:
dy = 0
dx = x > xdest and -1 or 1
robot.moveTo(x + dx, y + dy)
robot.points += robot.world.moveBox((x, y), robot.position)
class WanderingState:
def act(self, robot):
# select closest unassigned box
boxes = [ (box, dist(robot.position, box))
for box in robot.brain.getUnassignedBoxes() ]
if not boxes:
robot.wander()
return
boxes.sort(lambda x, y: cmp(x[1], y[1]))
myBox, distance = boxes[0][0], boxes[0][1]
robot.brain.assignBox(myBox, robot)
if distance == 0:
log('%s in MovingBoxState' %(robot.name,))
robot.state = MovingBoxState()
else:
log('%s in MovingToBoxState' %(robot.name,))
robot.state = MovingToBoxState(myBox)
robot.exchangeInfo()
class RobotBrain:
def __init__(self):
self.boxes = {} # map boxes -> assigned robots
self.holes = {}
self.newHoles = {}
self.newBoxes = {}
self.boxesToDelete = []
def getUpdatedItems(self, existingItems, newItems):
toUpdate = {}
for item in newItems:
if item not in existingItems.keys():
toUpdate[item] = None
return toUpdate
def updateBoxes(self, boxes, noboxes):
newBoxes = self.getUpdatedItems(self.boxes, boxes)
self.newBoxes = newBoxes
# delete moved boxes
boxesToDelete = []
for nobox in noboxes:
if self.boxes.has_key(nobox):
boxesToDelete.append(nobox)
self.boxesToDelete = boxesToDelete
def updateHoles(self, holes):
self.newHoles = self.getUpdatedItems(self.holes, holes)
def updateCachedChanges(self):
self.boxes.update(self.newBoxes)
self.holes.update(self.newHoles)
for box in self.boxesToDelete:
del self.boxes[box]
def updateAssignments(self, brain):
for box, robot in brain.boxes.items():
if not self.boxes.has_key(box) or self.boxes[box] is None:
self.boxes[box] = robot
for hole, robot in brain.holes.items():
if not self.holes.has_key(hole) or self.holes[hole] is None:
self.holes[hole] = robot
def isBoxAssigned(self, box):
return self.boxes[box] is not None
def isHoleAssigned(self, hole):
return self.holes[hole] is not None
def assignHole(self, hole, robot):
self.holes[hole] = robot
def assignBox(self, box, robot):
self.boxes[box] = robot
def getUnassignedBoxes(self):
return [box for box in self.boxes.keys()
if not self.isBoxAssigned(box) ]
def getUnassignedHoles(self):
return [hole for hole in self.holes.keys()
if not self.isHoleAssigned(hole) ]
class Dummy:
pass
_marker = Dummy()
class World:
def __init__(self, size, robots, boxes, holes):
self.size = size
self.robots = robots
self.boxes = boxes
self.holes = holes
def update(self):
robots = self.robots
for robot in robots:
robot.lookAround()
for robot in robots:
robot.act()
def getItemsNear(self, isItemAt, position, radius):
x_robot, y_robot = position
size = self.size
items, empty = [], []
# figure bounds for looking at stuff (rectangular universe)
x1, x2 = (x_robot - radius), (x_robot + radius)
y1, y2 = (y_robot - radius), (y_robot + radius)
xmax, ymax = min(max(x1, x2), size - 1), min(max(y1, y2), size - 1)
xmin, ymin = max(min(x1, x2), 0), max(min(y1, y2), 0)
for x in range(xmin, xmax + 1):
for y in range(ymin, ymax + 1):
if dist(position, (x,y)) < radius:
if isItemAt(x, y):
items.append((x, y))
else:
empty.append((x, y))
return items, empty
def isBoxAt(self, x, y=_marker):
if y is _marker:
return x in self.boxes
else:
return (x, y) in self.boxes
def isHoleAt(self, x, y=_marker):
if y is _marker:
return x in self.holes
else:
return (x, y) in self.holes
def getHolesNear(self, position, radius):
return self.getItemsNear(self.isHoleAt, position, radius)[0]
def getBoxesNear(self, position, radius):
# boxes move, so return empty squares too
return self.getItemsNear(self.isBoxAt, position, radius)
def moveBox(self, old, new):
""" return 1 if a box was pushed into a hole"""
self.boxes.remove(old)
if new in self.holes:
self.holes.remove(new)
return 1
else:
self.boxes.append(new)
return 0
def getNumberOfHoles(self):
return len(self.holes)
def getNumberOfBoxes(self):
return len(self.boxes)
def printGrid(self):
robotPositions = {}
for r in self.robots:
robotPositions[r.position] = (r, r.name[0])
for i in range(self.size):
for j in range(self.size):
if (i, j) in self.boxes:
print '*',
elif (i, j) in self.holes:
print '0',
elif (i, j) in robotPositions.keys():
robot, initial = robotPositions[(i, j)]
if isinstance(robot.state, WanderingState):
initial = initial.lower()
else:
initial = initial.upper()
print '%s' %(initial,),
else:
print '.',
print ''
def mean(list):
return reduce(add, list)/float(len(list))
def stdev(list):
from math import sqrt
return sqrt(mean(map(lambda x: x**2, list)) - mean(list)**2)
if __name__ == '__main__':
import sys
#sys.exit(main(sys.argv))
nRobots, size = 4, 15
#world, robots = initFullyConnectedWorld(nRobots, size)
robots = [Robot('1', (0, 0)), Robot('2', (9, 9))]
world = World(10, robots, [(3, 6), (7, 4)], [(8, 1), (4, 9)])
robots[0].setWorld(world)
robots[1].setWorld(world)
robots[0].addNeighbor(robots[1])
robots[1].addNeighbor(robots[0])
while world.getNumberOfHoles() > 0:
world.printGrid()
world.update()
for robot in robots:
robot.printBeliefs()
print '-'*size*2
raw_input()
world.printGrid()