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.math.optimization; 019 020 import static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.assertTrue; 022 import static org.junit.Assert.fail; 023 024 import org.apache.commons.math.MathException; 025 import org.apache.commons.math.analysis.QuinticFunction; 026 import org.apache.commons.math.analysis.SinFunction; 027 import org.apache.commons.math.analysis.UnivariateRealFunction; 028 import org.apache.commons.math.optimization.univariate.BrentOptimizer; 029 import org.apache.commons.math.random.JDKRandomGenerator; 030 import org.junit.Test; 031 032 public class MultiStartUnivariateRealOptimizerTest { 033 034 @Test 035 public void testSinMin() throws MathException { 036 UnivariateRealFunction f = new SinFunction(); 037 UnivariateRealOptimizer underlying = new BrentOptimizer(); 038 JDKRandomGenerator g = new JDKRandomGenerator(); 039 g.setSeed(44428400075l); 040 MultiStartUnivariateRealOptimizer minimizer = 041 new MultiStartUnivariateRealOptimizer(underlying, 10, g); 042 minimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0); 043 double[] optima = minimizer.getOptima(); 044 double[] optimaValues = minimizer.getOptimaValues(); 045 for (int i = 1; i < optima.length; ++i) { 046 double d = (optima[i] - optima[i-1]) / (2 * Math.PI); 047 assertTrue (Math.abs(d - Math.rint(d)) < 1.0e-8); 048 assertEquals(-1.0, f.value(optima[i]), 1.0e-10); 049 assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10); 050 } 051 assertTrue(minimizer.getEvaluations() > 2900); 052 assertTrue(minimizer.getEvaluations() < 3100); 053 } 054 055 @Test 056 public void testQuinticMin() throws MathException { 057 // The quintic function has zeros at 0, +-0.5 and +-1. 058 // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643, 059 UnivariateRealFunction f = new QuinticFunction(); 060 UnivariateRealOptimizer underlying = new BrentOptimizer(); 061 JDKRandomGenerator g = new JDKRandomGenerator(); 062 g.setSeed(4312000053l); 063 MultiStartUnivariateRealOptimizer minimizer = 064 new MultiStartUnivariateRealOptimizer(underlying, 5, g); 065 minimizer.setAbsoluteAccuracy(10 * minimizer.getAbsoluteAccuracy()); 066 minimizer.setRelativeAccuracy(10 * minimizer.getRelativeAccuracy()); 067 068 try { 069 minimizer.getOptima(); 070 fail("an exception should have been thrown"); 071 } catch (IllegalStateException ise) { 072 // expected 073 } catch (Exception e) { 074 fail("wrong exception caught"); 075 } 076 try { 077 minimizer.getOptimaValues(); 078 fail("an exception should have been thrown"); 079 } catch (IllegalStateException ise) { 080 // expected 081 } catch (Exception e) { 082 fail("wrong exception caught"); 083 } 084 085 assertEquals(-0.27195612846834, minimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2), 1.0e-13); 086 assertEquals(-0.27194301946870, minimizer.getResult(), 1.0e-13); 087 assertEquals(-0.04433426940878, minimizer.getFunctionValue(), 1.0e-13); 088 089 double[] optima = minimizer.getOptima(); 090 double[] optimaValues = minimizer.getOptimaValues(); 091 for (int i = 0; i < optima.length; ++i) { 092 assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10); 093 } 094 095 assertTrue(minimizer.getEvaluations() >= 510); 096 assertTrue(minimizer.getEvaluations() <= 530); 097 assertTrue(minimizer.getIterationCount() >= 150); 098 assertTrue(minimizer.getIterationCount() <= 170); 099 100 } 101 102 }