Exemplo n.º 1
0
    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]
Exemplo n.º 2
0
    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