Skip to content

Commit b2ba2f2

Browse files
committed
Add ILeastSquaresMinimizer interface
- Introduce a unified interface for nonlinear least squares minimization. - Define two overloads of the FindMinimum method (one accepting a Vector<double> and one accepting a double[]). - Update LevenbergMarquardtMinimizer and TrustRegionMinimizerBase classes to implement ILeastSquaresMinimizer.
1 parent 84e9432 commit b2ba2f2

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// <copyright file="ILeastSquaresMinimizer.cs" company="Math.NET">
2+
// Math.NET Numerics, part of the Math.NET Project
3+
// https://numerics.mathdotnet.com
4+
// https://github.com/mathnet/mathnet-numerics
5+
//
6+
// Copyright (c) 2009-$CURRENT_YEAR$ Math.NET
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the "Software"), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
// </copyright>
29+
30+
using MathNet.Numerics.LinearAlgebra;
31+
using System.Collections.Generic;
32+
33+
namespace MathNet.Numerics.Optimization
34+
{
35+
/// <summary>
36+
/// Interface for solving nonlinear least squares problems.
37+
/// This interface unifies the Levenberg-Marquardt and Trust Region minimization algorithms.
38+
/// </summary>
39+
public interface ILeastSquaresMinimizer
40+
{
41+
/// <summary>
42+
/// Finds the minimum of a nonlinear least squares problem using the specified objective model and initial guess vector.
43+
/// </summary>
44+
/// <param name="objective">The objective function model to be minimized.</param>
45+
/// <param name="initialGuess">The initial guess vector.</param>
46+
/// <param name="lowerBound">Optional lower bound for the parameters.</param>
47+
/// <param name="upperBound">Optional upper bound for the parameters.</param>
48+
/// <param name="scales">Optional scales for the parameters.</param>
49+
/// <param name="isFixed">Optional list indicating which parameters are fixed.</param>
50+
/// <returns>A <see cref="NonlinearMinimizationResult"/> containing the results of the minimization.</returns>
51+
NonlinearMinimizationResult FindMinimum(IObjectiveModel objective, Vector<double> initialGuess,
52+
Vector<double> lowerBound = null, Vector<double> upperBound = null, Vector<double> scales = null, List<bool> isFixed = null);
53+
54+
/// <summary>
55+
/// Finds the minimum of a nonlinear least squares problem using the specified objective model and initial guess array.
56+
/// </summary>
57+
/// <param name="objective">The objective function model to be minimized.</param>
58+
/// <param name="initialGuess">The initial guess array.</param>
59+
/// <param name="lowerBound">Optional lower bound array for the parameters.</param>
60+
/// <param name="upperBound">Optional upper bound array for the parameters.</param>
61+
/// <param name="scales">Optional scales array for the parameters.</param>
62+
/// <param name="isFixed">Optional array indicating which parameters are fixed.</param>
63+
/// <returns>A <see cref="NonlinearMinimizationResult"/> containing the results of the minimization.</returns>
64+
NonlinearMinimizationResult FindMinimum(IObjectiveModel objective, double[] initialGuess,
65+
double[] lowerBound = null, double[] upperBound = null, double[] scales = null, bool[] isFixed = null);
66+
}
67+
}

src/Numerics/Optimization/LevenbergMarquardtMinimizer.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,39 @@
55

66
namespace MathNet.Numerics.Optimization
77
{
8-
public class LevenbergMarquardtMinimizer : NonlinearMinimizerBase
8+
/// <summary>
9+
/// Implements the Levenberg-Marquardt algorithm for solving nonlinear least squares problems.
10+
/// This class inherits from <see cref="NonlinearMinimizerBase"/> and implements <see cref="ILeastSquaresMinimizer"/>.
11+
/// </summary>
12+
public class LevenbergMarquardtMinimizer : NonlinearMinimizerBase, ILeastSquaresMinimizer
913
{
1014
/// <summary>
1115
/// The scale factor for initial mu
1216
/// </summary>
1317
public double InitialMu { get; set; }
1418

19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="LevenbergMarquardtMinimizer"/> class using the Levenberg-Marquardt algorithm.
21+
/// </summary>
22+
/// <param name="initialMu">The initial damping parameter (mu) for the algorithm. Default is 1E-3.</param>
23+
/// <param name="gradientTolerance">The tolerance for the infinity norm of the gradient. Default is 1E-15.</param>
24+
/// <param name="stepTolerance">The tolerance for the parameter update step size. Default is 1E-15.</param>
25+
/// <param name="functionTolerance">The tolerance for the function value (residual sum of squares). Default is 1E-15.</param>
26+
/// <param name="maximumIterations">The maximum number of iterations. Default is -1 (unlimited).</param>
1527
public LevenbergMarquardtMinimizer(double initialMu = 1E-3, double gradientTolerance = 1E-15, double stepTolerance = 1E-15, double functionTolerance = 1E-15, int maximumIterations = -1)
1628
: base(gradientTolerance, stepTolerance, functionTolerance, maximumIterations)
1729
{
1830
InitialMu = initialMu;
1931
}
2032

33+
/// <inheritdoc/>
2134
public NonlinearMinimizationResult FindMinimum(IObjectiveModel objective, Vector<double> initialGuess,
2235
Vector<double> lowerBound = null, Vector<double> upperBound = null, Vector<double> scales = null, List<bool> isFixed = null)
2336
{
2437
return Minimum(objective, initialGuess, lowerBound, upperBound, scales, isFixed, InitialMu, GradientTolerance, StepTolerance, FunctionTolerance, MaximumIterations);
2538
}
2639

40+
/// <inheritdoc/>
2741
public NonlinearMinimizationResult FindMinimum(IObjectiveModel objective, double[] initialGuess,
2842
double[] lowerBound = null, double[] upperBound = null, double[] scales = null, bool[] isFixed = null)
2943
{

src/Numerics/Optimization/TrustRegion/TrustRegionMinimizerBase.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
namespace MathNet.Numerics.Optimization.TrustRegion
77
{
8-
public abstract class TrustRegionMinimizerBase : NonlinearMinimizerBase
8+
/// <summary>
9+
/// Abstract base class for trust region minimizers that solve nonlinear least squares problems.
10+
/// This class inherits from <see cref="NonlinearMinimizerBase"/> and implements <see cref="ILeastSquaresMinimizer"/>.
11+
/// </summary>
12+
public abstract class TrustRegionMinimizerBase : NonlinearMinimizerBase, ILeastSquaresMinimizer
913
{
1014
/// <summary>
1115
/// The trust region subproblem.
@@ -17,6 +21,15 @@ public abstract class TrustRegionMinimizerBase : NonlinearMinimizerBase
1721
/// </summary>
1822
public double RadiusTolerance { get; set; }
1923

24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="TrustRegionMinimizerBase"/> class using the specified trust region subproblem.
26+
/// </summary>
27+
/// <param name="subproblem">The trust region subproblem to be solved at each iteration.</param>
28+
/// <param name="gradientTolerance">The tolerance for the infinity norm of the gradient. Default is 1E-8.</param>
29+
/// <param name="stepTolerance">The tolerance for the parameter update step size. Default is 1E-8.</param>
30+
/// <param name="functionTolerance">The tolerance for the function value (residual sum of squares). Default is 1E-8.</param>
31+
/// <param name="radiusTolerance">The tolerance for the trust region radius. Default is 1E-8.</param>
32+
/// <param name="maximumIterations">The maximum number of iterations. Default is -1 (unlimited).</param>
2033
public TrustRegionMinimizerBase(ITrustRegionSubproblem subproblem,
2134
double gradientTolerance = 1E-8, double stepTolerance = 1E-8, double functionTolerance = 1E-8, double radiusTolerance = 1E-8, int maximumIterations = -1)
2235
: base(gradientTolerance, stepTolerance, functionTolerance, maximumIterations)
@@ -25,13 +38,15 @@ public TrustRegionMinimizerBase(ITrustRegionSubproblem subproblem,
2538
RadiusTolerance = radiusTolerance;
2639
}
2740

41+
/// <inheritdoc/>
2842
public NonlinearMinimizationResult FindMinimum(IObjectiveModel objective, Vector<double> initialGuess,
2943
Vector<double> lowerBound = null, Vector<double> upperBound = null, Vector<double> scales = null, List<bool> isFixed = null)
3044
{
3145
return Minimum(Subproblem, objective, initialGuess, lowerBound, upperBound, scales, isFixed,
3246
GradientTolerance, StepTolerance, FunctionTolerance, RadiusTolerance, MaximumIterations);
3347
}
3448

49+
/// <inheritdoc/>
3550
public NonlinearMinimizationResult FindMinimum(IObjectiveModel objective, double[] initialGuess,
3651
double[] lowerBound = null, double[] upperBound = null, double[] scales = null, bool[] isFixed = null)
3752
{

0 commit comments

Comments
 (0)