예제 #1
0
파일: output.py 프로젝트: sfcta/BikeRouter
def create_estimation_dataset(G,path_list_collection,id_list,trip_times,master_config):
	
	output_config=master_config.output_config
	choice_set_config=master_config.choice_set_config
	
	est_file=open(output_config['estimation_data'],'w')
	est_writer=csv.writer(est_file,lineterminator='\r')
	
	if output_config['path_size']:
		path_size_alias=['path_size']
	else:
		path_size_alias=[]
	
	est_writer.writerow(['occ','alt','trip_id','chosen']+output_config['aliases']+path_size_alias)
	
	path_size_data=path_size(G,path_list_collection,choice_set_config)
	
	for occ_idx in range(len(path_list_collection)):
		for alt_idx in range(len(path_list_collection[occ_idx])):
			path=path_list_collection[occ_idx][alt_idx]
			values=[]
			for i in range(len(output_config['variables'])):
				if output_config['variables'][i] in master_config['time_dependent_relation']:
					key=get_time_dependent_variable(output_config['variables'][i],trip_times[id_list[occ_idx]],master_config['time_dependent_relation'])
				else:
					key=output_config['variables'][i]
				values.append(str(path_trace(G,path,key,output_config['trace_funs'][i],output_config['final_funs'][i],output_config['weights'][i])))
			if output_config['path_size']:
				values.append(path_size_data[occ_idx][alt_idx])
			est_writer.writerow([str(occ_idx),str(alt_idx),str(id_list[occ_idx]),str(alt_idx==0)]+values)
	
	est_file.close()
예제 #2
0
def create_holdback_prediction_dataset(G, path_list_collection, id_list,
                                       trip_times, master_config,
                                       chosen_overlap):

    output_config = master_config.output_config
    choice_set_config = master_config.choice_set_config

    est_file = open(output_config['estimation_data'], 'w')
    est_writer = csv.writer(est_file, lineterminator='\r')

    if output_config['path_size']:
        path_size_alias = ['path_size']
    else:
        path_size_alias = []

    est_writer.writerow(['occ', 'alt', 'trip_id', 'chosen'] +
                        output_config['aliases'] + path_size_alias +
                        ['overlap'])

    sans_chosen = copy.deepcopy(path_list_collection)
    for path_list in sans_chosen:
        path_list.pop(0)
    path_size_data = path_size(G, sans_chosen, choice_set_config)

    for occ_idx in range(len(path_list_collection)):
        for alt_idx in range(len(path_list_collection[occ_idx])):
            path = path_list_collection[occ_idx][alt_idx]
            values = []
            for i in range(len(output_config['variables'])):
                if output_config['variables'][i] in master_config[
                        'time_dependent_relation']:
                    key = get_time_dependent_variable(
                        output_config['variables'][i],
                        trip_times[id_list[occ_idx]],
                        master_config['time_dependent_relation'])
                else:
                    key = output_config['variables'][i]
                values.append(
                    str(
                        path_trace(G, path, key,
                                   output_config['trace_funs'][i],
                                   output_config['final_funs'][i],
                                   output_config['weights'][i])))
            if output_config['path_size']:
                if alt_idx == 0:
                    values.append(0)
                else:
                    values.append(path_size_data[occ_idx][alt_idx - 1])
            values.append(chosen_overlap[occ_idx][alt_idx])
            est_writer.writerow([
                str(occ_idx),
                str(alt_idx),
                str(id_list[occ_idx]),
                str(alt_idx == 0)
            ] + values)

    est_file.close()
예제 #3
0
def trace_and_load(network,filtered_sets,master_config,matrix_list,source):
	config=master_config.assign_config
	this_network=network

	#trace
	print current_process().name, "- zone: ", source, "Tracing paths..."
	predict_collection={}
	for target in filtered_sets:
		predict_data=[]
		for path in filtered_sets[target]:
			trace_vals={}
			for i in range(len(config['variables'])):
				var=config['variables'][i]
				trace_vals[config['aliases'][i]]=path_trace(this_network,path,var,config['trace_funs'][i],config['final_funs'][i],config['weights'][i])
			for var in trace_vals:	
				if var in config['divisors']:
					trace_vals[var]=trace_vals[var]/trace_vals[config['divisors'][var]]
			predict_data.append(trace_vals)
		if config['path_size']:
			PS=path_size(this_network,[filtered_sets[target]],master_config.choice_set_config).pop()
			for i in range(len(filtered_sets[target])):
				if config['path_size_log']:
					PS[i]=log(PS[i])
				predict_data[i][config['path_size_alias']]=PS[i]
		predict_collection[target]=predict_data
	
	#apply logit probabilities
	print current_process().name, "- zone: ", source, "Applying logit probabilities..."
	
	to_load=[]
	for mat_idx in range(len(matrix_list)):
		to_load.append({})
		for target in predict_collection:
			num_pers=matrix_list[mat_idx][source-1,target-1]
			to_load[mat_idx][target]=simulate_mixed_logit(num_pers,predict_collection[target],config)
	
	#load onto network
	print current_process().name, "- zone: ", source, "Loading network..."
	for target in filtered_sets:
		for j in range(len(filtered_sets[target])):
			path=filtered_sets[target][j]
			if this_network.orig_network is None:
				for i in range(len(path)-1):
					for mat_idx in  range(len(matrix_list)):
						this_network[path[i]][path[i+1]][config['load_names'][mat_idx]]=this_network[path[i]][path[i+1]][config['load_names'][mat_idx]]+to_load[mat_idx][target][j]
			else:
				for i in range(1,len(path)-1):
					for mat_idx in  range(len(matrix_list)):
						this_network.orig_network[path[i][0]][path[i][1]][config['load_names'][mat_idx]]=this_network.orig_network[path[i][0]][path[i][1]][config['load_names'][mat_idx]]+to_load[mat_idx][target][j]
예제 #4
0
def trace_and_load(network, filtered_sets, master_config, matrix_list, source):
    config = master_config.assign_config
    this_network = network

    #trace
    print current_process().name, "- zone: ", source, "Tracing paths..."
    predict_collection = {}
    for target in filtered_sets:
        predict_data = []
        for path in filtered_sets[target]:
            trace_vals = {}
            for i in range(len(config['variables'])):
                var = config['variables'][i]
                trace_vals[config['aliases'][i]] = path_trace(
                    this_network, path, var, config['trace_funs'][i],
                    config['final_funs'][i], config['weights'][i])
            for var in trace_vals:
                if var in config['divisors']:
                    trace_vals[var] = trace_vals[var] / trace_vals[
                        config['divisors'][var]]
            predict_data.append(trace_vals)
        if config['path_size']:
            PS = path_size(this_network, [filtered_sets[target]],
                           master_config.choice_set_config).pop()
            for i in range(len(filtered_sets[target])):
                if config['path_size_log']:
                    PS[i] = log(PS[i])
                predict_data[i][config['path_size_alias']] = PS[i]
        predict_collection[target] = predict_data

    #apply logit probabilities
    print current_process(
    ).name, "- zone: ", source, "Applying logit probabilities..."

    to_load = []
    for mat_idx in range(len(matrix_list)):
        to_load.append({})
        for target in predict_collection:
            num_pers = matrix_list[mat_idx][source - 1, target - 1]
            to_load[mat_idx][target] = simulate_mixed_logit(
                num_pers, predict_collection[target], config)

    #load onto network
    print current_process().name, "- zone: ", source, "Loading network..."
    for target in filtered_sets:
        for j in range(len(filtered_sets[target])):
            path = filtered_sets[target][j]
            if this_network.orig_network is None:
                for i in range(len(path) - 1):
                    for mat_idx in range(len(matrix_list)):
                        this_network[path[i]][path[i + 1]][
                            config['load_names'][mat_idx]] = this_network[
                                path[i]][path[i + 1]][config['load_names'][
                                    mat_idx]] + to_load[mat_idx][target][j]
            else:
                for i in range(1, len(path) - 1):
                    for mat_idx in range(len(matrix_list)):
                        this_network.orig_network[path[i][0]][path[i][1]][
                            config['load_names']
                            [mat_idx]] = this_network.orig_network[path[i][0]][
                                path[i][1]][config['load_names'][
                                    mat_idx]] + to_load[mat_idx][target][j]