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 023 import org.apache.commons.math.ConvergenceException; 024 import org.apache.commons.math.FunctionEvaluationException; 025 import org.apache.commons.math.analysis.MultivariateRealFunction; 026 import org.apache.commons.math.optimization.direct.NelderMead; 027 import org.apache.commons.math.random.GaussianRandomGenerator; 028 import org.apache.commons.math.random.JDKRandomGenerator; 029 import org.apache.commons.math.random.RandomVectorGenerator; 030 import org.apache.commons.math.random.UncorrelatedRandomVectorGenerator; 031 import org.junit.Test; 032 033 public class MultiStartMultivariateRealOptimizerTest { 034 035 @Test 036 public void testRosenbrock() 037 throws FunctionEvaluationException, ConvergenceException { 038 039 Rosenbrock rosenbrock = new Rosenbrock(); 040 NelderMead underlying = new NelderMead(); 041 underlying.setStartConfiguration(new double[][] { 042 { -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 } 043 }); 044 JDKRandomGenerator g = new JDKRandomGenerator(); 045 g.setSeed(16069223052l); 046 RandomVectorGenerator generator = 047 new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g)); 048 MultiStartMultivariateRealOptimizer optimizer = 049 new MultiStartMultivariateRealOptimizer(underlying, 10, generator); 050 optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3)); 051 optimizer.setMaxIterations(100); 052 RealPointValuePair optimum = 053 optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 }); 054 055 assertEquals(rosenbrock.getCount(), optimizer.getEvaluations()); 056 assertTrue(optimizer.getEvaluations() > 20); 057 assertTrue(optimizer.getEvaluations() < 250); 058 assertTrue(optimum.getValue() < 8.0e-4); 059 060 } 061 062 private static class Rosenbrock implements MultivariateRealFunction { 063 064 private int count; 065 066 public Rosenbrock() { 067 count = 0; 068 } 069 070 public double value(double[] x) throws FunctionEvaluationException { 071 ++count; 072 double a = x[1] - x[0] * x[0]; 073 double b = 1.0 - x[0]; 074 return 100 * a * a + b * b; 075 } 076 077 public int getCount() { 078 return count; 079 } 080 081 } 082 083 }