diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 0a9f0ed3..77e1579d 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -127,9 +127,8 @@ - - - + + @@ -252,13 +251,14 @@ - + + diff --git a/src/Numerics/RootFinding/BfgsSolver.cs b/src/Numerics/Optimization/BfgsSolver.cs similarity index 91% rename from src/Numerics/RootFinding/BfgsSolver.cs rename to src/Numerics/Optimization/BfgsSolver.cs index a9b4d8a8..e457ac67 100644 --- a/src/Numerics/RootFinding/BfgsSolver.cs +++ b/src/Numerics/Optimization/BfgsSolver.cs @@ -3,9 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // 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 // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without @@ -14,10 +14,10 @@ // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: -// +// // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -30,13 +30,11 @@ using System; -using System.Collections.Generic; using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra.Double; -using System.Linq; -using System.Text; +using MathNet.Numerics.Optimization.LineSearch; -namespace MathNet.Numerics.RootFinding +namespace MathNet.Numerics.Optimization { /// /// Broyden-Fletcher-Goldfarb-Shanno solver for finding function minima @@ -45,8 +43,8 @@ namespace MathNet.Numerics.RootFinding /// public static class BfgsSolver { - private const double gradientTolerance = 1e-5; - private const int maxIterations = 100000; + private const double GradientTolerance = 1e-5; + private const int MaxIterations = 100000; /// /// 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 // it is updated via the Sherman–Morrison formula (http://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula) Matrix H = DenseMatrix.CreateIdentity(dim); - + Vector x = initialGuess; Vector x_old = x; Vector grad; @@ -94,13 +92,13 @@ namespace MathNet.Numerics.RootFinding var yM = y.ToColumnMatrix(); // Update the estimate of the hessian - H = H + H = H - rho * (sM * (yM.TransposeThisAndMultiply(H)) + (H * yM).TransposeAndMultiply(sM)) + rho * rho * (y.DotProduct(H * y) + 1.0 / rho) * (sM.TransposeAndMultiply(sM)); x_old = x; iter++; } - while ((grad.InfinityNorm() > gradientTolerance) && (iter < maxIterations)); + while ((grad.InfinityNorm() > GradientTolerance) && (iter < MaxIterations)); return x; } diff --git a/src/Numerics/RootFinding/WolfeRule.cs b/src/Numerics/Optimization/LineSearch/WolfeRule.cs similarity index 96% rename from src/Numerics/RootFinding/WolfeRule.cs rename to src/Numerics/Optimization/LineSearch/WolfeRule.cs index fa426275..1f30565d 100644 --- a/src/Numerics/RootFinding/WolfeRule.cs +++ b/src/Numerics/Optimization/LineSearch/WolfeRule.cs @@ -3,9 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // 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 // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without @@ -14,10 +14,10 @@ // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: -// +// // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -29,11 +29,9 @@ // using System; -using System.Collections.Generic; -using System.Linq; using MathNet.Numerics.LinearAlgebra; -namespace MathNet.Numerics.RootFinding +namespace MathNet.Numerics.Optimization.LineSearch { /// /// 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; // curvature condition invalid ? - if ((phi_dash < 0.9 * phi0_dash) || !decrease_direction) { + if ((phi_dash < 0.9 * phi0_dash) || !decrease_direction) { // increase interval alpha *= 4.0; } diff --git a/src/UnitTests/RootFindingTests/BfgsTest.cs b/src/UnitTests/OptimizationTests/BfgsTest.cs similarity index 83% rename from src/UnitTests/RootFindingTests/BfgsTest.cs rename to src/UnitTests/OptimizationTests/BfgsTest.cs index 37fc4337..7f612529 100644 --- a/src/UnitTests/RootFindingTests/BfgsTest.cs +++ b/src/UnitTests/OptimizationTests/BfgsTest.cs @@ -3,9 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // 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 // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without @@ -14,10 +14,10 @@ // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: -// +// // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -28,22 +28,17 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using MathNet.Numerics.LinearAlgebra; -using NUnit.Framework; 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")] internal class BfgsTest { - private const double precision = 1e-4; + private const double Precision = 1e-4; [Test] public void MinimizeRosenbrock() @@ -56,14 +51,14 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests private static void CheckRosenbrock(double a, double b, double expectedMin) { 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 x) { - double t1 = (1 - x[0]); + double t1 = (1 - 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 RosenbrockGradient(Vector x) diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 61d1ae4f..92d33f59 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -369,7 +369,7 @@ - +