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 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 solve(seed): print(f"Starting seed {seed}") folder = os.path.join( os.path.dirname( os.path.dirname(os.path.dirname(os.path.realpath(__file__)))), "results") start = time.time() method = get_algorithm("ga", pop_size=20, sampling=get_sampling("int_random"), crossover=get_crossover("int_sbx", prob=1.0, eta=3.0), mutation=get_mutation("int_pm", eta=3.0), eliminate_duplicates=True, callback=MyCallback(folder)) res = minimize(ISCSO2019(), method, termination=('n_eval', 10000), seed=seed, verbose=True) end = time.time() elapsed = end - start np.savetxt(os.path.join(folder, f"ga_{seed}.x"), res.pop.get("X").astype(np.int)) np.savetxt(os.path.join(folder, f"ga_{seed}.f"), res.pop.get("F")) np.savetxt(os.path.join(folder, f"ga_{seed}.g"), res.pop.get("G")) print(f"Finished seed {seed} - runtime: {elapsed}")
def optimize(self, initial_suggest: pd.DataFrame = None, fix_input: dict = None) -> pd.DataFrame: lb = self.space.opt_lb.numpy() ub = self.space.opt_ub.numpy() prob = BOProblem(lb, ub, self.acq, self.space, fix_input) init_pop = self.get_init_pop(initial_suggest) mutation = self.get_mutation() crossover = self.get_crossover() try: algo = get_algorithm(self.es, pop_size=self.pop, sampling=init_pop, mutation=mutation, crossover=crossover) res = minimize(prob, algo, ('n_gen', self.iter), verbose=self.verbose) except: if self.acq.num_obj > 1: algo = get_algorithm('nsga2', pop_size=self.pop, sampling=init_pop, mutation=mutation, crossover=crossover) else: algo = get_algorithm('ga', pop_size=self.pop, sampling=init_pop, mutation=mutation, crossover=crossover) res = minimize(prob, algo, ('n_gen', self.iter), verbose=self.verbose) opt_x = res.X.reshape(-1, len(lb)).astype(float) opt_xcont = torch.from_numpy(opt_x[:, :self.space.num_numeric]) opt_xenum = torch.from_numpy(opt_x[:, self.space.num_numeric:]) df_opt = self.space.inverse_transform(opt_xcont, opt_xenum) if fix_input is not None: for k, v in fix_input.items(): df_opt[k] = v return df_opt
def _next(self, archive, predictor, K): """ searching for next K candidate for high-fidelity evaluation (lower level) """ # the following lines corresponding to Algo 1 line 10 / Fig. 3(b) in the paper # get non-dominated architectures from archive F = np.column_stack(([x[1] for x in archive], [x[2] for x in archive])) front = NonDominatedSorting().do(F, only_non_dominated_front=True) # non-dominated arch bit-strings nd_X = np.array([self.search_space.encode(x[0]) for x in archive])[front] # initialize the candidate finding optimization problem problem = AuxiliarySingleLevelProblem( self.search_space, predictor, self.sec_obj, { 'n_classes': self.n_classes, 'model_path': self.supernet_path }) # initiate a multi-objective solver to optimize the problem method = get_algorithm( "nsga2", pop_size=40, sampling=nd_X, # initialize with current nd archs crossover=get_crossover("int_two_point", prob=0.9), mutation=get_mutation("int_pm", eta=1.0), eliminate_duplicates=True) # kick-off the search res = minimize(problem, method, termination=('n_gen', 20), save_history=True, verbose=True) # check for duplicates not_duplicate = np.logical_not([ x in [x[0] for x in archive] for x in [self.search_space.decode(x) for x in res.pop.get("X")] ]) # the following lines corresponding to Algo 1 line 11 / Fig. 3(c)-(d) in the paper # form a subset selection problem to short list K from pop_size indices = self._subset_selection(res.pop[not_duplicate], F[front, 1], K) pop = res.pop[not_duplicate][indices] candidates = [] for x in pop.get("X"): candidates.append(self.search_space.decode(x)) # decode integer bit-string to config and also return predicted top1_err return candidates, predictor.predict(pop.get("X"))
def _ga(self) -> object: sampling = get_sampling("int_random") crossover = get_crossover("int_sbx") mutation = get_mutation("int_pm") algorithm = get_algorithm("ga", pop_size=self._tamanho_populacao, sampling=sampling, crossover=crossover, mutation=mutation, eliminate_duplicates=True) return algorithm
def _initialize(self): pop = pop_from_sampling(self.problem, self.sampling, self.n_initial_samples) evaluate_if_not_done_yet(self.evaluator, self.problem, pop, algorithm=self) for i in np.argsort(pop.get("F")[:, 0]): algorithm = get_algorithm("nelder-mead", problem=self.problem, x0=pop[i], termination=NelderAndMeadTermination(x_tol=1e-3, f_tol=1e-3), evaluator=self.evaluator ) algorithm.initialize() self.algorithms.append(algorithm) self.pop = pop
def set_algorithm(name, no_obj, setup): """ Text. Args: name (str): Name of the optimization algorithm. n_obj (int): Number of objectives. setup (dict): Optimization setup parameters. Returns: algorithm (): Optization algorithm object. """ if name == "default": if no_obj == 1: name = "ga" elif no_obj > 1: name = "nsga3" # Get settings setup_gen = load_json( os.path.join(settings["root"], "app", "config", "opticonf", "general")) setup_alg = load_json( os.path.join(settings["root"], "app", "config", "opticonf", name)) algorithm_args = {} # Get optimization settings objects algorithm_args["sampling"] = get_sampling(setup_gen["sampling"]) if "operators" in setup: for operator in setup["operators"]: algorithm_args[operator] = get_operator(operator, setup) # Get reference directions if name == "nsga3": algorithm_args["ref_dirs"] = get_reference_directions( "energy", no_obj, setup_alg["ref_dirs_coef"] * no_obj) # Modify population if "n_offsprings" in setup: algorithm_args["n_offsprings"] = setup["n_offsprings"] if "pop_size" in setup: algorithm_args["pop_size"] = setup["pop_size"] algorithm = get_algorithm(name, eliminate_duplicates=True, **algorithm_args) return algorithm
def runGA(): start = time.time() pop_size = 10 n_mascons = 1 algorithm = get_algorithm("ga", pop_size=pop_size, sampling=get_sampling("int_random"), crossover=get_crossover("int_sbx", prob=0.9, eta=3.0), mutation=get_mutation("int_pm", eta=3.0), eliminate_duplicates=True) problem = MasconModel(n_mascons) #n #n_p res = minimize(problem, algorithm, seed=1, save_history=True, verbose=True) end = time.time() elapsedTime = (end - start) resultWithCoord = getCoordinatesByIndex(res.X) repeated_values = getRepeatedValues(res.X.tolist()) print("Elapsed (after compilation) = %s" % elapsedTime) print("Best solution found: %s" % res.X) print("Function value: %s" % res.F) #Save GA info to database gaDict = { "properties": { "fopt": res.F.tolist(), "degree": 165, "order": 165, "gen": res.algorithm.n_gen, "population": pop_size, "time": elapsedTime, "n_mascons": n_mascons, "solution": res.X.tolist(), "solution_repeated_values": repeated_values, }, "data": resultWithCoord } saveJson(gaDict, 'n_' + str(n_mascons)) #save information in a json file
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
def _next(self): # all place visited so far _X, _F, _evaluated_by_algorithm = self.evaluator.history.get("X", "F", "algorithm") # collect attributes from each algorithm and determine whether it has to be replaced or not pop, F, n_evals = [], [], [] for k, algorithm in enumerate(self.algorithms): # collect some data from the current algorithms _pop = algorithm.pop # if the algorithm has terminated or not has_finished = algorithm.termination.has_terminated(algorithm) # if the area was already explored before closest_dist_to_others = vectorized_cdist(_pop.get("X"), _X[_evaluated_by_algorithm != algorithm], func_dist=norm_euclidean_distance(self.problem)) too_close_to_others = (closest_dist_to_others.min(axis=1) < 1e-3).all() # whether the algorithm is the current best - if yes it will not be replaced current_best = self.evaluator.opt.get("F") == _pop.get("F").min() # algorithm not really useful anymore if not current_best and (has_finished or too_close_to_others): # find a suitable x0 which is far from other or has good expectations self.sampling.criterion = lambda X: vectorized_cdist(X, _X).min() X = self.sampling.do(self.problem, self.n_initial_samples).get("X") # distance in x space to other existing points x_dist = vectorized_cdist(X, _X, func_dist=norm_euclidean_distance(self.problem)).min(axis=1) f_pred, f_uncert = predict_by_nearest_neighbors(_X, _F, X, 5, self.problem) fronts = NonDominatedSorting().do(np.column_stack([- x_dist, f_pred, f_uncert])) I = np.random.choice(fronts[0]) # I = vectorized_cdist(X, _X, func_dist=norm_euclidean_distance(self.problem)).min(axis=1).argmax() # choose the one with the largest distance to current solutions x0 = X[[I]] # replace the current algorithm algorithm = get_algorithm("nelder-mead", problem=self.problem, x0=x0, termination=NelderAndMeadTermination(x_tol=1e-3, f_tol=1e-3), evaluator=self.evaluator, ) algorithm.initialize() self.algorithms[k] = algorithm pop.append(algorithm.pop) F.append(algorithm.pop.get("F")) n_evals.append(self.evaluator.algorithms[algorithm]) # get the values of all algorithms as arrays F, n_evals = np.array(F), np.array(n_evals) rewards = 1 - normalize(F.min(axis=1))[:, 0] n_evals_total = self.evaluator.n_eval - self.evaluator.algorithms[self] # calculate the upper confidence bound ucb = rewards + 0.95 * np.sqrt(np.log(n_evals_total) / n_evals) I = ucb.argmax() self.algorithms[I].next() # create the population object with all algorithms self.pop = Population.create(*pop) # update the current optimum self.opt = self.evaluator.opt
out["F"] = f out["G"] = anp.column_stack([g1, g2, g3, g4, g5, g6, g7, g8, g9]) def _calc_pareto_front(self): return -15 def _calc_pareto_set(self): return anp.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1]) myProblem = G1() # method = ga( # pop_size=100, # eliminate_duplicates=True) method = get_algorithm("ga", pop_size=200, sampling=get_sampling("bin_random"), crossover=get_crossover("bin_hux"), mutation=get_mutation("bin_bitflip"), elimate_duplicates=True) res = minimize(myProblem, method, termination=('n_gen', 50), disp=False) print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F)) print("Best solution found: %s" % res.X.astype(np.int)) print("Function value: %s" % res.F) print("Constraint violation: %s" % res.CV)
decomp = get_decomposition("asf", ideal_point=np.array([0.0, 0.0]), nadir_point=np.array([1.0, 1.0])) pf = original_problem.pareto_front() problem = decompose(original_problem, decomp, weights) for i in range(100): if i != 23: continue res = minimize( problem, get_algorithm("nelder-mead", n_max_restarts=10, adaptive=True), #scipy_minimize("Nelder-Mead"), #termination=("n_eval", 30000), seed=i, verbose=False) #print(res.X) F = ModifiedZDT1(n_var=n_var).evaluate(res.X, return_values_of="F") print(i, F) opt = decomp.do(pf, weights).argmin() print(pf[opt]) print(decomp.do(pf, weights).min())
from src.experiments import * if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--experiment", type=str, default="ThresholdPrototypeExperiment") parser.add_argument("--algorithm", type=str, default="nsga2") parser.add_argument("--pop-size", type=int, default=100) parser.add_argument("--generations", type=int, default=1000) parser.add_argument("--save", type=str, default="") args = parser.parse_args() if args.save != "": assert not os.path.exists(args.save), "%s already exists" % (args.save) os.mkdir(args.save) experiment = getattr(sys.modules[__name__], args.experiment)() problem_algorithm_arguments = dict() if args.algorithm in experiment.algorithm_arguments: problem_algorithm_arguments = experiment.algorithm_arguments[args.algorithm] algorithm = get_algorithm(args.algorithm, pop_size=args.pop_size, **problem_algorithm_arguments) res = minimize(experiment.problem, algorithm, ("n_gen", args.generations), verbose=True) if args.save != "": pickle.dump(res, open(os.path.join(args.save, "results.pkl"), "wb")) json.dump(vars(args), open(os.path.join(args.save, "args.json"), "w"))
) if iteration < config.generations else "genetic-it-final.%s" % ( ext, ) algorithm.problem.generator.save( generated, os.path.join(config.tmp_folder, name)) problem = GenerationProblem(config) operators = get_operators(config) if not os.path.exists(config.tmp_folder): os.mkdir(config.tmp_folder) algorithm = get_algorithm( config.algorithm, pop_size=config.pop_size, sampling=operators["sampling"], crossover=operators["crossover"], mutation=operators["mutation"], eliminate_duplicates=True, callback=save_callback, **(config.algorithm_args[config.algorithm] if "algorithm_args" in config and config.algorithm in config.algorithm_args else dict())) res = minimize( problem, algorithm, ("n_gen", config.generations), save_history=False, verbose=True, ) pickle.dump(dict( X=res.X,
L.append(l) M = np.dot(lambda1,(cost_all/T))+np.dot(lambda2,S)#凑款难度 # print('筹款难度'+str(M)) g1 = (np.zeros((x.shape[0],1)))**2 - 1e-5 out["F"] = np.column_stack([T, L, M]) out["G"] = g1 #%%使用NSGA2求解pareto前沿 from pymoo.factory import get_algorithm, get_crossover, get_mutation, get_sampling method = get_algorithm("nsga2", pop_size=100, sampling=get_sampling("int_random"), crossover=get_crossover("int_sbx", prob=1.0, eta=3.0), mutation=get_mutation("int_pm", eta=3.0), eliminate_duplicates=True, ) from pymoo.optimize import minimize #from pymoo.visualization.scatter import Scatter res = minimize(MyProblem(), method, termination=('n_gen', 40), seed=1, save_history=True) #plot = Scatter(title = "Objective Space") #plot.add(res.F, color="red")