AntColonyOptimizer<T, TInput, TOutput>
Models & Types · AiDotNet.Optimizers
Implements the Ant Colony Optimization algorithm for solving optimization problems.
Inheritance: OptimizerBase<T, TInput, TOutput> → AntColonyOptimizer<T, TInput, TOutput>
Implements: IDerivativeFreeFunctionOptimizer<T>
For Beginners
Think of this algorithm as a group of ants searching for the best path to food. Each ant leaves a trail (pheromone) that other ants can follow. Over time, the best paths get stronger trails, leading to better solutions.
How It Works
Ant Colony Optimization is inspired by the behavior of ants in finding paths between their colony and food sources. It uses virtual “ants” to explore the solution space and find optimal solutions.
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 AntColonyOptimizer<double, Tensor<double>, Tensor<double>>(model))
.ConfigureDataLoader(DataLoaders.FromTensors(trainX, trainY))
.BuildAsync();
Console.WriteLine("Trained with AntColonyOptimizer.");
Constructors
| Constructor | Summary |
|---|---|
AntColonyOptimizer(IFullModel<T, TInput, TOutput> model, AntColonyOptimizationOptions<T, TInput, TOutput> options, IEngine engine) |
Initializes a new instance of the AntColonyOptimizer class. |
Methods
| Method | Summary |
|---|---|
AntColonyOptimizer<T, TInput, TOutput> CreateForFunction(AntColonyOptimizationOptions<T, TInput, TOutput> options) |
Creates an ant colony optimizer for minimizing a plain function, with no model attached. |
OptimizationAlgorithmOptions<T, TInput, TOutput> GetOptions() |
Gets the current options for the Ant Colony Optimization algorithm. |
OptimizationResult<T, TInput, TOutput> Optimize(OptimizationInputData<T, TInput, TOutput> inputData) |
Performs the main optimization process using the Ant Colony Optimization algorithm. |
Vector<T> Minimize(Vector<T> initialParameters, Func<Vector<T>, T> objective, int maxIterations, T tolerance) |