-
Notifications
You must be signed in to change notification settings - Fork 0
/
rl.py
304 lines (264 loc) · 8.67 KB
/
rl.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import random
import multiprocessing as mp
import time
import pickle
import os
from federal import ControllerCommonNet, ClientCommonNet
dataset_name = 'Physics'
def server_run(supermask, epoch, dataset):
nfeat, nclass = 0, 0
if dataset == 'cora':
nfeat, nclass = 1433, 7
elif dataset == 'citeseer':
nfeat, nclass = 3703, 6
elif dataset == 'pubmed':
nfeat, nclass = 500, 3
elif dataset == 'corafull':
nfeat, nclass = 8710, 70
elif dataset == 'Physics':
nfeat, nclass = 8415, 5
client = 3
if dataset == "Physics":
client = 8
with open('tmp.pkl', 'wb') as f:
pickle.dump(supermask, f)
controller = ControllerCommonNet(client)
controller.configure('SonNet', dataset, nfeat, nclass)
res = controller.work(epochs=epoch)
print('Test on sonnet of {}, get the result as\n{}'.format(dataset, res))
controller.broadcast_with_waiting_res('ending')
controller.close()
path = "rl_"+dataset+"_accu.pkl"
with open(path, 'wb') as f:
pickle.dump(res["accu"], f)
return
def client_run(dataset):
clients = []
client = 3
if dataset == "Physics":
client = 8
for j in range(client):
clients.append(ClientCommonNet(j))
processes = []
for client in clients:
process = mp.Process(target=client.work)
process.start()
processes.append(process)
for process in processes:
process.join()
return
def getAcc(supermask, epoch=5):
server = mp.Process(target=server_run, args=[
supermask, epoch, dataset_name])
server.start()
time.sleep(10)
client = mp.Process(target=client_run, args=[dataset_name])
client.start()
server.join()
# read accuracy
path = "rl_"+dataset_name+"_accu.pkl"
with open(path, 'rb') as f:
acc = pickle.load(f)
return acc
class q_learning:
def __init__(self, shape):
"""
initialize a q-learning model
"""
self.table = {}
actionNum = 1
for node in shape:
layer = node[0]
if layer == 1:
# first layer
for action in range(actionNum):
self.table[(layer-1, action)] = [0]*node[1]
elif layer == len(shape):
for action in range(1, actionNum+1):
self.table[(layer-1, action)] = [0]*node[1]
self.table[(layer-1, 0)] = [0]
else:
for action in range(1, actionNum+1):
self.table[(layer-1, action)] = [0]*(node[1]+1)
self.table[(layer-1, 0)] = [0]
actionNum = node[1]
self.n = len(shape)
# epsilon greedy
self.epsilon = 0.5
# alpha q table
self.alpha = 0.5
def get_best_action(self, state):
"""
get best action
"""
actions = self.table[state]
index = actions.index(max(actions))
return index
def get_action(self, state, epsilon=0.5):
"""
use epsilon greedy algorithm to choose an action from state's actions
"""
actions = self.table[state]
if random.random() > epsilon:
# then choose action with max q value
index = actions.index(max(actions))
return index
# randomly choose an action
index = random.randint(0, len(actions)-1)
print("actions:{}".format(actions))
print("randomint:{}".format(random.randint(0, len(actions)-1)))
print("randomint:{}".format(random.randint(0, len(actions)-1)))
print("randomint:{}".format(random.randint(0, len(actions)-1)))
return index
def update(self, reward, state, action, next_state, states=None):
"""
use reward update q[state][action]
we do reward shape with states when arrive at last layer
states not contain state, otherState contains [nodeindex, action]
"""
# q = q + alpha[reward + max(q') -q]
# reward shape
if state[0] == self.n-1:
meanReward = reward / (len(states)+1)
for otherState in states:
self.table[otherState[:2]][otherState[-1]] = self.table[otherState[:2]][otherState[-1]] + self.alpha*(
meanReward+max(self.table[(otherState[0]+1, otherState[2])])-self.table[otherState[:2]][otherState[-1]])
self.table[state][action] = self.table[state][action] + \
self.alpha*(meanReward-self.table[state][action])
else:
self.table[state][action] = self.table[state][action] + self.alpha * \
(reward+max(self.table[next_state])-self.table[state][action])
def output(self):
"""
output the best result
"""
class env:
def __init__(self, shape):
"""
docstring
"""
self.state = (0, 0)
self.reward = 0
self.done = False
self.shape = shape
# trace is states contain state like (nodeid, las_action, action)
self.trace = []
self.isShape = False
self.trainNum = 0
def reset(self):
"""
reset the environment
"""
self.state = (0, 0)
self.done = False
self.reward = 0
self.trace = []
self.isShape = False
return self.state
def mov(self, action):
"""
interact with environment
return reward,next state
"""
# record trace
self.trace.append((self.state[0], self.state[1], action))
# update state
self.state = (self.state[0]+1, action)
if self.state[0] == len(self.shape):
# reach final state
trace = [item[2] for item in self.trace]
trace[0] += 1
trace[-1] += 1
self.trace = self.trace[:-1]
self.trainNum += 1
print("training trace: {},trainNum: {}".format(trace, self.trainNum))
self.reward = getAcc(trace)
self.done = True
self.isShape = True
else:
self.reward = 0
def createShape(raw):
"""
produce shape automatically
raw: [1,2,3] choices list
"""
shape = []
number = 1
for i in raw:
item = [number, i]
number = number*i
shape.append(item)
return shape
def readResult():
with open("./result", "rb") as result:
actions = pickle.load(result)
acc = pickle.load(result)
time = pickle.load(result)
print(acc)
print(actions)
print(time)
return (actions, acc, time)
def writeResult(actions, acc, time):
with open("./result", 'wb') as result:
pickle.dump(actions, result)
pickle.dump(acc, result)
pickle.dump(time, result)
def save_model(env, model, num):
if os.path.exists('rl_model.pkl'):
with open('rl_model.pkl', 'rb') as f:
map = pickle.load(f)
else:
map = dict()
with open('rl_model.pkl', 'wb') as f:
map[num] = (env, model)
pickle.dump(map, f)
def read_model():
with open('rl_model.pkl', 'rb') as f:
map = pickle.load(f)
return map
if __name__ == '__main__':
startTime = time.time()
mp.set_start_method('spawn')
random.seed(719)
shape = [[1, 5], [2, 12], [3, 24], [4, 36],
[5, 48], [6, 60], [7, 72], [8, 5]]
model = q_learning(shape)
envi = env(shape)
episodes = 1000
for i in range(episodes):
print("RL: episodes = {}".format(i))
state = envi.reset()
epsilon = epsilon = 0.9/(episodes ** 2) * (episodes ** 2 - i**2) + 0.1
while True:
action = model.get_action(state, epsilon=epsilon)
envi.mov(action)
if envi.isShape:
model.update(envi.reward, state, action,
envi.state, envi.trace)
else:
model.update(envi.reward, state, action, envi.state)
if envi.done == True:
break
state = envi.state
if i % 100 == 0:
save_model(envi, model, i)
print("{} step timeLength".format(i), time.time()-startTime)
if time.time()-startTime - 10*(i+1) > 6*60*60:
print("end episodes", i)
break
envi.reset()
state = (0, 0)
actions = []
for i in range(len(shape)):
action = model.get_best_action(state)
actions.append(action)
state = (state[0]+1, action)
actions[0] = actions[0]+1
actions[-1] = actions[-1]+1
endTime = time.time()
TimeLength = endTime-startTime
acc = getAcc(actions, 50)
print(acc)
print(actions)
print(TimeLength)
writeResult(actions=actions, acc=acc, time=TimeLength)