def Experiment(seedstrings): problem = MADM() seedset = [] while len(seedset) <= N_seeds: for string in seedstrings: seed = problem.create_solution() seed.variables = string seedset.append(seed) max_evaluations = 100000 #algorithm = SPEA2( algorithm = NSGAII( problem=problem, population_size=100, offspring_population_size=100, mutation=IntegerPolynomialMutation(probability=0.1), crossover=IntegerCrossover(probability=0.9), termination_criterion=StoppingByEvaluations(max=max_evaluations), population_generator=InjectorGenerator(seedset)) resultsets = algorithm.run() process = [] for g in range(len(resultsets)): process.append(get_non_dominated_solutions(resultsets[g])) return process, algorithm.get_result()
def nsgaii_train(particoes, regras, instancias, classes): problem = MixedIntegerFloatProblem(particoes, regras, instancias, classes) max_evaluations = 10 algorithm = NSGAII( problem=problem, population_size=10, offspring_population_size=10, mutation=CompositeMutation([IntegerPolynomialMutation(0.05, 20), IntegerPolynomialMutation(0.05, 20), PolynomialMutation(0.05, 20.0)]), crossover=CompositeCrossover([IntegerSBXCrossover(probability=0.95, distribution_index=20), IntegerSBXCrossover(probability=0.95, distribution_index=20), SBXCrossover(probability=0.95, distribution_index=20)]), termination_criterion=StoppingByEvaluations(max_evaluations=max_evaluations) ) algorithm.run() front = get_non_dominated_solutions(algorithm.get_result()) # Save results to file print_function_values_to_file(front, 'FUN.' + algorithm.label) print_variables_to_file(front, 'VAR.' + algorithm.label) print('Algorithm (continuous problem): ' + algorithm.get_name()) print('Problem: ' + problem.get_name()) print('Computing time: ' + str(algorithm.total_computing_time)) minAcuracia = 0 index = -1 for i, f in enumerate(front): if minAcuracia > f.objectives[0]: minAcuracia = f.objectives[0] index = i #for variable in front[index].variables: #print(variable.variables) particoes = problem.alterar_centroids(front[index].variables[2].variables) new_regras = problem.cromossomo_para_regras(front[index].variables[0].variables, front[index].variables[1].variables, problem.semente.qtdAntecedenteRegra, particoes) return particoes, new_regras
def configure_experiment(problems: dict, n_run: int): jobs = [] for run in range(n_run): for problem_tag, problem in problems.items(): jobs.append( Job( algorithm=NSGAII( problem=problem, population_size=20, offspring_population_size=20, mutation=IntegerPolynomialMutation(probability=0.05, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.3, distribution_index=20), # termination_criterion=StoppingByEvaluations(max_evaluations=max_evaluations) termination_criterion=stopCriterion ), algorithm_tag='NSGAII', problem_tag=problem_tag, run=run, ) ) jobs.append( Job( algorithm=SPEA2( problem=problem, population_size=20, offspring_population_size=20, mutation=IntegerPolynomialMutation(probability=0.05, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.3, distribution_index=20), termination_criterion=StoppingByEvaluations(max_evaluations=max_evaluations) ), algorithm_tag='SPEA2', problem_tag=problem_tag, run=run, ) ) return jobs
def search(dec2bin_param, group_name, program_name, alg): folder = './result/' + group_name if not os.path.exists(folder): os.makedirs(folder) num_vars = -1 if dec2bin_param == 10: num_vars = 50 elif dec2bin_param == 11: num_vars = 50 elif dec2bin_param == 7: num_vars = 7 elif dec2bin_param == 4: num_vars = 26 else: num_vars = -1 problem = TestingProblem(num_vars, dec2bin_param, group_name, program_name, alg) np.set_printoptions(linewidth=500) if alg == 'GA': algorithm = GeneticAlgorithm( problem=problem, population_size=10, # 10 offspring_population_size=10, mutation=IntegerPolynomialMutation(1.0 / problem.number_of_variables, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.9, distribution_index=20), # 0.9, 20 selection=BestSolutionSelection(), termination_criterion=StoppingByEvaluations(max_evaluations=500) # 1000 ) elif "RS": algorithm = RandomSearch( problem=problem, termination_criterion=StoppingByEvaluations(max_evaluations=500) ) else: print("Search algorithm " + alg + " not supported!") return algorithm.run() front = algorithm.get_result() print(f'Algorithm: ${algorithm.get_name()}') print(f'Problem: ${problem.get_name()}') print(f'Computing time: ${algorithm.total_computing_time}')
logging.basicConfig(level=logging.INFO, filename="Result20190123.log") N_seeds = 50 run = 30 fm = 'NSGAII' #if __name__ == '__main__': logging.info(get_apps()) #Find AO seed: GA problem = MADA() max_evaluations = 20000 algorithm = GeneticAlgorithm( problem=problem, population_size=100, offspring_population_size=100, mutation=IntegerPolynomialMutation(probability=0.1), crossover=IntegerCrossover(probability=0.9), selection=BinaryTournamentSelection(), termination_criterion=StoppingByEvaluations(max=max_evaluations)) #algorithm.observable.register(observer=PrintObjectivesObserver(frequency=10)) algorithm.run() aggseed = algorithm.get_result() logging.info('Aggregated seed solution is {}: {}'.format( aggseed.variables, aggseed.objectives[0])) #print(change) #Find the min time, also SO time seed: GA problem = MADT() max_evaluations = 10000
get_non_dominated_solutions, print_function_values_to_file, print_variables_to_file, ) from jmetal.util.termination_criterion import StoppingByEvaluations if __name__ == "__main__": problem = MixedIntegerFloatProblem(10, 10, 100, -100, -1000, 1000) max_evaluations = 25000 algorithm = NSGAII( problem=problem, population_size=100, offspring_population_size=100, mutation=CompositeMutation([ IntegerPolynomialMutation(0.01, 20), PolynomialMutation(0.01, 20.0) ]), crossover=CompositeCrossover([ IntegerSBXCrossover(probability=1.0, distribution_index=20), SBXCrossover(probability=1.0, distribution_index=20), ]), termination_criterion=StoppingByEvaluations( max_evaluations=max_evaluations), ) algorithm.run() front = get_non_dominated_solutions(algorithm.get_result()) # Save results to file print_function_values_to_file(front, "FUN." + algorithm.label)
if __name__ == '__main__': # Problem set problem = llvmMultiobjetiveProblem( max_epochs=config_max_epochs, population_size=config_population_size, offspring_population_size=config_offspring_population_size, solution_length=config_solution_length, verbose=config_verbose) # Algorithm set algorithm = NSGAII( problem=problem, population_size=config_population_size, offspring_population_size=config_offspring_population_size, mutation=IntegerPolynomialMutation(config_probability_mutation), crossover=SBXCrossover(config_probability_crossover), selection=RandomSolutionSelection(), termination_criterion=problem) algorithm.run() nds = get_non_dominated_solutions(algorithm.get_result()) with open(f"{problem.config_to_str()}_results.csv", "w") as file: for sol in nds: file.write(f'{sol.variables};{sol.objectives}\n') with open(f"{problem.config_to_str()}_results.data", "w") as file: #Outputs file.write('\nSettings:')
def run(self) -> List[S]: pool_1_size = self.population_size pool_2_size = self.population_size selection_operator_1 = BinaryTournamentSelection() crossover_operator_1 = IntegerSBXCrossover(1.0, 20.0) mutation_operator_1 = IntegerPolynomialMutation( 1.0 / self.problem.number_of_variables, 20.0) selection_operator_2 = DifferentialEvolutionSelection() crossover_operator_2 = DifferentialEvolutionCrossover(0.2, 0.5, 0.5) dominance = DominanceComparator() max_iterations = self.max_iterations iterations = 0 parent_1: List[IntegerSolution] = [None, None] generational_hv: List[float] = [] current_gen = 0 """Create the initial subpopulation pools and evaluate them""" pool_1: List[IntegerSolution] = [] for i in range(pool_1_size): pool_1.append(self.problem.create_solution()) pool_1[i] = self.problem.evaluate(pool_1[i]) pool_2: List[IntegerSolution] = [] for i in range(pool_2_size): pool_2.append(self.problem.create_solution()) pool_2[i] = self.problem.evaluate(pool_2[i]) evaluations = pool_1_size + pool_2_size mix = self.mix_interval problem = self.problem h = HyperVolume(reference_point=[1] * self.problem.number_of_objectives) initial_population = True """The main evolutionary cycle""" while iterations < max_iterations: combi: List[IntegerSolution] = [] if not initial_population: offspring_pop_1: List[IntegerSolution] = [] offspring_pop_2: List[IntegerSolution] = [] """Evolve pool 1""" for i in range(pool_1_size): parent_1[0] = selection_operator_1.execute(pool_1) parent_1[1] = selection_operator_1.execute(pool_1) child_1: IntegerSolution = crossover_operator_1.execute( parent_1)[0] child_1 = mutation_operator_1.execute(child_1) child_1 = problem.evaluate(child_1) evaluations += 1 offspring_pop_1.append(child_1) """Evolve pool 2""" for i in range(pool_2_size): parent_2: List[ IntegerSolution] = selection_operator_2.execute(pool_2) crossover_operator_2.current_individual = pool_2[i] child_2 = crossover_operator_2.execute(parent_2) child_2 = problem.evaluate(child_2[0]) evaluations += 1 result = dominance.compare(pool_2[i], child_2) if result == -1: offspring_pop_2.append(pool_2[i]) elif result == 1: offspring_pop_2.append(child_2) else: offspring_pop_2.append(child_2) offspring_pop_2.append(pool_2[i]) ind_1 = pool_1[random.randint(0, pool_1_size - 1)] ind_2 = pool_2[random.randint(0, pool_2_size - 1)] offspring_pop_1.append(ind_1) offspring_pop_2.append(ind_2) offspring_pop_1.extend(pool_1) pool_1 = self.r.replace(offspring_pop_1[:pool_1_size], offspring_pop_1[pool_1_size:]) pool_2 = self.r.replace(offspring_pop_2[:pool_2_size], offspring_pop_2[pool_2_size:]) mix -= 1 if mix == 0: """Time to perform fitness sharing""" mix = self.mix_interval combi = combi + pool_1 + pool_2 # print("Combi size: ", len(combi)) """pool1size/10""" combi = self.r.replace( combi[:int(pool_1_size / 10)], combi[int(pool_1_size / 10):len(combi)], ) """ print( "Sizes: ", len(pool_1) + len(combi), len(pool_2) + len(combi), "\n", ) """ pool_1 = self.r.replace(pool_1, combi) pool_2 = self.r.replace(pool_2, combi) if initial_population: initial_population = False iterations += 1 print("Iterations: ", str(iterations)) """ hval_1 = h.compute([s.objectives for s in pool_1]) hval_2 = h.compute([s.objectives for s in pool_2]) print("hval_1: ", str(hval_1)) print("hval_2: ", str(hval_2), "\n") """ new_gen = int(evaluations / self.report_interval) if new_gen > current_gen: combi = combi + pool_1 + pool_2 combi = self.r.replace(combi[:(2 * pool_1_size)], combi[(2 * pool_1_size):]) hval = h.compute([s.objectives for s in combi]) for i in range(current_gen, new_gen, 1): generational_hv.append(hval) current_gen = new_gen """#Write runtime generational HV to file""" """Return the first non dominated front""" combi_ini: List[IntegerSolution] = [] combi_ini.extend(pool_1) combi_ini.extend(pool_2) combi_ini = self.r.replace( combi_ini[:pool_1_size + pool_2_size], combi_ini[pool_1_size + pool_2_size:], ) return combi_ini
log_front = Log("front_140_Services") Net = Network(folderName="Topologia2") for executions in range(timesToRun): print("Interation number:", executions) problem = OTNProblem(Net, len(Net.LinkBundles)) max_evaluations = 1000 # stopCriterion = StoppingByEvaluationsCustom(max_evaluations, [200, 2.1]) # To topology 1, 200 is enough algorithm = NSGAII(problem=problem, population_size=20, offspring_population_size=20, mutation=IntegerPolynomialMutation( probability=0.05, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.3, distribution_index=20), termination_criterion=StoppingByEvaluationsCustom( max_evaluations=max_evaluations, reference_point=[5000, 2.1], AlgorithmName='Reference') # termination_criterion=stopCriterion ) progress_bar = ProgressBarObserver(max=max_evaluations) algorithm.observable.register(progress_bar) algorithm.run() solutions = algorithm.get_result()
def configure_experiment(problems: dict, n_run: int): jobs = [] for run in range(n_run): for problem_tag, problem in problems.items(): jobs.append( Job( algorithm=NSGAII( problem=problem, population_size=POPULATION_SIZE, offspring_population_size=POPULATION_SIZE, mutation=IntegerPolynomialMutation(probability=0.05, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.3, distribution_index=20), termination_criterion=StoppingByEvaluationsCustom(max_evaluations=max_evaluations, reference_point=REFERENCE_POINT, AlgorithmName='NSGAII') # termination_criterion=stopCriterion ), algorithm_tag='NSGAII', problem_tag=problem_tag, run=run, ) ) jobs.append( Job( algorithm=NSGAIII( problem=problem, population_size=POPULATION_SIZE, mutation=IntegerPolynomialMutation(probability=0.05, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.3, distribution_index=20), reference_directions=UniformReferenceDirectionFactory(2, n_points=91), termination_criterion=StoppingByEvaluationsCustom(max_evaluations=max_evaluations, reference_point=REFERENCE_POINT, AlgorithmName='NSGAIII') # termination_criterion=stopCriterion ), algorithm_tag='NSGAIII', problem_tag=problem_tag, run=run, ) ) jobs.append( Job( algorithm=SPEA2( problem=problem, population_size=POPULATION_SIZE, offspring_population_size=POPULATION_SIZE, mutation=IntegerPolynomialMutation(probability=0.05, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.3, distribution_index=20), termination_criterion=StoppingByEvaluationsCustom(max_evaluations=max_evaluations, reference_point=REFERENCE_POINT, AlgorithmName='SPEA2') ), algorithm_tag='SPEA2', problem_tag=problem_tag, run=run, ) ) jobs.append( Job( algorithm=HYPE( problem=problem, reference_point=reference_point, population_size=POPULATION_SIZE, offspring_population_size=POPULATION_SIZE, mutation=IntegerPolynomialMutation(probability=0.05, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.3, distribution_index=20), termination_criterion=StoppingByEvaluationsCustom(max_evaluations=max_evaluations, reference_point=REFERENCE_POINT, AlgorithmName='HYPE') ), algorithm_tag='HYPE', problem_tag=problem_tag, run=run, ) ) jobs.append( Job( algorithm=MOCell( problem=problem, population_size=POPULATION_SIZE, neighborhood=C9(4, 4), archive=CrowdingDistanceArchive(100), mutation=IntegerPolynomialMutation(probability=0.05, distribution_index=20), crossover=IntegerSBXCrossover(probability=0.3, distribution_index=20), termination_criterion=StoppingByEvaluationsCustom(max_evaluations=max_evaluations, reference_point=REFERENCE_POINT, AlgorithmName='MOCell') ), algorithm_tag='MOCELL', problem_tag=problem_tag, run=run, ) ) jobs.append( Job( algorithm=OMOPSO( problem=problem, swarm_size=swarm_size, epsilon=0.0075, uniform_mutation=UniformMutation(probability=0.05, perturbation=0.5), non_uniform_mutation=NonUniformMutation(mutation_probability, perturbation=0.5, max_iterations=int(max_evaluations / swarm_size)), leaders=CrowdingDistanceArchive(10), termination_criterion=StoppingByEvaluationsCustom(max_evaluations=max_evaluations, reference_point=REFERENCE_POINT, AlgorithmName='OMOPSO') ), algorithm_tag='OMOPSO', problem_tag=problem_tag, run=run, ) ) jobs.append( Job( algorithm=SMPSO( problem=problem, swarm_size=POPULATION_SIZE, mutation=PolynomialMutation(probability=0.05, distribution_index=20), leaders=CrowdingDistanceArchive(20), termination_criterion=StoppingByEvaluationsCustom(max_evaluations=max_evaluations, reference_point=REFERENCE_POINT, AlgorithmName='SMPSO') ), algorithm_tag='SMPSO', problem_tag=problem_tag, run=run, ) ) return jobs