-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridworld.py
More file actions
297 lines (227 loc) · 8.7 KB
/
gridworld.py
File metadata and controls
297 lines (227 loc) · 8.7 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
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
from base import *
from representation import *
import random
from copy import deepcopy
class GridAgentData(AgentData):
def __init__(self, linked_agent, grid_position, current_orientation):
"""
:param linked_agent: the agent
:param grid_position: the position on the grid
:param current_orientation: the orientation on the grid
"""
super(GridAgentData, self).__init__(linked_agent)
self.grid_position = grid_position
self.current_orientation = current_orientation
"""
The number of points gathered by the agent
"""
self.points = 0
def add_points(self, delta):
"""
:param delta: number of points to add; may be negative
"""
self.points += delta
class NearbyAgent(object):
"""
Structure storing information about a nearby agent.
"""
def __init__(self, relative_orientation, is_cognitive, agent_points):
"""
Creates a new structure indicating information about a nearby agent.
:param relative_orientation: The GridRelativeOrientation orientation relative to `this' agent
:param is_cognitive: is the neighbour agent cognitive?
:param agent_points: the points gathered by the neighbour agent
"""
self.relative_orientation = relative_orientation
self.is_cognitive = is_cognitive
self.agent_points = agent_points
class AbstractGridEnvironment(Environment):
def __init__(self):
super(AbstractGridEnvironment, self).__init__()
""" A `GridPosition' record of all positions on the map, such as to provide reference """
self._grid_positions = []
""" Set of junk tile `GridPosition' positions """
self._goals = []
""" Set of wall tile `GridPosition' positions """
self._xtiles = []
""" Min and max values for the corners of the grid environment """
self._x0 = 0
self._x1 = 0
self._y0 = 0
self._y1 = 0
""" Width and height of grid cells for display purposes """
self._cellW = 2
self._cellH = 2
""" Set of `GridAgentData' agents populating the environment """
self._agents = []
def goals_completed(self):
return not self._goals
def add_agent(self, agent_data):
self._agents.append(agent_data)
def initialize(self, w, h, nr_goals, nr_xtiles, rand_seed=None):
"""
Initializes the environment with the provided width, height and number of goal- and X-tiles.
:param w: width
:param h: height
:param nr_goals: number of goal tiles
:param nr_xtiles: number of x tiles
:param rand_seed: random number generator seed used in generation process, can be None
"""
for i in range(0, w + 2):
for j in range(0, h + 2):
self._grid_positions.append(GridPosition(i, j))
self._x0 = 0
self._x1 = w + 1
self._y0 = 0
self._y1 = h + 1
for i in range(0, w + 2):
self._xtiles.append(GridPosition(i, 0))
self._xtiles.append((GridPosition(i, self._y1)))
for j in range(0, h + 2):
self._xtiles.append(GridPosition(0, j))
self._xtiles.append((GridPosition(self._x1, j)))
## generate rest of X tiles
attempts = 10 * nr_xtiles * nr_xtiles
generated = 0
if rand_seed:
random.seed(rand_seed)
while attempts > 0 and generated < nr_xtiles:
x = random.randint(1, w)
y = random.randint(1, h)
pos = GridPosition(x, y)
ok = True
# tiles must be at a manhattan distance of 1 from each other to avoid isolated goals
for grid_pos in self._xtiles:
if pos.get_distance_to(grid_pos) <= 1:
ok = False
if ok:
generated += 1
self._xtiles.append(pos)
attempts -= 1
if generated < nr_xtiles:
print("Failed to generate all required X-tiles. Wanted: %i, generated: %i" % (nr_xtiles, generated))
## generate all goal tiles
attempts = 10 * nr_goals * nr_goals
generated = 0
while attempts > 0 and generated < nr_goals:
x = random.randint(1, w)
y = random.randint(1, h)
pos = GridPosition(x, y)
if not pos in self._goals and not pos in self._xtiles:
generated += 1
self._goals.append(pos)
attempts -= 1
if generated < nr_goals:
print("Failed to generate all required goal-tiles. Wanted: %i, generated: %i" % (nr_goals, generated))
def clean_tile(self, grid_position):
"""
Removes a position from the list of tiles.
:param grid_position: the goal tile to remove
"""
if not grid_position in self._goals:
raise ValueError("GridPosition was not in goals!")
self._goals.remove(grid_position)
def _get_positions(self):
return deepcopy(self._grid_positions)
def _get_x_tiles(self):
return deepcopy(self._xtiles)
def _get_goal_tiles(self):
return deepcopy(self._goals)
def get_bottom_left(self):
return GridPosition(self._x0 + 1, self._y0 + 1)
def get_top_left(self):
return GridPosition(self._x0 + 1, self._y1 - 1)
def get_bottom_right(self):
return GridPosition(self._x1 - 1, self._y0 + 1)
def get_top_right(self):
return GridPosition(self._x1 - 1, self._y1 - 1)
def __str__(self):
res = ""
res += " |"
## border top
for i in range(self._x0, self._x1 + 1):
step = 1
if i >= 10:
step = 2
for k in range(0, self._cellW - step):
res += " "
res += str(i) + "|"
res += "\n"
res += "--+"
for i in range(self._x0, self._x1 + 1):
for k in range(0, self._cellW):
res += "-"
res += "+"
res += "\n"
## for each line
for j in range(self._y1, self._y0 - 1, -1):
# first cell row
if j < 10:
res += " " + str(j) + "|"
else:
res += str(j) + "|"
for i in range(self._x0, self._x1 + 1):
pos = GridPosition(i, j)
agent_string = ""
for agent_data in self._agents:
if agent_data.grid_position == pos:
agent_string += str(agent_data.current_orientation) + str(agent_data.linked_agent)
k = 0
if pos in self._xtiles:
while k < self._cellW:
res += "X"
k += 1
if self._cellH < 2 and pos in self._goals:
res += "~"
k += 1
if len(agent_string) > 0:
if self._cellW == 1:
if len(agent_string) > 1:
res += "."
else:
res += agent_string
k += 1
else:
res += agent_string[:min(len(agent_string), self._cellW - k)]
k += min(len(agent_string), self._cellW - k)
while k < self._cellW:
res += " "
k += 1
res += "|"
res += "\n"
# second cell row
res += " |"
for i in range(self._x0, self._x1 + 1):
pos = GridPosition(i, j)
for k in range(0, self._cellW):
if pos in self._xtiles:
res += "X"
elif pos in self._goals:
res += "~"
else:
res += " "
res += "|"
res += "\n"
# other cell rows
for ky in range(0, self._cellH - 2):
res += "|"
for i in range(self._x0, self._x1 + 1):
for k in range(0, self._cellW):
if GridPosition(i, j) in self._xtiles:
res += "X"
else:
res += " "
res += "|"
res += "\n"
res += "--+"
for i in range(self._x0, self._x1 + 1):
for k in range(0, self._cellW):
res += "-"
res += "+"
res += "\n"
return res
# if __name__ == "__main__":
# env = AbstractGridEnvironment()
# env.initialize(10, 10, 3, 3)
#
# print(env)