Ejemplo n.º 1
0
def lowerBoundImprovementTry(graph, source, target):
    """
    Uses the label setting algorithm to find the paths, but with a dijkstra preprocessing
    """
    preprocessing = binarySearchDijkBiCr(graph, source, target)
    initSingleNode(graph, source)
    for distdang in preprocessing.keys():
        target.labelList.append((distdang[0], distdang[1], target, None, None))
    labelSettingAlgorithm(source, target)
    for _ in range(0, len(preprocessing)):
        target.labelList.pop(0)
def dijkstraBiCrIteration(graph, source, target, increaseVal):
    """
    Search solution by incrementing the value of α, so at each incrementation ther will be an execution of
    bicriteria Dijkstra algorithm

    increaseVale : float
        Value between 0 and 1, that is used to increment the value of alpha, each iteration.
        smallest value = more accurate results, but more execution time

    @return
        None if increaseVal is incorrect

        listDistDang : {
            (dist, dang) : alpha
            (dist, dang) : alpha
            ...
        }
    """
    if increaseVal >= 1 or increaseVal <= 0:
        print("Please, insert a number greater than 0, but smaller than 1")
        return None

    listDistDang = {}
    counter = 0
    alpha = 0
    ending = False

    while alpha <= 1:
        counter += 1
        initSingleNode(graph, source)
        dijkstraBiCrit(source, target, alpha)

        distDang = (target.distance, target.danger)

        if distDang in list(listDistDang.keys()):
            alpha += increaseVal
            if not ending and alpha >= 1:
                alpha = 1
                ending = True
        else:
            listDistDang.update({distDang: alpha})
            alpha = alpha / 2
    return listDistDang
Ejemplo n.º 3
0
def valuesCalculator(graph, source, target):
    """
    Function for calculate the value that will be shown in the summary table
    """
    resList = []

    initSingleNode(graph, source)
    start = time.time()
    dijkstraOneToAll(source)  #one to all
    end = time.time()
    dist = source.shortestPaths.get(target)
    times = end - start
    resList.append((dist, times))

    initSingleNode(graph, source)
    start = time.time()
    dijkstraOneToOne(graph, source, target)  #one to one
    end = time.time()
    dist = target.minWeight
    times = 0
    resList.append((dist, times))

    initSingleNode(graph, source)
    start = time.time()
    dijkstraListOfCandidate(source, target)  #list of candiadate
    end = time.time()
    dist = target.minWeight
    times = end - start
    resList.append((dist, times))

    initSingleNode(graph, source)
    start = time.time()
    a_star(source, target)  #a star
    end = time.time()
    dist = target.minWeight
    times = end - start
    resList.append((dist, times))

    return resList
######################
#### BACKTRACKING ####
######################
# dbgList = []
# pointer = target
# while pointer != source :
#     dbgList.append(pointer.index)
#     pointer = pointer.predecessor
# dbgList.append(pointer.index)
# dbgList = reversed(dbgList)
# print(*dbgList, sep="->") # Print all the path in the form of node->node

##########################################################################################
##########################################################################################
print("\n", "*"*40, "LABEL SETTING ALGORITHM", "*"*40)
initSingleNode(graph, source)

tmp_list= []
for n in range (0, 1):
    initSingleNode(graph, source)
    start = time.time()
    count = labelSettingAlgorithm(source, target) # algorithm
    end = time.time()
    tmp_list.append(end-start)
print("AVGtime:", sum(tmp_list) / len(tmp_list))
print("len:", len(target.labelList))
print("loops:", count)
# for label in target.labelList :
#     print((label[0], label[1]))

def binarySearchDijkBiCr(graph, source, target):
    """
    Search the solutions using Dijkstra Bicriteria algorithm, changing the value of α following a binary search algorihm

    Faster than the normal dijkstra iteration

    @return
        totList : {
            (dist, dang) : alpha
            (dist, dang) : alpha
            ...
         }
    """
    leftList = []  # store all results of α € [0, 0.5]
    rightList = []  # store all results of α € ]0.5, 0]

    totList = {}  # [(distDang, alpha), ...]

    approx = 5  # value of approximations
    right = 1
    left = 0

    dijkstraBiCrit(source, target, right)
    res = (target.distance, target.danger)
    rightList.append(res)
    totList.update({res: right})

    initSingleNode(graph, source)
    dijkstraBiCrit(source, target, left)
    res = (target.distance, target.danger)
    leftList.append(res)
    totList.update({res: left})

    listDimension = None
    condition = True
    while condition:
        alpha = (right - left) / 2
        tmp = str(alpha)
        alpha = float(tmp[:approx])
        if alpha > 0:
            alpha = alpha + left
            initSingleNode(graph, source)
            dijkstraBiCrit(source, target, alpha)
            res = (target.distance, target.danger)

            if res in leftList:
                left = alpha
            elif res in rightList:
                right = alpha
            else:
                if res[0] < leftList[len(
                        leftList
                ) - 1][0]:  # is res the lower than the lowest value in leftList
                    leftList.append(res)
                    totList.update({res: alpha})
                    right = alpha
                else:
                    rightList.append(res)
                    totList.update({res: alpha})
                    left = alpha
        else:
            if listDimension == len(totList):
                condition = False
            else:
                listDimension = len(totList)
            right = 1
            left = 0
    return totList
for row, nodes in enumerate(test):
    column = 1
    row = row + 2 + const
    if row == 6:
        const = 1
    elif row == 12:
        const = 2

    message = str(nodes[0]) + "->" + str(nodes[1])
    worksheet.write(row, 0, message)

    source = graph[nodes[0]]
    target = graph[nodes[1]]

    ####Binary search#####
    initSingleNode(graph, source)
    start = time.time()
    infoList = binarySearchDijkBiCr(graph, source, target)
    end = time.time()

    worksheet.write(row, column, len(infoList))
    column += 1
    worksheet.write(row, column, end - start)
    column += 1

    ####labelSetting####
    initSingleNode(graph, source)
    start = time.time()
    loop = labelSettingAlgorithm(source, target)
    end = time.time()