algorithm.run() front = algorithm.get_result() # Plot front plot_front = Plot( title="Pareto front approximation. Problem: " + problem.get_name(), reference_front=problem.reference_front, axis_labels=problem.obj_labels, ) plot_front.plot(front, label=algorithm.label, filename=algorithm.get_name()) # Plot interactive front plot_front = InteractivePlot( title="Pareto front approximation. Problem: " + problem.get_name(), reference_front=problem.reference_front, axis_labels=problem.obj_labels, ) plot_front.plot(front, label=algorithm.label, filename=algorithm.get_name()) # Save results to file print_function_values_to_file(front, "FUN." + algorithm.label) print_variables_to_file(front, "VAR." + algorithm.label) print(f"Algorithm: {algorithm.get_name()}") print(f"Problem: {problem.get_name()}") print(f"Computing time: {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_evaluations=max_evaluations), dominance_comparator=DominanceComparator(), ) 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))
crossover=SBXCrossover(probability=1.0, distribution_index=20), termination_criterion=StoppingEvaluator # termination_criterion = StoppingByQualityIndicator(quality_indicator=HyperVolume, expected_value=1, # degree=0.9) # selection = BinaryTournamentSelection() ) """==========================调用算法模板进行种群进化=========================""" # progress_bar = ProgressBarObserver(max=max_evaluations) # algorithm.observable.register(progress_bar) algorithm.run() front = algorithm.get_result() """==================================输出结果==============================""" # Save results to file fun_name = 'FUN.' + str(round_index) + '_' + algorithm.label print_function_values_to_file(front, os.path.join(target_dir, fun_name)) var_name = 'VAR.' + str(round_index) + '_' + algorithm.label print_variables_to_file(front, os.path.join(target_dir, var_name)) print(f'Algorithm: ${algorithm.get_name()}') print(f'Problem: ${problem.get_name()}') print(f'Computing time: ${algorithm.total_computing_time}') # print(search_round,round_index,total_search_round) search_round = int(StoppingEvaluator.evaluations / population) total_round = total_round - search_round print("real round: ", search_round, "idx: ", round_index, "left: ", total_round) round_index = round_index + 1
from jmetal.algorithm.singleobjective.simulated_annealing import SimulatedAnnealing from jmetal.operator import PolynomialMutation from jmetal.problem.singleobjective.unconstrained import Rastrigin 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 = Rastrigin(10) max_evaluations = 250000 algorithm = SimulatedAnnealing( problem=problem, mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20.0), termination_criterion=StoppingByEvaluations(max=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[0])) print('Fitness: ' + str(result.objectives[0])) print('Computing time: ' + str(algorithm.total_computing_time))
from jmetal.algorithm.multiobjective import NSGAII from jmetal.operator import SBXCrossover, PolynomialMutation from jmetal.problem import ZDT1 from jmetal.util.termination_criterion import StoppingByEvaluations problem = ZDT1() algorithm = NSGAII( problem=problem, population_size=100, offspring_population_size=100, mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20), crossover=SBXCrossover(probability=1.0, distribution_index=20), termination_criterion=StoppingByEvaluations(max_evaluations=25000) ) algorithm.run() from jmetal.util.solution import get_non_dominated_solutions, print_function_values_to_file, \ print_variables_to_file front = get_non_dominated_solutions(algorithm.get_result()) print_function_values_to_file(front, 'FUN.NSGAII.ZDT1') print_variables_to_file(front, 'VAR.NSGAII.ZDT1') from jmetal.lab.visualization import Plot plot_front = Plot(title='Pareto front approximation', axis_labels=['x', 'y']) plot_front.plot(front, label='NSGAII-ZDT1', filename='NSGAII-ZDT1', format='png')
mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20), crossover=SBXCrossover(probability=1.0, distribution_index=20), termination_criterion = StoppingByEvaluations(max_evaluations=max_evaluations) # termination_criterion = StoppingByQualityIndicator(quality_indicator=HyperVolume, expected_value=1, # degree=0.9) # selection = BinaryTournamentSelection() ) elif Configuration.algorithm == 'Random': algorithm = RandomSearch( problem=problem, termination_criterion=StoppingByEvaluations(max_evaluations=max_evaluations) ) """==========================调用算法模板进行种群进化=========================""" progress_bar = ProgressBarObserver(max=max_evaluations) algorithm.observable.register(progress_bar) algorithm.run() front = algorithm.get_result() """==================================输出结果==============================""" # Save results to file print_function_values_to_file(front, os.path.join(target_dir, '/FUN.' + algorithm.label)) print_variables_to_file(front, os.path.join(target_dir, '/VAR.' + algorithm.label)) print(f'Algorithm: ${algorithm.get_name()}') print(f'Problem: ${problem.get_name()}') print(f'Computing time: ${algorithm.total_computing_time}')
population_size=pop_size, offspring_population_size=int(pop_size * Cr), mutation=MulticastMutation(probability=Pmut, Pnmut=Pnmut, G=gr), crossover=MulticastCrossover( probability=Cr, root_node=root_node, destination_nodes=destination_nodes, graph=gr), termination_criterion=StoppingByEvaluations( max_evaluations=max_evaluations)) algorithm.run() front = algorithm.get_result() # Save results to file print_function_values_to_file( front, f"results/multiobjective/{params['algorithm']}/{params['pop_size']}_{params['nbr_generations']}_{params['network']}_{params['experiment']}_{i}" ) plot_front = Plot(title='Pareto front approximation', axis_labels=['cost', 'delay']) plot_front.plot(front, label=f'{params["algorithm"]}', filename=f'{params["algorithm"]}', format='png') print(f'Algorithm: ${algorithm.get_name()}') print(f'Problem: ${problem.get_name()}') print(f'Computing time: ${algorithm.total_computing_time}')
Configuration = configure() # global BestPopulation BestPopulation = BestPop(Configuration) # config.createfolders() Goal_num = Configuration.goal_num # file_name = text_create(Configuration ) # output = sys.stdout # outputfile = codecs.open(file_name, 'w', 'utf-8') # sys.stdout = outputfile """===============================实例化问题对象============================""" problem = OvertakeProblem(Goal_num, Configuration, BestPopulation) """=================================算法参数设置============================""" max_evaluations = Configuration.maxIterations algorithm = RandomSearch(problem=problem, termination_criterion=StoppingByEvaluations( max_evaluations=max_evaluations)) algorithm.run() front = algorithm.get_result() # Save results to file print_function_values_to_file( front, 'FUN.' + Configuration.file_dir_eval + algorithm.label) print_variables_to_file( front, 'VAR.' + Configuration.file_dir_var + algorithm.label) print(f'Algorithm: ${algorithm.get_name()}') print(f'Problem: ${problem.get_name()}') print(f'Computing time: ${algorithm.total_computing_time}')