org.apache.commons.math.optimization.OptimizationException Java Examples
The following examples show how to use
org.apache.commons.math.optimization.OptimizationException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NonLinearConjugateGradientOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testInconsistentEquations() throws FunctionEvaluationException, OptimizationException { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 4.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6)); RealPointValuePair optimum = optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 1, 1 }); assertTrue(optimum.getValue() > 0.1); }
Example #2
Source File: Math_82_SimplexSolver_t.java From coming with MIT License | 6 votes |
/** * Runs one iteration of the Simplex method on the given model. * @param tableau simple tableau for the problem * @throws OptimizationException if the maximal iteration count has been * exceeded or if the model is found not to have a bounded solution */ protected void doIteration(final SimplexTableau tableau) throws OptimizationException { incrementIterationsCounter(); Integer pivotCol = getPivotColumn(tableau); Integer pivotRow = getPivotRow(pivotCol, tableau); if (pivotRow == null) { throw new UnboundedSolutionException(); } // set the pivot element to 1 double pivotVal = tableau.getEntry(pivotRow, pivotCol); tableau.divideRow(pivotRow, pivotVal); // set the rest of the pivot column to 0 for (int i = 0; i < tableau.getHeight(); i++) { if (i != pivotRow) { double multiplier = tableau.getEntry(i, pivotCol); tableau.subtractRow(i, pivotRow, multiplier); } } }
Example #3
Source File: CurveFitter.java From astor with GNU General Public License v2.0 | 6 votes |
/** Fit a curve. * <p>This method compute the coefficients of the curve that best * fit the sample of observed points previously given through calls * to the {@link #addObservedPoint(WeightedObservedPoint) * addObservedPoint} method.</p> * @param f parametric function to fit * @param initialGuess first guess of the function parameters * @return fitted parameters * @exception FunctionEvaluationException if the objective function throws one during * the search * @exception OptimizationException if the algorithm failed to converge * @exception IllegalArgumentException if the start point dimension is wrong */ public double[] fit(final ParametricRealFunction f, final double[] initialGuess) throws FunctionEvaluationException, OptimizationException, IllegalArgumentException { // prepare least squares problem double[] target = new double[observations.size()]; double[] weights = new double[observations.size()]; int i = 0; for (WeightedObservedPoint point : observations) { target[i] = point.getY(); weights[i] = point.getWeight(); ++i; } // perform the fit VectorialPointValuePair optimum = optimizer.optimize(new TheoreticalValuesFunction(f), target, weights, initialGuess); // extract the coefficients return optimum.getPointRef(); }
Example #4
Source File: HarmonicFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void test1PercentError() throws OptimizationException { Random randomizer = new Random(64925784252l); HarmonicFunction f = new HarmonicFunction(0.2, 3.4, 4.1); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 10.0; x += 0.1) { fitter.addObservedPoint(1.0, x, f.value(x) + 0.01 * randomizer.nextGaussian()); } HarmonicFunction fitted = fitter.fit(); assertEquals(f.getAmplitude(), fitted.getAmplitude(), 7.6e-4); assertEquals(f.getPulsation(), fitted.getPulsation(), 2.7e-3); assertEquals(f.getPhase(), MathUtils.normalizeAngle(fitted.getPhase(), f.getPhase()), 1.3e-2); }
Example #5
Source File: CurveFitter.java From astor with GNU General Public License v2.0 | 6 votes |
/** Fit a curve. * <p>This method compute the coefficients of the curve that best * fit the sample of observed points previously given through calls * to the {@link #addObservedPoint(WeightedObservedPoint) * addObservedPoint} method.</p> * @param f parametric function to fit * @param initialGuess first guess of the function parameters * @return fitted parameters * @exception FunctionEvaluationException if the objective function throws one during * the search * @exception OptimizationException if the algorithm failed to converge * @exception IllegalArgumentException if the start point dimension is wrong */ public double[] fit(final ParametricRealFunction f, final double[] initialGuess) throws FunctionEvaluationException, OptimizationException, IllegalArgumentException { // prepare least squares problem double[] target = new double[observations.size()]; double[] weights = new double[observations.size()]; int i = 0; for (WeightedObservedPoint point : observations) { target[i] = point.getY(); weights[i] = point.getWeight(); ++i; } // perform the fit VectorialPointValuePair optimum = optimizer.optimize(new TheoreticalValuesFunction(f), target, weights, initialGuess); // extract the coefficients return optimum.getPointRef(); }
Example #6
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNoError() throws OptimizationException { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } PolynomialFunction fitted = fitter.fit(); for (double x = -1.0; x < 1.0; x += 0.01) { double error = Math.abs(p.value(x) - fitted.value(x)) / (1.0 + Math.abs(p.value(x))); assertEquals(0.0, error, 1.0e-6); } } }
Example #7
Source File: NonLinearConjugateGradientOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testInconsistentEquations() throws FunctionEvaluationException, OptimizationException { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 4.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6)); RealPointValuePair optimum = optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 1, 1 }); assertTrue(optimum.getValue() > 0.1); }
Example #8
Source File: SimplexSolver.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Solves Phase 1 of the Simplex method. * @param tableau simple tableau for the problem * @exception OptimizationException if the maximal number of iterations is * exceeded, or if the problem is found not to have a bounded solution, or * if there is no feasible solution */ protected void solvePhase1(final SimplexTableau tableau) throws OptimizationException { // make sure we're in Phase 1 if (tableau.getNumArtificialVariables() == 0) { return; } while (!isPhase1Solved(tableau)) { doIteration(tableau); } // if W is not zero then we have no feasible solution if (!MathUtils.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0, epsilon)) { throw new NoFeasibleSolutionException(); } }
Example #9
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMoreEstimatedParametersSimple() { LinearProblem problem = new LinearProblem(new double[][] { { 3.0, 2.0, 0.0, 0.0 }, { 0.0, 1.0, -1.0, 1.0 }, { 2.0, 0.0, 1.0, 0.0 } }, new double[] { 7.0, 3.0, 5.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 7, 6, 5, 4 }); fail("an exception should have been caught"); } catch (OptimizationException ee) { // expected behavior } catch (Exception e) { fail("wrong exception type caught"); } }
Example #10
Source File: SimplexSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testEpsilon() throws OptimizationException { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 10, 5, 1 }, 0); Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); constraints.add(new LinearConstraint(new double[] { 9, 8, 0 }, Relationship.EQ, 17)); constraints.add(new LinearConstraint(new double[] { 0, 7, 8 }, Relationship.LEQ, 7)); constraints.add(new LinearConstraint(new double[] { 10, 0, 2 }, Relationship.LEQ, 10)); SimplexSolver solver = new SimplexSolver(); RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertEquals(1.0, solution.getPoint()[0], 0.0); Assert.assertEquals(1.0, solution.getPoint()[1], 0.0); Assert.assertEquals(0.0, solution.getPoint()[2], 0.0); Assert.assertEquals(15.0, solution.getValue(), 0.0); }
Example #11
Source File: Cardumen_0064_s.java From coming with MIT License | 6 votes |
/** Compute and evaluate a new simplex. * @param original original simplex (to be preserved) * @param coeff linear coefficient * @param comparator comparator to use to sort simplex vertices from best to poorest * @return best point in the transformed simplex * @exception FunctionEvaluationException if the function cannot be evaluated at * some point * @exception OptimizationException if the maximal number of evaluations is exceeded */ private RealPointValuePair evaluateNewSimplex(final RealPointValuePair[] original, final double coeff, final Comparator<RealPointValuePair> comparator) throws FunctionEvaluationException, OptimizationException { final double[] xSmallest = original[0].getPointRef(); final int n = xSmallest.length; // create the linearly transformed simplex simplex = new RealPointValuePair[n + 1]; simplex[0] = original[0]; for (int i = 1; i <= n; ++i) { final double[] xOriginal = original[i].getPointRef(); final double[] xTransformed = new double[n]; for (int j = 0; j < n; ++j) { xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]); } simplex[i] = new RealPointValuePair(xTransformed, Double.NaN, false); } // evaluate it evaluateSimplex(comparator); return simplex[0]; }
Example #12
Source File: SimplexSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testEpsilon() throws OptimizationException { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 10, 5, 1 }, 0); Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); constraints.add(new LinearConstraint(new double[] { 9, 8, 0 }, Relationship.EQ, 17)); constraints.add(new LinearConstraint(new double[] { 0, 7, 8 }, Relationship.LEQ, 7)); constraints.add(new LinearConstraint(new double[] { 10, 0, 2 }, Relationship.LEQ, 10)); SimplexSolver solver = new SimplexSolver(); RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertEquals(1.0, solution.getPoint()[0], 0.0); Assert.assertEquals(1.0, solution.getPoint()[1], 0.0); Assert.assertEquals(0.0, solution.getPoint()[2], 0.0); Assert.assertEquals(15.0, solution.getValue(), 0.0); }
Example #13
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testOneSet() throws FunctionEvaluationException, OptimizationException { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0, 0 }, { -1, 1, 0 }, { 0, -1, 1 } }, new double[] { 1, 1, 1}); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); VectorialPointValuePair optimum = optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); assertEquals(0, optimizer.getRMS(), 1.0e-10); assertEquals(1.0, optimum.getPoint()[0], 1.0e-10); assertEquals(2.0, optimum.getPoint()[1], 1.0e-10); assertEquals(3.0, optimum.getPoint()[2], 1.0e-10); }
Example #14
Source File: NonLinearConjugateGradientOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testCircleFitting() throws FunctionEvaluationException, OptimizationException { Circle circle = new Circle(); 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); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-30, 1.0e-30)); BrentSolver solver = new BrentSolver(); solver.setAbsoluteAccuracy(1.0e-13); solver.setRelativeAccuracy(1.0e-15); optimizer.setLineSearchSolver(solver); RealPointValuePair optimum = optimizer.optimize(circle, GoalType.MINIMIZE, new double[] { 98.680, 47.345 }); Point2D.Double center = new Point2D.Double(optimum.getPointRef()[0], optimum.getPointRef()[1]); assertEquals(69.960161753, circle.getRadius(center), 1.0e-8); assertEquals(96.075902096, center.x, 1.0e-8); assertEquals(48.135167894, center.y, 1.0e-8); }
Example #15
Source File: Cardumen_0064_t.java From coming with MIT License | 6 votes |
/** Compute and evaluate a new simplex. * @param original original simplex (to be preserved) * @param coeff linear coefficient * @param comparator comparator to use to sort simplex vertices from best to poorest * @return best point in the transformed simplex * @exception FunctionEvaluationException if the function cannot be evaluated at * some point * @exception OptimizationException if the maximal number of evaluations is exceeded */ private RealPointValuePair evaluateNewSimplex(final RealPointValuePair[] original, final double coeff, final Comparator<RealPointValuePair> comparator) throws FunctionEvaluationException, OptimizationException { final double[] xSmallest = original[0].getPointRef(); final int n = xSmallest.length; // create the linearly transformed simplex simplex = new RealPointValuePair[n + 1]; simplex[0] = original[0]; for (int i = 1; i <= n; ++i) { final double[] xOriginal = original[i].getPointRef(); final double[] xTransformed = new double[n]; for (int j = 0; j < n; ++j) { xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]); } simplex[i] = new RealPointValuePair(xTransformed, Double.NaN, false); } // evaluate it evaluateSimplex(comparator); return simplex[0]; }
Example #16
Source File: SimplexSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testMath272() throws OptimizationException { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 2, 1 }, 0); Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); constraints.add(new LinearConstraint(new double[] { 1, 1, 0 }, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] { 1, 0, 1 }, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0 }, Relationship.GEQ, 1)); SimplexSolver solver = new SimplexSolver(); RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true); Assert.assertEquals(0.0, solution.getPoint()[0], .0000001); Assert.assertEquals(1.0, solution.getPoint()[1], .0000001); Assert.assertEquals(1.0, solution.getPoint()[2], .0000001); Assert.assertEquals(3.0, solution.getValue(), .0000001); }
Example #17
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testOneSet() throws FunctionEvaluationException, OptimizationException { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0, 0 }, { -1, 1, 0 }, { 0, -1, 1 } }, new double[] { 1, 1, 1}); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); VectorialPointValuePair optimum = optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); assertEquals(0, optimizer.getRMS(), 1.0e-10); assertEquals(1.0, optimum.getPoint()[0], 1.0e-10); assertEquals(2.0, optimum.getPoint()[1], 1.0e-10); assertEquals(3.0, optimum.getPoint()[2], 1.0e-10); }
Example #18
Source File: CurveFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testMath303() throws OptimizationException, FunctionEvaluationException { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); CurveFitter fitter = new CurveFitter(optimizer); fitter.addObservedPoint(2.805d, 0.6934785852953367d); fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d); fitter.addObservedPoint(1.655d, 0.9474675497289684); fitter.addObservedPoint(1.725d, 0.9013594835804194d); ParametricRealFunction sif = new SimpleInverseFunction(); double[] initialguess1 = new double[1]; initialguess1[0] = 1.0d; Assert.assertEquals(1, fitter.fit(sif, initialguess1).length); double[] initialguess2 = new double[2]; initialguess2[0] = 1.0d; initialguess2[1] = .5d; Assert.assertEquals(2, fitter.fit(sif, initialguess2).length); }
Example #19
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testTwoSets() throws FunctionEvaluationException, OptimizationException { 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}); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); VectorialPointValuePair optimum = optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1, 1, 1, 1 }, new double[] { 0, 0, 0, 0, 0, 0 }); assertEquals(0, optimizer.getRMS(), 1.0e-10); assertEquals( 3.0, optimum.getPoint()[0], 1.0e-10); assertEquals( 4.0, optimum.getPoint()[1], 1.0e-10); assertEquals(-1.0, optimum.getPoint()[2], 1.0e-10); assertEquals(-2.0, optimum.getPoint()[3], 1.0e-10); assertEquals( 1.0 + epsilon, optimum.getPoint()[4], 1.0e-10); assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10); }
Example #20
Source File: SimplexSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testEpsilon() throws OptimizationException { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 10, 5, 1 }, 0); Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); constraints.add(new LinearConstraint(new double[] { 9, 8, 0 }, Relationship.EQ, 17)); constraints.add(new LinearConstraint(new double[] { 0, 7, 8 }, Relationship.LEQ, 7)); constraints.add(new LinearConstraint(new double[] { 10, 0, 2 }, Relationship.LEQ, 10)); SimplexSolver solver = new SimplexSolver(); RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertEquals(1.0, solution.getPoint()[0], 0.0); Assert.assertEquals(1.0, solution.getPoint()[1], 0.0); Assert.assertEquals(0.0, solution.getPoint()[2], 0.0); Assert.assertEquals(15.0, solution.getValue(), 0.0); }
Example #21
Source File: Cardumen_0063_s.java From coming with MIT License | 6 votes |
/** * Runs one iteration of the Simplex method on the given model. * @param tableau simple tableau for the problem * @throws OptimizationException if the maximal iteration count has been * exceeded or if the model is found not to have a bounded solution */ protected void doIteration(final SimplexTableau tableau) throws OptimizationException { incrementIterationsCounter(); Integer pivotCol = getPivotColumn(tableau); Integer pivotRow = getPivotRow(pivotCol, tableau); if (pivotRow == null) { throw new UnboundedSolutionException(); } // set the pivot element to 1 double pivotVal = tableau.getEntry(pivotRow, pivotCol); tableau.divideRow(pivotRow, pivotVal); // set the rest of the pivot column to 0 for (int i = 0; i < tableau.getHeight(); i++) { if (i != pivotRow) { double multiplier = tableau.getEntry(i, pivotCol); tableau.subtractRow(i, pivotRow, multiplier); } } }
Example #22
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testCircleFitting() throws FunctionEvaluationException, OptimizationException { Circle circle = new Circle(); 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); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-13, 1.0e-13)); VectorialPointValuePair optimum = optimizer.optimize(circle, new double[] { 0, 0, 0, 0, 0 }, new double[] { 1, 1, 1, 1, 1 }, new double[] { 98.680, 47.345 }); assertEquals(1.768262623567235, Math.sqrt(circle.getN()) * optimizer.getRMS(), 1.0e-10); Point2D.Double center = new Point2D.Double(optimum.getPointRef()[0], optimum.getPointRef()[1]); assertEquals(69.96016175359975, circle.getRadius(center), 1.0e-10); assertEquals(96.07590209601095, center.x, 1.0e-10); assertEquals(48.135167894714, center.y, 1.0e-10); }
Example #23
Source File: AbstractLeastSquaresOptimizer.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Guess the errors in optimized parameters. * <p>Guessing is covariance-based, it only gives rough order of magnitude.</p> * @return errors in optimized parameters * @exception FunctionEvaluationException if the function jacobian cannot b evaluated * @exception OptimizationException if the covariances matrix cannot be computed * or the number of degrees of freedom is not positive (number of measurements * lesser or equal to number of parameters) */ public double[] guessParametersErrors() throws FunctionEvaluationException, OptimizationException { if (rows <= cols) { throw new OptimizationException( "no degrees of freedom ({0} measurements, {1} parameters)", rows, cols); } double[] errors = new double[cols]; final double c = Math.sqrt(getChiSquare() / (rows - cols)); double[][] covar = getCovariances(); for (int i = 0; i < errors.length; ++i) { errors[i] = Math.sqrt(covar[i][i]) * c; } return errors; }
Example #24
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMoreEstimatedParametersUnsorted() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0 }, { 0.0, 0.0, -1.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, -1.0, 1.0, 0.0 } }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1, 1, 1 }, new double[] { 2, 2, 2, 2, 2, 2 }); fail("an exception should have been caught"); } catch (OptimizationException ee) { // expected behavior } catch (Exception e) { fail("wrong exception type caught"); } }
Example #25
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testNonInversible() throws FunctionEvaluationException, OptimizationException { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); assertTrue(Math.sqrt(problem.target.length) * optimizer.getRMS() > 0.6); try { optimizer.getCovariances(); fail("an exception should have been thrown"); } catch (OptimizationException ee) { // expected behavior } catch (Exception e) { fail("wrong exception caught"); } }
Example #26
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMaxIterations() { Circle circle = new Circle(); 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); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleVectorialPointChecker(1.0e-30, 1.0e-30)); try { optimizer.optimize(circle, new double[] { 0, 0, 0, 0, 0 }, new double[] { 1, 1, 1, 1, 1 }, new double[] { 98.680, 47.345 }); fail("an exception should have been caught"); } catch (OptimizationException ee) { // expected behavior } catch (Exception e) { fail("wrong exception type caught"); } }
Example #27
Source File: SimplexSolverTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testMath272() throws OptimizationException { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 2, 1 }, 0); Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); constraints.add(new LinearConstraint(new double[] { 1, 1, 0 }, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] { 1, 0, 1 }, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0 }, Relationship.GEQ, 1)); SimplexSolver solver = new SimplexSolver(); RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true); assertEquals(0.0, solution.getPoint()[0], .0000001); assertEquals(1.0, solution.getPoint()[1], .0000001); assertEquals(1.0, solution.getPoint()[2], .0000001); assertEquals(3.0, solution.getValue(), .0000001); }
Example #28
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testSmallError() throws OptimizationException { Random randomizer = new Random(53882150042l); double maxError = 0; for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } PolynomialFunction fitted = fitter.fit(); for (double x = -1.0; x < 1.0; x += 0.01) { double error = Math.abs(p.value(x) - fitted.value(x)) / (1.0 + Math.abs(p.value(x))); maxError = Math.max(maxError, error); assertTrue(Math.abs(error) < 0.1); } } assertTrue(maxError > 0.01); }
Example #29
Source File: NonLinearConjugateGradientOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testInconsistentEquations() throws FunctionEvaluationException, OptimizationException { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 4.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE); optimizer.setMaxIterations(100); optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6)); RealPointValuePair optimum = optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 1, 1 }); assertTrue(optimum.getValue() > 0.1); }
Example #30
Source File: HarmonicFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNoError() throws OptimizationException { HarmonicFunction f = new HarmonicFunction(0.2, 3.4, 4.1); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 1.3; x += 0.01) { fitter.addObservedPoint(1.0, x, f.value(x)); } HarmonicFunction fitted = fitter.fit(); assertEquals(f.getAmplitude(), fitted.getAmplitude(), 1.0e-13); assertEquals(f.getPulsation(), fitted.getPulsation(), 1.0e-13); assertEquals(f.getPhase(), MathUtils.normalizeAngle(fitted.getPhase(), f.getPhase()), 1.0e-13); for (double x = -1.0; x < 1.0; x += 0.01) { assertTrue(Math.abs(f.value(x) - fitted.value(x)) < 1.0e-13); } }