def test_should_the_solution_change_if_the_probability_is_one(self): operator = SimpleRandomMutation(1.0) solution = FloatSolution([-5, -5, -5], [5, 5, 5], 1) solution.variables = [1.0, 2.0, 3.0] mutated_solution = operator.execute(solution) self.assertNotEqual([1.0, 2.0, 3.0], mutated_solution.variables)
def test_should_the_solution_remain_unchanged_if_the_probability_is_zero( self): operator = SimpleRandomMutation(0.0) solution = FloatSolution(3, 1, [-5, -5, -5], [5, 5, 5]) solution.variables = [1.0, 2.0, 3.0] mutated_solution = operator.execute(solution) self.assertEqual([1.0, 2.0, 3.0], mutated_solution.variables)
def test_should_the_solution_change_between_max_and_min_value(self): operator = SimpleRandomMutation(1.0) solution = FloatSolution([-1, 12, -3, -5], [1, 17, 3, -2], 1) solution.variables = [-7.0, 3.0, 12.0, 13.4] mutated_solution = operator.execute(solution) for i in range(solution.number_of_variables): self.assertGreaterEqual(mutated_solution.variables[i], solution.lower_bound[i]) self.assertLessEqual(mutated_solution.variables[i], solution.upper_bound[i])
def test_should_constructor_raise_an_exception_if_the_probability_is_lower_than_zero( self): with self.assertRaises(Exception): SimpleRandomMutation(-12)
def test_should_constructor_create_a_valid_operator(self): operator = SimpleRandomMutation(0.5) self.assertEqual(0.5, operator.probability)
def test_should_constructor_create_a_non_null_object(self): operator = SimpleRandomMutation(1.0) self.assertIsNotNone(operator)
def test_should_constructor_raises_an_exception_is_probability_is_higher_than_one( self) -> None: with self.assertRaises(Exception): SimpleRandomMutation(1.01)
def test_should_constructor_raises_an_exception_is_probability_is_negative( self) -> None: with self.assertRaises(Exception): SimpleRandomMutation(-1)