1.0.7 • Published 5 years ago

particle-swarm v1.0.7

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Particle-swarm.js

Particle-swarm.js is JavaScript implementation of a fully-informed particle swarm optimization algorithm. Image

Installing

With npm:

npm install --save particle-swarm

With yarn:

yarn add particle-swarm

Basic usage (with default parameters)

var createOptimizer = require('particle-swarm').default;

var optimizer = createOptimizer({
    maxVelocity:[4.05],
    minVelocity: [-4.05],
    maxPosition: [10.],
    minPosition: [-10.],
    populationSize: 30,
    numberOfDimensions: 1,
    maxIterations: 50,
    fitnessFunction: (x) => x*x,
});

var solution = optimizer.start();

Complex usage (with all parameters)

import createOptimizer from 'particle-swarm';

const optimizer = createOptimizer({
    useConstrictionFactor: true,
    maxVelocity:[4.05],
    minVelocity: [-4.05],
    maxPosition: [10.],
    minPosition: [-10.],
    populationSize: 30,
    numberOfDimensions: 1,
    maxIterations: 50,
    desiredFitness: 0,
    desiredPrecision: 1E-5,
    fitnessFunction: (x) => x*x,
    socialFactor: (iteration) => 2.05,
    individualFactor: (iteration) => 2.05,
    inertiaFactor: (iteration) => 1.,
    callbackFn: (meta) => console.log(meta.globalBestFitness),
});

const solution = optimizer.start();

Parameters

Required parameters

  • maxVelocity - max velocity of particle for each dimension
  • minVelocity - min velocity of particle for each dimension
  • maxPosition - max position of particle for each dimension
  • maxPosition - min position of particle for each dimension
  • populationSize - size of population, must be greater than zero
  • numberOfDimensions - number of dimensions, must be greater than zero
  • maxIterations - max number of iterations, must be greater than zero
  • fitnessFunction - function that evaluates each particle, algorithm is searching for position that gives smallest value of this function

Optional parameters

  • useConstrictionFactor - constriction factor prevents divergence of algorithm, false by default
  • randomFunction - function that returns random number from interval 0, 1, Math.random by default
  • desiredFitness - desired fitness algorithm should achieve, 0 by default
  • desiredPrecision - desired precision when comparing desired fitness and global best fitness, 1E-5 by default
  • socialFactor - function that calculates social factor for each iteration, 2.05 by default
  • individualFactor - function that calculates individual factor for each iteration, 2.05 by default
  • inertiaFactor - function that calculates individual factor for each iteration, 1 by default
  • callbackFn - function that is called after each iteration, can be used as a observer

Tips

  • Parameters maxVelocity, minVelocity, maxPosition and maxPosition must be arrays of length numberOfDimensions
  • Parameters socialFactor and individualFactor should return value of 2.05
  • Parameter inertiaFactor should have value of 1 in first iteration and decline in each iteration
  • Set useConstrictionFactor to true if you want to prevent divergence of algorithm
  • Velocity should be between 10 and 20 percent of space that is searched (e.g. searched space: -10, 10, velocity: -4,4)

Note: Please see finding global minimum of Rastring function example in demo file.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

1.0.7

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago