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    *      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  package org.apache.commons.math4.legacy.optim.univariate;
18  
19  import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
20  import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  /**
25   * Test for {@link BracketFinder}.
26   */
27  public class BracketFinderTest {
28  
29      @Test
30      public void testCubicMin() {
31          final BracketFinder bFind = new BracketFinder();
32          final UnivariateFunction func = new UnivariateFunction() {
33                  @Override
34                  public double value(double x) {
35                      if (x < -2) {
36                          return value(-2);
37                      }
38                      return (x - 1) * (x + 2) * (x + 3);
39                  }
40              };
41  
42          bFind.search(func, GoalType.MINIMIZE, -2 , -1);
43          final double tol = 1e-15;
44          // Comparing with results computed in Python.
45          Assert.assertEquals(-2, bFind.getLo(), tol);
46          Assert.assertEquals(-1, bFind.getMid(), tol);
47          Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol);
48      }
49  
50      @Test
51      public void testCubicMax() {
52          final BracketFinder bFind = new BracketFinder();
53          final UnivariateFunction func = new UnivariateFunction() {
54                  @Override
55                  public double value(double x) {
56                      if (x < -2) {
57                          return value(-2);
58                      }
59                      return -(x - 1) * (x + 2) * (x + 3);
60                  }
61              };
62  
63          bFind.search(func, GoalType.MAXIMIZE, -2 , -1);
64          final double tol = 1e-15;
65          Assert.assertEquals(-2, bFind.getLo(), tol);
66          Assert.assertEquals(-1, bFind.getMid(), tol);
67          Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol);
68      }
69  
70      @Test
71      public void testMinimumIsOnIntervalBoundary() {
72          final UnivariateFunction func = new UnivariateFunction() {
73                  @Override
74                  public double value(double x) {
75                      return x * x;
76                  }
77              };
78  
79          final BracketFinder bFind = new BracketFinder();
80  
81          bFind.search(func, GoalType.MINIMIZE, 0, 1);
82          Assert.assertTrue(bFind.getLo() <= 0);
83          Assert.assertTrue(0 <= bFind.getHi());
84  
85          bFind.search(func, GoalType.MINIMIZE, -1, 0);
86          Assert.assertTrue(bFind.getLo() <= 0);
87          Assert.assertTrue(0 <= bFind.getHi());
88      }
89  
90      @Test
91      public void testIntervalBoundsOrdering() {
92          final UnivariateFunction func = new UnivariateFunction() {
93                  @Override
94                  public double value(double x) {
95                      return x * x;
96                  }
97              };
98  
99          final BracketFinder bFind = new BracketFinder();
100 
101         bFind.search(func, GoalType.MINIMIZE, -1, 1);
102         Assert.assertTrue(bFind.getLo() <= 0);
103         Assert.assertTrue(0 <= bFind.getHi());
104 
105         bFind.search(func, GoalType.MINIMIZE, 1, -1);
106         Assert.assertTrue(bFind.getLo() <= 0);
107         Assert.assertTrue(0 <= bFind.getHi());
108 
109         bFind.search(func, GoalType.MINIMIZE, 1, 2);
110         Assert.assertTrue(bFind.getLo() <= 0);
111         Assert.assertTrue(0 <= bFind.getHi());
112 
113         bFind.search(func, GoalType.MINIMIZE, 2, 1);
114         Assert.assertTrue(bFind.getLo() <= 0);
115         Assert.assertTrue(0 <= bFind.getHi());
116     }
117 }