def simulatedAnnealing(data, hotel, maxIterations = 10):
    finalBestOrdering = {}
    finalBestTime = 0
    hotelObj = Place(None, None, None, None, None)
    hotelObj.pos = hotel
    foodLists = {}
    entertainLists = {}

    #get all food places out
    #edited this block
    for key in data:
        foodLists[key], entertainLists[key] = [], []
        for attraction in data[key]:
            if attraction.category == 'FOOD_BEVERAGE':
                foodLists[key].append(attraction)
            else:
                entertainLists[key].append(attraction)

    for key in data:
        if len(data[key]) == 0:
            finalBestOrdering[key] = data[key]
            continue

        elif len(data[key]) == 1:
            finalBestOrdering[key] = data[key]
            finalBestTime += getTime(hotelObj, data[key][0]) + getTime(data[key][0], hotelObj)
            continue

        ##edited these 2 lines
        curr = [hotelObj] + getInitialOrdering(foodLists[key], entertainLists[key]) + [hotelObj]
        foodIndeces, entertainIndeces = getIndeces(curr)

        bestOrdering = curr
        bestTime = getTime(curr)

        for i in range(maxIterations):
            curr = randomizeOrdering(curr, foodIndeces, entertainIndeces)
            val = getTime(curr)
            if val < bestTime:
                bestTime = val
                bestOrdering = curr
            else:
                rand = random.random()
                if rand < temperature(i):
                    bestTime = val
                    bestOrdering = curr

        finalBestOrdering[key] =  bestOrdering[1:-1]
        finalBestTime += bestTime

    return finalBestOrdering, finalBestTime
def simulatedAnnealing(data, hotel, maxIterations=100):
    finalBestOrdering = {}
    finalBestTime = 0
    hotelObj = Place(None, None, None, None)
    hotelObj.pos = hotel

    for key in data:
        if len(data[key]) == 0:
            finalBestOrdering[key] = data[key]
            continue

        elif len(data[key]) == 1:
            finalBestOrdering[key] = data[key]
            finalBestTime += getTime(hotelObj, data[key][0]) + getTime(
                data[key][0], hotelObj)
            continue

        curr = [hotelObj] + data[key] + [hotelObj]
        #numAttractions = len(data[key])

        # for _ in range(numAttractions):
        # 	rand = random.randint(0, len(data[key]))
        # 	curr.append(data[key][rand])
        # 	data[key].remove(data[key][rand])

        #curr.append(hotelObj)
        bestOrdering = curr
        bestTime = getTime(curr)

        for i in range(maxIterations):
            curr = randomizeOrdering(curr)
            val = getTime(curr)
            if val < bestTime:
                bestTime = val
                bestOrdering = curr
            else:
                rand = random.random()
                if rand < temperature(i):
                    bestTime = val
                    bestOrdering = curr

        finalBestOrdering[key] = bestOrdering[1:-1]
        finalBestTime += bestTime
    return finalBestOrdering, finalBestTime
Esempio n. 3
0
def simulatedAnnealing(data, hotel, maxIterations=10):
    finalBestOrdering = {}
    finalBestTime = 0
    hotelObj = Place(None, None, None, None, None)
    hotelObj.pos = hotel

    for key in data:
        if len(data[key]) == 0:
            finalBestOrdering[key] = data[key]
            continue

        elif len(data[key]) == 1:
            finalBestOrdering[key] = data[key]
            finalBestTime += getTime([hotelObj] +
                                     [data[key][0]]) + getTime([data[key][0]] +
                                                               [hotelObj])
            continue

        curr = [hotelObj] + data[key] + [hotelObj]

        bestOrdering = curr
        bestTime = getTime(curr)

        for i in range(maxIterations):
            curr = randomizeOrdering(curr)
            val = getTime(curr)
            if val < bestTime:
                bestTime = val
                bestOrdering = curr
            else:
                rand = random.random()
                if rand < temperature(i):
                    bestTime = val
                    bestOrdering = curr

        finalBestOrdering[key] = bestOrdering[1:-1]
        finalBestTime += bestTime
    return finalBestOrdering, finalBestTime