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.math3.optim.nonlinear.vector.jacobian;
19 
20 import java.io.IOException;
21 
22 import org.apache.commons.math3.exception.ConvergenceException;
23 import org.apache.commons.math3.exception.TooManyEvaluationsException;
24 import org.apache.commons.math3.exception.MathUnsupportedOperationException;
25 import org.apache.commons.math3.optim.SimpleVectorValueChecker;
26 import org.apache.commons.math3.optim.InitialGuess;
27 import org.apache.commons.math3.optim.MaxEval;
28 import org.apache.commons.math3.optim.SimpleBounds;
29 import org.apache.commons.math3.optim.nonlinear.vector.Target;
30 import org.apache.commons.math3.optim.nonlinear.vector.Weight;
31 import org.junit.Test;
32 
33 /**
34  * <p>Some of the unit tests are re-implementations of the MINPACK <a
35  * href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
36  * href="http://www.netlib.org/minpack/ex/file22">file22</a> test files.
37  * The redistribution policy for MINPACK is available <a
38  * href="http://www.netlib.org/minpack/disclaimer">here</a>, for
39  * convenience, it is reproduced below.</p>
40 
41  * <table border="0" width="80%" cellpadding="10" align="center" bgcolor="#E0E0E0">
42  * <tr><td>
43  *    Minpack Copyright Notice (1999) University of Chicago.
44  *    All rights reserved
45  * </td></tr>
46  * <tr><td>
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * <ol>
51  *  <li>Redistributions of source code must retain the above copyright
52  *      notice, this list of conditions and the following disclaimer.</li>
53  * <li>Redistributions in binary form must reproduce the above
54  *     copyright notice, this list of conditions and the following
55  *     disclaimer in the documentation and/or other materials provided
56  *     with the distribution.</li>
57  * <li>The end-user documentation included with the redistribution, if any,
58  *     must include the following acknowledgment:
59  *     <code>This product includes software developed by the University of
60  *           Chicago, as Operator of Argonne National Laboratory.</code>
61  *     Alternately, this acknowledgment may appear in the software itself,
62  *     if and wherever such third-party acknowledgments normally appear.</li>
63  * <li><strong>WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
64  *     WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
65  *     UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
66  *     THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
67  *     IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
68  *     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
69  *     OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
70  *     OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
71  *     USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
72  *     THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
73  *     DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
74  *     UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
75  *     BE CORRECTED.</strong></li>
76  * <li><strong>LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
77  *     HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
78  *     ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
79  *     INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
80  *     ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
81  *     PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
82  *     SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
83  *     (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
84  *     EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
85  *     POSSIBILITY OF SUCH LOSS OR DAMAGES.</strong></li>
86  * <ol></td></tr>
87  * </table>
88 
89  * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests)
90  * @author Burton S. Garbow (original fortran minpack tests)
91  * @author Kenneth E. Hillstrom (original fortran minpack tests)
92  * @author Jorge J. More (original fortran minpack tests)
93  * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation)
94  */
95 @Deprecated
96 public class GaussNewtonOptimizerTest
97     extends AbstractLeastSquaresOptimizerAbstractTest {
98 
99     @Override
createOptimizer()100     public AbstractLeastSquaresOptimizer createOptimizer() {
101         return new GaussNewtonOptimizer(new SimpleVectorValueChecker(1.0e-6, 1.0e-6));
102     }
103 
104     @Test(expected=MathUnsupportedOperationException.class)
testConstraintsUnsupported()105     public void testConstraintsUnsupported() {
106         createOptimizer().optimize(new MaxEval(100),
107                                    new Target(new double[] { 2 }),
108                                    new Weight(new double[] { 1 }),
109                                    new InitialGuess(new double[] { 1, 2 }),
110                                    new SimpleBounds(new double[] { -10, 0 },
111                                                     new double[] { 20, 30 }));
112     }
113 
114     @Override
115     @Test(expected = ConvergenceException.class)
testMoreEstimatedParametersSimple()116     public void testMoreEstimatedParametersSimple() {
117         /*
118          * Exception is expected with this optimizer
119          */
120         super.testMoreEstimatedParametersSimple();
121     }
122 
123     @Override
124     @Test(expected=ConvergenceException.class)
testMoreEstimatedParametersUnsorted()125     public void testMoreEstimatedParametersUnsorted() {
126         /*
127          * Exception is expected with this optimizer
128          */
129         super.testMoreEstimatedParametersUnsorted();
130     }
131 
132     @Test(expected=TooManyEvaluationsException.class)
testMaxEvaluations()133     public void testMaxEvaluations() throws Exception {
134         CircleVectorial circle = new CircleVectorial();
135         circle.addPoint( 30.0,  68.0);
136         circle.addPoint( 50.0,  -6.0);
137         circle.addPoint(110.0, -20.0);
138         circle.addPoint( 35.0,  15.0);
139         circle.addPoint( 45.0,  97.0);
140 
141         GaussNewtonOptimizer optimizer
142             = new GaussNewtonOptimizer(new SimpleVectorValueChecker(1e-30, 1e-30));
143 
144         optimizer.optimize(new MaxEval(100),
145                            circle.getModelFunction(),
146                            circle.getModelFunctionJacobian(),
147                            new Target(new double[] { 0, 0, 0, 0, 0 }),
148                            new Weight(new double[] { 1, 1, 1, 1, 1 }),
149                            new InitialGuess(new double[] { 98.680, 47.345 }));
150     }
151 
152     @Override
153     @Test(expected=ConvergenceException.class)
testCircleFittingBadInit()154     public void testCircleFittingBadInit() {
155         /*
156          * This test does not converge with this optimizer.
157          */
158         super.testCircleFittingBadInit();
159     }
160 
161     @Override
162     @Test(expected = ConvergenceException.class)
testHahn1()163     public void testHahn1()
164         throws IOException {
165         /*
166          * TODO This test leads to a singular problem with the Gauss-Newton
167          * optimizer. This should be inquired.
168          */
169         super.testHahn1();
170     }
171 }
172