-
Notifications
You must be signed in to change notification settings - Fork 166
/
env.py
159 lines (135 loc) · 6.61 KB
/
env.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
import numpy as np
import pyglet
class ArmEnv(object):
viewer = None
dt = .1 # refresh rate
action_bound = [-1, 1]
goal = {'x': 100., 'y': 100., 'l': 40}
state_dim = 9
action_dim = 2
def __init__(self):
self.arm_info = np.zeros(
2, dtype=[('l', np.float32), ('r', np.float32)])
self.arm_info['l'] = 100 # 2 arms length
self.arm_info['r'] = np.pi/6 # 2 angles information
self.on_goal = 0
def step(self, action):
done = False
action = np.clip(action, *self.action_bound)
self.arm_info['r'] += action * self.dt
self.arm_info['r'] %= np.pi * 2 # normalize
(a1l, a2l) = self.arm_info['l'] # radius, arm length
(a1r, a2r) = self.arm_info['r'] # radian, angle
a1xy = np.array([200., 200.]) # a1 start (x0, y0)
a1xy_ = np.array([np.cos(a1r), np.sin(a1r)]) * a1l + a1xy # a1 end and a2 start (x1, y1)
finger = np.array([np.cos(a1r + a2r), np.sin(a1r + a2r)]) * a2l + a1xy_ # a2 end (x2, y2)
# normalize features
dist1 = [(self.goal['x'] - a1xy_[0]) / 400, (self.goal['y'] - a1xy_[1]) / 400]
dist2 = [(self.goal['x'] - finger[0]) / 400, (self.goal['y'] - finger[1]) / 400]
r = -np.sqrt(dist2[0]**2+dist2[1]**2)
# done and reward
if self.goal['x'] - self.goal['l']/2 < finger[0] < self.goal['x'] + self.goal['l']/2:
if self.goal['y'] - self.goal['l']/2 < finger[1] < self.goal['y'] + self.goal['l']/2:
r += 1.
self.on_goal += 1
if self.on_goal > 50:
done = True
else:
self.on_goal = 0
# state
s = np.concatenate((a1xy_/200, finger/200, dist1 + dist2, [1. if self.on_goal else 0.]))
return s, r, done
def reset(self):
self.goal['x'] = np.random.rand()*400.
self.goal['y'] = np.random.rand()*400.
self.arm_info['r'] = 2 * np.pi * np.random.rand(2)
self.on_goal = 0
(a1l, a2l) = self.arm_info['l'] # radius, arm length
(a1r, a2r) = self.arm_info['r'] # radian, angle
a1xy = np.array([200., 200.]) # a1 start (x0, y0)
a1xy_ = np.array([np.cos(a1r), np.sin(a1r)]) * a1l + a1xy # a1 end and a2 start (x1, y1)
finger = np.array([np.cos(a1r + a2r), np.sin(a1r + a2r)]) * a2l + a1xy_ # a2 end (x2, y2)
# normalize features
dist1 = [(self.goal['x'] - a1xy_[0])/400, (self.goal['y'] - a1xy_[1])/400]
dist2 = [(self.goal['x'] - finger[0])/400, (self.goal['y'] - finger[1])/400]
# state
s = np.concatenate((a1xy_/200, finger/200, dist1 + dist2, [1. if self.on_goal else 0.]))
return s
def render(self):
if self.viewer is None:
self.viewer = Viewer(self.arm_info, self.goal)
self.viewer.render()
def sample_action(self):
return np.random.rand(2)-0.5 # two radians
class Viewer(pyglet.window.Window):
bar_thc = 5
def __init__(self, arm_info, goal):
# vsync=False to not use the monitor FPS, we can speed up training
super(Viewer, self).__init__(width=400, height=400, resizable=False, caption='Arm', vsync=False)
pyglet.gl.glClearColor(1, 1, 1, 1)
self.arm_info = arm_info
self.goal_info = goal
self.center_coord = np.array([200, 200])
self.batch = pyglet.graphics.Batch() # display whole batch at once
self.goal = self.batch.add(
4, pyglet.gl.GL_QUADS, None, # 4 corners
('v2f', [goal['x'] - goal['l'] / 2, goal['y'] - goal['l'] / 2, # location
goal['x'] - goal['l'] / 2, goal['y'] + goal['l'] / 2,
goal['x'] + goal['l'] / 2, goal['y'] + goal['l'] / 2,
goal['x'] + goal['l'] / 2, goal['y'] - goal['l'] / 2]),
('c3B', (86, 109, 249) * 4)) # color
self.arm1 = self.batch.add(
4, pyglet.gl.GL_QUADS, None,
('v2f', [250, 250, # location
250, 300,
260, 300,
260, 250]),
('c3B', (249, 86, 86) * 4,)) # color
self.arm2 = self.batch.add(
4, pyglet.gl.GL_QUADS, None,
('v2f', [100, 150, # location
100, 160,
200, 160,
200, 150]), ('c3B', (249, 86, 86) * 4,))
def render(self):
self._update_arm()
self.switch_to()
self.dispatch_events()
self.dispatch_event('on_draw')
self.flip()
def on_draw(self):
self.clear()
self.batch.draw()
def _update_arm(self):
# update goal
self.goal.vertices = (
self.goal_info['x'] - self.goal_info['l']/2, self.goal_info['y'] - self.goal_info['l']/2,
self.goal_info['x'] + self.goal_info['l']/2, self.goal_info['y'] - self.goal_info['l']/2,
self.goal_info['x'] + self.goal_info['l']/2, self.goal_info['y'] + self.goal_info['l']/2,
self.goal_info['x'] - self.goal_info['l']/2, self.goal_info['y'] + self.goal_info['l']/2)
# update arm
(a1l, a2l) = self.arm_info['l'] # radius, arm length
(a1r, a2r) = self.arm_info['r'] # radian, angle
a1xy = self.center_coord # a1 start (x0, y0)
a1xy_ = np.array([np.cos(a1r), np.sin(a1r)]) * a1l + a1xy # a1 end and a2 start (x1, y1)
a2xy_ = np.array([np.cos(a1r+a2r), np.sin(a1r+a2r)]) * a2l + a1xy_ # a2 end (x2, y2)
a1tr, a2tr = np.pi / 2 - self.arm_info['r'][0], np.pi / 2 - self.arm_info['r'].sum()
xy01 = a1xy + np.array([-np.cos(a1tr), np.sin(a1tr)]) * self.bar_thc
xy02 = a1xy + np.array([np.cos(a1tr), -np.sin(a1tr)]) * self.bar_thc
xy11 = a1xy_ + np.array([np.cos(a1tr), -np.sin(a1tr)]) * self.bar_thc
xy12 = a1xy_ + np.array([-np.cos(a1tr), np.sin(a1tr)]) * self.bar_thc
xy11_ = a1xy_ + np.array([np.cos(a2tr), -np.sin(a2tr)]) * self.bar_thc
xy12_ = a1xy_ + np.array([-np.cos(a2tr), np.sin(a2tr)]) * self.bar_thc
xy21 = a2xy_ + np.array([-np.cos(a2tr), np.sin(a2tr)]) * self.bar_thc
xy22 = a2xy_ + np.array([np.cos(a2tr), -np.sin(a2tr)]) * self.bar_thc
self.arm1.vertices = np.concatenate((xy01, xy02, xy11, xy12))
self.arm2.vertices = np.concatenate((xy11_, xy12_, xy21, xy22))
# convert the mouse coordinate to goal's coordinate
def on_mouse_motion(self, x, y, dx, dy):
self.goal_info['x'] = x
self.goal_info['y'] = y
if __name__ == '__main__':
env = ArmEnv()
while True:
env.render()
env.step(env.sample_action())