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:
    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:
                print("Stopping condition reached!")

            return reached

    variables = 10
    problem = Sphere(variables)

    algorithm = GGA2(
        problem=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()

    print("Algorithm (stop for timeout): " + 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))
Ejemplo n.º 3
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))
Ejemplo n.º 4
0
    def test_should_create_solution_create_a_valid_float_solution(self):
        problem = Sphere(3)
        solution = problem.create_solution()

        self.assertEqual(3, solution.number_of_variables)
        self.assertEqual(3, len(solution.variables))
        self.assertEqual(1, solution.number_of_objectives)
        self.assertEqual(1, len(solution.objectives))
        self.assertEqual(0, problem.number_of_constraints)

        self.assertEqual([-5.12, -5.12, -5.12], problem.lower_bound)
        self.assertEqual([5.12, 5.12, 5.12], problem.upper_bound)

        self.assertTrue(solution.variables[0] >= -5.12)
        self.assertTrue(solution.variables[0] <= 5.12)
Ejemplo n.º 5
0
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]))
Ejemplo n.º 6
0
    def test_should_constructor_create_a_valid_problem_with_5_variables(self):
        problem = Sphere(5)
        self.assertEqual(5, problem.number_of_variables)
        self.assertEqual(1, problem.number_of_objectives)
        self.assertEqual(0, problem.number_of_constraints)

        self.assertEqual([-5.12, -5.12, -5.12, -5.12, -5.12],
                         problem.lower_bound)
        self.assertEqual([5.12, 5.12, 5.12, 5.12, 5.12], problem.upper_bound)
Ejemplo n.º 7
0
    def test_should_constructor_create_a_valid_problem_with_default_settings(
            self):
        problem = Sphere()
        self.assertEqual(10, problem.number_of_variables)
        self.assertEqual(1, problem.number_of_objectives)
        self.assertEqual(0, problem.number_of_constraints)

        self.assertEqual([-5.12 for _ in range(10)], problem.lower_bound)
        self.assertEqual([5.12 for _ in range(10)], problem.upper_bound)
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.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 = 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=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))
Ejemplo n.º 12
0
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]))
Ejemplo n.º 13
0
 def test_should_constructor_create_a_non_null_object(self):
     problem = Sphere(3)
     self.assertIsNotNone(problem)
Ejemplo n.º 14
0
 def test_should_get_name_return_the_right_name(self):
     problem = Sphere()
     self.assertEqual("Sphere", problem.get_name())
Ejemplo n.º 15
0
from jmetal.algorithm.singleobjective.local_search import LocalSearch
from jmetal.operator import PolynomialMutation
from jmetal.problem.singleobjective.unconstrained import Sphere
from jmetal.util.observer import PrintObjectivesObserver
from jmetal.util.solutions_utils import print_function_values_to_file, print_variables_to_file
from jmetal.util.termination_criterion import StoppingByEvaluations

if __name__ == '__main__':
    problem = Sphere(10)

    max_evaluations = 1000000

    algorithm = LocalSearch(
        problem=problem,
        mutation=PolynomialMutation(1.0 / problem.number_of_variables, 20.0),
        termination_criterion=StoppingByEvaluations(max=max_evaluations)
    )

    objectives_observer = PrintObjectivesObserver(frequency=10000)
    algorithm.observable.register(observer=objectives_observer)

    algorithm.run()
    result = algorithm.get_result()

    # Save results to file
    print_function_values_to_file(result, 'FUN.'+ algorithm.get_name() + "." + problem.get_name())
    print_variables_to_file(result, 'VAR.' + algorithm.get_name() + "." + problem.get_name())

    print('Algorithm: ' + algorithm.get_name())
    print('Problem: ' + problem.get_name())
    print('Solution: ' + str(result.variables))
Ejemplo n.º 16
0
from jmetal.algorithm.singleobjective.local_search import LocalSearch
from jmetal.operator import PolynomialMutation
from jmetal.problem.singleobjective.unconstrained import Sphere
from jmetal.util.solution import print_function_values_to_file, print_variables_to_file
from jmetal.util.termination_criterion import StoppingByEvaluations

if __name__ == '__main__':
    problem = Sphere(10)

    max_evaluations = 1000000

    algorithm = LocalSearch(problem=problem,
                            mutation=PolynomialMutation(
                                1.0 / problem.number_of_variables, 20.0),
                            termination_criterion=StoppingByEvaluations(
                                max_evaluations=max_evaluations))

    algorithm.run()
    result = algorithm.get_result()

    # Save results to file
    print_function_values_to_file(
        result, 'FUN.' + algorithm.get_name() + "." + problem.get_name())
    print_variables_to_file(
        result, 'VAR.' + algorithm.get_name() + "." + problem.get_name())

    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))