-
Notifications
You must be signed in to change notification settings - Fork 1
/
Phase2b_gnn-dqn.py
348 lines (333 loc) · 19.6 KB
/
Phase2b_gnn-dqn.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import copy
import os
from modules.gnn.comb_opt import train, evaluate, evaluate_spath_heuristic, evaluate_tabular
from modules.rl.rl_custom_worlds import GetCustomWorld
from modules.rl.environments import SuperEnv
#from modules.gnn.nfm_gen import NFM_ec_t, NFM_ec_t_dt_at, NFM_ev_ec_t_dt_at_um_us, NFM_ec_dt, NFM_ec_dtscaled, NFM_ev_t, NFM_ev_ec_t, NFM_ev_ec_t_um_us
import modules.gnn.nfm_gen
from modules.sim.graph_factory import GetWorldSet, LoadData
from modules.sim.simdata_utils import SimulateInteractiveMode, SimulateAutomaticMode_DQN
from modules.gnn.comb_opt import init_model
from modules.rl.rl_policy import GNN_s2v_Policy
from modules.ppo.helpfuncs import CreateEnv
import numpy as np
import random
import torch
import argparse
from modules.gnn.construct_trainsets import ConstructTrainSet
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def GetConfig(args):
config={}
config['train_on'] = args.train_on
config['max_nodes'] = args.max_nodes
config['remove_paths'] = args.pursuit == 'Uoff'
assert args.pursuit in ['Uoff','Uon']
#state_repr = 'etUte0U0'
#state_enc = 'nfm'
config['reject_u_duplicates'] = False
config['Etrain'] = [int(i) for i in args.Etrain if i.isnumeric() ]
config['Utrain'] = [int(i) for i in args.Utrain if i.isnumeric() ]
config['solve_select'] = args.solve_select # only solvable worlds (so best achievable performance is 100%)
config['nfm_func'] = args.nfm_func
config['edge_blocking']= args.edge_blocking
config['node_dim'] = modules.gnn.nfm_gen.nfm_funcs[args.nfm_func].F
config['demoruns'] = args.demoruns
config['qnet'] = args.qnet
config['norm_agg'] = args.norm_agg
config['emb_dim'] = args.emb_dim
config['emb_iter_T'] = args.emb_itT
config['optim_target'] = args.optim_target
config['num_episodes'] = args.num_epi
config['memory_size'] = args.mem_size
config['num_step_ql'] = args.nstep
config['bsize'] = 32
config['gamma'] = .9
config['lr_init'] = 1e-3
config['lr_decay'] = 0.99999
config['tau'] = args.tau # num grad steps for each target network update
config['eps_0'] = 1.
config['eps_min'] = 0.1
config['epi_min'] = .9 # reach eps_min at % of episodes # .9
config['eps_decay'] = 1 - np.exp(np.log(config['eps_min'])/(config['epi_min']*config['num_episodes']))
config['rootdir']='./results/results_Phase2/Pathfinding/dqn/'+ \
config['train_on']+'_'+args.pursuit+'/'+ \
config['solve_select']+'_edgeblock'+str(config['edge_blocking'])+'/'+\
config['qnet']+'_normagg'+str(config['norm_agg'])
config['logdir'] = config['rootdir'] + '/' +\
config['nfm_func']+'/'+ \
'emb'+str(config['emb_dim']) + \
'_itT'+str(config['emb_iter_T']) + \
'_epi'+str(config['num_episodes']) + \
'_mem'+str(config['memory_size']) + \
'_nstep'+str(config['num_step_ql'])
config['seed0']=args.seed0
config['numseeds']=args.num_seeds
config['seedrange']=range(config['seed0'], config['seed0']+config['numseeds'])
print('seedrange')
for i in config['seedrange']: print(i)
config['obs_mask']=None # not implemented
config['obs_rate']=1. # not implemented
return config
def main(args):
config=GetConfig(args)
print('device',device)
#
# Load and test trainset
#
#databank_full, register_full, solvable = LoadData(edge_blocking = True)
#env_all_train, hashint2env, env2hashint, env2hashstr = GetWorldSet(state_repr, state_enc, U=Utrain, E=Etrain, edge_blocking=edge_blocking, solve_select=solve_select, reject_duplicates=reject_u_duplicates, nfm_func=nfm_func)
if args.train or args.eval:
senv, env_all_train_list = ConstructTrainSet(config, apply_wrappers=False, remove_paths=config['remove_paths'], tset=config['train_on']) #TODO check
env_all_train = [senv]
if config['demoruns']:
while True:
a = SimulateInteractiveMode(senv, filesave_with_time_suffix=False)
if a == 'Q': break
#
# Train the model on selected subset of graphs
#
if args.train:
for seed in config['seedrange']:
train(seed=seed, config=config, env_all=env_all_train)
#
# Evaluation
#
if args.eval:
evalResults={}
test_heuristics = True
test_full_trainset = False
test_full_solvable_3x3subs = False
test_all_solvable_3x3segments= False
test_other_worlds = False
if test_heuristics:
# Evaluate with simple shortest path heuristic on full trainet to get low mark on performance
evaluate_spath_heuristic(logdir=config['rootdir']+'/heur/spath', config=config, env_all=env_all_train_list)
if test_full_trainset:
# Evaluate on the full training set
evalName='trainset_eval'
evalResults[evalName]={'num_graphs.........':[],'num_graph_instances':[],'avg_return.........':[],'success_rate.......':[],}
for seed in config['seedrange']:
result = evaluate(logdir=config['logdir']+'/SEED'+str(seed), config=config, env_all=env_all_train_list, eval_subdir=evalName)
num_unique_graphs, num_graph_instances, avg_return, success_rate = result
evalResults[evalName]['num_graphs.........'].append(num_unique_graphs)
evalResults[evalName]['num_graph_instances'].append(num_graph_instances)
evalResults[evalName]['avg_return.........'].append(avg_return)
evalResults[evalName]['success_rate.......'].append(success_rate)
if test_full_solvable_3x3subs:
# Evaluate on the full evaluation set
evalName='testset_eval'
evalResults[evalName]={'num_graphs.........':[],'num_graph_instances':[],'avg_return.........':[],'success_rate.......':[],}
Etest=[0,1,2,3,4,5,6,7,8,9,10]
Utest=[1,2,3]
env_all_test, _, _, _ = GetWorldSet(state_repr, state_enc, U=Utest, E=Etest, edge_blocking=config['edge_blocking'], solve_select=config['solve_select'], reject_duplicates=config['reject_u_duplicates'], nfm_func=modules.gnn.nfm_gen.nfm_funcs[config['nfm_func']])
for seed in config['seedrange']:
result = evaluate(logdir=config['logdir']+'/SEED'+str(seed), config=config, env_all=env_all_test, eval_subdir=evalName)
num_unique_graphs, num_graph_instances, avg_return, success_rate = result
evalResults[evalName]['num_graphs.........'].append(num_unique_graphs)
evalResults[evalName]['num_graph_instances'].append(num_graph_instances)
evalResults[evalName]['avg_return.........'].append(avg_return)
evalResults[evalName]['success_rate.......'].append(success_rate)
if test_all_solvable_3x3segments:
# Evaluate on each individual segment of the evaluation set
evalName='graphsegments_eval'
evalResults[evalName]={'num_graphs.........':[],'num_graph_instances':[],'avg_return.........':[],'success_rate.......':[],}
for seed in config['seedrange']:
success_matrix =[]
num_graphs_matrix=[]
instances_matrix =[]
returns_matrix =[]
for u in Utest:
success_vec =[]
num_graphs_vec=[]
instances_vec =[]
returns_vec =[]
for e in Etest:
env_all_test, _, _, _ = GetWorldSet(state_repr, state_enc, U=[u], E=[e], edge_blocking=config['edge_blocking'], solve_select=config['solve_select'], reject_duplicates=config['reject_u_duplicates'], nfm_func=modules.gnn.nfm_gen.nfm_funcs[config['nfm_func']])
result = evaluate(logdir=config['logdir']+'/SEED'+str(seed), config=config, env_all=env_all_test, eval_subdir=evalName+'/runs/'+'E'+str(e)+'U'+str(u))
num_unique_graphs, num_graph_instances, avg_return, success_rate = result
success_vec.append(success_rate)
num_graphs_vec.append(num_unique_graphs)
instances_vec.append(num_graph_instances)
returns_vec.append(avg_return)
success_matrix.append(success_vec)
num_graphs_matrix.append(num_graphs_vec)
instances_matrix.append(instances_vec)
returns_matrix.append(returns_vec)
OF = open(config['logdir']+'/SEED'+str(seed)+'/'+evalName+'/success_matrix.txt', 'w')
def printing(text):
print(text)
OF.write(text + "\n")
success_matrix = np.array(success_matrix)
returns_matrix = np.array(returns_matrix)
num_graphs_matrix = np.array(num_graphs_matrix)
instances_matrix = np.array(instances_matrix)
weighted_return = (returns_matrix * instances_matrix).sum() / instances_matrix.sum()
weighted_success_rate = (success_matrix * instances_matrix).sum() / instances_matrix.sum()
np.set_printoptions(formatter={'float':"{0:0.3f}".format})
printing('success_matrix:')
printing(str(success_matrix))
printing('\nreturns_matrix:')
printing(str(returns_matrix))
printing('\nnum_graphs_matrix:')
printing(str(num_graphs_matrix))
printing('\ninstances_matrix:')
printing(str(instances_matrix))
printing('\nWeighted return: '+str(weighted_return))
printing('Weighted success rate: '+str(weighted_success_rate))
OF.close()
evalResults[evalName]['num_graphs.........'].append(num_graphs_matrix.sum())
evalResults[evalName]['num_graph_instances'].append(instances_matrix.sum())
evalResults[evalName]['avg_return.........'].append(weighted_return)
evalResults[evalName]['success_rate.......'].append(weighted_success_rate)
if test_other_worlds:
#
# Evaluate learned model on another (out of distribution) graph
#
world_names=[
'Manhattan3x3_PredictionExample',
#'Manhattan5x5_FixedEscapeInit',
#'Manhattan5x5_VariableEscapeInit',
#'MetroU3_e17tborder_FixedEscapeInit',
#'SparseManhattan5x5',
]
state_repr='etUte0U0'
state_enc='nfm'
for world_name in world_names:
evalName=world_name[:16]+'_eval'
evalResults[evalName]={'num_graphs.........':[],'num_graph_instances':[],'avg_return.........':[],'success_rate.......':[],}
custom_env = GetCustomWorld(world_name, make_reflexive=True, state_repr=state_repr, state_enc=state_enc)
custom_env.redefine_nfm(modules.gnn.nfm_gen.nfm_funcs[config['nfm_func']])
for seed in range(config['seed0'], config['seed0']+config['numseeds']):
result = evaluate(logdir=config['logdir']+'/SEED'+str(seed), config=config, env_all=[custom_env], eval_subdir=evalName)
num_unique_graphs, num_graph_instances, avg_return, success_rate = result
evalResults[evalName]['num_graphs.........'].append(num_unique_graphs)
evalResults[evalName]['num_graph_instances'].append(num_graph_instances)
evalResults[evalName]['avg_return.........'].append(avg_return)
evalResults[evalName]['success_rate.......'].append(success_rate)
for ename, results in evalResults.items():
OF = open(config['logdir']+'/Results_over_seeds_'+ename+'.txt', 'w')
def printing(text):
print(text)
OF.write(text + "\n")
np.set_printoptions(formatter={'float':"{0:0.3f}".format})
printing('Results over seeds for evaluation on '+ename+'\n')
for category,values in results.items():
printing(category)
printing(' avg over seeds: '+str(np.mean(values)))
printing(' std over seeds: '+str(np.std(values)))
printing(' per seed: '+str(np.array(values))+'\n')
#
# Test on unseen graphs
#
if args.test:
evalResults={}
world_list=[
# 'Manhattan5x5_DuplicateSetB',
#'Manhattan3x3_WalkAround',
# 'MetroU3_e1t31_FixedEscapeInit',
'full_solvable_3x3subs',
#'BifurGraphTask1'
# 'M3test1'
#'Manhattan3x3_PredictionExample',
# 'Manhattan5x5_FixedEscapeInit',
# 'Manhattan5x5_VariableEscapeInit',
# 'MetroU3_e17tborder_FixedEscapeInit',
# 'MetroU3_e17tborder_VariableEscapeInit',
# 'NWB_ROT_FixedEscapeInit',
# 'NWB_ROT_VariableEscapeInit',
# 'NWB_test_FixedEscapeInit',
# 'NWB_test_VariableEscapeInit',
# 'NWB_UTR_FixedEscapeInit',
# 'NWB_UTR_VariableEscapeInit',
# 'SparseManhattan5x5',
]
#node_maxims = [0,0,0,0]
#var_targets=[ None,None,None,None]
#eval_names = world_list
#eval_nums = [10,10,10,10]
for world_name in world_list:
evalName=world_name
if world_name == 'full_solvable_3x3subs':
Etest=[0,1,2,3,4,5,6,7,8,9,10]
Utest=[1,2,3]
evalenv, _, _, _ = GetWorldSet('etUte0U0', 'nfm', U=Utest, E=Etest, edge_blocking=config['edge_blocking'], solve_select=config['solve_select'], reject_duplicates=False, nfm_func=modules.gnn.nfm_gen.nfm_funcs[config['nfm_func']])
else:
env = CreateEnv(world_name, max_nodes=0, nfm_func_name = config['nfm_func'], var_targets=None, remove_world_pool=config['remove_paths'], apply_wrappers=False)
#env.redefine_goal_nodes([0])
evalenv=[env]
#env = CreateEnv('NWB_test_FixedEscapeInit',max_nodes=975,nfm_func_name = config['nfm_func'],var_targets=None, remove_world_pool=True, apply_wrappers=False)
#env = CreateEnv('MetroU3_e17tborder_FixedEscapeInit',max_nodes=33,nfm_func_name = config['nfm_func'],var_targets=[1,1], remove_world_pool=True, apply_wrappers=False)
#env = CreateEnv('MetroU3_e1t31_FixedEscapeInit',max_nodes=33,nfm_func_name = config['nfm_func'],var_targets=[1,1], remove_world_pool=True, apply_wrappers=False)
#env = CreateEnv('MetroU3_e17tborder_VariableEscapeInit',max_nodes=33,nfm_func_name = config['nfm_func'],var_targets=None, remove_world_pool=True, apply_wrappers=False)
#env, env_all_train_list = ConstructTrainSet(config, apply_wrappers=False, remove_paths=config['remove_paths'], tset=config['train_on']) #TODO check
# calcHeur=True
# if calcHeur:
# evaluate_spath_heuristic(logdir=config['rootdir']+'/heur/'+evalName, config=config, env_all=evalenv)
# continue
if config['demoruns']:
Q_func, Q_net, optimizer, lr_scheduler = init_model(config,fname=config['logdir']+'/SEED'+str(config['seed0'])+'/best_model.tar')
policy=GNN_s2v_Policy(Q_func)
while True:
entries=None#[1697]#ROT[427]#[5012,218,3903]
env = random.choice(evalenv)
a = SimulateAutomaticMode_DQN(env, policy, t_suffix=True, entries=entries)
if a == 'Q': break
#evalenv=SuperEnv([env], {1:0}, node_maxim, probs=[1])
#evalenv=[env]
#evalName='MetroU0_e1t31_vartarget_eval'
#evalName=eval_name
#n_eval=eval_num
evalResults[evalName]={'num_graphs.........':[],'num_graph_instances':[],'avg_return.........':[],'success_rate.......':[],}
for seed in config['seedrange']:
logdir=config['logdir']+'/SEED'+str(seed)
try:
assert os.path.exists(logdir)
except:
continue
result = evaluate(logdir=logdir, config=config, env_all=evalenv, eval_subdir=evalName)
#result = evaluate(logdir=config['logdir']+'/SEED'+str(seed), config=config, env_all=[env], eval_subdir=evalName)
num_unique_graphs, num_graph_instances, avg_return, success_rate = result
evalResults[evalName]['num_graphs.........'].append(num_unique_graphs)
evalResults[evalName]['num_graph_instances'].append(num_graph_instances)
evalResults[evalName]['avg_return.........'].append(avg_return)
evalResults[evalName]['success_rate.......'].append(success_rate)
for ename, results in evalResults.items():
OF = open(config['logdir']+'/Results_over_seeds_'+ename+'.txt', 'w')
def printing(text):
print(text)
OF.write(text + "\n")
np.set_printoptions(formatter={'float':"{0:0.3f}".format})
printing('Results over seeds for evaluation on '+ename+'\n')
for category,values in results.items():
printing(category)
printing(' avg over seeds: '+str(np.mean(values)))
printing(' std over seeds: '+str(np.std(values)))
printing(' per seed: '+str(np.array(values))+'\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Model hyperparameters
parser.add_argument('--emb_dim', default=64, type=int)
parser.add_argument('--emb_itT', default=5, type=int)
parser.add_argument('--num_epi', default=25000, type=int)
parser.add_argument('--mem_size', default=5000, type=int)
parser.add_argument('--nfm_func', default='NFM_ev_ec_t_dt_at_um_us', type=str)
parser.add_argument('--train_on', default='None', type=str)
parser.add_argument('--qnet', default='s2v', type=str)
parser.add_argument('--norm_agg', type=lambda s: s.lower() in ['true', 't', 'yes', '1'],default=True)
parser.add_argument('--optim_target', default='returns', type=str)
parser.add_argument('--tau', default=100, type=int)
parser.add_argument('--nstep', default=1, type=int)
parser.add_argument('--train', type=lambda s: s.lower() in ['true', 't', 'yes', '1'],default=False)
parser.add_argument('--eval', type=lambda s: s.lower() in ['true', 't', 'yes', '1'],default=False)
parser.add_argument('--test', type=lambda s: s.lower() in ['true', 't', 'yes', '1'],default=False)
parser.add_argument('--Etrain', default=[], type=list)
parser.add_argument('--Utrain', default=[], type=list)
parser.add_argument('--num_seeds', default=1, type=int)
parser.add_argument('--seed0', default=0, type=int)
parser.add_argument('--solve_select', default='solvable', type=str)
parser.add_argument('--edge_blocking', type=lambda s: s.lower() in ['true', 't', 'yes', '1'],default=True)
parser.add_argument('--max_nodes', default=25, type=int)
parser.add_argument('--demoruns', type=lambda s: s.lower() in ['true', 't', 'yes', '1'],default=True)
parser.add_argument('--pursuit', default='Uon', type=str)
args=parser.parse_args()
main(args)