def test_should_the_operator_work_with_two_solutions_with_two_variables(self, random_call): operator = CXCrossover(1.0) solution1 = PermutationSolution(number_of_variables=2, number_of_objectives=1) solution1.variables[0] = [1, 2, 3, 4, 7] solution1.variables[1] = [2, 6, 4, 5, 3] solution2 = PermutationSolution(number_of_variables=2, number_of_objectives=1) solution2.variables[0] = [2, 3, 4, 1, 9] solution2.variables[1] = [5, 3, 2, 4, 6] random_call.return_value = 0 offspring = operator.execute([solution1, solution2]) self.assertEqual([1, 2, 3, 4, 9], offspring[0].variables[0]) self.assertEqual([2, 3, 4, 5, 6], offspring[0].variables[1]) self.assertEqual([1, 2, 3, 4, 7], offspring[1].variables[0]) self.assertEqual([2, 6, 4, 5, 3], offspring[1].variables[1])
def test_should_the_solution_remain_unchanged_if_the_probability_is_zero( self, random_call): operator = CXCrossover(1.0) solution1 = PermutationSolution(number_of_variables=2, number_of_objectives=1) solution1.variables[0] = [1, 2, 3, 4, 7] solution1.variables[1] = [2, 6, 4, 5, 3] solution2 = PermutationSolution(number_of_variables=2, number_of_objectives=1) solution2.variables[0] = [2, 3, 4, 1, 9] solution2.variables[1] = [5, 3, 2, 4, 6] random_call.return_value = 0 offspring = operator.execute([solution1, solution2]) self.assertEqual([1, 2, 3, 4, 9], offspring[0].variables[0]) self.assertEqual([2, 3, 4, 5, 6], offspring[0].variables[1]) self.assertEqual([1, 2, 3, 4, 7], offspring[1].variables[0]) self.assertEqual([2, 6, 4, 5, 3], offspring[1].variables[1])
def construct(self, hyperparameters: Mapping, scenario: Mapping, warm_startup_info: Mapping) -> None: problem_init_params = HashableDict( scenario['problem_initialization_parameters']) problem = self._get_problem(scenario['Problem'], problem_init_params) self.load_initial_solutions(warm_startup_info, problem) from jmetal.operator.mutation import PermutationSwapMutation termination_criterion_cls = self._get_class_from_module( name=scenario["Budget"]["Type"], module=termination) termination_criterion = termination_criterion_cls( scenario["Budget"]["Amount"]) mh_name = hyperparameters["low level heuristic"].split(".")[1] if mh_name == "GeneticAlgorithm": from jmetal.algorithm.singleobjective.genetic_algorithm import ( GeneticAlgorithm) from jmetal.operator.crossover import CXCrossover from jmetal.operator.selection import BinaryTournamentSelection self._llh_algorithm = GeneticAlgorithm( problem=problem, population_size=100, offspring_population_size=100, mutation=PermutationSwapMutation(0.5), crossover=CXCrossover(0.5), selection=BinaryTournamentSelection(), termination_criterion=termination_criterion, population_generator=self._solution_generator) elif mh_name == "SimulatedAnnealing": from jmetal.algorithm.singleobjective.simulated_annealing import ( SimulatedAnnealing) self._llh_algorithm = SimulatedAnnealing( problem=problem, mutation=PermutationSwapMutation(0.5), termination_criterion=termination_criterion, solution_generator=self._solution_generator) elif mh_name == "EvolutionStrategy": from jmetal.algorithm.singleobjective.evolution_strategy import ( EvolutionStrategy) self._llh_algorithm = EvolutionStrategy( problem=problem, mu=500, lambda_=500, elitist=False, mutation=PermutationSwapMutation(0.5), termination_criterion=termination_criterion, population_generator=self._solution_generator) else: self.logger.error(f"Wrong meta-heuristic name: {mh_name}")
def test_should_constructor_raise_an_exception_if_the_probability_is_lower_than_zero( self): with self.assertRaises(Exception): CXCrossover(-12)
def test_should_constructor_raise_an_exception_if_the_probability_is_greater_than_one( self): with self.assertRaises(Exception): CXCrossover(2)
def test_should_constructor_create_a_valid_operator(self): operator = CXCrossover(0.5) self.assertEqual(0.5, operator.probability)
def test_should_constructor_create_a_non_null_object(self): solution = CXCrossover(1.0) self.assertIsNotNone(solution)
def test_should_constructor_raises_an_exception_is_probability_is_higher_than_one( self) -> None: with self.assertRaises(Exception): CXCrossover(1.01)
def test_should_constructor_raises_an_exception_is_probability_is_negative( self) -> None: with self.assertRaises(Exception): CXCrossover(-1)