def test_should_the_solution_remain_unchanged_if_the_probability_is_zero(self): operator = Polynomial(0.0) solution = FloatSolution(2, 1, 0, [-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__if_the_probability_is_one(self): operator = Polynomial(1.0) solution = FloatSolution(2, 1, 0, [-5, -5, -5], [5, 5, 5]) solution.variables = [1.0, 2.0, 3.0] FloatSolution.lower_bound = [-5, -5, -5] FloatSolution.upper_bound = [5, 5, 5] mutated_solution = operator.execute(solution) self.assertNotEqual([1.0, 2.0, 3.0], mutated_solution.variables)
def main() -> None: class NSGA2b(NSGAII[S, R]): def is_stopping_condition_reached(self): # Re-define the stopping condition reached = [False, True][self.get_current_computing_time() > 4] if reached: logger.info("Stopping condition reached!") return reached problem = Fonseca() algorithm = NSGA2b[FloatSolution, List[FloatSolution]]( problem, population_size=100, max_evaluations=25000, mutation=Polynomial(1.0 / problem.number_of_variables, distribution_index=20), crossover=SBX(1.0, distribution_index=20), selection=BinaryTournament(RankingAndCrowdingDistanceComparator())) algorithm.run() result = algorithm.get_result() SolutionListOutput[FloatSolution].print_function_values_to_file( "FUN." + problem.get_name(), result) logger.info("Algorithm (continuous problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name())
def main() -> None: problem = ZDT1() algorithm = NSGAII[FloatSolution, List[FloatSolution]]( problem=problem, population_size=100, max_evaluations=25000, mutation=Polynomial(1.0 / problem.number_of_variables, distribution_index=20), crossover=SBX(1.0, distribution_index=20), #selection=BinaryTournamentSelection(RankingAndCrowdingDistanceComparator())) selection=BinaryTournament2Selection([ SolutionAttributeComparator("dominance_ranking"), SolutionAttributeComparator("crowding_distance", lowest_is_best=False) ])) algorithm.run() result = algorithm.get_result() SolutionListOutput[FloatSolution].print_function_values_to_file( "FUN." + problem.get_name(), result) logger.info("Algorithm (continuous problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name()) logger.info("Computing time: " + str(algorithm.total_computing_time))
def main() -> None: class GGA2(GenerationalGeneticAlgorithm[FloatSolution, FloatSolution]): def is_stopping_condition_reached(self): # Re-define the stopping condition reached = [False, True][self.get_current_computing_time() > 4] if reached: logger.info("Stopping condition reached!") return reached variables = 10 problem = Sphere(variables) algorithm = GGA2(problem, population_size=100, max_evaluations=0, mutation=Polynomial(1.0 / variables, distribution_index=20), crossover=SBX(1.0, distribution_index=20), selection=BinaryTournamentSelection()) algorithm.run() result = algorithm.get_result() logger.info("Algorithm (stop for timeout): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name()) logger.info("Solution: " + str(result.variables)) logger.info("Fitness: " + str(result.objectives[0]))
def main() -> None: variables = 10 problem = Sphere(variables) algorithm = NonElitistEvolutionStrategy[FloatSolution, FloatSolution]\ (problem, mu=10, lambdA=10, max_evaluations= 50000, mutation=Polynomial(1.0/variables)) 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 main() -> None: problem = Kursawe() algorithm = NSGAII[FloatSolution, List[FloatSolution]]( problem=problem, population_size=100, max_evaluations=25000, mutation=Polynomial(1.0/problem.number_of_variables, distribution_index=20), crossover=SBX(1.0, distribution_index=20), selection = BinaryTournament(RankingAndCrowdingDistanceComparator())) algorithm.run() result = algorithm.get_result() SolutionListOutput[FloatSolution].print_function_values_to_file("FUN."+problem.get_name(), result) logger.info("Algorithm (continuous problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name())
def main() -> None: problem = Kursawe() algorithm = SMPSO(problem=problem, swarm_size=100, max_evaluations=25000, mutation=Polynomial(1.0 / problem.number_of_variables, distribution_index=20), leaders=CrowdingDistanceArchive(100)) algorithm.run() result = algorithm.get_result() SolutionListOutput[FloatSolution].print_function_values_to_file( "FUN." + problem.get_name(), result) logger.info("Algorithm (continuous problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name())
def main() -> None: variables = 10 problem = Sphere(variables) algorithm = GenerationalGeneticAlgorithm[FloatSolution, FloatSolution]( problem, population_size=100, max_evaluations=25000, mutation=Polynomial(1.0/variables, distribution_index=20), crossover=SBX(1.0, distribution_index=20), selection=BinaryTournamentSelection()) algorithm.run() result = algorithm.get_result() logger.info("Algorithm (continuous problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name()) logger.info("Solution: " + str(result.variables)) logger.info("Fitness: " + str(result.objectives[0]))
def main() -> None: problem = ZDT1() algorithm = NSGAII[FloatSolution, List[FloatSolution]]( problem, population_size=100, max_evaluations=25000, mutation=Polynomial(1.0/problem.number_of_variables, distribution_index=20), crossover=SBX(1.0, distribution_index=20), #selection=BinaryTournament(RankingAndCrowdingDistanceComparator())) selection = BinaryTournament2([SolutionAttributeComparator("dominance_ranking"), SolutionAttributeComparator("crowding_distance", lowest_is_best=False)])) observer = AlgorithmObserver(animation_speed=1*10e-8) algorithm.observable.register(observer=observer) algorithm.run() logger.info("Algorithm (continuous problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name())
def main() -> None: variables = 10 problem = Sphere(variables) algorithm = ElitistEvolutionStrategy[FloatSolution, FloatSolution]( problem=problem, mu=10, lambd_a=10, max_evaluations=50000, mutation=Polynomial(probability=1.0 / variables)) algorithm.start() print("Algorithm (running as a thread): " + algorithm.get_name()) print("Problem: " + problem.get_name()) algorithm.join() result = algorithm.get_result() print("Solution: " + str(result.variables)) print("Fitness: " + str(result.objectives[0])) print("Computing time: " + str(algorithm.total_computing_time))
def main() -> None: variables = 10 problem = Sphere(variables) algorithm = GenerationalGeneticAlgorithm[FloatSolution, FloatSolution]( problem=problem, population_size=100, max_evaluations=25000, mutation=Polynomial(probability=1.0 / variables, distribution_index=20), crossover=SBX(probability=1.0, distribution_index=20), 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 main() -> None: variables = 10 problem = Sphere(variables) algorithm = GenerationalGeneticAlgorithm[FloatSolution, FloatSolution]( problem, population_size=100, max_evaluations=25000, mutation=Polynomial(1.0 / variables, distribution_index=20), crossover=SBX(1.0, distribution_index=20), selection=BinaryTournament()) observer = BasicAlgorithmConsumer(2000) algorithm.observable.register(observer=observer) algorithm.start() algorithm.join() result = algorithm.get_result() logger.info("Algorithm: " + algorithm.get_name()) logger.info("Problem: " + problem.get_name()) logger.info("Solution: " + str(result.variables)) logger.info("Fitness: " + str(result.objectives[0]))
def main() -> None: problem = ZDT1() algorithm = NSGAII[FloatSolution, List[FloatSolution]]( problem, population_size = 100, max_evaluations = 25000, mutation = Polynomial(1.0/problem.number_of_variables, distribution_index=20), crossover = SBX(1.0, distribution_index=20), selection = BinaryTournament(RankingAndCrowdingDistanceComparator())) observer = BasicAlgorithmConsumer(1000) algorithm.observable.register(observer=observer) algorithm.run() result = algorithm.get_result() SolutionListOutput[FloatSolution].print_function_values_to_file("FUN."+problem.get_name(), result) logger.info("Algorithm (continuous problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name()) logger.info("Computing time: " + str(algorithm.total_computing_time))
def main() -> None: problem = Kursawe() algorithm = NSGAII[FloatSolution, List[FloatSolution]]( problem, population_size=100, max_evaluations=25000, mutation=Polynomial(1.0 / problem.number_of_variables, distribution_index=20), crossover=SBX(1.0, distribution_index=20), selection=BinaryTournamentSelection()) algorithm.run() result = algorithm.get_result() SolutionListOutput[FloatSolution].plot_scatter_to_file(result, file_name="FUN." + problem.get_name(), output_format='eps', dpi=200) SolutionListOutput[FloatSolution].plot_scatter_to_screen(result) logger.info("Algorithm (continuous problem): " + algorithm.get_name()) logger.info("Problem: " + problem.get_name())
def test_should_constructor_create_a_valid_object(self): problem = self.__DummyFloatProblem() algorithm = SMPSO(problem=problem, swarm_size=100, max_evaluations=200, mutation=Polynomial(probability=1.0 / problem.number_of_variables), leaders=BoundedArchive[FloatSolution](100)) self.assertEqual(1.5, algorithm.c1_min) self.assertEqual(2.5, algorithm.c1_max) self.assertEqual(1.5, algorithm.c2_min) self.assertEqual(2.5, algorithm.c2_max) self.assertEqual(0.1, algorithm.min_weight) self.assertEqual(0.1, algorithm.max_weight) self.assertEqual(-1.0, algorithm.change_velocity1) self.assertEqual(-1.0, algorithm.change_velocity2) self.assertEqual(200, algorithm.max_evaluations) self.assertEqual((100, 2), algorithm.speed.shape) numpy.testing.assert_array_almost_equal(numpy.array([2.0, 2.0]), algorithm.delta_max) numpy.testing.assert_array_almost_equal(algorithm.delta_max * -1.0, algorithm.delta_min)
def test_should_constructor_create_a_non_null_object(self): mutation = Polynomial(1.0) self.assertIsNotNone(mutation)
def test_should_constructor_create_a_valid_operator(self): operator = Polynomial(0.5, 20) self.assertEqual(0.5, operator.probability) self.assertEqual(20, operator.distribution_index)
def test_should_constructor_raise_an_exception_if_the_probability_is_greater_than_one(self): with self.assertRaises(Exception): Polynomial(2)
def test_should_constructor_raise_an_exception_if_the_probability_is_lower_than_zero(self): with self.assertRaises(Exception): Polynomial(-12)