def writeNYNetwork(): print "Loading file" filename = 'bin/zhangNYDataOSM.mat' raw = planparser.loadMatData(filename) print "Loading variables" RoadGraph = raw['RoadGraph'] NodesLocation = raw['NodesLocation'] RoadCap = sp.csr_matrix(raw['RoadCap']) TravelTimes = sp.csr_matrix(raw['LinkTime']) LinkLengths = sp.csr_matrix(raw['LinkLength']) NumLanes = sp.csr_matrix(raw['NumLanes']) LinkSpeed = sp.csr_matrix(raw['LinkSpeed']) print "Creating things" network = ET.Element('network') nodes = ET.SubElement(network, 'nodes') links = ET.SubElement(network, 'links') links.set('capperiod', '01:00:00') print "Calling writer for nodes" writeNodes(nodes, NodesLocation) print "Calling writer for links" writeLinks(links, RoadGraph, RoadCap, LinkLengths, NumLanes, LinkSpeed) parsed = pathwriter.cleanXML(network) parsed = parsed[0:22] + '\n<!DOCTYPE network SYSTEM "http://www.matsim.org/files/dtd/network_v1.dtd">' + \ parsed[22:] print('saving') outfilename = 'res/NYOSMnetwork_225cap.xml' print "Saving" with open(outfilename, 'w') as f: f.write(parsed)
def writeNYNetwork(): filename = 'bin/zhangNYdata.mat' raw = planparser.loadMatData(filename) RoadGraph = raw['RoadGraph'] NodesLocation = raw['NodesLocation'] RoadCap = sp.csr_matrix(raw['RoadCap']) TravelTimes = sp.csr_matrix(raw['LinkTime']) LinkLengths = sp.csr_matrix(raw['LinkLength']) NumLanes = sp.csr_matrix(raw['NumLanes']) LinkSpeed = sp.csr_matrix(raw['LinkSpeed']) network = ET.Element('network') nodes = ET.SubElement(network, 'nodes') links = ET.SubElement(network, 'links') links.set('capperiod', '01:00:00') writeNodes(nodes, NodesLocation) writeLinks(links, RoadGraph, RoadCap, LinkLengths, NumLanes, LinkSpeed) parsed = pathwriter.cleanXML(network) parsed = parsed[0:22] + '\n<!DOCTYPE network SYSTEM "http://www.matsim.org/files/dtd/network_v1.dtd">' + \ parsed[22:] outfilename = 'res/nynetwork.xml' with open(outfilename, 'w') as f: f.write(parsed)
def turnCSVIntoPlans(filename, roads): plans = ET.Element("population") rawdata = planparser.loadMatData('bin/zhangNYdata.mat') locations = rawdata['NodesLocation'] with open(filename, 'r') as f: reader = csv.reader(f) for i, trip in enumerate(reader): dropoffx, dropoffy = float(trip[7])*1000000, float(trip[8])*1000000 pickupx, pickupy = float(trip[5])*1000000, float(trip[6])*1000000 closestpickupnode = planparser.findClosestNode(pickupx, pickupy, locations) listoflinks = roads.get(str(closestpickupnode)) pickuplink = str(random.choice(listoflinks)) closestdropoffnode = planparser.findClosestNode(dropoffx, dropoffy, locations) listoflinks = roads.get(str(closestdropoffnode)) dropofflink = random.choice(listoflinks) if (dropofflink != pickuplink): person = ET.SubElement(plans, "person") person.set('id', str(i+1)) plan = ET.SubElement(person, "plan") plan.set('selected', 'yes') hour, minute, second = int(trip[2]), int(trip[3]), int(trip[4]) time = datetime.datetime(100,1,1,hour,minute,second) act = ET.SubElement(plan, 'act') act.set('type', 'dummy') act.set('end_time', str(time.time())) act.set('link', pickuplink) leg = ET.SubElement(plan, 'leg') leg.set('mode', 'taxi') leg.set('dep_time', str(time.time())) leg.set('trav_time', '00:01:00') delta = datetime.timedelta(0,TRAV_TIME_SECONDS) leg.set('arr_time', str((time + delta).time())) route = ET.SubElement(leg, 'route') act2 = ET.SubElement(plan, 'act') act2.set('type', 'dummy') act2.set('link', dropofflink) print(i) print("Cleaning XML...") parsed = pathwriter.cleanXML(plans) parsed = parsed[0:22] + '\n<!DOCTYPE population SYSTEM "http://www.matsim.org/files/dtd/population_v5.dtd">' + \ parsed[22:] print("Writing XML...") outfilename = 'res/amod1820plans.xml' with open(outfilename, 'w') as f: f.write(parsed)
def makeVehicles(): vehicles = ET.Element('vehicles') for i in range(NUMVEHICLES): initializeVehicle(vehicles, i) parsed = pathwriter.cleanXML(vehicles) parsed = parsed[0:22] + '\n<!DOCTYPE vehicles SYSTEM "http://www.matsim.org/files/dtd/dvrp_vehicles_v1.dtd">' + \ parsed[22:] outfile = 'res/amod_vehicles_OSM.xml' with open(outfile, 'w') as f: f.write(parsed)
def makeVehicles(): vehicles = ET.Element("vehicles") for i in range(NUMVEHICLES): initializeVehicle(vehicles, i) parsed = pathwriter.cleanXML(vehicles) parsed = ( parsed[0:22] + '\n<!DOCTYPE vehicles SYSTEM "http://www.matsim.org/files/dtd/dvrp_vehicles_v1.dtd">' + parsed[22:] ) outfile = "res/amod_vehicles.xml" with open(outfile, "w") as f: f.write(parsed)
def turnCSVIntoPlans(filename, roadsIn, roadsOut, roadMap): plans = ET.Element("population") rawdata = planparser.loadMatData('bin/zhangNYDataOSM.mat') locations = rawdata['NodesLocation'] with open(filename, 'r') as f: reader = csv.reader(f) possiblepickups = [locations[int(node)-1] for node in list(roadsOut.keys())] possiblepickupsidx = [node for node in list(roadsOut.keys())] possibledropoffs = [locations[int(node)-1] for node in list(roadsIn.keys())] possibledropoffsidx = [node for node in list(roadsIn.keys())] possiblenodes = [locations[int(node)-1] for node in list(set(roadsOut.keys()) & set(roadsIn.keys()))] possiblenodesidx= [node for node in list(set(roadsOut.keys()) & set(roadsIn.keys()))] for i, trip in enumerate(reader): # if i > count: # break hour, minute, second = int(trip[2]), int(trip[3]), int(trip[4]) if (START_HOUR <= hour < END_HOUR): dropoffx, dropoffy = float(trip[7])*1000, float(trip[8])*1000 pickupx, pickupy = float(trip[5])*1000, float(trip[6])*1000 closestpickupnode = planparser.findClosestNode(pickupx, pickupy, possiblepickups) #closestpickupnode = planparser.findClosestNode(pickupx, pickupy, possiblenodes) #print("Pickup distance (short):",planparser.findDistance(pickupx,pickupy,possiblenodes[int(closestpickupnode-1)])) #Map the node back to the original list of nodes (not the list of pickupnodes) #I do not really understand why we need the -1...Federico closestpickupnode = possiblepickupsidx[closestpickupnode-1] #closestpickupnode = possiblenodesidx[closestpickupnode-1] #print("Pickup distance (long) :",planparser.findDistance(pickupx,pickupy,locations[int(closestpickupnode)-1])) listoflinks = roadsOut.get(str(closestpickupnode)) # Now, what nodes do these links lead to? #Let us only keep the links such that the end node (exit) has neighbors #listoflinks = [link for link in listoflinks if len([roadsOut.get(roadMap[link][1])])] #listoflinks = [link for link in listoflinks if roadMap[link][1] in possiblenodesidx] pickuplink = str(random.choice(listoflinks)) closestdropoffnode = planparser.findClosestNode(dropoffx, dropoffy, possibledropoffs) #closestdropoffnode = planparser.findClosestNode(dropoffx, dropoffy, possiblenodes) #print("Dropoff distance (short):",planparser.findDistance(dropoffx,dropoffy,possiblenodes[int(closestdropoffnode-1)])) closestdropoffnode = possibledropoffsidx[closestdropoffnode-1] #closestdropoffnode = possiblenodesidx[closestdropoffnode-1] #print("Dropoff distance (long) :",planparser.findDistance(dropoffx,dropoffy,locations[int(closestdropoffnode)-1])) listoflinks = roadsIn.get(str(closestdropoffnode)) #listoflinks = [link for link in listoflinks if roadMap[link][1] in possiblenodesidx] dropofflink = random.choice(listoflinks) if (dropofflink != pickuplink): person = ET.SubElement(plans, "person") person.set('id', str(i+1)) plan = ET.SubElement(person, "plan") plan.set('selected', 'yes') time = datetime.datetime(100,1,1,hour-START_HOUR,minute,second) act = ET.SubElement(plan, 'act') act.set('type', 'dummy') act.set('end_time', str(time.time())) act.set('link', pickuplink) leg = ET.SubElement(plan, 'leg') leg.set('mode', 'taxi') leg.set('dep_time', str(time.time())) leg.set('trav_time', '00:01:00') delta = datetime.timedelta(0,TRAV_TIME_SECONDS) leg.set('arr_time', str((time + delta).time())) #route = ET.SubElement(leg, 'route') act2 = ET.SubElement(plan, 'act') act2.set('type', 'dummy') act2.set('link', dropofflink) print(i) print("Cleaning XML...") parsed = pathwriter.cleanXML(plans) parsed = parsed[0:22] + '\n<!DOCTYPE population SYSTEM "http://www.matsim.org/files/dtd/population_v5.dtd">' + \ parsed[22:] print("Writing XML...") outfilename = 'res/march05plansOSM.xml' with open(outfilename, 'w') as f: f.write(parsed)