1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.math4.legacy.fitting.leastsquares;
19
20 import org.apache.commons.math4.legacy.exception.ConvergenceException;
21 import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
22 import org.apache.commons.math4.legacy.fitting.leastsquares.GaussNewtonOptimizer.Decomposition;
23 import org.apache.commons.math4.legacy.optim.SimpleVectorValueChecker;
24 import org.junit.Test;
25
26 import java.io.IOException;
27
28 /**
29 * <p>Some of the unit tests are re-implementations of the MINPACK <a
30 * href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
31 * href="http://www.netlib.org/minpack/ex/file22">file22</a> test files.
32 * The redistribution policy for MINPACK is available <a
33 * href="http://www.netlib.org/minpack/disclaimer">here</a>/
34 *
35 */
36 public class GaussNewtonOptimizerWithQRTest
37 extends AbstractLeastSquaresOptimizerAbstractTest {
38
39 @Override
40 public int getMaxIterations() {
41 return 1000;
42 }
43
44 @Override
45 public LeastSquaresOptimizer getOptimizer() {
46 return new GaussNewtonOptimizer(Decomposition.QR);
47 }
48
49 @Override
50 @Test
51 public void testMoreEstimatedParametersUnsorted() {
52 /*
53 * Exception is expected with this optimizer
54 */
55 try{
56 super.testMoreEstimatedParametersUnsorted();
57 fail(optimizer);
58 }catch (ConvergenceException e){
59 //expected
60 }
61 }
62
63 @Test
64 public void testMaxEvaluations() throws Exception {
65 try{
66 CircleVectorial circle = new CircleVectorial();
67 circle.addPoint( 30.0, 68.0);
68 circle.addPoint( 50.0, -6.0);
69 circle.addPoint(110.0, -20.0);
70 circle.addPoint( 35.0, 15.0);
71 circle.addPoint( 45.0, 97.0);
72
73 LeastSquaresProblem lsp = builder(circle)
74 .checkerPair(new SimpleVectorValueChecker(1e-30, 1e-30))
75 .maxIterations(Integer.MAX_VALUE)
76 .start(new double[]{98.680, 47.345})
77 .build();
78
79 optimizer.optimize(lsp);
80
81 fail(optimizer);
82 }catch (TooManyEvaluationsException e){
83 //expected
84 }
85 }
86
87 @Override
88 @Test
89 public void testCircleFittingBadInit() {
90 /*
91 * This test does not converge with this optimizer.
92 */
93 try{
94 super.testCircleFittingBadInit();
95 fail(optimizer);
96 }catch (ConvergenceException e){
97 //expected
98 }
99 }
100
101 @Override
102 @Test
103 public void testHahn1()
104 throws IOException {
105 /*
106 * TODO This test leads to a singular problem with the Gauss-Newton
107 * optimizer. This should be inquired.
108 */
109 try{
110 super.testHahn1();
111 fail(optimizer);
112 } catch (ConvergenceException e){
113 //expected for LU
114 } catch (TooManyEvaluationsException e){
115 //expected for QR
116 }
117 }
118 }