Esempio n. 1
0
    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
Esempio n. 2
0
    def _step(self):
        pop = self.pop
        X = pop.get("X")
        F = pop.get("F")

        #Levy Flight
        best = self.opt
        G_X = best.get("X")

        step_size = self._get_global_step_size(X)
        _X = X + np.random.rand(*X.shape) * step_size * (G_X - X)
        _X = set_to_bounds_if_outside_by_problem(self.problem, _X)

        #Evaluate
        off = Population(len(pop)).set("X", _X)
        self.evaluator.eval(self.problem, off, algorithm=self)

        # replace the worse pop with better off per index
        # this method includes replacement with less constraints violation
        # which the original paper doesn't have
        ImprovementReplacement().do(self.problem, pop, off, inplace=True)

        #Local Random Walk
        dir_vec = self._get_local_directional_vector(X)
        _X = X + dir_vec
        _X = set_to_bounds_if_outside_by_problem(self.problem, _X)
        off = Population(len(pop)).set("X", _X)
        self.evaluator.eval(self.problem, off, algorithm=self)

        #append offspring to population and then sort for elitism (survival)
        self.pop = Population.merge(pop, off)
        self.pop = self.survival.do(self.problem,
                                    self.pop,
                                    self.pop_size,
                                    algorithm=self)
Esempio n. 3
0
    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
Esempio n. 4
0
    def _next(self):

        # make a step and create the offsprings
        self.off = self._step()

        # evaluate the offsprings
        self.evaluator.eval(self.problem, self.off, algorithm=self)

        # replace the individuals that have improved
        self.pop = ImprovementReplacement().do(self.problem, self.pop,
                                               self.off)
Esempio n. 5
0
    def _next(self):
        pop, n_offsprings = self.pop, self.n_offsprings

        best = FitnessSurvival().do(self.problem, pop, 1,
                                    return_indices=True)[0]

        perm = lambda: np.random.permutation(len(pop))

        # randomly select the parents and to be used for mating
        a = perm()[:n_offsprings]
        # a = np.repeat(best, n_offsprings)

        # b = perm()[:n_offsprings]
        # b[np.random.random(len(b)) < 0.25] = best

        # a = np.repeat(best, n_offsprings)
        b = np.repeat(best, n_offsprings)
        # b = np.random.permutation(len(pop))[:n_offsprings]
        c = perm()[:n_offsprings]

        P = np.column_stack([a, b, c])

        # do the levy flight mating and evaluate the result offsprings
        off = self.mating.do(self.problem,
                             pop,
                             len(pop),
                             parents=P,
                             algorithm=self)
        self.evaluator.eval(self.problem, off, algorithm=self)

        # randomly assign an offspring to a nest to be replaced
        # I = perm()[:len(off)]
        I = off.get("index")

        # replace the solution in each nest where it has improved
        has_improved = ImprovementReplacement().do(self.problem,
                                                   pop[I],
                                                   off,
                                                   return_indices=True)

        # choose some nests to be abandon no matter if they have improved or not
        abandon = np.random.random(len(I)) < self.pa

        # never abandon the currently best solution
        abandon[I == best] = False

        # either because they have improved or they have been abandoned they are replaced
        replace = has_improved | abandon

        # replace the individuals in the population
        self.pop[I[replace]] = off[replace]
        self.off = off
Esempio n. 6
0
    def _next(self):
        selection, crossover, mutation = self.mating.selection, self.mating.crossover, self.mating.mutation

        # retrieve the current population
        pop = self.pop

        # get the vectors from the population
        F, CV, feasible = pop.get("F", "CV", "feasible")
        F = parameter_less(F, CV)

        # create offsprings and add it to the data of the algorithm
        if self.var_selection == "rand":
            P = selection.do(pop, self.pop_size, crossover.n_parents)

        elif self.var_selection == "best":
            best = np.argmin(F[:, 0])
            P = selection.do(pop, self.pop_size, crossover.n_parents - 1)
            P = np.column_stack([np.full(len(pop), best), P])

        elif self.var_selection == "rand+best":
            best = np.argmin(F[:, 0])
            P = selection.do(pop, self.pop_size, crossover.n_parents)
            use_best = np.random.random(len(pop)) < 0.3
            P[use_best, 0] = best

        else:
            raise Exception("Unknown selection: %s" % self.var_selection)

        # do the first crossover which is the actual DE operation
        self.off = crossover.do(self.problem, pop, P, algorithm=self)

        # then do the mutation (which is actually )
        _pop = Population.merge(self.pop, self.off)
        _P = np.column_stack(
            [np.arange(len(pop)),
             np.arange(len(pop)) + len(pop)])
        self.off = mutation.do(self.problem, _pop, _P,
                               algorithm=self)[:len(self.pop)]

        # bounds back if something is out of bounds
        self.off = BounceBackOutOfBoundsRepair().do(self.problem, self.off)

        # evaluate the results
        self.evaluator.eval(self.problem, self.off, algorithm=self)

        # replace the individuals that have improved
        self.pop = ImprovementReplacement().do(self.problem, self.pop,
                                               self.off)
Esempio n. 7
0
    def _next(self):

        # get the offspring solutions and evaluate them
        off = self._step()
        self.evaluator.eval(self.problem, off, algorithm=self)

        # the the personal bests of the current population
        pbest = Population.create(*self.pop.get("pbest"))

        # if an offspring has improved the personal store that index
        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])
        pop = off

        self.off = off
        self.pop = pop

        if self.adaptive:
            self._adapt()