def minimize(problem, algorithm, termination=None, **kwargs): """ Minimization of function of one or more variables, objectives and constraints. This is used as a convenience function to execute several algorithms with default settings which turned out to work for a test single. However, evolutionary computations utilizes the idea of customizing a meta-algorithm. Customizing the algorithm using the object oriented interface is recommended to improve the convergence. Parameters ---------- problem : pymoo.problem A problem object which is defined using pymoo. algorithm : :class:`~pymoo.model.algorithm.Algorithm` The algorithm object that should be used for the optimization. termination : class or tuple The termination criterion that is used to stop the algorithm. Returns ------- res : :class:`~pymoo.model.result.Result` The optimization result represented as an object. """ # create a copy of the algorithm object to ensure no side-effects algorithm = copy.deepcopy(algorithm) # set the termination criterion and store it in the algorithm object if termination is None: termination = None elif not isinstance(termination, Termination): if isinstance(termination, str): termination = get_termination(termination) else: termination = get_termination(*termination) # initialize the method given a problem algorithm.initialize(problem, termination=termination, **kwargs) if algorithm.termination is None: if problem.n_obj > 1: algorithm.termination = MultiObjectiveDefaultTermination() else: algorithm.termination = SingleObjectiveDefaultTermination() # actually execute the algorithm res = algorithm.solve() # store the copied algorithm in the result object res.algorithm = algorithm return res
def termination_from_tuple(termination): from pymoo.model.termination import Termination # get the termination if provided as a tuple - create an object if termination is not None and not isinstance(termination, Termination): from pymoo.factory import get_termination if isinstance(termination, str): termination = get_termination(termination) else: termination = get_termination(*termination) return termination
def test_against_scipy(self): problem = get_problem("rosenbrock") problem.xl = None problem.xu = None x0 = np.array([0.5, 0.5]) hist_scipy = [] def fun(x): hist_scipy.append(x) return problem.evaluate(x) scipy_minimize(fun, x0, method='Nelder-Mead') hist_scipy = np.array(hist_scipy) hist = [] def callback(x): if x.shape == 2: hist.extend(x) else: hist.append(x) problem.callback = callback minimize( problem, nelder_mead(x0=x0, max_restarts=0, termination=get_termination("n_eval", len(hist_scipy)))) hist = np.row_stack(hist)[:len(hist_scipy)] self.assertTrue(np.all(hist - hist_scipy < 1e-7))
def pymoo_cube(objective, n_trials, method_name, n_dim, with_count, ref_dirs=None): class ObjectiveProblem(Problem): def __init__(self): super().__init__(n_var=n_dim, n_obj=1, n_constr=0, xl=0.0, xu=1.0) self.feval_count = 0 def _evaluate(self, x, out, *args, **kwargs): """ vectorized """ self.feval_count = self.feval_count + len(x) out["F"] = np.array([objective(u) for u in x]) try: algorithm = get_algorithm(method_name, ref_dirs=ref_dirs) except ValueError: algorithm = get_algorithm(method_name) termination = get_termination("n_eval", n_trials) problem = ObjectiveProblem() result = minimize(problem=problem, algorithm=algorithm, termination=termination, seed=None, verbose=False, display=None, callback=None, return_least_infeasible=False, save_history=False ) best_val = result.F[0] best_x = result.X.tolist() return (best_val, best_x, problem.feval_count) if with_count else (best_val, best_x)
def set_optimization(no_obj): """ Set the selected optimization technique. Args: n_obj (int): Number of objectives. Returns: algorithm (): Optization algorithm object. termination (): Termination object. """ setup = settings["optimization"] # Get optimization algorithm alg = set_algorithm(setup["algorithm"], no_obj, setup) # Get termination criterion termination = setup["termination"] if termination == "default": terimnation = "f_tol" setup_term = [0.001, 5, 5, 150, None] term = default_termination(no_obj, setup_term) else: term = get_termination(termination, *setup["termination_val"]) return alg, term
async def optimize(evaluate, configs): # config # D = 8 decision space dimension # N0 = 100 initial population # Ng = 100 total generation # seed = None random seed D, Ng, N0, seed = itemgetter('D', 'Ng', 'N0', 'seed')(configs) problem = Evaluator(evaluate, D) algorithm = NSGA2(pop_size=N0, n_offsprings=N0, sampling=get_sampling('real_random'), crossover=get_crossover('real_sbx', prob=0.9, eta=15), mutation=get_mutation('real_pm', eta=20), eliminate_duplicates=True) termination = get_termination('n_gen', Ng) await asyncio.sleep(0) res = minimize(problem, algorithm, termination, seed=seed, save_history=True, verbose=False) gbest = (res.X, res.F) return gbest
def test_against_scipy(): problem = get_problem("rosenbrock") problem.xl = None problem.xu = None x0 = np.array([0.5, 0.5]) hist_scipy = [] def fun(x): hist_scipy.append(x) return problem.evaluate(x) scipy_minimize(fun, x0, method='Nelder-Mead') hist_scipy = np.array(hist_scipy) hist = [] def callback(x, _): if x.shape == 2: hist.extend(x) else: hist.append(x) problem.callback = callback minimize( problem, NelderMead(x0=x0, max_restarts=0, termination=get_termination("n_eval", len(hist_scipy)))) hist = np.row_stack(hist)[:len(hist_scipy)] np.testing.assert_allclose(hist, hist_scipy, rtol=1e-04)
def pymoo_cube(objective, scale, n_trials, method_name, ref_dirs=None): class ObjectiveProblem(Problem): def __init__(self): super().__init__(n_var=3, n_obj=1, n_constr=0, xl=-scale, xu=scale) def _evaluate(self, x, out, *args, **kwargs): out["F"] = np.array([objective(u)[0] for u in x]) try: algorithm = get_algorithm(method_name, ref_dirs=ref_dirs) except ValueError: algorithm = get_algorithm(method_name) termination = get_termination("n_eval", n_trials) problem = ObjectiveProblem() result = minimize(problem=problem, algorithm=algorithm, termination=termination, seed=None, verbose=False, display=None, callback=None, return_least_infeasible=False, save_history=False ) f_min = result.F[0] return f_min
def test_multiObjOptimization(self): algorithm = NSGA2(pop_size=10, n_offsprings=10, sampling=get_sampling("real_random"), crossover=get_crossover("real_sbx", prob=0.9, eta=15), mutation=get_mutation("real_pm", eta=20), eliminate_duplicates=True) termination = get_termination("n_gen", 5) bayer = self.datasetUtils.readCFAImages() twoComplement = self.datasetUtils.twoComplementMatrix(bayer) twoComplement = twoComplement.astype("float32") problem = MyProblem(twoComplement) res = minimize(problem, algorithm, termination, save_history=True, verbose=True) # Objective Space res.F = 1 / res.F plot = Scatter(title="Objective Space") plot.add(res.F) plot.show() print("Best filter{}".format(np.reshape(res.opt[-1].X, [2, 2])))
def get_termination_for_final_tuning(time: str = '06:00:00'): """ Returns the termination criteria for the final tuning blackbox optimization problem """ termination = get_termination('time', time) return termination
def run(countryname, capacity): problem = ScheduleProblem(country_name=countryname, critical_capacity=capacity, record_all=True) algorithm = NSGA2( pop_size=100, n_offsprings=100, sampling=get_sampling("int_random"), crossover=get_crossover("int_sbx", prob=0.9, eta=15), mutation=get_mutation("int_pm", eta=20), eliminate_duplicates=True ) termination = get_termination("n_gen", 100) res = minimize(problem, algorithm, termination, seed=1, pf=problem.pareto_front(use_cache=False), save_history=True, verbose=True) # create the performance indicator object with reference point (4,4) metric = Hypervolume(ref_point=np.array([1.0, 1.0])) # collect the population in each generation pop_each_gen = [a.pop for a in res.history] with open("./experiments/ga_{}_lastpop.json".format(countryname), 'w') as f: json.dump( {"df":[e.to_dict() for e in problem.last[0]],"x":problem.last[1].tolist()}, f) with open("./experiments/ga_{}_lastobj.json".format(countryname), 'w') as f: json.dump( {"deaths": problem.last_objectives[0].tolist(), "activity":problem.last_objectives[1].tolist()} , f) # Objective Space fig = plt.figure() plot = Scatter(title = "Objective Space") plot.add(res.F) plt.savefig("./experiments/ga_{}_objective.png".format(countryname)) # receive the population in each generation obj_and_feasible_each_gen = [pop[pop.get("feasible")[:,0]].get("F") for pop in pop_each_gen] # calculate for each generation the HV metric hv = [metric.calc(f) for f in obj_and_feasible_each_gen] # function evaluations at each snapshot n_evals = np.array([a.evaluator.n_eval for a in res.history]) # visualize the convergence curve fig = plt.figure() plt.plot(n_evals, hv, '-o') plt.title("Convergence") plt.xlabel("Function Evaluations") plt.ylabel("Hypervolume") plt.savefig("./experiments/ga_{}_hypervolume.png".format(countryname)) plt.show()
def FI_minimize(problem, feas_algo, infeas_algo, termination, feas_mask=None, infeas_mask=None, **kwargs): # create a copy of the algorithm object to ensure no side-effects # algorithm = copy.deepcopy(algorithm) # set the termination criterion and store it in the algorithm object if termination is None: termination = None elif not isinstance(termination, Termination): if isinstance(termination, str): termination = get_termination(termination) else: termination = get_termination(*termination) # initialize the method given a problem feas_algo.initialize(problem, feas_mask, termination=termination, **kwargs) infeas_algo.initialize(problem, infeas_mask, termination=termination, **kwargs) #create feasible-infeasible algorithm f_if = FI_Algo() #initialize it # print('termination', termination) # print(kwargs) f_if.initialize(feas_algo, infeas_algo, problem=problem, termination=termination, **kwargs) # print(f_if.termination) #run solver res = f_if.solve() res.algorithm = f_if return res
def set_algorithm(self): self.algorithm = NSGA2(pop_size=self.pop_size, n_offsprings=self.n_offsprings, sampling=get_sampling("real_lhs"), crossover=get_crossover("real_sbx", prob=0.9, eta=15), mutation=get_mutation("real_pm", eta=20), eliminate_duplicates=True) self.termination = get_termination("n_gen", self.n_gen)
def optim(pop_size=10, n_gen=10, batch_size=10000, img_size=[28, 28], solution=[], modelName=None, dataName=None): datasets = {'mnist': mnsitDataset, 'cifar': cifarDataset, 'digits': digitsDataset} model = {'cnn': cnn, 'cnn2': cnn2, 'cnn3': cnn3, 'fnn2': fnn2, 'fnn3': fnn3} train_ds = datasets[dataName] myProblem = MyProblem(model[modelName], train_ds, batch_size, img_size) termination = get_termination("n_gen", n_gen) algorithm = NSGA2(pop_size=pop_size, sampling=Initing(solution)) print('\nThe first step of optimization is in progress...') res = minimize(myProblem, algorithm, termination, seed=1, save_history=True, display=MyDisplay(), verbose=True) return res.X, res.pop.get("X"), res.F, precisions, recalls, accuracies, entropys, hvs
def test_surrogate_optimize_with_all_selection_and_infill_methods( infill, surrogate_select_method): # Define original problem try: problem = benchmarks.zdt3() # Sample randomSample = sampling.rand(problem, 15) # Define surrogate ensemble # Define Optimizer optimizer = NSGA2(pop_size=20, n_offsprings=10, sampling=get_sampling("real_random"), crossover=get_crossover("real_sbx", prob=0.9, eta=15), mutation=get_mutation("real_pm", eta=20), eliminate_duplicates=True) # Define termination criteria termination = get_termination("n_gen", 10) # Define infill criteria infill_method = infill # Define surrogate selection surrogate_selection_function = surrogate_select_method # Optimize samples = copy.deepcopy(randomSample) surrogate_ensemble = [ LinearRegression(), KNeighborsRegressor(), DecisionTreeRegressor(), ] res = surrogate_optimization.optimize(problem, optimizer, termination, surrogate_ensemble, samples, infill_method, surrogate_selection_function, n_infill=2, max_samples=20) except Exception as e: raise pytest.fail( "infill: {0} /n and surrogate_selection: {1}/n raise a error: {2}". format(infill, surrogate_select_method, e))
def run(self): t_s = time.time() self.problem.logger.info("pymoo: {}".format(type(self.options["algorithm"]).__name__)) moo_algorithm = self.options["algorithm"] moo_problem = MooProblem(self.problem, self, moo_algorithm) termination = get_termination("n_gen", self.options["n_iterations"]) moo_algorithm.setup(moo_problem, termination, seed=1, save_history=True, verbose=self.options["verbose_level"] > 0) # until the algorithm has no terminated while moo_algorithm.has_next(): # do the next iteration moo_algorithm.next() # do same more things, printing, logging, storing or even modifying the algorithm object # print(moo_algorithm.n_gen, moo_algorithm.evaluator.n_eval) # obtain the result objective from the algorithm res = moo_algorithm.result() t = time.time() - t_s self.problem.logger.info("NLopt: elapsed time: {} s".format(t)) # create last population if res.X.ndim == 2: for x, f in zip(res.X, res.F): individual = Individual(list(x)) individual.costs = list(f) # append to problem self.problem.individuals.append(individual) # add to population individual.population_id = moo_algorithm.n_gen + 1 else: individual = Individual(list(res.X)) individual.costs = list(res.F) # append to problem self.problem.individuals.append(individual) # add to population individual.population_id = moo_algorithm.n_gen + 1 # sync changed individual informations self.problem.data_store.sync_all()
def calculate_pareto_front( state_space_equation, tau, y_measured, x0, xl: Union[np.ndarray, None], xu: Union[np.ndarray, None], n_var: int, n_obj: int, normalize_quaternions: bool, n_constr=0, pop_size=100, n_max_gen=1000, verbose=True, display=MyDisplay(), ) -> Result: # calculate pareto frontier problem = UUVParameterProblem( state_space_equation, tau, y_measured, x0, n_var=n_var, n_obj=n_obj, n_constr=n_constr, xl=xl, xu=xu, normalize_quaternions=normalize_quaternions, ) algorithm = NSGA2( pop_size=pop_size, # n_offsprings=int(pop_size / 2), crossover=SimulatedBinaryCrossover(eta=15, prob=0.9), mutation=PolynomialMutation(prob=None, eta=15), ) termination_default = MultiObjectiveDefaultTermination(n_max_gen=n_max_gen) termination_design_space = DesignSpaceToleranceTermination(n_max_gen=n_max_gen) termination_generations = get_termination("n_gen", 100) res = minimize( problem, algorithm, termination_default, verbose=verbose, display=display, ) return res
def solve_optimization_problem(problem: AdaptationProblem): # defining algorithm algorithm = NSGA2(pop_size=100, n_offsprings=10, eliminate_duplicates=True) # defining termination termination = get_termination('n_gen', 500) # solving problem and getting the results res = minimize(problem, algorithm, termination, seed=1, save_history=True, verbose=True) # you can find objectives in res.F and variables in res.X return res.F, res.X
def optimize( evaluator: FitnessEvaluator, free_parameters_range: "OrderedDict[str, Tuple[int, int]]", metrics: List[Metric], execution_time: Optional[str], power_of_2: Optional[List[str]], ) -> float: problem = MyProblem(evaluator, free_parameters_range, metrics) algorithm = NSGA2( repair=MyRepair(power_of_2) if power_of_2 else None, pop_size=10 * len(free_parameters_range.keys()), n_offsprings=10 * len(free_parameters_range.keys()), sampling=get_sampling("int_random"), crossover=get_crossover("int_sbx", prob=0.9, eta=15), mutation=get_mutation("int_pm", eta=20), eliminate_duplicates=True, ) if execution_time: termination = get_termination("time", execution_time) res = minimize( problem, algorithm, termination, seed=1, save_history=True, verbose=True, ) else: res = minimize( problem, algorithm, seed=1, save_history=True, verbose=True, ) # TODO take this out and write file design_space_path = "dovado_work/design_space.csv" objective_space_path = "dovado_work/objective_space.csv" Path(design_space_path).open("w") Path(objective_space_path).open("w") np.savetxt(design_space_path, res.X, delimiter=",") np.savetxt(objective_space_path, res.F, delimiter=",") return res.exec_time
def minimize(problem, method, termination, **kwargs): """ Minimization of function of one or more variables, objectives and constraints. This is used as a convenience function to execute several algorithms with default settings which turned out to work for a test problems. However, evolutionary computations utilizes the idea of customizing a meta-algorithm. Customizing the algorithm using the object oriented interface is recommended to improve the convergence. Parameters ---------- problem : pymop.problem A problem object defined using the pymop framework. Either existing test problems or custom problems can be provided. please have a look at the documentation. method : :class:`~pymoo.model.algorithm.Algorithm` The algorithm object that should be used for the optimization. termination : tuple The termination criterium that is used to stop the algorithm when the result is satisfying. Returns ------- res : :class:`~pymoo.model.result.Result` The optimization result represented as an object. """ # create an evaluator defined by the termination criterium if not isinstance(termination, Termination): termination = get_termination(*termination) # set a random random seed if not provided if 'seed' not in kwargs: kwargs['seed'] = random.randint(1, 10000) res = method.solve(problem, termination, **kwargs) return res
def run_nsga(s): # Define range for decision variables algorithm = NSGA2(pop_size=100, sampling=get_sampling("bin_random"), crossover=get_crossover("real_sbx", prob=0.9, prob_per_variable=1.0), mutation=get_mutation("real_pm", prob=0.8), eliminate_duplicates=True) termination = get_termination("n_gen", 100) problem = FunctionalProblem(1, my_objs, xl=np.array([0]), xu=np.array([1])) result = minimize(problem, algorithm, termination, seed=int(s), save_history=True, verbose=True) print("function:" + str(result.F)) print("x:" + str(result.X))
def _nsga_optimize(self, models): """NSGA-II optimization with categorical domains""" from pymoo.algorithms.nsga2 import NSGA2 from pymoo.optimize import minimize from pymoo.factory import get_termination optimizer = NSGA2(pop_size=self.pop_size) problem = TSEMOInternalWrapper(models, self.domain) termination = get_termination("n_gen", self.generations) self.internal_res = minimize(problem, optimizer, termination, seed=1, verbose=False) X = np.atleast_2d(self.internal_res.X).tolist() y = np.atleast_2d(self.internal_res.F).tolist() X = DataSet(X, columns=problem.X_columns) y = DataSet(y, columns=[v.name for v in self.domain.output_variables]) return X, y
def run_alg(): termination = get_termination("n_gen", constants.generations) algorithm = NSGA2(pop_size=constants.popsize, sampling=MySampling(), crossover=MyCrossover(), mutation=MyMutation(), display=MyDisplay(), eliminate_duplicates=MyDuplicateElimination()) result = minimize( MyProblem(), algorithm, termination, # seed=constants.seed_no, save_history=True, verbose=True) return result
def run(cross=0.7, ag=None, mut=0.01): algorithm = GA( pop_size=100, sampling=get_sampling("real_random"), crossover=get_crossover("real_one_point", prob=cross), mutation=get_mutation("real_pm", eta=20, prob=mut), #bin_bitflip n_offsprings=ag, #ag=1 -> steady-state, ag=None -> geracional eliminate_duplicates=True) X, F = problem.evaluate(np.random.rand(100, 1), return_values_of=["X", "F"]) termination = get_termination("n_eval", 25000) res = minimize( problem, algorithm, ('n_gen', 200), termination, seed=10, # elitismo verbose=False) return X, F, res
def _nsga_optimize_mixed(self, models): """NSGA-II optimization with mixed continuous-categorical domains""" from pymoo.algorithms.nsga2 import NSGA2 from pymoo.optimize import minimize from pymoo.factory import get_termination combos = self.categorical_combos transformed_combos = self._transform_categorical(combos) X_list, y_list = [], [] # Loop through all combinations of categoricals and run optimization bar = progress_bar(transformed_combos.iterrows(), total=transformed_combos.shape[0]) for _, combo in bar: # bar.comment = "NSGA Mixed Optimization" optimizer = NSGA2(pop_size=self.pop_size) problem = TSEMOInternalWrapper(models, self.domain, fixed_variables=combo.to_dict()) termination = get_termination("n_gen", self.generations) self.internal_res = minimize(problem, optimizer, termination, seed=1, verbose=False) X = np.atleast_2d(self.internal_res.X).tolist() y = np.atleast_2d(self.internal_res.F).tolist() X = DataSet(X, columns=problem.X_columns) y = DataSet(y, columns=[v.name for v in self.domain.output_variables]) # Add in categorical variables for key, value in combo.to_dict().items(): X[key] = value X_list.append(X) y_list.append(y) return pd.concat(X_list, axis=0), pd.concat(y_list, axis=0)
def pymoo_cube_sortof(n_trials=50): class SphereWithConstraint(Problem): def __init__(self): super().__init__(n_var=10, n_obj=1, n_constr=1, xl=0, xu=1) def _evaluate(self, x, out, *args, **kwargs): out["F"] = np.sum((x - 0.5)**2, axis=1) out["G"] = 0.1 - out["F"] algorithm = get_algorithm("nsga2") termination = get_termination("n_eval", n_trials) problem = SphereWithConstraint() result = minimize(problem=problem, algorithm=algorithm, termination=termination, seed=None, verbose=False, display=None, callback=None, return_least_infeasible=False, save_history=False) f_min = result.F[0] return f_min
from pymoo.algorithms.nsga2 import NSGA2 from pymoo.factory import get_problem, get_termination from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter problem = get_problem("zdt3") algorithm = NSGA2(pop_size=100) termination = get_termination("f_tol") res = minimize(problem, algorithm, termination, pf=True, seed=1, verbose=True) print(res.algorithm.n_gen) plot = Scatter(title="ZDT3") plot.add(problem.pareto_front(use_cache=False, flatten=False), plot_type="line", color="black") plot.add(res.F, color="red", alpha=0.8, s=20) plot.show()
from pymoo.algorithms.nsga2 import NSGA2 from pymoo.factory import get_problem, get_termination from pymoo.operators.crossover.simulated_binary_crossover import SimulatedBinaryCrossover from pymoo.operators.mutation.polynomial_mutation import PolynomialMutation from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter problem = get_problem("welded_beam") algorithm = NSGA2( pop_size=200, crossover=SimulatedBinaryCrossover(eta=20, prob=0.9), mutation=PolynomialMutation(prob=0.25, eta=40), ) termination = get_termination("f_tol", n_last=60) res = minimize( problem, algorithm, # ("n_gen", 800), pf=False, seed=10, verbose=True) print(res.algorithm.n_gen) Scatter().add(res.F, s=20).add(problem.pareto_front(), plot_type="line", color="black").show()
from pymoo.algorithms.nsga2 import NSGA2 from pymoo.factory import get_problem, get_termination from pymoo.optimize import minimize problem = get_problem("zdt3") algorithm = NSGA2(pop_size=100) termination = get_termination("x_tol", tol=0.001, n_last=20, nth_gen=10) res = minimize(problem, algorithm, termination, pf=problem.pareto_front(), seed=1, verbose=False) print(res.algorithm.n_gen)
from pymoo.algorithms.so_genetic_algorithm import GA from pymoo.factory import get_problem, get_termination from pymoo.optimize import minimize problem = get_problem("rastrigin") algorithm = GA(pop_size=100) termination = get_termination("f_tol_s") res = minimize(problem, algorithm, termination, pf=problem.pareto_front(), seed=1, verbose=True) print(res.opt.get("F")) print(res.algorithm.n_gen)