Browse Source

Optimization: move BFGS over to Optimization namespace

pull/489/head
Christoph Ruegg 11 years ago
committed by Erik Ovegard
parent
commit
dc9c70098b
  1. 8
      src/Numerics/Numerics.csproj
  2. 26
      src/Numerics/Optimization/BfgsSolver.cs
  3. 16
      src/Numerics/Optimization/LineSearch/WolfeRule.cs
  4. 29
      src/UnitTests/OptimizationTests/BfgsTest.cs
  5. 2
      src/UnitTests/UnitTests.csproj

8
src/Numerics/Numerics.csproj

@ -127,9 +127,8 @@
<Compile Include="Optimization\ObjectiveFunctions\GradientObjectiveFunction.cs" /> <Compile Include="Optimization\ObjectiveFunctions\GradientObjectiveFunction.cs" />
<Compile Include="Optimization\ObjectiveFunctions\GradientHessianObjectiveFunction.cs" /> <Compile Include="Optimization\ObjectiveFunctions\GradientHessianObjectiveFunction.cs" />
<Compile Include="Optimization\IObjectiveFunction.cs" /> <Compile Include="Optimization\IObjectiveFunction.cs" />
<Compile Include="Optimization\Implementation\LineSearchResult.cs" /> <Compile Include="Optimization\LineSearch\LineSearchResult.cs" />
<Compile Include="Optimization\Implementation\CheckedObjectiveFunction.cs" /> <Compile Include="Optimization\LineSearch\WeakWolfeLineSearch.cs" />
<Compile Include="Optimization\Implementation\WeakWolfeLineSearch.cs" />
<Compile Include="Optimization\IUnconstrainedMinimizer.cs" /> <Compile Include="Optimization\IUnconstrainedMinimizer.cs" />
<Compile Include="Optimization\MinimizationResult.cs" /> <Compile Include="Optimization\MinimizationResult.cs" />
<Compile Include="Optimization\MinimizationWithLineSearchResult.cs" /> <Compile Include="Optimization\MinimizationWithLineSearchResult.cs" />
@ -252,13 +251,14 @@
<Compile Include="Providers\Common\NativeProviderLoader.cs" /> <Compile Include="Providers\Common\NativeProviderLoader.cs" />
<Compile Include="Random\SystemRandomSource.cs" /> <Compile Include="Random\SystemRandomSource.cs" />
<Compile Include="Random\RandomSeed.cs" /> <Compile Include="Random\RandomSeed.cs" />
<Compile Include="RootFinding\BfgsSolver.cs" /> <Compile Include="Optimization\BfgsSolver.cs" />
<Compile Include="RootFinding\Broyden.cs" /> <Compile Include="RootFinding\Broyden.cs" />
<Compile Include="RootFinding\Cubic.cs" /> <Compile Include="RootFinding\Cubic.cs" />
<Compile Include="RootFinding\NewtonRaphson.cs" /> <Compile Include="RootFinding\NewtonRaphson.cs" />
<Compile Include="RootFinding\RobustNewtonRaphson.cs" /> <Compile Include="RootFinding\RobustNewtonRaphson.cs" />
<Compile Include="RootFinding\Secant.cs" /> <Compile Include="RootFinding\Secant.cs" />
<Compile Include="RootFinding\WolfeRule.cs" /> <Compile Include="RootFinding\WolfeRule.cs" />
<Compile Include="Optimization\LineSearch\WolfeRule.cs" />
<Compile Include="RootFinding\ZeroCrossingBracketing.cs" /> <Compile Include="RootFinding\ZeroCrossingBracketing.cs" />
<Compile Include="RootFinding\Brent.cs" /> <Compile Include="RootFinding\Brent.cs" />
<Compile Include="FindRoots.cs" /> <Compile Include="FindRoots.cs" />

26
src/Numerics/RootFinding/BfgsSolver.cs → src/Numerics/Optimization/BfgsSolver.cs

@ -3,9 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2013 Math.NET // Copyright (c) 2009-2015 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -14,10 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
// //
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -30,13 +30,11 @@
using System; using System;
using System.Collections.Generic;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using System.Linq; using MathNet.Numerics.Optimization.LineSearch;
using System.Text;
namespace MathNet.Numerics.RootFinding namespace MathNet.Numerics.Optimization
{ {
/// <summary> /// <summary>
/// Broyden-Fletcher-Goldfarb-Shanno solver for finding function minima /// Broyden-Fletcher-Goldfarb-Shanno solver for finding function minima
@ -45,8 +43,8 @@ namespace MathNet.Numerics.RootFinding
/// </summary> /// </summary>
public static class BfgsSolver public static class BfgsSolver
{ {
private const double gradientTolerance = 1e-5; private const double GradientTolerance = 1e-5;
private const int maxIterations = 100000; private const int MaxIterations = 100000;
/// <summary> /// <summary>
/// Finds a minimum of a function by the BFGS quasi-Newton method /// Finds a minimum of a function by the BFGS quasi-Newton method
@ -63,7 +61,7 @@ namespace MathNet.Numerics.RootFinding
// H represents the approximation of the inverse hessian matrix // H represents the approximation of the inverse hessian matrix
// it is updated via the Sherman–Morrison formula (http://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula) // it is updated via the Sherman–Morrison formula (http://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula)
Matrix<double> H = DenseMatrix.CreateIdentity(dim); Matrix<double> H = DenseMatrix.CreateIdentity(dim);
Vector<double> x = initialGuess; Vector<double> x = initialGuess;
Vector<double> x_old = x; Vector<double> x_old = x;
Vector<double> grad; Vector<double> grad;
@ -94,13 +92,13 @@ namespace MathNet.Numerics.RootFinding
var yM = y.ToColumnMatrix(); var yM = y.ToColumnMatrix();
// Update the estimate of the hessian // Update the estimate of the hessian
H = H H = H
- rho * (sM * (yM.TransposeThisAndMultiply(H)) + (H * yM).TransposeAndMultiply(sM)) - rho * (sM * (yM.TransposeThisAndMultiply(H)) + (H * yM).TransposeAndMultiply(sM))
+ rho * rho * (y.DotProduct(H * y) + 1.0 / rho) * (sM.TransposeAndMultiply(sM)); + rho * rho * (y.DotProduct(H * y) + 1.0 / rho) * (sM.TransposeAndMultiply(sM));
x_old = x; x_old = x;
iter++; iter++;
} }
while ((grad.InfinityNorm() > gradientTolerance) && (iter < maxIterations)); while ((grad.InfinityNorm() > GradientTolerance) && (iter < MaxIterations));
return x; return x;
} }

16
src/Numerics/RootFinding/WolfeRule.cs → src/Numerics/Optimization/LineSearch/WolfeRule.cs

@ -3,9 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2013 Math.NET // Copyright (c) 2009-2015 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -14,10 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
// //
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -29,11 +29,9 @@
// </copyright> // </copyright>
using System; using System;
using System.Collections.Generic;
using System.Linq;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
namespace MathNet.Numerics.RootFinding namespace MathNet.Numerics.Optimization.LineSearch
{ {
/// <summary> /// <summary>
/// Performs an inexact line search. This is used as a part of quasi-Newton optimization methods to figure /// Performs an inexact line search. This is used as a part of quasi-Newton optimization methods to figure
@ -92,7 +90,7 @@ namespace MathNet.Numerics.RootFinding
double phi_dash = z * grad2; double phi_dash = z * grad2;
// curvature condition invalid ? // curvature condition invalid ?
if ((phi_dash < 0.9 * phi0_dash) || !decrease_direction) { if ((phi_dash < 0.9 * phi0_dash) || !decrease_direction) {
// increase interval // increase interval
alpha *= 4.0; alpha *= 4.0;
} }

29
src/UnitTests/RootFindingTests/BfgsTest.cs → src/UnitTests/OptimizationTests/BfgsTest.cs

@ -3,9 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2013 Math.NET // Copyright (c) 2009-2015 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -14,10 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
// //
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -28,22 +28,17 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using NUnit.Framework;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.RootFinding; using MathNet.Numerics.Optimization;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.RootFindingTests namespace MathNet.Numerics.UnitTests.OptimizationTests
{ {
[TestFixture, Category("RootFinding")] [TestFixture, Category("RootFinding")]
internal class BfgsTest internal class BfgsTest
{ {
private const double precision = 1e-4; private const double Precision = 1e-4;
[Test] [Test]
public void MinimizeRosenbrock() public void MinimizeRosenbrock()
@ -56,14 +51,14 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests
private static void CheckRosenbrock(double a, double b, double expectedMin) private static void CheckRosenbrock(double a, double b, double expectedMin)
{ {
var x = BfgsSolver.Solve(new DenseVector(new[] { a, b }), Rosenbrock, RosenbrockGradient); var x = BfgsSolver.Solve(new DenseVector(new[] { a, b }), Rosenbrock, RosenbrockGradient);
Precision.AlmostEqual(expectedMin, Rosenbrock(x), precision); Numerics.Precision.AlmostEqual(expectedMin, Rosenbrock(x), Precision);
} }
private static double Rosenbrock(Vector<double> x) private static double Rosenbrock(Vector<double> x)
{ {
double t1 = (1 - x[0]); double t1 = (1 - x[0]);
double t2 = (x[1] - x[0] * x[0]); double t2 = (x[1] - x[0] * x[0]);
return t1 * t1 + 100 * t2 * t2; return t1 * t1 + 100 * t2 * t2;
} }
private static Vector<double> RosenbrockGradient(Vector<double> x) private static Vector<double> RosenbrockGradient(Vector<double> x)

2
src/UnitTests/UnitTests.csproj

@ -369,7 +369,7 @@
<Compile Include="OptimizationTests\RosenbrockFunction.cs" /> <Compile Include="OptimizationTests\RosenbrockFunction.cs" />
<Compile Include="OptimizationTests\TestNewtonMinimizer.cs" /> <Compile Include="OptimizationTests\TestNewtonMinimizer.cs" />
<Compile Include="Random\SystemRandomSourceTests.cs" /> <Compile Include="Random\SystemRandomSourceTests.cs" />
<Compile Include="RootFindingTests\BfgsTest.cs" /> <Compile Include="OptimizationTests\BfgsTest.cs" />
<Compile Include="RootFindingTests\BisectionTest.cs" /> <Compile Include="RootFindingTests\BisectionTest.cs" />
<Compile Include="PermutationTest.cs" /> <Compile Include="PermutationTest.cs" />
<Compile Include="PrecisionTest.cs" /> <Compile Include="PrecisionTest.cs" />

Loading…
Cancel
Save