-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathzigzag.py
More file actions
215 lines (191 loc) · 7.37 KB
/
Copy pathzigzag.py
File metadata and controls
215 lines (191 loc) · 7.37 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
import os
from PaintRLEnv.robot_gym_env import PaintGymEnv
EXTRA_CONFIG = {
'RENDER_HEIGHT': 720,
'RENDER_WIDTH': 960,
'Part_NO': 1,
'Expected_Episode_Length': 245,
'EPISODE_MAX_LENGTH': 245,
'TERMINATION_MODE': 'late',
'SWITCH_THRESHOLD': 0.9,
'START_POINT_MODE': 'fixed',
'TURNING_PENALTY': False,
'OVERLAP_PENALTY': False,
'COLOR_MODE': 'RGB',
}
def simple_rgb_zigzag():
PaintGymEnv.change_action_mode(1, 'discrete', 4)
PaintGymEnv.change_obs_mode('discrete', 4)
with PaintGymEnv(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'PaintRLEnv'), with_robot=False,
renders=True, render_video=False, rollout=True, extra_config=EXTRA_CONFIG) as env:
horizontal_move = 0
up = True
done = False
obs = [0] * 5
total_return = 0
step_counter = 0
while not done:
current_pos = 0 if obs[-1] == 0 else round(1 / obs[-1])
if up:
if current_pos % 22 != 19:
obs, reward, done, info = env.step(1)
step_counter += 1
elif horizontal_move < 2:
obs, reward, done, info = env.step(0)
step_counter += 1
horizontal_move += 1
else:
horizontal_move = 0
reward = 0
up = False
else:
if current_pos % 22 != 2:
obs, reward, done, info = env.step(3)
step_counter += 1
elif horizontal_move < 2:
obs, reward, done, info = env.step(0)
step_counter += 1
horizontal_move += 1
else:
horizontal_move = 0
reward = 0
up = True
print('OBS: {0}, REWARD: {1}'.format(obs, reward))
total_return += reward
print('In {0} steps get {1} rewards'.format(step_counter, total_return))
def simple_rgb1_zigzag():
PaintGymEnv.change_action_mode(1, 'discrete', 4)
PaintGymEnv.change_obs_mode('simple', 4)
with PaintGymEnv(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'PaintRLEnv'), with_robot=False,
renders=True, render_video=False, rollout=True, extra_config=EXTRA_CONFIG) as env:
horizontal_move = 0
up = True
done = False
obs = [0, 0]
total_return = 0
step_counter = 0
while not done:
if up:
if obs[1] < 0.95:
obs, reward, done, info = env.step(1)
step_counter += 1
elif horizontal_move < 2:
obs, reward, done, info = env.step(0)
step_counter += 1
horizontal_move += 1
else:
horizontal_move = 0
reward = 0
up = False
else:
if obs[1] > 0.05:
obs, reward, done, info = env.step(3)
step_counter += 1
elif horizontal_move < 2:
obs, reward, done, info = env.step(0)
step_counter += 1
horizontal_move += 1
else:
horizontal_move = 0
reward = 0
up = True
print('OBS: {0}, REWARD: {1}'.format(obs, reward))
total_return += reward
print('In {0} steps get {1} rewards'.format(step_counter, total_return))
def simple_rgb_profile_zigzag():
PaintGymEnv.change_action_mode(1, 'discrete', 4)
PaintGymEnv.change_obs_mode('grid', 10)
with PaintGymEnv(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'PaintRLEnv'), with_robot=False,
renders=False, render_video=False, rollout=False, extra_config=EXTRA_CONFIG) as env:
for _ in range(100):
env.reset()
horizontal_move = 0
up = True
done = False
obs = [0, 0]
total_return = 0
step_counter = 0
while not done:
if up:
if obs[1] < 0.95:
obs, reward, done, info = env.step(1)
step_counter += 1
elif horizontal_move < 2:
obs, reward, done, info = env.step(0)
step_counter += 1
horizontal_move += 1
else:
horizontal_move = 0
reward = 0
up = False
else:
if obs[1] > 0.05:
obs, reward, done, info = env.step(3)
step_counter += 1
elif horizontal_move < 2:
obs, reward, done, info = env.step(0)
step_counter += 1
horizontal_move += 1
else:
horizontal_move = 0
reward = 0
up = True
print('OBS: {0}, REWARD: {1}'.format(obs, reward))
total_return += reward
print('In {0} steps get {1} rewards'.format(step_counter, total_return))
def simple_hsi_zigzag():
EXTRA_CONFIG['COLOR_MODE'] = 'HSI'
PaintGymEnv.change_action_mode(2, 'continuous')
PaintGymEnv.change_obs_mode('simple', 4)
with PaintGymEnv(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'PaintRLEnv'), with_robot=False,
renders=True, render_video=False, rollout=True, extra_config=EXTRA_CONFIG) as env:
horizontal_move = 0
up = True
done = False
obs = [0, 0]
total_return = 0
step_counter = 0
while not done:
if up:
if obs[1] < 0.95:
obs, reward, done, info = env.step([0, 1])
step_counter += 1
elif horizontal_move < 2:
obs, reward, done, info = env.step([0.5, 0])
step_counter += 1
horizontal_move += 1
else:
horizontal_move = 0
reward = 0
up = False
else:
if obs[1] > 0.05:
obs, reward, done, info = env.step([0, -1])
step_counter += 1
elif horizontal_move < 2:
obs, reward, done, info = env.step([0.5, 0])
step_counter += 1
horizontal_move += 1
else:
horizontal_move = 0
reward = 0
up = True
print('OBS: {0}, REWARD: {1}'.format(obs, reward))
total_return += reward
print('In {0} steps get {1} rewards'.format(step_counter, total_return))
def profile_rgb_zigzag():
import cProfile as Profile
pr = Profile.Profile()
pr.disable()
pr.enable()
simple_rgb_profile_zigzag()
pr.disable()
pr.dump_stats('/home/pyang/profile_grid.pstat')
def show_profile_result():
import pstats
ps = pstats.Stats('/home/pyang/profile.pstat')
ps.strip_dirs().print_stats()
if __name__ == '__main__':
# simple_rgb1_zigzag()
simple_hsi_zigzag()
# profile_rgb_zigzag()