def waiting(self, train, dep_win, arr_win, min_window=1, min_num_p=10): """ Uses algorithm1 on the delay data Returns a tuple of lists (departures, arrivals). Both departures and arrivals are lists of lists containing (delayingtrain,thistrain, algoresult). """ victim = self.data_to_array(train, 'Pl.avg.', 'Fakt.avg.') sig_deps = [] sig_arrs = [] #departures for key in dep_win: delayer = self.data_to_array(key, 'Pl.avg.', 'Fakt.avg.') pairs = train_sorting.sort_out_complete_pairs(delayer, victim) result = train_algos.algorithm1(pairs[0], pairs[1]) if result[0] > min_num_p: if result[1] < result[2] and result[1] > 0: if abs(result[2] - result[1]) >= min_window: sig_deps.append([key, result]) #arrivals for key in arr_win: delayer = self.data_to_array(key, 'Pl.ank.', 'Fakt.ank.') pairs = train_sorting.sort_out_complete_pairs(delayer, victim) result = train_algos.algorithm1(pairs[0], pairs[1]) if result[0] > min_num_p: if result[1] < result[2] and result[1] > 0: if abs(result[2] - result[1]) >= min_window: sig_arrs.append([key, result]) return sig_deps, sig_arrs
def algorithm1_mod4(train1, train2): """ - Performs algorithm 1 described in 'Mining Railway Delay Dependencies 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. - This is a modified version of algorithm1 as described in section 5 of 'Mining Railway Delay Dependencies in Large-Scale Real-World Delay Data' (Flier, Gelashvili, Graffagnino, Nunkesser). """ train1, train2 = train_sorting.sort_two_non_dec(train1, train2) train1 = train_sorting.append(train1, float('inf')) train2 = train_sorting.append(train2, float(0)) #set max number of exceptional points rbar = 0.025*(len(train1)) #Locals exceptional = Queue.PriorityQueue(maxsize=int(math.floor(rbar))) s_i = train_sorting.difference(train1, train2) k = 0 #pylint:disable=invalid-name k_star = 0 s = s_i[0]#pylint:disable=invalid-name s_star = 0 l = 0 #pylint:disable=invalid-name e_star = 0 # maximal delay sol = False #The algorithm: for i in range(len(train1)): if s_i[i] > s: if exceptional.full(): sol = True #cannot extend current solution to train1[i], train2[i] if k > k_star: #update best solution k_star = k s_star = s e_star = train1[i - 1] #initialize new solution s = s_i[exceptional.get()[1]] #pylint:disable=invalid-name #find first point in new interval while train1[l] < s and l < len(train1): l += 1 #pylint:disable=invalid-name k = i - l + 1 else: exceptional.put((s_i[i], i)) k += 1 else: k += 1 if sol: return (k_star, s_star, e_star) else: return algorithm1(train1, train2)
CSV_WRITER2 = csv.writer(open('ALGRES/' + FILENAME[11:21] + '_blocking' + '.csv', 'w'), delimiter=',') #write header CSV_WRITER1.writerow(['VICTIM', 'DELAYER', 'ISVICTIMARRIVING', 'ISDELAYERARRIVING', 'NUMOFPOINTS', 'START', 'END']) CSV_WRITER2.writerow(['VICTIM', 'DELAYER', 'ISVICTIMARRIVING', 'ISDELAYERARRIVING', 'NUMOFPOINTS', 'START', 'END']) for train in DATASTRUCTURE: for delayer in DATASTRUCTURE[train]: delayer_arr = FILENAME[18:21] == 'ARR' train_arr = FILENAME[11:14] == 'ARR' delay = get_matching_delay(delayer, train, delayer_arr, train_arr) #run algorithms res1 = algorithm1(array(delay[0]), array(delay[1])) res2 = algorithm2(array(delay[0]), array(delay[1])) if res1[2] - res1[1] > 90: CSV_WRITER1.writerow([train, delayer, int(train_arr), int(delayer_arr), res1[0], res1[1], res1[2]]) if res2[2] - res2[1] > 120: CSV_WRITER2.writerow([train, delayer, int(train_arr), int(delayer_arr), res2[0], res2[1], res2[2]])
y_cor = ([int(100*math.log(i+1)*math.cos(random.random())) for i in range(25)] + [int(1000*math.log(i+1)*math.sin(random.random())) for i in range(25)] + [int(1000*random.uniform(-0.5, 1)) for i in range(15)] + map(lambda x: x + int(random.random()*100),#pylint:disable=bad-builtin,deprecated-lambda [linear[i] - 360 for i in range(40)])) return x_cor, y_cor if __name__ == '__main__': PTS = test_data() RES1 = algorithm1_mod3(PTS[0], PTS[1]) RES2 = algorithm1(PTS[0], PTS[1]) RES3 = algorithm2(PTS[0], PTS[1]) SCORE1 = delaycounter(PTS[0], PTS[1], (RES1[1], RES1[2]), 'waiting') SCORE2 = delaycounter(PTS[0], PTS[1], (RES2[1], RES2[2]), 'waiting') SCORE3 = delaycounter(PTS[0], PTS[1], (RES3[1], RES3[2]), 'blocking') print SCORE1 print RES1 print SCORE2 print RES2 print SCORE3 print RES3 scatter_trains(PTS[0], PTS[1], RES1) scatter_trains(PTS[0], PTS[1], RES2) scatter_trains(PTS[0], PTS[1], RES3, False) plt.show()