forked from FFrankyy/FINDER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nstep_replay_mem_prioritized.pyx
269 lines (251 loc) · 10.1 KB
/
nstep_replay_mem_prioritized.pyx
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from cython.operator import dereference as deref
from libcpp.memory cimport shared_ptr
from libc.stdlib cimport malloc
from libcpp cimport bool
import graph
import numpy as np
# import gc
from libc.stdlib cimport free
cdef class py_Data:
cdef shared_ptr[Data] inner_Data#使用unique_ptr优于shared_ptr
def __cinit__(self):
self.inner_Data = shared_ptr[Data](new Data())
# def __dealloc__(self):
# if self.inner_Data != NULL:
# self.inner_Data.reset()
# gc.collect()
@property
def graph(self):
return self.G2P(deref(deref(self.inner_Data).g))
@property
def s_t(self):
return deref(self.inner_Data).s_t
@property
def a_t(self):
return deref(self.inner_Data).a_t
@property
def r_t(self):
return deref(self.inner_Data).r_t
@property
def s_prime(self):
return deref(self.inner_Data).s_prime
@property
def term_t(self):
return deref(self.inner_Data).term_t
cdef G2P(self,Graph graph1):
num_nodes = graph1.num_nodes #得到Graph对象的节点个数
num_edges = graph1.num_edges #得到Graph对象的连边个数
edge_list = graph1.edge_list
cint_edges_from = np.zeros([num_edges],dtype=np.int)
cint_edges_to = np.zeros([num_edges],dtype=np.int)
for i in range(num_edges):
cint_edges_from[i]=edge_list[i].first
cint_edges_to[i] =edge_list[i].second
return graph.py_Graph(num_nodes,num_edges,cint_edges_from,cint_edges_to)
cdef class py_LeafResult:
cdef shared_ptr[LeafResult] inner_LeafResult
cdef shared_ptr[Data] inner_Data
def __cinit__(self):
self.inner_LeafResult = shared_ptr[LeafResult](new LeafResult())
# def __dealloc__(self):
# if self.inner_LeafResult != NULL:
# self.inner_LeafResult.reset()
# gc.collect()
@property
def leaf_idx(self):
return deref(self.inner_LeafResult).leaf_idx
@property
def p(self):
return deref(self.inner_LeafResult).p
@property
def Data(self):
self.inner_Data = deref(self.inner_LeafResult).data
result = py_Data()
result.inner_Data = self.inner_Data
return result
cdef class py_SumTree:
cdef shared_ptr[SumTree] inner_SumTree
cdef shared_ptr[Data] inner_Data
cdef shared_ptr[Graph] inner_Graph
cdef shared_ptr[LeafResult] inner_LeafResult
def __cinit__(self,capacity):
self.inner_SumTree = shared_ptr[SumTree](new SumTree(capacity))
# def __dealloc__(self):
# if self.inner_SumTree != NULL:
# self.inner_SumTree.reset()
# gc.collect()
@property
def capacity(self):
return deref(self.inner_SumTree).capacity
@property
def data_pointer(self):
return deref(self.inner_SumTree).data_pointer
@property
def tree(self):
return deref(self.inner_SumTree).tree
@property
def data(self):
result = []
for dataPtr in deref(self.inner_SumTree).data:
Data = py_Data()
Data.inner_Data = dataPtr
result.append(Data)
return result
def Add(self,p,pyData):
g = pyData.graph
self.inner_Data =shared_ptr[Data](new Data())
self.inner_Graph =shared_ptr[Graph](new Graph())
deref(self.inner_Graph).num_nodes= g.num_nodes
deref(self.inner_Graph).num_edges=g.num_edges
deref(self.inner_Graph).edge_list=g.edge_list
deref(self.inner_Graph).adj_list=g.adj_list
deref(self.inner_Data).g=self.inner_Graph
deref(self.inner_Data).s_t= pyData.s_t
deref(self.inner_Data).a_t= pyData.a_t
deref(self.inner_Data).r_t= pyData.r_t
deref(self.inner_Data).s_prime= pyData.s_prime
deref(self.inner_Data).term_t= pyData.term_t
deref(self.inner_SumTree).Add(p,self.inner_Data)
def Update(self,int tree_idx,double p):
deref(self.inner_SumTree).Update(tree_idx,p)
def Get_leaf(self,double v):
self.inner_LeafResult = deref(self.inner_SumTree).Get_leaf(v)
result = py_LeafResult()
result.inner_LeafResult = self.inner_LeafResult
return result
cdef class py_ReplaySample:
cdef shared_ptr[ReplaySample] inner_ReplaySample
def __cinit__(self,int n):
self.inner_ReplaySample = shared_ptr[ReplaySample](new ReplaySample(n))
# def __dealloc__(self):
# if self.inner_ReplaySample != NULL:
# self.inner_ReplaySample.reset()
# gc.collect()
@property
def b_idx(self):
return deref(self.inner_ReplaySample).b_idx
@property
def ISWeights(self):
return deref(self.inner_ReplaySample).ISWeights
@property
def g_list(self):
result = []
for graphPtr in deref(self.inner_ReplaySample).g_list:
result.append(self.G2P(deref(graphPtr)))
return result
@property
def list_st(self):
return deref(self.inner_ReplaySample).list_st
@property
def list_s_primes(self):
return deref(self.inner_ReplaySample).list_s_primes
@property
def list_at(self):
return deref(self.inner_ReplaySample).list_at
@property
def list_rt(self):
return deref(self.inner_ReplaySample).list_rt
@property
def list_term(self):
return deref(self.inner_ReplaySample).list_term
cdef G2P(self,Graph graph1):
num_nodes = graph1.num_nodes #得到Graph对象的节点个数
num_edges = graph1.num_edges #得到Graph对象的连边个数
edge_list = graph1.edge_list
cint_edges_from = np.zeros([num_edges],dtype=np.int)
cint_edges_to = np.zeros([num_edges],dtype=np.int)
for i in range(num_edges):
cint_edges_from[i]=edge_list[i].first
cint_edges_to[i] =edge_list[i].second
return graph.py_Graph(num_nodes,num_edges,cint_edges_from,cint_edges_to)
cdef class py_Memory:
cdef shared_ptr[Memory] inner_Memory
cdef shared_ptr[SumTree] inner_SumTree
cdef shared_ptr[Data] inner_Data
cdef shared_ptr[ReplaySample] inner_ReplaySample
cdef shared_ptr[Graph] inner_Graph
cdef shared_ptr[MvcEnv] inner_MvcEnv
def __cinit__(self,double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity):
self.inner_Memory = shared_ptr[Memory](new Memory(epsilon,alpha,beta,beta_increment_per_sampling,abs_err_upper,capacity))
# def __dealloc__(self):
# if self.inner_Memory != NULL:
# self.inner_Memory.reset()
# gc.collect()
# if self.inner_SumTree != NULL:
# self.inner_SumTree.reset()
# gc.collect()
# if self.inner_Data != NULL:
# self.inner_Data.reset()
# gc.collect()
# if self.inner_ReplaySample != NULL:
# self.inner_ReplaySample.reset()
# gc.collect()
# if self.inner_Graph != NULL:
# self.inner_Graph.reset()
# gc.collect()
# if self.inner_MvcEnv != NULL:
# self.inner_MvcEnv.reset()
# gc.collect()
@property
def tree(self):
self.inner_SumTree = deref(self.inner_Memory).tree
result = py_SumTree()
result.inner_SumTree = self.inner_SumTree
return result
@property
def epsilon(self):
return deref(self.inner_Memory).epsilon
@property
def alpha(self):
return deref(self.inner_Memory).alpha
@property
def beta(self):
return deref(self.inner_Memory).beta
@property
def beta_increment_per_sampling(self):
return deref(self.inner_Memory).beta_increment_per_sampling
@property
def abs_err_upper(self):
return deref(self.inner_Memory).abs_err_upper
def Store(self,transition):
g = transition.graph
self.inner_Data =shared_ptr[Data](new Data())
self.inner_Graph =shared_ptr[Graph](new Graph())
deref(self.inner_Graph).num_nodes= g.num_nodes
deref(self.inner_Graph).num_edges=g.num_edges
deref(self.inner_Graph).edge_list=g.edge_list
deref(self.inner_Graph).adj_list=g.adj_list
deref(self.inner_Data).g=self.inner_Graph
deref(self.inner_Data).s_t= transition.s_t
deref(self.inner_Data).a_t=transition.a_t
deref(self.inner_Data).r_t=transition.r_t
deref(self.inner_Data).s_prime=transition.s_prime
deref(self.inner_Data).term_t=transition.term_t
deref(self.inner_Memory).Store(self.inner_Data)
def Add(self,mvcenv,int nstep):
self.inner_Graph =shared_ptr[Graph](new Graph())
# g = self.GenNetwork(mvcenv.graph)
g = mvcenv.graph
deref(self.inner_Graph).num_nodes= g.num_nodes
deref(self.inner_Graph).num_edges=g.num_edges
deref(self.inner_Graph).edge_list=g.edge_list
deref(self.inner_Graph).adj_list=g.adj_list
self.inner_MvcEnv = shared_ptr[MvcEnv](new MvcEnv(mvcenv.norm))
deref(self.inner_MvcEnv).norm = mvcenv.norm
deref(self.inner_MvcEnv).graph = self.inner_Graph
deref(self.inner_MvcEnv).state_seq = mvcenv.state_seq
deref(self.inner_MvcEnv).act_seq = mvcenv.act_seq
deref(self.inner_MvcEnv).action_list = mvcenv.action_list
deref(self.inner_MvcEnv).reward_seq = mvcenv.reward_seq
deref(self.inner_MvcEnv).sum_rewards = mvcenv.sum_rewards
deref(self.inner_MvcEnv).numCoveredEdges = mvcenv.numCoveredEdges
deref(self.inner_MvcEnv).covered_set = mvcenv.covered_set
deref(self.inner_MvcEnv).avail_list = mvcenv.avail_list
deref(self.inner_Memory).Add(self.inner_MvcEnv,nstep)
def Sampling(self,int n):
self.inner_ReplaySample=deref(self.inner_Memory).Sampling(n)
result = py_ReplaySample(n)
result.inner_ReplaySample = self.inner_ReplaySample
return result
def batch_update(self,tree_idx,abs_errors):
deref(self.inner_Memory).batch_update(tree_idx,abs_errors)