ParticleSwarmOptimizer<T, TInput, TOutput>
Models & Types · AiDotNet.Optimizers
Implements a Particle Swarm Optimization algorithm for finding optimal solutions.
Inheritance: OptimizerBase<T, TInput, TOutput> → ParticleSwarmOptimizer<T, TInput, TOutput>
Implements: IDerivativeFreeFunctionOptimizer<T>
For Beginners
Particle Swarm Optimization is like a group of birds searching for food.
Imagine a flock of birds looking for the best food source in a field:
- Each bird is a “particle” in the swarm
- Each bird remembers where it personally found the most food
- The flock shares information about where the most food has been found overall
- Birds adjust their flight based on their own experience and what they learn from others
- Over time, the whole flock converges on the best food source
This approach is very effective for finding good solutions to complex problems where traditional methods might get stuck in suboptimal areas.
How It Works
Particle Swarm Optimization (PSO) is a population-based stochastic optimization technique inspired by the social behavior of birds flocking or fish schooling. The algorithm maintains a population (swarm) of candidate solutions (particles) that move around in the search space according to simple mathematical formulas that consider the particle’s position and velocity.
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Enums;
using AiDotNet.NeuralNetworks;
using AiDotNet.Optimizers;
using AiDotNet.Tensors.LinearAlgebra;
var rng = new Random(0);
var trainX = new Tensor<double>(new[] { 32, 8 });
var trainY = new Tensor<double>(new[] { 32, 2 });
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 8; j++) trainX[new[] { i, j }] = rng.NextDouble();
trainY[new[] { i, i % 2 }] = 1.0;
}
var model = new NeuralNetwork<double>(new NeuralNetworkArchitecture<double>(
inputFeatures: 8, numClasses: 2, complexity: NetworkComplexity.Simple));
var result = await new AiModelBuilder<double, Tensor<double>, Tensor<double>>()
.ConfigureModel(model)
.ConfigureOptimizer(new ParticleSwarmOptimizer<double, Tensor<double>, Tensor<double>>(model))
.ConfigureDataLoader(DataLoaders.FromTensors(trainX, trainY))
.BuildAsync();
Console.WriteLine("Trained with ParticleSwarmOptimizer.");
Constructors
| Constructor | Summary |
|---|---|
ParticleSwarmOptimizer(IFullModel<T, TInput, TOutput> model, ParticleSwarmOptimizationOptions<T, TInput, TOutput> options) |
Initializes a new instance of the ParticleSwarmOptimizer class with the specified options. |
Methods
| Method | Summary |
|---|---|
OptimizationAlgorithmOptions<T, TInput, TOutput> GetOptions() |
Gets the current options for this optimizer. |
OptimizationResult<T, TInput, TOutput> Optimize(OptimizationInputData<T, TInput, TOutput> inputData) |
Performs the particle swarm optimization to find the best solution for the given input data. |
ParticleSwarmOptimizer<T, TInput, TOutput> CreateForFunction(ParticleSwarmOptimizationOptions<T, TInput, TOutput> options) |
Creates a particle swarm optimizer for minimizing a plain function, with no model. |
Vector<T> Minimize(Vector<T> initialParameters, Func<Vector<T>, T> objective, int maxIterations, T tolerance) |