Development
If you are contributing a new algorithm to this package, you need to know an internal API which allows to add a new algorithm without considerable changes to overall structure of the package.
Adding an algorithm
If you're contributing a new algorithm, you shouldn't need to touch any of the code in src/api/optimize.jl. You should rather add a file named (solver is the name of the solver) algo.jl
in src, and make sure that you define an optimizer parameters and state types, initial_population
that initializes a population of individual
objects, a state type that holds all variables that are (re)used throughout the iterative procedure, an initial_state
that initializes such a state, and an update_state!
method that does the actual work.
Algorithm
Every optimization algorithm have to implement an algorithm parameters type derived from AbstractOptimizer
type, e.g. struct Algo <: AbstractOptimizer end
, with appropriate fields, a default constructor with a keyword for each field.
Function initial_state
returns an initial state for the algorithm, see State section. Function update_state!
returns a Bool
value. If the state update is successfully completed then the function returns false
, otherwise true
.
Evolutionary.AbstractOptimizer
— TypeAbstract evolutionary optimizer algorithm
Evolutionary.initial_state
— FunctionInitialization of ES algorithm state
Initialization of CMA-ES algorithm state
Initialization of GA algorithm state
Initialization of DE algorithm state
Initialization of GP algorithm state
State
Every optimization algorithm have to implement a state type derived from AbstractOptimizerState
type, e.g. struct AlgoState <: AbstractOptimizerState end
. All derived types should implement value
and minimizer
functions
Evolutionary.AbstractOptimizerState
— TypeAbstract type for defining an optimizer state
Every algorithm have to implement a state type derived from this abstract type.
NLSolversBase.value
— Methodvalue(state)
Returns a minimum value of the current state
.
Evolutionary.minimizer
— Methodvalue(state)
Returns a minimizer object in the current state
.
Evolutionary.terminate
— Methodterminate(state)
Returns true
if the state
requires early termination.
Population
The evolutionary algorithms require a collection of individuals, population, which the algorithm constantly modifies. The population collection type must be derived from the AbstractVector
type. Function initial_population
is used for implementing a strategy of population collection initialization.
The initial_population
must accept two parameters:
method
, an algorithm object derived fromAbstractOptimizer
typeindividual
, a description of an individual template used to create the population
Following population initialization strategies are available:
Evolutionary.initial_population
— Functioninitial_population(method, individual::AbstractVector)
Initialize population by replicating the inividual
vector.
initial_population(method, individuals::AbstractVector{<:AbstractVector})
Initialize population from the collection of inividuals
vectors.
initial_population(method, individual::Function)
Initialize population from the inividual
function which returns an individual object.
initial_population(method, individual::AbstractMatrix)
Initialize population by replicating the inividual
matrix.
initial_population(method, bounds::ConstraintBounds)
Initialize a random population within the individual bounds
.
initial_population(m::TreeGP, expr::{Expr,Nothing}=nothing)
Initialize a random population of expressions derived from expr
.
Constraints
All constraints derived from the AbstractConstraints
abstract type. Usually the derived type wraps a ConstraintBounds
object, so the
Following methods can be overridden for the derived types:
Missing docstring for value(::AbstractConstraints, ::AbstractObjective, x)
. Check Documenter's build log for details.
NLSolversBase.value
— Methodconstraints(c::AbstractConstraints, f::AbstractObjective, x)
Return a values of constraints for a variable x
given the set of constraints object c
.
Following auxillary functions are available for every derived type of AbstractConstraints
.
Missing docstring for isfeasible(::AbstractConstraints, x)
. Check Documenter's build log for details.
Package provides following constrains implementations.
Evolutionary.NoConstraints
— TypeType for an empty set of constratins
Evolutionary.BoxConstraints
— TypeBox constraints
Evolutionary.PenaltyConstraints
— TypeThis type encodes constraints as a following additional penalty for an objective function:
$p(x) = \sum^n_{i=1} r_i max(0, g_i(x))^2$
where $g_i(x)$ is an inequality constraint. The equality constraints $h_i(x) = 0$ are transformed to inequality constraints as follows:
$h(x) - \epsilon \leq 0$
Evolutionary.WorstFitnessConstraints
— TypeThis type encodes constraints as a following additional penalty for an objective function:
$p(x) = f_{worst} + \sum^n_{i=1} |g_i(x)|$
if $x$ is not feasible, otherwise no penalty is applied.
Evolutionary.MixedTypePenaltyConstraints
— TypeThis type provides an additional type constraints on the varaibles required for mixed integer optimization problmes.