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 ')
Пример #2
0
 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))
Пример #3
0
 def write_n_exp_progress(self):
     n_replay = [str(self.rl_stats[i]['n_exp']) for i in self.tsc_ids]
     write_line_to_file( self.n_exp_path, 'a+', ','.join([get_time_now()]+n_replay) )
Пример #4
0
 def write_replay_progress(self):
     n_replay = [str(len(self.exp_replay[i])) for i in self.tsc_ids]
     write_line_to_file( self.replay_path, 'a+', ','.join([get_time_now()]+n_replay) )
Пример #5
0
 def write_training_progress(self):
     updates = [str(self.rl_stats[i]['updates']) for i in self.tsc_ids]
     write_line_to_file( self.updates_path, 'a+', ','.join([get_time_now()]+updates) )
Пример #6
0
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 ')
    '''
    
    '''
Пример #7
0
def write_temp_hp(hp, results, fp):
    write_line_to_file(fp, 'a+', hp+','+str(results['mean'])+','+str(results['std'])+','+str(results['mean']+results['std']))