-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
255 lines (190 loc) · 8.3 KB
/
main.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
import torch
# import torch.autograd as autograd
# import torch.nn as nn
import torch.nn.functional as F
# import torch.optim as optim
# import torch.nn.utils as utils
# from torch.autograd import Variable
# import math
# from blackhc.mdp import dsl
# from blackhc import mdp
import time
# from blackhc.mdp import lp
import functools
# import numpy as np
from tqdm import tqdm
# from matplotlib import pyplot as plt
# from numpy import random
# from operator import itemgetter
# from collections import defaultdict
import numpy as np
##############################################
from Agent import Agent
from env import MULTI_ROUND_NDMP,solver
from utils import ReplayMemory
##############################################
class Test_bench:
def __init__(self):
self.horizon_len=20
self.num_of_real_epi=10
self.k=10
self.mult_factor=1.0
self.real_ratio=0.5
self.capacity=10000
self.batch_size=2000
self.learning_rate=0.001
self.gamma=0.99
self.num_of_outerloop=100 # outer loop
self.num_of_innerloop=10 # inner loop
self.D_real=ReplayMemory(self.capacity)
self.env=None
self.Agent=None
def init_play_ground(self,env):
self.env=env
self.env.reset()
self.A1=Agent(self.env.observation_space,self.env.action_space,gamma=self.gamma,learning_rate=self.learning_rate,horizon_len=self.horizon_len,k=self.k,fraction_of_real=self.real_ratio,batch_size=self.batch_size)
self.A1.reset()
def reset_play_ground(self):
self.env.reset()
self.A1.reset()
self.D_real.flush_all()
def display_env(self):
self.env.render()
self.env.render_widget.width=500
time.sleep(0.200)
def update_D_real(self,num_of_epochs=None):
num_of_episodes=self.num_of_real_epi
if num_of_epochs is not None:
num_of_episodes=num_of_epochs
self.env.reset()
s_t_index=self.env._state.index
s_t=F.one_hot(torch.tensor(s_t_index),num_classes=self.env.observation_space.n).unsqueeze(dim=0)
s_t=s_t.type(torch.FloatTensor)
trajs=[]
# D_real.flush_all()
result=[]
for traj_id in range(num_of_episodes):
self.env.reset()
# display_env()
s_t_index=self.env._state.index
states=[]
log_probs=[]
rewards=[]
actions=[]
nstates=[]
for t in range(self.horizon_len):
s_t=F.one_hot(torch.tensor(s_t_index),num_classes=self.env.observation_space.n).unsqueeze(dim=0)
s_t=s_t.type(torch.FloatTensor)
a_t, log_prob = self.A1.action(s_t)
ns_t, r_t, done, _ = self.env.step(a_t.numpy()[0][0])
# display_env()
if t!=0:
nstates.append(s_t)
states.append(s_t)
actions.append(a_t)
log_probs.append(log_prob)
rewards.append(r_t)
s_t_index=ns_t
if done:
break
# time.sleep(2)
s_t=F.one_hot(torch.tensor(s_t_index),num_classes=self.env.observation_space.n).unsqueeze(dim=0)
s_t=s_t.type(torch.FloatTensor)
nstates.append(s_t)
self.D_real.push(states, actions, rewards,nstates, log_probs)
return
def perform_pure_fake(self,mul_factor,init_params):
# MBPO based agent
self.reset_play_ground()
result=[]
for param in self.A1.model.fc1.parameters():
param.data = torch.nn.parameter.Parameter(init_params)
# print("\nBefore training:")
# print(list(self.A1.model.fc1.parameters()))
for x in range(self.num_of_outerloop):
result.append(self.update_D_real(num_of_epochs=10))
for i in range(self.num_of_innerloop):
self.A1.MBPO_train_1(self.D_real,mul_factor)
# A1.MBPO_train_2(D_real)
# A1.train_(D_real)
# pass
# print("\nAfter training:")
# print(list(self.A1.model.fc1.parameters()))
return result
def perform_mixed_strategy(self,fraction,init_params):
# MBPO based agent
self.reset_play_ground()
result=[]
for param in self.A1.model.fc1.parameters():
param.data = torch.nn.parameter.Parameter(init_params)
# print("\nBefore training:")
# print(list(self.A1.model.fc1.parameters()))
for x in range(self.num_of_outerloop):
result.append(self.update_D_real(num_of_epochs=10))
for i in range(self.num_of_innerloop):
# self.A1.MBPO_train_1(self.D_real)
self.A1.MBPO_train_2(self.D_real,fraction)
# self.A1.train_(D_real)
# pass
# print("\nAfter training:")
# print(list(self.A1.model.fc1.parameters()))
return result
def perform_pure_real(self,init_params):
# MBPO based agent
self.reset_play_ground()
result=[]
for param in self.A1.model.fc1.parameters():
param.data = torch.nn.parameter.Parameter(init_params)
# print("\nBefore training:")
# print(list(self.A1.model.fc1.parameters()))
for x in range(self.num_of_outerloop):
result.append(self.update_D_real(num_of_epochs=10))
for i in range(self.num_of_innerloop):
# self.A1.MBPO_train_1(self.D_real)
# self.A1.MBPO_train_2(D_real)
self.A1.train_(self.D_real)
# pass
# print("\nAfter training:")
# print(list(self.A1.model.fc1.parameters()))
return result
if __name__=='__main__':
print("Entered training")
play_ground=Test_bench() # create an instance of Test_bench
env = MULTI_ROUND_NDMP.to_env() # initialize the environment
play_ground.init_play_ground(env=env) # initialize Playground,Agent will be initilized within the play_ground
play_ground.reset_play_ground() # setting to the Default state of Playground
Q_array=solver.compute_q_table(max_iterations=10000, all_close=functools.partial(np.allclose, rtol=1e-10, atol=1e-10)) # Q_value associated with the environment
# play_ground.perform_pure_real()
# play_ground.perform_pure_fake(mult_factor=1)
# play_ground.perform_mixed_strategy()
# my_data = np.genfromtxt("./experiment/init_param_small.csv", delimiter=',')
# init_params=torch.from_numpy(my_data)
# result=play_ground.perform_pure_real(init_params.float())
fract_list=np.arange(0,1.1,.1)
prim_policy_list=[]
for fract in fract_list:
fract=np.round(fract,2)
my_data = np.genfromtxt("./experiment/init_param_small.csv", delimiter=',')
init_params=torch.from_numpy(my_data)
result=play_ground.perform_mixed_strategy(fract,init_params.float())
prim_policy_list.append(list(play_ground.A1.model.fc1.parameters()))
prim_policy_list=np.array(prim_policy_list)
np.savetxt("./experiment/exp_rslt_1.csv", prim_policy_list, delimiter=",")
fract_list=np.arange(0,1.1,.1)
sec_policy_list=[]
number_of_times=10
for fract in fract_list:
fract=np.round(fract,2)
policy_for_fract=[]
for i in range(number_of_times):
my_data = np.genfromtxt("./experiment/init_param_small.csv", delimiter=',')
init_params=torch.from_numpy(my_data)
result=play_ground.perform_mixed_strategy(fract,init_params.float())
policy_for_fract.append(list(play_ground.A1.model.fc1.parameters()))
policy_for_fract=np.array(policy_for_fract)
sec_policy_list.append(policy_for_fract)
print("Done with",fract)
sec_policy_list=np.array(sec_policy_list)
np.savetxt("./experiment/exp_rslt_2.csv", sec_policy_list, delimiter=",")
true_Q=solver.compute_q_table(max_iterations=10000, all_close=functools.partial(np.allclose, rtol=1e-10, atol=1e-10))
np.savetxt("./experiment/true_Q.csv", true_Q, delimiter=",")