org.apache.commons.math3.optim.MaxIter Java Examples

The following examples show how to use org.apache.commons.math3.optim.MaxIter. 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: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath930() {
    Collection<LinearConstraint> constraints = createMath930Constraints();
    
    double[] objFunctionCoeff = new double[33];
    objFunctionCoeff[3] = 1;
    LinearObjectiveFunction f = new LinearObjectiveFunction(objFunctionCoeff, 0);
    SimplexSolver solver = new SimplexSolver(1e-4, 10, 1e-6);
    
    PointValuePair solution = solver.optimize(new MaxIter(1000), f, new LinearConstraintSet(constraints),
                                              GoalType.MINIMIZE, new NonNegativeConstraint(true));
    Assert.assertEquals(0.3752298, solution.getValue(), 1e-4);
}
 
Example #2
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testSolutionCallback() {
    // re-use the problem from testcase for MATH-288
    // it normally requires 5 iterations
    
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 7, 3, 0, 0 }, 0 );

    List<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 3, 0, -5, 0 }, Relationship.LEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] { 2, 0, 0, -5 }, Relationship.LEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] { 0, 3, 0, -5 }, Relationship.LEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0 }, Relationship.LEQ, 1.0));
    constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 0 }, Relationship.LEQ, 1.0));

    final SimplexSolver solver = new SimplexSolver();
    final SolutionCallback callback = new SolutionCallback();
    
    Assert.assertNull(callback.getSolution());
    Assert.assertFalse(callback.isSolutionOptimal());

    try {
        solver.optimize(new MaxIter(3), f, new LinearConstraintSet(constraints),
                        GoalType.MAXIMIZE, new NonNegativeConstraint(true), callback);
        Assert.fail("expected TooManyIterationsException");
    } catch (TooManyIterationsException ex) {
        // expected
    }
    
    final PointValuePair solution = callback.getSolution();
    Assert.assertNotNull(solution);
    Assert.assertTrue(validSolution(solution, constraints, 1e-4));
    Assert.assertFalse(callback.isSolutionOptimal());
    // the solution is clearly not optimal: optimal = 10.0
    Assert.assertEquals(7.0, solution.getValue(), 1e-4);
}
 
Example #3
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath930() {
    Collection<LinearConstraint> constraints = createMath930Constraints();
    
    double[] objFunctionCoeff = new double[33];
    objFunctionCoeff[3] = 1;
    LinearObjectiveFunction f = new LinearObjectiveFunction(objFunctionCoeff, 0);
    SimplexSolver solver = new SimplexSolver(1e-4, 10, 1e-6);
    
    PointValuePair solution = solver.optimize(new MaxIter(1000), f, new LinearConstraintSet(constraints),
                                              GoalType.MINIMIZE, new NonNegativeConstraint(true));
    Assert.assertEquals(0.3752298, solution.getValue(), 1e-4);
}
 
Example #4
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testSolutionCallback() {
    // re-use the problem from testcase for MATH-288
    // it normally requires 5 iterations
    
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 7, 3, 0, 0 }, 0 );

    List<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 3, 0, -5, 0 }, Relationship.LEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] { 2, 0, 0, -5 }, Relationship.LEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] { 0, 3, 0, -5 }, Relationship.LEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0 }, Relationship.LEQ, 1.0));
    constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 0 }, Relationship.LEQ, 1.0));

    final SimplexSolver solver = new SimplexSolver();
    final SolutionCallback callback = new SolutionCallback();
    
    Assert.assertNull(callback.getSolution());
    Assert.assertFalse(callback.isSolutionOptimal());

    try {
        solver.optimize(new MaxIter(3), f, new LinearConstraintSet(constraints),
                        GoalType.MAXIMIZE, new NonNegativeConstraint(true), callback);
        Assert.fail("expected TooManyIterationsException");
    } catch (TooManyIterationsException ex) {
        // expected
    }
    
    final PointValuePair solution = callback.getSolution();
    Assert.assertNotNull(solution);
    Assert.assertTrue(validSolution(solution, constraints, 1e-4));
    Assert.assertFalse(callback.isSolutionOptimal());
    // the solution is clearly not optimal: optimal = 10.0
    Assert.assertEquals(7.0, solution.getValue(), 1e-4);
}
 
Example #5
Source File: Solver.java    From dataflow-java with Apache License 2.0 3 votes vote down vote up
/**
 * Maximizes a univariate function using a grid search followed by Brent's algorithm.
 *
 * @param fn the likelihood function to minimize
 * @param gridStart the lower bound for the grid search
 * @param gridEnd the upper bound for the grid search
 * @param gridStep step size for the grid search
 * @param relErr relative error tolerance for Brent's algorithm
 * @param absErr absolute error tolerance for Brent's algorithm
 * @param maxIter maximum # of iterations to perform in Brent's algorithm
 * @param maxEval maximum # of Likelihood function evaluations in Brent's algorithm
 *
 * @return the value of the parameter that maximizes the function
 */
public static double maximize(UnivariateFunction fn, double gridStart, double gridEnd,
    double gridStep, double relErr, double absErr, int maxIter, int maxEval) {
  Interval interval = gridSearch(fn, gridStart, gridEnd, gridStep);
  BrentOptimizer bo = new BrentOptimizer(relErr, absErr);
  UnivariatePointValuePair max = bo.optimize(
      new MaxIter(maxIter),
      new MaxEval(maxEval),
      new SearchInterval(interval.getInf(), interval.getSup()),
      new UnivariateObjectiveFunction(fn),
      GoalType.MAXIMIZE);
  return max.getPoint();
}