Ejemplo n.º 1
0
def anneal(init_function,move_operator,objective_function,max_evaluations,\
            start_temp,alpha, draw, every):

    # wrap the objective function (so we record the best)
    objective_function = ObjectiveFunction(objective_function)

    current = init_function()
    current_score = objective_function(current)
    num_evaluations = 0
    cl = None
    t = None

    if draw == True:
        cl = visualizer.loadCityFile("./files/cityPixels.txt")
        cities = fileio.readCities("./files/cities.txt")

    cooling_schedule = kirkpatrick_cooling(start_temp, alpha)

    logging.info('anneal started: score=%f', current_score)

    for temperature in cooling_schedule:
        done = False
        # examine moves around our current position
        for next in move_operator(current):
            if num_evaluations >= max_evaluations:
                done = True
                break

            next_score = objective_function(next)

            if draw == True:
                if (num_evaluations % every) == 0:
                    best = objective_function.best
                    bt = visualizer.makeTour(best, cities)
                    visualizer.plotTour(bt, cl, outFile = "./images/" + \
                                        str(num_evaluations) + ".png")

            num_evaluations += 1

            # probablistically accept this solution
            # always accepting better solutions
            p = P(current_score, next_score, temperature)
            if random.random() < p:
                current = next
                current_score = next_score
                break

        # see if completely finished
        if done: break

    best_score = objective_function.best_score
    best = objective_function.best
    logging.info('final temperature: %f', temperature)
    logging.info('anneal finished: num_evaluations=%d, best_score=%f',
                 num_evaluations, best_score)
    return (num_evaluations, best_score, best)
Ejemplo n.º 2
0
Archivo: sa.py Proyecto: jhoward/gk-12
def anneal(init_function, move_operator, objective_function, max_evaluations, start_temp, alpha, draw, every):

    # wrap the objective function (so we record the best)
    objective_function = ObjectiveFunction(objective_function)

    current = init_function()
    current_score = objective_function(current)
    num_evaluations = 0
    cl = None
    t = None

    if draw == True:
        cl = visualizer.loadCityFile("./files/cityPixels.txt")
        cities = fileio.readCities("./files/cities.txt")

    cooling_schedule = kirkpatrick_cooling(start_temp, alpha)

    logging.info("anneal started: score=%f", current_score)

    for temperature in cooling_schedule:
        done = False
        # examine moves around our current position
        for next in move_operator(current):
            if num_evaluations >= max_evaluations:
                done = True
                break

            next_score = objective_function(next)

            if draw == True:
                if (num_evaluations % every) == 0:
                    best = objective_function.best
                    bt = visualizer.makeTour(best, cities)
                    visualizer.plotTour(bt, cl, outFile="./images/" + str(num_evaluations) + ".png")

            num_evaluations += 1

            # probablistically accept this solution
            # always accepting better solutions
            p = P(current_score, next_score, temperature)
            if random.random() < p:
                current = next
                current_score = next_score
                break

        # see if completely finished
        if done:
            break

    best_score = objective_function.best_score
    best = objective_function.best
    logging.info("final temperature: %f", temperature)
    logging.info("anneal finished: num_evaluations=%d, best_score=%f", num_evaluations, best_score)
    return (num_evaluations, best_score, best)
Ejemplo n.º 3
0
            copy = tour[:]
            if i < j:
                copy[i:j + 1] = reversed(tour[i:j + 1])
            else:
                copy[i + 1:] = reversed(tour[:j])
                copy[:j] = reversed(tour[i + 1:])
            if copy != tour:  # no point returning the same tour
                yield copy


def run_sa(iterations, dist, start, alpha, draw=True, every=100):
    init_function = lambda: init_random_tour(len(dist[0]))
    objective_function = lambda tour: -tour_length(dist, tour)
    move_operator = reversed_sections

    iterations,score,best = sa.anneal(init_function, move_operator, \
                                        objective_function, iterations, \
                                        start, alpha, draw, every)

    print iterations, score, best
    return best


if __name__ == "__main__":
    cities = fileio.readCities("./files/cities.txt")
    dist = fileio.readDistances("./files/distMiles.txt")
    best = run_sa(5000, dist, 100, 0.90, draw=True, every=100)

    bt = visualizer.makeTour(best, cities)
    fileio.writeTour("tour.txt", bt)
Ejemplo n.º 4
0
        x = ch[t][0]
        y = ch[t][1]

        if xOld:
            imd.line((xOld, yOld, x, y), fill = (0, 0, 0), width = 4)
    
        xOld = x
        yOld = y

    imd.line((xOld, yOld, ch[first][0], ch[first][1]), fill = (0, 0, 0), \
                width = 4)


def plotCities(imd, ch, s = 5):
    """For a given city hash, draws the points onto a img draw object.
    """

    for x,y in ch.values():
        imd.ellipse((x - s, y - s, x + s, y + s), \
                    fill = (0, 0, 0))


if __name__ == "__main__":
    cl = loadCityFile("./files/cityPixels.txt")
    tour = loadTour("tour.txt")
    cities = fileio.readCities("./files/cities.txt")
    tourNum = loadTourNum(tour, cities)
    dist = fileio.readDistances("./files/distMiles.txt")
    plotTour(tour, cl)
    print "Tour length:" + str(tsp.tour_length(dist,tourNum))