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.nonlinear.scalar.noderiv;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.ArrayList;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
25  import org.apache.commons.math4.legacy.optim.PointValuePair;
26  
27  /**
28   * Test for {@link Simplex}.
29   */
30  public class SimplexTest {
31      @Test
32      public void testTriangle() {
33          final double[] o = new double[] { 1, 2 };
34          final double s = 3.45;
35          final int dim = o.length;
36          final int size = dim + 1;
37  
38          final double[][] expected = new double[][] {
39              o,
40              new double[] { o[0] + s, o[1] },
41              new double[] { o[0] + s, o[1] + s }
42          };
43  
44          final Simplex simplex = Simplex.equalSidesAlongAxes(dim, s).translate(o);
45          Assert.assertEquals(dim, simplex.getDimension());
46          Assert.assertEquals(size, simplex.getSize());
47  
48          for (int i = 0; i < size; i++) {
49              final double[] e = expected[i];
50              final double[] a = simplex.get(i).getPoint();
51              Assert.assertArrayEquals("i=" + i + ": e=" + Arrays.toString(e) + " a=" + Arrays.toString(a), e, a, 0.0);
52          }
53      }
54  
55      @Test
56      public void testJavadocExample() {
57          final double[] o = new double[] { 1, 10, 2 };
58          final double[] start = new double[] { 1, 1, 1 };
59          final int dim = o.length;
60          final int size = dim + 1;
61  
62          final double[][] expected = new double[][] {
63              new double[] { 1, 1, 1 },
64              new double[] { 2, 1, 1 },
65              new double[] { 2, 11, 1 },
66              new double[] { 2, 11, 3 },
67          };
68  
69          final Simplex simplex = Simplex.alongAxes(o).translate(start);
70          Assert.assertEquals(dim, simplex.getDimension());
71          Assert.assertEquals(size, simplex.getSize());
72  
73          for (int i = 0; i < size; i++) {
74              final double[] e = expected[i];
75              final double[] a = simplex.get(i).getPoint();
76  
77              final String msg = "i=" + i +
78                  ": e=" + Arrays.toString(e) +
79                  " a=" + Arrays.toString(a);
80              Assert.assertArrayEquals(msg, e, a, 0.0);
81          }
82      }
83  
84      @Test
85      public void testCentroid() {
86          final List<PointValuePair> list = new ArrayList<>();
87  
88          double[] centroid = null;
89  
90          // Interval.
91          list.clear();
92          list.add(new PointValuePair(new double[] { 1, 1 }, 0d));
93          list.add(new PointValuePair(new double[] { 2, 2 }, 0d));
94          centroid = Simplex.centroid(list);
95          Assert.assertEquals(1.5, centroid[0], 0d);
96          Assert.assertEquals(1.5, centroid[1], 0d);
97  
98          // Square.
99          list.clear();
100         list.add(new PointValuePair(new double[] { 1, 0 }, 0d));
101         list.add(new PointValuePair(new double[] { 2, 0 }, 0d));
102         list.add(new PointValuePair(new double[] { 2, 1 }, 0d));
103         list.add(new PointValuePair(new double[] { 1, 1 }, 0d));
104         centroid = Simplex.centroid(list);
105         Assert.assertEquals(1.5, centroid[0], 0d);
106         Assert.assertEquals(0.5, centroid[1], 0d);
107     }
108 
109     @Test
110     public void testAsList() {
111         final int dim = 10;
112         final Simplex simplex = Simplex.equalSidesAlongAxes(dim, 1d);
113         final List<PointValuePair> list = simplex.asList();
114         Assert.assertEquals(dim + 1, list.size());
115 
116         for (int i = 0; i < dim + 1; i++) {
117             final PointValuePair p = list.get(i);
118             final double[] a = simplex.get(i).getPointRef();
119             final double[] b = p.getPointRef();
120             Assert.assertNotSame(a, b);
121             Assert.assertArrayEquals(a, b, 0.0);
122             Assert.assertTrue(Double.isNaN(p.getValue()));
123         }
124     }
125 
126     @Test
127     public void testReplaceLast1() {
128         final int dim = 7;
129         final int nPoints = dim + 1;
130         final Simplex initSimplex = Simplex.equalSidesAlongAxes(dim, 1d);
131         for (PointValuePair pv : initSimplex.asList()) {
132             Assert.assertTrue(Double.isNaN(pv.getValue()));
133         }
134 
135         final int nRepl = 3;
136         final double value = 12.345;
137         final List<PointValuePair> replace = new ArrayList<>();
138         for (int i = 0; i < nRepl; i++) {
139             replace.add(new PointValuePair(new double[dim], value));
140         }
141 
142         final Simplex newSimplex = initSimplex.replaceLast(replace);
143         Assert.assertEquals(nPoints, newSimplex.getSize());
144 
145         final int from = nPoints - nRepl;
146         for (int i = 0; i < from; i++) {
147             Assert.assertTrue(Double.isNaN(newSimplex.get(i).getValue()));
148         }
149         for (int i = from; i < nPoints; i++) {
150             Assert.assertEquals(value, newSimplex.get(i).getValue(), 0d);
151         }
152     }
153 
154     @Test
155     public void testReplaceLast2() {
156         final int dim = 7;
157         final int nPoints = dim + 1;
158         final Simplex initSimplex = Simplex.equalSidesAlongAxes(dim, 1d);
159         for (PointValuePair pv : initSimplex.asList()) {
160             Assert.assertTrue(Double.isNaN(pv.getValue()));
161         }
162 
163         final double value = 12.345;
164         final PointValuePair replace = new PointValuePair(new double[dim], value);
165         final Simplex newSimplex = initSimplex.replaceLast(replace);
166         Assert.assertEquals(nPoints, newSimplex.getSize());
167 
168         final int from = nPoints - 1;
169         for (int i = 0; i < from; i++) {
170             Assert.assertTrue(Double.isNaN(newSimplex.get(i).getValue()));
171         }
172         for (int i = from; i < nPoints; i++) {
173             Assert.assertEquals(value, newSimplex.get(i).getValue(), 0d);
174         }
175     }
176 
177     @Test
178     public void testNewPoint() {
179         final double[] a = new double[] { 1, 2 };
180         final double[] b = new double[] { 3, -1 };
181         final double s = 7;
182 
183         final MultivariateFunction f = x -> {
184             double v = 0;
185             for (int i = 0; i < x.length; i++) {
186                 v += x[i] * x[i];
187             }
188             return v;
189         };
190 
191         final double[] eP = new double[] { 15, -19 };
192         final double eV = 586;
193 
194         final PointValuePair pv = Simplex.newPoint(a, s, b, f);
195         Assert.assertArrayEquals(eP, pv.getPoint(), 0.0);
196         Assert.assertEquals(eV, pv.getValue(), 0d);
197     }
198 }