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.math.optimization;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import org.apache.commons.math.MathException;
25  import org.apache.commons.math.analysis.QuinticFunction;
26  import org.apache.commons.math.analysis.SinFunction;
27  import org.apache.commons.math.analysis.UnivariateRealFunction;
28  import org.apache.commons.math.optimization.univariate.BrentOptimizer;
29  import org.apache.commons.math.random.JDKRandomGenerator;
30  import org.junit.Test;
31  
32  public class MultiStartUnivariateRealOptimizerTest {
33  
34      @Test
35      public void testSinMin() throws MathException {
36          UnivariateRealFunction f = new SinFunction();
37          UnivariateRealOptimizer underlying = new BrentOptimizer();
38          JDKRandomGenerator g = new JDKRandomGenerator();
39          g.setSeed(44428400075l);
40          MultiStartUnivariateRealOptimizer minimizer =
41              new MultiStartUnivariateRealOptimizer(underlying, 10, g);
42          minimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0);
43          double[] optima = minimizer.getOptima();
44          double[] optimaValues = minimizer.getOptimaValues();
45          for (int i = 1; i < optima.length; ++i) {
46              double d = (optima[i] - optima[i-1]) / (2 * Math.PI);
47              assertTrue (Math.abs(d - Math.rint(d)) < 1.0e-8);
48              assertEquals(-1.0, f.value(optima[i]), 1.0e-10);
49              assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
50          }
51          assertTrue(minimizer.getEvaluations() > 2900);
52          assertTrue(minimizer.getEvaluations() < 3100);
53      }
54  
55      @Test
56      public void testQuinticMin() throws MathException {
57          // The quintic function has zeros at 0, +-0.5 and +-1.
58          // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
59          UnivariateRealFunction f = new QuinticFunction();
60          UnivariateRealOptimizer underlying = new BrentOptimizer();
61          JDKRandomGenerator g = new JDKRandomGenerator();
62          g.setSeed(4312000053l);
63          MultiStartUnivariateRealOptimizer minimizer =
64              new MultiStartUnivariateRealOptimizer(underlying, 5, g);
65          minimizer.setAbsoluteAccuracy(10 * minimizer.getAbsoluteAccuracy());
66          minimizer.setRelativeAccuracy(10 * minimizer.getRelativeAccuracy());
67  
68          try {
69              minimizer.getOptima();
70              fail("an exception should have been thrown");
71          } catch (IllegalStateException ise) {
72              // expected
73          } catch (Exception e) {
74              fail("wrong exception caught");
75          }
76          try {
77              minimizer.getOptimaValues();
78              fail("an exception should have been thrown");
79          } catch (IllegalStateException ise) {
80              // expected
81          } catch (Exception e) {
82              fail("wrong exception caught");
83          }
84  
85          assertEquals(-0.27195612846834, minimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2), 1.0e-13);
86          assertEquals(-0.27194301946870, minimizer.getResult(), 1.0e-13);
87          assertEquals(-0.04433426940878, minimizer.getFunctionValue(), 1.0e-13);
88  
89          double[] optima = minimizer.getOptima();
90          double[] optimaValues = minimizer.getOptimaValues();
91          for (int i = 0; i < optima.length; ++i) {
92              assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
93          }
94  
95          assertTrue(minimizer.getEvaluations()    >= 510);
96          assertTrue(minimizer.getEvaluations()    <= 530);
97          assertTrue(minimizer.getIterationCount() >= 150);
98          assertTrue(minimizer.getIterationCount() <= 170);
99  
100     }
101 
102 }