GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - Rookfighter/pso-cpp: A single header-only C++ library for particle swarm optimization.

BranchesTags
Open more actions menu

Latest commit

 

History

72 Commits
72 Commits

Folders and files

NameName
Last commit message
Last commit date
cmake
cmake
 
 
dep
dep
 
 
doc
doc
 
 
examples
examples
 
 
include
include
 
 
test
test
 
 
.appveyor.yml
.appveyor.yml
 
 
.gitignore
.gitignore
 
 
.gitmodules
.gitmodules
 
 
.travis.yml
.travis.yml
 
 
CMakeLists.txt
CMakeLists.txt
 
 
LICENSE.txt
LICENSE.txt
 
 
README.md
README.md
 
 

Repository files navigation

pso-cpp

Cpp11 License Travis Status Appveyer Status

pso-cpp is a header-only C++ library for particle swarm optimization using the Eigen3 linear algebra library.

Install

Simply copy the header file into your project or install it using the CMake build system by typing

cd path/to/repo
mkdir build
cd build
cmake ..
make install

The library requires Eigen3 to be installed on your system. In Debian based systems you can simply type

apt-get install libeigen3-dev

Make sure Eigen3 can be found by your build system.

You can use the CMake Find module in cmake/ to find the installed header.

Usage

There are three steps to use gradient-descent-cpp:

  • Implement your objective function as functor
  • Instantiate the particle swarm optimizer
  • Choose your parameters
#include <psocpp.h>

// Implement an objective functor.
struct Ackley
{
    static double pi()
    { return 3.141592653589; }

    Ackley()
    { }

    template<typename Derived>
    double operator()(const Eigen::MatrixBase<Derived> &xval) const
    {
        assert(xval.size() == 2);
        double x = xval(0);
        double y = xval(1);
        return -20.0 * std::exp(-0.2 * std::sqrt(0.5 * (x * x + y * y))) -
            std::exp(0.5 * (std::cos(2 * pi() * x) + std::cos(2 * pi() * y))) +
            std::exp(1) + 20.0;
    }
};

int main()
{
    // Create optimizer object with Ackley functor as objective.
    //
    // You can specify an InertiaWeightStrategy functor as
    // template parameter. There are ConstantWeight, LinearDecrease,
    // ExponentialDecrease1, ExponentialDecrease2, ExponentialDecrease3
    // available. (Default is ConstantWeight)
    //
    // You can additionally specify a Callback functor as template parameter.
    pso::ParticleSwarmOptimization<double, Ackley,
        pso::ExponentialDecrease2<double>> optimizer;

    // Set number of iterations as stop criterion.
    // Set it to 0 or negative for infinite iterations (default is 0).
    optimizer.setMaxIterations(100);

    // Set the minimum change of the x-values (particles) (default is 1e-6).
    // If the change in the current iteration is lower than this value, then
    // the optimizer stops minimizing.
    optimizer.setMinParticleChange(1e-6);

    // Set the minimum change of the function values (default is 1e-6).
    // If the change in the current iteration is lower than this value, then
    // the optimizer stops minimizing.
    optimizer.setMinFunctionChange(1e-6);

    // Set the number of threads used for evaluation (OpenMP only).
    // Set it to 0 or negative for auto detection (default is 1).
    optimizer.setThreads(2);

    // Turn verbosity on, so the optimizer prints status updates after each
    // iteration.
    optimizer.setVerbosity(2);

    // Set the bounds in which the optimizer should search.
    // Each column vector defines the (min, max) for each dimension  of the
    // particles.
    Eigen::MatrixXd bounds(2, 2);
    bounds << -5, -5,
               5,  5;

    // start the optimization with a particle count
    auto result = optimizer.minimize(bounds, 40);

    std::cout << "Done! Converged: " << (result.converged ? "true" : "false")
        << " Iterations: " << result.iterations << std::endl;

    // do something with final function value
    std::cout << "Final fval: " << result.fval << std::endl;

    // do something with final x-value
    std::cout << "Final xval: " << result.xval.transpose() << std::endl;

    return 0;
}

About

A single header-only C++ library for particle swarm optimization.

Resources

Readme
MIT license
Activity

Stars

22 stars

Watchers

1 watching

Forks

4 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.