本文整理汇总了Java中org.apache.commons.math3.analysis.solvers.BrentSolver类的典型用法代码示例。如果您正苦于以下问题:Java BrentSolver类的具体用法?Java BrentSolver怎么用?Java BrentSolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BrentSolver类属于org.apache.commons.math3.analysis.solvers包,在下文中一共展示了BrentSolver类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSolveWaterVapourFunction
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Test
public void testSolveWaterVapourFunction() throws Exception {
// all numbers in this test taken from IDL test run, cellIndexX=1, cellIndexY=0
WaterVapourFunction wvFunction = new WaterVapourFunction();
wvFunction.setMerisRatio(0.64714217);
wvFunction.setWvGr2(new double[]{0.301, 1.0, 1.5, 2.0, 2.7, 4.999});
final double[][][] parAtmH = initParAtmH();
wvFunction.setParAtmH(parAtmH);
wvFunction.setReflPix(new double[]{0.358543, 0.365387});
// Define the fractional tolerance:
final double ftol = 1.0e-4;
final int maxIter = 10000;
final double wvLower = 0.302;
final double wvUpper = 4.998;
BrentSolver brentSolver = new BrentSolver(ftol);
double result = brentSolver.solve(maxIter, wvFunction, wvLower, wvUpper);
assertEquals(1.96476, result, ftol);
}
示例2: testDekkerBrentFunction
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Test
public void testDekkerBrentFunction() throws Exception {
// Define the fractional tolerance:
final double ftol = 1.0e-4;
final int maxIter = 10000;
// f(x) = x - 1
DekkerBrentTestFunction1 testFunction = new DekkerBrentTestFunction1();
double a = 0.5;
double b = 2.0;
BrentSolver brentSolver = new BrentSolver(ftol);
double result = brentSolver.solve(maxIter, testFunction, a, b);
assertEquals(1.0, result, 1.E-4);
// f(x) = cos(x)
DekkerBrentTestFunction2 testFunction2 = new DekkerBrentTestFunction2();
a = 1.0;
b = 2.0;
result = brentSolver.solve(maxIter, testFunction2, a, b);
assertEquals(Math.PI/2.0, result, 1.E-4);
}
示例3: testCircleFitting
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Test
public void testCircleFitting() {
CircleScalar circle = new CircleScalar();
circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0);
circle.addPoint(110.0, -20.0);
circle.addPoint( 35.0, 15.0);
circle.addPoint( 45.0, 97.0);
NonLinearConjugateGradientOptimizer optimizer =
new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE,
new SimpleValueChecker(1e-30, 1e-30),
new BrentSolver(1e-15, 1e-13));
PointValuePair optimum =
optimizer.optimize(100, circle, GoalType.MINIMIZE, new double[] { 98.680, 47.345 });
Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
Assert.assertEquals(69.960161753, circle.getRadius(center), 1.0e-8);
Assert.assertEquals(96.075902096, center.getX(), 1.0e-8);
Assert.assertEquals(48.135167894, center.getY(), 1.0e-8);
}
示例4: doSolve
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Override
protected double doSolve() throws TooManyEvaluationsException, NoBracketingException {
final double min = getMin();
final double max = getMax();
final double[] xSearchGrid = createHybridSearchGrid(min, max, numBisections, depth);
final double[] fSearchGrid = Arrays.stream(xSearchGrid).map(this::computeObjectiveValue).toArray();
/* find bracketing intervals on the search grid */
final List<Bracket> bracketsList = detectBrackets(xSearchGrid, fSearchGrid);
if (bracketsList.isEmpty()) {
throw new NoBracketingException(min, max, fSearchGrid[0], fSearchGrid[fSearchGrid.length-1]);
}
final BrentSolver solver = new BrentSolver(getRelativeAccuracy(), getAbsoluteAccuracy(), getFunctionValueAccuracy());
final List<Double> roots = bracketsList.stream()
.map(b -> solver.solve(getMaxEvaluations(), this::computeObjectiveValue, b.min, b.max, 0.5 * (b.min + b.max)))
.collect(Collectors.toList());
if (roots.size() == 1 || meritFunc == null) {
return roots.get(0);
}
final double[] merits = roots.stream().mapToDouble(meritFunc::value).toArray();
final int bestRootIndex = IntStream.range(0, roots.size())
.boxed()
.max((i, j) -> (int) (merits[i] - merits[j]))
.get();
return roots.get(bestRootIndex);
}
示例5: simpleTest
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
/**
* Test on a 4th degree polynomial with 4 real roots at x = 0, 1, 2, 3. This objective function is positive for
* large enough positive and negative values of its arguments. Therefore, the simple Brent solver complains that
* the search interval does not bracket a root. The robust Brent solver, however, subdivides the given search
* interval and finds a bracketing sub-interval.
*
* The "best" root according to the given merit function (set to the anti-derivative of the objective function)
* is in fact the one at x = 0. We require the robust solver to output x = 0, and the simple solver to fail.
*/
@Test
public void simpleTest() {
final UnivariateFunction objFunc = x -> 30 * x * (x - 1) * (x - 2) * (x - 3);
final UnivariateFunction meritFunc = x -> 6 * FastMath.pow(x, 5) - 45 * FastMath.pow(x, 4) + 110 * FastMath.pow(x, 3) -
90 * FastMath.pow(x, 2);
final RobustBrentSolver solverRobust = new RobustBrentSolver(DEF_REL_ACC, DEF_REL_ACC, DEF_F_ACC,
meritFunc, 4, 1);
final BrentSolver solverSimple = new BrentSolver(DEF_REL_ACC, DEF_REL_ACC, DEF_F_ACC);
final double xRobust = solverRobust.solve(100, objFunc, -1, 4);
Assert.assertEquals(xRobust, 0, DEF_ABS_ACC);
boolean simpleSolverFails = false;
try {
/* this will fail */
solverSimple.solve(100, objFunc, -1, 4);
} catch (final NoBracketingException ex) {
simpleSolverFails = true;
}
Assert.assertTrue(simpleSolverFails);
}
示例6: testCircleFitting
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Test
public void testCircleFitting() {
CircleScalar problem = new CircleScalar();
problem.addPoint( 30.0, 68.0);
problem.addPoint( 50.0, -6.0);
problem.addPoint(110.0, -20.0);
problem.addPoint( 35.0, 15.0);
problem.addPoint( 45.0, 97.0);
NonLinearConjugateGradientOptimizer optimizer
= new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
new SimpleValueChecker(1e-30, 1e-30),
new BrentSolver(1e-15, 1e-13));
PointValuePair optimum
= optimizer.optimize(new MaxEval(100),
problem.getObjectiveFunction(),
problem.getObjectiveFunctionGradient(),
GoalType.MINIMIZE,
new InitialGuess(new double[] { 98.680, 47.345 }));
Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
Assert.assertEquals(69.960161753, problem.getRadius(center), 1.0e-8);
Assert.assertEquals(96.075902096, center.getX(), 1.0e-8);
Assert.assertEquals(48.135167894, center.getY(), 1.0e-8);
}
示例7: testIllConditioned
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Test
public void testIllConditioned() {
LinearProblem problem1 = new LinearProblem(new double[][] {
{ 10.0, 7.0, 8.0, 7.0 },
{ 7.0, 5.0, 6.0, 5.0 },
{ 8.0, 6.0, 10.0, 9.0 },
{ 7.0, 5.0, 9.0, 10.0 }
}, new double[] { 32, 23, 33, 31 });
NonLinearConjugateGradientOptimizer optimizer =
new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE,
new SimpleValueChecker(1e-13, 1e-13),
new BrentSolver(1e-15, 1e-15));
PointValuePair optimum1 =
optimizer.optimize(200, problem1, GoalType.MINIMIZE, new double[] { 0, 1, 2, 3 });
Assert.assertEquals(1.0, optimum1.getPoint()[0], 1.0e-4);
Assert.assertEquals(1.0, optimum1.getPoint()[1], 1.0e-4);
Assert.assertEquals(1.0, optimum1.getPoint()[2], 1.0e-4);
Assert.assertEquals(1.0, optimum1.getPoint()[3], 1.0e-4);
LinearProblem problem2 = new LinearProblem(new double[][] {
{ 10.00, 7.00, 8.10, 7.20 },
{ 7.08, 5.04, 6.00, 5.00 },
{ 8.00, 5.98, 9.89, 9.00 },
{ 6.99, 4.99, 9.00, 9.98 }
}, new double[] { 32, 23, 33, 31 });
PointValuePair optimum2 =
optimizer.optimize(200, problem2, GoalType.MINIMIZE, new double[] { 0, 1, 2, 3 });
Assert.assertEquals(-81.0, optimum2.getPoint()[0], 1.0e-1);
Assert.assertEquals(137.0, optimum2.getPoint()[1], 1.0e-1);
Assert.assertEquals(-34.0, optimum2.getPoint()[2], 1.0e-1);
Assert.assertEquals( 22.0, optimum2.getPoint()[3], 1.0e-1);
}
示例8: testTwoSets
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Test
public void testTwoSets() {
final double epsilon = 1.0e-7;
LinearProblem problem = new LinearProblem(new double[][] {
{ 2, 1, 0, 4, 0, 0 },
{ -4, -2, 3, -7, 0, 0 },
{ 4, 1, -2, 8, 0, 0 },
{ 0, -3, -12, -1, 0, 0 },
{ 0, 0, 0, 0, epsilon, 1 },
{ 0, 0, 0, 0, 1, 1 }
}, new double[] { 2, -9, 2, 2, 1 + epsilon * epsilon, 2});
final Preconditioner preconditioner
= new Preconditioner() {
public double[] precondition(double[] point, double[] r) {
double[] d = r.clone();
d[0] /= 72.0;
d[1] /= 30.0;
d[2] /= 314.0;
d[3] /= 260.0;
d[4] /= 2 * (1 + epsilon * epsilon);
d[5] /= 4.0;
return d;
}
};
NonLinearConjugateGradientOptimizer optimizer =
new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE,
new SimpleValueChecker(1e-13, 1e-13),
new BrentSolver(),
preconditioner);
PointValuePair optimum =
optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0, 0 });
Assert.assertEquals( 3.0, optimum.getPoint()[0], 1.0e-10);
Assert.assertEquals( 4.0, optimum.getPoint()[1], 1.0e-10);
Assert.assertEquals(-1.0, optimum.getPoint()[2], 1.0e-10);
Assert.assertEquals(-2.0, optimum.getPoint()[3], 1.0e-10);
Assert.assertEquals( 1.0 + epsilon, optimum.getPoint()[4], 1.0e-10);
Assert.assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10);
}
示例9: testTwoSets
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Test
public void testTwoSets() {
final double epsilon = 1.0e-7;
LinearProblem problem = new LinearProblem(new double[][] {
{ 2, 1, 0, 4, 0, 0 },
{ -4, -2, 3, -7, 0, 0 },
{ 4, 1, -2, 8, 0, 0 },
{ 0, -3, -12, -1, 0, 0 },
{ 0, 0, 0, 0, epsilon, 1 },
{ 0, 0, 0, 0, 1, 1 }
}, new double[] { 2, -9, 2, 2, 1 + epsilon * epsilon, 2});
final Preconditioner preconditioner
= new Preconditioner() {
public double[] precondition(double[] point, double[] r) {
double[] d = r.clone();
d[0] /= 72.0;
d[1] /= 30.0;
d[2] /= 314.0;
d[3] /= 260.0;
d[4] /= 2 * (1 + epsilon * epsilon);
d[5] /= 4.0;
return d;
}
};
NonLinearConjugateGradientOptimizer optimizer
= new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
new SimpleValueChecker(1e-13, 1e-13),
new BrentSolver(),
preconditioner);
PointValuePair optimum
= optimizer.optimize(new MaxEval(100),
problem.getObjectiveFunction(),
problem.getObjectiveFunctionGradient(),
GoalType.MINIMIZE,
new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 }));
Assert.assertEquals( 3.0, optimum.getPoint()[0], 1.0e-10);
Assert.assertEquals( 4.0, optimum.getPoint()[1], 1.0e-10);
Assert.assertEquals(-1.0, optimum.getPoint()[2], 1.0e-10);
Assert.assertEquals(-2.0, optimum.getPoint()[3], 1.0e-10);
Assert.assertEquals( 1.0 + epsilon, optimum.getPoint()[4], 1.0e-10);
Assert.assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10);
}
示例10: testIllConditioned
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
@Test
public void testIllConditioned() {
LinearProblem problem1 = new LinearProblem(new double[][] {
{ 10.0, 7.0, 8.0, 7.0 },
{ 7.0, 5.0, 6.0, 5.0 },
{ 8.0, 6.0, 10.0, 9.0 },
{ 7.0, 5.0, 9.0, 10.0 }
}, new double[] { 32, 23, 33, 31 });
NonLinearConjugateGradientOptimizer optimizer
= new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
new SimpleValueChecker(1e-13, 1e-13),
new BrentSolver(1e-15, 1e-15));
PointValuePair optimum1
= optimizer.optimize(new MaxEval(200),
problem1.getObjectiveFunction(),
problem1.getObjectiveFunctionGradient(),
GoalType.MINIMIZE,
new InitialGuess(new double[] { 0, 1, 2, 3 }));
Assert.assertEquals(1.0, optimum1.getPoint()[0], 1.0e-4);
Assert.assertEquals(1.0, optimum1.getPoint()[1], 1.0e-4);
Assert.assertEquals(1.0, optimum1.getPoint()[2], 1.0e-4);
Assert.assertEquals(1.0, optimum1.getPoint()[3], 1.0e-4);
LinearProblem problem2 = new LinearProblem(new double[][] {
{ 10.00, 7.00, 8.10, 7.20 },
{ 7.08, 5.04, 6.00, 5.00 },
{ 8.00, 5.98, 9.89, 9.00 },
{ 6.99, 4.99, 9.00, 9.98 }
}, new double[] { 32, 23, 33, 31 });
PointValuePair optimum2
= optimizer.optimize(new MaxEval(200),
problem2.getObjectiveFunction(),
problem2.getObjectiveFunctionGradient(),
GoalType.MINIMIZE,
new InitialGuess(new double[] { 0, 1, 2, 3 }));
Assert.assertEquals(-81.0, optimum2.getPoint()[0], 1.0e-1);
Assert.assertEquals(137.0, optimum2.getPoint()[1], 1.0e-1);
Assert.assertEquals(-34.0, optimum2.getPoint()[2], 1.0e-1);
Assert.assertEquals( 22.0, optimum2.getPoint()[3], 1.0e-1);
}
示例11: NonLinearConjugateGradientOptimizer
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
/**
* Constructor with default {@link BrentSolver line search solver} and
* {@link IdentityPreconditioner preconditioner}.
*
* @param updateFormula formula to use for updating the β parameter,
* must be one of {@link ConjugateGradientFormula#FLETCHER_REEVES} or {@link
* ConjugateGradientFormula#POLAK_RIBIERE}.
* @param checker Convergence checker.
*/
public NonLinearConjugateGradientOptimizer(final ConjugateGradientFormula updateFormula,
ConvergenceChecker<PointValuePair> checker) {
this(updateFormula,
checker,
new BrentSolver(),
new IdentityPreconditioner());
}
示例12: NonLinearConjugateGradientOptimizer
import org.apache.commons.math3.analysis.solvers.BrentSolver; //导入依赖的package包/类
/**
* Constructor with default {@link BrentSolver line search solver} and
* {@link IdentityPreconditioner preconditioner}.
*
* @param updateFormula formula to use for updating the β parameter,
* must be one of {@link Formula#FLETCHER_REEVES} or
* {@link Formula#POLAK_RIBIERE}.
* @param checker Convergence checker.
*/
public NonLinearConjugateGradientOptimizer(final Formula updateFormula,
ConvergenceChecker<PointValuePair> checker) {
this(updateFormula,
checker,
new BrentSolver(),
new IdentityPreconditioner());
}