-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree_env.py
executable file
·219 lines (169 loc) · 8.87 KB
/
tree_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
218
219
import networkx as nx
import numpy as np
from scipy.special import logsumexp
class SyntheticTree:
def __init__(self, k, d, algorithm, tau, alpha, gamma, step_size):
self._k = k
self._d = d
self._algorithm = algorithm
self._tau = tau
self._alpha = alpha
self._gamma = gamma
self._step_size = step_size
if algorithm == 'alpha-divergence' and alpha == 1:
self._algorithm = 'ments'
if algorithm == 'alpha-divergence' and alpha == 2:
self._algorithm = 'tents'
self._tree = nx.balanced_tree(k, d, create_using=nx.DiGraph)
random_weights = np.random.rand(len(self._tree.edges))
for i, e in enumerate(self._tree.edges):
self._tree[e[0]][e[1]]['weight'] = random_weights[i]
self._tree[e[0]][e[1]]['N'] = 0
self._tree[e[0]][e[1]]['Q'] = 0.
if algorithm == "w-mcts":
self._tree[e[0]][e[1]]['q_mean'] = 0.
self._tree[e[0]][e[1]]['q_variance'] = 0.
for n in self._tree.nodes:
self._tree.nodes[n]['N'] = 0
self._tree.nodes[n]['V'] = 0.
if algorithm == "w-mcts":
self._tree.nodes[n]['v_mean'] = 0.
self._tree.nodes[n]['v_variance'] = 0.
elif algorithm == "dng":
self._tree.nodes[n]["mu"] = 0.
self._tree.nodes[n]["lambda"] = 1e-2
self._tree.nodes[n]["alpha"] = 1.
self._tree.nodes[n]["beta"] = 100.
self.leaves = [x for x in self._tree.nodes() if
self._tree.out_degree(x) == 0 and self._tree.in_degree(x) == 1]
self._compute_mean()
means = np.array([self._tree.nodes[n]['mean'] for n in self.leaves])
means = (means - means.min()) / (means.max() - means.min()) if len(means) > 1 else [0.]
for i, n in enumerate(self.leaves):
self._tree.nodes[n]['mean'] = means[i]
self.max_mean = 0
for leaf in self.leaves:
self.max_mean = max(self.max_mean, self._tree.nodes[leaf]['mean'])
self._assign_priors_maxs()
self.optimal_v_root, self.q_root = self._solver()
self.state = None
self.reset()
def reset(self, state=None):
if state is not None:
self.state = state
else:
self.state = 0
return self.state
def step(self, action):
edges = [e for e in self._tree.edges(self.state)]
self.state = edges[action][1]
return self.state
def rollout(self, state):
# return np.random.normal(self._tree.nodes[state]['mean'], scale=.5)
return np.random.normal(self._tree.nodes[state]['mean'], scale=.05)
@property
def tree(self):
return self._tree
def _compute_mean(self, node=0, weight=0):
if node not in self.leaves:
for e in self._tree.edges(node):
self._compute_mean(e[1],
weight + self._tree[e[0]][e[1]]['weight'])
else:
self._tree.nodes[node]['mean'] = weight
def _assign_priors_maxs(self, node=0):
successors = [n for n in self._tree.successors(node)]
if successors[0] not in self.leaves:
means = np.array([self._assign_priors_maxs(s) for s in successors])
self._tree.nodes[node]['prior'] = means / means.sum()
self._tree.nodes[node]['mean'] = means.max()
return means.max()
else:
means = np.array([self._tree.nodes[s]['mean'] for s in successors])
self._tree.nodes[node]['prior'] = means / means.sum()
self._tree.nodes[node]['mean'] = means.max()
return means.max()
def _solver(self, node=0):
if self._algorithm == 'w-mcts':
successors = [n for n in self._tree.successors(node)]
means = np.array([self._tree.nodes[s]['mean'] for s in successors])
return self.max_mean, means
elif self._algorithm == 'dng':
successors = [n for n in self._tree.successors(node)]
means = np.array([self._tree.nodes[s]['mean'] for s in successors])
return self.max_mean, means
elif self._algorithm == 'uct':
successors = [n for n in self._tree.successors(node)]
means = np.array([self._tree.nodes[s]['mean'] for s in successors])
return self.max_mean, means
elif self._algorithm == 'power-uct':
successors = [n for n in self._tree.successors(node)]
means = np.array([self._tree.nodes[s]['mean'] for s in successors])
# n_state_action = np.array([self._tree.nodes[s]['N'] for s in successors])
#
# self.max_mean = np.power(np.mean(n_state_action*np.power(means, self._alpha)), 1.0/self._alpha)
return self.max_mean, means
else:
successors = [n for n in self._tree.successors(node)]
if self._algorithm == 'ments':
if successors[0] in self.leaves:
x = np.array([self._tree.nodes[n]['mean'] for n in self._tree.successors(node)])
return self._tau * logsumexp(x / self._tau), x
else:
x = np.array([self._solver(n)[0] for n in self._tree.successors(node)])
return self._tau * logsumexp(x / self._tau), x
elif self._algorithm == 'rents':
if successors[0] in self.leaves:
x = np.array([self._tree.nodes[n]['mean'] for n in self._tree.successors(node)])
return self._tau * np.log(np.sum(self._tree.nodes[node]['prior'] * np.exp(x / self._tau))), x
else:
x = np.array([self._solver(n)[0] for n in self._tree.successors(node)])
return self._tau * np.log(np.sum(self._tree.nodes[node]['prior'] * np.exp(x / self._tau))), x
elif self._algorithm == 'alpha-divergence':
def sparse_max_alpha_divergence(means_tau):
temp_means_tau = means_tau.copy()
sorted_means = np.flip(np.sort(temp_means_tau))
kappa = list()
for i in range(1, len(sorted_means) + 1):
if 1 + i * sorted_means[i-1] > sorted_means[:i].sum() + i * (1 - (1/(self._alpha-1))):
idx = np.argwhere(temp_means_tau == sorted_means[i-1]).ravel()[0]
temp_means_tau[idx] = np.nan
kappa.append(idx)
kappa = np.array(kappa)
c_s_tau = ((means_tau[kappa].sum() - 1) / len(kappa)) + (1 - (1/(self._alpha-1)))
max_omega_tmp = np.maximum(means_tau - c_s_tau, np.zeros(len(means_tau)))
max_omega = np.power(max_omega_tmp * (self._alpha - 1), 1/(self._alpha-1))
max_omega = max_omega/np.sum(max_omega)
# sparse_max_tmp = max_omega * (means_tau + (1/(self._alpha - 1)) * (1 - max_omega_tmp))
sparse_max_tmp = max_omega * means_tau
sparse_max = sparse_max_tmp.sum()
return sparse_max
if successors[0] in self.leaves:
x = np.array([self._tree.nodes[n]['mean'] for n in self._tree.successors(node)])
return self._tau * sparse_max_alpha_divergence(x / self._tau), x
else:
x = np.array([self._solver(n)[0] for n in self._tree.successors(node)])
return self._tau * sparse_max_alpha_divergence(np.array(x / self._tau)), x
elif self._algorithm == 'tents':
def sparse_max(means_tau):
temp_means_tau = means_tau.copy()
sorted_means = np.flip(np.sort(temp_means_tau))
kappa = list()
for i in range(1, len(sorted_means) + 1):
if 1 + i * sorted_means[i-1] > sorted_means[:i].sum():
idx = np.argwhere(temp_means_tau == sorted_means[i-1]).ravel()[0]
temp_means_tau[idx] = np.nan
kappa.append(idx)
kappa = np.array(kappa)
sparse_max = means_tau[kappa] ** 2 / 2 - (
means_tau[kappa].sum() - 1) ** 2 / (2 * len(kappa) ** 2)
sparse_max = sparse_max.sum() + .5
return sparse_max
if successors[0] in self.leaves:
x = np.array([self._tree.nodes[n]['mean'] for n in self._tree.successors(node)])
return self._tau * sparse_max(x / self._tau), x
else:
x = np.array([self._solver(n)[0] for n in self._tree.successors(node)])
return self._tau * sparse_max(np.array(x / self._tau)), x
else:
raise ValueError