def _step(self): pop = self.pop X, F, V = pop.get("X", "F", "V") # get the personal best of each particle pbest = Population.create(*pop.get("pbest")) P_X, P_F = pbest.get("X", "F") # get the global best solution best = self.opt.repeat(len(pop)) G_X = best.get("X") # perform the pso equation inerta = self.w * V # calculate random values for the updates r1 = np.random.random((len(pop), self.problem.n_var)) r2 = np.random.random((len(pop), self.problem.n_var)) cognitive = self.c1 * r1 * (P_X - X) social = self.c2 * r2 * (G_X - X) # calculate the velocity vector _V = inerta + cognitive + social _V = repair_out_of_bounds_manually(_V, -self.V_max, self.V_max) # update the values of each particle _X = X + _V _X = repair_out_of_bounds(self.problem, _X) # evaluate the offspring population off = Population(len(pop)).set("X", _X, "V", _V, "pbest", pbest) self.evaluator.eval(self.problem, off, algorithm=self) # check whether a solution has improved or not - also consider constraints here has_improved = ImprovementReplacement().do(self.problem, pbest, off, return_indices=True) # replace the personal best of each particle if it has improved off[has_improved].set("pbest", off[has_improved]) off.set("best", best) pop = off # try to improve the current best with a pertubation if self.pertube_best: opt = FitnessSurvival().do(self.problem, Population.create(*pop.get("pbest")), 1) eta = int(np.random.uniform(5, 30)) mutant = PolynomialMutation(eta).do(self.problem, opt) self.evaluator.eval(self.problem, mutant, algorithm=self) if ImprovementReplacement().do(self.problem, opt, mutant, return_indices=True)[0]: k = [i for i, e in enumerate(pop.get("pbest")) if e == opt][0] pop[k].set("pbest", mutant) self.pop = pop
def _step(self): pop = self.pop X, F, V = pop.get("X", "F", "V") # get the personal best of each particle pbest = Population.create(*pop.get("pbest")) P_X, P_F = pbest.get("X", "F") # get the GLOBAL best solution - other variants such as local best can be implemented here too best = self.opt.repeat(len(pop)) G_X = best.get("X") # get the inertia weight of the individual inerta = self.w * V # calculate random values for the updates r1 = np.random.random((len(pop), self.problem.n_var)) r2 = np.random.random((len(pop), self.problem.n_var)) cognitive = self.c1 * r1 * (P_X - X) social = self.c2 * r2 * (G_X - X) # calculate the velocity vector _V = inerta + cognitive + social _V = set_to_bounds_if_outside(_V, - self.V_max, self.V_max) # update the values of each particle _X = X + _V _X = InversePenaltyOutOfBoundsRepair().do(self.problem, _X, P=X) # evaluate the offspring population off = Population(len(pop)).set("X", _X, "V", _V, "pbest", pbest) self.evaluator.eval(self.problem, off, algorithm=self) # check whether a solution has improved or not - also consider constraints here has_improved = ImprovementReplacement().do(self.problem, pbest, off, return_indices=True) # replace the personal best of each particle if it has improved off[has_improved].set("pbest", off[has_improved]) off.set("best", best) pop = off # try to improve the current best with a pertubation if self.pertube_best: pbest = Population.create(*pop.get("pbest")) k = FitnessSurvival().do(self.problem, pbest, 1, return_indices=True)[0] eta = int(np.random.uniform(5, 30)) mutant = PolynomialMutation(eta).do(self.problem, pbest[[k]])[0] self.evaluator.eval(self.problem, mutant, algorithm=self) # if the mutant is in fact better - replace the personal best if is_better(mutant, pop[k]): pop[k].set("pbest", mutant) self.pop = pop
def solve(self, X_init, Y_init): ''' Solve the real multi-objective problem from initial data. Parameters ---------- X_init: np.array Initial design variables. Y_init: np.array Initial performance values. Returns ------- X_next: np.array Proposed design samples to evaluate next. Y_prediction: tuple (None, None) because there is no prediction in MOO algorithms. ''' # forward transformation X_init = self.transformation.do(X_init) # convert maximization to minimization X, Y = X_init, Y_init.copy() obj_type = self.real_problem.obj_type if isinstance(obj_type, str): obj_type = [obj_type] * Y.shape[1] assert isinstance(obj_type, Iterable) maxm_idx = np.array(obj_type) == 'max' Y[:, maxm_idx] = -Y[:, maxm_idx] # construct population pop = Population(0, individual=Individual()) pop = pop.new('X', X) pop.set('F', Y) pop.set('CV', np.zeros([X.shape[0], 1])) # assume input samples are all feasible off = self._solve(pop) X_next = off.get('X')[:self.batch_size] # backward transformation X_next = self.transformation.undo(X_next) return X_next, (None, None)
# create the optimization problem import numpy as np from pymoo.model.population import Population from pymoo.optimize import minimize from pymop.factory import get_problem problem = get_problem("rastrigin") pop_size = 100 pop = Population(pop_size) pop.set("X", np.random.random((pop_size, problem.n_var))) res = minimize(problem, method='ga', method_args={ 'pop_size': pop_size, 'sampling': pop }, termination=('n_gen', 200), disp=True)
import numpy as np from pymoo.algorithms.so_genetic_algorithm import GA from pymoo.factory import get_problem from pymoo.model.evaluator import Evaluator from pymoo.model.population import Population from pymoo.optimize import minimize problem = get_problem("sphere") X = np.random.random((500, problem.n_var)) pop = Population(len(X)) pop.set("X", X) Evaluator().eval(problem, pop) algorithm = GA(sampling=pop) res = minimize(problem, algorithm, seed=1, verbose=False) print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))