@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2015 Math.NET
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -28,54 +28,16 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.LinearAlgebra.Double ;
using MathNet.Numerics.Optimization ;
using NUnit.Framework ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using MathNet.Numerics.Optimization ;
using MathNet.Numerics.LinearAlgebra.Double ;
namespace MathNet.Numerics.UnitTests.OptimizationTests
{
[TestFixture]
public class NelderMeadSimplexTests
{
/// <summary>
/// Test that finds the constants of a parable, function adds noise and return the mean square error
/// Copied from the test in https://code.google.com/p/nelder-mead-simplex/
/// </summary>
[Test]
public void FindParableConstantsThatMinimizesErrors ( )
{
var nms = new NelderMeadSimplex ( 1e-6 , 1 0 0 0 ) ;
double a = 5 ;
double b = 1 0 ;
System . Random r = new System . Random ( 2 ) ;
IObjectiveFunction objFun = ObjectiveFunction . Value ( ( constants ) = >
{
double ssq = 0 ;
for ( double x = - 1 0 ; x < 1 0 ; x + = . 1 )
{
double yTrue = a * x * x + b * x + r . NextDouble ( ) ;
double yRegress = constants [ 0 ] * x * x + constants [ 1 ] * x ;
ssq + = Math . Pow ( ( yTrue - yRegress ) , 2 ) ;
}
return ssq ;
} ) ;
var initialGuess = new DenseVector ( 2 ) ;
initialGuess [ 0 ] = 3 ;
initialGuess [ 1 ] = 5 ;
var result = nms . FindMinimum ( objFun , initialGuess ) ;
Assert . NotNull ( result ) ;
Assert . NotNull ( result . MinimizingPoint ) ;
Assert . NotNull ( result . FunctionInfoAtMinimum ) ;
Assert . That ( Math . Abs ( result . MinimizingPoint [ 0 ] - a ) , Is . LessThan ( 1e-2 ) ) ;
Assert . That ( Math . Abs ( result . MinimizingPoint [ 1 ] - b ) , Is . LessThan ( 1e-2 ) ) ;
}
[Test]
public void NMS_FindMinimum_Rosenbrock_Easy ( )
{