def algorithm1(train1, train2): """ - Performs algorithm 1 described in 'Mining Railway Delay Dependenies in Large-Scale Real-World Delay Data' (Flier, Gelashvili, Graffagnino, Nunkesser). - train1 and train2 must be numpy arrays containing delay times of the trains there can be no missing values, and train1[d], train2[d] must be delay data from the same day d. Implicitly train1 and train2 must be of equal length. """ train1, train2 = trainsorting.sort_two_non_decreasing(train1, train2) train1 = trainsorting.append(train1, float('inf')) train2 = trainsorting.append(train2, float(0)) #Locals diff = trainsorting.difference(train1, train2) points = 0 points_star = 0 bft = diff[0] #buffertime bft_star = 0 lpi = 0 #last point index md_star = 0 # maximal delay #The algorithm: for i in range(len(train1)): if diff[i] > bft: if points > points_star: #update best solution points_star = points bft_star = bft md_star = train1[i-1] bft = diff[i] notdone = True while notdone: if (train1[lpi] < bft and lpi < len(train1)): lpi += 1 else: notdone = False points = i - lpi + 1 else: points += 1 return (points_star, bft_star, md_star)
def algorithm2(train1, train2, minwidth=1): """ - Performs algorithm 2 described in 'Mining Railway Delay Dependenies in Large-Scale Real-World Delay Data' (Flier, Gelashvili, Graffagnino, Nunkesser). - Optional parameter minwidth describes the minimum requiered headway between trains using the same infrastructure, varies from station to station. - train1 and train2 must be numpy arrays containing delay times of the trains there can be no missing values, and train1[d], train2[d] must be delay data from the same day d. Implicitly train1 and train2 must be of equal length. """ train1 = trainsorting.append(train1, float('inf')) train2 = trainsorting.append(train2, 0) #Locals intercepts = sorted(trainsorting.difference(train1, train2)) points = 0 points_star = 0 left = 0 leftstar = 0 right = 0 rightstar = 0 fai = 0 #index of first point above stripe #The algorithm for j in range(1, len(intercepts)): left = intercepts[j-1] right = intercepts[j] if right - left >= minwidth: while train1[fai] < left: fai += 1 points = j - fai if points > points_star: #update best solution points_star = points leftstar = left rightstar = right return (points_star, leftstar, rightstar)