Fixed missing "repair" of a point that lies outside the boundaries. Thanks
to Frank Hessen for the report and for pinpointing the cause of the problem.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1388517 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2012-09-21 14:17:37 +00:00
parent ce118ef14a
commit abe53a537b
3 changed files with 38 additions and 1 deletions

View File

@ -52,6 +52,10 @@ If the output is not quite correct, check for invisible trailing spaces!
<body>
<release version="3.1" date="TBD" description="
">
<action dev="erans" type="fix" issue="MATH-864" due-to="Frank Hess">
"CMAESOptimizer": Solution was not constrained to lie within the
provided boundaries.
</action>
<action dev="erans" type="add" issue="MATH-863" due-to="Julien Anxionnat">
New "Quaternion" class (package "o.a.c.m.complex").
</action>

View File

@ -24,9 +24,11 @@ import java.util.List;
import org.apache.commons.math3.analysis.MultivariateFunction;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.MathUnsupportedOperationException;
import org.apache.commons.math3.exception.MathIllegalStateException;
import org.apache.commons.math3.exception.NotPositiveException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.TooManyEvaluationsException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.EigenDecomposition;
import org.apache.commons.math3.linear.MatrixUtils;
@ -414,7 +416,7 @@ public class CMAESOptimizer
bestValue = bestFitness;
lastResult = optimum;
optimum = new PointValuePair(
fitfun.decode(bestArx.getColumn(0)),
fitfun.repairAndDecode(bestArx.getColumn(0)),
isMinimize ? bestFitness : -bestFitness);
if (getConvergenceChecker() != null && lastResult != null) {
if (getConvergenceChecker().converged(iterations, optimum, lastResult)) {
@ -911,6 +913,16 @@ public class CMAESOptimizer
return res;
}
/**
* @param x Normalized objective variables.
* @return the original objective variables, possibly repaired.
*/
public double[] repairAndDecode(final double[] x) {
return boundaries != null && isRepairMode ?
decode(repair(x)) :
decode(x);
}
/**
* @param x Normalized objective variables.
* @return the original objective variables.

View File

@ -372,6 +372,27 @@ public class CMAESOptimizerTest {
1e-10, 1e-4, 1000000, expected);
}
@Test
public void testMath864() {
final CMAESOptimizer optimizer = new CMAESOptimizer();
final MultivariateFunction fitnessFunction = new MultivariateFunction() {
@Override
public double value(double[] parameters) {
final double target = 1;
final double error = target - parameters[0];
return error * error;
}
};
final double[] start = { 0 };
final double[] lower = { -1e6 };
final double[] upper = { 0.5 };
final double[] result = optimizer.optimize(10000, fitnessFunction, GoalType.MINIMIZE,
start, lower, upper).getPoint();
Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")",
result[0] <= upper[0]);
}
/**
* @param func Function to optimize.
* @param startPoint Starting point.