def main(): show = True save = False ha.print_to_moohst(show=show, save=save) problem = MOOHST() algorithm = NSGA2(pop_size=450) res = minimize(problem, algorithm, ("n_gen", 25), verbose=True, seed=3, save_history=True) dm = get_decision_making("high-tradeoff") I = dm.do(res.F) #* Plotting plot_scatter_front_3d(res, I, show_figure=show, save_figure=save) plot_scatter_set_3d(res, I, show_figure=show, save_figure=save) plot_scatter_matrix_front(res, I, show_figure=show, save_figure=save) plot_scatter_matrix_set(res, I, show_figure=show, save_figure=save) plot_convergence(res, show_figure=show, save_figure=save)
import argparse import pickle from pymoo.factory import get_decision_making from src.experiments import * parser = argparse.ArgumentParser() parser.add_argument("--folder", type=str, required=True) args = parser.parse_args() decision_making_strategies = dict(pw_all=("pseudo-weights", [.33, .33, .33]), pw_energy=("pseudo-weights", [.9, .05, .05]), pw_switches=("pseudo-weights", [.05, .9, .05]), pw_volume=("pseudo-weights", [.05, .05, .9])) res = pickle.load(open(os.path.join(args.folder, "results.pkl"), "rb")) problem_args = json.load(open(os.path.join(args.folder, "args.json"), "r")) vars(args).update(problem_args) experiment = getattr(sys.modules[__name__], args.experiment)() problem = experiment.problem for name, strategy in decision_making_strategies.items(): result = get_decision_making(*strategy).do(res.F) os.mkdir(os.path.join(args.folder, name)) problem.export_results(res.X[result], res.F[result], res.G[result], experiment.logger, os.path.join(args.folder, name))
problem = MyProblem() res = minimize(problem, algorithm, ('n_gen', 40), seed=1, pf=problem.pareto_front(use_cache=False), save_history=True, verbose=True) import numpy as np from pymoo.factory import get_decision_making, get_reference_directions weights = np.array([0.5, 0.5]) b, pseudo_weights = get_decision_making("pseudo-weights", weights).do(res.F, return_pseudo_weights=True) print(b) problem = MyProblem() print(problem.pareto_front()) print(problem.pareto_set()) F, dF = problem.evaluate(anp.array([0.1, 0.2]), return_values_of=["F", "dF"]) ps = problem.pareto_set(use_cache=False, flatten=False) pf = problem.pareto_front(use_cache=False, flatten=False) print(ps) print(pf)
res = pickle.load(open(os.path.join(args.folder, "results.pkl"), "rb")) problem_args = json.load(open(os.path.join(args.folder, "args.json"), "r")) vars(args).update(problem_args) experiment = getattr(sys.modules[__name__], args.experiment)() problem = experiment.problem names = problem.get_names() x_df = pd.DataFrame(data=res.X, columns=["X_%d" % (i, ) for i in range(0, res.X.shape[1])]) f_df = pd.DataFrame(data=res.F, columns=["f_%s" % (n, ) for n in names["objectives"]]) g_df = pd.DataFrame(data=res.G, columns=["g_%s" % (n, ) for n in names["constraints"]]) dataframes = [x_df, f_df, g_df] if args.export_pseudo_weights: from pymoo.factory import get_decision_making _, pseudo_weights = get_decision_making("pseudo-weights", [0, 0, 0]).do( res.F, return_pseudo_weights=True) pw_df = pd.DataFrame( data=pseudo_weights, columns=["pw_%s" % (n, ) for n in names["objectives"]]) dataframes.append(pw_df) results_df = pd.concat(dataframes, axis=1) results_df.to_excel(os.path.join(args.save, "results.xlsx"))
def main(): """ Optimization module main driver """ # Define initialization objects initializer_class = SmellInitialization if config.WARM_START else RandomInitialization initializer_object = initializer_class( udb_path=config.UDB_PATH, population_size=config.POPULATION_SIZE, lower_band=config.LOWER_BAND, upper_band=config.UPPER_BAND ) # ------------------------------------------- # Define optimization problems problems = list() # 0: Genetic (Single), 1: NSGA-II (Multi), 2: NSGA-III (Many) objectives problems problems.append( ProblemSingleObjective( n_objectives=config.NUMBER_OBJECTIVES, n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND, evaluate_in_parallel=False, ) ) problems.append( ProblemMultiObjective( n_objectives=config.NUMBER_OBJECTIVES, n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND, evaluate_in_parallel=False, ) ) problems.append( ProblemManyObjective( n_objectives=config.NUMBER_OBJECTIVES, n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND, evaluate_in_parallel=False, verbose_design_metrics=True, ) ) # Define search algorithms algorithms = list() # 1: GA alg1 = GA( pop_size=config.POPULATION_SIZE, sampling=PopulationInitialization(initializer_object), crossover=AdaptiveSinglePointCrossover(prob=config.CROSSOVER_PROBABILITY), # crossover=get_crossover("real_k_point", n_points=2), mutation=BitStringMutation(prob=config.MUTATION_PROBABILITY, initializer=initializer_object), eliminate_duplicates=ElementwiseDuplicateElimination(cmp_func=is_equal_2_refactorings_list), n_gen=config.NGEN, ) algorithms.append(alg1) # 2: NSGA-II alg2 = NSGA2( pop_size=config.POPULATION_SIZE, sampling=PopulationInitialization(initializer_object), crossover=AdaptiveSinglePointCrossover(prob=config.CROSSOVER_PROBABILITY), # crossover=get_crossover("real_k_point", n_points=2), mutation=BitStringMutation(prob=config.MUTATION_PROBABILITY, initializer=initializer_object), eliminate_duplicates=ElementwiseDuplicateElimination(cmp_func=is_equal_2_refactorings_list), n_gen=config.NGEN, ) algorithms.append(alg2) # 3: NSGA-III # pop_size must be equal or larger than the number of reference directions number_of_references_points = config.POPULATION_SIZE - int(config.POPULATION_SIZE * 0.20) ref_dirs = get_reference_directions( 'energy', # algorithm config.NUMBER_OBJECTIVES, # number of objectives number_of_references_points, # number of reference directions seed=1 ) alg3 = NSGA3( ref_dirs=ref_dirs, pop_size=config.POPULATION_SIZE, # 200 sampling=PopulationInitialization(initializer_object), selection=TournamentSelection(func_comp=binary_tournament), crossover=AdaptiveSinglePointCrossover(prob=config.CROSSOVER_PROBABILITY, ), # crossover=get_crossover("real_k_point", n_points=2), mutation=BitStringMutation(prob=config.MUTATION_PROBABILITY, initializer=initializer_object), eliminate_duplicates=ElementwiseDuplicateElimination(cmp_func=is_equal_2_refactorings_list), n_gen=config.NGEN, ) algorithms.append(alg3) # Termination of algorithms my_termination = MultiObjectiveDefaultTermination( x_tol=None, cv_tol=None, f_tol=0.0015, nth_gen=5, n_last=5, n_max_gen=config.MAX_ITERATIONS, # about 1000 - 1400 n_max_evals=1e6 ) # Do optimization for various problems with various algorithms res = minimize( problem=problems[config.PROBLEM], algorithm=algorithms[config.PROBLEM], termination=my_termination, seed=1, verbose=False, copy_algorithm=True, copy_termination=True, save_history=False, callback=LogCallback(), ) # np.save('checkpoint', res.algorithm) # Log results logger.info(f"***** Algorithm was finished in {res.algorithm.n_gen + config.NGEN} generations *****") logger.info(" ") logger.info("============ time information ============") logger.info(f"Start time: {datetime.fromtimestamp(res.start_time).strftime('%Y-%m-%d %H:%M:%S')}") logger.info(f"End time: {datetime.fromtimestamp(res.end_time).strftime('%Y-%m-%d %H:%M:%S')}") logger.info(f"Execution time in seconds: {res.exec_time}") logger.info(f"Execution time in minutes: {res.exec_time / 60}") logger.info(f"Execution time in hours: {res.exec_time / (60 * 60)}") # logger.info(f"Number of generations: {res.algorithm.n_gen}") # logger.info(f"Number of generations", res.algorithm.termination) # Log optimum solutions logger.info("============ All opt solutions ============") for i, ind in enumerate(res.opt): logger.info(f'Opt refactoring sequence {i}:') logger.info(ind.X) logger.info(f'Opt refactoring sequence corresponding objectives vector {i}:') logger.info(ind.F) logger.info("-" * 75) # Log best refactorings logger.info("============ Best refactoring sequences (a set of non-dominated solutions) ============") for i, ind in enumerate(res.X): logger.info(f'Best refactoring sequence {i}:') logger.info(ind) logger.info("-" * 75) logger.info("============ Best objective values (a set of non-dominated solutions) ============") for i, ind_objective in enumerate(res.F): logger.info(f'Best refactoring sequence corresponding objectives vector {i}:') logger.info(ind_objective) logger.info("-" * 75) # Save best refactorings population_trimmed = [] objective_values_content = '' for chromosome in res.X: chromosome_new = [] if config.PROBLEM == 0: # i.e., single objective problem for gene_ in chromosome: chromosome_new.append((gene_.name, gene_.params)) else: for gene_ in chromosome[0]: chromosome_new.append((gene_.name, gene_.params)) population_trimmed.append(chromosome_new) for objective_vector in res.F: objective_values_content += f'{res.algorithm.n_gen + config.NGEN},' if config.PROBLEM == 0: objective_values_content += f'{objective_vector},' else: for objective_ in objective_vector: objective_values_content += f'{objective_},' objective_values_content += '\n' best_refactoring_sequences_path = os.path.join( config.PROJECT_LOG_DIR, f'best_refactoring_sequences_after_{res.algorithm.n_gen + config.NGEN}gens.json' ) with open(best_refactoring_sequences_path, mode='w', encoding='utf-8') as fp: json.dump(population_trimmed, fp, indent=4) best_refactoring_sequences_objectives_path = os.path.join( config.PROJECT_LOG_DIR, f'best_refactoring_sequences_objectives_after_{res.algorithm.n_gen + config.NGEN}gens.csv' ) with open(best_refactoring_sequences_objectives_path, mode='w', encoding='utf-8') as fp: fp.write(objective_values_content) try: pf = res.F # dm = HighTradeoffPoints() dm = get_decision_making("high-tradeoff") I = dm.do(pf) logger.info("============ High-tradeoff points refactoring sequences ============") for i, ind in enumerate(res.X[I]): logger.info(f'High tradeoff points refactoring sequence {i}:') logger.info(ind) logger.info("-" * 75) logger.info("============ High-tradeoff points objective values ============") for i, ind_objective in enumerate(pf[I]): logger.info(f'High-tradeoff points refactoring sequence corresponding objectives vector {i}:') logger.info(ind_objective) logger.info("-" * 75) logger.info("High-tradeoff points mean:") logger.info(np.mean(pf[I], axis=0)) logger.info("High-tradeoff points median:") logger.info(np.median(pf[I], axis=0)) # Save high-tradeoff refactorings population_trimmed = [] objective_values_content = '' for chromosome in res.X[I]: chromosome_new = [] if config.PROBLEM == 0: # i.e., single objective problem for gene_ in chromosome: chromosome_new.append((gene_.name, gene_.params)) else: for gene_ in chromosome[0]: chromosome_new.append((gene_.name, gene_.params)) population_trimmed.append(chromosome_new) for objective_vector in pf[I]: objective_values_content += f'{res.algorithm.n_gen + config.NGEN},' if config.PROBLEM == 0: objective_values_content += f'{objective_vector},' else: for objective_ in objective_vector: objective_values_content += f'{objective_},' objective_values_content += '\n' high_tradeoff_path = os.path.join( config.PROJECT_LOG_DIR, f'high_tradeoff_points_refactoring_after_{res.algorithm.n_gen + config.NGEN}gens.json' ) with open(high_tradeoff_path, mode='w', encoding='utf-8') as fp: json.dump(population_trimmed, fp, indent=4) high_tradeoff_path_objectives_path = os.path.join( config.PROJECT_LOG_DIR, f'high_tradeoff_points_after_{res.algorithm.n_gen + config.NGEN}gens.csv' ) with open(high_tradeoff_path_objectives_path, mode='w', encoding='utf-8') as fp: fp.write(objective_values_content) except: logger.error("No multi-optimal solutions (error in computing high tradeoff points)!")
def main(): # Define search algorithms algorithms = list() # 1: GA algorithm = GA( pop_size=config.POPULATION_SIZE, sampling=PureRandomInitialization(), crossover=AdaptiveSinglePointCrossover(prob=0.9), # crossover=get_crossover("real_k_point", n_points=2), mutation=BitStringMutation(prob=0.1), eliminate_duplicates=ElementwiseDuplicateElimination( cmp_func=is_equal_2_refactorings_list)) algorithms.append(algorithm) # 2: NSGA II algorithm = NSGA2( pop_size=config.POPULATION_SIZE, sampling=PureRandomInitialization(), crossover=AdaptiveSinglePointCrossover(prob=0.9), # crossover=get_crossover("real_k_point", n_points=2), mutation=BitStringMutation(prob=0.1), eliminate_duplicates=ElementwiseDuplicateElimination( cmp_func=is_equal_2_refactorings_list)) algorithms.append(algorithm) # 3: NSGA III # pop_size must be equal or larger than the number of reference directions number_of_references_points = config.POPULATION_SIZE - int( config.POPULATION_SIZE * 0.20) ref_dirs = get_reference_directions( 'energy', # algorithm 8, # number of objectives number_of_references_points, # number of reference directions seed=1) algorithm = NSGA3( ref_dirs=ref_dirs, pop_size=config.POPULATION_SIZE, # 200 sampling=PureRandomInitialization(), selection=TournamentSelection(func_comp=binary_tournament), crossover=AdaptiveSinglePointCrossover(prob=0.8), # crossover=get_crossover("real_k_point", n_points=2), mutation=BitStringMutation(prob=0.1), eliminate_duplicates=ElementwiseDuplicateElimination( cmp_func=is_equal_2_refactorings_list)) algorithms.append(algorithm) # ------------------------------------------- # Define problems problems = list() problems.append( ProblemSingleObjective(n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND)) problems.append( ProblemMultiObjective(n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND)) problems.append( ProblemManyObjective(n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND, evaluate_in_parallel=True)) # Termination of algorithms my_termination = MultiObjectiveDefaultTermination( x_tol=None, cv_tol=None, f_tol=0.0015, nth_gen=10, n_last=20, n_max_gen=config.MAX_ITERATIONS, # about 1000 - 1400 n_max_evals=1e6) # Do optimization for various problems with various algorithms res = minimize( problem=problems[2], algorithm=algorithms[2], termination=my_termination, seed=1, verbose=False, copy_algorithm=True, copy_termination=True, save_history=False, ) # np.save('checkpoint', res.algorithm) # Log results logger.info("\n** FINISHED **\n") logger.info( "Best refactoring sequences (a set of non-dominated solutions):") logger.info(res.X) logger.info("Best objective values (a set of non-dominated solutions):") logger.info(res.F) logger.info("=" * 75) logger.info("Other solutions:") for ind in res.opt: logger.info(ind.X) logger.info(ind.F) logger.info("-" * 50) logger.info("=" * 75) logger.info(f"Start time: {res.start_time}") logger.info(f"End time: {res.end_time}") logger.info(f"Execution time in seconds: {res.exec_time}") logger.info(f"Execution time in minutes: {res.exec_time / 60}") logger.info(f"Execution time in hours: {res.exec_time / (60 * 60)}") logger.info(f"Number of generations: {res.algorithm.n_gen}") # logger.info(f"Number of generations", res.algorithm.termination) pf = res.F # dm = HighTradeoffPoints() dm = get_decision_making("high-tradeoff") try: I = dm.do(pf) logger.info(f"High tradeoff points: {pf[I][0]}") logger.info( f"High tradeoff points corresponding refactorings: {res.X[I]}") logger.info( f"The mean improvement of quality attributes: {np.mean(pf[I][0], axis=0)}" ) logger.info( f"The median improvement of quality attributes: {np.median(pf[I][0], axis=0)}" ) except: logger.info( "No multi optimal solutions (error in computing high tradeoff points)!" )
def get_knee_point(F: ndarray) -> ndarray: dm = get_decision_making("high-tradeoff") return dm.do(F)
if config.problem_args["n_obj"] == 1: sortedpop = sorted(res.pop, key=lambda p: p.F) X = np.stack([p.X for p in sortedpop]) else: X = res.pop.get("X") ls = config.latent(config) ls.set_from_population(X) torch.save(ls.state_dict(), os.path.join(config.tmp_folder, "ls_result")) if config.problem_args["n_obj"] == 1: X = np.atleast_2d(res.X) else: try: result = get_decision_making("pseudo-weights", [0, 1]).do(res.F) except: print("Warning: cant use pseudo-weights") result = get_decomposition("asf").do(res.F, [0, 1]).argmin() X = res.X[result] X = np.atleast_2d(X) ls.set_from_population(X) with torch.no_grad(): generated = problem.generator.generate(ls) if config.task == "txt2img": ext = "jpg" elif config.task == "img2txt":