001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math3.optimization.direct;
019    
020    import org.apache.commons.math3.analysis.MultivariateFunction;
021    import org.apache.commons.math3.optimization.GoalType;
022    import org.apache.commons.math3.optimization.PointValuePair;
023    import org.apache.commons.math3.optimization.SimpleValueChecker;
024    import org.apache.commons.math3.util.FastMath;
025    import org.junit.Assert;
026    import org.junit.Test;
027    
028    public class SimplexOptimizerMultiDirectionalTest {
029        @Test
030        public void testMinimize1() {
031            SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
032            optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
033            final FourExtrema fourExtrema = new FourExtrema();
034    
035            final PointValuePair optimum
036                = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { -3, 0 });
037            Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 4e-6);
038            Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6);
039            Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 8e-13);
040            Assert.assertTrue(optimizer.getEvaluations() > 120);
041            Assert.assertTrue(optimizer.getEvaluations() < 150);
042        }
043    
044        @Test
045        public void testMinimize2() {
046            SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
047            optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
048            final FourExtrema fourExtrema = new FourExtrema();
049    
050            final PointValuePair optimum
051                =  optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { 1, 0 });
052            Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8);
053            Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6);
054            Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 2e-12);
055            Assert.assertTrue(optimizer.getEvaluations() > 120);
056            Assert.assertTrue(optimizer.getEvaluations() < 150);
057        }
058    
059        @Test
060        public void testMaximize1() {
061            SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
062            optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
063            final FourExtrema fourExtrema = new FourExtrema();
064    
065            final PointValuePair optimum
066                = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 });
067            Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 7e-7);
068            Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-7);
069            Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 2e-14);
070            Assert.assertTrue(optimizer.getEvaluations() > 120);
071            Assert.assertTrue(optimizer.getEvaluations() < 150);
072        }
073    
074        @Test
075        public void testMaximize2() {
076            SimplexOptimizer optimizer = new SimplexOptimizer(new SimpleValueChecker(1e-15, 1e-30));
077            optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
078            final FourExtrema fourExtrema = new FourExtrema();
079    
080            final PointValuePair optimum
081                = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { 1, 0 });
082            Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8);
083            Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6);
084            Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 2e-12);
085            Assert.assertTrue(optimizer.getEvaluations() > 180);
086            Assert.assertTrue(optimizer.getEvaluations() < 220);
087        }
088    
089        @Test
090        public void testRosenbrock() {
091            MultivariateFunction rosenbrock =
092                new MultivariateFunction() {
093                    public double value(double[] x) {
094                        ++count;
095                        double a = x[1] - x[0] * x[0];
096                        double b = 1.0 - x[0];
097                        return 100 * a * a + b * b;
098                    }
099                };
100    
101            count = 0;
102            SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
103            optimizer.setSimplex(new MultiDirectionalSimplex(new double[][] {
104                        { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
105                    }));
106            PointValuePair optimum =
107                optimizer.optimize(100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1 });
108    
109            Assert.assertEquals(count, optimizer.getEvaluations());
110            Assert.assertTrue(optimizer.getEvaluations() > 50);
111            Assert.assertTrue(optimizer.getEvaluations() < 100);
112            Assert.assertTrue(optimum.getValue() > 1e-2);
113        }
114    
115        @Test
116        public void testPowell() {
117            MultivariateFunction powell =
118                new MultivariateFunction() {
119                    public double value(double[] x) {
120                        ++count;
121                        double a = x[0] + 10 * x[1];
122                        double b = x[2] - x[3];
123                        double c = x[1] - 2 * x[2];
124                        double d = x[0] - x[3];
125                        return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
126                    }
127                };
128    
129            count = 0;
130            SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
131            optimizer.setSimplex(new MultiDirectionalSimplex(4));
132            PointValuePair optimum =
133                optimizer.optimize(1000, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 });
134            Assert.assertEquals(count, optimizer.getEvaluations());
135            Assert.assertTrue(optimizer.getEvaluations() > 800);
136            Assert.assertTrue(optimizer.getEvaluations() < 900);
137            Assert.assertTrue(optimum.getValue() > 1e-2);
138        }
139    
140        @Test
141        public void testMath283() {
142            // fails because MultiDirectional.iterateSimplex is looping forever
143            // the while(true) should be replaced with a convergence check
144            SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14);
145            optimizer.setSimplex(new MultiDirectionalSimplex(2));
146            final Gaussian2D function = new Gaussian2D(0, 0, 1);
147            PointValuePair estimate = optimizer.optimize(1000, function,
148                                                             GoalType.MAXIMIZE, function.getMaximumPosition());
149            final double EPSILON = 1e-5;
150            final double expectedMaximum = function.getMaximum();
151            final double actualMaximum = estimate.getValue();
152            Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);
153    
154            final double[] expectedPosition = function.getMaximumPosition();
155            final double[] actualPosition = estimate.getPoint();
156            Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
157            Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );
158        }
159    
160        private static class FourExtrema implements MultivariateFunction {
161            // The following function has 4 local extrema.
162            final double xM = -3.841947088256863675365;
163            final double yM = -1.391745200270734924416;
164            final double xP =  0.2286682237349059125691;
165            final double yP = -yM;
166            final double valueXmYm = 0.2373295333134216789769; // Local maximum.
167            final double valueXmYp = -valueXmYm; // Local minimum.
168            final double valueXpYm = -0.7290400707055187115322; // Global minimum.
169            final double valueXpYp = -valueXpYm; // Global maximum.
170    
171            public double value(double[] variables) {
172                final double x = variables[0];
173                final double y = variables[1];
174                return (x == 0 || y == 0) ? 0 :
175                    FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y);
176            }
177        }
178    
179        private static class Gaussian2D implements MultivariateFunction {
180            private final double[] maximumPosition;
181            private final double std;
182    
183            public Gaussian2D(double xOpt, double yOpt, double std) {
184                maximumPosition = new double[] { xOpt, yOpt };
185                this.std = std;
186            }
187    
188            public double getMaximum() {
189                return value(maximumPosition);
190            }
191    
192            public double[] getMaximumPosition() {
193                return maximumPosition.clone();
194            }
195    
196            public double value(double[] point) {
197                final double x = point[0], y = point[1];
198                final double twoS2 = 2.0 * std * std;
199                return 1.0 / (twoS2 * FastMath.PI) * FastMath.exp(-(x * x + y * y) / twoS2);
200            }
201        }
202    
203        private int count;
204    }