Java Code Examples for org.apache.commons.math3.optim.PointValuePair#getPoint()

The following examples show how to use org.apache.commons.math3.optim.PointValuePair#getPoint() . 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: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param fLineTol Tolerance (relative error on the objective function)
 * for the internal line search algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double fLineTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                      fLineTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }

    Assert.assertTrue(optim.getIterations() > 0);
}
 
Example 2
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }
}
 
Example 3
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }
}
 
Example 4
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param fLineTol Tolerance (relative error on the objective function)
 * for the internal line search algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double fLineTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                      fLineTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }

    Assert.assertTrue(optim.getIterations() > 0);
}
 
Example 5
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }
}
 
Example 6
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath293() {
  LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 );
  Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
  constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, 10.0));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, 10.0));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, 10.0));

  SimplexSolver solver = new SimplexSolver();
  PointValuePair solution1 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                             GoalType.MAXIMIZE, new NonNegativeConstraint(true));

  Assert.assertEquals(15.7143, solution1.getPoint()[0], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[1], .0001);
  Assert.assertEquals(14.2857, solution1.getPoint()[2], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[3], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[4], .0001);
  Assert.assertEquals(30.0, solution1.getPoint()[5], .0001);
  Assert.assertEquals(40.57143, solution1.getValue(), .0001);

  double valA = 0.8 * solution1.getPoint()[0] + 0.2 * solution1.getPoint()[1];
  double valB = 0.7 * solution1.getPoint()[2] + 0.3 * solution1.getPoint()[3];
  double valC = 0.4 * solution1.getPoint()[4] + 0.6 * solution1.getPoint()[5];

  f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 );
  constraints = new ArrayList<LinearConstraint>();
  constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, valA));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, valB));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, valC));

  PointValuePair solution2 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                             GoalType.MAXIMIZE, new NonNegativeConstraint(true));
  Assert.assertEquals(40.57143, solution2.getValue(), .0001);
}
 
Example 7
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 8
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 9
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath293() {
  LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 );
  Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
  constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, 10.0));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, 10.0));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, 10.0));

  SimplexSolver solver = new SimplexSolver();
  PointValuePair solution1 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                             GoalType.MAXIMIZE, new NonNegativeConstraint(true));

  Assert.assertEquals(15.7143, solution1.getPoint()[0], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[1], .0001);
  Assert.assertEquals(14.2857, solution1.getPoint()[2], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[3], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[4], .0001);
  Assert.assertEquals(30.0, solution1.getPoint()[5], .0001);
  Assert.assertEquals(40.57143, solution1.getValue(), .0001);

  double valA = 0.8 * solution1.getPoint()[0] + 0.2 * solution1.getPoint()[1];
  double valB = 0.7 * solution1.getPoint()[2] + 0.3 * solution1.getPoint()[3];
  double valC = 0.4 * solution1.getPoint()[4] + 0.6 * solution1.getPoint()[5];

  f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 );
  constraints = new ArrayList<LinearConstraint>();
  constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, valA));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, valB));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, valC));

  PointValuePair solution2 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                             GoalType.MAXIMIZE, new NonNegativeConstraint(true));
  Assert.assertEquals(40.57143, solution2.getValue(), .0001);
}
 
Example 10
Source File: LPChecks.java    From OSPREY3 with GNU General Public License v2.0 5 votes vote down vote up
public static double[] getFeasiblePt(ArrayList<LinearConstraint> polytope){
    //assuming polytope has a feasible pt, find one
    if(polytope.isEmpty())
        return new double[0];
    SimplexSolver ss = new SimplexSolver();
    LinearObjectiveFunction of = constrAsFunction(polytope.get(0));//obj func doesn't matter
    PointValuePair ans = ss.optimize(of, new LinearConstraintSet(polytope), GoalType.MINIMIZE);
    return ans.getPoint();
}
 
Example 11
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 12
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath293() {
  LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 );
  Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
  constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, 10.0));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, 10.0));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, 10.0));

  SimplexSolver solver = new SimplexSolver();
  PointValuePair solution1 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                             GoalType.MAXIMIZE, new NonNegativeConstraint(true));

  Assert.assertEquals(15.7143, solution1.getPoint()[0], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[1], .0001);
  Assert.assertEquals(14.2857, solution1.getPoint()[2], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[3], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[4], .0001);
  Assert.assertEquals(30.0, solution1.getPoint()[5], .0001);
  Assert.assertEquals(40.57143, solution1.getValue(), .0001);

  double valA = 0.8 * solution1.getPoint()[0] + 0.2 * solution1.getPoint()[1];
  double valB = 0.7 * solution1.getPoint()[2] + 0.3 * solution1.getPoint()[3];
  double valC = 0.4 * solution1.getPoint()[4] + 0.6 * solution1.getPoint()[5];

  f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 );
  constraints = new ArrayList<LinearConstraint>();
  constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, valA));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, valB));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, valC));

  PointValuePair solution2 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                             GoalType.MAXIMIZE, new NonNegativeConstraint(true));
  Assert.assertEquals(40.57143, solution2.getValue(), .0001);
}
 
Example 13
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath293() {
  LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 );
  Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
  constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, 10.0));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, 10.0));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, 10.0));

  SimplexSolver solver = new SimplexSolver();
  PointValuePair solution1 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                             GoalType.MAXIMIZE, new NonNegativeConstraint(true));

  Assert.assertEquals(15.7143, solution1.getPoint()[0], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[1], .0001);
  Assert.assertEquals(14.2857, solution1.getPoint()[2], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[3], .0001);
  Assert.assertEquals(0.0, solution1.getPoint()[4], .0001);
  Assert.assertEquals(30.0, solution1.getPoint()[5], .0001);
  Assert.assertEquals(40.57143, solution1.getValue(), .0001);

  double valA = 0.8 * solution1.getPoint()[0] + 0.2 * solution1.getPoint()[1];
  double valB = 0.7 * solution1.getPoint()[2] + 0.3 * solution1.getPoint()[3];
  double valC = 0.4 * solution1.getPoint()[4] + 0.6 * solution1.getPoint()[5];

  f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 );
  constraints = new ArrayList<LinearConstraint>();
  constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0));
  constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, valA));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, valB));
  constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, valC));

  PointValuePair solution2 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                             GoalType.MAXIMIZE, new NonNegativeConstraint(true));
  Assert.assertEquals(40.57143, solution2.getValue(), .0001);
}
 
Example 14
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static boolean validSolution(PointValuePair solution, List<LinearConstraint> constraints, double epsilon) {
    double[] vals = solution.getPoint();
    for (LinearConstraint c : constraints) {
        double[] coeffs = c.getCoefficients().toArray();
        double result = 0.0d;
        for (int i = 0; i < vals.length; i++) {
            result += vals[i] * coeffs[i];
        }
        
        switch (c.getRelationship()) {
        case EQ:
            if (!Precision.equals(result, c.getValue(), epsilon)) {
                return false;
            }
            break;
            
        case GEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) < 0) {
                return false;
            }
            break;
            
        case LEQ:
            if (Precision.compareTo(result, c.getValue(), epsilon) > 0) {
                return false;
            }
            break;
        }
    }
    
    return true;
}
 
Example 15
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Cf. MATH-867
 */
@Test
public void testFitAccuracyDependsOnBoundary() {
    final CMAESOptimizer optimizer
        = new CMAESOptimizer(30000, 0, true, 10,
                             0, new MersenneTwister(), false, null);
    final MultivariateFunction fitnessFunction = new MultivariateFunction() {
            public double value(double[] parameters) {
                final double target = 11.1;
                final double error = target - parameters[0];
                return error * error;
            }
        };

    final double[] start = { 1 };
 
    // No bounds.
    PointValuePair result = optimizer.optimize(new MaxEval(100000),
                                               new ObjectiveFunction(fitnessFunction),
                                               GoalType.MINIMIZE,
                                               SimpleBounds.unbounded(1),
                                               new CMAESOptimizer.PopulationSize(5),
                                               new CMAESOptimizer.Sigma(new double[] { 1e-1 }),
                                               new InitialGuess(start));
    final double resNoBound = result.getPoint()[0];

    // Optimum is near the lower bound.
    final double[] lower = { -20 };
    final double[] upper = { 5e16 };
    final double[] sigma = { 10 };
    result = optimizer.optimize(new MaxEval(100000),
                                new ObjectiveFunction(fitnessFunction),
                                GoalType.MINIMIZE,
                                new CMAESOptimizer.PopulationSize(5),
                                new CMAESOptimizer.Sigma(sigma),
                                new InitialGuess(start),
                                new SimpleBounds(lower, upper));
    final double resNearLo = result.getPoint()[0];

    // Optimum is near the upper bound.
    lower[0] = -5e16;
    upper[0] = 20;
    result = optimizer.optimize(new MaxEval(100000),
                                new ObjectiveFunction(fitnessFunction),
                                GoalType.MINIMIZE,
                                new CMAESOptimizer.PopulationSize(5),
                                new CMAESOptimizer.Sigma(sigma),
                                new InitialGuess(start),
                                new SimpleBounds(lower, upper));
    final double resNearHi = result.getPoint()[0];

    // System.out.println("resNoBound=" + resNoBound +
    //                    " resNearLo=" + resNearLo +
    //                    " resNearHi=" + resNearHi);

    // The two values currently differ by a substantial amount, indicating that
    // the bounds definition can prevent reaching the optimum.
    Assert.assertEquals(resNoBound, resNearLo, 1e-3);
    Assert.assertEquals(resNoBound, resNearHi, 1e-3);
}
 
Example 16
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@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),
                                                  1e-15, 1e-15, 1);
    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-3);
    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 }));

    final double[] result2 = optimum2.getPoint();
    final double[] expected2 = {-81, 137, -34, 22};

    Assert.assertEquals(expected2[0], result2[0], 2);
    Assert.assertEquals(expected2[1], result2[1], 4);
    Assert.assertEquals(expected2[2], result2[2], 1);
    Assert.assertEquals(expected2[3], result2[3], 1);
}
 
Example 17
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Cf. MATH-867
 */
@Test
public void testFitAccuracyDependsOnBoundary() {
    final CMAESOptimizer optimizer
        = new CMAESOptimizer(30000, 0, true, 10,
                             0, new MersenneTwister(), false, null);
    final MultivariateFunction fitnessFunction = new MultivariateFunction() {
            public double value(double[] parameters) {
                final double target = 11.1;
                final double error = target - parameters[0];
                return error * error;
            }
        };

    final double[] start = { 1 };
 
    // No bounds.
    PointValuePair result = optimizer.optimize(new MaxEval(100000),
                                               new ObjectiveFunction(fitnessFunction),
                                               GoalType.MINIMIZE,
                                               SimpleBounds.unbounded(1),
                                               new CMAESOptimizer.PopulationSize(5),
                                               new CMAESOptimizer.Sigma(new double[] { 1e-1 }),
                                               new InitialGuess(start));
    final double resNoBound = result.getPoint()[0];

    // Optimum is near the lower bound.
    final double[] lower = { -20 };
    final double[] upper = { 5e16 };
    final double[] sigma = { 10 };
    result = optimizer.optimize(new MaxEval(100000),
                                new ObjectiveFunction(fitnessFunction),
                                GoalType.MINIMIZE,
                                new CMAESOptimizer.PopulationSize(5),
                                new CMAESOptimizer.Sigma(sigma),
                                new InitialGuess(start),
                                new SimpleBounds(lower, upper));
    final double resNearLo = result.getPoint()[0];

    // Optimum is near the upper bound.
    lower[0] = -5e16;
    upper[0] = 20;
    result = optimizer.optimize(new MaxEval(100000),
                                new ObjectiveFunction(fitnessFunction),
                                GoalType.MINIMIZE,
                                new CMAESOptimizer.PopulationSize(5),
                                new CMAESOptimizer.Sigma(sigma),
                                new InitialGuess(start),
                                new SimpleBounds(lower, upper));
    final double resNearHi = result.getPoint()[0];

    // System.out.println("resNoBound=" + resNoBound +
    //                    " resNearLo=" + resNearLo +
    //                    " resNearHi=" + resNearHi);

    // The two values currently differ by a substantial amount, indicating that
    // the bounds definition can prevent reaching the optimum.
    Assert.assertEquals(resNoBound, resNearLo, 1e-3);
    Assert.assertEquals(resNoBound, resNearHi, 1e-3);
}
 
Example 18
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Cf. MATH-867
 */
@Test
public void testFitAccuracyDependsOnBoundary() {
    final CMAESOptimizer optimizer
        = new CMAESOptimizer(30000, 0, true, 10,
                             0, new MersenneTwister(), false, null);
    final MultivariateFunction fitnessFunction = new MultivariateFunction() {
            public double value(double[] parameters) {
                final double target = 11.1;
                final double error = target - parameters[0];
                return error * error;
            }
        };

    final double[] start = { 1 };
 
    // No bounds.
    PointValuePair result = optimizer.optimize(new MaxEval(100000),
                                               new ObjectiveFunction(fitnessFunction),
                                               GoalType.MINIMIZE,
                                               SimpleBounds.unbounded(1),
                                               new CMAESOptimizer.PopulationSize(5),
                                               new CMAESOptimizer.Sigma(new double[] { 1e-1 }),
                                               new InitialGuess(start));
    final double resNoBound = result.getPoint()[0];

    // Optimum is near the lower bound.
    final double[] lower = { -20 };
    final double[] upper = { 5e16 };
    final double[] sigma = { 10 };
    result = optimizer.optimize(new MaxEval(100000),
                                new ObjectiveFunction(fitnessFunction),
                                GoalType.MINIMIZE,
                                new CMAESOptimizer.PopulationSize(5),
                                new CMAESOptimizer.Sigma(sigma),
                                new InitialGuess(start),
                                new SimpleBounds(lower, upper));
    final double resNearLo = result.getPoint()[0];

    // Optimum is near the upper bound.
    lower[0] = -5e16;
    upper[0] = 20;
    result = optimizer.optimize(new MaxEval(100000),
                                new ObjectiveFunction(fitnessFunction),
                                GoalType.MINIMIZE,
                                new CMAESOptimizer.PopulationSize(5),
                                new CMAESOptimizer.Sigma(sigma),
                                new InitialGuess(start),
                                new SimpleBounds(lower, upper));
    final double resNearHi = result.getPoint()[0];

    // System.out.println("resNoBound=" + resNoBound +
    //                    " resNearLo=" + resNearLo +
    //                    " resNearHi=" + resNearHi);

    // The two values currently differ by a substantial amount, indicating that
    // the bounds definition can prevent reaching the optimum.
    Assert.assertEquals(resNoBound, resNearLo, 1e-3);
    Assert.assertEquals(resNoBound, resNearHi, 1e-3);
}
 
Example 19
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@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),
                                                 1e-7, 1e-7, 1,
                                                 preconditioner);

    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 }));

    final double[] result = optimum.getPoint();
    final double[] expected = {3, 4, -1, -2, 1 + epsilon, 1 - epsilon};

    Assert.assertEquals(expected[0], result[0], 1.0e-7);
    Assert.assertEquals(expected[1], result[1], 1.0e-7);
    Assert.assertEquals(expected[2], result[2], 1.0e-9);
    Assert.assertEquals(expected[3], result[3], 1.0e-8);
    Assert.assertEquals(expected[4] + epsilon, result[4], 1.0e-6);
    Assert.assertEquals(expected[5] - epsilon, result[5], 1.0e-6);

}
 
Example 20
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@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),
                                                 1e-7, 1e-7, 1,
                                                 preconditioner);

    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 }));

    final double[] result = optimum.getPoint();
    final double[] expected = {3, 4, -1, -2, 1 + epsilon, 1 - epsilon};

    Assert.assertEquals(expected[0], result[0], 1.0e-7);
    Assert.assertEquals(expected[1], result[1], 1.0e-7);
    Assert.assertEquals(expected[2], result[2], 1.0e-9);
    Assert.assertEquals(expected[3], result[3], 1.0e-8);
    Assert.assertEquals(expected[4] + epsilon, result[4], 1.0e-6);
    Assert.assertEquals(expected[5] - epsilon, result[5], 1.0e-6);

}