forked from natolambert/dynamicslearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipps_experiments.py
217 lines (188 loc) · 6.39 KB
/
pipps_experiments.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
from pipps_psuedocode import PIPPS_policy
import gym
from model_general_nn import GeneralNN
import torch.nn as nn
import torch
import numpy as np
from utils.nn import *
from utils.data import *
# from utils.rl import *
import utils.rl
load_params = {
'delta_state': True, # normally leave as True, prediction mode
# when true, will include the time plus one in the dataframe (for trying predictions of true state vs delta)
'include_tplus1': True,
# trims high vbat because these points the quad is not moving
'takeoff_points': 180,
# if all the euler angles (floats) don't change, it is not realistic data
'trim_0_dX': False,
'find_move': True,
# if the states change by a large amount, not realistic
'trime_large_dX': True,
# IMPORTANT ONE: stacks the past states and inputs to pass into network
'stack_states': 3,
# adds a column to the dataframe tracking end of trajectories
'terminals': True
}
# load_iono_txt('data_wiggle.txt', load_params)
# quit()
############ SETUP EXPERIMENT ENV ########################
# env = gym.make('CartPole-v1')
# env = gym.make('MountainCarContinuous-v0')
env = gym.make("CartPoleContEnv-v0")
# double bounds
# env.unwrapped.theta_threshold_radians = 2*env.unwrapped.theta_threshold_radians
# env.unwrapped.x_threshold = 2*env.unwrapped.x_threshold
observations = []
actions = []
rewards = []
for i_episode in range(50):
observation = env.reset()
rewards = []
for t in range(100):
# env.render()
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
#reshape obersvation
observation = observation.reshape(-1,1)
# print(action)
observations.append(observation)
actions.append([action])
rewards.append(reward)
if done:
print("Episode finished after {} timesteps".format(t+1))
print("Ep reward: ", np.sum(rewards))
break
############ PROCESS DATA ########################
# o = np.array(observations) # mountain car
o = np.array(observations).squeeze() # cartpole
actions = np.array(actions).reshape(-1,1)
# shape into trainable set
d_o = o[1:,:]-o[:-1,:]
actions = actions[:-1,:]
o = o[:-1,:]
print('---')
print("X has shape: ", np.shape(o))
print("U has shape: ", np.shape(actions))
print("dX has shape: ", np.shape(d_o))
print('---')
############ TRAIN MODEL ########################
ensemble = False
nn_params = { # all should be pretty self-explanatory
'dx': np.shape(o)[1],
'du': np.shape(actions)[1],
'dt': np.shape(d_o)[1],
'hid_width': 250,
'hid_depth': 2,
'bayesian_flag': True,
'activation': Swish(),
'dropout': 0.0,
'split_flag': False,
'pred_mode': 'Delta State',
'ensemble': ensemble
}
train_params = {
'epochs': 28,
'batch_size': 18,
'optim': 'Adam',
'split': 0.8,
'lr': .00175, # bayesian .00175, mse: .0001
'lr_schedule': [30, .6],
'test_loss_fnc': [],
'preprocess': True,
'noprint': True
}
if ensemble:
newNN = EnsembleNN(nn_params, 10)
acctest, acctrain = newNN.train_cust((o, actions, d_o), train_params)
else:
newNN = GeneralNN(nn_params)
newNN.init_weights_orth()
if nn_params['bayesian_flag']:
newNN.init_loss_fnc(d_o, l_mean=1, l_cov=1) # data for std,
acctest, acctrain = newNN.train_cust((o, actions, d_o), train_params)
pipps_nn_params = { # all should be pretty self-explanatory
'dx': np.shape(o)[1],
'du': np.shape(actions)[1],
'hid_width': 50,
'hid_depth': 2,
'bayesian_flag': True,
'activation': Swish(),
'dropout': 0.0,
'bayesian_flag': False
}
# for the pipps policy update step
policy_update_params = {
'P': 100,
'T': 15,
'learning_rate': 3e-3,
}
############ INITAL PIPPS POLICY ########################
# init PIPPS policy
PIPPSy = PIPPS_policy(pipps_nn_params, policy_update_params, newNN)
PIPPSy.init_weights_orth() # to see if this helps initial rollouts
# set cost function
"""
Observation:
Type: Box(4)
Num Observation Min Max
0 Cart Position - 4.8 4.8
1 Cart Velocity - Inf Inf
2 Pole Angle - 24 deg 24 deg
3 Pole Velocity At Tip - Inf Inf
"""
def simple_cost_cartpole(vect):
l_pos = 1
l_vel = 5
l_theta = 2
l_theta_dot = 1
return l_pos*vect[0]**2 + l_vel*vect[1]**2 \
+ l_theta*vect[2]**2 + l_theta_dot*vect[3]**2
def simple_cost_car(vect):
l_pos = 10
l_vel = 2.5
return -l_pos*vect[0] #+ l_vel*vect[1]**2
PIPPSy.set_cost_function(simple_cost_cartpole)
# set baseline function
PIPPSy.set_baseline_function(np.mean)
PIPPSy.policy_step(o)
# PIPPSy.viz_comp_graph()
# quit()
############ PIPPS ITERATIONS ########################
P_rollouts = 20
for p in range(P_rollouts):
print("------ PIPPS Training Rollout", p, " ------")
observations_new = []
actions = []
rewards_fin = []
for i_episode in range(10):
observation = env.reset()
rewards = []
for t in range(100):
if p > 10: env.render()
observation = observation.reshape(-1)
# print(observation)
# action = PIPPSy.predict(observation) # env.action_space.sample()
action = PIPPSy.forward(torch.Tensor([observation])) # env.action_space.sample()
# PIPPSy.viz_comp_graph(action.requires_grad_(True))
# print(action)
# action = action.int().data.numpy()[0]
action = action.data.numpy()
# print(action)
observation, reward, done, info = env.step(action[0])
observation = observation.reshape(-1, 1)
observations_new.append(observation)
actions.append([action])
rewards.append(reward)
if done:
# print("Episode finished after {} timesteps".format(t+1))
# print(np.sum(rewards))
rewards_fin.append(np.sum(rewards))
break
observations_new = np.array(observations_new).squeeze()
o = np.concatenate((o,observations_new),0)
print("New Dataset has shape: ", np.shape(o))
print("Reward at this iteration: ", np.mean(rewards_fin))
print('---')
PIPPSy.policy_step(np.array(o))
# print('---')