def initialize(traci): # Load targets from file if not env.target_file == None: print("Loading targets from file \"%s\"." % (env.target_file)) env.target_nodes = purr.readJSON(env.target_file) # Otherwise use cone spread else: env.target_nodes = cone_spread() purr.save("temp/targets.json", env.target_nodes) if env.target_generate: print( "Target's Generated. Exiting. Check temp/targets.json for targets." ) sys.exit(0) # Mark the targets on the map for node in env.target_nodes: xy = (float(node['x']), float(node['y'])) preprocess.add_radius_polygon(traci, xy, 30, (255, 153, 204), layer=3) # Add target dictionaries to the env for node in env.target_nodes: target = {'id': node['id'], 'sampling times': [], 'sampling vids': []} env.targets.append(target) continue print("Complete!") return
def initialize_edges_for_spawns_and_sinks(traci): print("Initializeing edges/nodes.") initialize_edges_and_nodes() print("Finding spawn edge candidates.") if not os.path.exists('temp'): os.mkdir('temp') generate_spawns = True try: env.spawn_edge, last_spawn_point = purr.load('temp/spawns.bin') if last_spawn_point == env.point_spawn: generate_spawns = False except FileNotFoundError: pass if generate_spawns: env.spawn_edge = get_valid_edges_from_point(env.point_spawn,env.radius_spawn_sink) purr.save('temp/spawns.bin',[env.spawn_edge,env.point_spawn]) add_radius_polygon(traci,env.point_spawn,env.radius_spawn_sink,(0,255,0)) print("Finding sink edge candidates.") generate_sinks = True try: env.sink_edge,last_sink_point = purr.load('temp/sinks.bin') if last_sink_point == env.point_sink: generate_sinks = False except FileNotFoundError: pass if generate_sinks: env.sink_edge = get_valid_edges_from_point(env.point_sink,env.radius_spawn_sink) purr.save('temp/sinks.bin',[env.sink_edge,env.point_sink]) add_radius_polygon(traci,env.point_sink,env.radius_spawn_sink,(255,0,0)) return
def run_sumo(self): purr.save(self.points_pybin, (self.vPs_nodes, self.vPd_nodes, self.tP_nodes)) for method in ("greedy", "smart"): cmd = "python runner.py " # ~ cmd += "--nogui " cmd += "--map-dir=%s " % (self.map_dir) cmd += "--veh.total=%d " % (self.N) cmd += "--veh.exists.max=%d " % (self.N) cmd += "--method=%s " % (method) cmd += "--points.file=%s " % (self.points_pybin) cmd += "--nash.R=%d " % (self.R) cmd += "--nash.tau=%f " % (self.tau) cmd += "--debug " if method == "greedy": cmd += "--out.dir=%s" % (self.out_dir_sumo_greedy) else: cmd += "--out.dir=%s" % (self.out_dir_sumo_smart) os.system(cmd) if method == "greedy": self.nA_sumo_greedy = purr.load(env.nA_greedy_file) else: self.nA_sumo_smart = purr.load(env.nA_smart_file) # ~ purr.pause() continue
def load_complete_spm(): complete_spm_pybin = "temp/complete-spm.pybin" try: env.spm_complete = purr.load(complete_spm_pybin) except FileNotFoundError: nodes = list(env.nx.nodes) weights = [] n = 0 total = len(nodes) * len(nodes) for irow, rn in enumerate(nodes): row = [] for icol, cn in enumerate(nodes): weight = float('inf') if not cn == rn: weight = None row.append(weight) n += 1 purr.update(n, total, "Generating Complete SPM...") continue weights.append(row) continue spm = {'colnames': nodes, 'rownames': nodes, 'weights': weights} env.spm_complete = spm purr.save(complete_spm_pybin, env.spm_complete) print("Complete!") return
def dst2src_point_validation(self): print("Determining valid src and destination points.") while True: # Determine vehicle start/dest and target SUMO nodes self.vP, self.tP = ss2.getLocalizedPlacement(2 * self.N, self.M) vP_nodes = self.correlateNormalPoints2SumoNodes( self.vP, "Determining vehicle start/dest SUMO nodes ") self.vPs_nodes = [] self.vPd_nodes = [] for i, node in enumerate(vP_nodes): node['index'] = i if i % 2 == 0: self.vPs_nodes.append(node) else: self.vPd_nodes.append(node) continue del (vP_nodes) self.tP_nodes = self.correlateNormalPoints2SumoNodes( self.tP, "Correlating target SUMO nodes ") purr.save(self.points_pybin, (self.vPs_nodes, self.vPd_nodes, self.tP_nodes)) # Run SUMO to validate points cmd = "python runner.py " cmd += "--nogui " cmd += "--map-dir=%s " % (self.map_dir) cmd += "--veh.total=%d " % (self.N) cmd += "--veh.exists.max=%d " % (self.N) cmd += "--method=%s " % ("validate.pts") cmd += "--points.file=%s " % (self.points_pybin) os.system(cmd) if purr.load(env.status_file): break continue self.vPs_nodes, self.vPd_nodes, self.tP_nodes = purr.load( env.valid_points_file) zipped_vP = [] for i, node in enumerate(self.vPs_nodes): zipped_vP.append(node['index']) zipped_vP.append(self.vPd_nodes[i]['index']) temp = [self.vP[index] for index in zipped_vP] self.vP = temp print("Complete") return
def run(): map_dir = "../london-seg4/data/" N = 100 #100 # Vehicles M = 25 #25 # Targets tau = 1 R = 500 # Rewards ss = SimulationSychronizer(map_dir, N, M, tau, R) ss.run_sumo() ss.run_geometric() purr.save("temp/ss.pybin", ss) ss.plot() print("SUCCESS!") return
def finalize(): if os.path.exists(env.out_dir): purr.deldir(env.out_dir) os.mkdir(env.out_dir) vehicle.csv() vehicle.out_pretty() target.csv() target.out_pretty() if env.method == "greedy": purr.save(env.nA_greedy_file, env.nash_assigner) else: purr.save(env.nA_smart_file, env.nash_assigner) return
def main(): banner() global OPTIONS; OPTIONS = get_options() global SN; SN = read_graph() nids = list(SN.nodes)[:10] weights = [] n = 0; total = 100*100#len(nids)*len(nids) for i,nid_from in enumerate(nids): row = list() for j,nid_to in enumerate(nids): try: path = nx.dijkstra_path(SN,nid_from,nid_to) weight = path_info(SN,path) if weight == 0: weight = float("inf") except nx.NetworkXNoPath: weight = float("inf") row.append(weight) n += 1 if n % 10 == 0: print("Progress %d/%d %.2f%%" % (n,total, n/total*100), end='\r') continue weights.append(tuple(row)) continue weights = tuple(weights) print('%10s ' % (''),end='') for i,row in enumerate(weights): print('%7s ' % (nids[i][:7]), end='') print() for i,row in enumerate(weights): print('%10s:' % (nids[i][:10]), end='') for val in row: if val == float('inf'): print(' inf ',end='') else: print('%7.2f ' % (val),end='') print() purr.save('weight.pydata',weights) return
def initialize(): env.vehicles = [None for iveh in range(env.veh_total)] for i, item in enumerate(env.vehicles): veh = vehdict() veh['id'] = 'veh%d' % (i) try: veh['shortest path'] = shortest_path(i) # Starting points xy = (float(env.vPs[i]['x']),float(env.vPs[i]['y'])) preprocess.add_radius_polygon(xy,50,(0,0,255),3) preprocess.add_radius_polygon(xy,10,(0,255,255),4) # Ending points xy = (float(env.vPd[i]['x']),float(env.vPd[i]['y'])) preprocess.add_radius_polygon(xy,50,(255,0,0),3) preprocess.add_radius_polygon(xy,10,(255,255,0),4) except nx.NetworkXNoPath: # Starting points xy0 = (float(env.vPs[i]['x']),float(env.vPs[i]['y'])) preprocess.add_radius_polygon(xy0,50,(0,0,150),i+3) preprocess.add_radius_polygon(xy0,10,(0,150,150),i+4) # Ending points xy1 = (float(env.vPd[i]['x']),float(env.vPd[i]['y'])) preprocess.add_radius_polygon(xy1,50,(150,0,0),i+3) preprocess.add_radius_polygon(xy1,10,(150,150,0),i+4) purr.save(env.status_file,False) print("\nPoints improperly placed.") sys.exit(0) pass veh['source'] = veh['shortest path']['nids'][0] veh['destination'] = veh['shortest path']['nids'][-1] veh['shortest path length'] = veh['shortest path']['weight'] env.vehicles[i] = veh purr.update(i+1,env.veh_total,"Initializing vehicles ") continue return
def validate(): if not env.method == "validate.pts": return vPs = [] vPd = [] n = 0 total = env.veh_total for i in range(2 * env.veh_total): try: vehicle.shortest_path(i) n += 1 purr.update(n, total, "Gathering valid points ") vPs.append(env.vPs[i]) vPd.append(env.vPd[i]) if len(vPs) == env.veh_total: print("\nValid points determined. Exiting...") purr.save(env.valid_points_file, (vPs, vPd, env.tP)) purr.save(env.status_file, True) sys.exit(0) except nx.NetworkXNoPath: pass continue print("Not good points, need to reload.") purr.save(env.status_file, False) sys.exit(0) return
def json(): print("Writing targets.json...",end='') purr.save("%s/targets.json" % (env.out_dir),env.targets) print("Complete!") return