def test_should_the_solution_remain_unchanged_if_the_probability_is_zero(self): operator = BitFlip(0.0) solution = BinarySolution(number_of_variables=1, number_of_objectives=1) solution.variables[0] = [True, True, False, False, True, False] mutated_solution = operator.execute(solution) self.assertEqual([True, True, False, False, True, False], mutated_solution.variables[0])
def test_should_the_solution_change_all_the_bits_if_the_probability_is_one(self): operator = BitFlip(1.0) solution = BinarySolution(number_of_variables=2, number_of_objectives=1) solution.variables[0] = [True, True, False, False, True, False] solution.variables[1] = [False, True, True, False, False, True] mutated_solution = operator.execute(solution) self.assertEqual([False, False, True, True, False, True], mutated_solution.variables[0]) self.assertEqual([True, False, False, True, True, False], mutated_solution.variables[1])
def main() -> None: bits = 512 problem = OneMax(bits) algorithm = NonElitistEvolutionStrategy[BinarySolution, BinarySolution]\ (problem, mu=1, lambdA=10, max_evaluations= 25000, mutation=BitFlip(1.0/bits)) algorithm.run() result = algorithm.get_result() print("Algorithm: " + algorithm.get_name()) print("Problem: " + problem.get_name()) print("Solution: " + str(result.variables[0])) print("Fitness: " + str(result.objectives[0])) print("Computing time: " + str(algorithm.total_computing_time))
def main() -> None: bits = 256 problem = OneMax(bits) algorithm = GenerationalGeneticAlgorithm[BinarySolution, BinarySolution]( problem, population_size=100, max_evaluations=150000, mutation=BitFlip(1.0 / bits), crossover=SinglePoint(0.9), selection=BinaryTournamentSelection()) algorithm.run() result = algorithm.get_result() logger.info("Algorithm (binary problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name()) logger.info("Solution: " + str(result.variables[0])) logger.info("Fitness: " + str(result.objectives[0]))
def main() -> None: bits = 256 problem = OneMax(bits) algorithm = GenerationalGeneticAlgorithm[BinarySolution, BinarySolution]( problem=problem, population_size=100, max_evaluations=150000, mutation=BitFlip(1.0 / bits), crossover=SP(0.9), selection=BinaryTournamentSelection()) algorithm.run() result = algorithm.get_result() print("Algorithm: " + algorithm.get_name()) print("Problem: " + problem.get_name()) print("Solution: " + str(result.variables)) print("Fitness: " + str(result.objectives[0])) print("Computing time: " + str(algorithm.total_computing_time))
def test_should_constructor_raise_an_exception_if_the_probability_is_lower_than_zero(self): with self.assertRaises(Exception): BitFlip(-12)
def test_should_constructor_raise_an_exception_if_the_probability_is_greater_than_one(self): with self.assertRaises(Exception): BitFlip(2)
def test_should_constructor_create_a_valid_operator(self): operator = BitFlip(0.5) self.assertEqual(0.5, operator.probability)
def test_should_constructor_create_a_non_null_object(self): solution = BitFlip(1.0) self.assertIsNotNone(solution)