-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpred_env.py
218 lines (155 loc) · 6.48 KB
/
pred_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
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
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
##############################################
##############################################
class pred_env:
# initialize
def __init__(self,horizon_length,k):
self.prev_state =None
self.curr_state =None
self.state_list=list()
self.action_list=list()
self.state_to_action_map=dict()
self.P=defaultdict()
self.R=defaultdict()
self.horizon_len=horizon_length
self.terminal_state=None
self.k=k
def reset(self):
self.state_list=list()
self.action_list=list()
self.state_to_action_map=dict()
self.P=defaultdict()
self.R=defaultdict()
# Parameter Estimation
def update_param_given_epi(self,D_real):
episodes=D_real.buffer
# following SARSA format
for epi_id in range(len(episodes)):
t_states, t_actions, t_rewards,t_nstates,t_log_probs = self.cvt_axis(episodes[epi_id])
i=0
while i<len(t_states):
# updating the list of states
if any([torch.equal(x,t_states[i]) for x in self.state_list])!=True:
self.state_list.append(t_states[i])
self.state_to_action_map.update({t_states[i]:[]})
# updating the list of actions
if any([torch.equal(x,t_actions[i]) for x in self.action_list])!=True:
self.action_list.append(t_actions[i])
if any([torch.equal(x,t_actions[i]) for x in self.state_to_action_map[self.smooth_check(self.state_to_action_map,t_states[i])]])!=True:
self.state_to_action_map[self.smooth_check(self.state_to_action_map,t_states[i])].append(t_actions[i])
# # update state,action to next state count map
tru_tup,flag=self.double_smooth_check(self.P,(t_states[i],t_actions[i]))
if flag!=True:
self.P[(t_states[i],t_actions[i])]={t_nstates[i]:1}
self.R[(t_states[i],t_actions[i])]={t_nstates[i]:t_rewards[i]}
else:
if any([torch.equal(x,t_nstates[i]) for x in self.P[tru_tup].keys()])!=True:
self.P[tru_tup].update({t_nstates[i]:1})
self.R[tru_tup].update({t_nstates[i]:t_rewards[i]})
else:
sec_tup=self.smooth_check(self.P[tru_tup],t_nstates[i])
self.P[tru_tup][sec_tup]+=1
i+=1
if self.terminal_state is None and i<self.horizon_len:
self.terminal_state=t_nstates[i-1]
self.state_list.append(t_nstates[i-1])
return
# Support functions
def double_smooth_check(self,A,a):
for ele in A.keys():
if torch.equal(a[0],ele[0]) and torch.equal(a[1],ele[1]):
return ele,True
return a,False
def smooth_check(self,A,a):
for ele in A.keys():
if torch.eq(a,ele).all():
return ele
return a
def cvt_axis(self,traj):
t_states =[]
t_actions =[]
t_nstates =[]
t_rewards=[]
t_log_probs=[]
for i in range(len(traj[0])):
t_states.append(traj[0][i])
t_actions.append(traj[1][i])
t_rewards.append(traj[2][i])
t_nstates.append(traj[3][i])
t_log_probs.append(traj[4][i])
return (t_states, t_actions, t_rewards,t_nstates,t_log_probs)
def get_parameters(self):
print("\nState list")
print(self.state_list)
print("\nAction list")
print(self.action_list)
print("\nState to action map")
print(self.state_to_action_map)
print("\nstate_action to next state")
for x in self.P:
print(x)
print(self.P[x])
print("\n state_action to reward map")
for x in self.R:
print(x)
print(self.R[x])
return
def Is_terminal_state(self,s_t):
if torch.equal(self.terminal_state,s_t):
return True
return False
def set_start_state(self):
if len(self.state_list)>0:
p=[1]*len(self.state_list)
p=[x/len(self.state_list) for x in p]
s_t_index=np.random.choice(np.arange(len(self.state_list)),p=p)
s_t=self.state_list[s_t_index]
while torch.equal(s_t,self.terminal_state):
s_t_index=np.random.choice(np.arange(len(self.state_list)),p=p)
s_t=self.state_list[s_t_index]
self.curr_state=s_t
return
def list_check_up(self,A,s_t):
for x in A:
if torch.equal(x,s_t):
return x
return None
# Fake Data generation functions
def step_v1(self,a_t):
next_state=0
un_norm_distr=self.P[self.double_smooth_check(self.P,(self.curr_state,a_t))[0]]
norm_factor=sum(list(un_norm_distr.values()))
choices=list(un_norm_distr.keys())
p=[x/norm_factor for x in un_norm_distr.values()]
next_state_id=np.random.choice(np.arange(len(choices)),p=p)
next_state=choices[next_state_id]
rew_dict=self.R[self.double_smooth_check(self.R,(self.curr_state,a_t))[0]]
next_state_repr=None
for x in rew_dict.keys():
if torch.equal(x,next_state):
next_state_repr=x
break
reward=rew_dict[next_state_repr]
Is_done=False
if self.Is_terminal_state(next_state):
Is_done=True
return next_state,reward,Is_done,None
# sample a state from D_real