def test_cartesian_matrix(): coords=[(0,0),(0,1),(1,0),(1,1)] matrix=tsp.cartesian_matrix(coords) assert 16 == len(matrix) assert matrix[(0,0)] == 0 assert matrix[(1,1)] == 0 assert matrix[(2,2)] == 0 assert matrix[(3,3)] == 0 assert matrix[(0,1)] == 1 assert matrix[(1,0)] == 1 assert matrix[(0,2)] == 1 assert matrix[(2,0)] == 1 assert matrix[(0,3)] == sqrt(2) assert matrix[(3,0)] == sqrt(2) assert matrix[(1,2)] == sqrt(2) assert matrix[(2,1)] == sqrt(2) assert matrix[(1,3)] == 1 assert matrix[(3,1)] == 1 assert matrix[(2,3)] == 1 assert matrix[(3,2)] == 1
def test_tour_length(): coords=[(0,0),(0,1),(1,0),(1,1)] matrix=tsp.cartesian_matrix(coords) assert 2 == tsp.tour_length(matrix,[0,1]) assert 2 == tsp.tour_length(matrix,[0,2]) assert (1+sqrt(2)+1) == tsp.tour_length(matrix,[0,1,2]) assert (1+sqrt(2)+1+sqrt(2)) == tsp.tour_length(matrix,[0,1,2,3])
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)')