def test_tsp_classic(): num_cities = 10 num_iterations = 10 cities = tsp.random_cities(num_cities) adjacency_matrix = tsp.generate_graph(cities) solution, penalty = solve_tsp_classic(adjacency_matrix, num_cities, num_iterations) assert sorted(solution) == [0] + list(range(num_cities)) assert penalty <= num_cities * sqrt(2)
def main_tsp(num_cities=100, num_iterations=100, use_islands=False, use_multiprocess=False, verbose=True): ''' This functions creates a random travelling salesman problem and solves it using classical or island model genetic algorithm. ''' cities = tsp.random_cities(num_cities) adjacency_matrix = tsp.generate_graph(cities) if use_islands: solution, penalty = solve_tsp_islands( adjacency_matrix, num_iterations, use_multiprocess=use_multiprocess, verbose=verbose) else: solution, penalty = solve_tsp_classic(adjacency_matrix) print("solution = {0}".format(solution)) print("penalty = {0}".format(penalty))
def create_random_twoopt(): adjacency_matrix = generate_graph(random_cities(10)) two_opt = get_twoopt_mutation(adjacency_matrix, 1.0) return two_opt, adjacency_matrix
def __init__(self): cities = [(0.0, 0.0), (1.0, 0.0)] self.adjacency_matrix = generate_graph(cities)