Complete Reference¶
pyfde¶
-
class
pyfde.
ClassicDE
(fitness, n_dim, n_pop=30, limits=(-1., 1.), batch=False, seed=None, minimize=False)¶ Bases:
object
Implements the classic differential evolution algorithm (DE/rand/1/bin). Holds the vector population, fitness evaluations, and the parameters.
Parameters: - fitness (function) –
Fitness evaluation function to be used. Must accept a double array as parameter and return the fitness evaluation as a float.
Alternatively (by using batch=True), it can be a function that accepts two parameters: a double[n_pop, n_dim] corresponding to the population to be evaluated and a double[n_pop], correponding to where the fitness evaluations must be written to.
- n_dim (int, > 0) – Number of dimensions
- n_pop (int, > 3) – Number of vectors in the population
- limits (tuple (min, max), list of tuples or double[n_dim][2]) – Limits for the search space dimension. If it is a tuple, then every dimension will have the same limits. If it is a list of tuples or an array, then each dimension will have the corresponding limit.
- batch (bool) – If the batch mode should be used.
- minimize (bool) – If the fitness function should be minimized. Default is minimize=False.
- seed (int, > 0) – Seed used to initialize the pseudo random number generator.
-
__call__
¶ Returns a generator that can be used for writing custom stopping conditions.
Parameters: n_it (int) – Number of iterations to perform before yielding the current solution Returns: Returns a generator that yields the current best solution and its fitness value Return type: generator
-
__iter__
¶ Iterates through every vector in the population
Returns: Returns a (vector, fitness) iterator Return type: iterator
-
best_k
(self, int k)¶ Returns the index of a random vector from the k vectors with the highest fitness. If k is equal to one, then the best vector index is returned deterministically.
The sorted indexes are cached to optimize the common case where this function can be called multiple times with the same fitness array.
Parameters: k (int) – Size of the group with the best fitness values Returns: Index of the choosen vector Return type: int
-
cr
¶ ‘double’ Crossover rate, in the [0., 1.] range
Type: cr
-
evaluate
(self, double[:, ::1] pop, double[::1] fit)¶ Evaluates a population, storing their fitness for later use.
Parameters: - pop (double[n_pop,n_dim]) – Population to evaluate
- fit (double[n_pop]) – Location to store the fitness evaluations
-
f
¶ ‘double’ Differential weight, in the [0., 2.] range
Type: f
-
generate_trial
(self)¶ Generates a new trial population by using the current vectors.
-
prepare
(self, new_pop=None)¶ Resets the solver internal state for a new computation.
Can be called to reuse an existing solver instance.
Parameters: new_pop (int or None) – New number of vectors in the population. If None, will use the current number of vectors.
-
run
(self, int n_it=1000)¶ Runs the optimization algorithm for a number of iterations.
Parameters: n_it (int) – Number of iterations to be performed Returns: Returns a tuple with the best solution and its fitness evaluation Return type: (double[n_dim], float)
-
selection
(self)¶ Merges the best trial vectors to the main population.
-
step
(self)¶ Performs one iteration of the optimization algorithm.
- fitness (function) –
-
class
pyfde.
JADE
(*args, **kwargs)¶ Bases:
pyfde.classicde.ClassicDE
Implementation of the adaptative DE algorithm proposed by Zhang, J. et al, JADE: Adaptive Differential Evolution with Optional External Archive.
See pyfde.ClassicDE constructor for the constructor parameters.
-
__call__
¶ Returns a generator that can be used for writing custom stopping conditions.
Parameters: n_it (int) – Number of iterations to perform before yielding the current solution Returns: Returns a generator that yields the current best solution and its fitness value Return type: generator
-
__iter__
¶ Iterates through every vector in the population
Returns: Returns a (vector, fitness) iterator Return type: iterator
-
best_k
(self, int k)¶ Returns the index of a random vector from the k vectors with the highest fitness. If k is equal to one, then the best vector index is returned deterministically.
The sorted indexes are cached to optimize the common case where this function can be called multiple times with the same fitness array.
Parameters: k (int) – Size of the group with the best fitness values Returns: Index of the choosen vector Return type: int
-
c
¶ ‘double’
Parameter between 0 and 1 that controls the rate of the adaptation of the CR and F parameters.
Type: c
-
cr
¶ ‘double’ Crossover rate, in the [0., 1.] range
Type: cr
-
evaluate
(self, double[:, ::1] pop, double[::1] fit)¶ Evaluates a population, storing their fitness for later use.
Parameters: - pop (double[n_pop,n_dim]) – Population to evaluate
- fit (double[n_pop]) – Location to store the fitness evaluations
-
f
¶ ‘double’ Differential weight, in the [0., 2.] range
Type: f
-
generate_trial
(self)¶ Generates a new trial population by using the current vectors.
This method differs from the base implementation in two points:
- The random vector selected to guide the optimization procedure is selected from the top p vectors (bias towards better fitness).
- The CR and F parameters are computed per vector, and are fluctuations around the mean CR and mean F adaptative parameters.
The original JADE algorithm can also be implemented with an archive, that can enhance the performance of the algorithm in high dimentional cases. This implementations does not includes this archive.
-
p
¶ ‘double’
Parameter between 0 and 1 that determined the top p of the population were the best vector will be selected to guide the optimization procedure.
Type: p
-
prepare
(self, new_pop=None)¶ Resets the solver internal state for a new computation. Can be called to reuse an existing solver instance.
Parameters: new_pop (int or None) – New number of vectors in the population. If None, will use the current number of vectors.
-
run
(self, int n_it=1000)¶ Runs the optimization algorithm for a number of iterations.
Parameters: n_it (int) – Number of iterations to be performed Returns: Returns a tuple with the best solution and its fitness evaluation Return type: (double[n_dim], float)
-
selection
(self)¶ Merges the best trial vectors to the main population.
-
step
(self)¶ Performs one iteration of the optimization algorithm.
-
pyfde.bench¶
Implements some common benchmark functions.
-
pyfde.bench.
rastrigin
(double[::1] p)¶ Rastrigin function.
Parameters: p (double[n_dim]) – Array with the coefficients Returns: Function value at p Return type: float
-
pyfde.bench.
schewefel
(double[::1] p)¶ Schewefel function
Parameters: p (double[n_dim]) – Array with the coefficients Returns: Function value at p Return type: float
-
pyfde.bench.
schewefel_batch
(double[:, ::1] pop, double[::1] fit)¶ Batch version of the Schewefel function.
Parameters: - pop (double[n_pop, n_dim]) – Array with the population to be evaluated
- fit (double[n_pop]) – Array to write the fitness evaluations to