particle-swarm
v1.0.7
Published
Fully informed particle swarm optimization library
Downloads
22
Maintainers
Readme
Particle-swarm.js
Particle-swarm.js is JavaScript implementation of a fully-informed particle swarm optimization algorithm.
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 dimensionminVelocity
- min velocity of particle for each dimensionmaxPosition
- max position of particle for each dimensionmaxPosition
- min position of particle for each dimensionpopulationSize
- size of population, must be greater than zeronumberOfDimensions
- number of dimensions, must be greater than zeromaxIterations
- max number of iterations, must be greater than zerofitnessFunction
- 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 defaultrandomFunction
- function that returns random number from interval [0, 1], Math.random by defaultdesiredFitness
- desired fitness algorithm should achieve, 0 by defaultdesiredPrecision
- desired precision when comparing desired fitness and global best fitness, 1E-5 by defaultsocialFactor
- function that calculates social factor for each iteration, 2.05 by defaultindividualFactor
- function that calculates individual factor for each iteration, 2.05 by defaultinertiaFactor
- function that calculates individual factor for each iteration, 1 by defaultcallbackFn
- 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