def solve(self, problem, termination, seed=None, disp=False, callback=None, save_history=False, pf=None, **kwargs): """ Solve a given problem by a given evaluator. The evaluator determines the termination condition and can either have a maximum budget, hypervolume or whatever. The problem can be any problem the algorithm is able to solve. Parameters ---------- problem: class Problem to be solved by the algorithm termination: class object that evaluates and saves the number of evaluations and determines the stopping condition seed: int Random seed for this run. Before the algorithm starts this seed is set. disp : bool If it is true than information during the algorithm execution are displayed callback : func A callback function can be passed that is executed every generation. The parameters for the function are the algorithm itself, the number of evaluations so far and the current population. def callback(algorithm): pass save_history : bool If true, a current snapshot of each generation is saved. pf : np.array The Pareto-front for the given problem. If provided performance metrics are printed during execution. Returns ------- res : dict A dictionary that saves all the results of the algorithm. Also, the history if save_history is true. """ # set the random seed for generator if seed is not None: random.seed(seed) # the evaluator object which is counting the evaluations self.evaluator = Evaluator() self.problem = problem self.termination = termination self.pf = pf self.disp = disp self.callback = callback self.save_history = save_history # call the algorithm to solve the problem pop = self._solve(problem, termination) # get the optimal result by filtering feasible and non-dominated opt = pop.copy() opt = opt[opt.collect(lambda ind: ind.feasible)[:, 0]] # if at least one feasible solution was found if len(opt) > 0: if problem.n_obj > 1: I = NonDominatedSorting().do(opt.get("F"), only_non_dominated_front=True) opt = opt[I] X, F, CV, G = opt.get("X", "F", "CV", "G") else: opt = opt[np.argmin(opt.get("F"))] X, F, CV, G = opt.X, opt.F, opt.CV, opt.G else: opt = None res = Result(opt, opt is None, "") res.algorithm, res.problem, res.pf = self, problem, pf res.pop = pop if opt is not None: res.X, res.F, res.CV, res.G = X, F, CV, G res.history = self.history return res
def solve( self, problem, evaluator, seed=1, return_only_feasible=True, return_only_non_dominated=True, history=None, ): """ Solve a given problem by a given evaluator. The evaluator determines the termination condition and can either have a maximum budget, hypervolume or whatever. The problem can be any problem the algorithm is able to solve. Parameters ---------- problem: class Problem to be solved by the algorithm evaluator: class object that evaluates and saves the number of evaluations and determines the stopping condition seed: int Random seed for this run. Before the algorithm starts this seed is set. return_only_feasible : bool If true, only feasible solutions are returned. return_only_non_dominated : bool If true, only the non dominated solutions are returned. Otherwise, it might be - dependend on the algorithm - the final population Returns ------- X: matrix Design space F: matrix Objective space G: matrix Constraint space """ # set the random seed for generator pymoo.rand.random.seed(seed) # just to be sure also for the others seed = pymoo.rand.random.randint(0, 100000) random.seed(seed) np.random.seed(seed) # this allows to provide only an integer instead of an evaluator object if not isinstance(evaluator, Evaluator): evaluator = Evaluator(evaluator) # call the algorithm to solve the problem X, F, G = self._solve(problem, evaluator) if return_only_feasible: if G is not None and G.shape[0] == len(F) and G.shape[1] > 0: cv = Problem.calc_constraint_violation(G)[:, 0] X = X[cv <= 0, :] F = F[cv <= 0, :] if G is not None: G = G[cv <= 0, :] if return_only_non_dominated: idx_non_dom = NonDominatedRank.calc_as_fronts(F, G)[0] X = X[idx_non_dom, :] F = F[idx_non_dom, :] if G is not None: G = G[idx_non_dom, :] return X, F, G