Java源码示例:org.apache.commons.math3.optim.SimplePointChecker
示例1
@Test
public void testOptimumOutsideRange() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(600),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例2
@Test
public void testHalfBounded() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0,
1.0, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-10, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(400),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例3
@Test
public void testOptimumOutsideRange() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(600),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例4
@Test
public void testHalfBounded() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0,
1.0, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-10, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(400),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例5
@Test
public void testOptimumOutsideRange() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(600),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例6
@Test
public void testHalfBounded() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0,
1.0, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-10, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(400),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例7
@Test
public void testOptimumOutsideRange() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(600),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例8
@Test
public void testHalfBounded() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0,
1.0, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-10, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(400),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例9
@Test
public void testOptimumOutsideRange() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(600),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例10
@Test
public void testHalfBounded() {
final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0,
1.0, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 3.0);
final MultivariateFunctionPenaltyAdapter wrapped
= new MultivariateFunctionPenaltyAdapter(biQuadratic,
biQuadratic.getLower(),
biQuadratic.getUpper(),
1000.0, new double[] { 100.0, 100.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-10, 1.0e-20));
final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });
final PointValuePair optimum
= optimizer.optimize(new MaxEval(400),
new ObjectiveFunction(wrapped),
simplex,
GoalType.MINIMIZE,
new InitialGuess(new double[] { -1.5, 4.0 }));
Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
示例11
@Override
public Optimizer getOptimizer(final ObjectiveFunction objectiveFunction, final double[] initialParameters, final double[] lowerBound,final double[] upperBound, final double[] parameterStep, final double[] targetValues) {
final double[] values = new double[targetValues.length];
final double[] effectiveParameterLowerBound = parameterLowerBound != null ? parameterLowerBound : lowerBound;
final double[] effectiveParameterUpperBound = parameterUppderBound != null ? parameterUppderBound : upperBound;
final double[] effectiveParameterStandardDeviation = parameterStandardDeviation != null ? parameterStandardDeviation : parameterStep;
// Throw exception if std dev is non null, but lower bound / upper bound are null.
return new Optimizer() {
private org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer optimizer;
private org.apache.commons.math3.optim.PointValuePair result;
@Override
public double[] getBestFitParameters() {
return result.getPoint();
}
@Override
public double getRootMeanSquaredError() {
return result.getValue();
}
@Override
public int getIterations() {
return optimizer != null ? optimizer.getIterations() : 0;
}
@Override
public void run() {
optimizer = new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer(maxIterations, accuracy, true, 0, 0, new MersenneTwister(3141), false, new SimplePointChecker<org.apache.commons.math3.optim.PointValuePair>(0, 0)) {
@Override
public double computeObjectiveValue(final double[] parameters) {
try {
objectiveFunction.setValues(parameters, values);
} catch (final SolverException e) {
return Double.NaN;
}
double rms = 0;
for(final double value : values) {
rms += value*value;
}
return Math.sqrt(rms);
}
@Override
public org.apache.commons.math3.optim.nonlinear.scalar.GoalType getGoalType() {
return org.apache.commons.math3.optim.nonlinear.scalar.GoalType.MINIMIZE;
}
@Override
public double[] getStartPoint() {
return initialParameters;
}
@Override
public double[] getLowerBound() {
return effectiveParameterLowerBound;
}
@Override
public double[] getUpperBound() {
return effectiveParameterUpperBound;
}
};
try {
result = optimizer.optimize(
new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.PopulationSize((int) (4 + 3 * Math.log(initialParameters.length))),
new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.Sigma(effectiveParameterStandardDeviation)
);
} catch(final org.apache.commons.math3.exception.MathIllegalStateException e) {
new SolverException(e);
}
}
};
}
示例12
@Override
public Optimizer getOptimizer(final ObjectiveFunction objectiveFunction, final double[] initialParameters, final double[] lowerBound,final double[] upperBound, final double[] parameterStep, final double[] targetValues) {
final double[] values = new double[targetValues.length];
final double[] effectiveParameterLowerBound = parameterLowerBound != null ? parameterLowerBound : lowerBound;
final double[] effectiveParameterUpperBound = parameterUppderBound != null ? parameterUppderBound : upperBound;
final double[] effectiveParameterStandardDeviation = parameterStandardDeviation != null ? parameterStandardDeviation : parameterStep;
// Throw exception if std dev is non null, but lower bound / upper bound are null.
return new Optimizer() {
private org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer optimizer;
private org.apache.commons.math3.optim.PointValuePair result;
@Override
public double[] getBestFitParameters() {
return result.getPoint();
}
@Override
public double getRootMeanSquaredError() {
return result.getValue();
}
@Override
public int getIterations() {
return optimizer != null ? optimizer.getIterations() : 0;
}
@Override
public void run() {
optimizer = new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer(maxIterations, accuracy, true, 0, 0, new MersenneTwister(3141), false, new SimplePointChecker<org.apache.commons.math3.optim.PointValuePair>(0, 0)) {
@Override
public double computeObjectiveValue(final double[] parameters) {
try {
objectiveFunction.setValues(parameters, values);
} catch (final SolverException e) {
return Double.NaN;
}
double rms = 0;
for(final double value : values) {
rms += value*value;
}
return Math.sqrt(rms);
}
@Override
public org.apache.commons.math3.optim.nonlinear.scalar.GoalType getGoalType() {
return org.apache.commons.math3.optim.nonlinear.scalar.GoalType.MINIMIZE;
}
@Override
public double[] getStartPoint() {
return initialParameters;
}
@Override
public double[] getLowerBound() {
return effectiveParameterLowerBound;
}
@Override
public double[] getUpperBound() {
return effectiveParameterUpperBound;
}
};
try {
result = optimizer.optimize(
new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.PopulationSize((int) (4 + 3 * Math.log(initialParameters.length))),
new org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.Sigma(effectiveParameterStandardDeviation)
);
} catch(final org.apache.commons.math3.exception.MathIllegalStateException e) {
new SolverException(e);
}
}
};
}