def test_recombine(): # when recombining the edges that appear in the child should # be present in one or both parents (only recombing the existing edges # not making new ones) for i in xrange(1000): # create two parent tours of same length tour_len = int(3 + random.random() * 12) tour1 = tsp.init_random_tour(tour_len) tour2 = tsp.init_random_tour(tour_len) child = tsp.recombine(tour1, tour2) assert len(child) == tour_len # make sure child edges appear in parent edges parent_edges = set(tsp.edges(tour1)).union(set(tsp.edges(tour2))) for edge in tsp.edges(child): assert edge in parent_edges
def solve_anneal(aff): objective_function=lambda tour: tour_sum_affinities(aff,tour) init_function=lambda: tsp.init_random_tour(sqrt(len(aff))) move_operator=tsp.reversed_sections #suggested parameters for simulated annealing: start_temp = 10.0 max_iterations = 5000 alpha = .001**(1/max_iterations) from sa import anneal iterations,score,best=anneal(init_function,move_operator,objective_function,max_iterations,start_temp,alpha) return iterations,score,best
def get_parameters(): global build_dict_thread, polyline_dict cycles = int(request.form['cycles']) algorithm = request.form['algorithm'] start_temp = float(request.form['start_temp']) alpha = float(request.form['alpha']) move_operator = request.form['move_operator'] start_city = int(request.form['start']) mode = request.form['mode'] selected_cities = request.form['selected_cities'] return_list = selected_cities.split(',') id_list = [] current_score=[] animation_coords = [] nodes2 = [] animation_steps = [] #Extract city ids and names into lists then look up coordinates and build #a dictionary with id as the key and a tuple of coordinates as the value for i in range(len(return_list)): if i%2 == 0: id_list.append(int(return_list[i])) for id in id_list: nodes2.append(model.session.query(model.City).filter_by(id = id).one()) coords = tsp.read_coords_db(nodes2) i=0 coord_dict = {} for id in id_list: coord_dict[id]=coords[i] i+= 1 #Build the distance matrix. The distance matrix is of the form #{(i, j): dist, ... (i,j): dist} if mode == "as_the_crow_flies": matrix = tsp.distance_matrix2(coords, id_list) elif mode == "roads": matrix = tsp.road_matrix(polyline_dict, id_list) elif mode == "airline": matrix = tsp.air_matrix(coords, id_list) else: return "Error" #Choose our algorithm init_function =lambda: tsp.init_random_tour(id_list)#returns shuffled objective_function=lambda tour: -tsp.tour_length(matrix,tour) #note negation move_dict={"swapped_cities": tsp.swapped_cities, "reversed_sections": tsp.reversed_sections} if algorithm == "hillclimb": result = tsp.hillclimb(init_function, move_dict[move_operator], objective_function, cycles) num_evaluations, best_score, best, animation_steps, current_score = result elif algorithm == "hill_restart": result = tsp.hillclimb_and_restart(init_function, move_dict[move_operator], objective_function, cycles) num_evaluations, best_score, best, animation_steps, current_score = result elif algorithm == "nearest": result = tsp.greedy(matrix, start_city) num_evaluations, best_score, best = result elif algorithm == "annealing": result = tsp.anneal(init_function, move_dict[move_operator], objective_function, cycles,start_temp,alpha) num_evaluations, best_score, best, animation_steps, current_score = result else: print "error" return "error" tour_coords = tsp.drawtour_on_map2(coord_dict, best) #coordinates for each step for i in range(len(animation_steps)): animation_coords.append(tsp.drawtour_on_map2(coord_dict, animation_steps[i])) tour_cities = convert_tour_to_city(best) if mode == "roads" and not build_dict_thread.isAlive(): poly_list, data = poly_line_tour2(best) poly_animation_steps = polyline_animation_steps2(animation_steps) else: poly_list = [] poly_animation_steps = [] results = {"iterations" : num_evaluations, "best_score" : best_score, "route" : best, "tour_coords": tour_coords, "tour_cities": tour_cities, "animation_coords": animation_coords, "current_score": current_score, "poly_list": poly_list, "poly_animation_steps": poly_animation_steps} data = json.dumps(results) return data
import tsp import profile coords=tsp.read_coords(file('city500.txt')) init_function=lambda: tsp.init_random_tour(len(coords)) matrix=tsp.cartesian_matrix(coords) objective_function=lambda tour: -tsp.tour_length(matrix,tour) move_operator=tsp.reversed_sections max_iterations=1000 #profile.run('tsp.run_hillclimb(init_function,move_operator,objective_function,max_iterations)') profile.run('tsp.run_evolve(init_function,move_operator,objective_function,max_iterations)')
def get_parameters(): global build_dict_thread, polyline_dict cycles = int(request.form['cycles']) algorithm = request.form['algorithm'] start_temp = float(request.form['start_temp']) alpha = float(request.form['alpha']) move_operator = request.form['move_operator'] start_city = int(request.form['start']) mode = request.form['mode'] selected_cities = request.form['selected_cities'] return_list = selected_cities.split(',') id_list = [] current_score = [] animation_coords = [] nodes2 = [] animation_steps = [] #Extract city ids and names into lists then look up coordinates and build #a dictionary with id as the key and a tuple of coordinates as the value for i in range(len(return_list)): if i % 2 == 0: id_list.append(int(return_list[i])) for id in id_list: nodes2.append(model.session.query(model.City).filter_by(id=id).one()) coords = tsp.read_coords_db(nodes2) i = 0 coord_dict = {} for id in id_list: coord_dict[id] = coords[i] i += 1 #Build the distance matrix. The distance matrix is of the form #{(i, j): dist, ... (i,j): dist} if mode == "as_the_crow_flies": matrix = tsp.distance_matrix2(coords, id_list) elif mode == "roads": matrix = tsp.road_matrix(polyline_dict, id_list) elif mode == "airline": matrix = tsp.air_matrix(coords, id_list) else: return "Error" #Choose our algorithm init_function = lambda: tsp.init_random_tour(id_list) #returns shuffled objective_function = lambda tour: -tsp.tour_length(matrix, tour ) #note negation move_dict = { "swapped_cities": tsp.swapped_cities, "reversed_sections": tsp.reversed_sections } if algorithm == "hillclimb": result = tsp.hillclimb(init_function, move_dict[move_operator], objective_function, cycles) num_evaluations, best_score, best, animation_steps, current_score = result elif algorithm == "hill_restart": result = tsp.hillclimb_and_restart(init_function, move_dict[move_operator], objective_function, cycles) num_evaluations, best_score, best, animation_steps, current_score = result elif algorithm == "nearest": result = tsp.greedy(matrix, start_city) num_evaluations, best_score, best = result elif algorithm == "annealing": result = tsp.anneal(init_function, move_dict[move_operator], objective_function, cycles, start_temp, alpha) num_evaluations, best_score, best, animation_steps, current_score = result else: print "error" return "error" tour_coords = tsp.drawtour_on_map2(coord_dict, best) #coordinates for each step for i in range(len(animation_steps)): animation_coords.append( tsp.drawtour_on_map2(coord_dict, animation_steps[i])) tour_cities = convert_tour_to_city(best) if mode == "roads" and not build_dict_thread.isAlive(): poly_list, data = poly_line_tour2(best) poly_animation_steps = polyline_animation_steps2(animation_steps) else: poly_list = [] poly_animation_steps = [] results = { "iterations": num_evaluations, "best_score": best_score, "route": best, "tour_coords": tour_coords, "tour_cities": tour_cities, "animation_coords": animation_coords, "current_score": current_score, "poly_list": poly_list, "poly_animation_steps": poly_animation_steps } data = json.dumps(results) return data