-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathswimmer.py
291 lines (245 loc) · 9.85 KB
/
swimmer.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
"""
Path finding task.
A fish needs to reach a goal location while avoiding urchins.
"""
import ivy
import gym
import numpy as np
# Environment Class #
# ------------------#
# noinspection PyAttributeOutsideInit
class Swimmer(gym.Env):
metadata = {"render.modes": ["human", "rgb_array"], "video.frames_per_second": 30}
def __init__(self, num_urchins=5): # noqa
"""
Initialize Swimmer environment.
Parameters
----------
num_urchins
Number of urchins.
"""
self.num_urchins = num_urchins
self.dt = 0.05
self.action_space = gym.spaces.Box(low=-1, high=1, shape=[2], dtype=np.float32)
high = np.array([np.inf] * (num_urchins * 2 + 6), dtype=np.float32)
self.observation_space = gym.spaces.Box(low=-high, high=high, dtype=np.float32)
self.viewer = None
self._logged_headless_message = False
def get_observation(self):
"""Get observation from environment.
Returns
-------
ret
observation array
"""
ob = (
ivy.reshape(self.urchin_xys, (-1, 2)),
ivy.reshape(self.xy, (-1, 2)),
ivy.reshape(self.xy_vel, (-1, 2)),
ivy.reshape(self.goal_xy, (-1, 2)),
)
ob = ivy.concat(ob, axis=0)
return ivy.reshape(ob, (-1,))
def get_reward(self):
"""Get reward based on current state
Returns
-------
ret
Reward array
"""
# Goal proximity.
rew = ivy.exp(-0.5 * ivy.sum((self.xy - self.goal_xy) ** 2, axis=-1))
# Urchins proximity.
rew = rew * ivy.prod(
1 - ivy.exp(-30 * ivy.sum((self.xy - self.urchin_xys) ** 2, axis=-1)),
axis=-1,
)
return ivy.reshape(rew, (1,))
def get_state(self):
"""Get current state in environment.
Returns
-------
ret
Urchin xys, xy, xy velocity, and goal xy arrays
"""
return self.urchin_xys, self.xy, self.xy_vel, self.goal_xy
def set_state(self, state):
"""Set current state in environment.
Parameters
----------
state
tuple of urchin xys, xy, xy velocity, and goal xy
Returns
-------
ret
observation array
"""
self.urchin_xys, self.xy, self.xy_vel, self.goal_xy = state
return self.get_observation()
def reset(self):
self.urchin_xys = ivy.random_uniform(
low=-1, high=1, shape=(self.num_urchins, 2)
)
self.xy = ivy.random_uniform(low=-1, high=1, shape=(2,))
self.xy_vel = ivy.zeros((2,))
self.goal_xy = ivy.random_uniform(low=-1, high=1, shape=(2,))
return self.get_observation()
def step(self, action):
"""
Parameters
----------
action
"""
self.xy_vel = self.xy_vel + self.dt * action
self.xy = self.xy + self.dt * self.xy_vel
return self.get_observation(), self.get_reward(), False, {}
def render(self, mode="human"):
"""Renders the environment.
The set of supported modes varies per environment. (And some
environments do not support rendering at all.) By convention,
if mode is:
- human: render to the current display or terminal and
return nothing. Usually for human consumption.
- rgb_array: Return an numpy.ndarray with shape (x, y, 3),
representing RGB values for an x-by-y pixel image, suitable
for turning into a video.
- ansi: Return a string (str) or StringIO.StringIO containing a
terminal-style text representation. The text can include newlines
and ANSI escape sequences (e.g. for colors).
Parameters
----------
mode
Render mode, one of [human|rgb_array], default human
Returns
-------
ret
Rendered image.
"""
if self.viewer is None:
# noinspection PyBroadException
try:
from gym.envs.classic_control import rendering
except Exception:
if not self._logged_headless_message:
print(
"Unable to connect to display. Running the Ivy environment "
"in headless mode..."
)
self._logged_headless_message = True
return
from pyglet import gl
class _StarGeom(rendering.Geom):
def __init__(self, r1, r2, n):
super().__init__()
self.r1 = r1
self.r2 = r2
self.n = n
def render1(self):
n = self.n * 2
for i in range(0, n, 2):
gl.glBegin(gl.GL_TRIANGLES)
a0 = 2 * np.pi * i / n
a1 = 2 * np.pi * (i + 1) / n
a2 = 2 * np.pi * (i - 1) / n
gl.glVertex3f(np.cos(a0) * self.r1, np.sin(a0) * self.r1, 0)
gl.glVertex3f(np.cos(a1) * self.r2, np.sin(a1) * self.r2, 0)
gl.glVertex3f(np.cos(a2) * self.r2, np.sin(a2) * self.r2, 0)
gl.glEnd()
gl.glBegin(gl.GL_POLYGON)
for i in range(0, n, 2):
a = 2 * np.pi * (i + 1) / n
gl.glVertex3f(np.cos(a) * self.r2, np.sin(a) * self.r2, 0)
gl.glEnd()
class _FishGeom(rendering.Geom):
def __init__(self):
super().__init__()
self.color = 0.0, 0.0, 0.0
def render1(self):
points = [
[0.08910714285714288, -0.009017857142857133],
[0.13910714285714287, -0.04026785714285712],
[0.12285714285714289, 0.07098214285714288],
[0.08535714285714285, 0.03348214285714288],
[0.10535714285714287, 0.07848214285714286],
[0.04910714285714285, 0.13348214285714285],
[-0.03589285714285714, 0.11723214285714287],
[-0.14964285714285713, 0.08598214285714287],
[-0.21714285714285714, 0.023482142857142868],
[-0.18589285714285714, -0.004017857142857129],
[-0.12714285714285714, -0.11151785714285713],
[-0.039642857142857146, -0.15651785714285713],
[0.044107142857142845, -0.15651785714285713],
[0.12035714285714288, -0.06526785714285713],
]
gl.glColor3f(*self.color)
gl.glBegin(gl.GL_POLYGON)
for p0, p1 in points:
gl.glVertex3f(p0, -p1, 0)
gl.glEnd()
points = [
[-0.14964285714285713, -0.016517857142857112],
[-0.11214285714285714, 0.020982142857142866],
[-0.15839285714285714, 0.06973214285714288],
[-0.17089285714285712, 0.013482142857142887],
]
gl.glColor3f(0.5, 0.4, 0.3)
gl.glBegin(gl.GL_POLYGON)
for p0, p1 in points:
gl.glVertex3f(p0, -p1, 0)
gl.glEnd()
points = []
for i in range(20):
ang = 2 * np.pi * i / 20
points.append(
(np.cos(ang) * 0.018 - 0.16, np.sin(ang) * 0.018 - 0.01)
)
gl.glColor3f(0, 0, 0)
gl.glBegin(gl.GL_POLYGON)
for p0, p1 in points:
gl.glVertex3f(p0, p1, 0)
gl.glEnd()
def set_color(self, r, g, b):
"""
Parameters
----------
r
g
b
"""
self.color = r, g, b
self.viewer = rendering.Viewer(500, 500)
self.viewer.set_bounds(-1.5, 1.5, -1.5, 1.5)
# Goal.
goal_geom = rendering.make_circle(0.2)
self.goal_tr = rendering.Transform()
goal_geom.add_attr(self.goal_tr)
goal_geom.set_color(0.4, 0.6, 1.0)
self.viewer.add_geom(goal_geom)
# Urchins.
self.urchin_trs = []
for _ in range(self.num_urchins):
urchin_geom = _StarGeom(0.2, 0.05, 15)
urchin_tr = rendering.Transform()
self.urchin_trs.append(urchin_tr)
urchin_geom.add_attr(urchin_tr)
urchin_geom.set_color(0.0, 0.0, 0.0)
self.viewer.add_geom(urchin_geom)
# Fish.
self.fish_geom = _FishGeom()
self.fish_tr = rendering.Transform()
self.fish_geom.add_attr(self.fish_tr)
self.viewer.add_geom(self.fish_geom)
self.goal_tr.set_translation(*ivy.to_numpy(self.goal_xy).tolist())
for urchin_tr, (x, y) in zip(
self.urchin_trs, ivy.reshape(self.urchin_xys, (5, 2, 1))
):
urchin_tr.set_translation(ivy.to_numpy(x)[0], ivy.to_numpy(y)[0])
self.fish_tr.set_translation(*ivy.to_numpy(ivy.reshape(self.xy, (2,))).tolist())
rew = ivy.to_numpy(self.get_reward())[0]
self.fish_geom.set_color(1 - rew, rew, 0.0)
return self.viewer.render(return_rgb_array=mode == "rgb_array")
def close(self):
"""Close environment."""
if self.viewer:
self.viewer.close()
self.viewer = None