def bounding_box_worker(work_queue, done_queue_range,done_queue_vars,G,choice_set_config,time_dependent_relation,trip_data,trip_times): config=choice_set_config inverse_relation=get_inverse_time_dependent_relation(time_dependent_relation) idx=0 vars={} for id in iter(work_queue.get,'STOP'): idx=idx+1 cur_bound=find_coef_bounding_box(G,trip_data[id][0],trip_data[id][-1],choice_set_config,time_dependent_relation,trip_times[id[0]]) if choice_set_config['verbose']: print current_process().name, "-",idx,'CUR_BOUND: ', cur_bound this_bound={} for key in cur_bound: orig_key=key if key in inverse_relation: orig_key=inverse_relation[key] this_bound[orig_key]=cur_bound[key] vars[orig_key]=1 done_queue_range.put(cur_bound) done_queue_vars.put(vars) done_queue_vars.put('STOP') done_queue_range.put('STOP') return True
def get_extreme_bounding_box_multithreaded(G,choice_set_config,time_dependent_relation,trip_times,trip_data,n_threads=2): config=choice_set_config inverse_relation=get_inverse_time_dependent_relation(time_dependent_relation) id_sample=trip_data.keys() if config['bounding_box_sample_size']<len(trip_data): id_sample=random.sample(id_sample,config['bounding_box_sample_size']) work_queue = Queue() done_queue_range = Queue() done_queue_vars=Queue() processes = [] for trip_id in id_sample: work_queue.put(trip_id) for w in xrange(n_threads): p = Process(target=bounding_box_worker, args=(work_queue, done_queue_range,done_queue_vars,G,choice_set_config,time_dependent_relation,trip_data,trip_times)) p.start() processes.append(p) work_queue.put('STOP') all_bounds=[] vars={} for i in range(n_threads): all_bounds = all_bounds + list(iter(done_queue_range.get,'STOP')) this_vars= list(iter(done_queue_vars.get,'STOP')) vars=dict(vars,**this_vars[0]) for p in processes: p.join() ranges={} for var in vars: low=[] high=[] for bound in all_bounds: if var in bound: low.append(bound[var][0]) high.append(bound[var][1]) low.sort() high.sort() if low: low=gmean(low) high=gmean(high) if low<=high: ranges[var]=(low,high) else: print 'WARNING: coefficient for ' + var + ' had inconsistent high/low. Try increasing sample size for bounding box determination.' else: print 'WARNING: coefficient for ' + var + ' had no range. Try increasing sample size for bounding box determination.' if choice_set_config['verbose']: print 'EXT_BOUND: ', ranges return ranges
def get_extreme_bounding_box_random_sample(G,choice_set_config,time_dependent_relation,trip_times,trip_data): config=choice_set_config inverse_relation=get_inverse_time_dependent_relation(time_dependent_relation) id_sample=trip_data.keys() if config['bounding_box_sample_size']<len(trip_data): id_sample=random.sample(id_sample,config['bounding_box_sample_size']) all_bounds=[] cur_bound={} vars={} for id in id_sample: cur_bound=find_coef_bounding_box(G,trip_data[id][0],trip_data[id][-1],choice_set_config,time_dependent_relation,trip_times[id[0]]) if choice_set_config['verbose']: print 'CUR_BOUND: ', cur_bound this_bound={} for key in cur_bound: orig_key=key if key in inverse_relation: orig_key=inverse_relation[key] this_bound[orig_key]=cur_bound[key] vars[orig_key]=1 all_bounds.append(this_bound) ranges={} for var in vars: low=[] high=[] for bound in all_bounds: if var in bound: low.append(bound[var][0]) high.append(bound[var][1]) low.sort() high.sort() if low: low=gmean(low) high=gmean(high) if low<=high: ranges[var]=(low,high) else: print 'WARNING: coefficient for ' + var + ' had inconsistent high/low. Try increasing sample size or percentile for bounding box determination.' else: print 'WARNING: coefficient for ' + var + ' had no range. Try increasing sample size or percentile for bounding box determination.' if choice_set_config['verbose']: print 'EXT_BOUND: ', ranges return ranges