From 83d609727df58cb71cb92bf43057054bde6b21f7 Mon Sep 17 00:00:00 2001 From: Tom McTiernan Date: Wed, 2 Oct 2019 14:16:12 +0100 Subject: [PATCH 1/3] Detect early completion in BfgsBMinimizer --- .../OptimizationTests/BfgsBMinimizerTests.cs | 36 +++++++++++++++++++ src/Numerics/Optimization/BfgsBMinimizer.cs | 8 ++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Numerics.Tests/OptimizationTests/BfgsBMinimizerTests.cs b/src/Numerics.Tests/OptimizationTests/BfgsBMinimizerTests.cs index fd9be504..e98582f6 100644 --- a/src/Numerics.Tests/OptimizationTests/BfgsBMinimizerTests.cs +++ b/src/Numerics.Tests/OptimizationTests/BfgsBMinimizerTests.cs @@ -121,6 +121,42 @@ namespace MathNet.Numerics.UnitTests.OptimizationTests Assert.That(Math.Abs(result.MinimizingPoint[1] - RosenbrockFunction.Minimum[1]), Is.LessThan(1e-3)); } + [Test] + public void FindMinimum_Quadratic() + { + var obj = ObjectiveFunction.Gradient( + x => x[0] * x[0] + x[1] * x[1], + x => new DenseVector(new[] {2 * x[0], 2 * x[1]}) + ); + var solver = new BfgsBMinimizer(1e-5, 1e-5, 1e-5, maximumIterations: 1000); + var lowerBound = new DenseVector(new[] {-1.0, -1.0}); + var upperBound = new DenseVector(new[] {2.0, 2.0}); + var initialGuess = new DenseVector(new[] {1.5, 1.5}); + + var result = solver.FindMinimum(obj, lowerBound, upperBound, initialGuess); + + Assert.That(Math.Abs(result.MinimizingPoint[0] - 0.0), Is.LessThan(1e-3)); + Assert.That(Math.Abs(result.MinimizingPoint[1] - 0.0), Is.LessThan(1e-3)); + } + + [Test] + public void FindMinimum_Quadratic_TwoBoundaries() + { + var obj = ObjectiveFunction.Gradient( + x => x[0] * x[0] + x[1] * x[1], + x => new DenseVector(new[] {2 * x[0], 2 * x[1]}) + ); + var solver = new BfgsBMinimizer(1e-5, 1e-5, 1e-5, maximumIterations: 1000); + var lowerBound = new DenseVector(new[] {1.0, 1.0}); + var upperBound = new DenseVector(new[] {2.0, 2.0}); + var initialGuess = new DenseVector(new[] {1.5, 1.5}); + + var result = solver.FindMinimum(obj, lowerBound, upperBound, initialGuess); + + Assert.That(Math.Abs(result.MinimizingPoint[0] - 1.0), Is.LessThan(1e-3)); + Assert.That(Math.Abs(result.MinimizingPoint[1] - 1.0), Is.LessThan(1e-3)); + } + [Test] public void FindMinimum_Rosenbrock_MinimumGreateerOrEqualToLowerBoundary() { diff --git a/src/Numerics/Optimization/BfgsBMinimizer.cs b/src/Numerics/Optimization/BfgsBMinimizer.cs index 524f7f08..a1f79d96 100644 --- a/src/Numerics/Optimization/BfgsBMinimizer.cs +++ b/src/Numerics/Optimization/BfgsBMinimizer.cs @@ -74,7 +74,7 @@ namespace MathNet.Numerics.Optimization ValidateGradientAndObjective(objective); // Check that we're not already done - ExitCondition currentExitCondition = ExitCriteriaSatisfied(objective, null, 0); + var currentExitCondition = ExitCriteriaSatisfied(objective, null, 0); if (currentExitCondition != ExitCondition.None) return new MinimizationResult(objective, 0, currentExitCondition); @@ -140,6 +140,12 @@ namespace MathNet.Numerics.Optimization var previousPoint = objective.Fork(); var candidatePoint = lineSearchResult.FunctionInfoAtMinimum; + + // Check that we're not done + currentExitCondition = ExitCriteriaSatisfied(candidatePoint, previousPoint, 0); + if (currentExitCondition != ExitCondition.None) + return new MinimizationResult(candidatePoint, 0, currentExitCondition); + var gradient = candidatePoint.Gradient; var step = candidatePoint.Point - initialGuess; From ac733b8ae3259d366471542e1c6d7b7b3c2731a0 Mon Sep 17 00:00:00 2001 From: Tom McTiernan Date: Thu, 3 Oct 2019 17:04:38 +0100 Subject: [PATCH 2/3] Make extra validity check --- .paket/Paket.Restore.targets | 167 ++++++++++++-------- src/Numerics/Optimization/BfgsBMinimizer.cs | 1 + 2 files changed, 104 insertions(+), 64 deletions(-) diff --git a/.paket/Paket.Restore.targets b/.paket/Paket.Restore.targets index 818b4eca..a7955581 100644 --- a/.paket/Paket.Restore.targets +++ b/.paket/Paket.Restore.targets @@ -5,6 +5,11 @@ $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + + $(MSBuildVersion) + 15.0.0 + false + true true $(MSBuildThisFileDirectory) @@ -22,44 +27,13 @@ $(PaketRootPath)paket.bootstrapper.exe $(PaketToolsPath)paket.bootstrapper.exe $([System.IO.Path]::GetDirectoryName("$(PaketBootStrapperExePath)"))\ - - - - - $(PaketRootPath)paket.exe - $(PaketToolsPath)paket.exe - $(PaketToolsPath)paket.exe - $(_PaketBootStrapperExeDir)paket.exe - paket.exe - - - $(PaketRootPath)paket - $(PaketToolsPath)paket - $(PaketToolsPath)paket - - - $(PaketRootPath)paket.exe - $(PaketToolsPath)paket.exe - - - $(PaketBootStrapperExeDir)paket.exe - - - paket - - - <_PaketExeExtension>$([System.IO.Path]::GetExtension("$(PaketExePath)")) - dotnet "$(PaketExePath)" - $(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)" - "$(PaketExePath)" - - + "$(PaketBootStrapperExePath)" $(MonoPath) --runtime=v4.0.30319 "$(PaketBootStrapperExePath)" - + true true @@ -69,41 +43,102 @@ $(BaseIntermediateOutputPath.TrimEnd('\').TrimEnd('\/')) + + + + + + + + + + + dotnet paket + + + + + + $(PaketRootPath)paket.exe + $(PaketToolsPath)paket.exe + $(PaketToolsPath)paket.exe + $(_PaketBootStrapperExeDir)paket.exe + paket.exe + + + $(PaketRootPath)paket + $(PaketToolsPath)paket + $(PaketToolsPath)paket + + + $(PaketRootPath)paket.exe + $(PaketToolsPath)paket.exe + + + $(PaketBootStrapperExeDir)paket.exe + + + paket + + <_PaketExeExtension>$([System.IO.Path]::GetExtension("$(PaketExePath)")) + dotnet "$(PaketExePath)" + $(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)" + "$(PaketExePath)" + + + + + + + + + + - + + + + - + true $(NoWarn);NU1603;NU1604;NU1605;NU1608 + false + true - - - /usr/bin/shasum "$(PaketRestoreCacheFile)" | /usr/bin/awk '{ print $1 }' - /usr/bin/shasum "$(PaketLockFilePath)" | /usr/bin/awk '{ print $1 }' + + + + + + + $([System.IO.File]::ReadAllText('$(PaketRestoreCacheFile)')) + + + + + + + $([System.Text.RegularExpressions.Regex]::Split(`%(Identity)`, `": "`)[0].Replace(`"`, ``).Replace(` `, ``)) + $([System.Text.RegularExpressions.Regex]::Split(`%(Identity)`, `": "`)[1].Replace(`"`, ``).Replace(` `, ``)) + + + + + %(PaketRestoreCachedKeyValue.Value) + %(PaketRestoreCachedKeyValue.Value) - - - - - - - - - - - - - - $([System.IO.File]::ReadAllText('$(PaketRestoreCacheFile)')) - $([System.IO.File]::ReadAllText('$(PaketLockFilePath)')) + + true - false + false true @@ -116,7 +151,10 @@ + + + @@ -126,7 +164,7 @@ - + $(PaketIntermediateOutputPath)\$(MSBuildProjectFile).paket.references.cached @@ -163,6 +201,7 @@ + @@ -184,7 +223,7 @@ $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[0]) $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[1]) $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[4]) - $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[5]) + $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[5]) %(PaketReferencesFileLinesInfo.PackageVersion) @@ -224,12 +263,10 @@ false - $(MSBuildVersion) - 15.8.0 - + <_NuspecFilesNewLocation Include="$(PaketIntermediateOutputPath)\$(Configuration)\*.nuspec"/> @@ -252,10 +289,12 @@ - <_NuspecFiles Include="$(AdjustedNuspecOutputPath)\*.$(PackageVersion).nuspec"/> + <_NuspecFiles Include="$(AdjustedNuspecOutputPath)\*.$(PackageVersion.Split(`+`)[0]).nuspec"/> - + + + @@ -281,7 +320,7 @@ DevelopmentDependency="$(DevelopmentDependency)" BuildOutputInPackage="@(_BuildOutputInPackage)" TargetPathsToSymbols="@(_TargetPathsToSymbols)" - SymbolPackageFormat="symbols.nupkg" + SymbolPackageFormat="$(SymbolPackageFormat)" TargetFrameworks="@(_TargetFrameworks)" AssemblyName="$(AssemblyName)" PackageOutputPath="$(PackageOutputAbsolutePath)" @@ -328,7 +367,7 @@ DevelopmentDependency="$(DevelopmentDependency)" BuildOutputInPackage="@(_BuildOutputInPackage)" TargetPathsToSymbols="@(_TargetPathsToSymbols)" - SymbolPackageFormat="symbols.nupkg" + SymbolPackageFormat="$(SymbolPackageFormat)" TargetFrameworks="@(_TargetFrameworks)" AssemblyName="$(AssemblyName)" PackageOutputPath="$(PackageOutputAbsolutePath)" diff --git a/src/Numerics/Optimization/BfgsBMinimizer.cs b/src/Numerics/Optimization/BfgsBMinimizer.cs index a1f79d96..773f1b02 100644 --- a/src/Numerics/Optimization/BfgsBMinimizer.cs +++ b/src/Numerics/Optimization/BfgsBMinimizer.cs @@ -140,6 +140,7 @@ namespace MathNet.Numerics.Optimization var previousPoint = objective.Fork(); var candidatePoint = lineSearchResult.FunctionInfoAtMinimum; + ValidateGradientAndObjective(candidatePoint); // Check that we're not done currentExitCondition = ExitCriteriaSatisfied(candidatePoint, previousPoint, 0); From 9d21b2c46636cb80e11a4321be68ce6108633522 Mon Sep 17 00:00:00 2001 From: Tom McTiernan Date: Fri, 4 Oct 2019 13:52:03 +0100 Subject: [PATCH 3/3] Revert accidental .paket change --- .paket/Paket.Restore.targets | 167 ++++++++++++++--------------------- 1 file changed, 64 insertions(+), 103 deletions(-) diff --git a/.paket/Paket.Restore.targets b/.paket/Paket.Restore.targets index a7955581..818b4eca 100644 --- a/.paket/Paket.Restore.targets +++ b/.paket/Paket.Restore.targets @@ -5,11 +5,6 @@ $(MSBuildAllProjects);$(MSBuildThisFileFullPath) - - $(MSBuildVersion) - 15.0.0 - false - true true $(MSBuildThisFileDirectory) @@ -27,13 +22,44 @@ $(PaketRootPath)paket.bootstrapper.exe $(PaketToolsPath)paket.bootstrapper.exe $([System.IO.Path]::GetDirectoryName("$(PaketBootStrapperExePath)"))\ - + + + + + $(PaketRootPath)paket.exe + $(PaketToolsPath)paket.exe + $(PaketToolsPath)paket.exe + $(_PaketBootStrapperExeDir)paket.exe + paket.exe + + + $(PaketRootPath)paket + $(PaketToolsPath)paket + $(PaketToolsPath)paket + + + $(PaketRootPath)paket.exe + $(PaketToolsPath)paket.exe + + + $(PaketBootStrapperExeDir)paket.exe + + + paket + + + <_PaketExeExtension>$([System.IO.Path]::GetExtension("$(PaketExePath)")) + dotnet "$(PaketExePath)" + $(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)" + "$(PaketExePath)" + + "$(PaketBootStrapperExePath)" $(MonoPath) --runtime=v4.0.30319 "$(PaketBootStrapperExePath)" - + true true @@ -43,102 +69,41 @@ $(BaseIntermediateOutputPath.TrimEnd('\').TrimEnd('\/')) - - - - - - - - - - - dotnet paket - - - - - - $(PaketRootPath)paket.exe - $(PaketToolsPath)paket.exe - $(PaketToolsPath)paket.exe - $(_PaketBootStrapperExeDir)paket.exe - paket.exe - - - $(PaketRootPath)paket - $(PaketToolsPath)paket - $(PaketToolsPath)paket - - - $(PaketRootPath)paket.exe - $(PaketToolsPath)paket.exe - - - $(PaketBootStrapperExeDir)paket.exe - - - paket - - <_PaketExeExtension>$([System.IO.Path]::GetExtension("$(PaketExePath)")) - dotnet "$(PaketExePath)" - $(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)" - "$(PaketExePath)" - - - - - - - - - - - - - - + - + true $(NoWarn);NU1603;NU1604;NU1605;NU1608 - false - true - - - - - - - $([System.IO.File]::ReadAllText('$(PaketRestoreCacheFile)')) - - - - - - - $([System.Text.RegularExpressions.Regex]::Split(`%(Identity)`, `": "`)[0].Replace(`"`, ``).Replace(` `, ``)) - $([System.Text.RegularExpressions.Regex]::Split(`%(Identity)`, `": "`)[1].Replace(`"`, ``).Replace(` `, ``)) - - - - - %(PaketRestoreCachedKeyValue.Value) - %(PaketRestoreCachedKeyValue.Value) + + + /usr/bin/shasum "$(PaketRestoreCacheFile)" | /usr/bin/awk '{ print $1 }' + /usr/bin/shasum "$(PaketLockFilePath)" | /usr/bin/awk '{ print $1 }' - - + + + + + + + + + + + + + + $([System.IO.File]::ReadAllText('$(PaketRestoreCacheFile)')) + $([System.IO.File]::ReadAllText('$(PaketLockFilePath)')) true - false + false true @@ -151,10 +116,7 @@ - - - @@ -164,7 +126,7 @@ - + $(PaketIntermediateOutputPath)\$(MSBuildProjectFile).paket.references.cached @@ -201,7 +163,6 @@ - @@ -223,7 +184,7 @@ $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[0]) $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[1]) $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[4]) - $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[5]) + $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[5]) %(PaketReferencesFileLinesInfo.PackageVersion) @@ -263,10 +224,12 @@ false + $(MSBuildVersion) + 15.8.0 - + <_NuspecFilesNewLocation Include="$(PaketIntermediateOutputPath)\$(Configuration)\*.nuspec"/> @@ -289,12 +252,10 @@ - <_NuspecFiles Include="$(AdjustedNuspecOutputPath)\*.$(PackageVersion.Split(`+`)[0]).nuspec"/> + <_NuspecFiles Include="$(AdjustedNuspecOutputPath)\*.$(PackageVersion).nuspec"/> - - - + @@ -320,7 +281,7 @@ DevelopmentDependency="$(DevelopmentDependency)" BuildOutputInPackage="@(_BuildOutputInPackage)" TargetPathsToSymbols="@(_TargetPathsToSymbols)" - SymbolPackageFormat="$(SymbolPackageFormat)" + SymbolPackageFormat="symbols.nupkg" TargetFrameworks="@(_TargetFrameworks)" AssemblyName="$(AssemblyName)" PackageOutputPath="$(PackageOutputAbsolutePath)" @@ -367,7 +328,7 @@ DevelopmentDependency="$(DevelopmentDependency)" BuildOutputInPackage="@(_BuildOutputInPackage)" TargetPathsToSymbols="@(_TargetPathsToSymbols)" - SymbolPackageFormat="$(SymbolPackageFormat)" + SymbolPackageFormat="symbols.nupkg" TargetFrameworks="@(_TargetFrameworks)" AssemblyName="$(AssemblyName)" PackageOutputPath="$(PackageOutputAbsolutePath)"