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)
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)
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)
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)