from jmetal.problem.singleobjective.unconstrained import Rastrigin from jmetal.util.observer import PrintObjectivesObserver from jmetal.util.solution_list import print_function_values_to_file, print_variables_to_file from jmetal.util.termination_criterion import StoppingByEvaluations if __name__ == '__main__': problem = Rastrigin(10) max_evaluations = 100000 algorithm = LocalSearch( problem=problem, mutation=PolynomialMutation(1.0 / problem.number_of_variables, 20.0), termination_criterion=StoppingByEvaluations(max=max_evaluations) ) objectives_observer = PrintObjectivesObserver(frequency=1000) 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)) print('Fitness: ' + str(result.objectives[0])) print('Computing time: ' + str(algorithm.total_computing_time))
if __name__ == '__main__': problem = Rastrigin(10) max_evaluations = 50000 algorithm = NSGAII( problem=problem, population_size=100, offspring_population_size=100, mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20.0), crossover=SBXCrossover(probability=0.9, distribution_index=20.0), termination_criterion=StoppingByEvaluations(max=max_evaluations), dominance_comparator=DominanceComparator()) algorithm.observable.register(observer=PrintObjectivesObserver(1000)) algorithm.run() front = algorithm.get_result() # Save results to file print_function_values_to_file( front, 'FUN.' + algorithm.get_name() + "-" + problem.get_name()) print_variables_to_file( front, 'VAR.' + algorithm.get_name() + "-" + problem.get_name()) print('Algorithm (continuous problem): ' + algorithm.get_name()) print('Problem: ' + problem.get_name()) print('Computing time: ' + str(algorithm.total_computing_time))
from jmetal.algorithm.singleobjective.genetic_algorithm import GeneticAlgorithm from jmetal.operator import BitFlipMutation, SPXCrossover, BinaryTournamentSelection from jmetal.problem import OneMax from jmetal.util.observer import PrintObjectivesObserver from jmetal.util.termination_criterion import StoppingByEvaluations if __name__ == '__main__': problem = OneMax(number_of_bits=1024) algorithm = GeneticAlgorithm( problem=problem, population_size=100, offspring_population_size=100, mutation=BitFlipMutation(1.0 / problem.number_of_bits), crossover=SPXCrossover(1.0), selection=BinaryTournamentSelection(), termination_criterion=StoppingByEvaluations(max=20000)) algorithm.observable.register(observer=PrintObjectivesObserver( frequency=1000)) algorithm.run() result = algorithm.get_result() print('Algorithm: {}'.format(algorithm.get_name())) print('Problem: {}'.format(problem.get_name())) print('Solution: ' + result.get_binary_string()) print('Fitness: ' + str(result.objectives[0])) print('Computing time: {}'.format(algorithm.total_computing_time))