Source code for preprocess

# overall this py file takes data 
import csv
import sys
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
from collections import *
import os

EdgeFile='Edge.csv' # contains 
ODFile='OD.csv' # contains
StationFile='Stations.csv' # contains



########################################################################################
##Function creates two dictionaries with arrival and departure events at each terminal##
##station. These lists will be used along with the platform availability at these#######
##stations to decide linking of arrival and departure events.###########################
########################################################################################

[docs]def turnaround(): ''' Function creates two dictionaries with arrival and departure events at each terminal station. These lists will be used along with the platform availability at these stations to decide linking of arrival and departure events. ''' for k,v in turnaround_arr: #now this condition is no more required...we are already taking care of it if v not in link_uplist.values() and v not in link_downlist.values(): turnaround_arr_list[k].append(v) for k,v in turnaround_dep: ##now if condition is no more required...we are already taking care of it if v not in link_uplist.values() and v not in link_downlist.values(): turnaround_dep_list[k].append(v)
####################################################################################### ##Function creates set containing arrival events of services between which dwell time## ##################################will be specified#################################### #######################################################################################
[docs]def dwell(): ''' Function creates set containing arrival events of services between which dwell time will be specified ''' for k,v in dwell_up: dwell_list_up[k].append(v) for k,v in dwell_down: dwell_list_down[k].append(v)
####################################################################################### ##Function creates traversal constraints between two consecutive stations. Four dicts## ##are created to hold the arrival and departure time of up and down trains. The######## ##traversal time is stored in dict called odtrav####################################### #######################################################################################
[docs]def traversal(tau,tdu,tad,tdd): ''' Function creates traversal constraints between two consecutive stations. Four dicts are created to hold the arrival and departure time of up and down trains. The traversal time is stored in dict called odtrav ''' for k,v in tau: traversal_arr_up_list[k].append(v) for k,v in tdu: traversal_dep_up_list[k].append(v) for k,v in tad: traversal_arr_down_list[k].append(v) for k,v in tdd: traversal_dep_down_list[k].append(v)
###################################################################################### ##Function creates symmetry constraints among services plying between same origin-#### ##destination pairs.Therfore lists containing keys among which symmetry/frequency##### ##constraints are to be defined are made by this function############################# ######################################################################################
[docs]def frequency(frequp,freqdown,fup_arr,fdown_arr): ''' Function creates symmetry constraints among services plying between same origin-destination pairs.Therfore lists containing keys among which symmetry/frequency constraints are to be defined are made by this function ''' for k,v in frequp: frequency_list_up[k].append(v) for x,y in freqdown: frequency_list_down[x].append(y) for k,v in fup_arr: frequency_list_up_arr[k].append(v) for k,v in fdown_arr: frequency_list_down_arr[k].append(v)
###################################################################################### ##Function creates the services between which headway constraints are to be defined## ##If services have same two consecutive stations headways are to be put between the### ##departure times of these services.################################################## ######################################################################################
[docs]def headway(infoup,infodown): ''' Function creates the services between which headway constraints are to be defined If services have same two consecutive stations headways are to be put between the departure times of these services. ''' # for key and values in infoup ... for k,v in infoup: headway_list_up[k].append(v) for x,y in infodown: headway_list_down[x].append(y)
[docs]def upwrite(l,demand,sup,f,sup1): ''' Function writes lines in up.csv file. Apart from this this function also creates some lists using which lists of services for different types of constraints are created in other functions such as headway,frequency etc. ''' global a global d global cntu for i in range(demand): line=[] count=0 for j in range(len(l)): if l[j]==0: line.append(',') elif l[j]==1 and count==0: line.append(',') count=count+1 else: if count%2==0: prevst=([x for x in range(1,j) if l[x]==1]) ### Change deparr=str(int((prevst[-1]+1)/2 + 10 ))+ str(int(j/2+1 + 10 )) traversal_arr_up.append([deparr,a]) if l[j+1]==1: dwell_up.append([str(j/2+1 + 10),a]) nextst=([x for x in range(j+1,len(l)) if l[x]==1]) if nextst==[]: turnaround_arr.append([str(j/2+1 + 10),a]) if f==0: frequencyup_arr.append([sup,a]) if f==1: frequencyup_arr.append([sup1,a]) item=str(a)+"," a=a+1 count= count +1 line.append(item) else: nextst=([x for x in range(j+1,len(l)) if l[x]==1]) if count==1: if f == 0: turnaround_dep.append([str((j+1)/2 + 10),d]) frequencyup.append([sup,d]) if f == 1: cntu = cntu+1 link_upfile.append([cntu,d]) deparr= str(int((j+1)/2) + 10) + str(int(nextst[0]/2+1 + 10)) headwayinfoup.append([deparr,d]) traversal_dep_up.append([deparr,d]) item=str(d)+"," d=d+1 count=count+1 line.append(item) for li in line: up.write(li) up.write("\n")
###################################################################################### ##Function writes lines in down.csv file. Apart from this this function also creates## ##some lists using which lists of services for different types of constraints are##### ##created in other functions such as headway,frequency etc.########################### ######################################################################################
[docs]def downwrite(l,demand,sdown,f,sdown1): ''' Function writes lines in down.csv file. Apart from this this function also creates some lists using which lists of services for different types of constraints are created in other functions such as headway,frequency etc. ''' global a global d global cntd for i in range(demand): line=[] count=0 for j in range(len(l)): if l[j]==0: line.append(',') elif l[j]==1 and count==0: line.append(',') count=count+1 else: if count%2==0: prevst=([x for x in range(1,j) if l[x]==1]) deparr= str(int((prevst[-1]+1)/2 + 10)) + str(int(j/2+1 + 10)) traversal_arr_down.append([deparr,a]) if l[j+1]==1: dwell_down.append([str(j/2+1 + 10),a]) nextst=([x for x in range(j+1,len(l)) if l[x]==1]) if nextst==[]: if f==0: turnaround_arr.append([str((len(l)-j)/2 + 10),a]) frequencydown_arr.append([sdown,a]) if f==1: cntd=cntd+1 link_downfile.append([cntd,a]) item=str(a)+"," a=a+1 count=count+1 line.append(item) else: nextst=([x for x in range(j+1,len(l)) if l[x]==1]) if count==1: if f==0: frequencydown.append([sdown,d]) if f==1: frequencydown.append([sdown1,d]) turnaround_dep.append([str((len(l)-j+1)/2 + 10 ),d]) deparr= str(int((len(l)-nextst[0])/2 + 10)) + str(int((len(l)-j+1)/2 + 10)) headwayinfodown.append([deparr,d]) traversal_dep_down.append([deparr,d]) item=str(d)+"," d=d+1 count=count+1 line.append(item) for li in line: down.write(li) down.write("\n")
[docs]def updown(l): ''' Written by anurag for panvel-andheri services ''' ldown=[] lup=[] for x in range(len(l)-1): if l[x]>l[x+1]: ldown.append(l[x]) ldown.append(l[x+1]) if l[x]<l[x+1]: lup.append(l[x]) lup.append(l[x+1]) # here also we can do 1) instead of appending both l[x] and l[x+1] # just appending l[x] and append l[x+1] only when the other if condition # start getting satisfied ldown=list(set(ldown)) lup=list(set(lup)) # i guess set just removes duplicates entries and make it a set. # then list function just converted in list format ldown.sort(reverse=True),lup.sort() up_or,up_dest,down_or,down_dest=lup[0],lup[-1],ldown[0],ldown[-1] return up_or,up_dest,down_or,down_dest
[docs]def keys(src,demand,des,gph,station_number): ''' Function generates keys to be written in the up and down.csv files and creates all the constraint dictionaries by calling the other functions ''' for ind in range(len(src)): flag=0 if demand[ind]==0: continue stnumseq=nx.shortest_path(gph,src[ind],des[ind]) if sorted(stnumseq,key=int)!=stnumseq and sorted(stnumseq,key=int,reverse=True)!=stnumseq: flag=1 up_start,up_end,down_start,down_end=updown(stnumseq) if (src[ind] < des[ind] and flag==0) or flag==1: if flag==0: stnumseq=nx.shortest_path(gph,src[ind],des[ind]) else: stnumseq=nx.shortest_path(gph,up_start,up_end) odup=str(stnumseq[0])+str(stnumseq[-1]) odup1 = str(src[ind])+str(des[ind]) uplist=[] for x in range(len(station_number)): if station_number[x] in stnumseq and stnumseq.index(station_number[x])==len(stnumseq)-1: uplist.append(1) uplist.append(0) elif station_number[x] in stnumseq: uplist.append(1) uplist.append(1) else: uplist.append(0) uplist.append(0) upwrite(uplist,demand[ind],odup,flag,odup1) if (src[ind] > des[ind] and flag==0) or flag==1: if flag==0: stnumseq=nx.shortest_path(gph,src[ind],des[ind]) else: stnumseq=nx.shortest_path(gph,down_start,down_end) downlist=[] oddown=str(stnumseq[0])+str(stnumseq[-1]) odddown1=str(src[ind])+str(des[ind]) for x in range(len(station_number)): if station_number[len(station_number)-1-x] in stnumseq and stnumseq.index(station_number[len(station_number)-1-x])==len(stnumseq)-1: downlist.append(1) downlist.append(0) elif station_number[len(station_number)-1-x] in stnumseq: downlist.append(1) downlist.append(1) else: downlist.append(0) downlist.append(0) downwrite(downlist,demand[ind],oddown,flag,odddown1) headway(headwayinfoup,headwayinfodown) frequency(frequencyup,frequencydown,frequencyup_arr,frequencydown_arr) traversal(traversal_arr_up,traversal_dep_up,traversal_arr_down,traversal_dep_down) dwell() link() turnaround()
[docs]def headers(station_name): ''' Writes header titles for the files up.csv and down.csv ''' headerup=[] headerdown=[] for x in range(len(station_name)): sa,sd=station_name[x]+"a,",station_name[x]+"d," headerup.append(sa) headerup.append(sd) for item in headerup: up.write(item) up.write("\n") for x in range(len(station_name)-1,-1,-1): sa,sd=station_name[x]+"a,",station_name[x]+"d," headerdown.append(sa) headerdown.append(sd) for item in headerdown: down.write(item) down.write("\n")
[docs]def graph(): ''' The first function to be called and takes Edge, Stations and OD as input files. It generates the network and calls the function headers and keys. ''' gup=nx.Graph() edge,od,stations=pd.read_csv('Edge.csv'),pd.read_csv('OD.csv'),pd.read_csv('Stations.csv') source,dest,dem=od["SourceSrNum"],od["DestinationSrNum"],od["Demand"] From,to,trav_time=edge["From"],edge["To"],edge["Traversal_time"] stnum,stname,junc,platform=stations["Number"],stations["Station_name"],stations["Junction"],stations["Turnaround"] for ind in range(len(stnum)): gup.add_node(stnum[ind]) st_numplatform[str(stnum[ind]*1.0)]=platform[ind] for ind in range(0,len(edge),2): gup.add_edge(From[ind],to[ind],weight=trav_time[ind]) od=str(From[ind])+str(to[ind]) odtrav[od]=trav_time[ind] #nx.draw(gup) #plt.show() #plt.savefig('Networkup.png') #plt.hold() headers(stname) keys(source,dem,dest,gup,stnum) return stname
a = 100 d = 10000 cntu = 0 cntd = 0 st_numplatform = {} odtrav={} headway_list_up=defaultdict(list) headwayinfoup=[] headwayinfodown=[] headway_list_down=defaultdict(list) frequency_list_up=defaultdict(list) frequency_list_down=defaultdict(list) frequency_list_up_arr =defaultdict(list) frequency_list_down_arr =defaultdict(list) frequencyup=[] frequencyup_arr = [] frequencydown=[] frequencydown_arr = [] traversal_arr_up=[] traversal_dep_up=[] traversal_arr_down=[] traversal_dep_down=[] traversal_arr_up_list=defaultdict(list) traversal_dep_up_list=defaultdict(list) traversal_arr_down_list=defaultdict(list) traversal_dep_down_list=defaultdict(list) dwell_up=[] dwell_down=[] dwell_list_up=defaultdict(list) dwell_list_down=defaultdict(list) link_upfile=[] link_downfile=[] link_uplist=defaultdict(list) link_downlist=defaultdict(list) turnaround_arr_list=defaultdict(list) turnaround_dep_list=defaultdict(list) turnaround_arr=[] turnaround_dep=[] st_numplatform={} up=open('up.csv','w') down=open('down.csv','w') station_list = graph() up.close() down.close()