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;
18  
19  import org.junit.Test;
20  import org.junit.Assert;
21  
22  /**
23   * Tests for {@link BaseOptimizer}.
24   */
25  public class BaseOptimizerTest {
26      @Test
27      public void testDefault() {
28          final DummyOptimizer dummy = new DummyOptimizer(null);
29  
30          final Object result = dummy.optimize();
31  
32          // No default checker.
33          Assert.assertEquals(null, dummy.getConvergenceChecker());
34          // Default "MaxIter".
35          Assert.assertEquals(Integer.MAX_VALUE, dummy.getMaxIterations());
36          // Default "MaxEval".
37          Assert.assertEquals(0, dummy.getMaxEvaluations());
38      }
39  
40      @Test
41      public void testParseOptimizationData() {
42          final DummyOptimizer dummy = new DummyOptimizer(null);
43  
44          final ConvergenceChecker<Object> checker = new ConvergenceChecker<Object>() {
45                  @Override
46                  public boolean converged(int iteration,
47                                           Object previous,
48                                           Object current) {
49                      return true;
50                  }
51              };
52  
53          final int maxEval = 123;
54          final int maxIter = 4;
55          final Object result = dummy.optimize(checker,
56                                               new MaxEval(maxEval),
57                                               new MaxIter(maxIter));
58  
59          Assert.assertEquals(checker, dummy.getConvergenceChecker());
60          Assert.assertEquals(maxIter, dummy.getMaxIterations());
61          Assert.assertEquals(maxEval, dummy.getMaxEvaluations());
62      }
63  }
64  
65  class DummyOptimizer extends BaseOptimizer<Object> {
66      /**
67       * @param checker Checker.
68       */
69      DummyOptimizer(ConvergenceChecker<Object> checker) {
70          super(checker);
71      }
72  
73      @Override
74      protected Object doOptimize() {
75          return new Object();
76      }
77  }