Ejemplo n.º 1
0
    def adapt_es_opts(self, opts):
        self.es.opts = opts
        parameter_opts = self.es.parameters.getParameterOpts()

        # __init__ of CustomizedES without new instance of ES
        # not a great solution, if package gets updates we should change this
        lambda_, eff_lambda, mu = self.es.calculateDependencies(
            opts, self.lambda_, self.mu
        )

        selector = Sel.pairwise if opts["selection"] == "pairwise" else Sel.best
        # This is done for safety reasons
        # Else offset and all_offspring may be None
        self.es.parameters.offset = np.column_stack(
            [ind.mutation_vector for ind in self.es.new_population]
        )
        self.es.parameters.all_offspring = np.column_stack(
            [ind.genotype for ind in self.es.new_population]
        )
        # Same here. Probably the representation space should be restricted
        self.es.parameters.tpa_result = -1

        def select(pop, new_pop, _, param):
            return selector(pop, new_pop, param)

        if opts["base-sampler"] == "quasi-sobol":
            sampler = Sam.QuasiGaussianSobolSampling(self.dim)
        elif opts["base-sampler"] == "quasi-halton" and Sam.halton_available:
            sampler = Sam.QuasiGaussianHaltonSampling(self.dim)
        else:
            sampler = Sam.GaussianSampling(self.dim)

        if opts["orthogonal"]:
            orth_lambda = eff_lambda
            if opts["mirrored"]:
                orth_lambda = max(orth_lambda // 2, 1)
            sampler = Sam.OrthogonalSampling(
                self.dim, lambda_=orth_lambda, base_sampler=sampler
            )

        if opts["mirrored"]:
            sampler = Sam.MirroredSampling(self.dim, base_sampler=sampler)

        if opts["sequential"] and opts["selection"] == "pairwise":
            parameter_opts["seq_cutoff"] = 2

        mutate = partial(
            Mut.CMAMutation, sampler=sampler, threshold_convergence=opts["threshold"]
        )

        self.es.mutate = mutate
        self.es.parameters = self.es.instantiateParameters(parameter_opts)
        self.es.seq_cutoff = self.es.parameters.mu_int * self.es.parameters.seq_cutoff
Ejemplo n.º 2
0
    def __init__(self,
                 n,
                 fitnessFunction,
                 budget,
                 mu=None,
                 lambda_=None,
                 elitist=False):
        parameters = Parameters(n, budget, mu, lambda_, elitist=elitist)
        population = [FloatIndividual(n) for _ in range(parameters.mu_int)]

        # Artificial init
        wcm = parameters.wcm
        for individual in population:
            individual.genotype = wcm

        # We use functions here to 'hide' the additional passing of parameters that are algorithm specific
        recombine = Rec.weighted
        mutate = partial(Mut.CMAMutation, sampler=Sam.GaussianSampling(n))

        def select(pop, new_pop, _, params):
            return Sel.best(pop, new_pop, params)

        mutateParameters = parameters.adaptCovarianceMatrix

        functions = {
            'recombine': recombine,
            'mutate': mutate,
            'select': select,
            'mutateParameters': mutateParameters,
        }

        super(CMAESOptimizer, self).__init__(population, fitnessFunction,
                                             budget, functions, parameters)
Ejemplo n.º 3
0
    def get_sampler(self, options):
        '''
        Method for getting a correct sampler 
        based on a dictionary of options.
        '''
        if options["base-sampler"] == 'quasi-sobol':
            sampler = Sampling.QuasiGaussianSobolSampling(self.parameters.n)
        elif options["base-sampler"] == 'quasi-halton' and Sampling.halton_available:
            sampler = Sampling.QuasiGaussianHaltonSampling(self.parameters.n)
        else:
            sampler = Sampling.GaussianSampling(self.parameters.n)

        if options["orthogonal"]:
            orth_lambda = self.parameters.eff_lambda
            if options['mirrored']:
                orth_lambda = max(orth_lambda // 2, 1)
            sampler = Sampling.OrthogonalSampling(self.parameters.n, lambda_=orth_lambda, base_sampler=sampler)

        if options['mirrored']:
            sampler = Sampling.MirroredSampling(self.parameters.n, base_sampler=sampler)
        return sampler
Ejemplo n.º 4
0
    def __init__(self, n, fitnessFunction, budget):

        parameters = Parameters(n, budget, 1, 1)
        population = [FloatIndividual(n)]

        # We use functions here to 'hide' the additional passing of parameters that are algorithm specific
        recombine = Rec.onePlusOne
        mutate = partial(Mut.addRandomOffset, sampler=Sam.GaussianSampling(n))
        select = Sel.onePlusOneSelection
        mutateParameters = parameters.oneFifthRule

        functions = {
            'recombine': recombine,
            'mutate': mutate,
            'select': select,
            'mutateParameters': mutateParameters,
        }

        super(OnePlusOneOptimizer,
              self).__init__(population, fitnessFunction, budget, functions,
                             parameters)
Ejemplo n.º 5
0
    def switchConfiguration(self, opts):
        selector = Sel.pairwise if opts["selection"] == "pairwise" else Sel.best

        def select(pop, new_pop, _, param):
            return selector(pop, new_pop, param)

        # Pick the lowest-level sampler
        if opts["base-sampler"] == "quasi-sobol":
            sampler = Sam.QuasiGaussianSobolSampling(self.es.n)
        elif opts["base-sampler"] == "quasi-halton" and Sam.halton_available:
            sampler = Sam.QuasiGaussianHaltonSampling(self.es.n)
        else:
            sampler = Sam.GaussianSampling(self.es.n)

        # Create an orthogonal sampler using the determined base_sampler
        if opts["orthogonal"]:
            orth_lambda = self.es.parameters.eff_lambda
            if opts["mirrored"]:
                orth_lambda = max(orth_lambda // 2, 1)
            sampler = Sam.OrthogonalSampling(
                self.es.n, lambda_=orth_lambda, base_sampler=sampler
            )

        # Create a mirrored sampler using the sampler (structure) chosen so far
        if opts["mirrored"]:
            sampler = Sam.MirroredSampling(self.es.n, base_sampler=sampler)

        parameter_opts = {
            "weights_option": opts["weights_option"],
            "active": opts["active"],
            "elitist": opts["elitist"],
            "sequential": opts["sequential"],
            "tpa": opts["tpa"],
            "local_restart": opts["ipop"],
        }

        # In case of pairwise selection, sequential evaluation may only stop after 2mu instead of mu individuals

        if opts["sequential"] and opts["selection"] == "pairwise":
            parameter_opts["seq_cutoff"] = 2
            self.es.parameters.seq_cutoff = 2

        # Init all individuals of the first population at the same random point in the search space

        # We use functions/partials here to 'hide' the additional passing of parameters that are algorithm specific
        recombine = Rec.weighted
        mutate = partial(
            Mut.CMAMutation, sampler=sampler, threshold_convergence=opts["threshold"]
        )

        functions = {
            "recombine": recombine,
            "mutate": mutate,
            "select": select,
            # 'mutateParameters': None
        }
        self.setConfigurationParameters(functions, parameter_opts)
        lambda_, eff_lambda, mu = self.es.calculateDependencies(opts, None, None)
        self.es.parameters.lambda_ = lambda_
        self.es.parameters.eff_lambda = eff_lambda
        self.es.parameters.mu = mu
        self.es.parameters.weights = self.es.parameters.getWeights(
            self.es.parameters.weights_option
        )
        self.es.parameters.mu_eff = 1 / np.sum(np.square(self.es.parameters.weights))
        mu_eff = self.es.parameters.mu_eff  # Local copy
        n = self.es.parameters.n
        self.es.parameters.c_sigma = (mu_eff + 2) / (mu_eff + n + 5)
        self.es.parameters.c_c = (4 + mu_eff / n) / (n + 4 + 2 * mu_eff / n)
        self.es.parameters.c_1 = 2 / ((n + 1.3) ** 2 + mu_eff)
        self.es.parameters.c_mu = min(
            1 - self.es.parameters.c_1,
            self.es.parameters.alpha_mu
            * (
                (mu_eff - 2 + 1 / mu_eff)
                / ((n + 2) ** 2 + float(self.es.parameters.alpha_mu) * mu_eff / 2)
            ),
        )
        self.es.parameters.damps = (
            1
            + 2 * np.max([0, np.sqrt((mu_eff - 1) / (n + 1)) - 1])
            + self.es.parameters.c_sigma
        )
        self.es.seq_cutoff = self.es.parameters.mu_int * self.es.parameters.seq_cutoff
Ejemplo n.º 6
0
    def __init__(self,
                 n,
                 fitnessFunction,
                 budget,
                 mu=None,
                 lambda_=None,
                 opts=None,
                 values=None):

        if opts is None:
            opts = dict()
        self.addDefaults(opts)

        self.n = n
        l_bound = ones((n, 1)) * -5
        u_bound = ones((n, 1)) * 5

        lambda_, eff_lambda, mu = self.calculateDependencies(opts, lambda_, mu)

        selector = Sel.pairwise if opts['selection'] == 'pairwise' else Sel.best

        def select(pop, new_pop, _, param):
            return selector(pop, new_pop, param)

        # Pick the lowest-level sampler
        if opts['base-sampler'] == 'quasi-sobol':
            sampler = Sam.QuasiGaussianSobolSampling(n)
        elif opts['base-sampler'] == 'quasi-halton' and Sam.halton_available:
            sampler = Sam.QuasiGaussianHaltonSampling(n)
        else:
            sampler = Sam.GaussianSampling(n)

        # Create an orthogonal sampler using the determined base_sampler
        if opts['orthogonal']:
            orth_lambda = eff_lambda
            if opts['mirrored']:
                orth_lambda = max(orth_lambda // 2, 1)
            sampler = Sam.OrthogonalSampling(n,
                                             lambda_=orth_lambda,
                                             base_sampler=sampler)

        # Create a mirrored sampler using the sampler (structure) chosen so far
        if opts['mirrored']:
            sampler = Sam.MirroredSampling(n, base_sampler=sampler)

        parameter_opts = {
            'n': n,
            'budget': budget,
            'mu': mu,
            'lambda_': lambda_,
            'u_bound': u_bound,
            'l_bound': l_bound,
            'weights_option': opts['weights_option'],
            'active': opts['active'],
            'elitist': opts['elitist'],
            'sequential': opts['sequential'],
            'tpa': opts['tpa'],
            'local_restart': opts['ipop'],
            'values': values,
        }

        # In case of pairwise selection, sequential evaluation may only stop after 2mu instead of mu individuals
        mu_int = int(1 + floor(mu * (eff_lambda - 1)))
        if opts['sequential'] and opts['selection'] == 'pairwise':
            parameter_opts['seq_cutoff'] = 2
        population = [FloatIndividual(n) for _ in range(mu_int)]

        # Init all individuals of the first population at the same random point in the search space
        wcm = (np.random.randn(n, 1) * (u_bound - l_bound)) + l_bound
        parameter_opts['wcm'] = wcm
        for individual in population:
            individual.genotype = copy(wcm)

        # We use functions/partials here to 'hide' the additional passing of parameters that are algorithm specific
        recombine = Rec.weighted
        mutate = partial(Mut.CMAMutation,
                         sampler=sampler,
                         threshold_convergence=opts['threshold'])

        functions = {
            'recombine': recombine,
            'mutate': mutate,
            'select': select,
            'mutateParameters': None
        }

        super(CustomizedES, self).__init__(population, fitnessFunction, budget,
                                           functions, parameter_opts)