Skip to content

Commit

Permalink
MATH-1658: Enable passing a "ConvergenceChecker" as "OptimizationData".
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles Sadowski committed Jun 29, 2023
1 parent 4b0ffd3 commit 2fb5f6a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class BaseOptimizer<PAIR> {
private static final MaxIterCallback MAX_ITER_CALLBACK = new MaxIterCallback();

/** Convergence checker. */
private final ConvergenceChecker<PAIR> checker;
private ConvergenceChecker<PAIR> checker;
/** Maximum number of evaluations. */
private int maxEvaluations;
/** Maximum number of iterations. */
Expand Down Expand Up @@ -141,6 +141,7 @@ public ConvergenceChecker<PAIR> getConvergenceChecker() {
* <ul>
* <li>{@link MaxEval}</li>
* <li>{@link MaxIter}</li>
* <li>{@link ConvergenceChecker}</li>
* </ul>
* @return a point/value pair that satisfies the convergence criteria.
* @throws TooManyEvaluationsException if the maximal number of
Expand Down Expand Up @@ -210,6 +211,7 @@ protected void incrementIterationCount() {
* <ul>
* <li>{@link MaxEval}</li>
* <li>{@link MaxIter}</li>
* <li>{@link ConvergenceChecker}</li>
* </ul>
*/
protected void parseOptimizationData(OptimizationData... optData) {
Expand All @@ -224,6 +226,10 @@ protected void parseOptimizationData(OptimizationData... optData) {
maxIterations = ((MaxIter) data).getMaxIter();
continue;
}
if (data instanceof ConvergenceChecker) {
checker = (ConvergenceChecker<PAIR>) data;
continue;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*
* @since 3.0
*/
public interface ConvergenceChecker<PAIR> {
public interface ConvergenceChecker<PAIR> extends OptimizationData {
/**
* Check if the optimization algorithm has converged.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.legacy.optim;

import org.junit.Test;
import org.junit.Assert;

/**
* Tests for {@link BaseOptimizer}.
*/
public class BaseOptimizerTest {
@Test
public void testDefault() {
final DummyOptimizer dummy = new DummyOptimizer(null);

final Object result = dummy.optimize();

// No default checker.
Assert.assertEquals(null, dummy.getConvergenceChecker());
// Default "MaxIter".
Assert.assertEquals(Integer.MAX_VALUE, dummy.getMaxIterations());
// Default "MaxEval".
Assert.assertEquals(0, dummy.getMaxEvaluations());
}

@Test
public void testParseOptimizationData() {
final DummyOptimizer dummy = new DummyOptimizer(null);

final ConvergenceChecker<Object> checker = new ConvergenceChecker<Object>() {
@Override
public boolean converged(int iteration,
Object previous,
Object current) {
return true;
}
};

final int maxEval = 123;
final int maxIter = 4;
final Object result = dummy.optimize(checker,
new MaxEval(maxEval),
new MaxIter(maxIter));

Assert.assertEquals(checker, dummy.getConvergenceChecker());
Assert.assertEquals(maxIter, dummy.getMaxIterations());
Assert.assertEquals(maxEval, dummy.getMaxEvaluations());
}
}

class DummyOptimizer extends BaseOptimizer<Object> {
/**
* @param checker Checker.
*/
DummyOptimizer(ConvergenceChecker<Object> checker) {
super(checker);
}

@Override
protected Object doOptimize() {
return new Object();
}
}
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Caveat:
to support the whole codebase (it was one of the main reasons for
creating more focused components).
">
<action dev="erans" type="update" issue="MATH-1658">
Allow convergence checker to be specified as "OptimizationData".
</action>
<action dev="erans" type="update" issue="MATH-1657" due-to="François Laferrière">
Small refactoring of the "evaluation counter" in optimizers' base class:
Provide (protected) accessor (to replace method "computeObjectiveValue").
Expand Down

0 comments on commit 2fb5f6a

Please sign in to comment.