-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_pointbot_demos1.py
199 lines (174 loc) · 6.72 KB
/
gen_pointbot_demos1.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
from env.simplepointbot1 import SimplePointBot, SimplePointBotTeacher
import numpy as np
import pickle
def get_random_transitions_pointbot1(w1,
w2,
discount,
num_transitions,
task_demos=False,
save_rollouts=False):
env = SimplePointBot(w1 = w1, w2 = w2)
transitions = []
rollouts = []
done = True
total =0
while True:
if done:
if len(rollouts):
mc_reward =0
for transition in rollouts[::-1]:
mc_reward = transition[2] + discount * mc_reward
transition.append(mc_reward)
transitions.extend(rollouts)
if total > num_transitions / 3:
break
state = np.array(
[np.random.uniform(-50, 10),
np.random.uniform(-25, 25)])
while env.obstacle(state):
state = np.array(
[np.random.uniform(-50, 10),
np.random.uniform(-25, 25)])
rollouts = []
action = np.clip(np.random.randn(2), -1, 1)
next_state = env._next_state(state, action, override=True)
constraint = env.obstacle(next_state)
done = constraint or len(rollouts)==9
reward = env.step_cost(state, action)
rollouts.append([state, action, constraint, next_state,
not constraint])
state = next_state
total+=1
rollouts = []
done = True
total = 0
while True:
if done:
if len(rollouts):
mc_reward =0
for transition in rollouts[::-1]:
mc_reward = transition[2] + discount * mc_reward
transition.append(mc_reward)
transitions.extend(rollouts)
if total > num_transitions /4:
break
state = np.array(
[np.random.uniform(-35-w1, -30-w1),
np.random.uniform(-12, 12)])
rollouts = []
action = np.clip(
np.array([np.random.uniform(0.5, 1, 1),
np.random.randn(1)]), -1, 1).ravel()
next_state = env._next_state(state, action, override=True)
constraint = env.obstacle(next_state)
done = constraint or len(rollouts)==9
reward = env.step_cost(state, action)
rollouts.append([state, action, constraint, next_state,
not constraint])
state = next_state
total+=1
rollouts = []
done = True
total = 0
while True:
if done:
if len(rollouts):
mc_reward =0
for transition in rollouts[::-1]:
mc_reward = transition[2] + discount * mc_reward
transition.append(mc_reward)
transitions.extend(rollouts)
if total > num_transitions /4:
break
state = np.array(
[np.random.uniform(-20+w1, -15+w1),
np.random.uniform(-12, 12)])
rollouts = []
action = np.clip(
np.array([np.random.uniform(-1, -0.5, 1),
np.random.randn(1)]), -1, 1).ravel()
next_state = env._next_state(state, action, override=True)
constraint = env.obstacle(next_state)
done = constraint or len(rollouts)==9
reward = env.step_cost(state, action)
rollouts.append([state, action, constraint, next_state,
not constraint])
state = next_state
total+=1
rollouts = []
done = True
total = 0
while True:
if done:
if len(rollouts):
mc_reward =0
for transition in rollouts[::-1]:
mc_reward = transition[2] + discount * mc_reward
transition.append(mc_reward)
transitions.extend(rollouts)
if total > num_transitions /4:
break
state = np.array(
[np.random.uniform(-30-w1, -20-w1),
np.random.uniform(10+w2, 15+w2)])
rollouts = []
action = np.clip(
np.array([np.random.randn(1),
np.random.uniform(-1, -0.5, 1)]), -1, 1).ravel()
next_state = env._next_state(state, action, override=True)
constraint = env.obstacle(next_state)
done = constraint or len(rollouts)==9
reward = env.step_cost(state, action)
rollouts.append([state, action, constraint, next_state,
not constraint])
state = next_state
total+=1
rollouts = []
done = True
total = 0
while True:
if done:
if len(rollouts):
mc_reward =0
for transition in rollouts[::-1]:
mc_reward = transition[2] + discount * mc_reward
transition.append(mc_reward)
transitions.extend(rollouts)
if total > num_transitions /4:
break
state = np.array(
[np.random.uniform(-30-w1, -20-w1),
np.random.uniform(-15-w2, -10-w2)])
rollouts = []
action = np.clip(
np.array([np.random.randn(1),
np.random.uniform(0.5, 1, 1)]), -1, 1).ravel()
next_state = env._next_state(state, action, override=True)
constraint = env.obstacle(next_state)
done = constraint or len(rollouts)==9
reward = env.step_cost(state, action)
rollouts.append([state, action, constraint, next_state,
not constraint])
state = next_state
total+=1
return transitions
if __name__ == '__main__':
counter =0
num_constraint_transitions = 30000
for i in range(0, 25):
print(counter)
w1 = np.uniform(low=-5.0, high=5.0)
w2 = np.uniform(low=-5.0, high=5.0)
constraint_demo_data = get_random_transitions_pointbot0(w1=i, w2=j, discount=0.65, num_transitions = num_constraint_transitions)
num_constraint_transitions = 0
num_constraint_violations = 0
for transition in constraint_demo_data:
num_constraint_violations += int(transition[2])
num_constraint_transitions += 1
print("Number of Constraint Transitions: ",
num_constraint_transitions)
print("Number of Constraint Violations: ",
num_constraint_violations)
with open("demos/pointbot_1/constraint_demos_" + str(counter) + ".pkl", 'wb') as handle:
pickle.dump(constraint_demo_data, handle)
counter+=1