github.com/gopherd/gonum@v0.0.4/optimize/functions/functions_test.go (about)

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package functions
     6  
     7  import "testing"
     8  
     9  func TestBeale(t *testing.T) {
    10  	t.Parallel()
    11  	tests := []funcTest{
    12  		{
    13  			X:        []float64{1, 1},
    14  			F:        14.203125,
    15  			Gradient: []float64{0, 27.75},
    16  		},
    17  		{
    18  			X:        []float64{1, 4},
    19  			F:        4624.453125,
    20  			Gradient: []float64{8813.25, 6585},
    21  		},
    22  	}
    23  	testFunction(Beale{}, tests, t)
    24  }
    25  
    26  func TestBiggsEXP2(t *testing.T) {
    27  	t.Parallel()
    28  	tests := []funcTest{
    29  		{
    30  			X:        []float64{1, 2},
    31  			F:        32.26255055084012,
    32  			Gradient: []float64{8.308203800550878, -25.32607145221645},
    33  		},
    34  	}
    35  	testFunction(BiggsEXP2{}, tests, t)
    36  }
    37  
    38  func TestBiggsEXP3(t *testing.T) {
    39  	t.Parallel()
    40  	tests := []funcTest{
    41  		{
    42  			X:        []float64{1, 2, 1},
    43  			F:        1.598844540607779,
    44  			Gradient: []float64{1.0633795027631927, -0.5196392672262664, -0.3180919155433357},
    45  		},
    46  	}
    47  	testFunction(BiggsEXP3{}, tests, t)
    48  }
    49  
    50  func TestBiggsEXP4(t *testing.T) {
    51  	t.Parallel()
    52  	tests := []funcTest{
    53  		{
    54  			X: []float64{1, 2, 1, 1},
    55  			F: 1.598844540607779,
    56  			Gradient: []float64{1.0633795027631927, -0.5196392672262664,
    57  				-0.44245622408151464, -0.3180919155433357},
    58  		},
    59  	}
    60  	testFunction(BiggsEXP4{}, tests, t)
    61  }
    62  
    63  func TestBiggsEXP5(t *testing.T) {
    64  	t.Parallel()
    65  	tests := []funcTest{
    66  		{
    67  			X: []float64{1, 2, 1, 1, 1},
    68  			F: 13.386420552801937,
    69  			Gradient: []float64{-6.54665204477596, 3.5259856535515293,
    70  				14.36984212995392, -9.522506150695783, -19.639956134327882},
    71  		},
    72  	}
    73  	testFunction(BiggsEXP5{}, tests, t)
    74  }
    75  
    76  func TestBiggsEXP6(t *testing.T) {
    77  	t.Parallel()
    78  	tests := []funcTest{
    79  		{
    80  			X: []float64{1, 2, 1, 1, 1, 1},
    81  			F: 0.77907007565597,
    82  			Gradient: []float64{-0.149371887533426, -0.183163468182936,
    83  				-1.483958013575642, 1.428277503849742, -0.149371887533426,
    84  				-1.483958013575642},
    85  		},
    86  	}
    87  	testFunction(BiggsEXP6{}, tests, t)
    88  }
    89  
    90  func TestBox3D(t *testing.T) {
    91  	t.Parallel()
    92  	tests := []funcTest{
    93  		{
    94  			X:        []float64{0, 10, 20},
    95  			F:        1031.1538106093985,
    96  			Gradient: []float64{98.22343149849218, -2.11937420675874, 112.38817362220350},
    97  		},
    98  	}
    99  	testFunction(Box3D{}, tests, t)
   100  }
   101  
   102  func TestBrownBadlyScaled(t *testing.T) {
   103  	t.Parallel()
   104  	tests := []funcTest{
   105  		{
   106  			X:        []float64{1, 1},
   107  			F:        999998000003,
   108  			Gradient: []float64{-2e+6, -4e-6},
   109  		},
   110  	}
   111  	testFunction(BrownBadlyScaled{}, tests, t)
   112  }
   113  
   114  // TODO(vladimir-ch): The minimum of BrownAndDennis is not known accurately
   115  // enough, which would force defaultGradTol to be unnecessarily large for the
   116  // tests to pass. This is the only function that causes problems, so disable
   117  // this test until the minimum is more accurate.
   118  // func TestBrownAndDennis(t *testing.T) {
   119  // 	tests := []funcTest{
   120  // 		{
   121  // 			X:        []float64{25, 5, -5, -1},
   122  // 			F:        7926693.33699744,
   123  // 			Gradient: []float64{1149322.836365895, 1779291.674339785, -254579.585463521, -173400.429253115},
   124  // 		},
   125  // 	}
   126  // 	testFunction(BrownAndDennis{}, tests, t)
   127  // }
   128  
   129  func TestExtendedPowellSingular(t *testing.T) {
   130  	t.Parallel()
   131  	tests := []funcTest{
   132  		{
   133  			X:        []float64{3, -1, 0, 3},
   134  			F:        95,
   135  			Gradient: []float64{-14, -144, -22, 30},
   136  		},
   137  		{
   138  			X:        []float64{3, -1, 0, 3, 3, -1, 0, 3},
   139  			F:        190,
   140  			Gradient: []float64{-14, -144, -22, 30, -14, -144, -22, 30},
   141  		},
   142  	}
   143  	testFunction(ExtendedPowellSingular{}, tests, t)
   144  }
   145  
   146  func TestExtendedRosenbrock(t *testing.T) {
   147  	t.Parallel()
   148  	tests := []funcTest{
   149  		{
   150  			X:        []float64{-1.2, 1},
   151  			F:        24.2,
   152  			Gradient: []float64{-215.6, -88},
   153  		},
   154  		{
   155  			X:        []float64{-1.2, 1, -1.2},
   156  			F:        508.2,
   157  			Gradient: []float64{-215.6, 792, -440},
   158  		},
   159  		{
   160  			X:        []float64{-1.2, 1, -1.2, 1},
   161  			F:        532.4,
   162  			Gradient: []float64{-215.6, 792, -655.6, -88},
   163  		},
   164  	}
   165  	testFunction(ExtendedRosenbrock{}, tests, t)
   166  }
   167  
   168  func TestGaussian(t *testing.T) {
   169  	t.Parallel()
   170  	tests := []funcTest{
   171  		{
   172  			X:        []float64{0.4, 1, 0},
   173  			F:        3.88810699116688e-06,
   174  			Gradient: []float64{7.41428466839991e-03, -7.44126392165149e-04, -5.30189685421989e-20},
   175  		},
   176  	}
   177  	testFunction(Gaussian{}, tests, t)
   178  }
   179  
   180  func TestGulfResearchAndDevelopment(t *testing.T) {
   181  	t.Parallel()
   182  	tests := []funcTest{
   183  		{
   184  			X:        []float64{5, 2.5, 0.15},
   185  			F:        12.11070582556949,
   186  			Gradient: []float64{2.0879783574289799, 0.0345792619697154, -39.6766801029386400},
   187  		},
   188  	}
   189  	testFunction(GulfResearchAndDevelopment{}, tests, t)
   190  }
   191  
   192  func TestHelicalValley(t *testing.T) {
   193  	t.Parallel()
   194  	tests := []funcTest{
   195  		{
   196  			X:        []float64{-1, 0, 0},
   197  			F:        2500,
   198  			Gradient: []float64{0, -1.59154943091895e+03, -1e+03},
   199  		},
   200  	}
   201  	testFunction(HelicalValley{}, tests, t)
   202  }
   203  
   204  func TestPenaltyI(t *testing.T) {
   205  	t.Parallel()
   206  	tests := []funcTest{
   207  		{
   208  			X:        []float64{1, 2, 3, 4},
   209  			F:        885.06264,
   210  			Gradient: []float64{119, 238.00002, 357.00004, 476.00006},
   211  		},
   212  		{
   213  			X: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
   214  			F: 148032.56535,
   215  			Gradient: []float64{1539, 3078.00002, 4617.00004, 6156.00006,
   216  				7695.00008, 9234.0001, 10773.00012, 12312.00014, 13851.00016, 15390.00018},
   217  		},
   218  	}
   219  	testFunction(PenaltyI{}, tests, t)
   220  }
   221  
   222  func TestPenaltyII(t *testing.T) {
   223  	t.Parallel()
   224  	tests := []funcTest{
   225  		{
   226  			X: []float64{0.5, 0.5, 0.5, 0.5},
   227  			F: 2.34000880546302,
   228  			Gradient: []float64{12.59999952896435, 8.99999885134508,
   229  				5.99999776830493, 2.99999875380719},
   230  		},
   231  		{
   232  			X: []float64{0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5},
   233  			F: 162.65277656596712,
   234  			Gradient: []float64{255.5999995289644, 229.4999988513451,
   235  				203.9999977683049, 178.4999965713605, 152.9999952485322,
   236  				127.4999937865809, 101.9999921708749, 76.4999903852436,
   237  				50.9999884118158, 25.4999938418451},
   238  		},
   239  	}
   240  	testFunction(PenaltyII{}, tests, t)
   241  }
   242  
   243  func TestPowelBadlyScaled(t *testing.T) {
   244  	t.Parallel()
   245  	tests := []funcTest{
   246  		{
   247  			X:        []float64{0, 1},
   248  			F:        1.13526171734838,
   249  			Gradient: []float64{-2.00007355588823e+04, -2.70596990584991e-01},
   250  		},
   251  	}
   252  	testFunction(PowellBadlyScaled{}, tests, t)
   253  }
   254  
   255  func TestTrigonometric(t *testing.T) {
   256  	t.Parallel()
   257  	tests := []funcTest{
   258  		{
   259  			X:        []float64{0.5, 0.5},
   260  			F:        0.0126877761614045,
   261  			Gradient: []float64{-0.00840962732040673, -0.09606967736232540},
   262  		},
   263  		{
   264  			X: []float64{0.2, 0.2, 0.2, 0.2, 0.2},
   265  			F: 0.0116573789904718,
   266  			Gradient: []float64{0.04568602319608119, -0.00896259022885634,
   267  				-0.04777056509084983, -0.07073790138989976, -0.07786459912600564},
   268  		},
   269  		{
   270  			X: []float64{0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1},
   271  			F: 0.00707575946622261,
   272  			Gradient: []float64{0.03562782195259399, 0.01872017956076182,
   273  				0.00380754216611998, -0.00911009023133202, -0.02003271763159338,
   274  				-0.02896034003466506, -0.03589295744054654, -0.04083056984923782,
   275  				-0.04377317726073873, -0.04472077967504980},
   276  		},
   277  	}
   278  	testFunction(Trigonometric{}, tests, t)
   279  }
   280  
   281  func TestVariablyDimensioned(t *testing.T) {
   282  	t.Parallel()
   283  	tests := []funcTest{
   284  		{
   285  			X:        []float64{0.5, 0},
   286  			F:        46.5625,
   287  			Gradient: []float64{-68.5, -137},
   288  		},
   289  		{
   290  			X:        []float64{2.0 / 3, 1.0 / 3, 0},
   291  			F:        497.60493827160514,
   292  			Gradient: []float64{-416.518518518519, -833.037037037037, -1249.555555555556},
   293  		},
   294  		{
   295  			X:        []float64{0.75, 0.5, 0.25, 0},
   296  			F:        3222.1875,
   297  			Gradient: []float64{-1703, -3406, -5109, -6812},
   298  		},
   299  	}
   300  	testFunction(VariablyDimensioned{}, tests, t)
   301  }
   302  
   303  func TestWatson(t *testing.T) {
   304  	t.Parallel()
   305  	tests := []funcTest{
   306  		{
   307  			X:        []float64{0, 0},
   308  			F:        30,
   309  			Gradient: []float64{0, -60},
   310  		},
   311  		{
   312  			X: []float64{0, 0, 0, 0, 0, 0},
   313  			F: 30,
   314  			Gradient: []float64{0, -60, -60, -61.034482758620697,
   315  				-62.068965517241381, -63.114928861371936},
   316  		},
   317  		{
   318  			X: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0},
   319  			F: 30,
   320  			Gradient: []float64{0, -60, -60, -61.034482758620697,
   321  				-62.068965517241381, -63.114928861371936, -64.172372791012350,
   322  				-65.241283655050239, -66.321647802373235},
   323  		},
   324  		{
   325  			X: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   326  			F: 30,
   327  			Gradient: []float64{0, -60, -60, -61.034482758620697,
   328  				-62.068965517241381, -63.114928861371936, -64.172372791012350,
   329  				-65.241283655050239, -66.321647802373235, -67.413448880864095,
   330  				-68.516667837400661, -69.631282933991471},
   331  		},
   332  	}
   333  	testFunction(Watson{}, tests, t)
   334  }
   335  
   336  func TestWood(t *testing.T) {
   337  	t.Parallel()
   338  	tests := []funcTest{
   339  		{
   340  			X:        []float64{-3, -1, -3, -1},
   341  			F:        19192,
   342  			Gradient: []float64{-12008, -2080, -10808, -1880},
   343  		},
   344  	}
   345  	testFunction(Wood{}, tests, t)
   346  }