-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBeadyRing_env.py
More file actions
230 lines (182 loc) · 7.74 KB
/
BeadyRing_env.py
File metadata and controls
230 lines (182 loc) · 7.74 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
import random
from math import floor
import scriptcontext as sc
import ghpythonlib.components as ghcomp
import ghpythonlib.treehelpers as th
class BeadyRing_env:
def __init__(self):
self._carrier_color = 127.5
self._house_color = 0
self._street_color = 255
self._cell_size = 3
self._max_row_len = 21
self._obs_size = 9
self._3d = False
self.pad = int(floor(self._obs_size/2))
self.max_world_row_len = int(self._max_row_len + 2*self.pad)
self.iter = 0
# grid world
xy_plane = ghcomp.XYPlane(ghcomp.ConstructPoint(0, 0, 0))
cell, _ = ghcomp.Rectangle(xy_plane, self._cell_size, self._cell_size, 0)
r_max = self._cell_size * self.max_world_row_len
move_range = [i for i in range(0, r_max, self._cell_size)]
y_vec = ghcomp.UnitY(move_range)
cell_col, _ = ghcomp.Move(cell, y_vec)
x_vec = ghcomp.UnitX(move_range)
grid_world = []
for c in cell_col:
cell_row, _ = ghcomp.Move(c, x_vec)
grid_world.append(cell_row)
self.grid_world = ghcomp.ReverseList(grid_world)
# initial location
self.R = int(floor(self.max_world_row_len/2))
self.C = int(floor(self.max_world_row_len/2))
R_space = [r for r in range(int(self.pad), int(self._max_row_len + self.pad))]
C_space = [c for c in range(int(self.pad), int(self._max_row_len + self.pad))]
self.RC_space = [[[r, c] for c in C_space] for r in R_space]
self.x = self.R - self.pad
self.y = self.C - self.pad
self.cell = [[self.x, self.y], [self.R, self.C]]
self.adjacent_cells = [[[self.x, self.y], [self.R, self.C]]]
self.adj_cells = [[[self.x, self.y], [self.R, self.C]]]
# initial state
self.state = [[self._carrier_color for _ in range(int(self.max_world_row_len))]
for _ in range(int(self.max_world_row_len))]
# initial observation
self.observation = self.get_obs()
def step(self, _cell_state):
self.iter += 1
# update state step
self.state[self.R][self.C] = 255*_cell_state
for i, a in enumerate(self.adj_cells):
if a == self.cell:
del self.adj_cells[i]
adjacent = self.get_adjacent()
# reward
reward = 0
adj_street_count = 0
for a in adjacent:
if int(self.state[a[1][0]][a[1][1]]) == 255:
adj_street_count += 1
if _cell_state == 1:
if adj_street_count >= 3:
reward -= 1
elif adj_street_count == 0:
reward -= 1
elif 0 < adj_street_count < 3:
reward += 1
elif _cell_state == 0:
# density metric
reward += 1/(self._max_row_len**2)
if adj_street_count == 0:
reward -= 1
elif adj_street_count >= 3:
reward -= 1
elif 0 < adj_street_count < 3:
reward += 2
for item in adjacent:
if item not in self.adjacent_cells:
self.adjacent_cells.append(item)
self.adj_cells.append(item)
# done
done = False
if len(self.adj_cells) == 0:
done = True
elif len(self.adj_cells) > 0:
# next action location selection
self.cell = random.choice(self.adj_cells)
self.x = self.cell[0][0]
self.y = self.cell[0][1]
self.R = self.cell[1][0]
self.C = self.cell[1][1]
# observation
self.observation = self.get_obs()
return self.observation, reward, done, {}
def reset(self):
# initial state
self.state = [[self._carrier_color for _ in range(int(self.max_world_row_len))]
for _ in range(int(self.max_world_row_len))]
# reset step counter
self.iter = 0
# initial location
self.R = int(floor(self.max_world_row_len/2))
self.C = int(floor(self.max_world_row_len/2))
self.x = self.R - self.pad
self.y = self.C - self.pad
self.cell = [[self.x, self.y], [self.R, self.C]]
self.adjacent_cells = [[[self.x, self.y], [self.R, self.C]]]
self.adj_cells = [[[self.x, self.y], [self.R, self.C]]]
# initial observation
self.observation = self.get_obs()
return self.observation
def render(self):
# state visualization
color_state = [ghcomp.ColourRGB(255, self.state[i], self.state[i],
self.state[i]) for i in range(self.max_world_row_len)]
# observation grid
left_up_R = int(self.R - self.pad)
left_up_C = int(self.C - self.pad)
right_bottom_R = int(self.R + self.pad)
right_bottom_C = int(self.C + self.pad)
obs_grid_ = []
for i in range(left_up_R, right_bottom_R + 1):
obs_grid_.append(self.grid_world[i][left_up_C : right_bottom_C + 1])
return color_state, obs_grid_
def get_obs(self):
# observation
left_up_R = int(self.R - self.pad)
left_up_C = int(self.C - self.pad)
right_bottom_R = int(self.R + self.pad)
right_bottom_C = int(self.C + self.pad)
obs_ = []
for i in range(left_up_R, right_bottom_R + 1):
obs_row = []
for j in range(left_up_C, right_bottom_C + 1):
item = self.state[i][j]
obs_row.append(item)
obs_.append(obs_row)
return obs_
def get_adjacent(self):
# von Neumann neighbourhood
adjacent = []
if self.x < len(self.RC_space) - 1:
adjacent.append([[self.x+1, self.y], self.RC_space[self.x+1][self.y]])
if self.y > 0:
adjacent.append([[self.x, self.y-1], self.RC_space[self.x][self.y-1]])
if self.x > 0:
adjacent.append([[self.x-1, self.y], self.RC_space[self.x-1][self.y]])
if self.y < len(self.RC_space[self.x]) - 1:
adjacent.append([[self.x, self.y+1], self.RC_space[self.x][self.y+1]])
return adjacent
def get_house_cells(self):
## house cells
house_cells_ = []
for i in range(self.max_world_row_len):
for j in range(self.max_world_row_len):
if self.state[i][j] == 0:
house_cells_.append(self.grid_world[i][j])
return house_cells_
if 'env' not in globals():
env = BeadyRing_env()
sc.sticky['env'] = env
if reset:
observation = sc.sticky['env'].reset()
reward = None
done = False
info = {}
elif action is not None:
observation, reward, done, info = sc.sticky['env'].step(action)
else:
raise RuntimeError("Either reset or action must be provided")
if render:
grid_world_ = th.list_to_tree(sc.sticky['env'].grid_world, source=[0,0])
color_state, obs_grid = sc.sticky['env'].render()
color_state_ = th.list_to_tree(color_state, source=[0,0])
obs_grid_ = th.list_to_tree(obs_grid, source=[0,0])
if sc.sticky['env']._3d:
house_cells = sc.sticky['env'].get_house_cells()
house_cells_ = th.list_to_tree(house_cells, source=[0,0])
sc.sticky["observation"] = observation
sc.sticky["reward"] = reward
sc.sticky["done"] = done
sc.sticky["info"] = info