Exemple #1
0
    def test_ga_iter(self):
        # parameters
        iterations = 10  # keep this value small
        population_size = 50  # keep this value small
        mutation_probability = 0.8
        selection_size = 5

        for i, fjs_instance in enumerate(fjs_data):
            print(
                f"testing fjs instance {fjs_instance} ({i + 1} of {len(fjs_data)})"
            )
            try:
                data_instance = data.FJSData(fjs_instance)

                # run GA
                solver = Solver(data_instance)
                solver.genetic_algorithm_iter(
                    iterations=iterations,
                    population_size=population_size,
                    mutation_probability=mutation_probability,
                    selection_size=selection_size)
            except Exception as e:
                self.fail(
                    f'Unexpected exception raised while running GA for {fjs_instance}:'
                    + str(e))

            self.assertIsNotNone(solver.solution)
            self.assertIsNotNone(solver.ga_agent)

            # test parameters were set
            self.assertEqual(iterations, solver.ga_agent.iterations)
            self.assertFalse(solver.ga_agent.time_condition)
            self.assertFalse(solver.ga_agent.benchmark)
            self.assertEqual(population_size, solver.ga_agent.population_size)
            self.assertEqual(mutation_probability,
                             solver.ga_agent.mutation_probability)
            self.assertEqual(selection_size, solver.ga_agent.selection_size)

            self.assertEqual(population_size,
                             len(solver.ga_agent.initial_population))
            self.assertEqual(population_size,
                             len(solver.ga_agent.result_population))

            # test that the result solution is better than all the solutions in the initial population
            for initial_sol in solver.ga_agent.initial_population:
                self.assertLessEqual(solver.solution, initial_sol)

            # test that the result population does not have duplicate solutions
            seen = []
            self.assertTrue(not any(
                sol in seen or seen.append(sol)
                for sol in solver.ga_agent.result_population))

            # output results
            output_file = tmp_dir / 'fjs_ga_schedule.xlsx'
            solver.solution.create_schedule_xlsx_file(output_file)
            self.assertTrue(output_file.exists(),
                            "fjs_ga_schedule.xlsx was not produced")
    def test_ts_multiple_solutions_per_process(self):
        iterations = 10
        tabu_list_size = 50
        neighborhood_size = 200
        neighborhood_wait = 0.1
        probability_change_machine = 0.8

        num_processes_list = [1, 2, 3, 4]
        num_solutions_per_process_list = [1, 2, 3, 4, 5]

        for num_processes in num_processes_list:
            for num_solutions_per_process in num_solutions_per_process_list:
                solver = Solver(csv_data)
                solver.tabu_search_iter(
                    iterations,
                    num_solutions_per_process=num_solutions_per_process,
                    num_processes=num_processes,
                    tabu_list_size=tabu_list_size,
                    neighborhood_size=neighborhood_size,
                    neighborhood_wait=neighborhood_wait,
                    probability_change_machine=probability_change_machine)

                self.assertIsNotNone(solver.solution)
                self.assertIsNotNone(solver.ts_agent_list)

                # test parameters were set
                self.assertEqual(len(solver.ts_agent_list), num_processes)
                for ts_agent in solver.ts_agent_list:
                    self.assertEqual(iterations, ts_agent.iterations)
                    self.assertFalse(ts_agent.time_condition)
                    self.assertFalse(ts_agent.benchmark)
                    self.assertEqual(num_solutions_per_process,
                                     ts_agent.num_solutions_to_find)
                    self.assertEqual(tabu_list_size, ts_agent.tabu_list_size)
                    self.assertEqual(neighborhood_size,
                                     ts_agent.neighborhood_size)
                    self.assertEqual(neighborhood_wait,
                                     ts_agent.neighborhood_wait)
                    self.assertEqual(probability_change_machine,
                                     ts_agent.probability_change_machine)

                all_solutions = []
                for ts_agent in solver.ts_agent_list:
                    all_solutions += ts_agent.all_solutions

                self.assertEqual(
                    len(all_solutions),
                    num_processes * num_solutions_per_process,
                    f"Parallel TS should have produced {num_processes * num_solutions_per_process} solutions"
                )
    def test_ts_iter(self):
        try:
            iterations = 50
            num_solutions_per_process = 1
            num_processes = 2
            tabu_list_size = 50
            neighborhood_size = 200
            neighborhood_wait = 0.1
            probability_change_machine = 0.8

            solver = Solver(csv_data)
            solver.tabu_search_iter(
                iterations,
                num_solutions_per_process=num_solutions_per_process,
                num_processes=num_processes,
                tabu_list_size=tabu_list_size,
                neighborhood_size=neighborhood_size,
                neighborhood_wait=neighborhood_wait,
                probability_change_machine=probability_change_machine)
        except Exception as e:
            self.fail('Unexpected exception raised:' + str(e))

        self.assertIsNotNone(solver.solution)
        self.assertIsNotNone(solver.ts_agent_list)

        # test parameters were set
        self.assertEqual(len(solver.ts_agent_list), num_processes)
        for ts_agent in solver.ts_agent_list:
            self.assertEqual(iterations, ts_agent.iterations)
            self.assertFalse(ts_agent.time_condition)
            self.assertFalse(ts_agent.benchmark)
            self.assertEqual(num_solutions_per_process,
                             ts_agent.num_solutions_to_find)
            self.assertEqual(tabu_list_size, ts_agent.tabu_list_size)
            self.assertEqual(neighborhood_size, ts_agent.neighborhood_size)
            self.assertEqual(neighborhood_wait, ts_agent.neighborhood_wait)
            self.assertEqual(probability_change_machine,
                             ts_agent.probability_change_machine)

        # output results
        output_file = tmp_dir / 'ts_test_schedule.xlsx'
        solver.solution.create_schedule_xlsx_file(output_file)
        self.assertTrue(output_file.exists(),
                        "ts_test_schedule.xlsx was not produced")
Exemple #4
0
    def test_ts_iter(self):
        # parameters
        iterations = 50  # keep this value small
        num_processes = 1
        tabu_list_size = 10
        neighborhood_size = 25
        neighborhood_wait = 0.1
        probability_change_machine = 0.8

        for i, fjs_instance in enumerate(fjs_data):
            print(
                f"testing fjs instance {fjs_instance} ({i + 1} of {len(fjs_data)})"
            )
            try:
                data_instance = data.FJSData(fjs_instance)

                solver = Solver(data_instance)
                solver.tabu_search_iter(
                    iterations,
                    num_solutions_per_process=1,
                    num_processes=num_processes,
                    tabu_list_size=tabu_list_size,
                    neighborhood_size=neighborhood_size,
                    neighborhood_wait=neighborhood_wait,
                    probability_change_machine=probability_change_machine)

            except Exception as e:
                self.fail(
                    f'Unexpected exception raised while running TS for {fjs_instance}:'
                    + str(e))

            self.assertIsNotNone(solver.solution,
                                 "TS should have produced a best solution")

            # output results
            output_file = tmp_dir / 'fjs_ts_schedule.xlsx'
            solver.solution.create_schedule_xlsx_file(output_file)
            self.assertTrue(output_file.exists(),
                            "fjs_ts_schedule.xlsx was not produced")
    def test_ga_iter(self):
        iterations = 50
        population = None
        population_size = 100
        mutation_probability = 0.8
        selection_size = 5

        solver = Solver(csv_data)
        solver.genetic_algorithm_iter(iterations=iterations,
                                      population=population,
                                      population_size=population_size,
                                      mutation_probability=mutation_probability,
                                      selection_size=selection_size)

        self.assertIsNotNone(solver.solution)
        self.assertIsNotNone(solver.ga_agent)

        # test parameters were set
        self.assertEqual(iterations, solver.ga_agent.iterations)
        self.assertFalse(solver.ga_agent.time_condition)
        self.assertFalse(solver.ga_agent.benchmark)
        self.assertEqual(population_size, solver.ga_agent.population_size)
        self.assertEqual(mutation_probability, solver.ga_agent.mutation_probability)
        self.assertEqual(selection_size, solver.ga_agent.selection_size)

        self.assertNotEqual(0, len(solver.ga_agent.initial_population))
        self.assertNotEqual(0, len(solver.ga_agent.result_population))
        self.assertEqual(len(solver.ga_agent.initial_population), len(solver.ga_agent.result_population))

        # test that the result solution is better than all the solutions in the initial population
        for initial_sol in solver.ga_agent.initial_population:
            self.assertLessEqual(solver.solution, initial_sol)

        # output results
        output_file = tmp_dir / 'ga_test_schedule.xlsx'
        solver.solution.create_schedule_xlsx_file(output_file)
        self.assertTrue(output_file.exists(), "ga_test_schedule.xlsx was not produced")
def _test_selection(unit_test, selection_method, instance_data):
    iterations = 50
    population = None
    population_size = 100
    mutation_probability = 0.8

    # run GA
    solver = Solver(instance_data)
    solver.genetic_algorithm_iter(iterations=iterations,
                                  population=population,
                                  population_size=population_size,
                                  selection_method_enum=selection_method,
                                  mutation_probability=mutation_probability,
                                  selection_size=2 if selection_method is not GASelectionEnum.TOURNAMENT else 5)

    unit_test.assertIsNotNone(solver.solution)
    unit_test.assertIsNotNone(solver.ga_agent)

    # test parameters were set
    unit_test.assertEqual(iterations, solver.ga_agent.iterations)
    unit_test.assertFalse(solver.ga_agent.time_condition)
    unit_test.assertFalse(solver.ga_agent.benchmark)
    unit_test.assertEqual(population_size, solver.ga_agent.population_size)
    unit_test.assertEqual(mutation_probability, solver.ga_agent.mutation_probability)
    unit_test.assertEqual(selection_method, solver.ga_agent.selection_method)

    unit_test.assertNotEqual(0, len(solver.ga_agent.initial_population))
    unit_test.assertNotEqual(0, len(solver.ga_agent.result_population))
    unit_test.assertEqual(len(solver.ga_agent.initial_population), len(solver.ga_agent.result_population))

    # test that the result solution is better than all the solutions in the initial population
    for initial_sol in solver.ga_agent.initial_population:
        unit_test.assertLessEqual(solver.solution, initial_sol)

    # test that the result population does not have duplicate solutions
    seen = []
    unit_test.assertFalse(any(sol in seen or seen.append(sol) for sol in solver.ga_agent.result_population))
    def test_ts_iter_benchmark(self):
        try:
            iterations = 50
            num_solutions_per_process = 1
            num_processes = 2
            tabu_list_size = 50
            neighborhood_size = 200
            neighborhood_wait = 0.1
            probability_change_machine = 0.8

            solver = Solver(csv_data)
            solver.tabu_search_iter(
                iterations,
                num_solutions_per_process=num_solutions_per_process,
                num_processes=num_processes,
                tabu_list_size=tabu_list_size,
                neighborhood_size=neighborhood_size,
                neighborhood_wait=neighborhood_wait,
                probability_change_machine=probability_change_machine,
                benchmark=True)
        except Exception as e:
            self.fail('Unexpected exception raised:' + str(e))

        self.assertIsNotNone(solver.solution)
        self.assertIsNotNone(solver.ts_agent_list)

        # test parameters were set
        self.assertEqual(len(solver.ts_agent_list), num_processes)
        for ts_agent in solver.ts_agent_list:
            self.assertEqual(iterations, ts_agent.iterations)
            self.assertFalse(ts_agent.time_condition)
            self.assertTrue(ts_agent.benchmark)
            self.assertEqual(num_solutions_per_process,
                             ts_agent.num_solutions_to_find)
            self.assertEqual(tabu_list_size, ts_agent.tabu_list_size)
            self.assertEqual(neighborhood_size, ts_agent.neighborhood_size)
            self.assertEqual(neighborhood_wait, ts_agent.neighborhood_wait)
            self.assertEqual(probability_change_machine,
                             ts_agent.probability_change_machine)

            # test benchmark results were produced
            self.assertNotEqual(0, ts_agent.benchmark_iterations)
            self.assertNotEqual(0, len(ts_agent.neighborhood_size_v_iter))
            self.assertNotEqual(0, len(ts_agent.seed_solution_makespan_v_iter))
            self.assertNotEqual(0, len(ts_agent.tabu_size_v_iter))
            self.assertNotEqual((0, 0), ts_agent.min_makespan_coordinates)

        # output results
        output_file = tmp_dir / 'ts_test_benchmark'
        solver.output_benchmark_results(output_file, auto_open=False)
        self.assertTrue(output_file.exists(),
                        "TS benchmark results were not produced")
    def test_ga_time_benchmark(self):
        runtime = 5  # seconds
        population = None
        population_size = 100
        mutation_probability = 0.8
        selection_size = 5

        solver = Solver(csv_data)
        solver.genetic_algorithm_time(runtime=runtime,
                                      population=population,
                                      population_size=population_size,
                                      mutation_probability=mutation_probability,
                                      selection_size=selection_size,
                                      benchmark=True)

        self.assertIsNotNone(solver.solution)
        self.assertIsNotNone(solver.ga_agent)

        # test parameters were set
        self.assertEqual(runtime, solver.ga_agent.runtime)
        self.assertTrue(solver.ga_agent.time_condition)
        self.assertTrue(solver.ga_agent.benchmark)
        self.assertEqual(population_size, solver.ga_agent.population_size)
        self.assertEqual(mutation_probability, solver.ga_agent.mutation_probability)
        self.assertEqual(selection_size, solver.ga_agent.selection_size)

        self.assertNotEqual(0, len(solver.ga_agent.initial_population))
        self.assertNotEqual(0, len(solver.ga_agent.result_population))
        self.assertEqual(len(solver.ga_agent.initial_population), len(solver.ga_agent.result_population))

        # test that the result solution is better than all the solutions in the initial population
        for initial_sol in solver.ga_agent.initial_population:
            self.assertLessEqual(solver.solution, initial_sol)

        # test benchmark results were produced
        self.assertNotEqual(0, len(solver.ga_agent.best_solution_makespan_v_iter))
        self.assertNotEqual(0, len(solver.ga_agent.avg_population_makespan_v_iter))
        self.assertNotEqual(0, len(solver.ga_agent.min_makespan_coordinates))

        # output results
        output_file = tmp_dir / 'ga_test_benchmark'
        solver.output_benchmark_results(output_file, auto_open=False)
        self.assertTrue(output_file.exists(), "GA benchmark results were not produced")