Example #1
0
    def __init__(self,
                 method,
                 with_bounds=False,
                 with_constr=False,
                 require_jac=False,
                 use_bounds=True,
                 use_constr=True,
                 estm_gradients=True,
                 disp=False,
                 show_warnings=False,
                 **kwargs):

        super().__init__(display=SingleObjectiveDisplay(), **kwargs)

        self.method, self.with_bounds, self.with_constr, self.require_jac = method, with_bounds, with_constr, require_jac
        self.show_warnings = show_warnings
        self.use_bounds = use_bounds
        self.use_constr = use_constr
        self.estm_gradients = estm_gradients

        self.options = {
            'maxiter':
            int(1e8),  # because of C code interfacing this can not be inf
            'disp': disp
        }
Example #2
0
 def __init__(self,
              eps=1e-2,
              penalty=0.1,
              display=SingleObjectiveDisplay(),
              **kwargs):
     super().__init__(display=display, **kwargs)
     self.eps = eps
     self.penalty = penalty
Example #3
0
 def __init__(self,
              n_points_per_iteration=100,
              sampling=LatinHypercubeSampling(),
              display=SingleObjectiveDisplay(),
              **kwargs):
     super().__init__(display=display, **kwargs)
     self.n_points_per_iteration = n_points_per_iteration
     self.sampling = sampling
Example #4
0
    def __init__(self,
                 n_elites=200,
                 n_offsprings=700,
                 n_mutants=100,
                 bias=0.7,
                 sampling=FloatRandomSampling(),
                 survival=None,
                 display=SingleObjectiveDisplay(),
                 eliminate_duplicates=False,
                 **kwargs
                 ):
        """


        Parameters
        ----------

        n_elites : int
            Number of elite individuals

        n_offsprings : int
            Number of offsprings to be generated through mating of an elite and a non-elite individual

        n_mutants : int
            Number of mutations to be introduced each generation

        bias : float
            Bias of an offspring inheriting the allele of its elite parent

        eliminate_duplicates : bool or class
            The duplicate elimination is more important if a decoding is used. The duplicate check has to be
            performed on the decoded variable and not on the real values. Therefore, we recommend passing
            a DuplicateElimination object.
            If eliminate_duplicates is simply set to `True`, then duplicates are filtered out whenever the
            objective values are equal.

        """

        if survival is None:
            survival = EliteSurvival(n_elites, eliminate_duplicates=eliminate_duplicates)

        super().__init__(pop_size=n_elites + n_offsprings + n_mutants,
                         n_offsprings=n_offsprings,
                         sampling=sampling,
                         selection=EliteBiasedSelection(),
                         crossover=BinomialCrossover(bias, prob=1.0),
                         mutation=NoMutation(),
                         survival=survival,
                         display=display,
                         eliminate_duplicates=True,
                         advance_after_initial_infill=True,
                         **kwargs)

        self.n_elites = n_elites
        self.n_mutants = n_mutants
        self.bias = bias
        self.default_termination = SingleObjectiveDefaultTermination()
Example #5
0
 def __init__(self,
              eps=1e-2,
              penalty=0.1,
              n_max_candidates=10,
              display=SingleObjectiveDisplay(),
              **kwargs):
     super().__init__(display=display, **kwargs)
     self.eps = eps
     self.penalty = penalty
     self.n_max_candidates = n_max_candidates
Example #6
0
    def __init__(self,
                 pop_size=100,
                 sampling=LatinHypercubeSampling(),
                 variant="DE/rand/1/bin",
                 CR=0.5,
                 F=0.3,
                 dither="vector",
                 jitter=False,
                 display=SingleObjectiveDisplay(),
                 **kwargs):
        """

        Parameters
        ----------

        pop_size : {pop_size}

        sampling : {sampling}

        variant : {{DE/(rand|best)/1/(bin/exp)}}
         The different variants of DE to be used. DE/x/y/z where x how to select individuals to be pertubed,
         y the number of difference vector to be used and z the crossover type. One of the most common variant
         is DE/rand/1/bin.

        F : float
         The weight to be used during the crossover.

        CR : float
         The probability the individual exchanges variable values from the donor vector.

        dither : {{'no', 'scalar', 'vector'}}
         One strategy to introduce adaptive weights (F) during one run. The option allows
         the same dither to be used in one iteration ('scalar') or a different one for
         each individual ('vector).

        jitter : bool
         Another strategy for adaptive weights (F). Here, only a very small value is added or
         subtracted to the weight used for the crossover for each individual.

        """

        mating = DifferentialEvolutionMating(variant=variant,
                                             CR=CR,
                                             F=F,
                                             dither=dither,
                                             jitter=jitter)

        super().__init__(pop_size=pop_size,
                         sampling=sampling,
                         mating=mating,
                         survival=None,
                         display=display,
                         **kwargs)

        self.default_termination = SingleObjectiveDefaultTermination()
Example #7
0
    def __init__(self,
                 pop_size=200,
                 n_parallel=10,
                 sampling=LatinHypercubeSampling(),
                 display=SingleObjectiveDisplay(),
                 repair=None,
                 individual=Individual(),
                 **kwargs):
        """

        Parameters
        ----------
        pop_size : {pop_size}
        sampling : {sampling}
        selection : {selection}
        crossover : {crossover}
        mutation : {mutation}
        eliminate_duplicates : {eliminate_duplicates}
        n_offsprings : {n_offsprings}

        """

        super().__init__(display=display, **kwargs)

        self.initialization = Initialization(sampling,
                                             individual=individual,
                                             repair=repair)

        self.pop_size = pop_size
        self.n_parallel = n_parallel
        self.each_pop_size = pop_size // n_parallel

        self.solvers = None
        self.niches = []

        def cmaes(problem, x):
            solver = CMAES(x0=x, tolfun=1e-11, tolx=1e-3, restarts=0)
            solver.initialize(problem)
            solver.next()
            return solver

        def nelder_mead(problem, x):
            solver = NelderMead(X=x)
            solver.initialize(problem)
            solver._initialize()
            solver.n_gen = 1
            solver.next()
            return solver

        self.func_create_solver = nelder_mead

        self.default_termination = SingleObjectiveDefaultTermination()
Example #8
0
 def __init__(self,
              eps=1e-2,
              penalty=0.1,
              n_max_candidates=10,
              n_max_archive=400,
              archive_reduct=0.66,
              display=SingleObjectiveDisplay(),
              **kwargs):
     super().__init__(display=display, **kwargs)
     self.eps = eps
     self.penalty = penalty
     self.n_max_candidates = n_max_candidates
     self.n_max_archive = n_max_archive
     self.archive_reduct = archive_reduct
Example #9
0
    def __init__(self,
                 n_offsprings=200,
                 pop_size=None,
                 rule=1.0 / 7.0,
                 phi=1.0,
                 gamma=0.85,
                 sampling=FloatRandomSampling(),
                 survival=FitnessSurvival(),
                 display=SingleObjectiveDisplay(),
                 **kwargs):
        """
        Evolutionary Strategy (ES)

        Parameters
        ----------
        n_offsprings : int
            The number of individuals created in each iteration.
        pop_size : int
            The number of individuals which are surviving from the offspring population (non-elitist)
        rule : float
            The rule (ratio) of individuals surviving. This automatically either calculated `n_offsprings` or `pop_size`.
        phi : float
            Expected rate of convergence (usually 1.0).
        gamma : float
            If not `None`, some individuals are created using the differentials with this as a length scale.
        sampling : object
            The sampling method for creating the initial population.
        """

        if pop_size is None and n_offsprings is not None:
            pop_size = int(np.math.ceil(n_offsprings * rule))
        elif n_offsprings is None and pop_size is not None:
            n_offsprings = int(np.math.fllor(n_offsprings / rule))

        assert pop_size is not None and n_offsprings is not None, "You have to at least provivde pop_size of n_offsprings."
        assert n_offsprings >= 2 * pop_size, "The number of offsprings should be at least double the population size."

        super().__init__(pop_size=pop_size,
                         n_offsprings=n_offsprings,
                         sampling=sampling,
                         survival=survival,
                         display=display,
                         advance_after_initial_infill=True,
                         **kwargs)

        self.default_termination = SingleObjectiveDefaultTermination()
        self.phi = phi
        self.gamma = gamma

        self.tau, self.taup, self.sigma_max = None, None, None
Example #10
0
    def __init__(self,
                 n_elites=20,
                 n_offsprings=70,
                 n_mutants=10,
                 bias=0.7,
                 sampling=FloatRandomSampling(),
                 survival=None,
                 display=SingleObjectiveDisplay(),
                 eliminate_duplicates=False,
                 **kwargs):
        """


        Parameters
        ----------

        n_elites : int
            Population size

        n_offsprings : int
            Fraction of elite items into each population

        n_mutants : int
            Fraction of mutants introduced at each generation into the population

        bias : float
            Probability that an offspring inherits the allele of its elite parent

        """

        if survival is None:
            survival = EliteSurvival(n_elites,
                                     eliminate_duplicates=eliminate_duplicates)

        super().__init__(pop_size=n_elites + n_offsprings + n_mutants,
                         n_offsprings=n_offsprings,
                         sampling=sampling,
                         selection=EliteBiasedSelection(),
                         crossover=BiasedCrossover(bias, prob=1.0),
                         mutation=NoMutation(),
                         survival=survival,
                         display=display,
                         eliminate_duplicates=True,
                         **kwargs)

        self.n_elites = n_elites
        self.n_mutants = n_mutants
        self.bias = bias
        self.default_termination = SingleObjectiveToleranceBasedTermination()
Example #11
0
    def __init__(self,
                 func_params=adaptive_params,
                 display=SingleObjectiveDisplay(),
                 **kwargs):
        """

        Parameters
        ----------
        X : np.array or Population
            The initial point where the search should be based on or the complete initial simplex (number of
            dimensions plus 1 points). The population objective can be already evaluated with objective
            space values. If a numpy array is provided it is evaluated by the algorithm.
            By default it is None which means the search starts at a random point.

        func_params : func
            A function that returns the parameters alpha, beta, gamma, delta for the search. By default:

            >>>  def adaptive_params(problem):
            ...     n = problem.n_var
            ...
            ...     alpha = 1
            ...     beta = 1 + 2 / n
            ...     gamma = 0.75 - 1 / (2 * n)
            ...     delta = 1 - 1 / n
            ...     return alpha, beta, gamma, delta

            It can be overwritten if necessary.


        criterion_local_restart : Termination
            Provide a termination object which decides whether a local restart should be performed or not.

        """

        super().__init__(display=display, **kwargs)

        # the function to return the parameter
        self.func_params = func_params

        # the attributes for the simplex operations
        self.alpha, self.beta, self.gamma, self.delta = None, None, None, None

        # the scaling used for the initial simplex
        self.simplex_scaling = None

        # the termination used for nelder and mead if nothing else provided
        self.default_termination = NelderAndMeadTermination()
Example #12
0
    def __init__(self,
                 x0=None,
                 sampling=LatinHypercubeSampling(),
                 display=SingleObjectiveDisplay(),
                 n_sample_points="auto",
                 n_max_sample_points=20,
                 **kwargs):

        super().__init__(display=display, **kwargs)
        self.sampling = sampling
        self.n_sample_points = n_sample_points
        self.n_max_sample_points = n_max_sample_points
        self.x0 = x0
        self.default_termination = SingleObjectiveSpaceToleranceTermination(
            n_last=5, tol=1e-8)

        self.is_local_initialized = False
Example #13
0
    def __init__(self,
                 X,
                 dX=None,
                 objective=0,
                 display=SingleObjectiveDisplay(),
                 **kwargs) -> None:
        super().__init__(display=display, **kwargs)

        self.objective = objective
        self.n_restarts = 0
        self.default_termination = SingleObjectiveDefaultTermination()

        self.X, self.dX = X, dX
        self.F, self.CV = None, None

        if self.X.ndim == 1:
            self.X = np.atleast_2d(X)
Example #14
0
    def __init__(self,
                 pop_size=25,
                 n_offsprings=None,
                 sampling=LHS(),
                 termination=SingleObjectiveDefaultTermination(),
                 display=SingleObjectiveDisplay(),
                 beta=1.5,
                 alpha=0.01,
                 pa=0.1,
                 **kwargs):
        """

        Parameters
        ----------

        sampling : {sampling}

        termination : {termination}

        pop_size : int
         The number of nests to be used

        beta : float
            The input parameter of the Mantegna's Algorithm to simulate
            sampling on Levy Distribution

        alpha : float
            The step size scaling factor and is usually 0.01.

        pa : float
            The switch probability, pa fraction of the nests will be abandoned on every iteration
        """
        mating = kwargs.get("mating")
        if mating is None:
            mating = LevyFlights(alpha, beta)

        super().__init__(pop_size=pop_size,
                         n_offsprings=n_offsprings,
                         sampling=sampling,
                         mating=mating,
                         termination=termination,
                         display=display,
                         **kwargs)

        self.pa = pa
Example #15
0
    def __init__(self,
                 pop_size=100,
                 sampling=FloatRandomSampling(),
                 selection=RandomSelection(),
                 crossover=SimulatedBinaryCrossover(prob=0.9, eta=3),
                 mutation=PolynomialMutation(prob=None, eta=5),
                 eliminate_duplicates=True,
                 n_offsprings=None,
                 display=SingleObjectiveDisplay(),
                 **kwargs):
        """

        Parameters
        ----------
        pop_size : {pop_size}
        sampling : {sampling}
        selection : {selection}
        crossover : {crossover}
        mutation : {mutation}
        eliminate_duplicates : {eliminate_duplicates}
        n_offsprings : {n_offsprings}

        """

        super().__init__(pop_size=pop_size,
                         sampling=sampling,
                         selection=selection,
                         crossover=crossover,
                         mutation=mutation,
                         survival=NichingSurvival(),
                         eliminate_duplicates=eliminate_duplicates,
                         n_offsprings=n_offsprings,
                         display=display,
                         **kwargs)

        # self.mating = NeighborBiasedMating(selection,
        #                                    crossover,
        #                                    mutation,
        #                                    repair=self.mating.repair,
        #                                    eliminate_duplicates=self.mating.eliminate_duplicates,
        #                                    n_max_iterations=self.mating.n_max_iterations)

        self.default_termination = SingleObjectiveDefaultTermination()
Example #16
0
    def __init__(
            self,
            pop_size=100,
            sampling=FloatRandomSampling(),
            selection=TournamentSelection(func_comp=comp_by_cv_and_fitness),
            crossover=SimulatedBinaryCrossover(prob=0.9, eta=3),
            mutation=PolynomialMutation(prob=None, eta=5),
            survival=FitnessSurvival(),
            eliminate_duplicates=True,
            n_offsprings=None,
            display=SingleObjectiveDisplay(),
            **kwargs):
        """

        Parameters
        ----------
        pop_size : {pop_size}
        sampling : {sampling}
        selection : {selection}
        crossover : {crossover}
        mutation : {mutation}
        eliminate_duplicates : {eliminate_duplicates}
        n_offsprings : {n_offsprings}

        """

        super().__init__(pop_size=pop_size,
                         sampling=sampling,
                         selection=selection,
                         crossover=crossover,
                         mutation=mutation,
                         survival=survival,
                         eliminate_duplicates=eliminate_duplicates,
                         n_offsprings=n_offsprings,
                         display=display,
                         **kwargs)

        self.default_termination = SingleObjectiveDefaultTermination()
Example #17
0
    def __init__(self,
                 pop_size=100,
                 sampling=FloatRandomSampling(),
                 selection=DES("rand"),
                 crossover=DEX(CR=0.7),
                 mutation=PM(eta=20),
                 survival=NichingSurvival(0.1, 0.01, 5, 4),
                 eliminate_duplicates=True,
                 n_offsprings=None,
                 display=SingleObjectiveDisplay(),
                 **kwargs):

        super().__init__(pop_size=pop_size,
                         sampling=sampling,
                         selection=selection,
                         crossover=crossover,
                         mutation=mutation,
                         survival=survival,
                         eliminate_duplicates=eliminate_duplicates,
                         n_offsprings=n_offsprings,
                         display=display,
                         **kwargs)

        self.default_termination = SingleObjectiveDefaultTermination()
Example #18
0
    def __init__(self,
                 X=None,
                 func_params=adaptive_params,
                 n_max_local_restarts=0,
                 criterion_local_restart=NelderAndMeadTermination(xtol=1e-2, ftol=1e-2),
                 display=SingleObjectiveDisplay(),
                 **kwargs):
        """

        Parameters
        ----------
        X : np.array or Population
            The initial point where the search should be based on or the complete initial simplex (number of
            dimensions plus 1 points). The population objective can be already evaluated with objective
            space values. If a numpy array is provided it is evaluated by the algorithm.
            By default it is None which means the search starts at a random point.

        func_params : func
            A function that returns the parameters alpha, beta, gamma, delta for the search. By default:

            >>>  def adaptive_params(problem):
            ...     n = problem.n_var
            ...
            ...     alpha = 1
            ...     beta = 1 + 2 / n
            ...     gamma = 0.75 - 1 / (2 * n)
            ...     delta = 1 - 1 / n
            ...     return alpha, beta, gamma, delta

            It can be overwritten if necessary.

        n_max_local_restarts : int
            This algorithm employs local restarts by starting with a new initial simplex. This can be turned of
            by setting it to 0.

        criterion_local_restart : Termination
            Provide a termination object which decides whether a local restart should be performed or not.

        """

        super().__init__(display=display, **kwargs)

        # the function to return the parameter
        self.func_params = func_params

        # the attributes for the simplex operations
        self.alpha, self.beta, self.gamma, self.delta = None, None, None, None

        # the scaling used for the initial simplex
        self.simplex_scaling = None

        # the initial point to be used to build the simplex
        self.x0 = X
        self.opt = None

        self.default_termination = NelderAndMeadTermination(xtol=1e-6, ftol=1e-6, n_max_iter=1e6, n_max_evals=1e6)

        # everything needed for local restarts
        self.n_max_local_restarts = n_max_local_restarts
        self.criterion_local_restart = criterion_local_restart

        # internal variables to keep track of restarts
        self.restarts_disabled = False
        self.restart_history = []
Example #19
0
    def __init__(self,
                 pop_size=100,
                 n_offsprings=None,
                 sampling=LHS(),
                 variant="DE/best/1/bin",
                 CR=0.5,
                 F=None,
                 dither="vector",
                 jitter=False,
                 mutation=NoMutation(),
                 survival=None,
                 display=SingleObjectiveDisplay(),
                 **kwargs):
        """

        Parameters
        ----------

        pop_size : {pop_size}

        sampling : {sampling}

        variant : {{DE/(rand|best)/1/(bin/exp)}}
         The different variants of DE to be used. DE/x/y/z where x how to select individuals to be pertubed,
         y the number of difference vector to be used and z the crossover type. One of the most common variant
         is DE/rand/1/bin.

        F : float
         The F to be used during the crossover.

        CR : float
         The probability the individual exchanges variable values from the donor vector.

        dither : {{'no', 'scalar', 'vector'}}
         One strategy to introduce adaptive weights (F) during one run. The option allows
         the same dither to be used in one iteration ('scalar') or a different one for
         each individual ('vector).

        jitter : bool
         Another strategy for adaptive weights (F). Here, only a very small value is added or
         subtracted to the F used for the crossover for each individual.

        """

        # parse the information from the string
        _, sel, n_diff, mut, = variant.split("/")
        n_diffs = int(n_diff)
        if "-to-" in variant:
            n_diffs += 1

        selection = DES(sel)

        crossover = DEX(prob=1.0,
                        n_diffs=n_diffs,
                        F=F,
                        CR=CR,
                        variant=mut,
                        dither=dither,
                        jitter=jitter)

        super().__init__(pop_size=pop_size,
                         n_offsprings=n_offsprings,
                         sampling=sampling,
                         selection=selection,
                         crossover=crossover,
                         mutation=mutation,
                         survival=survival,
                         display=display,
                         **kwargs)

        self.default_termination = SingleObjectiveDefaultTermination()