def main(): '''can change the number of epsilons below''' seed(0) #set seed simple = False #simple is just %bridges out, which is computationally efficient number_of_highway_bridges = 1743 numeps = 3 #the number of epsilons tol = 0.00001 #the minimum annual rate that you care about in the original event set (the weight now is the original annual rate / number of epsilons per event) demand = bd.build_demand('input/BATS2000_34SuperD_TripTableData.csv', 'input/superdistricts_centroids.csv') #we just take a percentage in ita.py, namely #to get morning flows, take 5.3% of daily driver values. 11.5/(4.5*6+11.5*10+14*4+4.5*4) from Figure S10 of http://www.nature.com/srep/2012/121220/srep01001/extref/srep01001-s1.pdf #figure out ground motions lnsas, weights = ground_motions(numeps, tol, '/Users/mahalia/Documents/matlab/Research/Herbst2011/output_data/SF2_mtc_total_3909scenarios_1743bridgesPlusBART_3eps.txt') bart_dict = transit_to_damage.make_bart_dict() muni_dict = transit_to_damage.make_muni_dict() set_main_path('/Users/mahaliamiller/Desktop/trn/transit_lines/', None) #TODO: need to change THREE file paths (these plus bart) print 'the number of ground motion events we are considering: ', len(lnsas) index = 0 bridge_array = [] travel_index_times = [] # G = nx.read_gpickle("input/graphMTC_noCentroidsLength15.gpickle") G = nx.read_gpickle("input/graphMTC_CentroidsLength6.gpickle") # Directed! only one edge between nodes G = nx.freeze(G) #prevents edges or nodes to be added or deleted print 'am I a multi graph? ', G.is_multigraph() no_damage_travel_time, no_damage_vmt = compute_tt_vmt(G, demand) if not os.path.isdir(time.strftime("%Y%m%d")+'_filesForCube/'): os.mkdir(time.strftime("%Y%m%d")+'_filesForCube/') if not os.path.isdir(time.strftime("%Y%m%d")+'_filesForCube/transit/'): os.mkdir(time.strftime("%Y%m%d")+'_filesForCube/transit/') if not os.path.isdir(time.strftime("%Y%m%d")+'_filesForCube/modCapacities/'): os.mkdir(time.strftime("%Y%m%d")+'_filesForCube/modCapacities/') for scenario in lnsas: print index #figure out bridge damage for each scenario damaged_bridges, num_bridges_out = damage_bridges(scenario) #e.g., [1, 89, 598] #num_bridges_out is highway bridges only bridge_array.append(damaged_bridges) #figure out network damage and output Cube files to this effect G = damage_network(damaged_bridges, G, time.strftime("%Y%m%d")+'_filesForCube/', index) #figure out impact (performance metrics) flow, shortest_paths, travel_time, vmt = measure_performance(G, damaged_bridges, demand, no_damage_travel_time, no_damage_vmt) travel_index_times.append((index, num_bridges_out, flow, shortest_paths, travel_time, vmt, num_bridges_out/float(number_of_highway_bridges))) G = util.clean_up_graph(G) index +=1 # if index%3909 == 0: if index%100 == 0: save_results(bridge_array, travel_index_times, int(index/float(3909))) test(numeps, lnsas, damaged_bridges, damaged_graph, num_bridges_out, flow, shortest_paths, travel_time, vmt)
def make_cube_transit_damage(path, damaged_bridges_new): '''damages 4 public transit network based on the list of bridges. see the file transit_to_damage.py for more detalls. path is the destination of the damaged transit files. damaged bridges has the new ids (1-1743, 1744-3152''' try: if len(damaged_bridges_new) > 0: b = damaged_bridges_new[0]/10 except TypeError: raise('Sorry. You must use the new ids, which are all numbers, not strings') #get the data set_main_path('/Users/mahalia/ita/trn/transit_lines/', path) #path_to_unmodified, path_to_what_I_will_modify bart_dict = make_bart_dict() muni_dict = make_muni_dict() #make sure the files are clear clear_transit_file('Transit_Lines.block') #copies over a clean file clear_transit_file('BART.TPL') #copies over a clean file clear_transit_file('Munimetro.tpl') #copies over a clean file #wreak havoc damage_bart(damaged_bridges_new, bart_dict) damage_caltrain(damaged_bridges_new) damage_muni(damaged_bridges_new, muni_dict) damage_vta(damaged_bridges_new)
def main(): '''can change the number of epsilons below''' seed(0) #set seed simple = False #simple is just %bridges out, which is computationally efficient number_of_highway_bridges = 1743 numeps = 3 #the number of epsilons tol = 0.00001 #the minimum annual rate that you care about in the original event set (the weight now is the original annual rate / number of epsilons per event) demand = bd.build_demand('input/BATS2000_34SuperD_TripTableData.csv', 'input/superdistricts_centroids_dummies.csv') #we just take a percentage in ita.py, namely #to get morning flows, take 5.3% of daily driver values. 11.5/(4.5*6+11.5*10+14*4+4.5*4) from Figure S10 of http://www.nature.com/srep/2012/121220/srep01001/extref/srep01001-s1.pdf #figure out ground motions # lnsas, weights = ground_motions(numeps, tol, 'input/SF2_mtc_total_3909scenarios_1743bridgesPlusBART_1epsFake.txt') lnsas, weights, magnitudes = ground_motions(numeps, tol, 'input/SF2_mtc_total_3909scenarios_1743bridgesPlusBART_3eps.txt') # with open ('input/20140114_lnsas_1epsFake.pkl', 'wb') as f: # pickle.dump(lnsas, f) with open ('input/20140114_magnitudes_3eps.pkl', 'wb') as f: pickle.dump(magnitudes, f) with open('input/20140114_lnsas_3eps.pkl','rb') as f: lnsas = pickle.load(f) # with open('input/20140114_lnsas_1epsFake.pkl','rb') as f: # lnsas = pickle.load(f) print 'the number of ground motion events we are considering: ', len(lnsas) print 'first length: ', len(lnsas[0]) bart_dict = transit_to_damage.make_bart_dict() muni_dict = transit_to_damage.make_muni_dict() bridge_array_new = [] bridge_array_internal = [] travel_index_times = [] # G = nx.read_gpickle("input/graphMTC_noCentroidsLength15.gpickle") G = get_graph() print 'am I a multi graph? I really do not want to be!', G.is_multigraph() #An undirected graph class that can store multiedges. Multiedges are multiple edges between two nodes. Each edge can hold optional data or attributes.A MultiGraph holds undirected edges. Self loops are allowed. no_damage_travel_time, no_damage_vmt = compute_tt_vmt(G, demand) G = util.clean_up_graph(G) # make_directories(range(len(lnsas))) # transit_to_damage.set_main_path('input/trn/transit_lines/', 'input/trncopy/transit_lines/') #TODO: need to change THREE file paths (these plus bart) # run in SERIES #--------------------------------------------- # targets = [0, 5000] # # targets = range(len(lnsas)) # for i in targets: # print i # start = time.time() # damaged_bridges_internal, damaged_bridges_new, num_damaged_bridges, flow, shortest_paths, travel_time, vmt = compute_performance(lnsas[i], G, i, demand, no_damage_travel_time, no_damage_vmt) # bridge_array_internal.append(damaged_bridges_internal) # bridge_array_new.append(damaged_bridges_new) # travel_index_times.append((i, num_damaged_bridges, flow, shortest_paths, travel_time, vmt, num_damaged_bridges/float(number_of_highway_bridges), magnitudes[i])) # print 'time for one: ', time.time() - start # if i%3909 == 0: # save_results(bridge_array_internal, bridge_array_new, travel_index_times, int((i + 1)/float(3909))) # # scenario = lnsas[i] # # #figure out bridge damage for each scenario # # damaged_bridges_internal, damaged_bridges_new, num_damaged_bridges = damage_bridges(scenario) #e.g., [1, 89, 598] #num_bridges_out is highway bridges only # # bridge_array_internal.append(damaged_bridges_internal) # # bridge_array_new.append(damaged_bridges_new) # # #figure out network damage and output Cube files to this effect # # G = damage_network(damaged_bridges_internal, damaged_bridges_new, G, time.strftime("%Y%m%d")+'_filesForCube/', i) # # #figure out impact (performance metrics) # # flow, shortest_paths, travel_time, vmt = measure_performance(G, num_damaged_bridges, demand, no_damage_travel_time, no_damage_vmt) # # travel_index_times.append((i, num_damaged_bridges, flow, shortest_paths, travel_time, vmt, num_damaged_bridges/float(number_of_highway_bridges), magnitudes[i])) # # G = util.clean_up_graph(G) # # # if i%3909 == 0: # # if i%1 == 0: # # save_results(bridge_array_internal, bridge_array_new, travel_index_times, int(i/float(3909))) # # #--------------------------------------------- # # # run in PARALLEL # # # #--------------------------------------------- ppservers = () # Creates jobserver with automatically detected number of workers job_server = pp.Server(ppservers=ppservers) print "Starting pp with", job_server.get_ncpus(), "workers" start_time = time.time() # set up jobs jobs = [] targets = range(3909, len(lnsas)) #len(lnsas)) 7818, # targets = [0, 33, 5000] # for i in range(len(lnsas)): for i in targets: jobs.append(job_server.submit(compute_performance, (lnsas[i], None, i, demand, no_damage_travel_time, no_damage_vmt, ), modules = ('networkx', ))) # functions, modules # get results # if len(jobs) != len(lnsas): # pdb.set_trace() # error checking! index = 0 for job in jobs: (damaged_bridges_internal, damaged_bridges_new, num_damaged_bridges, flow, shortest_paths, travel_time, vmt) = job() i = targets[index] print 'target id: ', i bridge_array_internal.append(damaged_bridges_internal) bridge_array_new.append(damaged_bridges_new) travel_index_times.append((i, num_damaged_bridges, flow, shortest_paths, travel_time, vmt, num_damaged_bridges/float(number_of_highway_bridges), magnitudes[i])) if i%3909 == 0: save_results(bridge_array_internal, bridge_array_new, travel_index_times, int((i + 1)/float(3909))) index += 1 # #--------------------------------------------- save_results(bridge_array_internal, bridge_array_new, travel_index_times, int((i + 1)/float(3909)))