Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy > Class Template Reference

Class container for the particle swarm optimizer. More...

#include <ParticleSwarm.hh>

Inherits Optimist::Optimizer::Optimizer< Vector, ParticleSwarm< Vector >, true >.

Public Types

using VectorTrait = TypeTrait<Vector>
using Scalar = typename Vector::Scalar
using Particles = std::vector<Vector>
Public Types inherited from Optimist::Optimizer::Optimizer< Vector, ParticleSwarm< Vector >, true >
using Scalar
using Input
using Output
Public Types inherited from Optimist::SolverBase< Vector, TypeTrait< Vector >::Scalar, ParticleSwarm< Vector > >
using InputTrait
using OutputTrait
using Scalar
using FirstDerivative
using SecondDerivative

Public Member Functions

 ParticleSwarm ()
constexpr std::string name_impl () const
void randomize_particles (Particles &particles) const
void randomize_velocities (Particles &velocities) const
void evaluate_objective (const Particles &particles, Vector &fvals)
void maintainBounds (Matrix &particles) const
void calculateVelocities (const Matrix &particles, const Matrix &best_particles, const Integer gbest, const Integer iteration, Matrix &velocities)
Result _minimize (const Matrix &bounds, Matrix &particles)
 ParticleSwarmOptimization ()
void setMinParticleChange (const Scalar change=ParticleSwarm::SQRT_EPSILON)
void setMinFunctionChange (const Scalar change=ParticleSwarm::SQRT_EPSILON)
void setPhiParticles (const Scalar phip)
void setPhiGlobal (const Scalar phig)
void setMaxVelocity (const Scalar max_velocity)
void inertia_weight_strategy (const InertiaWeightStrategy &t_weightStrategy)
Result minimize (const Integer cnt)
Result minimize (const Integer cnt, const Vector &initGuess)
Result minimize (Matrix &particles)
void getRandomParticles (const Integer cnt, Matrix &particles)
Public Member Functions inherited from Optimist::Optimizer::Optimizer< Vector, ParticleSwarm< Vector >, true >
 Optimizer ()
constexpr std::string name () const
Integer gradient_evaluations () const
Integer max_gradient_evaluations () const
Integer hessian_evaluations () const
Integer max_hessian_evaluations () const
Public Member Functions inherited from Optimist::SolverBase< Vector, TypeTrait< Vector >::Scalar, ParticleSwarm< Vector > >
 SolverBase ()
void reset_bounds (const Integer n=InputTrait::IsDynamic ? 0 :InputTrait::Dimension)
const Vector & lower_bound () const
const Vector & upper_bound () const
void bounds (const Vector &t_lower_bound, const Vector &t_upper_bound)
constexpr Integer input_dimension () const
constexpr Integer output_dimension () const
Integer function_evaluations () const
void max_function_evaluations (const Integer t_max_function_evaluations)
Integer iterations () const
Integer max_iterations () const
Scalar alpha () const
Integer relaxations () const
Integer max_relaxations () const
Scalar tolerance () const
void verbose_mode (bool t_verbose)
void enable_verbose_mode ()
void disable_verbose_mode ()
void damped_mode (bool t_damped)
void enable_damped_mode ()
void disable_damped_mode ()
std::string task () const
bool converged () const
std::ostream & ostream () const
bool solve (FunctionLambda &&function, const Vector &x_ini, Vector &x_sol)
bool rootfind (const FunctionBase< FunctionInput, FunctionOutput, DerivedFunction > &function, const Vector &x_ini, Vector &x_sol)
bool optimize (const FunctionBase< FunctionInput, FunctionOutput, DerivedFunction > &function, const Vector &x_ini, Vector &x_sol)
constexpr std::string name () const

Static Public Attributes

static constexpr bool RequiresFunction {true}
static constexpr bool RequiresFirstDerivative {false}
static constexpr bool RequiresSecondDerivative {false}
Static Public Attributes inherited from Optimist::Optimizer::Optimizer< Vector, ParticleSwarm< Vector >, true >
static constexpr bool IsRootFinder
static constexpr bool IsOptimizer

Private Attributes

InertiaWeightStrategy m_weight_strategy
Scalar m_phi_p {SQRT_EPSILON}
Scalar m_phi_g {SQRT_EPSILON}
Scalar xeps_
Scalar feps_
std::function< Scalar()> this m_dice

Additional Inherited Members

Protected Member Functions inherited from Optimist::Optimizer::Optimizer< Vector, ParticleSwarm< Vector >, true >
bool evaluate_gradient (GradientLambda &&gradient, const Input &x, FirstDerivative &out)
bool evaluate_hessian (HessianLambda &&hessian, const Input &x, SecondDerivative &out)
bool solve (FunctionLambda &&function, const Input &x_ini, Output &x_sol)
Protected Member Functions inherited from Optimist::SolverBase< Vector, TypeTrait< Vector >::Scalar, ParticleSwarm< Vector > >
Integer first_derivative_evaluations () const
Integer max_first_derivative_evaluations () const
Integer second_derivative_evaluations () const
Integer max_second_derivative_evaluations () const
void reset_counters ()
bool evaluate_function (FunctionLambda &&function, const Vector &x, TypeTrait< Vector >::Scalar &out)
bool evaluate_first_derivative (FirstDerivativeLambda &&function, const Vector &x, FirstDerivative &out)
bool evaluate_second_derivative (SecondDerivativeLambda &&function, const Vector &x, SecondDerivative &out)
bool damp (FunctionLambda &&function, const Vector &x_old, const Vector &function_old, const Vector &step_old, Vector &x_new, Vector &function_new, Vector &step_new)
void header ()
void bottom ()
void info (Scalar residuals, const std::string &notes="-")
Protected Attributes inherited from Optimist::SolverBase< Vector, TypeTrait< Vector >::Scalar, ParticleSwarm< Vector > >
Vector m_lower_bound
Vector m_upper_bound
Integer m_function_evaluations
Integer m_first_derivative_evaluations
Integer m_second_derivative_evaluations
Integer m_max_function_evaluations
Integer m_max_first_derivative_evaluations
Integer m_max_second_derivative_evaluations
Integer m_iterations
Integer m_max_iterations
Scalar m_alpha
Integer m_relaxations
Integer m_max_relaxations
Scalar m_tolerance
bool m_verbose
bool m_damped
std::ostream * m_ostream
std::string m_task
bool m_converged

Detailed Description

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
requires TypeTrait<Vector>::IsEigen && (!TypeTrait<Vector>::IsFixed || TypeTrait<Vector>::Dimension > 0)
class Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >

The optimization process can be configured by providing an inertia weight strategy functor. The inertia weight functor determines the amount of velocity, which is maintained from the previous iterations. It has a huge effect on convergence speed and stability of the optimization.

Template Parameters
VectorEigen vector type.
InertiaWeightStrategyInertia weight strategy functor type.

Member Typedef Documentation

◆ Particles

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
using Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::Particles = std::vector<Vector>

Particles container type.

◆ Scalar

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
using Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::Scalar = typename Vector::Scalar

◆ VectorTrait

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
using Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::VectorTrait = TypeTrait<Vector>

Constructor & Destructor Documentation

◆ ParticleSwarm()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::ParticleSwarm ( )
inline

Class constructor for the ParticleSwarm solver.

Member Function Documentation

◆ _minimize()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Result Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::_minimize ( const Matrix & bounds,
Matrix & particles )
inline

◆ calculateVelocities()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::calculateVelocities ( const Matrix & particles,
const Matrix & best_particles,
const Integer gbest,
const Integer iteration,
Matrix & velocities )
inline

◆ evaluate_objective()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::evaluate_objective ( const Particles & particles,
Vector & fvals )
inline

◆ getRandomParticles()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::getRandomParticles ( const Integer cnt,
Matrix & particles )
inline

◆ inertia_weight_strategy()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::inertia_weight_strategy ( const InertiaWeightStrategy & t_weightStrategy)
inline

◆ maintainBounds()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::maintainBounds ( Matrix & particles) const
inline

◆ minimize() [1/3]

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Result Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::minimize ( const Integer cnt)
inline

Perform minimization with the given bounds and number of particels.

The swarm of particles will be drawn uniform randomly within the given bounds.

The bounds matrix has to have 2 rows and one column per dimension of particle. The first row holds the minimum value of the respective dimension and the second row holds the maximum value.

Parameters
bounds2xM matrix for bounds of M-dimensional particles
cntnumber of particles used for optimization

◆ minimize() [2/3]

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Result Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::minimize ( const Integer cnt,
const Vector & initGuess )
inline

Perform minimization with the given bounds, number of particels and initial guess.

The swarm of particles will be drawn uniform randomly within the given bounds.

The bounds matrix has to have 2 rows and one column per dimension of particle. The first row holds the minimum value of the respective dimension and the second row holds the maximum value.

The initial guess vector has to have the same length as the number of columns of the bounds. It will be included as one particle of the swarm.

Parameters
bounds2xM matrix for bounds of M-dimensional particles
cntnumber of particles used for optimization
initGuessinitial guess for a particle

◆ minimize() [3/3]

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Result Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::minimize ( Matrix & particles)
inline

Perform minimization with the given bounds and a pre-computed swarm of particles.

The bounds matrix has to have 2 rows and one column per dimension of particle. The first row holds the minimum value of the respective dimension and the second row holds the maximum value.

Parameters
bounds2xM matrix for bounds of M-dimensional particles
particlesinitial swarm used for optimization

◆ name_impl()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
std::string Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::name_impl ( ) const
inlineconstexpr

Get the ParticleSwarm's solver name.

Returns
The ParticleSwarm's solver name.

◆ ParticleSwarmOptimization()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::ParticleSwarmOptimization ( )
inline

◆ randomize_particles()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::randomize_particles ( Particles & particles) const
inline

Randomize the particles within the given bounds.

Parameters
[out]particlesMatrix containing the particles to be randomized.

◆ randomize_velocities()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::randomize_velocities ( Particles & velocities) const
inline

Randomize the velocities within the given bounds.

Parameters
[out]velocitiesMatrix containing the velocities to be randomized.

◆ setMaxVelocity()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::setMaxVelocity ( const Scalar max_velocity)
inline

Set an upper bound for the velocity of particles. A particle cannot move faster than this value, which may prevent divergence.

Parameters
[in]max_velocitymaximum velocity of a particle

◆ setMinFunctionChange()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::setMinFunctionChange ( const Scalar change = ParticleSwarm< Vector, InertiaWeightStrategy >::SQRT_EPSILON)
inline

Set the minimum average change of function values per iteration. If the average change of functions values falls below this value, the optimization terminates.

Parameters
changeminimum change of function values

◆ setMinParticleChange()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::setMinParticleChange ( const Scalar change = ParticleSwarm< Vector, InertiaWeightStrategy >::SQRT_EPSILON)
inline

Set the minimum average change of particles per iteration. If the average change of particles (input parameters) falls below this value, the optimization terminates.

Parameters
changeminimum change of input paramaters

◆ setPhiGlobal()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::setPhiGlobal ( const Scalar phig)
inline

Set the tendency of particles to move towards the global optimum found so far. The swarm maintains a collective memory of where it has visited the lowest function value so far. Increasing this value increases the particles' tendency to move towards that point.

Parameters
phigtendency to move towards collective optimum

◆ setPhiParticles()

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
void Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::setPhiParticles ( const Scalar phip)
inline

Set the tendency of particles to move towards their local optimum found so far. Each particle individually maintains a memory of where it has visited the lowest function value so far. Increasing this value increases the particles' tendency to move towards that point.

Parameters
phiptendency to move towards individual optimum

Member Data Documentation

◆ feps_

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Scalar Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::feps_
private

◆ m_dice

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
std::function<Scalar()> this Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::m_dice
private

◆ m_phi_g

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Scalar Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::m_phi_g {SQRT_EPSILON}
private

Scalar m_max_velocity; /**< Maximum velocity for each particle.

◆ m_phi_p

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Scalar Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::m_phi_p {SQRT_EPSILON}
private

Cognitive coefficient.

◆ m_weight_strategy

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
InertiaWeightStrategy Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::m_weight_strategy
private

Inertia weight strategy functor.

◆ RequiresFirstDerivative

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
bool Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::RequiresFirstDerivative {false}
staticconstexpr

◆ RequiresFunction

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
bool Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::RequiresFunction {true}
staticconstexpr

◆ RequiresSecondDerivative

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
bool Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::RequiresSecondDerivative {false}
staticconstexpr

◆ xeps_

template<typename Vector, typename InertiaWeightStrategy = ConstantWeight<typename Vector::Scalar>>
Scalar Optimist::Optimizer::ParticleSwarm< Vector, InertiaWeightStrategy >::xeps_
private

The documentation for this class was generated from the following file: