示例#1
0
def update_dtw(data_feature, DTW):
    ids = data_feature.keys()
    for _id in ids:
        DTW[_id] = {}

    for _id in ids:
        for key in ids:
            try:
                DTW[_id][key]
                DTW[key][_id]
            except KeyError:
                value = Dtw(data_feature[_id], data_feature[key], calDistance)
                value = value.calculate() / len(value.get_path())
                DTW[_id][key] = value
                DTW[key][_id] = value
示例#2
0
def update_dtw(data_feature, DTW):
    ids = data_feature.keys()
    for _id in ids:
        DTW[_id] = {}

    for _id in ids:
        for key in ids:
            try:
                DTW[_id][key]
                DTW[key][_id]
            except KeyError:
                value = Dtw(data_feature[_id], data_feature[key], calDistance)
                value = value.calculate()/len(value.get_path())
                DTW[_id][key] = value
                DTW[key][_id] = value
示例#3
0
def fullMatchDistance(route1,
                      route2,
                      step1=100000,
                      step2=100000,
                      method='lcs',
                      radius1=2000):
    ## see how if "route1" can partially match with "route"
    ## will be mainly used in matching with transit route
    # print(lst[0],lst[-1])
    # print(route)
    dis = 999999
    if len(route1) < 2 or len(route2) < 2:
        return dis

## using DTW Iteration
    if method == 'dtw':
        new_dis = dynamicTimeWarp(refineRoute(route1, step1),
                                  refineRoute(route2, step2), calDistance)
## using DTW Recursion
    if method == 'DTW':
        aa = Dtw(refineRoute(route1, step1), refineRoute(route2, step2),
                 calDistance)
        new_dis = aa.calculate_distance()
## using symmetric DTW
    if method == 'DTWSym':
        aa = DtwSym(refineRoute(route1, step1), refineRoute(route2, step2),
                    calDistance)
        new_dis = aa.calculate_distance()
## using Asymmetric DTW
    if method == 'DTWAsym':
        aa = DtwAsym(refineRoute(route1, step1), refineRoute(route2, step2),
                     calDistance)
        new_dis = aa.calculate_distance()
## using Frechet
    if method == 'Frechet':
        new_dis = Frechet(refineRoute(route1, step1),
                          refineRoute(route2, step2))


## using lcs
    if method == 'lcs':
        new_dis = lcsScore(refineRoute(route1, step1),
                           refineRoute(route2, step2), radius1)
    if new_dis < dis:
        dis = new_dis

    # print(dis)
    return dis
def existingMatchDistance(route1,
                          route2,
                          step1=100000,
                          step2=100000,
                          method='lcs',
                          radius1=2000):
    ## see how if "route1" can match with an existing route "route2"
    ## will be mainly used in matching with transit route
    # print(lst[0],lst[-1])
    # print(route)
    dis = 999999
    for start_route2 in range(len(route2)):
        coverage_start = find_near(route1, route2[start_route2], radius1)
        if coverage_start != []:
            break
    for end_route2 in range(len(route2) - 1, -1, -1):
        coverage_end = find_near(route1, route2[end_route2], radius1)
        if coverage_end != []:
            break
    # print(start_route2,end_route2)
    # print(coverage_start,coverage_end)
    if abs(start_route2 -
           end_route2) > 1 and start_route2 != len(route2) and end_route2 != 0:
        start_route1 = coverage_start[0]
        end_route1 = coverage_end[-1]

        if abs(start_route1 - end_route1) >= 1:
            ## using DTW
            if method == 'DTW':
                if start_route1 < end_route1:
                    aa = Dtw(
                        refineRoute(route1[start_route1:end_route1 + 1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2), calDistance)
                elif end_route1 < start_route1:
                    aa = Dtw(
                        refineRoute(route1[end_route1:start_route1 + 1][::-1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2), calDistance)

                new_dis = aa.calculate_distance()
        ## using symmetric DTW
            if method == 'DTWSym':
                if start_route1 < end_route1:
                    aa = Dtw(
                        refineRoute(route1[start_route1:end_route1 + 1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2), calDistance)
                elif end_route1 < start_route1:
                    aa = Dtw(
                        refineRoute(route1[end_route1:start_route1 + 1][::-1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2), calDistance)

                new_dis = aa.calculate_distance()
        ## using DTW
            if method == 'DTWAsym':
                if start_route1 < end_route1:
                    aa = Dtw(
                        refineRoute(route1[start_route1:end_route1 + 1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2), calDistance)
                elif end_route1 < start_route1:
                    aa = Dtw(
                        refineRoute(route1[end_route1:start_route1 + 1][::-1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2), calDistance)

                new_dis = aa.calculate_distance()
        ## using Frechet
            if method == 'Frechet':
                if start_route1 < end_route1:
                    new_dis = Frechet(
                        refineRoute(route1[start_route1:end_route1 + 1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2))
                elif end_route1 < start_route1:
                    new_dis = Frechet(
                        refineRoute(route1[end_route1:start_route1 + 1][::-1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2))
        ## using lcs
            if method == 'lcs':
                if start_route1 < end_route1:
                    new_dis = lcsScore(
                        refineRoute(route1[start_route1:end_route1 + 1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2), radius1)
                elif end_route1 < start_route1:
                    # print(route1[start_route1:end_route1-1])
                    # print(start_route1,end_route1)
                    # print(len(route1[start_route1:end_route1-1:-1]))
                    new_dis = lcsScore(
                        refineRoute(route1[end_route1:start_route1 + 1][::-1],
                                    step1),
                        refineRoute(route2[start_route2:end_route2 + 1],
                                    step2), radius1)
            if new_dis < dis:
                dis = new_dis

    # print(dis)
    return [start_route2, end_route2, dis]