View Javadoc
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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.optim.nonlinear.scalar.noderiv;
24  
25  import org.hipparchus.analysis.MultivariateFunction;
26  import org.hipparchus.exception.MathRuntimeException;
27  import org.hipparchus.optim.InitialGuess;
28  import org.hipparchus.optim.MaxEval;
29  import org.hipparchus.optim.PointValuePair;
30  import org.hipparchus.optim.SimpleBounds;
31  import org.hipparchus.optim.SimpleValueChecker;
32  import org.hipparchus.optim.nonlinear.scalar.GoalType;
33  import org.hipparchus.optim.nonlinear.scalar.ObjectiveFunction;
34  import org.hipparchus.util.FastMath;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  public class SimplexOptimizerMultiDirectionalTest {
39      @Test(expected=MathRuntimeException.class)
40      public void testBoundsUnsupported() {
41          SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
42          final FourExtrema fourExtrema = new FourExtrema();
43  
44          optimizer.optimize(new MaxEval(100),
45                             new ObjectiveFunction(fourExtrema),
46                             GoalType.MINIMIZE,
47                             new InitialGuess(new double[] { -3, 0 }),
48                             new NelderMeadSimplex(new double[] { 0.2, 0.2 }),
49                             new SimpleBounds(new double[] { -5, -1 },
50                                              new double[] { 5, 1 }));
51      }
52  
53      @Test
54      public void testMinimize1() {
55          SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
56          final FourExtrema fourExtrema = new FourExtrema();
57  
58          final PointValuePair optimum
59              = optimizer.optimize(new MaxEval(200),
60                                   new ObjectiveFunction(fourExtrema),
61                                   GoalType.MINIMIZE,
62                                   new InitialGuess(new double[] { -3, 0 }),
63                                   new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
64          Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 4e-6);
65          Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6);
66          Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 8e-13);
67          Assert.assertTrue(optimizer.getEvaluations() > 120);
68          Assert.assertTrue(optimizer.getEvaluations() < 150);
69  
70          // Check that the number of iterations is updated (MATH-949).
71          Assert.assertTrue(optimizer.getIterations() > 0);
72      }
73  
74      @Test
75      public void testMinimize2() {
76          SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
77          final FourExtrema fourExtrema = new FourExtrema();
78  
79          final PointValuePair optimum
80              = optimizer.optimize(new MaxEval(200),
81                                   new ObjectiveFunction(fourExtrema),
82                                   GoalType.MINIMIZE,
83                                   new InitialGuess(new double[] { 1, 0 }),
84                                   new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
85          Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8);
86          Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6);
87          Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 2e-12);
88          Assert.assertTrue(optimizer.getEvaluations() > 120);
89          Assert.assertTrue(optimizer.getEvaluations() < 150);
90  
91          // Check that the number of iterations is updated (MATH-949).
92          Assert.assertTrue(optimizer.getIterations() > 0);
93      }
94  
95      @Test
96      public void testMaximize1() {
97          SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
98          final FourExtrema fourExtrema = new FourExtrema();
99  
100         final PointValuePair optimum
101             = optimizer.optimize(new MaxEval(200),
102                                  new ObjectiveFunction(fourExtrema),
103                                  GoalType.MAXIMIZE,
104                                  new InitialGuess(new double[] { -3.0, 0.0 }),
105                                  new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
106         Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 7e-7);
107         Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-7);
108         Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 2e-14);
109         Assert.assertTrue(optimizer.getEvaluations() > 120);
110         Assert.assertTrue(optimizer.getEvaluations() < 150);
111 
112         // Check that the number of iterations is updated (MATH-949).
113         Assert.assertTrue(optimizer.getIterations() > 0);
114     }
115 
116     @Test
117     public void testMaximize2() {
118         SimplexOptimizer optimizer = new SimplexOptimizer(new SimpleValueChecker(1e-15, 1e-30));
119         final FourExtrema fourExtrema = new FourExtrema();
120 
121         final PointValuePair optimum
122             = optimizer.optimize(new MaxEval(200),
123                                  new ObjectiveFunction(fourExtrema),
124                                  GoalType.MAXIMIZE,
125                                  new InitialGuess(new double[] { 1, 0 }),
126                                  new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
127         Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8);
128         Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6);
129         Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 2e-12);
130         Assert.assertTrue(optimizer.getEvaluations() > 180);
131         Assert.assertTrue(optimizer.getEvaluations() < 220);
132 
133         // Check that the number of iterations is updated (MATH-949).
134         Assert.assertTrue(optimizer.getIterations() > 0);
135     }
136 
137     @Test
138     public void testRosenbrock() {
139         MultivariateFunction rosenbrock
140             = new MultivariateFunction() {
141                     public double value(double[] x) {
142                         ++count;
143                         double a = x[1] - x[0] * x[0];
144                         double b = 1.0 - x[0];
145                         return 100 * a * a + b * b;
146                     }
147                 };
148 
149         count = 0;
150         SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
151         PointValuePair optimum
152            = optimizer.optimize(new MaxEval(100),
153                                 new ObjectiveFunction(rosenbrock),
154                                 GoalType.MINIMIZE,
155                                 new InitialGuess(new double[] { -1.2, 1 }),
156                                 new MultiDirectionalSimplex(new double[][] {
157                                         { -1.2,  1.0 },
158                                         { 0.9, 1.2 },
159                                         {  3.5, -2.3 } }));
160 
161         Assert.assertEquals(count, optimizer.getEvaluations());
162         Assert.assertTrue(optimizer.getEvaluations() > 50);
163         Assert.assertTrue(optimizer.getEvaluations() < 100);
164         Assert.assertTrue(optimum.getValue() > 1e-2);
165     }
166 
167     @Test
168     public void testPowell() {
169         MultivariateFunction powell
170             = new MultivariateFunction() {
171                     public double value(double[] x) {
172                         ++count;
173                         double a = x[0] + 10 * x[1];
174                         double b = x[2] - x[3];
175                         double c = x[1] - 2 * x[2];
176                         double d = x[0] - x[3];
177                         return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
178                     }
179                 };
180 
181         count = 0;
182         SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
183         PointValuePair optimum
184             = optimizer.optimize(new MaxEval(1000),
185                                  new ObjectiveFunction(powell),
186                                  GoalType.MINIMIZE,
187                                  new InitialGuess(new double[] { 3, -1, 0, 1 }),
188                                  new MultiDirectionalSimplex(4));
189         Assert.assertEquals(count, optimizer.getEvaluations());
190         Assert.assertTrue(optimizer.getEvaluations() > 800);
191         Assert.assertTrue(optimizer.getEvaluations() < 900);
192         Assert.assertTrue(optimum.getValue() > 1e-2);
193     }
194 
195     @Test
196     public void testMath283() {
197         // fails because MultiDirectional.iterateSimplex is looping forever
198         // the while(true) should be replaced with a convergence check
199         SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14);
200         final Gaussian2D function = new Gaussian2D(0, 0, 1);
201         PointValuePair estimate = optimizer.optimize(new MaxEval(1000),
202                                                      new ObjectiveFunction(function),
203                                                      GoalType.MAXIMIZE,
204                                                      new InitialGuess(function.getMaximumPosition()),
205                                                      new MultiDirectionalSimplex(2));
206         final double EPSILON = 1e-5;
207         final double expectedMaximum = function.getMaximum();
208         final double actualMaximum = estimate.getValue();
209         Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);
210 
211         final double[] expectedPosition = function.getMaximumPosition();
212         final double[] actualPosition = estimate.getPoint();
213         Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
214         Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );
215     }
216 
217     private static class FourExtrema implements MultivariateFunction {
218         // The following function has 4 local extrema.
219         final double xM = -3.841947088256863675365;
220         final double yM = -1.391745200270734924416;
221         final double xP =  0.2286682237349059125691;
222         final double yP = -yM;
223         final double valueXmYm = 0.2373295333134216789769; // Local maximum.
224         final double valueXmYp = -valueXmYm; // Local minimum.
225         final double valueXpYm = -0.7290400707055187115322; // Global minimum.
226         final double valueXpYp = -valueXpYm; // Global maximum.
227 
228         public double value(double[] variables) {
229             final double x = variables[0];
230             final double y = variables[1];
231             return (x == 0 || y == 0) ? 0 :
232                 FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y);
233         }
234     }
235 
236     private static class Gaussian2D implements MultivariateFunction {
237         private final double[] maximumPosition;
238         private final double std;
239 
240         public Gaussian2D(double xOpt, double yOpt, double std) {
241             maximumPosition = new double[] { xOpt, yOpt };
242             this.std = std;
243         }
244 
245         public double getMaximum() {
246             return value(maximumPosition);
247         }
248 
249         public double[] getMaximumPosition() {
250             return maximumPosition.clone();
251         }
252 
253         public double value(double[] point) {
254             final double x = point[0], y = point[1];
255             final double twoS2 = 2.0 * std * std;
256             return 1.0 / (twoS2 * FastMath.PI) * FastMath.exp(-(x * x + y * y) / twoS2);
257         }
258     }
259 
260     private int count;
261 }