org.apache.commons.math.optimization.GoalType Java Examples

The following examples show how to use org.apache.commons.math.optimization.GoalType. 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: NelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testPowell()
  throws FunctionEvaluationException, ConvergenceException {

  Powell powell = new Powell();
  NelderMead optimizer = new NelderMead();
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
  optimizer.setMaxIterations(200);
  RealPointValuePair optimum =
    optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
  assertEquals(powell.getCount(), optimizer.getEvaluations());
  assertTrue(optimizer.getEvaluations() > 110);
  assertTrue(optimizer.getEvaluations() < 130);
  assertTrue(optimum.getValue() < 2.0e-3);

}
 
Example #2
Source File: BracketFinderTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCubicMin() throws MathException {
    final BracketFinder bFind = new BracketFinder();
    final UnivariateRealFunction func = new UnivariateRealFunction() {
            public double value(double x)
                throws FunctionEvaluationException {
                if (x < -2) {
                    return value(-2);
                }
                else  {
                    return (x - 1) * (x + 2) * (x + 3);
                }
            }
        };

    bFind.search(func, GoalType.MINIMIZE, -2 , -1);
    final double tol = 1e-15;
    // Comparing with results computed in Python.
    Assert.assertEquals(-2, bFind.getLo(), tol);
    Assert.assertEquals(-1, bFind.getMid(), tol);
    Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol);
}
 
Example #3
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOneSet() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1,  0, 0 },
            { -1,  1, 0 },
            {  0, -1, 1 }
    }, new double[] { 1, 1, 1});
    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair optimum =
        optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 });
    Assert.assertEquals(1.0, optimum.getPoint()[0], 1.0e-10);
    Assert.assertEquals(2.0, optimum.getPoint()[1], 1.0e-10);
    Assert.assertEquals(3.0, optimum.getPoint()[2], 1.0e-10);

}
 
Example #4
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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 #5
Source File: SimplexTableauTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testTableauWithNoArtificialVars() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {15, 10}, 0);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2));
    constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3));
    constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.LEQ, 4));    
    SimplexTableau tableau =
        new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
    double[][] initialTableau = {
                                 {1, -15, -10, 25, 0, 0, 0, 0},
                                 {0,   1,   0, -1, 1, 0, 0, 2},
                                 {0,   0,   1, -1, 0, 1, 0, 3},
                                 {0,   1,   1, -2, 0, 0, 1, 4}
    };
    assertMatrixEquals(initialTableau, tableau.getData());
}
 
Example #6
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath286() throws OptimizationException {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.6, 0.4 }, 0 );
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 23.0));
    constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 23.0));
    constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0, 0, 0 }, Relationship.GEQ, 10.0));
    constraints.add(new LinearConstraint(new double[] { 0, 0, 1, 0, 0, 0 }, Relationship.GEQ, 8.0));
    constraints.add(new LinearConstraint(new double[] { 0, 0, 0, 0, 1, 0 }, Relationship.GEQ, 5.0));

    SimplexSolver solver = new SimplexSolver();
    RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true);

    Assert.assertEquals(25.8, solution.getValue(), .0000001);
    Assert.assertEquals(23.0, solution.getPoint()[0] + solution.getPoint()[2] + solution.getPoint()[4], 0.0000001);
    Assert.assertEquals(23.0, solution.getPoint()[1] + solution.getPoint()[3] + solution.getPoint()[5], 0.0000001);
    Assert.assertTrue(solution.getPoint()[0] >= 10.0 - 0.0000001);
    Assert.assertTrue(solution.getPoint()[2] >= 8.0 - 0.0000001);
    Assert.assertTrue(solution.getPoint()[4] >= 5.0 - 0.0000001);
}
 
Example #7
Source File: Math_88_SimplexTableau_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Build a tableau for a linear problem.
 * @param f linear objective function
 * @param constraints linear constraints
 * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
 * or {@link GoalType#MINIMIZE}
 * @param restrictToNonNegative whether to restrict the variables to non-negative values
 * @param epsilon amount of error to accept in floating point comparisons
 */
SimplexTableau(final LinearObjectiveFunction f,
               final Collection<LinearConstraint> constraints,
               final GoalType goalType, final boolean restrictToNonNegative,
               final double epsilon) {
    this.f                      = f;
    this.constraints            = constraints;
    this.restrictToNonNegative  = restrictToNonNegative;
    this.epsilon                = epsilon;
    this.numDecisionVariables   = getNumVariables() + (restrictToNonNegative ? 0 : 1);
    this.numSlackVariables      = getConstraintTypeCounts(Relationship.LEQ) +
                                  getConstraintTypeCounts(Relationship.GEQ);
    this.numArtificialVariables = getConstraintTypeCounts(Relationship.EQ) +
                                  getConstraintTypeCounts(Relationship.GEQ);
    this.tableau = new RealMatrixImpl(createTableau(goalType == GoalType.MAXIMIZE));
    initialize();
}
 
Example #8
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283() {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    SimplexOptimizer optimizer = new SimplexOptimizer();
    optimizer.setSimplex(new MultiDirectionalSimplex(2));
    final Gaussian2D function = new Gaussian2D(0, 0, 1);
    RealPointValuePair estimate = optimizer.optimize(1000, function,
                                                     GoalType.MAXIMIZE, function.getMaximumPosition());
    final double EPSILON = 1e-5;
    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );
}
 
Example #9
Source File: NelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1()
throws FunctionEvaluationException, ConvergenceException {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
            { 1.0, 0.0 },
            { 0.0, 1.0 }
        }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
        public double[] value(double[] variables) {
            return factors.operate(variables);
        }
    }, new double[] { 2.0, -3.0 });
    NelderMead optimizer = new NelderMead();
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
    optimizer.setMaxIterations(200);
    RealPointValuePair optimum =
        optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
    assertEquals( 2.0, optimum.getPointRef()[0], 3.0e-5);
    assertEquals(-3.0, optimum.getPointRef()[1], 4.0e-4);
    assertTrue(optimizer.getEvaluations() > 60);
    assertTrue(optimizer.getEvaluations() < 80);
    assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #10
Source File: NelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRosenbrock()
  throws FunctionEvaluationException, ConvergenceException {

  Rosenbrock rosenbrock = new Rosenbrock();
  NelderMead optimizer = new NelderMead();
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
  optimizer.setMaxIterations(100);
  optimizer.setStartConfiguration(new double[][] {
          { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
  });
  RealPointValuePair optimum =
      optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });

  assertEquals(rosenbrock.getCount(), optimizer.getEvaluations());
  assertTrue(optimizer.getEvaluations() > 40);
  assertTrue(optimizer.getEvaluations() < 50);
  assertTrue(optimum.getValue() < 8.0e-4);

}
 
Example #11
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2.0, -3.0 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    RealPointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #12
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );

}
 
Example #13
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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});
        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[] { 0, 0, 0 });
        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: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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 #15
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testMoreEstimatedParametersUnsorted()
    throws FunctionEvaluationException, OptimizationException {
    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 });
    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[] { 2, 2, 2, 2, 2, 2 });
    assertEquals(0, optimum.getValue(), 1.0e-10);
}
 
Example #16
Source File: AbstractLinearOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final LinearObjectiveFunction f,
                                   final Collection<LinearConstraint> constraints,
                                   final GoalType goalType, final boolean restrictToNonNegative)
     throws OptimizationException {

    // store linear problem characteristics
    this.f                     = f;
    this.constraints           = constraints;
    this.goalType              = goalType;
    this.restrictToNonNegative = restrictToNonNegative;

    iterations  = 0;

    // solve the problem
    return doOptimize();

}
 
Example #17
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testPowell() {
    MultivariateRealFunction powell =
        new MultivariateRealFunction() {
            public double value(double[] x) {
                ++count;
                double a = x[0] + 10 * x[1];
                double b = x[2] - x[3];
                double c = x[1] - 2 * x[2];
                double d = x[0] - x[3];
                return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
            }
        };

    count = 0;
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
    optimizer.setSimplex(new MultiDirectionalSimplex(4));
    RealPointValuePair optimum =
        optimizer.optimize(1000, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 });
    Assert.assertEquals(count, optimizer.getEvaluations());
    Assert.assertTrue(optimizer.getEvaluations() > 800);
    Assert.assertTrue(optimizer.getEvaluations() < 900);
    Assert.assertTrue(optimum.getValue() > 1e-2);
}
 
Example #18
Source File: PowellOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the minimum of the function {@code f(p + alpha * d)}.
 *
 * @param p Starting point.
 * @param d Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 * @throws org.apache.commons.math.exception.MathUserException if the
 * objective function throws one.
 */
public UnivariateRealPointValuePair search(final double[] p, final double[] d) {
    final int n = p.length;
    final UnivariateRealFunction f = new UnivariateRealFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = p[i] + alpha * d[i];
                }
                final double obj = PowellOptimizer.this.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = PowellOptimizer.this.getGoalType();
    bracket.search(f, goal, 0, 1);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return optimize(Integer.MAX_VALUE, f, goal,
                    bracket.getLo(), bracket.getHi(), bracket.getMid());
}
 
Example #19
Source File: NelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2()
throws FunctionEvaluationException, ConvergenceException {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
            { 1.0, 0.0 },
            { 0.0, 1.0 }
        }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
        public double[] value(double[] variables) {
            return factors.operate(variables);
        }
    }, new double[] { 2.0, -3.0 }, new double[] { 10.0, 0.1 });
    NelderMead optimizer = new NelderMead();
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
    optimizer.setMaxIterations(200);
    RealPointValuePair optimum =
        optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
    assertEquals( 2.0, optimum.getPointRef()[0], 5.0e-5);
    assertEquals(-3.0, optimum.getPointRef()[1], 8.0e-4);
    assertTrue(optimizer.getEvaluations() > 60);
    assertTrue(optimizer.getEvaluations() < 80);
    assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #20
Source File: NPEfix_00135_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Build a tableau for a linear problem.
 * @param f linear objective function
 * @param constraints linear constraints
 * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
 * or {@link GoalType#MINIMIZE}
 * @param restrictToNonNegative whether to restrict the variables to non-negative values
 * @param epsilon amount of error to accept in floating point comparisons
 */
SimplexTableau(final LinearObjectiveFunction f,
               final Collection<LinearConstraint> constraints,
               final GoalType goalType, final boolean restrictToNonNegative,
               final double epsilon) {
    this.f                      = f;
    this.constraints            = constraints;
    this.restrictToNonNegative  = restrictToNonNegative;
    this.epsilon                = epsilon;
    this.numDecisionVariables   = getNumVariables() + (restrictToNonNegative ? 0 : 1);
    this.numSlackVariables      = getConstraintTypeCounts(Relationship.LEQ) +
                                  getConstraintTypeCounts(Relationship.GEQ);
    this.numArtificialVariables = getConstraintTypeCounts(Relationship.EQ) +
                                  getConstraintTypeCounts(Relationship.GEQ);
    this.tableau = new Array2DRowRealMatrix(createTableau(goalType == GoalType.MAXIMIZE));
    initialize();
}
 
Example #21
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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 #22
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Build a tableau for a linear problem.
 * @param f linear objective function
 * @param constraints linear constraints
 * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
 * or {@link GoalType#MINIMIZE}
 * @param restrictToNonNegative whether to restrict the variables to non-negative values
 * @param epsilon amount of error to accept in floating point comparisons
 */
SimplexTableau(final LinearObjectiveFunction f,
               final Collection<LinearConstraint> constraints,
               final GoalType goalType, final boolean restrictToNonNegative,
               final double epsilon) {
    this.f                      = f;
    this.constraints            = normalizeConstraints(constraints);
    this.restrictToNonNegative  = restrictToNonNegative;
    this.epsilon                = epsilon;
    this.numDecisionVariables   = f.getCoefficients().getDimension() +
                                  (restrictToNonNegative ? 0 : 1);
    this.numSlackVariables      = getConstraintTypeCounts(Relationship.LEQ) +
                                  getConstraintTypeCounts(Relationship.GEQ);
    this.numArtificialVariables = getConstraintTypeCounts(Relationship.EQ) +
                                  getConstraintTypeCounts(Relationship.GEQ);
    this.tableau = createTableau(goalType == GoalType.MAXIMIZE);
    initializeColumnLabels();
}
 
Example #23
Source File: AbstractScalarDifferentiableOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
                                     final GoalType goalType,
                                     final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException, IllegalArgumentException {

    // reset counters
    iterations          = 0;
    evaluations         = 0;
    gradientEvaluations = 0;

    // store optimization problem characteristics
    this.f        = f;
    gradient      = f.gradient();
    this.goalType = goalType;
    point         = startPoint.clone();

    return doOptimize();

}
 
Example #24
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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 #25
Source File: AbstractScalarDifferentiableOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
                                     final GoalType goalType,
                                     final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException, IllegalArgumentException {

    // reset counters
    iterations          = 0;
    evaluations         = 0;
    gradientEvaluations = 0;

    // store optimization problem characteristics
    function = f;
    gradient = f.gradient();
    goal     = goalType;
    point    = startPoint.clone();

    return doOptimize();

}
 
Example #26
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testColumnsPermutation() throws FunctionEvaluationException, OptimizationException {

        LinearProblem problem =
            new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 } },
                              new double[] { 4.0, 6.0, 1.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[] { 0, 0 });
        assertEquals(7.0, optimum.getPoint()[0], 1.0e-10);
        assertEquals(3.0, optimum.getPoint()[1], 1.0e-10);
        assertEquals(0.0, optimum.getValue(), 1.0e-10);

    }
 
Example #27
Source File: NelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testPowell()
  throws FunctionEvaluationException, ConvergenceException {

  Powell powell = new Powell();
  NelderMead optimizer = new NelderMead();
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-3));
  optimizer.setMaxIterations(200);
  RealPointValuePair optimum =
    optimizer.optimize(powell, GoalType.MINIMIZE, new double[] { 3.0, -1.0, 0.0, 1.0 });
  assertEquals(powell.getCount(), optimizer.getEvaluations());
  assertTrue(optimizer.getEvaluations() > 110);
  assertTrue(optimizer.getEvaluations() < 130);
  assertTrue(optimum.getValue() < 2.0e-3);

}
 
Example #28
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDegeneracy() throws OptimizationException {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.7 }, 0 );
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.LEQ, 18.0));
    constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.GEQ, 10.0));
    constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 8.0));

    SimplexSolver solver = new SimplexSolver();
    RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true);
    Assert.assertEquals(13.6, solution.getValue(), .0000001);
}
 
Example #29
Source File: BrentOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testQuinticMinStatistics() {
    // The function has local minima at -0.27195613 and 0.82221643.
    UnivariateRealFunction f = new QuinticFunction();
    UnivariateRealOptimizer optimizer = new BrentOptimizer(1e-11, 1e-14);

    final DescriptiveStatistics[] stat = new DescriptiveStatistics[2];
    for (int i = 0; i < stat.length; i++) {
        stat[i] = new DescriptiveStatistics();
    }

    final double min = -0.75;
    final double max = 0.25;
    final int nSamples = 200;
    final double delta = (max - min) / nSamples;
    for (int i = 0; i < nSamples; i++) {
        final double start = min + i * delta;
        stat[0].addValue(optimizer.optimize(40, f, GoalType.MINIMIZE, min, max, start).getPoint());
        stat[1].addValue(optimizer.getEvaluations());
    }

    final double meanOptValue = stat[0].getMean();
    final double medianEval = stat[1].getPercentile(50);
    Assert.assertTrue(meanOptValue > -0.2719561281);
    Assert.assertTrue(meanOptValue < -0.2719561280);
    Assert.assertEquals(23, (int) medianEval);
}
 
Example #30
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEllipse() throws MathException {
    double[] startPoint = point(DIM,1.0);
    double[] insigma = point(DIM,0.1);
    double[][] boundaries = null;
    RealPointValuePair expected =
        new RealPointValuePair(point(DIM,0.0),0.0);
    doTest(new Elli(), startPoint, insigma, boundaries,
            GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
            1e-13, 1e-6, 100000, expected);
    doTest(new Elli(), startPoint, insigma, boundaries,
            GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
            1e-13, 1e-6, 100000, expected);
}