Optimizer using Cross Entropy¶
CrossEntropyOptimizer¶
-
class
l2l.optimizers.crossentropy.optimizer.
CrossEntropyOptimizer
(traj, optimizee_create_individual, optimizee_fitness_weights, parameters, optimizee_bounding_func=None)[source]¶ Bases:
l2l.optimizers.optimizer.Optimizer
Class for a generic cross entropy optimizer. In the pseudo code the algorithm does:
- For n iterations do:
Sample individuals from distribution
evaluate individuals and get fitness
pick rho * pop_size number of elite individuals
Out of the remaining non-elite individuals, select them using a simulated-annealing style selection based on the difference between their fitness and the 1-rho quantile (gamma) fitness, and the current temperature
Fit the distribution family to the new elite individuals by minimizing cross entropy. The distribution fitting is smoothed to prevent premature convergence to local minima. A weight equal to the smoothing parameter is assigned to the previous parameters when smoothing.
return final distribution parameters. (The final distribution parameters contain information regarding the location of the maxima)
- Parameters
traj (Trajectory) – Use this trajectory to store the parameters of the specific runs. The parameters should be initialized based on the values in parameters
optimizee_create_individual – Function that creates a new individual. All parameters of the Individual-Dict returned should be of numpy.float64 type
optimizee_fitness_weights – Fitness weights. The fitness returned by the Optimizee is multiplied by these values (one for each element of the fitness vector)
parameters – Instance of
namedtuple()
CrossEntropyParameters
containing the parameters needed by the Optimizer
-
post_process
(traj, fitnesses_results)[source]¶ See
post_process()
CrossEntropyParameters¶
-
class
l2l.optimizers.crossentropy.optimizer.
CrossEntropyParameters
(pop_size, rho, smoothing, temp_decay, n_iteration, distribution, stop_criterion, seed)¶ Bases:
tuple
- Parameters
pop_size – Minimal number of individuals per simulation.
rho – Fraction of solutions to be considered elite in each iteration.
smoothing –
This is a factor between 0 and 1 that determines the weight assigned to the previous distribution parameters while calculating the new distribution parameters. The smoothing is done as a linear combination of the optimal parameters for the current data, and the previous distribution as follows:
new_params = smoothing * old_params + (1 - smoothing) * optimal_new_params
temp_decay – This parameter is the factor (necessarily between 0 and 1) by which the temperature decays each generation. To see the use of temperature, look at the documentation of
CrossEntropyOptimizer
n_iteration – Number of iterations to perform
distribution – Distribution object to use. Has to implement a fit and sample function. Should be one of
Gaussian
,NoisyGaussian
,BayesianGaussianMixture
,NoisyBayesianGaussianMixture
stop_criterion – (Optional) Stop if this fitness is reached.
seed – The random seed used to sample and fit the distribution.
CrossEntropyOptimizer
uses a random generator seeded with this seed.
-
property
distribution
¶
-
property
n_iteration
¶
-
property
pop_size
¶
-
property
rho
¶
-
property
seed
¶
-
property
smoothing
¶
-
property
stop_criterion
¶
-
property
temp_decay
¶
Distributions¶
-
class
l2l.optimizers.crossentropy.distribution.
Distribution
[source]¶ Bases:
object
Generic base for a distribution. Needs to implement the functions fit and sample.
-
abstract
init_random_state
(random_state)[source]¶ Used to initialize the random number generator which is used to fit/sample data. Note that if the random_state is already set, this raises an AssertionError. The reason this is not a part of the constructor is that the distribution random state must be initialized only by the optimizer and not in the main function (where it is constructed). It is essential to call this function before using the distribution
- Parameters
random_state – An instance of class:numpy.random.RandomState
-
abstract
fit
(data_list)[source]¶ This function fits the distributions parameters to the given samples in maximum likelihood fashion.
- Parameters
data_list – A list or array of individuals to fit to.
- Return dict
a dict describing the current parametrization
-
abstract
-
class
l2l.optimizers.crossentropy.distribution.
Gaussian
[source]¶ Bases:
l2l.optimizers.crossentropy.distribution.Distribution
Gaussian distribution.
-
init_random_state
(random_state)[source]¶ Used to initialize the random number generator which is used to fit/sample data. Note that if the random_state is already set, this raises an AssertionError. The reason this is not a part of the constructor is that the distribution random state must be initialized only by the optimizer and not in the main function (where it is constructed). It is essential to call this function before using the distribution
- Parameters
random_state – An instance of class:numpy.random.RandomState
-
fit
(data_list, smooth_update=0)[source]¶ Fit a gaussian distribution to the given data
- Parameters
data_list – list or numpy array with individuals as rows
smooth_update – determines to which extent the new samples account for the new distribution. default is 0 -> old parameters are fully discarded
- Return dict
specifying current parametrization
-
-
class
l2l.optimizers.crossentropy.distribution.
NoisyGaussian
(noise_magnitude=1.0, coordinate_scale=None, noise_decay=0.95)[source]¶ Bases:
l2l.optimizers.crossentropy.distribution.Gaussian
Additive Noisy Gaussian distribution. The initialization of its noise components happens during the first fit where the magnitude of the noise in each diagonalized component is estimated.
- Parameters
noise_magnitude – scalar factor that affects the magnitude of noise applied on the distribution parameters
coordinate_scale – This should be a vector representing the scaling of the coordinates. The noise applied to each coordinate i is noise_magnitude*coordinate_scale[i]
noise_decay – Multiplicative decay of the noise components
-
fit
(data_list, smooth_update=0)[source]¶ Fits the parameters to the given data (see
Gaussian
) and additionally adds noise in form of variance to the covariance matrix. Also, the noise is decayed after each step- Parameters
data_list – Data to be fitted to
smooth_update – Smooth the parameter update with regard to the previous configuration
- Return dict
describing parameter configuration
-
class
l2l.optimizers.crossentropy.distribution.
BayesianGaussianMixture
(n_components=2, **kwargs)[source]¶ Bases:
l2l.optimizers.crossentropy.distribution.Distribution
BayesianGaussianMixture from sklearn
Unlike normal Gaussian mixture, the algorithm has tendency to set the weights of non present modes close to zero. Meaning that it effectively inferences the number of active modes present in the given data.
- Parameters
n_components – components of the mixture model
kwargs – Additional arguments that get passed on to
sklearn.mixture.BayesianGaussianMixture
-
init_random_state
(random_state)[source]¶ Used to initialize the random number generator which is used to fit/sample data. Note that if the random_state is already set, this raises an AssertionError. The reason this is not a part of the constructor is that the distribution random state must be initialized only by the optimizer and not in the main function (where it is constructed). It is essential to call this function before using the distribution
- Parameters
random_state – An instance of class:numpy.random.RandomState
-
class
l2l.optimizers.crossentropy.distribution.
NoisyBayesianGaussianMixture
(n_components, noise_magnitude=1.0, coordinate_scale=None, noise_decay=0.95, **kwargs)[source]¶ Bases:
l2l.optimizers.crossentropy.distribution.BayesianGaussianMixture
NoisyBayesianGaussianMixture is basically the same as BayesianGaussianMixture but superimposed with noise
- Parameters
n_components – number of components in the mixture model
noise_magnitude – scalar factor that affects the magnitude of noise applied on the fitted distribution parameters+
coordinate_scale –
This should be a vector representing the scaling of the coordinates. The noise applied to each coordinate i is
noise_magnitude * coordinate_scale[i]
Defaults to 1 for each coordinate.
noise_decay – factor that will decay the additive noise
kwargs – additional arguments that get passed on to
BayesianGaussianMixture
distribution