Beispiel #1
0
def ds_generate_master(G,chosen,choice_set_config,link_randomizer,time_dependent_relation,trip_time,ext_bound):
	
	config=choice_set_config
	source=chosen[0]
	target=chosen[-1]
	
	if ext_bound is not None:
		bounding_box=ext_bound
	else:
		bounding_box=find_coef_bounding_box(G,source,target,config,time_dependent_relation,trip_time)
	
	num_draws=config['ds_num_draws']
	
	varcoef={}
	master_set=[]
	for i in range(num_draws):
		
		#sample random coefficients from bounding box
		for prelim_key in bounding_box:
			key=get_time_dependent_variable(prelim_key,trip_time,time_dependent_relation)
			if config['log_prior']:
				varcoef[key]=exp(random.uniform(log(bounding_box[prelim_key][0]),log(bounding_box[prelim_key][1])))
			else:
				varcoef[key]=random.uniform(bounding_box[prelim_key][0],bounding_box[prelim_key][1])
		
		to_iter=1
		if config['randomize_after']:
			to_iter=config['randomize_after_iters']
			
		for i in range(to_iter):
			#perform generalized cost shortest path search
			master_set.append(bidirectional_dijkstra(G,source,target,varcoef,config['weights'],link_randomizer)[1])
		
	return master_set
def inverted_estimation_search_worker(work_queue, done_queue, network,
                                      trip_data, master_config, ext_bound):

    if not master_config.choice_set_config['randomize_after']:
        print time.asctime(time.localtime()), "-", current_process(
        ).name, "- initializing link randomizer..."
        link_randomizer = master_config.choice_set_config.get_link_randomizer(
            network, master_config)

    rand_net = network.copy()
    rand_net.orig_network = None
    bounding_box = ext_bound
    varcoef = {}
    """if '1' in current_process().name:
		for var in link_randomizer['variables']:
			print var," zero p: ",link_randomizer['zero']['probs'][var]
			print var," posi m: ",link_randomizer['pos']['means'][var]
			
	print "No randomize: ", link_randomizer['no_randomize']"""

    for i in iter(work_queue.get, 'STOP'):

        if master_config.choice_set_config[
                'inverted_nested'] and not master_config.choice_set_config[
                    'randomize_after']:
            #randomize link values
            for e in rand_net.edges_iter():
                for key in master_config.choice_set_config['variables']:
                    rand_net[e[0]][e[1]][key] = link_randomizer.generate_value(
                        network, e[0], e[1], key)

        #loop over number of parameter randomizations
        for j in range(master_config.choice_set_config['inverted_N_param']):

            print time.asctime(time.localtime()), "-", current_process(
            ).name, "- Attr #", i, ', Param #', j

            if not master_config.choice_set_config[
                    'inverted_nested'] and not master_config.choice_set_config[
                        'randomize_after']:
                #randomize link values
                for e in rand_net.edges_iter():
                    for key in master_config.choice_set_config['variables']:
                        rand_net[e[0]][
                            e[1]][key] = link_randomizer.generate_value(
                                network, e[0], e[1], key)

            #sample generalized cost coefficients
            for key in bounding_box:
                if master_config.choice_set_config['log_prior']:
                    varcoef[key] = exp(
                        random.uniform(log(bounding_box[key][0]),
                                       log(bounding_box[key][1])))
                else:
                    varcoef[key] = random.uniform(bounding_box[key][0],
                                                  bounding_box[key][1])

            print varcoef

            #calculate generalized costs
            for e in rand_net.edges_iter():
                rand_net[e[0]][e[1]]['gencost'] = 0
                for key in varcoef:
                    wgt = 1
                    if key in master_config.choice_set_config['weights']:
                        wgt = network[e[0]][e[1]][
                            master_config.choice_set_config['weights'][key]]
                    rand_net[e[0]][e[1]]['gencost'] = rand_net[e[0]][
                        e[1]]['gencost'] + varcoef[key] * rand_net[e[0]][
                            e[1]][key] * wgt

            use_cost = 'gencost'
            to_iter = 1
            if master_config.choice_set_config['randomize_after']:
                use_cost = 'usecost'
                to_iter = master_config.choice_set_config[
                    'randomize_after_iters']

            while to_iter:
                to_iter = to_iter - 1
                result_paths = {}

                if master_config.choice_set_config['randomize_after']:
                    for e in rand_net.edges_iter():
                        rand_net[e[0]][e[1]]['usecost'] = rand_net[e[0]][
                            e[1]]['gencost'] * random.uniform(
                                1 - master_config.
                                choice_set_config['randomize_after_dev'],
                                1 + master_config.
                                choice_set_config['randomize_after_dev'])

                for trip_id in trip_data.keys():

                    source = trip_data[trip_id][0]
                    target = trip_data[trip_id][-1]

                    #add gateways
                    if network.orig_network is not None:
                        for neighbor in network.orig_network.successors_iter(
                                source):
                            new_data = dict(
                                network.orig_network[source][neighbor],
                                **{use_cost: 0})
                            for key in varcoef:
                                if key not in [
                                        'TURN', 'L_TURN', 'R_TURN', 'U_TURN'
                                ]:
                                    wgt = 1
                                    if key in master_config.choice_set_config[
                                            'weights']:
                                        wgt = new_data[
                                            master_config.
                                            choice_set_config['weights'][key]]
                                    new_data[use_cost] = new_data[
                                        use_cost] + varcoef[key] * new_data[
                                            key] * wgt
                            rand_net.add_edge(source, (source, neighbor),
                                              new_data)
                        for neighbor in network.orig_network.predecessors_iter(
                                target):
                            rand_net.add_edge((neighbor, target), target,
                                              {use_cost: 0})

                    result_paths[trip_id] = bidirectional_dijkstra(
                        rand_net, source, target, use_cost)[1]

                    #remove gateways
                    if network.orig_network is not None:
                        for neighbor in network.orig_network.successors_iter(
                                source):
                            rand_net.remove_edge(source, (source, neighbor))
                        for neighbor in network.orig_network.predecessors_iter(
                                target):
                            rand_net.remove_edge((neighbor, target), target)

                done_queue.put(result_paths)

    done_queue.put('STOP')
    return True
Beispiel #3
0
def inverted_estimation_search_worker(work_queue,done_queue,network,trip_data,master_config,ext_bound):
	
	if not master_config.choice_set_config['randomize_after'] :
		print time.asctime(time.localtime()), "-", current_process().name, "- initializing link randomizer..."
		link_randomizer=master_config.choice_set_config.get_link_randomizer(network,master_config)
	
	rand_net=network.copy()
	rand_net.orig_network=None
	bounding_box=ext_bound
	varcoef={}
	
	"""if '1' in current_process().name:
		for var in link_randomizer['variables']:
			print var," zero p: ",link_randomizer['zero']['probs'][var]
			print var," posi m: ",link_randomizer['pos']['means'][var]
			
	print "No randomize: ", link_randomizer['no_randomize']"""
	
	for i in iter(work_queue.get,'STOP'):
		
		if master_config.choice_set_config['inverted_nested'] and not master_config.choice_set_config['randomize_after'] :
			#randomize link values
			for e in rand_net.edges_iter():
				for key in master_config.choice_set_config['variables']:
					rand_net[e[0]][e[1]][key]=link_randomizer.generate_value(network,e[0],e[1],key)
					
		#loop over number of parameter randomizations
		for j in range(master_config.choice_set_config['inverted_N_param']):
			
			print time.asctime(time.localtime()), "-", current_process().name, "- Attr #", i, ', Param #', j
			
			if not master_config.choice_set_config['inverted_nested'] and not master_config.choice_set_config['randomize_after']:
				#randomize link values
				for e in rand_net.edges_iter():
					for key in master_config.choice_set_config['variables']:
						rand_net[e[0]][e[1]][key]=link_randomizer.generate_value(network,e[0],e[1],key)			
					
			#sample generalized cost coefficients
			for key in bounding_box:
				if master_config.choice_set_config['log_prior']:
					varcoef[key]=exp(random.uniform(log(bounding_box[key][0]),log(bounding_box[key][1])))
				else:
					varcoef[key]=random.uniform(bounding_box[key][0],bounding_box[key][1])
					
			print varcoef
					
			#calculate generalized costs
			for e in rand_net.edges_iter():
				rand_net[e[0]][e[1]]['gencost']=0
				for key in varcoef:
					wgt=1
					if key in master_config.choice_set_config['weights']:
						wgt=network[e[0]][e[1]][master_config.choice_set_config['weights'][key]]
					rand_net[e[0]][e[1]]['gencost']=rand_net[e[0]][e[1]]['gencost']+varcoef[key]*rand_net[e[0]][e[1]][key]*wgt
			
			use_cost='gencost'
			to_iter=1
			if master_config.choice_set_config['randomize_after']:
				use_cost='usecost'
				to_iter=master_config.choice_set_config['randomize_after_iters']
				
			while to_iter:
				to_iter=to_iter-1
				result_paths={}
				
				if master_config.choice_set_config['randomize_after']:
					for e in rand_net.edges_iter():
						rand_net[e[0]][e[1]]['usecost']=rand_net[e[0]][e[1]]['gencost']*random.uniform(1-master_config.choice_set_config['randomize_after_dev'],1+master_config.choice_set_config['randomize_after_dev'])
					
				for trip_id in trip_data.keys():
				
					source=trip_data[trip_id][0]
					target=trip_data[trip_id][-1]
				
					#add gateways
					if network.orig_network is not None:
						for neighbor in network.orig_network.successors_iter(source):
							new_data=dict(network.orig_network[source][neighbor],**{use_cost:0})
							for key in varcoef:
								if key not in ['TURN','L_TURN','R_TURN','U_TURN']:
									wgt=1
									if key in master_config.choice_set_config['weights']:
										wgt=new_data[master_config.choice_set_config['weights'][key]]
									new_data[use_cost]=new_data[use_cost]+varcoef[key]*new_data[key]*wgt
							rand_net.add_edge(source,(source,neighbor),new_data)
						for neighbor in network.orig_network.predecessors_iter(target):
							rand_net.add_edge((neighbor,target),target,{use_cost:0})
						
					result_paths[trip_id]=bidirectional_dijkstra(rand_net,source,target,use_cost)[1]
				
					#remove gateways
					if network.orig_network is not None:
						for neighbor in network.orig_network.successors_iter(source):
							rand_net.remove_edge(source,(source,neighbor))
						for neighbor in network.orig_network.predecessors_iter(target):
							rand_net.remove_edge((neighbor,target),target)
						
				done_queue.put(result_paths)
	
	done_queue.put('STOP')
	return True
Beispiel #4
0
def find_coef_bounding_box(G,source,target,choice_set_config,time_dependent_relation,trip_time):
	
	final_bound={}
	
	config=choice_set_config
	verbose=False#config['verbose']
	
	for prelim_key in config['variables']:
		
		key=get_time_dependent_variable(prelim_key,trip_time,time_dependent_relation)
		
		if key==config['ref']:
			final_bound[key]=[1,1]
			continue
		vc={config['ref']:1}
		if key in config['median_compare']:
			for compare_key in final_bound:
				if key not in config['median_compare']:
					if config['log_prior']:
						vc[compare_key]=exp( (log(final_bound[compare_key][0])+log(final_bound[compare_key][1]))/2)
					else:
						vc[compare_key]=(final_bound[compare_key][0]+final_bound[compare_key][1])/2
		
		link_randomizer=None
		if key in config['randomize_compare']:
			if not config['randomize_after']:
				raise Exception, "randomize_compare not allowed without randomize_after"
			link_randomizer=config['randomize_after_dev']
		the_seed=random.randint(0,sys.maxint)
		
		if verbose:
			print vc
		cur_wgt=None
		if key in config['weights']:
			cur_wgt=config['weights'][key]
		myfun=lambda cur_coef: path_trace(
						G,
						bidirectional_dijkstra(G,source,target,dict(vc,**{key:cur_coef}),config['weights'],link_randomizer)[1],
						key, 'sum', wgtvar=cur_wgt
						)
		
		coef_min_low = coef_min_high = log(config['ranges'][prelim_key][0])
		coef_max_low = coef_max_high = log(config['ranges'][prelim_key][1])
		val_min_low = val_min_high = myfun(exp(coef_min_low))
		val_max_low = val_max_high = myfun(exp(coef_max_low))
		if verbose:	
			print key, "coef_min_low:", exp(coef_min_low)
			print key, "coef_max_low:", exp(coef_max_low)
			print key, "val_min_low:", val_min_low
			print key, "val_max_low:", val_max_low
		
		if val_min_low == val_max_low:
			if verbose:
				print key, "no range... ignoring"
			continue
		
		if verbose:	
			print key, "coef_min_low:", exp(coef_min_low)
			print key, "coef_max_low:", exp(coef_max_low)
			print key, "val_min_low:", val_min_low
			print key, "val_max_low:", val_max_low
		
		while True:
			random.seed(the_seed)
			
			coef_mid_low = (coef_min_low+coef_max_low)/2
			coef_mid_high = (coef_min_high+coef_max_high)/2
			val_mid_low =  myfun(exp(coef_mid_low))
			val_mid_high =  myfun(exp(coef_mid_high))
			
			if verbose:
				print key, "coef_mid_low:", exp(coef_mid_low)
				print key, "coef_mid_high:", exp(coef_mid_high)
				print key, "val_mid_low:", val_mid_low
				print key, "val_mid_high:", val_mid_high
			
			if val_mid_low==val_min_low:
				coef_min_low=coef_mid_low
			else:
				coef_max_low=coef_mid_low
				val_max_low=val_mid_low
			if val_mid_high==val_max_high:
				coef_max_high=coef_mid_high
			else:
				coef_min_high=coef_mid_high
				val_min_high=val_mid_high
			
			if verbose:
				print key, "coef_low:", (exp(coef_min_low),exp(coef_max_low))
				print key, "val_low:", (val_min_low,val_max_low)
				print key, "coef_high:", (exp(coef_min_high),exp(coef_max_high))
				print key, "val_high:", (val_min_high,val_max_high)
			
			if (coef_max_low-coef_min_low)<config['tolerance']:
				break
		if coef_mid_low!=coef_mid_high:
			final_bound[key]=[exp(coef_mid_low),exp(coef_mid_high)]
	
	if verbose:
		print final_bound
	
	return final_bound