Function minimisation
The main functionality of ConsensusBasedX.jl is function minimisation via Consensus-Based Optimisation. It assumes you have defined a function f(x::AbstractVector) that takes a single vector argument x of length D = length(x).
For instance, if D = 2, you can minimise f by running:
minimise(f, D = 2)By default, minimise returns a Vector{Float64} of length D which contains the candidate to the global minimiser of f.
You must always provide D.
Full example
using ConsensusBasedX
f(x) = ConsensusBasedX.Ackley(x, shift = 1)
minimise(f, D = 2) # should be close to [1, 1]Using a config object
For more advanced usage, you will select several options. You can pass these as extra keyword arguments to minimise, or you can create a NamedTuple called config and pass that:
config = (; D = 2)
minimise(f, config)If you pass a Dict instead, it will be converted to a NamedTuple automatically.
This is a version of the full-code example above, using config instead:
Full example
using ConsensusBasedX
f(x) = ConsensusBasedX.Ackley(x, shift = 1)
config = (; D = 2, N = 20)
minimise(f, config) # should be close to [1, 1]Receiving extended output
It is possible to receive extended output from minimise by passing the option extended_output = true:
config = (; D = 2, extended_output = true)
minimise(f, config)For more details, see Extended output.
Maximisation
ConsensusBasedX.jl also defines maximise for convenience. If you call
maximise(f, D = 2)or
config = (; D = 2)
maximise(f, config)maximise will attempt to define g(x) = -f(x) and call minimise(g, config).
These are full-code examples using keywords
Full example
using ConsensusBasedX
f(x) = -ConsensusBasedX.Ackley(x, shift = 1)
maximise(f, D = 2) # should be close to [1, 1]or using config
Full example
using ConsensusBasedX
f(x) = -ConsensusBasedX.Ackley(x, shift = 1)
config = (; D = 2, N = 20)
maximise(f, config) # should be close to [1, 1]Method reference
ConsensusBasedX.maximise — Functionmaximise(f; keywords...)maximise(f, config::NamedTuple)Maximise the function f using Consensus-Based Optimisation.
Attempts to define x -> -f(x) and calls the minimise routine. This might be better handled directly by the user (see Maximisation).
See also minimise.
ConsensusBasedX.minimise — Functionminimise(f; keywords...)minimise(f, config::NamedTuple)Minimise the function f using Consensus-Based Optimisation (see Function minimisation).
You must specify the dimension D of the problem. Other parameters (e.g. the number of particles N or the number of ensembles M) can also be specified; see Summary of options.
Examples
minimise(f, D = 2)config = (; D = 2);
minimise(f, config)minimise(f, D = 2, N = 20)config = (; D = 2, N = 20);
minimise(f, config)