def sweep_paras(zone, n_iter=1, data_version=None, force=False):
	#paras_iter = {'sig_V':[0., 0.1]}
	#paras = Paras({'sig_V':0.})

	main_dir = os.path.abspath(__file__)
	main_dir = os.path.split(os.path.dirname(main_dir))[0]

	choose_paras('nsim', 1)
	choose_paras('tmp_from_file', 1)

	config_file = main_dir + '/abm_tactical/config/config.cfg'
	#loop(paras_iter, paras_iter.keys(), paras, thing_to_do=do, paras=paras, build_pat=build_pat)
	
	input_file = result_dir + '/trajectories/M1/trajs_' + zone + '_real_data.dat'
	#input_file = main_dir + '/trajectories/M1/trajs_for_testing_10_sectors.dat'
	
	produce_M1_trajs_from_data(zone=zone, data_version=data_version, put_fake_sectors=True, save_file=input_file)

	with open(main_dir + '/libs/All_shapes_334.pic','r') as f:
		all_shapes = pickle.load(f)
	boundary = list(all_shapes[zone]['boundary'][0].exterior.coords)
	assert boundary[0]==boundary[-1]

	with open(result_dir + '/abm_tactical/config/bound_latlon.dat', 'w') as f:
		for x, y in boundary:
			f.write(str(x) + '\t' + str(y) + '\n')

	compute_temporary_points(50000, boundary)

	#sig_V_iter = [0.] + [10**(-float(i)) for i in range(5, -1, -1)]
	sig_V_iter = np.arange(0., 0.26, 0.04)
	#sig_V_iter = [10**(-float(i)) for i in range(4, -1, -1)]
	#sig_V_iter = [0., 0.0001] # [0.] + [10**(-float(i)) for i in range(5, -1, -1)]
	#t_w_iter = [40, 80, 120, 160, 240] # times 8 sec 
	t_w_iter = [40, 60, 80, 100, 120]#, 160, 240] # times 8 sec 
	#t_w_iter = [40, 80] # [40, 80, 120, 160, 240] # times 8 sec 
	print 
	for sig_V in sig_V_iter:
		print "sig_V=", sig_V
		choose_paras('sig_V', sig_V)
		for t_w in t_w_iter:
			print "t_w=", t_w
			choose_paras('t_w', t_w)

			for i in range(n_iter):
				counter(i, n_iter, message="Doing iterations... ")
				output_file = result_dir + '/trajectories/M3/trajs_' + zone + '_real_data_sigV' + str(sig_V) + '_t_w' + str(t_w) + '_' + str(i) + '.dat'
				if not os.path.exists(output_file.split('.dat')[0] + '_0.dat') or force:
					with stdout_redirected(to=result_dir + '/trajectories/M3/log_trajs_' + zone + '_real_data_sigV' + str(sig_V) + '_t_w' + str(t_w) + '_' + str(i) + '.txt'):
						do_ABM_tactical(input_file, output_file, config_file, verbose=1)
		print
示例#2
0
def sweep_paras_shocks(zone, paras, input_files, config_file, shock_file_zone, bound_file_zone, tmp_navpoints_file_zone, capacity_file, force=False, trajectories=None,\
	temp_config_dir=main_dir+'/abm_tactical/config_temp', n_cores=1, suff = '', dryrun=False, **kwargs):

	"""
	Script which sweeps two levels of parameters for the tactical ABM. Config files have to be given externally.
	The script creates a temporary folder to store all config files and avoid interactions between scripts.
	"""

	# config_file=main_dir + '/abm_tactical/config/config.cfg',\
	# shock_file=main_dir + '/abm_tactical/config/shock_tmp.dat',\
	# bound_file=main_dir + '/abm_tactical/config/bound_latlon.dat',\
	# tmp_navpoints_file=main_dir + '/abm_tactical/config/temp_nvp.dat',
	
	# config_file_temp = temp_config_dir + '/config.cfg'

	# print "Sweeping parameters with", n_cores, "cores."

	os.system('mkdir -p ' + temp_config_dir)
	
	# Targets
	shock_file = temp_config_dir + '/shock_tmp.dat'
	bound_file = temp_config_dir + '/bound_latlon.dat'
	tmp_navpoints_file = temp_config_dir + '/temp_nvp.dat'

	if not dryrun:	
		if trajectories!=None:
			write_trajectories_for_tact(trajectories, fil=input_file) 

		# Boundaries 
		with open(main_dir + '/libs/All_shapes_334.pic','r') as f:
			all_shapes = pickle.load(f)
		boundary = list(all_shapes[zone]['boundary'][0].exterior.coords)
		assert boundary[0]==boundary[-1]

		# Bounds
		if not os.path.exists(bound_file_zone):
			with open(bound_file_zone, 'w') as f:
				for x, y in boundary:
					f.write(str(x) + '\t' + str(y) + '\n')
		os.system('cp ' + bound_file_zone + ' ' + bound_file)


		# Temporary navpoints
		if not os.path.exists(tmp_navpoints_file_zone) or 1:
			tmp_nvp = compute_temporary_points(50000, boundary, save_file=tmp_navpoints_file_zone)
		os.system('cp ' + tmp_navpoints_file_zone + ' ' + tmp_navpoints_file)

		# Points for shocks
		if not os.path.exists(shock_file_zone) or 1:
			#points = points_in_sectors(zone, all_shapes)
			points = tmp_nvp[:100]
			with open(shock_file_zone, 'w') as f:
				for x, y in points:
					f.write(str(x) + '\t' + str(y) + '\n')
		os.system('cp ' + shock_file_zone + ' ' + shock_file)

	# Copy config file in temporary folder 
	os.system('cp ' + config_file + ' ' + temp_config_dir + '/config.cfg')
	config_file = temp_config_dir + '/config.cfg'

	files, logs = [], []
	#Iterations on every input files

	for input_file in input_files:
		print "Input file:", input_file
		name_par1, name_par2 = tuple(paras['paras_to_loop'])
		for par1 in paras[name_par1 + '_iter']:
			print name_par1, "=", par1
			paras.update(name_par1, par1, config_file=config_file)
			for par2 in paras[name_par2 + '_iter']:
				print name_par2, "=", par2
				paras.update(name_par2, par2, config_file=config_file)
				print 'Updated parameters:'
				for par in paras.keys():
					print ' --', par, '=', paras[par] 
				with clock_time():
					output_file = name_results_shocks(paras, m1_file_name=input_file, zone=zone, suff=suff)
					for i in range(paras['nsim']):
						#counter(i, n_iter, message="Doing iterations... ")
						files.append((input_file, output_file.split('.dat')[0] + '_' + str(i) + '.dat'))
					
					#print output_file
					#print output_file.split('.dat')[0] + '_' + str(paras['nsim']-1) + '.dat'
					#print os.path.exists(output_file.split('.dat')[0] + '_' + str(paras['nsim']-1) + '.dat')
					#raise Exception()
					if (not os.path.exists(output_file.split('.dat')[0] + '_' + str(paras['nsim']-1) + '.dat') or force) and not dryrun:
						#logs.append(output_file.split('.dat')[0] + '_' + str(paras['nsim']-1) + '.txt')
						
						#with stdout_redirected(to=output_file.split('.dat')[0] + '_' + str(paras['nsim']-1) + suff + '.txt'):
						do_ABM_tactical(input_file, output_file, config_file, verbose=1, 
								shock_tmp=shock_file,
								bound_latlon=bound_file,
								temp_nvp=tmp_navpoints_file,
								capacity_file=capacity_file)
		print

	# print "Running parallelized sweep..."
	# p = Pool(n_cores)
	# chouip = {'suff':suff, 'config_file':config_file, 'shock_file':shock_file, 'tmp_navpoints_file':tmp_navpoints_file, 'bound_file':bound_file, 'force':force, 'zone':zone}
	# coin =  p.map(do_sweep_on, [(iptf, paras, chouip) for iptf in input_files])
	# #coin =  p.map(do_sweep_on, list(range(10)))
	# #print coin
	# p.close()



	# files, logs = zip(*coin)
	# files = [ff for f in files for ff in f]
	# logs = [ff for f in logs for ff in f]

	# for fs in files:
	# 	print fs
	
	# raise Exception()

	return files, logs