import pandas as pd
import preprocess
import traversal_time
import numpy as np
from collections import *
T = 360 * 1 + 60
#Any Value of T >= 180 will work
T1 = 250
#Headway time
m = 3
#headway1 time
m1 = 6
#Time_period
Tp = 60*5
Tp2 = 120
Tp1 = Tp - Tp2
#### Commenting out ....
#tolerance
#tol = 12
#dwell limits:
lwr = 0.5
upr = 1
#platform variables:
p1 = 3
p2 = 4
[docs]def variables():
'''
The code generates an ampl modelfile that is given to Gurobi as an input and the input file contains all the constraints required.
'''
mod.write('#Decision variables\n\n')
mod.write('var arr {100..'+str(num_arr)+'} >=0 ;\n')
mod.write('var dep {10000..'+str(num_dep)+'} >=0 ;\n')
#mod.write('var pd { i in {10000..'+str(num_dep)+'},j in {10000..'+str(num_dep)+'} : i<>j} integer <=1;\n')
s='set array1={'
for l in turnaround_dep.keys():
for y in range(len(turnaround_dep[l])):
#if turnaround_list_arr[l][y] not in up and turnaround_list_arr[l][y] not in down:
s=s+str(turnaround_dep[l][y])+','
#if turnaround_list_arr[l][-1] not in up and turnaround_list_arr[l][-1] not in down:
s = s[0:-1] + '};\n\n'
mod.write(s)
s='set array2={'
for l in turnaround_arr.keys():
for y in range(len(turnaround_arr[l])):
#if turnaround_list_arr[l][y] not in up and turnaround_list_arr[l][y] not in down:
s=s+str(turnaround_arr[l][y])+','
#if turnaround_list_arr[l][-1] not in up and turnaround_list_arr[l][-1] not in down:
s = s[0:-1] + '};\n\n'
mod.write(s)
#mod.write('var st { i in {10000..'+str(num_dep)+'},j in {100..'+str(num_arr)+'} : i<>j} binary;\n')
#mod.write('var st { i in array1, j in array2 : i<>j} binary;\n')
mod.write('var sink {i in array2} binary ;\n')
mod.write('var source {i in array1} binary ;\n')
#mod.write('var sink {100..'+str(num_arr)+'} binary ;\n')
#mod.write('var source {10000..'+str(num_dep)+'} binary ;\n')
#mod.write('var dummmy < = 60 ; \n')
#mod.write('var pad { i in {100..'+str(num_arr)+'},j in {100..'+str(num_dep)+'} : i<>j} integer <=1;\n')
#mod.write('var pda { i in {1000..'+str(num_dep)+'},j in {100..'+str(num_arr)+'} : i<>j} integer <=1;\n\n')
mod.write('\n')
def headway(headway_list):
global headwaynum
global con
for l in headway_list:
s='set headway'+str(headwaynum)+'={'
for x in range(len(l)-1):
s=s+str(l[x])+','
s=s+str(l[-1])+'};\n'
mod.write(s)
mod.write('var pd'+str(headwaynum)+'{i in headway' + str(headwaynum) + ',j in headway' + str(headwaynum)+ ' : i<>j} binary;\n')
mod.write('subject to con'+str(con)+'{i in headway'+str(headwaynum)+',j in headway'+str(headwaynum)+':i<>j}:(dep[j]-dep[i]+'+ str(T+m)+ '*pd'+str(headwaynum)+'[i,j])>='+str(m)+';\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in headway'+str(headwaynum)+',j in headway'+str(headwaynum)+':i<>j}:(dep[j]-dep[i]+'+ str(T+m)+'*pd'+str(headwaynum)+'[i,j])<='+str(T)+';\n\n')
con=con+1
headwaynum=headwaynum+1
def anurag(hl):
global headwaynum
j=headwaynum
global con
# Bandra and Andheri services pe extra headway constraints
ba_list = hl['111']
for k in hl['112']:
ba_list.append(k)
l = ba_list
print(l)
s='set headway'+str(j)+'={'
for x in range(len(l)-1):
s=s+str(l[x])+','
s=s+str(l[-1])+'};\n'
mod.write(s)
mod.write('var pd'+str(headwaynum)+'{i in headway' + str(j) + ',j in headway' + str(j)+ ' : i<>j} binary;\n')
mod.write('subject to con'+str(con)+'{i in headway'+str(j)+',j in headway'+str(j)+':i<>j}:(dep[j]-dep[i]+'+ str(T+m1)+ '*pd'+str(j)+'[i,j])>='+str(m1)+';\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in headway'+str(j)+',j in headway'+str(j)+':i<>j}:(dep[j]-dep[i]+'+ str(T+m1)+'*pd'+str(j)+'[i,j])<='+str(T)+';\n\n')
con=con+1
def anurag1(ba_and_list,lngth,tol):
global headwaynum
j=headwaynum
global con
s='set headway'+str(j)+'={'
for x in range(len(ba_and_list)-1):
s=s+str(ba_and_list[x])+','
s=s+str(ba_and_list[-1])+'};\n'
mod.write(s)
mod.write('var pd'+str(headwaynum)+'{i in headway' + str(j) + ',j in headway' + str(j)+ ' : i<>j} binary;\n')
head_diff = int(Tp1/lngth) - tol
mod.write('subject to con'+str(con)+'{i in headway'+str(j)+',j in headway'+str(j)+':i<>j}:(dep[j]-dep[i]+'+ str(T+head_diff)+ '*pd'+str(j)+'[i,j])>='+str(head_diff)+';\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in headway'+str(j)+',j in headway'+str(j)+':i<>j}:(dep[j]-dep[i]+'+ str(T+head_diff)+'*pd'+str(j)+'[i,j])<='+str(T)+';\n\n')
con=con+1
headwaynum = headwaynum + 1
def symmetry(symmetry_list,f,tol_ll,tol_uu):
global symmetrynum
global con
for k in symmetry_list.keys():
l = symmetry_list[k]
n1 = f[k]
tol_l = tol_ll[k]
tol_u = tol_uu[k]
print(len(l))
print(n1)
s = "#nextline\n\n"
mod.write(s)
N = len(l) - n1
if(len(l)-n1 > 1):
if(n1<=1):
if(n1==1):
mod.write('subject to con' + str(con) + ': dep[' + str(l[n1-1]) + '] <=' + str(Tp2-0.5) + ';\n\n')
con = con+1
else:
s='set symmetry'+str(symmetrynum)+'={'
for x in range(n1-2):
s= s + '(' + str(l[x])+',' + str(l[x+1]) + '),'
s=s+ '(' + str(l[n1-2])+',' + str(l[n1-1]) + ')' + '};\n'
mod.write(s)
#mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(int(Tp1/N)-tol_l)+';\n')
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(0)+';\n')
con=con+1
mod.write('subject to con'+str(con)+':dep[' + str(l[n1-1])+'] <='+str(Tp2-0.5)+';\n\n')
con=con+1
symmetrynum = symmetrynum + 1;
s='set symmetry'+str(symmetrynum)+'={'
for x in range(n1,len(l)-2):
s= s + '(' + str(l[x])+',' + str(l[x+1]) + '),'
s=s+ '(' + str(l[len(l)-2])+',' + str(l[len(l)-1]) + ')' + '};\n'
mod.write(s)
## #tolerance calculation:
## a = 0.2 * Tp/len(l)
## tol1 = int(a) + (round(a,0) - int(a))/2
## #print(a)
## #print(tol1)
## tol = 4
#N = len(l) - n1
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(int(Tp1/N)-tol_l)+';\n')
con=con+1
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])<='+str(int(Tp1/N)+tol_u)+';\n')
con=con+1
## Commented
mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] <=' + str(Tp - 0*int(Tp1/N) + 0*tol_l) + ';\n')
con = con+1
mod.write('subject to con'+str(con)+':dep[' + str(l[n1])+'] >='+str(Tp2)+';\n\n')
con=con+1
symmetrynum = symmetrynum + 1
############################################## Adding new part############
## if (k =='110' or k=='101'):
## s='set SS'+str(symmetrynum)+'={'
## for x in range(n1,len(l)-2):
## s= s + '(' + str(l[x])+',' + str(l[x+2]) + '),'
## s = s[0:-1];
## s= s + '};\n'
## mod.write(s)
##
## mod.write('subject to con'+str(con)+'{(i,j) in SS'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(2*int(Tp1/N)-4)+';\n\n')
## con=con+1
##
##
## symmetrynum = symmetrynum + 1
##
## ###########################################################################
############################################## Adding new part 1 -- sometimes usable for tolerance ############
## if (k =='110' or k=='101'):
## s='set tol'+str(symmetrynum)+'={'
## for x in range(n1,len(l)):
## s= s + str(l[x])+','
## s = s[0:-1];
## s= s + '};\n'
## mod.write(s)
##
## symmetrynum = symmetrynum + 1
###########################################################################
s='set symmetry'+str(symmetrynum)+'={'
for x in range(n1,len(l)-1):
s= s + str(l[x])+','
s=s + str(l[len(l)-1]) + '};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{ i in symmetry'+str(symmetrynum)+'}:source[i] =' + str(0)+';\n\n')
con=con+1
### changed one.... commented now
#mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] <=' + str(Tp + 0*tol) + ';\n\n')
#con = con+1
#mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] <=' + str(Tp - int(Tp/len(l)) + tol ) + '+dep[' + str(l[0]) + '];\n')
#mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] - dep[' + str(l[0]) + '] <=' + str(Tp - int(Tp/len(l)) + 1*tol) + ';\n\n')
#con = con+1
#mod.write('subject to con'+str(con)+':('+ str(Tp) + '+ dep[' + str(l[0]) +']-dep[' + str(l[-1]) + '])>='+str(int(Tp/len(l))-tol)+';\n')
#con=con+1
#mod.write('subject to con'+str(con)+':('+str(Tp) + '+ dep[' + str(l[0]) +']-dep[' + str(l[-1]) + '])<='+str(int(Tp/len(l))+tol)+';\n')
#con=con+1
#mod.write('subject to con'+str(con)+'{i in symmetry'+str(symmetrynum)+',j in symmetry'+str(symmetrynum)+':i<>j}:(dep[j]-dep[i]+180*pd[i,j])<='+str(180-int(180/len(l))+3)+';\n\n')
#con=con+1
######### Commented....
#mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}: source[j] <= source[i];\n\n')
#con=con+1
symmetrynum=symmetrynum+1
if (len(l)- n1 == 1):
if(n1<=1):
if(n1==1):
mod.write('subject to con' + str(con) + ': dep[' + str(l[n1-1]) + '] <=' + str(Tp2-0.5) + ';\n\n')
con = con+1
else:
s='set symmetry'+str(symmetrynum)+'={'
for x in range(n1-2):
s= s + '(' + str(l[x])+',' + str(l[x+1]) + '),'
s=s+ '(' + str(l[n1-2])+',' + str(l[n1-1]) + ')' + '};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(0)+';\n')
con=con+1
mod.write('subject to con'+str(con)+':dep[' + str(l[n1-1])+'] <='+str(Tp2-0.5)+';\n\n')
con=con+1
symmetrynum = symmetrynum + 1;
num = n1
s='set symmetry' + str(symmetrynum)+'={' + str(l[n1])+ '};\n'
mod.write(s)
mod.write('subject to con' + str(con) + ': dep[' + str(l[num]) + '] <=' + str(Tp - 0*int(Tp/len(l)) + 0*tol_l) + ';\n')
con = con+1
mod.write('subject to con' + str(con) + ': dep[' + str(l[num]) + '] >=' + str(Tp2 - 0*int(Tp/len(l)) + 0*tol_l) + ';\n')
con = con+1
mod.write('subject to con'+str(con)+'{ i in symmetry'+str(symmetrynum)+'}:source[i] =' + str(0)+';\n\n')
con=con+1
symmetrynum=symmetrynum+1
#con = con+10
def symmetry1(symmetry_list):
global symmetrynum
global con
for l in symmetry_list:
if(len(l) > 1):
s='set symmetry'+str(symmetrynum)+'={'
for x in range(len(l)-2):
s= s + '(' + str(l[x])+',' + str(l[x+1]) + '),'
s=s+ '(' + str(l[len(l)-2])+',' + str(l[len(l)-1]) + ')' + '};\n'
mod.write(s)
#tolerance calculation:
#a = 0.2 * Tp/len(l)
#tol1 = int(a) + (round(a,0) - int(a))/2
#print(a)
#print(tol1)
#tol = 4
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(int(Tp/len(l))-tol)+';\n')
con=con+1
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])<='+str(int(Tp/len(l))+tol)+';\n')
con=con+1
## Commented
#mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] <=' + str(Tp - 0.5*int(Tp/len(l)) + 0*tol) + ';\n\n')
#con = con+1
### changed one....
mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] <=' + str(Tp + 0*tol) + ';\n\n')
con = con+1
#mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] <=' + str(Tp - int(Tp/len(l)) + tol ) + '+dep[' + str(l[0]) + '];\n')
#mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] - dep[' + str(l[0]) + '] <=' + str(Tp - int(Tp/len(l)) + 1*tol) + ';\n\n')
#con = con+1
#mod.write('subject to con'+str(con)+':('+ str(Tp) + '+ dep[' + str(l[0]) +']-dep[' + str(l[-1]) + '])>='+str(int(Tp/len(l))-tol)+';\n')
#con=con+1
#mod.write('subject to con'+str(con)+':('+str(Tp) + '+ dep[' + str(l[0]) +']-dep[' + str(l[-1]) + '])<='+str(int(Tp/len(l))+tol)+';\n')
#con=con+1
#mod.write('subject to con'+str(con)+'{i in symmetry'+str(symmetrynum)+',j in symmetry'+str(symmetrynum)+':i<>j}:(dep[j]-dep[i]+180*pd[i,j])<='+str(180-int(180/len(l))+3)+';\n\n')
#con=con+1
######### Commented....
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}: source[j] <= source[i];\n\n')
con=con+1
symmetrynum=symmetrynum+1
if (len(l)==1):
s='set symmetry' + str(symmetrynum)+'={' + str(l[len(l)-1])+ '};\n'
mod.write(s)
mod.write('subject to con' + str(con) + ': dep[' + str(l[0]) + '] <=' + str(Tp - 0*int(Tp/len(l)) + 0*tol) + ';\n\n')
con = con+1
symmetrynum=symmetrynum+1
def symmetry2(symmetry_list,f,tol_ll,tol_uu):
global symmetrynum
global con
for k in symmetry_list.keys():
l = symmetry_list[k]
n1 = f[k]
tol_l = tol_ll[k]
tol_u = tol_uu[k]
print(len(l))
print(n1)
s = "#nextline\n\n"
mod.write(s)
N = len(l) - n1
if(len(l)-n1 > 1):
s='set symmetry'+str(symmetrynum)+'={'
for x in range(0,N-2):
s= s + '(' + str(l[x])+',' + str(l[x+1]) + '),'
s=s+ '(' + str(l[N-2])+',' + str(l[N-1]) + ')' + '};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(int(Tp1/N)-tol_l)+';\n')
con=con+1
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])<='+str(int(Tp1/N)+tol_u)+';\n')
con=con+1
symmetrynum = symmetrynum + 1
mod.write('\n');
######## Adding the line ##############################
if(n1<=1):
if(n1==1):
mod.write('subject to con' + str(con) + ': dep[' + str(l[n1-1]) + '] <=' + str(Tp2-0.5) + ';\n\n')
con = con+1
if(n1>=2):
s='set symmetry'+str(symmetrynum)+'={'
for x in range(N,len(l)-2):
s= s + '(' + str(l[x])+',' + str(l[x+1]) + '),'
s=s+ '(' + str(l[len(l)-2])+',' + str(l[len(l)-1]) + ')' + '};\n'
mod.write(s)
#mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(int(Tp1/N)-tol_l)+';\n')
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(0)+';\n')
con=con+1
mod.write('subject to con'+str(con)+':dep[' + str(l[n1-1])+'] <='+str(Tp2-0.5)+';\n\n')
con=con+1
#symmetrynum = symmetrynum + 1;
#######################################################
## Commented
############################################################################################################################
###################################################Commented on 30 may#############################################
############################################################################################################################
#mod.write('subject to con' + str(con) + ': dep[' + str(l[-1]) + '] <=' + str(Tp - 0*int(Tp1/N) + 0*tol_l) + ';\n')
#con = con+1
############################################################################################################################
############################################################################################################################
############################################################################################################################
mod.write('subject to con'+str(con)+':dep[' + str(l[n1])+'] >='+str(Tp2)+';\n\n')
con=con+1
symmetrynum = symmetrynum + 1
s='set symmetry'+str(symmetrynum)+'={'
for x in range(n1,len(l)-1):
s= s + str(l[x])+','
s=s + str(l[len(l)-1]) + '};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{ i in symmetry'+str(symmetrynum)+'}:source[i] =' + str(0)+';\n\n')
con=con+1
symmetrynum=symmetrynum+1
if (len(l)- n1 == 1):
num = n1
s='set symmetry' + str(symmetrynum)+'={' + str(l[n1])+ '};\n'
mod.write(s)
############################################################################################################################
###################################################Commented on 30 may#############################################
############################################################################################################################
#mod.write('subject to con' + str(con) + ': dep[' + str(l[num]) + '] <=' + str(Tp - 0*int(Tp/len(l)) + 0*tol_l) + ';\n')
#con = con+1
############################################################################################################################
############################################################################################################################
############################################################################################################################
mod.write('subject to con' + str(con) + ': dep[' + str(l[num]) + '] >=' + str(Tp2 - 0*int(Tp/len(l)) + 0*tol_l) + ';\n')
con = con+1
mod.write('subject to con'+str(con)+'{ i in symmetry'+str(symmetrynum)+'}:source[i] =' + str(0)+';\n\n')
con=con+1
symmetrynum=symmetrynum+1
######## Adding the line ##############################
if(n1<=1):
if(n1==1):
mod.write('subject to con' + str(con) + ': dep[' + str(l[n1-1]) + '] <=' + str(Tp2-0.5) + ';\n\n')
con = con+1
if(n1>=2):
s='set symmetry'+str(symmetrynum)+'={'
for x in range(N,len(l)-2):
s= s + '(' + str(l[x])+',' + str(l[x+1]) + '),'
s=s+ '(' + str(l[len(l)-2])+',' + str(l[len(l)-1]) + ')' + '};\n'
mod.write(s)
#mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(int(Tp1/N)-tol_l)+';\n')
mod.write('subject to con'+str(con)+'{(i,j) in symmetry'+str(symmetrynum)+'}:(dep[j]-dep[i])>='+str(0)+';\n')
con=con+1
mod.write('subject to con'+str(con)+':dep[' + str(l[n1-1])+'] <='+str(Tp2-0.5)+';\n\n')
con=con+1
symmetrynum = symmetrynum + 1;
#######################################################
def symmetry_arr(symmetry_list):
global symmetrynum1
global con
for l in symmetry_list:
if(len(l) > 1):
s='set symmetry_arr'+str(symmetrynum1)+'={'
for x in range(len(l)-2):
s= s + '(' + str(l[x])+',' + str(l[x+1]) + '),'
s=s+ '(' + str(l[len(l)-2])+',' + str(l[len(l)-1]) + ')' + '};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{(i,j) in symmetry_arr'+str(symmetrynum1)+'}: sink[j] >= sink[i];\n\n')
con=con+1
symmetrynum1=symmetrynum1+1
def symmetry_arr1(symmetry_list,f):
global symmetrynum1
global con
for ky in symmetry_list.keys():
l = symmetry_list[ky]
n1 = f[ky]
#print(l)
#print(n1)
lngth = len(l)
mod.write('# Next line \n')
if(lngth - n1 > 0):
s='set symmetry_arr'+str(symmetrynum1)+'={'
for x in range(lngth-n1-1):
s= s + str(l[x])+','
s= s + str(l[lngth-n1-1]) + '};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{ j in symmetry_arr'+str(symmetrynum1)+'}: sink[j] = 0;\n')
con=con+1
######################## Added ... 28 May 2018....First arriving event should be greater than 120
mod.write('subject to con'+str(con)+': arr[' + str(l[0]) + '] >= ' + str(Tp2) + ';\n')
con=con+1
mod.write('subject to con'+str(con)+': arr[' + str(l[lngth-n1-1]) + '] <= ' + str(Tp) + ';\n')
con=con+1
mod.write('\n')
symmetrynum1 = symmetrynum1 + 1
if n1 > 0:
i = lngth - n1
s='set symmetry_arr'+str(symmetrynum1)+'={'
for x in range(i,lngth-1):
s= s + str(l[x])+','
s= s + str(l[lngth-1]) + '};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{ j in symmetry_arr'+str(symmetrynum1)+'}: sink[j] = 1;\n')
con=con+1
mod.write('subject to con'+str(con)+': arr[' + str(l[lngth-n1]) + '] >= ' + str(Tp) + ';\n')
con=con+1
symmetrynum1=symmetrynum1+1
mod.write('\n')
#############################################################################################################
##Dwell time constraints are written into the model file in this function####################################
#############################################################################################################
def dwell(dwell_list):
global dwellnum
global con
for l in dwell_list:
s='set dwell'+str(dwellnum)+'={'
for x in range(len(l)-1):
s=s+str(l[x])+','
s=s+str(l[-1])+'};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{i in dwell'+str(dwellnum)+'}:(dep[i+9901]-arr[i])>='+str(lwr)+';\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in dwell'+str(dwellnum)+'}:(dep[i+9901]-arr[i])<='+str(upr)+';\n\n')
con=con+1
dwellnum=dwellnum+1
#############################################################################################################
##Traversal constraints are written into the model file in this function#####################################
#############################################################################################################
def traversal(trav_list,time):
global travnum
global con
for l in trav_list.keys():
s='set traversal'+str(travnum)+'={'
for y in range(len(trav_list[l])-1):
s=s+str(trav_list[l][y])+','
s=s+str(trav_list[l][-1])+'};\n'
mod.write(s)
mod.write('subject to con'+str(con)+'{i in traversal'+str(travnum)+'}:(arr[i-9900]-dep[i])='+str(time[l])+';\n\n')
con=con+1
#mod.write('subject to con'+str(con)+'{i in traversal'+str(travnum)+'}:(arr[i-900]-dep[i]+180*pad[i-900,i])<='+str(time[l]+2)+';\n\n')
#con=con+1
travnum=travnum+1
def linkage(uplist,downlist):
global con
for x in uplist.keys():
mod.write('subject to con'+str(con)+':(dep['+str(uplist[x][0])+']-arr['+str(downlist[x][0])+'])>=2;\n')
con=con+1
mod.write('subject to con'+str(con)+':(dep['+str(uplist[x][0])+']-arr['+str(downlist[x][0])+'])<=5;\n\n')
con=con+1
def turnaround(turnaround_list_arr,turnaround_list_dep,platforms,uplist,downlist):
global turnaroundnum
global con
#up = [y for x in uplist.values() for y in x]
#down = [y for x in downlist.values() for y in x]
#print up
for l in turnaround_list_dep.keys():
#print(M[l])
s='set turnaround'+str(turnaroundnum)+'={'
for y in range(len(turnaround_list_arr[l])-1):
#if turnaround_list_arr[l][y] not in up and turnaround_list_arr[l][y] not in down:
s=s+str(turnaround_list_arr[l][y])+','
#if turnaround_list_arr[l][-1] not in up and turnaround_list_arr[l][-1] not in down:
s=s+str(turnaround_list_arr[l][-1])+'};\n'
mod.write(s)
turnaroundnum=turnaroundnum+1
s='set turnaround'+str(turnaroundnum)+'={'
for y in range(len(turnaround_list_dep[l])-1):
#if turnaround_list_dep[l][y] not in up and turnaround_list_dep[l][y] not in down:
s=s+str(turnaround_list_dep[l][y])+','
#if turnaround_list_dep[l][-1] not in up and turnaround_list_dep[l][-1] not in down:
s=s+str(turnaround_list_dep[l][-1])+'};\n'
mod.write(s)
mod.write('var st'+str(turnaroundnum-1)+'{i in turnaround'+str(turnaroundnum)+', j in turnaround'+str(turnaroundnum-1)+':i<>j} binary;\n')
#mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+'}:sum{j in turnaround'+str(turnaroundnum-1)+'}st'+str(turnaroundnum-1)+'[i,j]=1;\n')
mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+'}:sum{j in turnaround'+str(turnaroundnum-1)+'}st'+str(turnaroundnum-1)+'[i,j] + source[i] = 1;\n')
con=con+1
######
#mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+'}: (' + str(M[l]) + ' - dep[i] + ' + str(T) + '*(1-source[i])) >= 0;\n')
#mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+'}: (' + 'dummy' + '- dep[i] +' + str(T) + '*(1-source[i])) >= 0;\n')
#con=con+1
#mod.write('subject to con'+str(con)+'{j in turnaround'+str(turnaroundnum-1)+'}:sum{i in turnaround'+str(turnaroundnum)+'}st'+str(turnaroundnum-1)+'[i,j]=1;\n')
mod.write('subject to con'+str(con)+'{j in turnaround'+str(turnaroundnum-1)+'}:sum{i in turnaround'+str(turnaroundnum)+'}st'+str(turnaroundnum-1)+'[i,j] + sink[j] = 1;\n')
con=con+1
#mod.write('subject to con'+str(con)+'{j in turnaround'+str(turnaroundnum-1)+'}: (arr[j] - ' + str(Tp) + ' + ' + str(Tp + 1) + '*(1-sink[j])) >= 0;\n')
#mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+'}: (' + 'dummy' + '- dep[i] +' + str(T) + '*(1-source[i])) >= 0;\n')
#con=con+1
mod.write('\n');
#Commented .. 28 March
##
## mod.write('var k' + str(turnaroundnum-1) + '{ j in turnaround'+str(turnaroundnum-1)+'} binary;\n')
## con = con+1
##
## mod.write('subject to con'+str(con)+'{j in turnaround'+str(turnaroundnum-1)+'}:(arr[j] - ' + str(Tp2) + '+' + str(Tp2+1) +'*k' + str(turnaroundnum-1) + '[j]) >= 0 ; \n')
## con=con+1
##
## mod.write('subject to con'+str(con)+'{j in turnaround'+str(turnaroundnum-1)+'}:(' + str(Tp2) + ' - arr[j] + ' + str(T) + '*(1-k' + str(turnaroundnum-1) + '[j]) ) >= 0 ; \n')
## con=con+1
##
## mod.write('subject to con'+str(con)+'{j in turnaround'+str(turnaroundnum-1)+'}: (arr[j] - ' + str(Tp) + ' + ' +str(T) + '*(1-sink[j]) + 310*k' + str(turnaroundnum-1) + '[j] ) >= 0;\n\n')
## #mod.write('subject to con'+str(con)+'{j in turnaround'+str(turnaroundnum-1)+'}: (arr[j] - ' + str(Tp) + ' + ' + str(M[l]) + ' + ' +str(T) + '*(1-sink[j])) >= 0;\n')
## #mod.write('subject to con'+str(con)+'{j in turnaround'+str(turnaroundnum-1)+'}: (arr[j] - 180 + ' + 'dummy' + ' + ' +str(T) + '*(1-sink[j])) >= 0;\n')
## con=con+1
#mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+',j in turnaround'+str(turnaroundnum-1)+':i<>j}:(dep[i]-arr[j]+' + str(T) + '-' + str(T+m) + '*st'+str(turnaroundnum-1)+'[i,j])>=0;\n')
mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+',j in turnaround'+str(turnaroundnum-1)+':i<>j}:(dep[i]-arr[j]+' + str(T) + '-' + str(T+m) + '*st'+str(turnaroundnum-1)+'[i,j])>=0;\n')
con=con+1
#if platforms[int(l)]>2:
#mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+',j in turnaround'+str(turnaroundnum-1)+':i<>j}:(dep[i]-arr[j]+180*pda[i,j])<='+str(platforms[int(l)]+180)+'-180*st'+str(turnaroundnum-1)+'[i,j];\n\n')
#mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+',j in turnaround'+str(turnaroundnum-1)+':i<>j}:(dep[i]-arr[j]+'+ str(T - platforms[l]) + '*st'+str(turnaroundnum-1)+'[i,j])<=' + str(T) + ';\n\n')
mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+',j in turnaround'+str(turnaroundnum-1)+':i<>j}:(dep[i]-arr[j]+'+ str(T - platforms[str(float(l))]) + '*st'+str(turnaroundnum-1)+'[i,j])<=' + str(T) + ';\n\n')
con=con+1
#else:
# mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+',j in turnaround'+str(turnaroundnum-1)+':i<>j}:(dep[i]-arr[j]+60*pda[i,j])<=65-60*st'+str(turnaroundnum-1)+'[i,j];\n\n')
#con=con+1
#min_traversal_time_constraint
k = float(l) - 10
mod.write('subject to con'+str(con)+'{i in turnaround'+str(turnaroundnum)+'}: ( dep[i] - ' + str(trav_min[k]) + ' + ' + str(trav_min[k]) + '*source[i] ) >= 0;\n\n')
con=con+1
turnaroundnum=turnaroundnum+1
def platform():
global con
mod.write('set platform = {1,2};\n')
#turnaround 2 is CST Departure events and turnaround1 is CST Arrival events....
mod.write('var X{i in platform, j in turnaround2:i<>j} binary;\n')
mod.write('var Y{i in platform, j in turnaround1:i<>j} binary;\n')
mod.write('var pda{i in turnaround2, j in turnaround1:i<>j} binary;\n\n')
mod.write('subject to con'+str(con)+'{j in turnaround2}: sum{i in platform}X[i,j] = 1;\n')
con=con+1
mod.write('subject to con'+str(con)+'{j in turnaround1}: sum{i in platform}Y[i,j] = 1;\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in turnaround2, j in turnaround1}: Y[1,j] <= X[1,i] + (1-st1[i,j]);\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in turnaround2, j in turnaround1}: Y[2,j] <= X[2,i] + (1-st1[i,j]);\n')
con=con+1
mod.write('\n#To get Pda Values: \n')
mod.write('subject to con'+str(con)+'{i in turnaround2, j in turnaround1}: dep[i] - arr[j] + ' + str(T1) + '*pda[i,j] >= ' + str(0) + ';\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in turnaround2, j in turnaround1}: dep[i] - arr[j] + ' + str(T1) + '*pda[i,j] <= ' + str(T1) + ';\n')
con=con+1
mod.write('\n#For Platform 1: \n')
mod.write('subject to con'+str(con)+'{i in turnaround2, j in turnaround1}: dep[i] - arr[j] + ' + str(T1) + '*(2 - X[1,i] - Y[1,j]) + ' + str(T1) + '*pda[i,j] >= ' + str(p1) + ';\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in turnaround2, j in turnaround1}: dep[i] - arr[j] - ' + str(T1) + '*(2 - X[1,i] - Y[1,j]) + ' + str(T1+p1) + '*pda[i,j] <= ' + str(T1) + ';\n')
con=con+1
mod.write('\n#For Platform 2: \n')
mod.write('subject to con'+str(con)+'{i in turnaround2, j in turnaround1}: dep[i] - arr[j] + ' + str(T1) + '*(2 - X[2,i] - Y[2,j]) + ' + str(T1) + '*pda[i,j] + st1[i,j] >= ' + str(p2) + ';\n')
con=con+1
mod.write('subject to con'+str(con)+'{i in turnaround2, j in turnaround1}: dep[i] - arr[j] - ' + str(T1) + '*(2 - X[2,i] - Y[2,j]) + ' + str(T1+p2) + '*pda[i,j] <= ' + str(T1) + ';\n')
con=con+1
#############################################################################################################
##This is the part of the program that accesses different lists that have been created by the other main#####
##program####################################################################################################
#############################################################################################################
up, down = open('up.csv', 'r'), open('down.csv', 'r')
up_lines, down_lines = up.readlines(), down.readlines()
mod=open('modelfile.mod','w')
num_arr,num_dep=preprocess.a,preprocess.d
headwaylist_up=preprocess.headway_list_up.values()
headwaylist_down=preprocess.headway_list_down.values()
symmetry_up=preprocess.frequency_list_up
symmetry_down=preprocess.frequency_list_down
symmetry_up_arr=preprocess.frequency_list_up_arr.values()
symmetry_down_arr=preprocess.frequency_list_down_arr.values()
symmetry_up_arr1 = preprocess.frequency_list_up_arr
symmetry_down_arr1 = preprocess.frequency_list_down_arr
dwell_up=preprocess.dwell_list_up.values()
dwell_down=preprocess.dwell_list_down.values()
trav_up=preprocess.traversal_dep_up_list
travtime=preprocess.odtrav
trav_down=preprocess.traversal_dep_down_list
turnaround_arr=preprocess.turnaround_arr_list
turnaround_dep=preprocess.turnaround_dep_list
numplat=preprocess.st_numplatform
link_up=preprocess.link_uplist
link_down=preprocess.link_downlist
#################################################### Note that following part changes whenever station number changes
### New setup..
hello= preprocess.headway_list_up
sz = len(link_up)
i = 0
while i < sz/2 :
i = i+1
hello['1217'].append(link_up[i][0])
headwaylist_up = hello.values()
#######################################################################################################################
#############################################################################################################
variables()
#mod.write('#Objective function\nminimize cost : 0;\n\n')
######### Commented ...
#mod.write('#Objective function\n');
#mod.write('minimize cost: sum { i in {1000..'+str(num_dep)+'}} source[i];\n\n')
#mod.write('minimize cost: sum { i in array1} source[i];\n\n')
#mod.write('maximize cost: sum { i in {1000..'+str(num_dep)+'},j in {100..'+str(num_arr)+'}}st[i,j];\n\n')
#mod.write('minimize cost: sum { i in {1000..'+str(num_dep)+'}} source[i] + sum {j in {100..'+str(num_arr)+'}}sink[j];\n\n')
#mod.write('minimize cost: dummy; \n\n')
headwaynum=1
symmetrynum=1
symmetrynum1=1
dwellnum=1
travnum=1
turnaroundnum=1
con=1
values = 44
#mod.write('#Condition constraints\n\n')
#mod.write('subject to con'+str(con)+': sum{i in array1}source[i] <=' + str(values) + ';\n')
#con=con+1
mod.write('#Headway constraints\n\n')
dummy = 120
M=defaultdict(list)
M['1.0'] = dummy
M['2.0'] = dummy
M['3.0'] = dummy
M['4.0'] = dummy
M['5.0'] = dummy
M['6.0'] = dummy
M['7.0'] = dummy
M['8.0'] = dummy
M['9.0'] = dummy
M['10.0'] = dummy
M['11.0'] = dummy
M['12.0'] = dummy
###############################################################################
##############################################################################
############## Code designed for off peak period.....#####################
#########################################################################
####################################################################
srv_freq =defaultdict(list)
tol_ll = defaultdict(list)
tol_uu = defaultdict(list)
peak_srv_freq = defaultdict(list)
fileread = pd.read_csv('OD.csv')
offdemand = fileread["offdemand"]
srnum = fileread["SourceSrNum"]
dstnum = fileread["DestinationSrNum"]
tol_lower = fileread["tol_lower"]
tol_upper = fileread["tol_upper"]
psf = fileread["onpeak"]
for i in range(0,len(offdemand),1):
#print(i)
srdstpr = str(srnum[i]) + str(dstnum[i])
srv_freq[srdstpr] = offdemand[i]
print(srdstpr)
print(srv_freq[srdstpr])
tol_ll[srdstpr] = tol_lower[i]
tol_uu[srdstpr] = tol_upper[i]
peak_srv_freq[srdstpr] = psf[i]
##f_up[0] = 4 ; # CST-Panvel
##f_up[1] = 2 ; # CST-Andheri
##f_up[2] = 1; # Vdrd-panvel
##f_up[3] = 2 ; # CST-belapur
##f_up[4] = 0 ; # Vdrd-belapur
##f_up[5] = 2 ; #cst-vashi
##f_up[6] = 3 ; #cst-ba
##f_up[7] = 3 ; #thane-vashi
##f_up[8] = 2 ; #tna=nerul
##f_up[9] = 2 ; #thane-panvel
##f_up[10] = 1 ; #cst-mankhurd
##
##
##f_down[0] = 4 ; # CST-Panvel
##f_down[1] = 2 ; # CST-Andheri
##f_down[2] = 1; # Vdrd-panvel
##f_down[3] = 1 ; #panvel-andheri
##f_down[4] = 1 ; #andheri-panvel
##f_down[5] = 2 ; # CST-belapur
##f_down[6] = 0 ; # Vdrd-belapur
##f_down[7] = 2 ; #cst-vashi
##f_down[8] = 3 ; #cst-ba
##f_down[9] = 3 ; #thane-vashi
##f_down[10] = 2 ; #tna=nerul
##f_down[11] = 2 ; #thane-panvel
##f_down[12] = 1 ; #cst-mankhurd
##
trav_min = traversal_time.trav_min
headway(headwaylist_up)
headway(headwaylist_down)
mod.write('#Symmetry constraints\n\n')
symmetry2(symmetry_up,srv_freq,tol_ll, tol_uu)
symmetry2(symmetry_down,srv_freq,tol_ll, tol_uu)
#symmetry1(symmetry_up.values())
#symmetry1(symmetry_down.values())
###################################################################26 May #################################################################
###########################################################################################################################################
###########################################################################################################################################
## Length calculation for the bandra and andheri sevices:
############## Note that here stations number should be known:
ba_peak = peak_srv_freq['1124']
and_peak = peak_srv_freq['1125']
lngth = ba_peak + and_peak
tol_headway = 4 ;
### for bandra and andheri up direction ..
alpha = preprocess.frequency_list_up
# Departing from CST
ba_and_list = alpha['1124']
ba_and_list = ba_and_list + alpha['1125']
# We will modify the anurag function
mod.write('#Special headway constraints\n\n')
anurag1(ba_and_list, lngth, tol_headway)
## Length calculation for the bandra and andheri sevices - the other way:
ba_peak = peak_srv_freq['2411']
and_peak = peak_srv_freq['2511']
lngth = ba_peak + and_peak
tol_headway = 4 ;
### for bandra and andheri up direction ..
alpha = preprocess.frequency_list_down
# Departing from bandra to CST.. it containts the departure events at CST...
ba_and_list = alpha['2411']
### Andheri service departure events at Bandra
for item in alpha['2511']:
item = item + 1
ba_and_list.append(item)
anurag1(ba_and_list, lngth, tol_headway)
################################################################### 26 May####################################################################
##############################################################################################################################################
###########################################################################################################################################
### Commented for 4 hr timetable....
#mod.write('#Symmetry Arrival constraints\n\n')
#symmetry_arr(symmetry_up_arr)
#symmetry_arr(symmetry_down_arr)
#### Wrote this one for 4 or 5 hr timetable...
mod.write('#Symmetry Arrival constraints\n\n')
symmetry_arr1(symmetry_up_arr1,srv_freq)
symmetry_arr1(symmetry_down_arr1,srv_freq)
mod.write('#Dwell Time constraints\n\n')
dwell(dwell_up)
dwell(dwell_down)
mod.write('#Traversal Time constraints\n\n')
traversal(trav_up,travtime)
traversal(trav_down,travtime)
mod.write('#Turnaround constraints\n\n')
turnaround(turnaround_arr,turnaround_dep,numplat,link_up,link_down)
mod.write('#Linkage constraints\n\n')
linkage(link_up,link_down)
#mod.write('#Platform Constraints\n\n')
#platform()
f = open('updown.txt','w')
for i in range(1,len(link_up)+1):
a = str(link_up[i][0]) + ',' + str(link_down[i][0]) + '\n'
f.write(a)
f.close()
mod.close()