def test_mutation(
        self,
        mock_random_sample,
        mocker,
        mutation_strategy,
        side_effect,
        expected_mutated_population,
    ):

        mocked_choice = mocker.Mock(side_effect=side_effect)
        mocker.patch.object(np.random, "choice", mocked_choice)

        pop_size = 5

        tsp_solver = TravellingSalesmanProblemSolver(
            G, mutation_strategy=mutation_strategy, pop_size=pop_size, random_state=42,
        )

        population = np.array(
            [
                [9, 2, 6, 1, 8, 3, 10, 5, 4, 7],
                [1, 2, 9, 6, 4, 5, 8, 10, 7, 3],
                [10, 3, 1, 7, 9, 6, 4, 8, 2, 5],
                [2, 8, 7, 3, 9, 1, 4, 5, 6, 10],
                [2, 6, 5, 9, 1, 8, 7, 4, 3, 10],
            ]
        )

        mutated_population = tsp_solver.mutate_population(
            population, 2 * tsp_solver.n_genes
        )

        assert np.allclose(mutated_population, expected_mutated_population, rtol=1e-5)
    def test_get_worst_gene(self):

        pop_size = 5

        tsp_solver = TravellingSalesmanProblemSolver(G, random_state=42)

        route = np.arange(1, len(G.nodes))

        np.random.shuffle(route)

        assert tsp_solver.find_worst_gene(route) == 7
    def test_numba_speedup(self, mocker):

        mocked_fitness_function = mocker.patch(
            "geneal.applications.tsp.travelling_salesman_problem.fitness_function"
        )

        tsp_solver = TravellingSalesmanProblemSolver(
            G, numba_speedup=True, random_state=42
        )

        route = np.array([2, 6, 5, 9, 1, 8, 7, 4, 3, 10])

        _ = tsp_solver.fitness_function(route)

        mocked_fitness_function.assert_called()
    def test_n_genes_overriding(self, n_genes, expected_n_genes):

        tsp_solver = TravellingSalesmanProblemSolver(
            G, n_genes=n_genes, random_state=42
        )

        assert tsp_solver.n_genes == expected_n_genes
    def test_check_input(self):

        tsp_solver = TravellingSalesmanProblemSolver(
            G, n_crossover_points=3, random_state=42
        )

        assert tsp_solver.n_crossover_points == 2
    def test_exception(self):

        with pytest.raises(Exception) as excinfo:

            mutation_strategy = "invalid_strategy"

            tsp_solver = TravellingSalesmanProblemSolver(
                G, mutation_strategy="invalid_strategy", random_state=42,
            )

            tsp_solver.solve()

        assert excinfo.type == InvalidInput
        assert (
            str(excinfo.value) == f"{mutation_strategy} is an invalid mutation strategy"
        )
    def test_worst_gene_random_mutation(self):

        pop_size = 8

        tsp_solver = TravellingSalesmanProblemSolver(
            G,
            pop_size=pop_size,
            random_state=42,
        )

        route = np.array([2, 1, 5, 7, 3, 4, 6, 8])

        expected_mutated_route = np.array([2, 1, 5, 7, 3, 4, 8, 6])

        mutated_route = tsp_solver.worst_gene_random_mutation_helper(route)

        assert np.allclose(mutated_route, expected_mutated_route)
    def test_random_gene_nearest_neighbour(self):

        pop_size = 8

        tsp_solver = TravellingSalesmanProblemSolver(
            G,
            pop_size=pop_size,
            random_state=42,
        )

        route = np.array([2, 1, 5, 7, 3, 4, 6, 8])

        expected_mutated_route = np.array([2, 6, 1, 5, 7, 3, 4, 8])

        mutated_route = tsp_solver.random_gene_nearest_neighbour_helper(route)

        assert np.allclose(mutated_route, expected_mutated_route)
    def test_initialize_population(self, problem_type, expected_result):

        pop_size = 5
        n_genes = len(G.nodes)

        tsp_solver = TravellingSalesmanProblemSolver(
            G, pop_size=pop_size, random_state=42,
        )

        population = tsp_solver.initialize_population()

        assert population.shape[0] == pop_size
        assert population.shape[1] == n_genes

        for route in population:
            assert len(set(route)) == n_genes

        assert np.allclose(population, expected_result, rtol=1e-05)
    def test_random_inversion_mutation(self):

        pop_size = 8

        tsp_solver = TravellingSalesmanProblemSolver(
            G,
            pop_size=pop_size,
            random_state=42,
        )

        route = np.array([2, 1, 5, 7, 3, 4, 6, 8])
        mutation_cols = tsp_solver.get_consecutive_mutation_cols(route, 1, 5)

        expected_mutated_route = np.array([2, 8, 6, 7, 3, 4, 5, 1])

        mutated_route = tsp_solver.random_inversion_mutation_helper(
            route, mutation_cols)

        assert np.allclose(mutated_route, expected_mutated_route)
    def test_random_swap_mutation(self):

        pop_size = 8

        tsp_solver = TravellingSalesmanProblemSolver(
            G,
            pop_size=pop_size,
            random_state=42,
        )

        route = np.array([[2, 1, 5, 7, 3, 4, 6, 8]])

        expected_mutated_route = np.array([2, 3, 5, 7, 1, 4, 6, 8])

        mutation_rows = np.array([0])
        mutation_cols = np.array([[1, 4]])

        mutated_route = tsp_solver.random_swap_mutation(
            route, mutation_rows, mutation_cols)

        assert np.allclose(mutated_route, expected_mutated_route)
Example #12
0
    def test_plot_cities(
        self,
        mocker,
        mock_logging,
        mock_matplotlib,
        mock_plotly_figure_show,
        cities,
        graph_file,
        lat,
        lon,
        name,
    ):

        mocked_add_trace = mocker.patch(
            "geneal.applications.tsp.helpers._plot_cities.add_trace")

        with open(f"tests/applications/fixtures/{graph_file}.pickle",
                  "rb") as f:
            G = pickle.load(f)

        tsp_solver = TravellingSalesmanProblemSolver(
            G,
            max_gen=2,
            random_state=42,
        )

        tsp_solver.solve()

        plot_cities(
            cities,
            tsp_solver,
            lon=lon,
            lat=lat,
            name=name,
        )

        mocked_add_trace.assert_called()

        assert mocked_add_trace.call_count == len(G.nodes)
    def test_create_offspring(
        self, crossover_pt, expected_first_offspring, expected_second_offspring
    ):

        pop_size = 5

        tsp_solver = TravellingSalesmanProblemSolver(
            G, pop_size=pop_size, random_state=42,
        )

        first_parent = np.array([1, 3, 4, 7, 2, 5, 8, 6])
        sec_parent = np.array([5, 2, 3, 8, 1, 6, 4, 7])

        first_offspring = tsp_solver.create_offspring(
            first_parent, sec_parent, crossover_pt, "first"
        )

        second_offspring = tsp_solver.create_offspring(
            sec_parent, first_parent, crossover_pt, "second"
        )

        assert np.allclose(first_offspring, expected_first_offspring, rtol=1e-5)
        assert np.allclose(second_offspring, expected_second_offspring, rtol=1e-5)