def __init__(self, idx, args, barrier, netdata, agent_ids, rl_stats, exp_replay): Process.__init__(self) self.idx = idx self.args = args self.barrier = barrier self.netdata = netdata self.agent_ids = agent_ids self.rl_stats = rl_stats self.exp_replay = exp_replay self.save_t = 0 self.replay_fp = self.args.save_replay + '/' + self.args.tsc + '/' #for saving agent progress if self.idx == 0: path = 'tmp/' check_and_make_dir(path) now = get_time_now() self.updates_path = path + str( self.args.tsc) + '_' + str(now) + '_agent_updates.csv' self.replay_path = path + str( self.args.tsc) + '_' + str(now) + '_agent_replay.csv' self.n_exp_path = path + str( self.args.tsc) + '_' + str(now) + '_agent_nexp.csv' self.tsc_ids = list(sorted(list(self.netdata['inter'].keys()))) #write header line with tsc names write_line_to_file(self.updates_path, 'a+', ','.join([now] + self.tsc_ids)) write_line_to_file(self.replay_path, 'a+', ','.join([now] + self.tsc_ids)) write_line_to_file(self.n_exp_path, 'a+', ','.join([now] + self.tsc_ids))
def main(): global_params() #you must have the same number of colours as labels # colours = ['b', 'c', 'orange', 'y', 'm', 'gray', 'black', 'green'] # labels = {'ddpg':'DDPG', 'dqn':'DQN', 'sotl':'SOTL', 'maxpressure':'Max-pressure', 'sunpressure':'Sun-pressure', , 'normpressure':'Norm-pressure', 'websters':'Webster\'s', 'uniform':'Uniform'} # labels = {'ddpg':'DDPG', 'dqn':'DQN', 'sotl':'SOTL', 'maxpressure':'Max-pressure', 'websters':'Webster\'s', 'uniform':'Uniform'} colours = ['b', 'c', 'orange', 'y', 'm', 'gray', 'black'] labels = {'ddpg':'DDPG', 'dqn':'DQN', 'sotl':'SOTL', 'maxpressure':'Max-pressure', 'sunpressure':'Sun-pressure', 'normpressure':'Norm-pressure', 'uniform':'Uniform'} if len(colours) != len(labels): assert 0, 'Error: the number of colours '+str(len(colours))+' does not equal the number of labels'+str(len(labels)) #make dict of labels to colours colours = { l:c for c, l in zip(colours, labels)} args = parse_cl_args() check_and_make_dir(args.save_dir) if args.type == 'moe': fp = 'metrics/' graph_travel_time(labels, colours, fp, args.save_dir) metrics = ['queue', 'delay'] graph_individual_intersections(labels, colours, fp, metrics, args.save_dir) elif args.type == 'hp': fp = 'hp/' graph_hyper_params(labels, colours, fp, args.save_dir) else: assert 0, print('Error, supplied graph type argument '+str(args.type)+' does not exist')
def main(): start = time.time() args = parse_cl_args() #get hyperparams for command line supplied tsc tsc_str = args.tsc hp_dict = get_hp_dict(tsc_str) hp_order = sorted(list(hp_dict.keys())) hp_list = [hp_dict[hp] for hp in hp_order] #use itertools to produce cartesian product of hyperparams hp_set = list(itertools.product(*hp_list)) print(str(len(hp_set))+' total hyper params') #where to find metrics hp_travel_times = {} metrics_fp = 'metrics/'+tsc_str #where to print hp results path = 'hyperparams/'+tsc_str+'/' check_and_make_dir(path) fname = get_time_now() hp_fp = path+fname+'.csv' write_line_to_file(hp_fp, 'a+', ','.join(hp_order)+',mean,std,mean+std' ) #run each set of hp from cartesian product for hp in hp_set: hp_cmds = create_hp_cmds(args, hp_order, hp) #print(hp_cmds) for cmd in hp_cmds: os.system(cmd) #read travel times, store mean and std for determining best hp set hp_str = ','.join([str(h) for h in hp]) travel_times = get_hp_results(metrics_fp+'/traveltime/') hp_travel_times[hp_str] = {'mean':int(np.mean(travel_times)), 'std':int(np.std(travel_times))} write_temp_hp(hp_str, hp_travel_times[hp_str], hp_fp) #generate_returns(tsc_str, 'metrics/', hp_str) save_hp_performance(travel_times, 'hp/'+tsc_str+'/', hp_str) #remove all metrics for most recent hp shutil.rmtree(metrics_fp) #remove temp hp and write ranked final results os.remove(hp_fp) rank_hp(hp_travel_times, hp_order, tsc_str, hp_fp) print('All hyperparamers performance can be viewed at: '+str(hp_fp)) print('TOTAL HP SEARCH TIME') secs = time.time()-start print(str(int(secs/60.0))+' minutes ')
def write_sim_tsc_metrics(self): #get data dict of all tsc in sim #where each tsc has dict of all metrics tsc_metrics = self.sim.get_tsc_metrics() #create file name and path for writing metrics data #now = datetime.datetime.now() #fname = str(self.idx)+'_'+str(now).replace(" ","-") fname = get_time_now() #write all metrics to correct path #path = 'metrics/'+str(self.args.tsc) path = 'metrics/'+str(self.args.tsc) for tsc in tsc_metrics: for m in tsc_metrics[tsc]: mpath = path + '/'+str(m)+'/'+str(tsc)+'/' check_and_make_dir(mpath) save_data(mpath+fname+'_'+str(self.eps)+'_.p', tsc_metrics[tsc][m]) travel_times = self.sim.get_travel_times() path += '/traveltime/' check_and_make_dir(path) save_data(path+fname+'.p', travel_times)
def save_weights(self, nettype, path, fname): check_and_make_dir(path) self.models[nettype].save_weights(path + fname + '.h5', save_format='h5', overwrite='True')
def save_weights(self, nettype, path, fname): check_and_make_dir(path) weights = self.get_weights('online') save_data(path + fname + '.p', weights)
def save_replays(self): check_and_make_dir(self.replay_fp) for _id in self.agent_ids: save_data(self.replay_fp+_id+'.p', [ _ for _ in self.exp_replay[_id]]) print('FINISHED SAVING REPLAY FOR '+str(_id))
def main(): start = time.time() args = parse_cl_args() #get hyperparams for command line supplied tsc tsc_str = args.tsc hp_dict = get_hp_dict(tsc_str) hp_order = sorted(list(hp_dict.keys())) hp_list = [hp_dict[hp] for hp in hp_order] #use itertools to produce cartesian product of hyperparams hp_set = list(itertools.product(*hp_list)) print(str(len(hp_set))+' total hyper params') #where to find metrics hp_travel_times = {} metrics_fp = 'metrics/'+tsc_str #where to print hp results path = 'hyperparams/'+tsc_str+'/' check_and_make_dir(path) # hp_optimize for real cycle fname = 'real' hp_fp = path+fname+'.csv' write_line_to_file(hp_fp, 'a+', ','.join(hp_order)+',mean,std,mean+std' ) tt_hp = {} # store all travel_times for corresponding hp for hp in hp_set: hp_cmds = create_hp_cmds(args, hp_order, hp) #print(hp_cmds) travel_times = [] if args.demand == 'linear': assert False, 'demand should be real!!' elif args.demand == 'dynamic': assert False, 'demand should be real!!' elif args.demand == 'real': cmd_test = hp_cmds[-1] + ' -demand ' + 'real' else: assert False, 'Please only give demand: real' os.system(cmd_test) #read travel times, store mean and std for determining best hp set hp_str = ','.join([str(h) for h in hp]) travel_times += get_hp_results(metrics_fp+'/traveltime/') tt_hp[hp_str] = travel_times # !!!!!!!!!! the travel_times is cumulated in 10 processes n_v_pass = len(travel_times) #remove all metrics for most recent hp shutil.rmtree(metrics_fp) hp_travel_times[hp_str] = {'mean':int(np.mean(travel_times)), 'std':int(np.std(travel_times)), 'n_v_pass':n_v_pass} write_temp_hp(hp_str, hp_travel_times[hp_str], hp_fp) #generate_returns(tsc_str, 'metrics/', hp_str) save_hp_performance(travel_times, 'hp/'+tsc_str+'/', hp_str) #remove temp hp and write ranked final results os.remove(hp_fp) rank_hp(hp_travel_times, hp_order, tsc_str, hp_fp, tt_hp) print('All hyperparamers performance can be viewed at: '+str(hp_fp)) print('TOTAL HP SEARCH TIME') secs = time.time()-start print(str(int(secs/60.0))+' minutes ') ''' # hp_optimize for each linear cycle for idx_cycle in range(30): fname = 'cycle_'+str(idx_cycle).zfill(2) hp_fp = path+fname+'.csv' write_line_to_file(hp_fp, 'a+', ','.join(hp_order)+',mean,std,mean+std' ) #run each set of hp from cartesian product tt_hp = {} # store all travel_times for corresponding hp for hp in hp_set: hp_cmds = create_hp_cmds(args, hp_order, hp) #print(hp_cmds) travel_times = [] # only train the ddpg and dqn with the predefined sine wave if args.tsc == 'ddpg' or args.tsc == 'dqn': os.system(hp_cmds[0]) if args.demand == 'linear': cmd_test = hp_cmds[-1] + ' -demand ' + 'linear_' + str(idx_cycle).zfill(2) elif args.demand == 'dynamic': cmd_test = hp_cmds[-1] + ' -demand ' + 'dynamic' elif args.demand == 'real': cmd_test = hp_cmds[-1] + ' -demand ' + 'real' else: assert False, 'Please only give demand: linear, dynamic or real' os.system(cmd_test) #read travel times, store mean and std for determining best hp set hp_str = ','.join([str(h) for h in hp]) travel_times += get_hp_results(metrics_fp+'/traveltime/') tt_hp[hp_str] = travel_times # !!!!!!!!!! the travel_times is cumulated in 8 processes n_v_pass = len(travel_times) #remove all metrics for most recent hp shutil.rmtree(metrics_fp) hp_travel_times[hp_str] = {'mean':int(np.mean(travel_times)), 'std':int(np.std(travel_times)), 'n_v_pass':n_v_pass} write_temp_hp(hp_str, hp_travel_times[hp_str], hp_fp) #generate_returns(tsc_str, 'metrics/', hp_str) save_hp_performance(travel_times, 'hp/'+tsc_str+'/', hp_str) #remove temp hp and write ranked final results os.remove(hp_fp) rank_hp(hp_travel_times, hp_order, tsc_str, hp_fp, tt_hp) # ??? 记得把simlen 调到3600 # ??? 根据30 cycle 的结果调整hyper parameter 的范围,主要是maxpressure 和 uniform # ?????? n_vehilce_passed # ?????? n_vehilce_passed # ?????? n_vehilce_passed # ?????? n_vehilce_passed # ?????? n_vehilce_passed # ?????? n_vehilce_passed # ?????? n_vehilce_passed # ???? 找个地方把每个cycle 的best hyperparameter, mean, std, n_vehilce_passed 存起来 print('All hyperparamers performance can be viewed at: '+str(hp_fp)) print('TOTAL HP SEARCH TIME') secs = time.time()-start print(str(int(secs/60.0))+' minutes ') ''' '''
def save_hp_performance(data, path, hp_str): check_and_make_dir(path) #name the return the unique hp string save_data(path+hp_str+'.p', data)