/
MinpackTest.java
1484 lines (1349 loc) · 63 KB
/
MinpackTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.legacy.fitting.leastsquares;
import java.util.Arrays;
import org.apache.commons.math4.legacy.analysis.MultivariateMatrixFunction;
import org.apache.commons.math4.legacy.analysis.MultivariateVectorFunction;
import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
import org.apache.commons.math4.legacy.fitting.leastsquares.LeastSquaresOptimizer.Optimum;
import org.apache.commons.math4.legacy.linear.DiagonalMatrix;
import org.apache.commons.math4.core.jdkmath.JdkMath;
import org.junit.Assert;
import org.junit.Test;
/**
* <p>Some of the unit tests are re-implementations of the MINPACK <a
* href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
* href="http://www.netlib.org/minpack/ex/file22">file22</a> test files.
* The redistribution policy for MINPACK is available <a
* href="http://www.netlib.org/minpack/disclaimer">here</a>, for
* convenience, it is reproduced below.</p>
* <table border="" align="center" bgcolor="#E0E0E0">
* <tr><td>
* Minpack Copyright Notice (1999) University of Chicago.
* All rights reserved
* </td></tr>
* <tr><td>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* <ol>
* <li>Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.</li>
* <li>Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.</li>
* <li>The end-user documentation included with the redistribution, if any,
* must include the following acknowledgment:
* <code>This product includes software developed by the University of
* Chicago, as Operator of Argonne National Laboratory.</code>
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.</li>
* <li><strong>WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
* WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
* UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
* THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
* OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
* OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
* USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
* THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
* DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
* UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
* BE CORRECTED.</strong></li>
* <li><strong>LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
* HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
* ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
* INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
* ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
* PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
* SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
* EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
* POSSIBILITY OF SUCH LOSS OR DAMAGES.</strong></li>
* <ol></td></tr>
* </table>
* @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests)
* @author Burton S. Garbow (original fortran minpack tests)
* @author Kenneth E. Hillstrom (original fortran minpack tests)
* @author Jorge J. More (original fortran minpack tests)
* @author Luc Maisonobe (non-minpack tests and minpack tests Java translation)
*/
public class MinpackTest {
@Test
public void testMinpackLinearFullRank() {
minpackTest(new LinearFullRankFunction(10, 5, 1.0,
5.0, 2.23606797749979), false);
minpackTest(new LinearFullRankFunction(50, 5, 1.0,
8.06225774829855, 6.70820393249937), false);
}
@Test
public void testMinpackLinearRank1() {
minpackTest(new LinearRank1Function(10, 5, 1.0,
291.521868819476, 1.4638501094228), false);
minpackTest(new LinearRank1Function(50, 5, 1.0,
3101.60039334535, 3.48263016573496), false);
}
@Test
public void testMinpackLinearRank1ZeroColsAndRows() {
minpackTest(new LinearRank1ZeroColsAndRowsFunction(10, 5, 1.0), false);
minpackTest(new LinearRank1ZeroColsAndRowsFunction(50, 5, 1.0), false);
}
@Test
public void testMinpackRosenbrok() {
minpackTest(new RosenbrockFunction(new double[] { -1.2, 1.0 },
JdkMath.sqrt(24.2)), false);
minpackTest(new RosenbrockFunction(new double[] { -12.0, 10.0 },
JdkMath.sqrt(1795769.0)), false);
minpackTest(new RosenbrockFunction(new double[] { -120.0, 100.0 },
11.0 * JdkMath.sqrt(169000121.0)), false);
}
@Test
public void testMinpackHelicalValley() {
minpackTest(new HelicalValleyFunction(new double[] { -1.0, 0.0, 0.0 },
50.0), false);
minpackTest(new HelicalValleyFunction(new double[] { -10.0, 0.0, 0.0 },
102.95630140987), false);
minpackTest(new HelicalValleyFunction(new double[] { -100.0, 0.0, 0.0},
991.261822123701), false);
}
@Test
public void testMinpackPowellSingular() {
minpackTest(new PowellSingularFunction(new double[] { 3.0, -1.0, 0.0, 1.0 },
14.6628782986152), false);
minpackTest(new PowellSingularFunction(new double[] { 30.0, -10.0, 0.0, 10.0 },
1270.9838708654), false);
minpackTest(new PowellSingularFunction(new double[] { 300.0, -100.0, 0.0, 100.0 },
126887.903284750), false);
}
@Test
public void testMinpackFreudensteinRoth() {
minpackTest(new FreudensteinRothFunction(new double[] { 0.5, -2.0 },
20.0124960961895, 6.99887517584575,
new double[] {
11.4124844654993,
-0.896827913731509
}), false);
minpackTest(new FreudensteinRothFunction(new double[] { 5.0, -20.0 },
12432.833948863, 6.9988751744895,
new double[] {
11.41300466147456,
-0.896796038685959
}), false);
minpackTest(new FreudensteinRothFunction(new double[] { 50.0, -200.0 },
11426454.595762, 6.99887517242903,
new double[] {
11.412781785788564,
-0.8968051074920405
}), false);
}
@Test
public void testMinpackBard() {
minpackTest(new BardFunction(1.0, 6.45613629515967, 0.0906359603390466,
new double[] {
0.0824105765758334,
1.1330366534715,
2.34369463894115
}), false);
minpackTest(new BardFunction(10.0, 36.1418531596785, 4.17476870138539,
new double[] {
0.840666673818329,
-158848033.259565,
-164378671.653535
}), false);
minpackTest(new BardFunction(100.0, 384.114678637399, 4.17476870135969,
new double[] {
0.840666673867645,
-158946167.205518,
-164464906.857771
}), false);
}
@Test
public void testMinpackKowalikOsborne() {
minpackTest(new KowalikOsborneFunction(new double[] { 0.25, 0.39, 0.415, 0.39 },
0.0728915102882945,
0.017535837721129,
new double[] {
0.192807810476249,
0.191262653354071,
0.123052801046931,
0.136053221150517
}), false);
minpackTest(new KowalikOsborneFunction(new double[] { 2.5, 3.9, 4.15, 3.9 },
2.97937007555202,
0.032052192917937,
new double[] {
728675.473768287,
-14.0758803129393,
-32977797.7841797,
-20571594.1977912
}), false);
minpackTest(new KowalikOsborneFunction(new double[] { 25.0, 39.0, 41.5, 39.0 },
29.9590617016037,
0.0175364017658228,
new double[] {
0.192948328597594,
0.188053165007911,
0.122430604321144,
0.134575665392506
}), false);
}
@Test
public void testMinpackMeyer() {
minpackTest(new MeyerFunction(new double[] { 0.02, 4000.0, 250.0 },
41153.4665543031, 9.37794514651874,
new double[] {
0.00560963647102661,
6181.34634628659,
345.223634624144
}), false);
minpackTest(new MeyerFunction(new double[] { 0.2, 40000.0, 2500.0 },
4168216.89130846, 792.917871779501,
new double[] {
1.42367074157994e-11,
33695.7133432541,
901.268527953801
}), true);
}
@Test
public void testMinpackWatson() {
minpackTest(new WatsonFunction(6, 0.0,
5.47722557505166, 0.0478295939097601,
new double[] {
-0.0157249615083782, 1.01243488232965,
-0.232991722387673, 1.26043101102818,
-1.51373031394421, 0.99299727291842
}), false);
minpackTest(new WatsonFunction(6, 10.0,
6433.12578950026, 0.0478295939096951,
new double[] {
-0.0157251901386677, 1.01243485860105,
-0.232991545843829, 1.26042932089163,
-1.51372776706575, 0.99299573426328
}), false);
minpackTest(new WatsonFunction(6, 100.0,
674256.040605213, 0.047829593911544,
new double[] {
-0.0157247019712586, 1.01243490925658,
-0.232991922761641, 1.26043292929555,
-1.51373320452707, 0.99299901922322
}), false);
minpackTest(new WatsonFunction(9, 0.0,
5.47722557505166, 0.00118311459212420,
new double[] {
-0.153070644166722e-4, 0.999789703934597,
0.0147639634910978, 0.146342330145992,
1.00082109454817, -2.61773112070507,
4.10440313943354, -3.14361226236241,
1.05262640378759
}), false);
minpackTest(new WatsonFunction(9, 10.0,
12088.127069307, 0.00118311459212513,
new double[] {
-0.153071334849279e-4, 0.999789703941234,
0.0147639629786217, 0.146342334818836,
1.00082107321386, -2.61773107084722,
4.10440307655564, -3.14361222178686,
1.05262639322589
}), false);
minpackTest(new WatsonFunction(9, 100.0,
1269109.29043834, 0.00118311459212384,
new double[] {
-0.153069523352176e-4, 0.999789703958371,
0.0147639625185392, 0.146342341096326,
1.00082104729164, -2.61773101573645,
4.10440301427286, -3.14361218602503,
1.05262638516774
}), false);
minpackTest(new WatsonFunction(12, 0.0,
5.47722557505166, 0.217310402535861e-4,
new double[] {
-0.660266001396382e-8, 1.00000164411833,
-0.000563932146980154, 0.347820540050756,
-0.156731500244233, 1.05281515825593,
-3.24727109519451, 7.2884347837505,
-10.271848098614, 9.07411353715783,
-4.54137541918194, 1.01201187975044
}), false);
minpackTest(new WatsonFunction(12, 10.0,
19220.7589790951, 0.217310402518509e-4,
new double[] {
-0.663710223017410e-8, 1.00000164411787,
-0.000563932208347327, 0.347820540486998,
-0.156731503955652, 1.05281517654573,
-3.2472711515214, 7.28843489430665,
-10.2718482369638, 9.07411364383733,
-4.54137546533666, 1.01201188830857
}), false);
minpackTest(new WatsonFunction(12, 100.0,
2018918.04462367, 0.217310402539845e-4,
new double[] {
-0.663806046485249e-8, 1.00000164411786,
-0.000563932210324959, 0.347820540503588,
-0.156731504091375, 1.05281517718031,
-3.24727115337025, 7.28843489775302,
-10.2718482410813, 9.07411364688464,
-4.54137546660822, 1.0120118885369
}), false);
}
@Test
public void testMinpackBox3Dimensional() {
minpackTest(new Box3DimensionalFunction(10, new double[] { 0.0, 10.0, 20.0 },
32.1115837449572), false);
}
@Test
public void testMinpackJennrichSampson() {
minpackTest(new JennrichSampsonFunction(10, new double[] { 0.3, 0.4 },
64.5856498144943, 11.1517793413499,
new double[] {
// 0.2578330049, 0.257829976764542
0.2578199266368004, 0.25782997676455244
}), false);
}
@Test
public void testMinpackBrownDennis() {
minpackTest(new BrownDennisFunction(20,
new double[] { 25.0, 5.0, -5.0, -1.0 },
2815.43839161816, 292.954288244866,
new double[] {
-11.59125141003, 13.2024883984741,
-0.403574643314272, 0.236736269844604
}), false);
minpackTest(new BrownDennisFunction(20,
new double[] { 250.0, 50.0, -50.0, -10.0 },
555073.354173069, 292.954270581415,
new double[] {
-11.5959274272203, 13.2041866926242,
-0.403417362841545, 0.236771143410386
}), false);
minpackTest(new BrownDennisFunction(20,
new double[] { 2500.0, 500.0, -500.0, -100.0 },
61211252.2338581, 292.954306151134,
new double[] {
-11.5902596937374, 13.2020628854665,
-0.403688070279258, 0.236665033746463
}), false);
}
@Test
public void testMinpackChebyquad() {
minpackTest(new ChebyquadFunction(1, 8, 1.0,
1.88623796907732, 1.88623796907732,
new double[] { 0.5 }), false);
minpackTest(new ChebyquadFunction(1, 8, 10.0,
5383344372.34005, 1.88424820499951,
new double[] { 0.9817314924684 }), false);
minpackTest(new ChebyquadFunction(1, 8, 100.0,
0.118088726698392e19, 1.88424820499347,
new double[] { 0.9817314852934 }), false);
minpackTest(new ChebyquadFunction(8, 8, 1.0,
0.196513862833975, 0.0593032355046727,
new double[] {
0.0431536648587336, 0.193091637843267,
0.266328593812698, 0.499999334628884,
0.500000665371116, 0.733671406187302,
0.806908362156733, 0.956846335141266
}), false);
minpackTest(new ChebyquadFunction(9, 9, 1.0,
0.16994993465202, 0.0,
new double[] {
0.0442053461357828, 0.199490672309881,
0.23561910847106, 0.416046907892598,
0.5, 0.583953092107402,
0.764380891528940, 0.800509327690119,
0.955794653864217
}), false);
minpackTest(new ChebyquadFunction(10, 10, 1.0,
0.183747831178711, 0.0806471004038253,
new double[] {
0.0596202671753563, 0.166708783805937,
0.239171018813509, 0.398885290346268,
0.398883667870681, 0.601116332129320,
0.60111470965373, 0.760828981186491,
0.833291216194063, 0.940379732824644
}), false);
}
@Test
public void testMinpackBrownAlmostLinear() {
minpackTest(new BrownAlmostLinearFunction(10, 0.5,
16.5302162063499, 0.0,
new double[] {
0.979430303349862, 0.979430303349862,
0.979430303349862, 0.979430303349862,
0.979430303349862, 0.979430303349862,
0.979430303349862, 0.979430303349862,
0.979430303349862, 1.20569696650138
}), false);
minpackTest(new BrownAlmostLinearFunction(10, 5.0,
9765624.00089211, 0.0,
new double[] {
0.979430303349865, 0.979430303349865,
0.979430303349865, 0.979430303349865,
0.979430303349865, 0.979430303349865,
0.979430303349865, 0.979430303349865,
0.979430303349865, 1.20569696650135
}), false);
minpackTest(new BrownAlmostLinearFunction(10, 50.0,
0.9765625e17, 0.0,
new double[] {
1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0
}), false);
minpackTest(new BrownAlmostLinearFunction(30, 0.5,
83.476044467848, 0.0,
new double[] {
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 0.997754216442807,
0.997754216442807, 1.06737350671578
}), false);
minpackTest(new BrownAlmostLinearFunction(40, 0.5,
128.026364472323, 0.0,
new double[] {
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
1.00000000000002, 1.00000000000002,
0.999999999999121
}), false);
}
@Test
public void testMinpackOsborne1() {
minpackTest(new Osborne1Function(new double[] { 0.5, 1.5, -1.0, 0.01, 0.02, },
0.937564021037838, 0.00739249260904843,
new double[] {
0.375410049244025, 1.93584654543108,
-1.46468676748716, 0.0128675339110439,
0.0221227011813076
}), false);
}
@Test
public void testMinpackOsborne2() {
minpackTest(new Osborne2Function(new double[] {
1.3, 0.65, 0.65, 0.7, 0.6,
3.0, 5.0, 7.0, 2.0, 4.5, 5.5
},
1.44686540984712, 0.20034404483314,
new double[] {
1.30997663810096, 0.43155248076,
0.633661261602859, 0.599428560991695,
0.754179768272449, 0.904300082378518,
1.36579949521007, 4.82373199748107,
2.39868475104871, 4.56887554791452,
5.67534206273052
}), false);
}
private void minpackTest(MinpackFunction function, boolean exceptionExpected) {
final double tol = 2.22044604926e-16;
final double sqrtTol = JdkMath.sqrt(tol);
LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer()
.withCostRelativeTolerance(sqrtTol)
.withParameterRelativeTolerance(sqrtTol)
.withOrthoTolerance(tol);
LeastSquaresProblem problem = new LeastSquaresBuilder()
.maxEvaluations(400 * (function.getN() + 1))
.maxIterations(2000)
.model(function.getModelFunction(), function.getModelFunctionJacobian())
.target(function.getTarget())
.weight(new DiagonalMatrix(function.getWeight()))
.start(function.getStartPoint())
.build();
try {
final Optimum optimum = optimizer.optimize(problem);
Assert.assertFalse(exceptionExpected);
function.checkTheoreticalMinCost(optimum.getRMS());
function.checkTheoreticalMinParams(optimum.getPoint().toArray());
} catch (TooManyEvaluationsException e) {
Assert.assertTrue(exceptionExpected);
}
}
private abstract static class MinpackFunction {
protected int n;
protected int m;
protected double[] startParams;
protected double theoreticalMinCost;
protected double[] theoreticalMinParams;
protected double costAccuracy;
protected double paramsAccuracy;
protected MinpackFunction(int m, double[] startParams,
double theoreticalMinCost,
double[] theoreticalMinParams) {
this.m = m;
this.n = startParams.length;
this.startParams = startParams.clone();
this.theoreticalMinCost = theoreticalMinCost;
this.theoreticalMinParams = theoreticalMinParams;
this.costAccuracy = 1.0e-8;
this.paramsAccuracy = 1.0e-5;
}
protected static double[] buildArray(int n, double x) {
double[] array = new double[n];
Arrays.fill(array, x);
return array;
}
public double[] getTarget() {
return buildArray(m, 0.0);
}
public double[] getWeight() {
return buildArray(m, 1.0);
}
public double[] getStartPoint() {
return startParams.clone();
}
protected void setCostAccuracy(double costAccuracy) {
this.costAccuracy = costAccuracy;
}
protected void setParamsAccuracy(double paramsAccuracy) {
this.paramsAccuracy = paramsAccuracy;
}
public int getN() {
return startParams.length;
}
public void checkTheoreticalMinCost(double rms) {
double threshold = costAccuracy * (1.0 + theoreticalMinCost);
Assert.assertEquals(theoreticalMinCost, JdkMath.sqrt(m) * rms, threshold);
}
public void checkTheoreticalMinParams(double[] params) {
if (theoreticalMinParams != null) {
for (int i = 0; i < theoreticalMinParams.length; ++i) {
double mi = theoreticalMinParams[i];
double vi = params[i];
Assert.assertEquals(mi, vi, paramsAccuracy * (1.0 + JdkMath.abs(mi)));
}
}
}
public MultivariateVectorFunction getModelFunction() {
return new MultivariateVectorFunction() {
@Override
public double[] value(double[] point) {
return computeValue(point);
}
};
}
public MultivariateMatrixFunction getModelFunctionJacobian() {
return new MultivariateMatrixFunction() {
@Override
public double[][] value(double[] point) {
return computeJacobian(point);
}
};
}
public abstract double[][] computeJacobian(double[] variables);
public abstract double[] computeValue(double[] variables);
}
private static class LinearFullRankFunction extends MinpackFunction {
LinearFullRankFunction(int m, int n, double x0,
double theoreticalStartCost,
double theoreticalMinCost) {
super(m, buildArray(n, x0), theoreticalMinCost,
buildArray(n, -1.0));
}
@Override
public double[][] computeJacobian(double[] variables) {
double t = 2.0 / m;
double[][] jacobian = new double[m][];
for (int i = 0; i < m; ++i) {
jacobian[i] = new double[n];
for (int j = 0; j < n; ++j) {
jacobian[i][j] = (i == j) ? (1 - t) : -t;
}
}
return jacobian;
}
@Override
public double[] computeValue(double[] variables) {
double sum = 0;
for (int i = 0; i < n; ++i) {
sum += variables[i];
}
double t = 1 + 2 * sum / m;
double[] f = new double[m];
for (int i = 0; i < n; ++i) {
f[i] = variables[i] - t;
}
Arrays.fill(f, n, m, -t);
return f;
}
}
private static class LinearRank1Function extends MinpackFunction {
LinearRank1Function(int m, int n, double x0,
double theoreticalStartCost,
double theoreticalMinCost) {
super(m, buildArray(n, x0), theoreticalMinCost, null);
}
@Override
public double[][] computeJacobian(double[] variables) {
double[][] jacobian = new double[m][];
for (int i = 0; i < m; ++i) {
jacobian[i] = new double[n];
for (int j = 0; j < n; ++j) {
jacobian[i][j] = (i + 1) * (j + 1);
}
}
return jacobian;
}
@Override
public double[] computeValue(double[] variables) {
double[] f = new double[m];
double sum = 0;
for (int i = 0; i < n; ++i) {
sum += (i + 1) * variables[i];
}
for (int i = 0; i < m; ++i) {
f[i] = (i + 1) * sum - 1;
}
return f;
}
}
private static class LinearRank1ZeroColsAndRowsFunction extends MinpackFunction {
LinearRank1ZeroColsAndRowsFunction(int m, int n, double x0) {
super(m, buildArray(n, x0),
JdkMath.sqrt((m * (m + 3) - 6) / (2.0 * (2 * m - 3))),
null);
}
@Override
public double[][] computeJacobian(double[] variables) {
double[][] jacobian = new double[m][];
for (int i = 0; i < m; ++i) {
jacobian[i] = new double[n];
jacobian[i][0] = 0;
for (int j = 1; j < (n - 1); ++j) {
if (i == 0) {
jacobian[i][j] = 0;
} else if (i != (m - 1)) {
jacobian[i][j] = i * (j + 1);
} else {
jacobian[i][j] = 0;
}
}
jacobian[i][n - 1] = 0;
}
return jacobian;
}
@Override
public double[] computeValue(double[] variables) {
double[] f = new double[m];
double sum = 0;
for (int i = 1; i < (n - 1); ++i) {
sum += (i + 1) * variables[i];
}
for (int i = 0; i < (m - 1); ++i) {
f[i] = i * sum - 1;
}
f[m - 1] = -1;
return f;
}
}
private static class RosenbrockFunction extends MinpackFunction {
RosenbrockFunction(double[] startParams, double theoreticalStartCost) {
super(2, startParams, 0.0, buildArray(2, 1.0));
}
@Override
public double[][] computeJacobian(double[] variables) {
double x1 = variables[0];
return new double[][] { { -20 * x1, 10 }, { -1, 0 } };
}
@Override
public double[] computeValue(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
return new double[] { 10 * (x2 - x1 * x1), 1 - x1 };
}
}
private static class HelicalValleyFunction extends MinpackFunction {
HelicalValleyFunction(double[] startParams,
double theoreticalStartCost) {
super(3, startParams, 0.0, new double[] { 1.0, 0.0, 0.0 });
}
@Override
public double[][] computeJacobian(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double tmpSquare = x1 * x1 + x2 * x2;
double tmp1 = twoPi * tmpSquare;
double tmp2 = JdkMath.sqrt(tmpSquare);
return new double[][] {
{ 100 * x2 / tmp1, -100 * x1 / tmp1, 10 },
{ 10 * x1 / tmp2, 10 * x2 / tmp2, 0 },
{ 0, 0, 1 }
};
}
@Override
public double[] computeValue(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double x3 = variables[2];
double tmp1;
if (x1 == 0) {
tmp1 = (x2 >= 0) ? 0.25 : -0.25;
} else {
tmp1 = JdkMath.atan(x2 / x1) / twoPi;
if (x1 < 0) {
tmp1 += 0.5;
}
}
double tmp2 = JdkMath.sqrt(x1 * x1 + x2 * x2);
return new double[] {
10.0 * (x3 - 10 * tmp1),
10.0 * (tmp2 - 1),
x3
};
}
private static final double twoPi = 2.0 * JdkMath.PI;
}
private static class PowellSingularFunction extends MinpackFunction {
PowellSingularFunction(double[] startParams,
double theoreticalStartCost) {
super(4, startParams, 0.0, buildArray(4, 0.0));
}
@Override
public double[][] computeJacobian(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double x3 = variables[2];
double x4 = variables[3];
return new double[][] {
{ 1, 10, 0, 0 },
{ 0, 0, sqrt5, -sqrt5 },
{ 0, 2 * (x2 - 2 * x3), -4 * (x2 - 2 * x3), 0 },
{ 2 * sqrt10 * (x1 - x4), 0, 0, -2 * sqrt10 * (x1 - x4) }
};
}
@Override
public double[] computeValue(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double x3 = variables[2];
double x4 = variables[3];
return new double[] {
x1 + 10 * x2,
sqrt5 * (x3 - x4),
(x2 - 2 * x3) * (x2 - 2 * x3),
sqrt10 * (x1 - x4) * (x1 - x4)
};
}
private static final double sqrt5 = JdkMath.sqrt( 5.0);
private static final double sqrt10 = JdkMath.sqrt(10.0);
}
private static class FreudensteinRothFunction extends MinpackFunction {
FreudensteinRothFunction(double[] startParams,
double theoreticalStartCost,
double theoreticalMinCost,
double[] theoreticalMinParams) {
super(2, startParams, theoreticalMinCost,
theoreticalMinParams);
}
@Override
public double[][] computeJacobian(double[] variables) {
double x2 = variables[1];
return new double[][] {
{ 1, x2 * (10 - 3 * x2) - 2 },
{ 1, x2 * ( 2 + 3 * x2) - 14, }
};
}
@Override
public double[] computeValue(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
return new double[] {
-13.0 + x1 + ((5.0 - x2) * x2 - 2.0) * x2,
-29.0 + x1 + ((1.0 + x2) * x2 - 14.0) * x2
};
}
}
private static class BardFunction extends MinpackFunction {
BardFunction(double x0,
double theoreticalStartCost,
double theoreticalMinCost,
double[] theoreticalMinParams) {
super(15, buildArray(3, x0), theoreticalMinCost,
theoreticalMinParams);
}
@Override
public double[][] computeJacobian(double[] variables) {
double x2 = variables[1];
double x3 = variables[2];
double[][] jacobian = new double[m][];
for (int i = 0; i < m; ++i) {
double tmp1 = i + 1;
double tmp2 = 15 - i;
double tmp3 = (i <= 7) ? tmp1 : tmp2;
double tmp4 = x2 * tmp2 + x3 * tmp3;
tmp4 *= tmp4;
jacobian[i] = new double[] { -1, tmp1 * tmp2 / tmp4, tmp1 * tmp3 / tmp4 };
}
return jacobian;
}
@Override
public double[] computeValue(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double x3 = variables[2];
double[] f = new double[m];
for (int i = 0; i < m; ++i) {
double tmp1 = i + 1;
double tmp2 = 15 - i;
double tmp3 = (i <= 7) ? tmp1 : tmp2;
f[i] = y[i] - (x1 + tmp1 / (x2 * tmp2 + x3 * tmp3));
}
return f;
}
private static final double[] y = {
0.14, 0.18, 0.22, 0.25, 0.29,
0.32, 0.35, 0.39, 0.37, 0.58,
0.73, 0.96, 1.34, 2.10, 4.39
};
}
private static class KowalikOsborneFunction extends MinpackFunction {
KowalikOsborneFunction(double[] startParams,
double theoreticalStartCost,
double theoreticalMinCost,
double[] theoreticalMinParams) {
super(11, startParams, theoreticalMinCost,
theoreticalMinParams);
if (theoreticalStartCost > 20.0) {
setCostAccuracy(2.0e-4);
setParamsAccuracy(5.0e-3);
}
}
@Override
public double[][] computeJacobian(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double x3 = variables[2];
double x4 = variables[3];
double[][] jacobian = new double[m][];
for (int i = 0; i < m; ++i) {
double tmp = v[i] * (v[i] + x3) + x4;
double j1 = -v[i] * (v[i] + x2) / tmp;
double j2 = -v[i] * x1 / tmp;
double j3 = j1 * j2;
double j4 = j3 / v[i];
jacobian[i] = new double[] { j1, j2, j3, j4 };
}
return jacobian;
}
@Override
public double[] computeValue(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double x3 = variables[2];
double x4 = variables[3];
double[] f = new double[m];
for (int i = 0; i < m; ++i) {
f[i] = y[i] - x1 * (v[i] * (v[i] + x2)) / (v[i] * (v[i] + x3) + x4);
}
return f;
}
private static final double[] v = {
4.0, 2.0, 1.0, 0.5, 0.25, 0.167, 0.125, 0.1, 0.0833, 0.0714, 0.0625
};
private static final double[] y = {
0.1957, 0.1947, 0.1735, 0.1600, 0.0844, 0.0627,
0.0456, 0.0342, 0.0323, 0.0235, 0.0246
};
}
private static class MeyerFunction extends MinpackFunction {
MeyerFunction(double[] startParams,
double theoreticalStartCost,
double theoreticalMinCost,
double[] theoreticalMinParams) {
super(16, startParams, theoreticalMinCost,
theoreticalMinParams);
if (theoreticalStartCost > 1.0e6) {
setCostAccuracy(7.0e-3);
setParamsAccuracy(2.0e-2);
}
}
@Override
public double[][] computeJacobian(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double x3 = variables[2];
double[][] jacobian = new double[m][];
for (int i = 0; i < m; ++i) {
double temp = 5.0 * (i + 1) + 45.0 + x3;
double tmp1 = x2 / temp;
double tmp2 = JdkMath.exp(tmp1);
double tmp3 = x1 * tmp2 / temp;
jacobian[i] = new double[] { tmp2, tmp3, -tmp1 * tmp3 };
}
return jacobian;
}
@Override
public double[] computeValue(double[] variables) {
double x1 = variables[0];
double x2 = variables[1];
double x3 = variables[2];