예제 #1
0
파일: train_algos.py 프로젝트: simbrant/NSB
def algorithm1_mod(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()] #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(i, s_i[i])
                k += 1
        else:
            k += 1
    if sol:
        return (k_star, s_star, e_star)
    else:
        return algorithm1(train1, train2)
예제 #2
0
파일: train_algos.py 프로젝트: simbrant/NSB
def algorithm1(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.
    """

    train1, train2 = train_sorting.sort_two_non_dec(train1, train2)
    train1 = train_sorting.append(train1, float('inf'))
    train2 = train_sorting.append(train2, float(0))

    #Locals
    diff = train_sorting.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)
예제 #3
0
파일: train_algos.py 프로젝트: simbrant/NSB
def algorithm2(train1, train2, minwidth=60):
    """
    - Performs algorithm 2 described in 'Mining Railway Delay Dependencies 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, train2 = train_sorting.sort_two_non_dec(train1, train2)
    train1 = train_sorting.append(train1, float('inf'))
    train2 = train_sorting.append(train2, 0)

    #Locals
    intercepts = sorted(train_sorting.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)
예제 #4
0
파일: new_alg1mod.py 프로젝트: simbrant/NSB
def algorithm1_mod3(delayer, victim):
    """
    Solves the extended version of the waiting problem, in O(n^2)
    """
    delayer, victim = sort_two_non_dec(delayer, victim)
    n = len(delayer)
    diff = difference(delayer, victim)
    rbar = int(math.floor(n*0.025))
    kstar = 0
    sstar = 0
    estar = 0
    for i in range(n):
        r = 0
        s = diff[i]
        l = 0
        k = 0
        e = 0
        while l < n:
            if victim[l] >= delayer[l] - s and delayer[l] >= s:
                k += 1
            if delayer[l] >= s and victim[l] < delayer[l] - s:
                r += 1
            if r > rbar:
                temp = delayer[l]
                # backtrack
                while l >= 0:
                    if delayer[l] != temp and victim[l] >= delayer[l] - s:
                        break
                    elif delayer[l] == temp and victim[l] >= delayer[l] -s:
                        k -= 1
                    l -= 1
                e = delayer[l]
                break
            l += 1
        if e == 0:
            e = delayer[l- 1]

        if k > kstar:
            sstar = s
            estar = e
            kstar = k

    return (kstar, sstar, estar)
예제 #5
0
파일: new_alg1mod.py 프로젝트: simbrant/NSB
def algorithm1_mod2(delayer, victim):
    
    delayer, victim = sort_two_non_dec(delayer, victim)
    diff = difference(delayer, victim)
    n = len(delayer)
    rbar = int(math.floor(n*0.025))
    kstar = 0
    sstar = 0
    estar = 0
    for i in range(n):
        r = 0
        s = int(diff[i])
        k = 0
        e = s
        for j in range(i):
            if int(victim[j]) >= int(delayer[j]) - s and delayer[j] >= s:
                k += 1
            elif int(victim[j]) < int(delayer[j]) - s and delayer[j] >= s:
                r += 1

        if r <= rbar:
            for j in range(i, n):
                if int(victim[j]) >= int(delayer[j]) - s:
                    k += 1
                    e = delayer[j]
                elif r < rbar:
                    r +=1
                else:
                    break                
        else:
            k = 0
            s = s
            e = s

        if k > kstar:
            kstar = k
            sstar = s
            estar = e

    return (kstar, sstar, estar)