1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
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  
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          
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 }