def _initialize(self): # initialize the function parameters self.alpha, self.beta, self.gamma, self.delta = self.func_params( self.problem) # reset the restart history self.restart_history = [] # initialize the point if self.x0 is None: if not self.problem.has_bounds(): raise Exception( "Either provide an x0 or a problem with variable bounds!") # initialize randomly and make sure 5% is left for creating the initial simplex X = np.random.random(self.problem.n_var) self.x0 = denormalize(X, self.problem.xl, self.problem.xu) # parse the initial population from array or population object pop = pop_from_array_or_individual(self.x0) # if the simplex has not the correct number of points if len(pop) == 1: # the corresponding x values x0 = pop[0].X # if lower and upper bounds are given take 5% of the range and add if self.problem.has_bounds(): self.simplex_scaling = 0.05 * (self.problem.xu - self.problem.xl) # no bounds are given do it based on x0 - MATLAB procedure else: self.simplex_scaling = 0.05 * x0 # some value needs to be added if x0 is zero self.simplex_scaling[self.simplex_scaling == 0] = 0.00025 # initialize the simplex X = self.initialize_simplex(x0) # create a population object pop = pop.merge(Population().new("X", X)) # evaluate the values that are not already evaluated evaluate_if_not_done_yet(self.evaluator, self.problem, pop, algorithm=self) elif len(pop) != self.problem.n_var + 1: raise Exception( "Provided initial population has size of %s, but should have size of %s" % (len(pop), self.problem.n_var + 1)) # sort by its corresponding function values self.pop = pop[np.argsort(pop.get("F")[:, 0])] self.opt = self.pop[0]
def _initialize(self): super()._initialize() self.alpha, self.beta, self.gamma, self.delta = self.func_params( self.problem) # the corresponding x values of the provided or best found solution x0 = self.x0.X # if lower and upper bounds are given take 5% of the range and add if self.problem.has_bounds(): self.simplex_scaling = 0.05 * (self.problem.xu - self.problem.xl) # no bounds are given do it based on x0 - MATLAB procedure else: self.simplex_scaling = 0.05 * self.x0.X # some value needs to be added if x0 is zero self.simplex_scaling[self.simplex_scaling == 0] = 0.00025 # initialize the simplex simplex = pop_from_array_or_individual(self.initialize_simplex(x0)) self.evaluator.eval(self.problem, simplex, algorithm=self) # make the simplex the current the population and sort them by fitness pop = Population.merge(self.opt, simplex) self.pop = FitnessSurvival().do(self.problem, pop, len(pop))
def _initialize(self, **kwargs): super()._initialize(**kwargs) # no initial point is provided - sample in bounds and take the best if self.x0 is None: if not self.problem.has_bounds(): raise Exception( "Either provide an x0 or a problem with variable bounds!") self.pop = self.sampling.do(self.problem, self.n_sample_points) else: self.pop = pop_from_array_or_individual(self.x0) self.evaluator.eval(self.problem, self.pop, algorithm=self) self._set_optimum() self.x0 = self.opt[0]
def step(x, delta, k): # copy and add delta to the new point X = np.copy(x) # normalize the delta by the bounds if they are provided by the problem eps = delta[k] # if the problem has bounds normalize the delta if self.problem.has_bounds(): xl, xu = self.problem.bounds() eps *= (xu[k] - xl[k]) # now add to the current solution X[k] = X[k] + eps # repair if out of bounds if necessary X = set_to_bounds_if_outside_by_problem(self.problem, X) # return the new solution as individual mutant = pop_from_array_or_individual(X)[0] return mutant
def eval(self, problem, X, algorithm=None, **kwargs): pop = pop_from_array_or_individual(X) pop.set("algorithm", [algorithm] * len(pop)) if self.history is None: self.history = pop else: self.history = Population.create(self.history, pop) before = self.n_eval ret = super().eval(problem, X, **kwargs) after = self.n_eval if algorithm is not None: if algorithm not in self.algorithms: self.algorithms[algorithm] = 0 self.algorithms[algorithm] += after - before if self.opt is None or pop.get("F").min() < self.opt[0].F.min(): self.opt = pop[[pop.get("F").argmin()]] return ret