← Previous: Numerical Differentiation | Back to Index | Next: Root Finding →
Optimization is the process of finding the parameter set that minimizes (or maximizes) an objective function. The Numerics library provides a comprehensive suite of local and global optimization algorithms for both unconstrained and constrained problems. These methods are essential for parameter estimation, model calibration, machine learning, and engineering design optimization.
| Method | Type | Best For | Requires Derivatives |
|---|---|---|---|
| Local Methods | |||
| BFGS | Local | Smooth, differentiable functions | No (numerical) |
| L-BFGS-B | Local | Smooth functions with simple bounds, many parameters | No (numerical) |
| Nelder-Mead | Local | General purpose, robust | No |
| Powell | Local | Smooth functions without derivatives | No |
| Golden Section | Local | 1D problems | No |
| Brent Search | Local | 1D problems, smooth functions | No |
| Gradient Descent | Local | Large-scale, smooth problems | No (numerical) |
| ADAM | Local | Machine learning, stochastic | No (numerical) |
| Global Methods | |||
| Differential Evolution | Global | Multimodal, robust | No |
| Particle Swarm | Global | Multimodal, fast convergence | No |
| Shuffled Complex Evolution | Global | Hydrological calibration | No |
| Simulated Annealing | Global | Discrete/continuous, multimodal | No |
| Multi-Start | Global | Combines local search with random starts | No |
| MLSL | Global | Smooth multimodal functions | Requires local solver |
| Dynamically Dimensioned Search | Global | Expensive simulation calibration, fixed budget | No |
| CMA-ES | Global | Correlated, badly scaled continuous problems | No |
| Constrained | |||
| Augmented Lagrangian | Constrained | Equality and inequality constraints | No |
| Discrete and multi-objective | |||
| Dynamic Programming | Exact | Staged decisions, reservoir operation rules | No |
| Knapsack | Exact | Budget-constrained project selection | No |
| Pareto front utilities | Analysis | Non-dominated sorting, hypervolume | No |
An optimization problem seeks to find:
subject to:
where
For maximization problems, minimize Maximize() method.
All optimizers in Numerics inherit from the Optimizer base class and share a common interface.
MaxIterations: Maximum iterations allowed (default = 10,000)MaxFunctionEvaluations: Maximum function evaluations (default = int.MaxValue)AbsoluteTolerance: Absolute convergence tolerance (default = 1E-8)RelativeTolerance: Relative convergence tolerance (default = 1E-8)ReportFailure: Rethrow an exception raised by the objective or the algorithm (default = true). ReachingMaxIterationsorMaxFunctionEvaluationsnever throws fromMinimize()orMaximize(); it is reported throughStatusonlyRecordTraces: Save optimization trace (default = true)ComputeHessian: Compute Hessian at solution (default = true)CancellationToken: Stop the run early; the run ends withOptimizationStatus.Cancelledand keeps its best point (default = none)Progress,ProgressInterval: Receive anOptimizationProgresssnapshot everyProgressIntervalevaluations and at the end (default = none, 100)
BestParameterSet: Optimal solution (Values, Fitness)Iterations: Number of iterations performedFunctionEvaluations: Number of function evaluationsStatus: Optimization status (Success, Failure, etc.)ParameterSetTrace: Read-only trace of the best-so-far parameter set and fitness at every function evaluation (entries recorded between improvements share one values array)Hessian: Numerically differentiated Hessian matrix (if computed)
-
Minimize(): Minimize the objective function -
Maximize(): Maximize the objective function (minimizes $-f(\mathbf{x})$)
After Maximize(), BestParameterSet.Fitness and every trace entry hold the value the solver minimized, which is -BestParameterSet.Fitness. The Hessian is that of
var brent = new BrentSearch(x => -(x - 1) * (x - 1) + 4, -5, 5);
brent.Maximize();
Console.WriteLine($"x = {brent.BestParameterSet.Values[0]:F4}, f(x) = {-brent.BestParameterSet.Fitness:F4}"); // x = 1.0000, f(x) = 4.0000Long calibrations can report progress and be stopped. The token is checked at every function evaluation, including the evaluations of a local search run inside a global one. A cancelled run does not throw: it keeps the best point found so far and ends with OptimizationStatus.Cancelled. A NaN objective value is ranked as the worst possible fitness (positive infinity), so a point where the objective is undefined is always replaced.
using System.Threading;
using Numerics.Mathematics.Optimization;
double Sphere(double[] x) => x[0] * x[0] + x[1] * x[1] + x[2] * x[2];
using var stop = new CancellationTokenSource();
var de = new DifferentialEvolution(Sphere, 3, new[] { -5.0, -5, -5 }, new[] { 5.0, 5, 5 })
{
CancellationToken = stop.Token,
ProgressInterval = 500,
Progress = new Progress<OptimizationProgress>(p =>
Console.WriteLine($"{p.FunctionEvaluations} evaluations, best {p.BestFitness:E3}"))
};
de.Minimize();
Console.WriteLine($"Status: {de.Status}");Progress<T> posts its callback to the thread pool or the calling synchronization context; implement IProgress<OptimizationProgress> directly for a synchronous callback. For AugmentedLagrange, also give the inner optimizer the token, because it evaluates the penalized objective itself.
Local optimization methods find the nearest local minimum from a starting point. They are fast and efficient for smooth, unimodal functions but may get trapped in local minima for multimodal problems.
BFGS is a quasi-Newton method that builds an approximation to the Hessian matrix using gradient information [1]. It's one of the most effective methods for smooth, unconstrained optimization.
using Numerics.Mathematics.Optimization;
// Rosenbrock function: f(x,y) = (1-x)² + 100(y-x²)²
double Rosenbrock(double[] x)
{
return Math.Pow(1 - x[0], 2) + 100 * Math.Pow(x[1] - x[0] * x[0], 2);
}
// Its analytic gradient
double[] RosenbrockGradient(double[] x)
{
return new[] { -2 * (1 - x[0]) - 400 * x[0] * (x[1] - x[0] * x[0]), 200 * (x[1] - x[0] * x[0]) };
}
// Initial guess
var initial = new double[] { -1.2, 1.0 };
var lower = new double[] { -2, -2 };
var upper = new double[] { 2, 2 };
// Create and configure optimizer
var bfgs = new BFGS(Rosenbrock, 2, initial, lower, upper, RosenbrockGradient);
bfgs.RelativeTolerance = 1e-8;
bfgs.MaxIterations = 1000;
// Minimize
bfgs.Minimize();
// Results
Console.WriteLine($"Optimal solution: x = {bfgs.BestParameterSet.Values[0]:F6}, y = {bfgs.BestParameterSet.Values[1]:F6}");
Console.WriteLine($"Minimum value: {bfgs.BestParameterSet.Fitness:F10}");
Console.WriteLine($"Iterations: {bfgs.Iterations}");
Console.WriteLine($"Function evaluations: {bfgs.FunctionEvaluations}");
Console.WriteLine($"Status: {bfgs.Status}");Supply the gradient when it is available. A finite-difference gradient has an error near AbsoluteTolerance, so without the gradient this example ends with LineSearchFailed at the same point instead of Success; with it, the run takes 22 iterations and 25 evaluations.
The search starts from a unit inverse Hessian, so the first step is a steepest-descent step whose length depends on the scale of the objective. When the curvature is far from one, for example a log-likelihood summed over thousands of observations, set ScaleInitialInverseHessian = true. After the first accepted step, the identity is then multiplied by
var scaled = new BFGS(Rosenbrock, 2, initial, lower, upper);
scaled.ScaleInitialInverseHessian = true;
scaled.Minimize();
Console.WriteLine($"Scaled: {scaled.FunctionEvaluations} evaluations, status {scaled.Status}");Advantages: Fast convergence, memory efficient, works well for most smooth problems.
Disadvantages: Can fail on non-smooth functions, requires bounded search space.
LBFGSB is the limited-memory BFGS method for bound-constrained problems of Byrd, Lu, Nocedal and Zhu [27], in the L-BFGS-B 3.0 form [28] [29]. It keeps the last MemorySize (default 10) step and gradient-change pairs instead of a dense inverse Hessian. Each iteration finds the generalized Cauchy point along the projected steepest-descent path, which fixes the active bounds. It then minimizes the quadratic model over the free variables and takes a More-Thuente line search [30]. MaxLineSearchSteps (default 20) caps the function evaluations in one line search. The memory and the work per iteration grow linearly with the number of parameters, so it suits problems with hundreds or thousands of parameters.
Each bound may be infinite, so a parameter can be unbounded, bounded on one side, bounded on both sides, or fixed (lower == upper). A start outside the box is projected onto it. The run stops when the largest projected gradient component is at most AbsoluteTolerance or the relative reduction RelativeTolerance. These are SciPy's gtol and ftol; set them to 1e-5 and 2.220446049250313e-9 to reproduce SciPy's defaults. A trial point with a non-finite objective or gradient makes the line search restart with half the step. When a line search fails with an empty memory, the status is LineSearchFailed; otherwise the memory is discarded and the iteration is retried.
using Numerics.Mathematics.Optimization;
// Rosenbrock in 10 dimensions with an analytic gradient; x[3] is held at or below 0.5
int n = 10;
double F(double[] x)
{
double s = 0;
for (int i = 0; i < n - 1; i++) s += 100 * Math.Pow(x[i + 1] - x[i] * x[i], 2) + Math.Pow(1 - x[i], 2);
return s;
}
double[] G(double[] x)
{
var g = new double[n];
for (int i = 0; i < n - 1; i++)
{
double a = x[i + 1] - x[i] * x[i];
g[i] += -400 * x[i] * a - 2 * (1 - x[i]);
g[i + 1] += 200 * a;
}
return g;
}
var start = new double[n];
var lowerBounds = Enumerable.Repeat(double.NegativeInfinity, n).ToArray();
var upperBounds = Enumerable.Repeat(double.PositiveInfinity, n).ToArray();
upperBounds[3] = 0.5;
var lbfgsb = new LBFGSB(F, n, start, lowerBounds, upperBounds, G)
{
AbsoluteTolerance = 1e-5,
RelativeTolerance = 2.220446049250313e-9
};
lbfgsb.Minimize();
Console.WriteLine($"Status: {lbfgsb.Status}, iterations {lbfgsb.Iterations}, evaluations {lbfgsb.FunctionEvaluations}");
Console.WriteLine($"f = {lbfgsb.BestParameterSet.Fitness:F6}, x[3] = {lbfgsb.BestParameterSet.Values[3]:F6}");Agreement with SciPy. On 18 problems the iteration and evaluation counts equal those of scipy.optimize.minimize(method="L-BFGS-B") 1.17.1 with the same settings, and the optimum agrees to
Speed. Wall time against SciPy (BenchmarkDotNet mean; SciPy median of 21 runs, with NumPy objective and gradient functions that add Python overhead to each evaluation). Intel Core Ultra 9 185H, Windows 11, .NET 10 (BenchmarkDotNet 0.15.8, Benchmarks_Numerics/Optimization/LBFGSBBenchmarks.cs); Python 3.14.3, NumPy 2.4.2, SciPy 1.17.1 (oracles/lbfgsb/lbfgsb_scipy_timing.py). The iteration counts match except on the 100-dimensional problems, where dot-product rounding changes the path after a few hundred iterations; both reach the same optimum.
| Problem | Iterations, Numerics / SciPy | Numerics | SciPy |
|---|---|---|---|
| Rosenbrock, 10-D, unbounded | 71 / 71 | 0.36 ms | 6.7 ms |
| Rosenbrock, 100-D, unbounded | 513 / 517 | 6.9 ms | 48 ms |
| Rosenbrock, 100-D, on |
590 / 600 | 10.1 ms | 55 ms |
| Weighted quadratic, 1000-D, on |
14 / 14 | 1.25 ms | 7.5 ms |
Advantages: Handles bounds exactly, low memory, and stopping rules that match SciPy.
Disadvantages: Needs a smooth objective; a finite-difference gradient costs about
The Nelder-Mead method is a direct search algorithm that uses a simplex (geometric figure with n+1 vertices in n dimensions) to search the parameter space [2]. It's robust and doesn't require derivatives.
var nm = new NelderMead(Rosenbrock, 2, initial, lower, upper);
nm.RelativeTolerance = 1e-6;
nm.Minimize();
Console.WriteLine($"Optimal solution: [{nm.BestParameterSet.Values[0]:F6}, {nm.BestParameterSet.Values[1]:F6}]");
Console.WriteLine($"Minimum value: {nm.BestParameterSet.Fitness:F10}");Advantages: Very robust, doesn't require derivatives, handles non-smooth functions.
Disadvantages: Slower convergence than gradient-based methods, can stagnate.
Powell's method is a conjugate direction algorithm that doesn't require derivatives [1]. It performs successive line searches along conjugate directions.
var powell = new Powell(Rosenbrock, 2, initial, lower, upper);
powell.RelativeTolerance = 1e-8;
powell.Minimize();
Console.WriteLine($"Solution: [{powell.BestParameterSet.Values[0]:F6}, {powell.BestParameterSet.Values[1]:F6}]");Advantages: No derivatives required, good for smooth functions.
Disadvantages: Can be slow in high dimensions, sensitive to scaling.
Steepest descent with a fixed step size Alpha (there is no line search). The gradient is numerical unless one is supplied to the constructor:
var gd = new GradientDescent(Rosenbrock, 2, initial, lower, upper);
gd.Alpha = 0.001; // Step size (learning rate)
gd.Minimize();Adaptive Moment Estimation, popular in machine learning applications [3]:
var adam = new ADAM(Rosenbrock, 2, initial, lower, upper);
adam.Alpha = 0.001;
adam.Beta1 = 0.9; // First moment decay
adam.Beta2 = 0.999; // Second moment decay
adam.Minimize();Advantages: Adaptive learning rates, works well for noisy objectives.
Best for: Machine learning, stochastic optimization problems.
For single-parameter optimization, specialized 1D methods are more efficient:
Uses the golden ratio to bracket the minimum:
Func<double, double> f1d = x => Math.Pow(x - 2, 2) + 3;
var golden = new GoldenSection(f1d, 0.0, 5.0);
golden.Minimize();
Console.WriteLine($"Minimum at x = {golden.BestParameterSet.Values[0]:F6}");Combines golden section search with parabolic interpolation for faster convergence. The search is a port of SciPy's bounded minimizer, scipy.optimize.minimize_scalar(method='bounded'). Its step tolerance is RelativeTolerance * |x| + AbsoluteTolerance / 3; the constructor sets AbsoluteTolerance to about
var brent = new BrentSearch(f1d, 0.0, 5.0);
brent.Minimize();
// SciPy's defaults: xatol = 1e-5, maxiter = 500 evaluations, and its sqrt(2.2e-16) relative term.
var scipyLike = new BrentSearch(f1d, 0.0, 5.0)
{
RelativeTolerance = Math.Sqrt(2.2E-16),
AbsoluteTolerance = 1E-5,
MaxFunctionEvaluations = 500
};
scipyLike.Minimize();Global optimization methods are designed to find the global minimum across the entire search space, avoiding local minima. They typically require more function evaluations but are more robust for multimodal problems.
A global search finds the right basin but converges slowly inside it. Differential Evolution, Particle Swarm, SCE-UA, Simulated Annealing, DDS, and CMA-ES can refine their best point with a bounded local search when the global search ends:
Polish: Run the local search (default = false; true for Simulated Annealing)PolishMethod:LocalMethod.NelderMead(default),LocalMethod.BFGS, orLocalMethod.Powell
The polish starts at the best point, uses the optimizer's tolerances and remaining evaluation budget, and its evaluations count toward FunctionEvaluations. It runs after convergence or after MaxIterations, but not after the evaluation budget is spent or a cancellation, or when fewer than 10 evaluations remain in the budget. The reported status is the global search's. Multi-Start and MLSL always polish with their own LocalMethod (Polish = true by default).
double Rosen(double[] x) => Math.Pow(1 - x[0], 2) + 100 * Math.Pow(x[1] - x[0] * x[0], 2);
var polished = new DifferentialEvolution(Rosen, 2, new[] { -5.0, -5 }, new[] { 5.0, 5 }) { Polish = true };
polished.Minimize();
Console.WriteLine($"Polished minimum: {polished.BestParameterSet.Fitness:E2}");Differential Evolution (DE) is a population-based evolutionary algorithm that's very robust for continuous optimization [4]. For each member
Mutation (DE/rand/1): Three distinct population members $\mathbf{x}{r_0}$, $\mathbf{x}{r_1}$,
where
Crossover (binomial): The trial vector
where
Selection (greedy): The trial vector replaces the target only if it has equal or better fitness:
The algorithm converges when the standard deviation of fitness values across the population falls below the tolerance.
using Numerics.Mathematics.Optimization;
// Rastrigin function (highly multimodal)
double Rastrigin(double[] x)
{
double sum = 10 * x.Length;
for (int i = 0; i < x.Length; i++)
{
sum += x[i] * x[i] - 10 * Math.Cos(2 * Math.PI * x[i]);
}
return sum;
}
var lower = new double[] { -5.12, -5.12 };
var upper = new double[] { 5.12, 5.12 };
var de = new DifferentialEvolution(Rastrigin, 2, lower, upper);
de.PopulationSize = 50;
de.CrossoverProbability = 0.9;
de.Mutation = 0.75;
de.MaxIterations = 1000;
de.Minimize();
Console.WriteLine($"Global minimum: [{de.BestParameterSet.Values[0]:F6}, {de.BestParameterSet.Values[1]:F6}]");
Console.WriteLine($"Function value: {de.BestParameterSet.Fitness:F10}");Advantages: Very robust, handles discontinuities, good for difficult problems.
Parameters:
PopulationSize: Number of candidate solutions (default = 10 × dimensions)CrossoverProbability: Probability of crossover (default = 0.9)Mutation: Scaling factor for mutation (default = 0.75)InitialPopulation: Starting members that replace the first Latin hypercube members (default = none)IntegerParameters: Flags for integer-valued parameters (default = none)UseParallel,MaxDegreeOfParallelism: Evaluate each generation in parallel (default = off, no limit)
Warm start and integer parameters: InitialPopulation seeds the search with known points, such as a previous calibration or a baseline schedule; the other members and the random stream are unchanged. IntegerParameters marks parameters that must take integer values, such as start years in a project schedule: they are rounded to the nearest integer (ties to even) within integrality option does. Polishing is skipped when there are integer parameters, and the Hessian is not meaningful for them.
// Two integer start years and one continuous fraction.
double Schedule(double[] x) => Math.Pow(x[0] - 3.4, 2) + Math.Pow(x[1] - 7.6, 2) + Math.Pow(x[2] - 0.25, 2);
var plan = new DifferentialEvolution(Schedule, 3, new[] { 0.0, 0, 0 }, new[] { 10.0, 10, 1 })
{
IntegerParameters = new[] { true, true, false },
InitialPopulation = new List<double[]> { new[] { 3.0, 8.0, 0.5 } },
ComputeHessian = false
};
plan.Minimize();
Console.WriteLine($"Start years {plan.BestParameterSet.Values[0]}, {plan.BestParameterSet.Values[1]}; fraction {plan.BestParameterSet.Values[2]:F4}"); // 3, 8; 0.2500Parallel evaluation: With UseParallel = true, each generation's trial vectors are built from the population at the start of the generation, evaluated concurrently, and selected in index order (deferred updating, as SciPy's updating='deferred'). The random draws stay on one thread, so the result is reproducible and does not depend on the number of threads. It is a different search from the default immediate updating, where an accepted trial can serve as a donor later in the same generation. The objective must be thread safe. For a simulation objective costing about 1 ms per run, the speedup measured on a 22-core machine was about 9 times. Particle Swarm has the same option, with synchronous moves toward the previous iteration's global best.
PSO simulates social behavior of bird flocking or fish schooling [5]. Each particle
where:
-
$w$ is the inertia weight, which decreases linearly from$w_{\max} = 0.9$ to$w_{\min} = 0.4$ over the optimization, balancing exploration (high$w$ ) and exploitation (low$w$ ) -
$c_1 = c_2 = 2.05$ are the cognitive and social acceleration coefficients -
$r_1, r_2 \sim U(0,1)$ are independent random numbers (per component, per particle) -
$\mathbf{p}_i$ is particle$i$ 's personal best position (best position it has visited) -
$\mathbf{g}$ is the global best position (best position any particle has visited)
The three terms represent momentum (continue in the current direction), cognitive pull (return toward personal best), and social pull (move toward the swarm's best).
var pso = new ParticleSwarm(Rastrigin, 2, lower, upper);
pso.PopulationSize = 40;
pso.Minimize();
Console.WriteLine($"Solution: [{pso.BestParameterSet.Values[0]:F6}, {pso.BestParameterSet.Values[1]:F6}]");Advantages: Fast convergence, simple concept, works well for continuous problems.
Parameters:
PopulationSize: Number of particles (default = 30)UseParallel,MaxDegreeOfParallelism: Synchronous swarm with parallel evaluation (default = off)Polish,PolishMethod: Refine the best point at the end (default = off)
SCE-UA was specifically developed for calibrating hydrological models [6]. The algorithm partitions the population into
The CCE step within each complex proceeds as follows:
-
Sub-complex selection: Select
$q = D+1$ distinct points from the complex using a trapezoidal probability distribution that favors better-ranked points:$P(i) = \frac{2(N+1-i)}{N(N+1)}$ -
Reflection: Compute the centroid
$\mathbf{g}$ of all sub-complex points except the worst point$\mathbf{x}_w$ , then reflect:
-
Contraction: If the reflected point is infeasible or worse than
$\mathbf{x}_w$ , try contraction:
- Mutation: If contraction also fails, generate a random point within the smallest bounding box of the complex. It replaces the worst point whatever its value.
The evolved sub-complex is written back to the positions it was drawn from, as in Duan et al. (1994) [22].
The shuffling step redistributes points across complexes, preventing any single complex from stagnating. This combination of local competitive evolution within complexes and global information sharing between complexes makes SCE-UA particularly effective for the rugged, multimodal objective functions typical of hydrological model calibration.
var sce = new ShuffledComplexEvolution(Rastrigin, 2, lower, upper);
sce.Complexes = 5;
sce.MaxIterations = 1000;
sce.Minimize();
Console.WriteLine($"Calibrated parameters: [{sce.BestParameterSet.Values[0]:F6}, {sce.BestParameterSet.Values[1]:F6}]");Advantages: Excellent for hydrological model calibration, balances exploration and exploitation.
Best for: Calibrating watershed models, water resources applications.
DDS is a greedy, single-solution global search developed for calibrating watershed models when each model run is expensive [14]. Instead of stopping on a convergence test, DDS is given a budget of
Initialization: The search starts from a supplied initial point, or from the best of
Neighborhood selection: With
If no parameter is selected, one is chosen at random, so at least one parameter always changes.
Perturbation: Each selected parameter of the best solution is perturbed with a normal step whose standard deviation is a fixed fraction
The bounds are reflecting. A value below
Selection (greedy): The candidate replaces the best solution when
using Numerics.Mathematics.Optimization;
// Rastrigin function (highly multimodal)
double Rastrigin(double[] x)
{
double sum = 10 * x.Length;
for (int i = 0; i < x.Length; i++)
{
sum += x[i] * x[i] - 10 * Math.Cos(2 * Math.PI * x[i]);
}
return sum;
}
var lower = new double[] { -5.12, -5.12, -5.12, -5.12, -5.12 };
var upper = new double[] { 5.12, 5.12, 5.12, 5.12, 5.12 };
var dds = new DynamicallyDimensionedSearch(Rastrigin, 5, lower, upper);
dds.EvaluationBudget = 5000; // m: the search is scaled to this budget
dds.PerturbationSize = 0.2; // r: perturbation standard deviation as a fraction of the range
dds.Minimize();
Console.WriteLine($"Status: {dds.Status}, evaluations: {dds.FunctionEvaluations}");
Console.WriteLine($"Best value: {dds.BestParameterSet.Fitness:F4}");
// Warm start from a previous calibration: one initial evaluation instead of a random sample.
var start = new double[] { 1.0, -1.0, 0.5, 0.0, 2.0 };
var warm = new DynamicallyDimensionedSearch(Rastrigin, 5, start, lower, upper) { EvaluationBudget = 1000 };
warm.Minimize();
Console.WriteLine($"Warm start best value: {warm.BestParameterSet.Fitness:F4}");Spending the budget is the normal end of a DDS run, so it reports OptimizationStatus.Success and, when ComputeHessian is true, computes the Hessian at the best point. MaxFunctionEvaluations and MaxIterations still act as hard caps: if either is reached before the budget, the run stops early with that status. Iterations counts the DDS iterations, which is the budget less the initial evaluations.
Advantages: Needs no tuning, makes good use of a small evaluation budget, and handles many parameters well because most iterations change only a few of them.
Parameters:
-
EvaluationBudget: The planned number of evaluations$m$ , including the initial ones (default = 1,000) -
PerturbationSize: The neighborhood size$r$ , in$(0, 1]$ (default = 0.2, as recommended by the authors) -
PRNGSeed: The random seed (default = 12345) -
Polish,PolishMethod: Refine the best point after the budget (default = off)
Best for: Calibrating watershed and other simulation models with roughly 5 to 30 parameters, where each run is expensive and you can only afford a few hundred to a few thousand runs. Set EvaluationBudget to the number of model runs you can afford, not to a large safety value, because the search schedule depends on it. DDS is designed to find good solutions within the budget, not to converge precisely. When you need precise parameter values, set Polish = true (see Polishing) or refine the DDS result with a local method such as NelderMead or BFGS. With a known good starting point, the authors suggest reducing ShuffledComplexEvolution usually converges more precisely.
CMA-ES samples a population from a multivariate normal distribution and adapts the mean, the step size and the full covariance matrix from the ranked samples [23], [24]. Each generation draws
and ranks them by fitness. With $\mathbf{y}{i:\lambda} = (\mathbf{x}{i:\lambda} - \mathbf{m})/\sigma$ the
Step size (cumulative step-size adaptation): the conjugate evolution path
Covariance matrix: the rank-one update uses the evolution path
where
Bounds: the search runs in the unit cube, where every parameter range has length one. The objective is evaluated at BoundTransform [26]: it is the identity on
Restarts (IPOP): a run stops on the tutorial's termination criteria (TolFun, TolX, EqualFunValues, NoEffectAxis, NoEffectCoord, ConditionCov, Stagnation, TolXUp). The solver then restarts from a uniformly random mean with the population size doubled [25], up to Restarts times. Larger populations smooth out the local structure of multimodal functions such as Rastrigin. TolFun uses the common tolerances: a run converges when the best values of the last AbsoluteTolerance + RelativeTolerance
using Numerics.Mathematics.Optimization;
// Rastrigin function shifted so that the minimum f = 0 is at x = (1, ..., 1)
double ShiftedRastrigin(double[] x)
{
double sum = 10 * x.Length;
for (int i = 0; i < x.Length; i++)
{
double z = x[i] - 1;
sum += z * z - 10 * Math.Cos(2 * Math.PI * z);
}
return sum;
}
int n = 5;
var lower = Enumerable.Repeat(-5.12, n).ToArray();
var upper = Enumerable.Repeat(5.12, n).ToArray();
var cma = new CMAES(ShiftedRastrigin, n, lower, upper);
cma.Restarts = 9; // IPOP restarts with doubled population (default)
cma.MaxFunctionEvaluations = 200000;
cma.Minimize();
Console.WriteLine($"Status: {cma.Status}, restarts: {cma.RestartsPerformed}, evaluations: {cma.FunctionEvaluations}");
Console.WriteLine($"Solution: [{string.Join(", ", cma.BestParameterSet.Values.Select(v => v.ToString("F6")))}]");
Console.WriteLine($"Function value: {cma.BestParameterSet.Fitness:E3}");
// A single run from a given starting point, for a unimodal problem
double Rosenbrock(double[] x) => 100 * Math.Pow(x[1] - x[0] * x[0], 2) + Math.Pow(1 - x[0], 2);
var local = new CMAES(Rosenbrock, 2, new[] { -1.2, 1.0 }, new[] { -5.0, -5.0 }, new[] { 5.0, 5.0 });
local.Restarts = 0;
local.InitialStepSize = 0.1; // in units of the bound range
local.Minimize();
Console.WriteLine($"Rosenbrock: [{local.BestParameterSet.Values[0]:F6}, {local.BestParameterSet.Values[1]:F6}] after {local.FunctionEvaluations} evaluations");The IPOP run finds the global minimum of the 5-D Rastrigin function. Once a run has converged, reaching MaxFunctionEvaluations or MaxIterations during a later restart ends the optimization normally with Success and the best point found, so the evaluation limit acts as the restart budget. Without a limit, all Restarts runs are performed; with the default of 9 restarts the last population is 512 times the first, and a 10-D problem then uses roughly 0.5 to 1 million evaluations in total.
Parameters:
-
PopulationSize:$\lambda$ of the first run (default =$4 + \lfloor 3 \ln n \rfloor$ ) -
InitialStepSize:$\sigma_0$ in units of the bound range (default = 0.3) -
Restarts: maximum number of IPOP restarts (default = 9; 0 for a single run) -
MaxFunctionEvaluations: evaluation budget shared by all runs (default = 10,000 ×$n$ for CMA-ES). Once a run has converged, reaching the budget during a later restart ends the search withSuccess -
InitialValues: optional initial mean of the first run (default = center of the bounds), set through the constructor overload -
Polish,PolishMethod: Refine the best point after the restarts (default = off) -
PRNGSeed: random seed (default = 12345)
When to prefer CMA-ES over Differential Evolution: CMA-ES learns correlations and scaling between parameters, so it is much faster on smooth, ill-conditioned or non-separable problems. In 10 dimensions it reached Restarts = 0 when the problem is known to be unimodal; each restart costs at least as much as the run before it.
SA mimics the physical process of annealing in metallurgy [7]. It accepts uphill moves with decreasing probability, allowing escape from local minima.
var sa = new SimulatedAnnealing(Rastrigin, 2, lower, upper);
sa.InitialTemperature = 10.0;
sa.CoolingRate = 0.95;
sa.MaxIterations = 10000;
sa.Minimize();
Console.WriteLine($"Solution: [{sa.BestParameterSet.Values[0]:F6}, {sa.BestParameterSet.Values[1]:F6}]");Advantages: Can escape local minima, works for discrete and continuous problems.
Stopping and polishing: each temperature reduction makes TemperatureCycles × UpdateCycles sweeps over the OptimizationStatus.Success when the best value has not changed, within the tolerances, over the last ToleranceSteps (default = 20) reductions, and then polishes the best point with Nelder-Mead (Polish = true by default). The stall test usually ends a search after a few thousand evaluations, far fewer than the full MaxIterations schedule.
Parameters:
InitialTemperature: Starting temperature (default = 10)CoolingRate: Temperature reduction factor (default = 0.95)
Combines local search with multiple random starting points. The local search is LocalMethod.BFGS (default), LocalMethod.NelderMead, LocalMethod.Powell or LocalMethod.LBFGSB, which holds the search exactly on the box and also polishes the final result; MLSL accepts the same four.
var initial = new double[] { 0.0, 0.0 };
var ms = new MultiStart(Rastrigin, 2, initial, lower, upper, LocalMethod.BFGS);
ms.MaxIterations = 20; // Number of random starts
ms.Minimize();
Console.WriteLine($"Best solution: [{ms.BestParameterSet.Values[0]:F6}, {ms.BestParameterSet.Values[1]:F6}]");Advantages: Simple to implement, leverages fast local search.
Disadvantages: May waste evaluations in same basin of attraction.
Completing all the starts is the normal end, so the run reports the status of the final polish (Success in a normal run) and computes the Hessian.
Clustering-based global optimization that avoids redundant local searches:
var initial = new double[] { 0.0, 0.0 };
var mlsl = new MLSL(Rastrigin, 2, initial, lower, upper, LocalMethod.BFGS);
mlsl.Minimize();Advantages: More efficient than multi-start, avoids redundant searches.
A sample point starts a local search only if no better sample point or local minimum lies within the critical distance of Rinnooy Kan and Timmer [21], measured in the box scaled to the unit cube:
where SampleSize, and Sigma (default = 2).
The Augmented Lagrangian method handles equality and inequality constraints by adding penalty terms to the objective function [8].
using Numerics.Mathematics.Optimization;
// Objective: minimize x² + y²
double Objective(double[] x)
{
return x[0] * x[0] + x[1] * x[1];
}
// Constraint: x + y >= 1
var constraint = new Constraint(
x => x[0] + x[1], 2, 1.0, // g(x) >= value form
ConstraintType.GreaterThanOrEqualTo
);
var lower = new double[] { -5, -5 };
var upper = new double[] { 5, 5 };
var initial = new double[] { 0, 0 };
// Create the inner optimizer
var bfgs = new BFGS(Objective, 2, initial, lower, upper);
// Create the Augmented Lagrange optimizer with constraints
var al = new AugmentedLagrange(Objective, bfgs, new IConstraint[] { constraint });
al.MaxIterations = 100;
al.Minimize();
Console.WriteLine($"Optimal solution: [{al.BestParameterSet.Values[0]:F6}, {al.BestParameterSet.Values[1]:F6}]");
Console.WriteLine($"Constraint satisfied: {al.BestParameterSet.Values[0] + al.BestParameterSet.Values[1]:F6} >= 1");Constraint Types (each compares the constraint function Value
-
ConstraintType.EqualTo: Equality constraint$g(\mathbf{x}) = b$ -
ConstraintType.LesserThanOrEqualTo: Inequality constraint$g(\mathbf{x}) \leq b$ -
ConstraintType.GreaterThanOrEqualTo: Inequality constraint$g(\mathbf{x}) \geq b$
A constraint counts as satisfied when it is violated by no more than its Tolerance (default = 1E-8). Each inequality adds the Powell-Hestenes-Rockafellar term
Example: Minimize subject to multiple constraints:
// Minimize f(x,y) = (x-3)² + (y-2)²
// Subject to: x + y <= 5
// x >= 1
// y >= 1
double ObjectiveFunc(double[] x)
{
return Math.Pow(x[0] - 3, 2) + Math.Pow(x[1] - 2, 2);
}
var c1 = new Constraint(x => x[0] + x[1], 2, 5.0, ConstraintType.LesserThanOrEqualTo);
var c2 = new Constraint(x => x[0], 2, 1.0, ConstraintType.GreaterThanOrEqualTo);
var c3 = new Constraint(x => x[1], 2, 1.0, ConstraintType.GreaterThanOrEqualTo);
var innerOptimizer = new BFGS(ObjectiveFunc, 2, new[] { 2.0, 2.0 },
new[] { 0.0, 0.0 }, new[] { 10.0, 10.0 });
var constrained = new AugmentedLagrange(ObjectiveFunc, innerOptimizer,
new IConstraint[] { c1, c2, c3 });
constrained.Minimize();
Console.WriteLine($"Constrained optimum: [{constrained.BestParameterSet.Values[0]:F4}, " +
$"{constrained.BestParameterSet.Values[1]:F4}]");ParetoFront is a static class of pure functions for multi-objective results: dominance, non-dominated sorting, crowding distance, hypervolume, and the generational distance indicators. These are the building blocks of multi-objective optimizers such as NSGA-II, and they also summarize any set of trade-off alternatives, for example a dam safety portfolio scored on cost and risk.
All objectives are minimized; negate an objective to maximize it. A set of double[][] (one row per point) or double[,]. NaN is rejected with an ArgumentException. Dominance and sorting accept infinite values (for example, a penalized infeasible point); crowding distance, hypervolume and the distance indicators require finite values.
Point
ParetoFront.Dominates(a, b) tests the relation for two points:
bool d1 = ParetoFront.Dominates(new[] { 2.0, 3.0 }, new[] { 3.0, 4.0 }); // True
bool d2 = ParetoFront.Dominates(new[] { 1.0, 5.0 }, new[] { 4.0, 1.0 }); // False: a trade-off
bool d3 = ParetoFront.Dominates(new[] { 2.0, 3.0 }, new[] { 2.0, 3.0 }); // False: equal pointsEqual points do not dominate each other. The non-dominated fronts of Deb et al. [15] are defined recursively: front NonDominatedSort returns the fronts as lists of point indices, NonDominatedIndices returns
The points are first ordered lexicographically, so only earlier points can dominate a later one, and each point is placed by a binary search over the fronts built so far. With two objectives the sort is
Within one front, the crowding distance of Deb et al. [15] measures how isolated a point is. For each objective
Each objective is normalized by its range over the front. An objective with zero range contributes nothing, boundary points included, and every point of a front with one or two points gets calc_crowding_distance uses the same terms but divides the sum by filter_out_duplicates=True) pymoo also removes duplicate points before computing the distances and gives each removed duplicate a distance of zero; CrowdingDistance keeps every point, so its values differ from pymoo's on a front with duplicates.
The hypervolume [16] of a set
where
| Objectives | Method | Cost |
|---|---|---|
| 2 | Sort by |
|
| 3 | Sweep in |
|
| 4 or more | WFG [18]: slice on |
exponential in |
Given an approximation set
Smaller is better for both. GD measures convergence only; IGD also penalizes gaps in coverage. These are the definitions of pymoo's GD and IGD without normalization. Van Veldhuizen and Lamont [19] define GD with the root of the summed squared distances,
using Numerics.Mathematics.Optimization;
// Six alternatives scored on (cost, risk); both are minimized.
var points = new double[][]
{
new[] { 1.0, 5.0 },
new[] { 2.0, 3.0 },
new[] { 3.0, 4.0 }, // dominated by (2, 3)
new[] { 4.0, 1.0 },
new[] { 2.0, 3.0 }, // duplicate of point 1
new[] { 5.0, 5.0 }, // dominated
};
var fronts = ParetoFront.NonDominatedSort(points);
for (int f = 0; f < fronts.Count; f++)
Console.WriteLine($"Front {f}: {string.Join(", ", fronts[f])}");
// Crowding distance within the first front.
var distances = ParetoFront.CrowdingDistance(points, fronts[0]);
for (int i = 0; i < fronts[0].Count; i++)
Console.WriteLine($"Point {fronts[0][i]}: crowding distance {distances[i]:F4}");
// Hypervolume with respect to the reference point (6, 6).
double hv = ParetoFront.Hypervolume(points, new[] { 6.0, 6.0 });
Console.WriteLine($"Hypervolume: {hv:F4}");
// Distance to a reference front on the segment f1 + f2 = 5.
var reference = new double[][]
{
new[] { 0.0, 5.0 }, new[] { 1.0, 4.0 }, new[] { 2.0, 3.0 },
new[] { 3.0, 2.0 }, new[] { 4.0, 1.0 }, new[] { 5.0, 0.0 },
};
var first = fronts[0].Select(i => points[i]).ToArray();
Console.WriteLine($"GD: {ParetoFront.GenerationalDistance(first, reference):F4}");
Console.WriteLine($"IGD: {ParetoFront.InvertedGenerationalDistance(first, reference):F4}");Output:
Front 0: 0, 1, 3, 4
Front 1: 2
Front 2: 5
Point 0: crowding distance ∞
Point 1: crowding distance 0.8333
Point 3: crowding distance ∞
Point 4: crowding distance 1.1667
Hypervolume: 17.0000
GD: 0.2500
IGD: 0.8047
The duplicate points 1 and 4 share the first front. Ties in each objective are ordered by index, so point 1 lies between points 0 and 4 and point 4 between points 1 and 3, which gives them different crowding distances. The output above is from .NET 10; .NET Framework prints infinity as Infinity.
The Dijkstra, Network, and Edge types solve destination-rooted shortest paths over
directed networks [9]. They are the routing kernel for agent-based evacuation modeling, where every
agent needs its route to the nearest destination and edge costs (travel times) change as
conditions evolve. The solvers run on an internal indexed binary min-heap sized at the node
count; the public BinaryHeap<T> is a separate standalone utility.
Every solver returns one float[,] routing table with a row per node and three columns:
| Column | Contents |
|---|---|
[i, 0] |
The next node toward the destination |
[i, 1] |
The index of the edge to take (Edge.Index) |
[i, 2] |
The cumulative cost to the destination |
The destination row is (itself, -1, 0); an unreachable node's row is (-1, -1, +∞).
Dijkstra.PathExists tests reachability, and Dijkstra.GetPath / Dijkstra.TryGetPath walk a
table into the ordered edge-index list with the total cost.
Weight conventions: Dijkstra's algorithm requires non-negative weights: every Dijkstra solver
and every Network solve or path method throws ArgumentOutOfRangeException at entry when a
weight it would use is negative, which also rules out negative cycles; a weight of positive infinity
makes an edge impassable (the natural encoding for a flooded road segment), and a NaN weight
never relaxes, severing its edge.
Precision: the edge weights are float, but every solver accumulates costs in double and rounds
each cost to float once, in the table. The cost column is therefore the single-precision rounding
of the double-precision distance that scipy.sparse.csgraph.dijkstra computes on the same weights;
the unit tests check this bitwise.
using Numerics.Mathematics.Optimization;
var edges = new List<Edge>
{
new Edge(0, 1, 2.0f, 0), // from, to, weight (e.g. travel time), edge index
new Edge(1, 2, 1.5f, 1),
new Edge(0, 2, 5.0f, 2),
};
// Route every node to node 2.
float[,] table = Dijkstra.Solve(edges, destinationIndex: 2);
// Route every node to the nearest of several destinations in ONE pass.
float[,] nearest = Dijkstra.SolveNearest(edges, new[] { 0, 2 });
// Walk a route out of the table.
if (Dijkstra.TryGetPath(table, startNodeIndex: 0, out List<int> route, out float cost))
Console.WriteLine($"Route: {string.Join(" -> ", route)}, cost {cost:F2}");Dijkstra.Solve(edges, destinationIndices, ...) (the multi-destination overload) solves each
destination independently and keeps each node's strictly cheapest route, so on an exact cost tie
the earlier destination in the array wins. SolveNearest produces the same costs in a single
multi-source pass and is the faster form of the nearest-destination query.
Dijkstra.ShortestDistances takes a directed graph in compressed sparse row (CSR) form, with
double-precision weights, and returns the distance of every node from the nearest of several
sources. It follows scipy.sparse.csgraph.dijkstra(graph, indices=sources, min_only=True): an
optional limit leaves farther nodes unreached, and optional arrays receive each node's
predecessor and nearest source (-1 where SciPy reports -9999). Weights must be non-negative
and not NaN.
// Edges 0->1 (0.1), 0->2 (0.3), 1->2 (0.2), 1->3 (1.2), 2->3 (0.7).
int[] rowStart = { 0, 2, 4, 5, 5 };
int[] toNode = { 1, 2, 2, 3, 3 };
double[] weight = { 0.1, 0.3, 0.2, 1.2, 0.7 };
var predecessors = new int[4];
var nearestSource = new int[4];
double[] distance = Dijkstra.ShortestDistances(4, rowStart, toNode, weight, new[] { 0 },
predecessors: predecessors, nearestSource: nearestSource);
Console.WriteLine(string.Join(", ", distance)); // 0, 0.1, 0.3, 1
Console.WriteLine(string.Join(", ", predecessors)); // -1, 0, 0, 2Speed. Median wall time for ten sources on random directed graphs with five edges per node and
weights uniform on scipy.sparse.csgraph.dijkstra(min_only=True). Intel
Core Ultra 9 185H, Windows 11, .NET 10; Python 3.14.3, NumPy 2.5.4, SciPy 1.18.1, pynumerics
graphs.shortest_distances on a SciPy CSR matrix. BenchmarkDotNet (ShortRun,
Benchmarks_Numerics/Optimization/DijkstraBenchmarks.cs) gives 1.33 ms, 16.8 ms and 479 ms for the
three sizes.
| Nodes | Numerics (.NET) | pynumerics | SciPy |
|---|---|---|---|
| 10,000 | 1.03 ms | 1.17 ms | 1.65 ms |
| 100,000 | 15.4 ms | 17.8 ms | 24.6 ms |
| 1,000,000 | 473 ms | 573 ms | 624 ms |
On graphs larger than the processor cache the time goes to memory latency. The heap keeps each node's distance next to its slot, so one cache line answers both the comparison and the heap update, and after each removal the loop reads the edges of the node now at the top of the heap while the current node's edges relax. Neither changes the settle order, so distances, predecessors and nearest sources are the same bit for bit as a plain binary heap gives.
Network compiles a fixed topology once (node count, incoming and outgoing adjacency, and the
destination set), so a simulation can re-solve every time step against updated edge weights with
no rebuild. Weights are positional with the constructor's edge array, and the table-reuse
overloads run with zero steady-state allocation (they share instance scratch buffers and are not
thread safe; the allocating overloads remain safe for concurrent use):
var network = new Network(edgeArray, destinationIndices);
var table = new float[network.NodeCount, 3];
var weights = new float[edgeArray.Length];
for (int t = 0; t < timeSteps; t++)
{
UpdateTravelTimes(weights, t); // flooded edges -> float.PositiveInfinity
network.SolveNearest(weights, table); // zero-allocation re-solve
RouteAgents(table); // each agent walks its row
}
// Detour routing around blocked segments, splicing onto the precomputed table when possible.
var detour = network.GetPath(blockedEdgeIndices, agentNodeIndex, table);Network.GetPath finds the cheapest route to the nearest destination that avoids every edge
bearing an excluded edge index; when the precomputed table's recorded route is untouched by the
exclusions it is returned directly, with no solve. The table-based overload never returns null:
it returns an empty list when the start node is unreachable, is already a destination, or no
detour exists. The two-argument overload (without a table), by contrast, returns null when
every destination is unreachable.
DynamicProgramming solves sequential decision problems over integer states Knapsack gives exact answers for
small budget-constrained project-selection problems.
With stage cost or reward Solve() computes
where opt is min (the default) or max (Maximize = true). The work is
| Member | Meaning |
|---|---|
Value[t, s] |
Optimal value for |
Policy[t, s] |
Best decision for -1 where no decision is feasible |
DiscountFactor |
|
TerminalValue |
null means zero |
UseParallel |
Evaluates the states of each stage in parallel; the result is identical to the serial solve |
Conventions:
- A non-finite stage value (NaN or
$\pm\infty$ ) marks a decision infeasible; a deterministic transition returning -1 does too. A non-finite terminal value marks that terminal state infeasible. - A decision that can reach an infeasible state with positive probability is infeasible. A state with no
feasible decision has value
$+\infty$ when minimizing ($-\infty$ when maximizing) and policy-1. - Ties go to the lowest decision index.
- The delegates must be deterministic functions of their arguments (and thread safe with
UseParallel).
A reservoir holds 0 to 3 units, starts with 1, receives inflows of 2, 1 and 1 units over three seasons and
must end with at least 1 unit. Releasing
using Numerics.Mathematics.Optimization;
int[] inflow = { 2, 1, 1 };
double[] benefit = { 0, 5, 8, 9 };
var dp = new DynamicProgramming(stages: 3, states: 4, decisions: 4,
stageValue: (t, s, r) => benefit[r],
transition: (t, s, r) =>
{
int next = s + inflow[t] - r;
return next < 0 || next > 3 ? -1 : next; // -1 marks an infeasible release
})
{
Maximize = true,
TerminalValue = new[] { double.NegativeInfinity, 0, 0, 0 } // must end with at least 1 unit
};
dp.Solve();
var path = dp.Simulate(initialState: 1);
Console.WriteLine($"Total benefit: {path.TotalValue}"); // 18
Console.WriteLine($"Releases: {string.Join(", ", path.Decisions)}"); // 1, 1, 2
Console.WriteLine($"Storage: {string.Join(", ", path.States)}"); // 1, 2, 2, 1Simulate returns a DynamicProgrammingPath with the visited States (one per stage plus the terminal
state), the Decisions, the undiscounted StageValues and the discounted TotalValue. For a deterministic
problem TotalValue equals Value[0, initialState] exactly.
For stochastic transitions, pass a TransitionDistribution delegate and the largest number of outcomes.
The solver hands the delegate two reusable buffers to fill with next states and probabilities, so the inner
loop allocates nothing; the delegate returns the number of outcomes written (0 marks the decision infeasible).
Probabilities must be non-negative and sum to one within
The usual reservoir formulation keeps the current inflow class in the state,
using Numerics.Mathematics.Optimization;
const int Storages = 11, Classes = 3, Releases = 6;
int[] classInflow = { 0, 2, 4 };
double[,] markov = { { 0.6, 0.3, 0.1 }, { 0.25, 0.5, 0.25 }, { 0.1, 0.3, 0.6 } };
int demand = 3;
var sdp = new DynamicProgramming(stages: 12, states: Storages * Classes, decisions: Releases,
stageValue: (t, s, r) =>
{
int x = s / Classes + classInflow[s % Classes] - r;
if (x < 0) return double.NaN; // cannot release more than is stored
double shortage = Math.Max(demand - r, 0);
double spill = Math.Max(x - (Storages - 1), 0);
return shortage * shortage + 0.5 * spill;
},
transition: (t, s, r, nextStates, probabilities) =>
{
int j = s % Classes;
int x = s / Classes + classInflow[j] - r;
if (x < 0) return 0; // infeasible
int storage = Math.Min(x, Storages - 1);
for (int jn = 0; jn < Classes; jn++)
{
nextStates[jn] = storage * Classes + jn;
probabilities[jn] = markov[j, jn];
}
return Classes;
},
maxOutcomes: Classes)
{
DiscountFactor = 0.98
};
sdp.Solve();
int start = 5 * Classes + 1; // storage 5, middle inflow class
Console.WriteLine($"Expected cost: {sdp.Value[0, start]:F4}");
// Replay the policy along an observed class sequence, or sample classes with a seeded generator.
var observed = sdp.Simulate(start, new[] { 2, 2, 1, 0, 0, 0, 1, 2, 2, 1, 1, 0 });
var sampled = sdp.Simulate(start, new Numerics.Sampling.MersenneTwister(12345));
Console.WriteLine($"Observed path cost: {observed.TotalValue:F4}, sampled: {sampled.TotalValue:F4}");For a stationary infinite-horizon problem, SolveSteadyState(tolerance, maxIterations) applies value
iteration with the delegates evaluated once at stage 0 [13]:
starting from TerminalValue (zero if null) and stopping when tolerance
(default SteadyStateValue,
SteadyStatePolicy, SteadyStateIterations and SteadyStateConverged. With
using Numerics.Mathematics.Optimization;
// Two states, two decisions, discounted cost.
double[,] cost = { { 2.0, 1.0 }, { 3.0, 0.5 } };
double[,,] p = { { { 0.9, 0.1 }, { 0.2, 0.8 } }, { { 0.7, 0.3 }, { 0.05, 0.95 } } };
var mdp = new DynamicProgramming(1, 2, 2, (t, s, a) => cost[s, a],
(t, s, a, next, prob) =>
{
next[0] = 0; next[1] = 1;
prob[0] = p[s, a, 0]; prob[1] = p[s, a, 1];
return 2;
}, maxOutcomes: 2)
{
DiscountFactor = 0.9
};
bool converged = mdp.SolveSteadyState(tolerance: 1e-10);
Console.WriteLine($"Converged: {converged} after {mdp.SteadyStateIterations} sweeps");
Console.WriteLine($"V = ({mdp.SteadyStateValue[0]:F6}, {mdp.SteadyStateValue[1]:F6}), " +
$"policy = ({mdp.SteadyStatePolicy[0]}, {mdp.SteadyStatePolicy[1]})");
// V = (5.838150, 5.260116), policy = (1, 1): the exact solution of the linear Bellman system of policy (1, 1)Knapsack.Solve maximizes
in weights[i, k] and one capacity per constraint (for example one budget per
period); it runs over the product of all capacities,
using Numerics.Mathematics.Optimization;
// Risk reduction (value) and cost in $100k of five candidate projects, budget $1M.
double[] riskReduction = { 4.2, 2.9, 6.1, 1.5, 3.4 };
int[] cost = { 4, 3, 6, 1, 3 };
double best = Knapsack.Solve(riskReduction, cost, capacity: 10, out int[] selected);
Console.WriteLine($"Best: {best:F1} from projects {string.Join(", ", selected)}"); // 11.0 from 2, 3, 4
// Two budget periods.
int[,] periodCost = { { 2, 2 }, { 1, 2 }, { 4, 2 }, { 1, 0 }, { 2, 1 } };
double best2 = Knapsack.Solve(riskReduction, periodCost, new[] { 5, 4 }, out int[] selected2);
Console.WriteLine($"Best: {best2:F1} from projects {string.Join(", ", selected2)}"); // 9.1 from 0, 3, 4A complete example of using optimization to calibrate a watershed model:
using Numerics.Mathematics.Optimization;
using Numerics.Data.Statistics;
// Observed streamflow data
double[] observed = { 12.5, 15.3, 18.7, 16.2, 14.1, 11.8, 10.3 };
// Simple runoff model: Q = C × P^α where Q is flow, P is precipitation, C and α are parameters
double[] precipitation = { 10, 12, 15, 13, 11, 9, 8 };
// Objective function: minimize RMSE
double ObjectiveFunction(double[] parameters)
{
double C = parameters[0];
double alpha = parameters[1];
// Simulate streamflow
double[] simulated = new double[observed.Length];
for (int i = 0; i < observed.Length; i++)
{
simulated[i] = C * Math.Pow(precipitation[i], alpha);
}
// Compute RMSE
double rmse = GoodnessOfFit.RMSE(observed, simulated);
return rmse;
}
// Parameter bounds
var lower = new double[] { 0.1, 0.5 }; // C >= 0.1, α >= 0.5
var upper = new double[] { 5.0, 3.0 }; // C <= 5.0, α <= 3.0
// Use SCE-UA (recommended for hydrological calibration)
var optimizer = new ShuffledComplexEvolution(ObjectiveFunction, 2, lower, upper);
optimizer.Complexes = 5;
optimizer.MaxIterations = 1000;
optimizer.Minimize();
Console.WriteLine($"Calibrated parameters:");
Console.WriteLine($" C = {optimizer.BestParameterSet.Values[0]:F4}");
Console.WriteLine($" α = {optimizer.BestParameterSet.Values[1]:F4}");
Console.WriteLine($" RMSE = {optimizer.BestParameterSet.Fitness:F4}");
Console.WriteLine($" Function evaluations: {optimizer.FunctionEvaluations}");
// Verify calibration
double[] final_simulated = new double[observed.Length];
for (int i = 0; i < observed.Length; i++)
{
final_simulated[i] = optimizer.BestParameterSet.Values[0] *
Math.Pow(precipitation[i], optimizer.BestParameterSet.Values[1]);
}
double nse = GoodnessOfFit.NashSutcliffeEfficiency(observed, final_simulated);
Console.WriteLine($" NSE = {nse:F4}");| Problem Type | Recommended Method | Notes |
|---|---|---|
| Smooth, unimodal | BFGS or Powell | Fast convergence to local minimum |
| Non-smooth, unimodal | Nelder-Mead | Robust direct search |
| Multimodal, global search | Differential Evolution or SCE-UA | Thorough exploration |
| Quick global search | Particle Swarm | Faster but less thorough |
| Hydrological calibration | SCE-UA | Specifically designed for this |
| Expensive simulation calibration | DDS | Scales to a fixed run budget; polish for precision |
| Correlated or badly scaled parameters | CMA-ES | Learns the covariance; IPOP restarts for multimodal problems |
| Staged decisions (reservoir rules) | Dynamic Programming | Exact on a discretized state space |
| Budget-constrained selection | Knapsack | Exact for integer costs |
| With constraints | Augmented Lagrangian | Handles equality and inequality constraints |
| 1D problem | Brent Search or Golden Section | Specialized efficient methods |
| Machine learning | ADAM | Adaptive learning rates |
| Unknown smoothness | Start with Nelder-Mead | Very robust |
| High dimensions (>20) | Differential Evolution or ADAM | Scale better than others |
-
Scale Variables: Normalize parameters to similar ranges (e.g., [0,1] or [-1,1]) for better convergence.
-
Set Reasonable Bounds: Tight bounds improve convergence but shouldn't exclude the optimum.
-
Multiple Runs: For global optimization, run multiple times with different random seeds to ensure robustness.
-
Hybrid Approach: Use global method followed by local refinement:
// Global search var de = new DifferentialEvolution(ObjectiveFunc, n, lower, upper); de.Minimize(); // Local refinement var bfgs = new BFGS(ObjectiveFunc, n, de.BestParameterSet.Values, lower, upper); bfgs.Minimize();
-
Monitor Convergence: Check the
ParameterSetTraceto diagnose convergence issues. It holds one entry per function evaluation, each the best point found so far:for (int k = 0; k < optimizer.ParameterSetTrace.Count; k++) { Console.WriteLine($"Evaluation {k + 1}: best fitness so far {optimizer.ParameterSetTrace[k].Fitness}"); }
-
Adjust Tolerances: Tighter tolerances require more evaluations:
optimizer.RelativeTolerance = 1e-10; // Very tight optimizer.AbsoluteTolerance = 1e-10;
-
Population Size: For global methods, larger populations explore better but cost more:
de.PopulationSize = 20 * NumberOfParameters; // Rule of thumb
The BestParameterSet contains:
Values: The optimal parameter vectorFitness: The objective function value at the optimumWeight: (Optional) Used internally by some algorithms
When ComputeHessian = true, the Hessian at the solution is computed numerically. The Hessian
- All eigenvalues positive: The solution is a local minimum (the Hessian is positive definite)
- Large eigenvalue: The objective is highly curved in that direction, so the corresponding parameter is well-determined
- Small eigenvalue: The objective is nearly flat, so the parameter is poorly determined or identifiable
- Near-zero eigenvalue: Indicates a ridge or valley in the objective surface, suggesting parameter correlation or redundancy
When the objective is a negative log-likelihood, the inverse of the Hessian at the minimum approximates the covariance matrix of the parameters, so standard errors are $SE(\hat{\theta}i) \approx \sqrt{(\mathbf{H}^{-1}){ii}}$. Invert the whole matrix:
optimizer.ComputeHessian = true;
optimizer.Minimize();
// Covariance approximation from the inverse Hessian
var covariance = optimizer.Hessian!.Inverse();
// Parameter standard errors (approximation)
for (int i = 0; i < optimizer.NumberOfParameters; i++)
{
double se = Math.Sqrt(covariance[i, i]);
Console.WriteLine($"Parameter {i} ± {se:F4}");
}Check the Status property to verify success:
if (optimizer.Status == OptimizationStatus.Success)
{
Console.WriteLine("Optimization converged successfully");
}
else if (optimizer.Status == OptimizationStatus.MaximumIterationsReached)
{
Console.WriteLine("Maximum iterations reached - may not have converged");
}-
Function Evaluation Cost: Most optimization time is spent evaluating the objective function. Optimize your function first.
-
Parallel Evaluation: Differential Evolution and Particle Swarm can evaluate each generation in parallel (
UseParallel), reproducibly for any number of threads. The other optimizers evaluate one point at a time; for an expensive objective, parallelize inside the objective function itself (for example, across the events or reaches of one simulation). -
Warm Start: If solving similar problems repeatedly, use the previous solution as initial guess.
-
Gradient Information:
BFGS,GradientDescent, andADAMaccept an optional analytic gradient in their constructors; without one they use finite differences, which cost about$2D$ extra evaluations per gradient. -
Overhead: Differential Evolution, Simulated Annealing, Particle Swarm, Powell, Brent, Multi-Start, and MLSL reuse their working arrays rather than allocating per trial point, so the optimizer's own cost is small next to even a cheap objective.
[1] Nocedal, J., & Wright, S. J. (2006). Numerical Optimization (2nd ed.). Springer.
[2] Nelder, J. A., & Mead, R. (1965). A simplex method for function minimization. The Computer Journal, 7(4), 308-313.
[3] Kingma, D. P., & Ba, J. (2014). Adam: A method for stochastic optimization. arXiv preprint arXiv:1412.6980.
[4] Storn, R., & Price, K. (1997). Differential evolution: A simple and efficient heuristic for global optimization over continuous spaces. Journal of Global Optimization, 11(4), 341-359.
[5] Kennedy, J., & Eberhart, R. (1995). Particle swarm optimization. Proceedings of IEEE International Conference on Neural Networks, 4, 1942-1948.
[6] Duan, Q., Sorooshian, S., & Gupta, V. (1992). Effective and efficient global optimization for conceptual rainfall-runoff models. Water Resources Research, 28(4), 1015-1031.
[7] Kirkpatrick, S., Gelatt, C. D., & Vecchi, M. P. (1983). Optimization by simulated annealing. Science, 220(4598), 671-680.
[8] Birgin, E. G., & Martínez, J. M. (2014). Practical Augmented Lagrangian Methods for Constrained Optimization. SIAM.
[9] Dijkstra, E. W. (1959). A note on two problems in connexion with graphs. Numerische Mathematik, 1, 269-271.
[10] Bellman, R. (1957). Dynamic Programming. Princeton University Press.
[11] Loucks, D. P., & van Beek, E. (2017). Water Resource Systems Planning and Management. Springer. https://doi.org/10.1007/978-3-319-44234-1
[12] Labadie, J. W. (2004). Optimal operation of multireservoir systems: State-of-the-art review. Journal of Water Resources Planning and Management, 130(2), 93-111. https://doi.org/10.1061/(ASCE)0733-9496(2004)130:2(93)
[13] Puterman, M. L. (1994). Markov Decision Processes: Discrete Stochastic Dynamic Programming. Wiley. https://doi.org/10.1002/9780470316887
[14] Tolson, B. A., & Shoemaker, C. A. (2007). Dynamically dimensioned search algorithm for computationally efficient watershed model calibration. Water Resources Research, 43, W01413. https://doi.org/10.1029/2005WR004723
[15] Deb, K., Pratap, A., Agarwal, S., & Meyarivan, T. (2002). A fast and elitist multiobjective genetic algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), 182-197. https://doi.org/10.1109/4235.996017
[16] Zitzler, E., & Thiele, L. (1999). Multiobjective evolutionary algorithms: A comparative case study and the strength Pareto approach. IEEE Transactions on Evolutionary Computation, 3(4), 257-271. https://doi.org/10.1109/4235.797969
[17] Beume, N., Fonseca, C. M., López-Ibáñez, M., Paquete, L., & Vahrenhold, J. (2009). On the complexity of computing the hypervolume indicator. IEEE Transactions on Evolutionary Computation, 13(5), 1075-1082. https://doi.org/10.1109/TEVC.2009.2015575
[18] While, L., Bradstreet, L., & Barone, L. (2012). A fast way of calculating exact hypervolumes. IEEE Transactions on Evolutionary Computation, 16(1), 86-95. https://doi.org/10.1109/TEVC.2010.2077298
[19] Van Veldhuizen, D. A., & Lamont, G. B. (2000). On measuring multiobjective evolutionary algorithm performance. Proceedings of the 2000 Congress on Evolutionary Computation, 204-211. https://doi.org/10.1109/CEC.2000.870296
[20] Coello Coello, C. A., & Reyes Sierra, M. (2004). A study of the parallelization of a coevolutionary multi-objective evolutionary algorithm. In MICAI 2004: Advances in Artificial Intelligence, Lecture Notes in Computer Science (pp. 688-697). Springer. https://doi.org/10.1007/978-3-540-24694-7_71
[21] Rinnooy Kan, A. H. G., & Timmer, G. T. (1987). Stochastic global optimization methods part II: Multi level methods. Mathematical Programming, 39, 57-78. https://doi.org/10.1007/BF02592071
[22] Duan, Q., Sorooshian, S., & Gupta, V. K. (1994). Optimal use of the SCE-UA global optimization method for calibrating watershed models. Journal of Hydrology, 158(3-4), 265-284. https://doi.org/10.1016/0022-1694(94)90057-4
[23] Hansen, N. (2023). The CMA evolution strategy: A tutorial. arXiv preprint arXiv:1604.00772v2.
[24] Hansen, N., & Ostermeier, A. (2001). Completely derandomized self-adaptation in evolution strategies. Evolutionary Computation, 9(2), 159-195.
[25] Auger, A., & Hansen, N. (2005). A restart CMA evolution strategy with increasing population size. 2005 IEEE Congress on Evolutionary Computation, 2, 1769-1776.
[26] Hansen, N., Akimoto, Y., Baudis, P., et al. CMA-ES/pycma (version 4.5.0). Zenodo. https://doi.org/10.5281/zenodo.2559634
[27] Byrd, R. H., Lu, P., Nocedal, J., & Zhu, C. (1995). A limited memory algorithm for bound constrained optimization. SIAM Journal on Scientific Computing, 16(5), 1190-1208. https://doi.org/10.1137/0916069
[28] Zhu, C., Byrd, R. H., Lu, P., & Nocedal, J. (1997). Algorithm 778: L-BFGS-B: Fortran subroutines for large-scale bound-constrained optimization. ACM Transactions on Mathematical Software, 23(4), 550-560. https://doi.org/10.1145/279232.279236
[29] Morales, J. L., & Nocedal, J. (2011). Remark on "Algorithm 778: L-BFGS-B: Fortran subroutines for large-scale bound constrained optimization". ACM Transactions on Mathematical Software, 38(1), 7. https://doi.org/10.1145/2049662.2049669
[30] Moré, J. J., & Thuente, D. J. (1994). Line search algorithms with guaranteed sufficient decrease. ACM Transactions on Mathematical Software, 20(3), 286-307. https://doi.org/10.1145/192115.192132
← Previous: Numerical Differentiation | Back to Index | Next: Root Finding →