コード例 #1
0
def detect_waiting_dependency(train1, train2, tname1, tname2):
    """
    Performs algorithm 1 described in 'Mining Railway Delay Dependenies in
    Large-Scale Real-World Delay Data' (Flier, Gelashvili, Graffagnino,
    Nunkesser).
    """
    train1, train2 = trainsorting.sort_two_non_decreasing(train1, train2)
    train1 = trainsorting.append(train1, float('inf'))
    train2 = trainsorting.append(train2, 0)

    diff = numpy.empty(len(train1)) #pylint: disable = no-member
    for i in range(len(train1)):
        diff[i] = train1[i] - train2[i]

    points = 0
    points_star = 0
    start = diff[0]
    start_star = 0
    lastpointindex = 0 # pylint: disable = invalid-name
    end_star = 0

    for i in range(len(train1)):
        if diff[i] > start:
            if points > points_star:
                #update best solution
                points_star = points
                start_star = start
                end_star = train1[i-1]

            start = diff[i]
            notdone = True
            while notdone:
                if (train1[lastpointindex] < start and
                    lastpointindex < len(train1)):
                    lastpointindex += 1
                else:
                    notdone = False
            points = i - lastpointindex + 1
        else:
            points += 1

    #Write sorted list to file (Why do i do this?)
    #TODO: Ponder the pointfulness of this
    trainio.list_to_file(numbers_to_strings(train1[:len(train1)-1]),
                         tname1+'sorted'+'_offset.txt', header='OFFSET')
    trainio.list_to_file(numbers_to_strings(train2[:len(train2)-1]),
                         tname2+'sorted'+'_offset.txt', header='OFFSET')

    #plot in R:
    Rtrainplot(tname1, tname2, (points_star, start_star, end_star))
    print 'Number of points in optimal solution: %d'%points_star
    print 'Buffer time between the trains: %d'%start_star
    print 'Maximal delay: %d'%end_star
コード例 #2
0
def detect_blocking_dependency(train1, train2, tname1, tname2):
    """
    Performs algorithm 2 described in 'Mining Railway Delay Dependenies in
    Large-Scale Real-World Delay Data' (Flier, Gelashvili, Graffagnino,
    Nunkesser).
    """
    minimum = 1
    train1 = trainsorting.append(train1, float('inf'))
    train2 = trainsorting.append(train2, 0)
    intercepts = numpy.empty(len(train1)) # pylint: disable = no-member
    for i in range(len(train1)):
        intercepts[i] = train1[i] - train2[i]
    
    intercepts = trainsorting.sort_non_decreasing(intercepts)
    
    points = 0
    points_star = 0
    left = 0
    leftstar = 0
    right = 0
    rightstar = 0
    firstaboveindex = 0
    for j in range(1, len(intercepts)):
        left = intercepts[j-1]
        right = intercepts[j]
        if right - left >= minimum:
            while train1[firstaboveindex] < left:
                firstaboveindex += 1
            points = j - firstaboveindex
            if points > points_star:
                #update best solution
                points_star = points
                leftstar = left
                rightstar = right

    trainio.list_to_file(numbers_to_strings(train1[:len(train1)-1]),
                         tname1+'sorted'+'_offset.txt', header='OFFSET')
    trainio.list_to_file(numbers_to_strings(train2[:len(train2)-1]),
                         tname2+'sorted'+'_offset.txt', header='OFFSET')

    print (points_star, leftstar, rightstar)
    Rtrainplot(tname1, tname2, (points_star, leftstar, rightstar),
               waiting=False)
コード例 #3
0
ファイル: test.py プロジェクト: simbrant/NSB
def detect_waiting_dependency(train1, train2,tname1,tname2):
    """
    Performs algorithm 1 described in 'Mining Railway Delay Dependenies in
    Large-Scale Real-World Delay Data' (Flier, Gelashvili, Graffagnino,
    Nunkesser).
    """
    train1, train2 = trainsorting.sort_two_non_decreasing(train1, train2)
    train1 = trainsorting.append(train1, float('inf'))
    train2 = trainsorting.append(train2, 0)

    diff = numpy.empty(len(train1)) #pylint: disable = no-member
    for i in range(len(train1)):
        diff[i] = train1[i] - train2[i]

    points = 0
    points_star = 0
    start = diff[0]
    start_star = 0
    lastpointindex = 0 # pylint: disable = invalid-name
    end_star = 0

    for i in range(len(train1)):
        if diff[i] > start:
            if points > points_star:
                #update best solution
                points_star = points
                start_star = start
                end_star = train1[i-1]

            start = diff[i]
            notdone = True
            while notdone:
                if (train1[lastpointindex] < start and
                    lastpointindex < len(train1)):
                    lastpointindex += 1
                else:
                    notdone = False
            points = i - lastpointindex + 1
        else:
            points += 1
    
    print ('Pstar :%d %d') %(train1[points_star-1], train2[points_star-1])

    #Write sorted list to file
    trainio.list_to_file(numbers_to_strings(train1[:len(train1)-1]),
                         tname1+'sorted'+'_offset.txt', header='OFFSET')
    trainio.list_to_file(numbers_to_strings(train2[:len(train2)-1]),
                         tname2+'sorted'+'_offset.txt', header='OFFSET')

    #Do this in R instead
    #Globals are evil?
    plotname = "delayer"+tname1+"victim"+tname2+".pdf"
    os.system('Rscript trainplotter.R' + ' ' + tname1 + 'sorted_offset.txt'
              + ' ' + tname2 + 'sorted_offset.txt'
              +' %d %d %d' % (points_star, start_star, end_star) 
              + ' ' + plotname)
    os.system('xdg-open ' + plotname)
    #os.system('rm' + ' ' + '*_offset.txt')
    
    
    print 'Number of points in optimal solution: %d'%points_star
    print 'Buffer time between the trains: %d'%start_star
    print 'Maximal delay: %d'%end_star