This repository has been archived by the owner on Nov 12, 2019. It is now read-only.
forked from Prestophobia/CS-5820-Group-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVacuumCleanerWorld.py
299 lines (245 loc) · 8.99 KB
/
VacuumCleanerWorld.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
import random
CLEAN = 0
DIRTY = 1
WALL = 2
CCW = 0
CW = 1
NORTH = (0,-1)
NORTHEAST = (1,-1)
EAST = (1,0)
SOUTHEAST = (1,1)
SOUTH = (0,1)
SOUTHWEST = (-1,1)
WEST = (-1,0)
NORTHWEST = (-1,-1)
UP = NORTH
DOWN = SOUTH
LEFT = WEST
RIGHT = EAST
def RotateDirVec45Deg(dirVec,rotation):
if rotation == CW:
if dirVec == NORTH:
return NORTHEAST
elif dirVec == NORTHEAST:
return EAST
elif dirVec == EAST:
return SOUTHEAST
elif dirVec == SOUTHEAST:
return SOUTH
elif dirVec == SOUTH:
return SOUTHWEST
elif dirVec == SOUTHWEST:
return WEST
elif dirVec == WEST:
return NORTHWEST
elif dirVec == NORTHWEST:
return NORTH
elif rotation == CCW:
if dirVec == NORTH:
return NORTHWEST
elif dirVec == NORTHWEST:
return WEST
elif dirVec == WEST:
return SOUTHWEST
elif dirVec == SOUTHWEST:
return SOUTH
elif dirVec == SOUTH:
return SOUTHEAST
elif dirVec == SOUTHEAST:
return EAST
elif dirVec == EAST:
return NORTHEAST
elif dirVec == NORTHEAST:
return NORTH
def RotateDirVec90Deg(dirVec,rotation):
return RotateDirVec45Deg(RotateDirVec45Deg(dirVec,rotation),rotation)
def RotateDirVec180Deg(dirVec,rotation):
return RotateDirVec90Deg(RotateDirVec90Deg(dirVec,rotation),rotation)
print_rotations = False
class Environment:
Grid = []
Width = 0
Height = 0
NumCollisions = 0
NumTurns = 0
InitialDirtyAmount = 0
def __init__(self,width,height):
self.Width = width
self.Height = height
self.Grid = []
for x in range(width):
newCol = []
for y in range(height):
newCol.append(CLEAN)
self.Grid.append(newCol)
def GetTile(self,x,y):
if x >= self.Width or y >= self.Height or x < 0 or y < 0:
return WALL
else:
return self.Grid[x][y]
def SetTile(self,x,y,val):
self.Grid[x][y] = val
def Collide(self,pos):
self.NumTurns += 1
if pos[0] >= self.Width or pos[1] >= self.Height:
self.NumCollisions += 1
return True
elif self.GetTile(pos[0],pos[1]) == WALL:
self.NumCollisions += 1
return True
else:
return False
def CountDirty(self):
numDirty = 0
for x in range(self.Width):
for y in range(self.Height):
if self.GetTile(x,y) == DIRTY:
numDirty += 1
return numDirty
def GetPercentClean(self):
return (1-(self.CountDirty()/self.InitialDirtyAmount)) * 100
def RandomizeWithoutWalls(self):
for x in range(self.Width):
for y in range(self.Height):
self.SetTile(x,y,random.randint(CLEAN,DIRTY))
self.InitialDirtyAmount = self.CountDirty()
def RandomizeWithWalls(self):
for x in range(self.Width):
for y in range(self.Height):
self.SetTile(x,y,random.randint(CLEAN,WALL))
self.InitialDirtyAmount = self.CountDirty()
def Visualize(self):
print("visualizing w:{}, h:{}".format(self.Width,self.Height))
for y in range (self.Height):
for x in range (self.Width):
print("[{}] ".format(self.GetTile(x,y)),end="")
print("")
print("Collisions: {}, Steps: {}, Percent Cleaned: {}%".format(self.NumCollisions, self.NumTurns,self.GetPercentClean()))
return
class Agent:
Status = CLEAN
FacingTile = CLEAN
Position = (0,0)
DirFacingVec = NORTH
Environ = Environment(2,1)
def __init__(self, startingPos, startingDir, environ):
self.Position = startingPos
self.DirFacingVec = startingDir
self.Environ = environ
def GetPercept(self):
self.Status = self.Environ.GetTile(x=self.Position[0],y=self.Position[1])
self.FacingTile = self.Environ.GetTile(self.Position[0] + self.DirFacingVec[0],self.Position[1] + self.DirFacingVec[1])
def Rotate(self, dir):
self.DirFacingVec = RotateDirVec45Deg(self.DirFacingVec,dir)
if print_rotations:
if dir == CW:
print("agent rotated 45 degrees clockwise")
elif dir == CCW:
print("agent rotated 45 degrees counterclockwise")
print("agent direction: x:{} y:{}".format(self.DirFacingVec[0],self.DirFacingVec[1]))
def MoveForward(self):
newX = self.Position[0] + self.DirFacingVec[0]
newY = self.Position[1] + self.DirFacingVec[1]
if not self.Environ.Collide((newX,newY)):
self.Position = (self.Position[0] + self.DirFacingVec[0],self.Position[1] + self.DirFacingVec[1])
print("agent position: x:{} y:{}".format(self.Position[0],self.Position[1]))
return True
else:
print("agent collided")
return False
def CleanTile(self):
self.Environ.SetTile(self.Position[0],self.Position[1],CLEAN)
return
def Run(self):
return
class SimpleReflexAgent(Agent):
def __init__(self, startingPos, startingDir, environ):
super().__init__(startingPos, startingDir, environ)
def Run(self):
running = True
print("agent starting position: x:{} y:{}".format(self.Position[0],self.Position[1]))
print("agent starting direction: x:{} y:{}".format(self.DirFacingVec[0],self.DirFacingVec[1]))
while running:
self.GetPercept()
if self.Status == DIRTY:
self.CleanTile()
if self.FacingTile == DIRTY:
self.MoveForward()
else:
self.Rotate(CW) #45 deg
self.GetPercept()
if self.FacingTile == DIRTY:
self.MoveForward()
else:
self.Rotate(CW) #90 deg
self.GetPercept()
if self.FacingTile == DIRTY:
self.MoveForward()
else:
self.Rotate(CW) #135 deg
self.GetPercept()
if self.FacingTile == DIRTY:
self.MoveForward()
else:
self.Rotate(CW) #180 deg
self.GetPercept()
if self.FacingTile == DIRTY:
self.MoveForward()
else:
self.Rotate(CW) #225 deg
self.GetPercept()
if self.FacingTile == DIRTY:
self.MoveForward()
else:
self.Rotate(CW) #270 deg
self.GetPercept()
if self.FacingTile == DIRTY:
self.MoveForward()
else:
self.Rotate(CW) #315 deg
self.GetPercept()
if self.FacingTile == DIRTY:
self.MoveForward()
else:
running = False
return
class ZigZagVacuum(Agent):
def __init__(self, startingPos, startingDir, environ):
super().__init__(startingPos, startingDir, environ)
def Run(self):
steps = 0
running = True
print("agent starting position: x:{} y:{}".format(self.Position[0],self.Position[1]))
print("agent starting direction: x:{} y:{}".format(self.DirFacingVec[0],self.DirFacingVec[1]))
while running and steps < 100:
steps += 1
self.GetPercept() # >
if self.Status == DIRTY:
self.CleanTile()
if self.FacingTile == WALL: # >
rotationDir = CW
if self.DirFacingVec == WEST:
rotationDir = CCW
self.Rotate(rotationDir)
self.Rotate(rotationDir) # V
self.GetPercept()
if self.FacingTile == WALL:
running = False
else:
self.MoveForward()
self.Rotate(rotationDir)
self.Rotate(rotationDir) # <
else:
self.MoveForward()
def main():
vacuumWorld = Environment(10,10)
vacuumWorld.RandomizeWithoutWalls()
vacuumWorld.Visualize()
print("")
reflexAgent = SimpleReflexAgent((0,0),EAST,vacuumWorld)
reflexAgent.Run()
print("")
vacuumWorld.Visualize()
print("done")
if __name__ == "__main__":
main()