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

The following examples show how to use org.apache.commons.math3.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: SimplexSolverTest.java    From astor with GNU General Public License v2.0 7 votes vote down vote up
@Test
public void testMath828Cycle() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(
            new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 0.0);
    
    ArrayList <LinearConstraint>constraints = new ArrayList<LinearConstraint>();

    constraints.add(new LinearConstraint(new double[] {0.0, 16.0, 14.0, 69.0, 1.0, 85.0, 52.0, 43.0, 64.0, 97.0, 14.0, 74.0, 89.0, 28.0, 94.0, 58.0, 13.0, 22.0, 21.0, 17.0, 30.0, 25.0, 1.0, 59.0, 91.0, 78.0, 12.0, 74.0, 56.0, 3.0, 88.0,}, Relationship.GEQ, 91.0));
    constraints.add(new LinearConstraint(new double[] {0.0, 60.0, 40.0, 81.0, 71.0, 72.0, 46.0, 45.0, 38.0, 48.0, 40.0, 17.0, 33.0, 85.0, 64.0, 32.0, 84.0, 3.0, 54.0, 44.0, 71.0, 67.0, 90.0, 95.0, 54.0, 99.0, 99.0, 29.0, 52.0, 98.0, 9.0,}, Relationship.GEQ, 54.0));
    constraints.add(new LinearConstraint(new double[] {0.0, 41.0, 12.0, 86.0, 90.0, 61.0, 31.0, 41.0, 23.0, 89.0, 17.0, 74.0, 44.0, 27.0, 16.0, 47.0, 80.0, 32.0, 11.0, 56.0, 68.0, 82.0, 11.0, 62.0, 62.0, 53.0, 39.0, 16.0, 48.0, 1.0, 63.0,}, Relationship.GEQ, 62.0));
    constraints.add(new LinearConstraint(new double[] {83.0, -76.0, -94.0, -19.0, -15.0, -70.0, -72.0, -57.0, -63.0, -65.0, -22.0, -94.0, -22.0, -88.0, -86.0, -89.0, -72.0, -16.0, -80.0, -49.0, -70.0, -93.0, -95.0, -17.0, -83.0, -97.0, -31.0, -47.0, -31.0, -13.0, -23.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {41.0, -96.0, -41.0, -48.0, -70.0, -43.0, -43.0, -43.0, -97.0, -37.0, -85.0, -70.0, -45.0, -67.0, -87.0, -69.0, -94.0, -54.0, -54.0, -92.0, -79.0, -10.0, -35.0, -20.0, -41.0, -41.0, -65.0, -25.0, -12.0, -8.0, -46.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {27.0, -42.0, -65.0, -49.0, -53.0, -42.0, -17.0, -2.0, -61.0, -31.0, -76.0, -47.0, -8.0, -93.0, -86.0, -62.0, -65.0, -63.0, -22.0, -43.0, -27.0, -23.0, -32.0, -74.0, -27.0, -63.0, -47.0, -78.0, -29.0, -95.0, -73.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {15.0, -46.0, -41.0, -83.0, -98.0, -99.0, -21.0, -35.0, -7.0, -14.0, -80.0, -63.0, -18.0, -42.0, -5.0, -34.0, -56.0, -70.0, -16.0, -18.0, -74.0, -61.0, -47.0, -41.0, -15.0, -79.0, -18.0, -47.0, -88.0, -68.0, -55.0,}, Relationship.GEQ, 0.0));
    
    double epsilon = 1e-6;
    PointValuePair solution = new SimplexSolver().optimize(f, constraints, GoalType.MINIMIZE, true);
    Assert.assertEquals(1.0d, solution.getValue(), epsilon);
    Assert.assertTrue(validSolution(solution, constraints, epsilon));        
}
 
Example #2
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 7 votes vote down vote up
@Test
public void testMath781() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 6, 7 }, 0);

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

    double epsilon = 1e-6;
    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false);

    Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], 0.0d, epsilon) > 0);
    Assert.assertTrue(Precision.compareTo(solution.getPoint()[1], 0.0d, epsilon) > 0);
    Assert.assertTrue(Precision.compareTo(solution.getPoint()[2], 0.0d, epsilon) < 0);
    Assert.assertEquals(2.0d, solution.getValue(), epsilon);
}
 
Example #3
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(1e-14, 1e-14);
    optimizer.setSimplex(new MultiDirectionalSimplex(2));
    final Gaussian2D function = new Gaussian2D(0, 0, 1);
    PointValuePair 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 #4
Source File: UnivariateMultiStartOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sort the optima from best to worst, followed by {@code null} elements.
 *
 * @param goal Goal type.
 */
private void sortPairs(final GoalType goal) {
    Arrays.sort(optima, new Comparator<UnivariatePointValuePair>() {
            public int compare(final UnivariatePointValuePair o1,
                               final UnivariatePointValuePair o2) {
                if (o1 == null) {
                    return (o2 == null) ? 0 : 1;
                } else if (o2 == null) {
                    return -1;
                }
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goal == GoalType.MINIMIZE) ?
                    Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        });
}
 
Example #5
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRosenbrock() {
    MultivariateFunction rosenbrock =
        new MultivariateFunction() {
            public double value(double[] x) {
                ++count;
                double a = x[1] - x[0] * x[0];
                double b = 1.0 - x[0];
                return 100 * a * a + b * b;
            }
        };

    count = 0;
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
    optimizer.setSimplex(new MultiDirectionalSimplex(new double[][] {
                { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
            }));
    PointValuePair optimum =
        optimizer.optimize(100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1 });

    Assert.assertEquals(count, optimizer.getEvaluations());
    Assert.assertTrue(optimizer.getEvaluations() > 50);
    Assert.assertTrue(optimizer.getEvaluations() < 100);
    Assert.assertTrue(optimum.getValue() > 1e-2);
}
 
Example #6
Source File: UnivariateMultiStartOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testBadFunction() {
    UnivariateFunction f = new UnivariateFunction() {
            public double value(double x) {
                if (x < 0) {
                    throw new LocalException();
                }
                return 0;
            }
        };
    UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(4312000053L);
    UnivariateMultiStartOptimizer<UnivariateFunction> optimizer =
        new UnivariateMultiStartOptimizer<UnivariateFunction>(underlying, 5, g);
 
    try {
        optimizer.optimize(300, f, GoalType.MINIMIZE, -0.3, -0.2);
        Assert.fail();
    } catch (LocalException e) {
        // Expected.
    }

    // Ensure that the exception was thrown because no optimum was found.
    Assert.assertTrue(optimizer.getOptima()[0] == null);
}
 
Example #7
Source File: UnivariateMultiStartOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testBadFunction() {
    UnivariateFunction f = new UnivariateFunction() {
            public double value(double x) {
                if (x < 0) {
                    throw new LocalException();
                }
                return 0;
            }
        };
    UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(4312000053L);
    UnivariateMultiStartOptimizer<UnivariateFunction> optimizer =
        new UnivariateMultiStartOptimizer<UnivariateFunction>(underlying, 5, g);
 
    try {
        optimizer.optimize(300, f, GoalType.MINIMIZE, -0.3, -0.2);
        Assert.fail();
    } catch (LocalException e) {
        // Expected.
    }

    // Ensure that the exception was thrown because no optimum was found.
    Assert.assertTrue(optimizer.getOptima()[0] == null);
}
 
Example #8
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath828() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(
            new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 0.0);
    
    ArrayList <LinearConstraint>constraints = new ArrayList<LinearConstraint>();

    constraints.add(new LinearConstraint(new double[] {0.0, 39.0, 23.0, 96.0, 15.0, 48.0, 9.0, 21.0, 48.0, 36.0, 76.0, 19.0, 88.0, 17.0, 16.0, 36.0,}, Relationship.GEQ, 15.0));
    constraints.add(new LinearConstraint(new double[] {0.0, 59.0, 93.0, 12.0, 29.0, 78.0, 73.0, 87.0, 32.0, 70.0, 68.0, 24.0, 11.0, 26.0, 65.0, 25.0,}, Relationship.GEQ, 29.0));
    constraints.add(new LinearConstraint(new double[] {0.0, 74.0, 5.0, 82.0, 6.0, 97.0, 55.0, 44.0, 52.0, 54.0, 5.0, 93.0, 91.0, 8.0, 20.0, 97.0,}, Relationship.GEQ, 6.0));
    constraints.add(new LinearConstraint(new double[] {8.0, -3.0, -28.0, -72.0, -8.0, -31.0, -31.0, -74.0, -47.0, -59.0, -24.0, -57.0, -56.0, -16.0, -92.0, -59.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {25.0, -7.0, -99.0, -78.0, -25.0, -14.0, -16.0, -89.0, -39.0, -56.0, -53.0, -9.0, -18.0, -26.0, -11.0, -61.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {33.0, -95.0, -15.0, -4.0, -33.0, -3.0, -20.0, -96.0, -27.0, -13.0, -80.0, -24.0, -3.0, -13.0, -57.0, -76.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {7.0, -95.0, -39.0, -93.0, -7.0, -94.0, -94.0, -62.0, -76.0, -26.0, -53.0, -57.0, -31.0, -76.0, -53.0, -52.0,}, Relationship.GEQ, 0.0));
    
    double epsilon = 1e-6;
    PointValuePair solution = new SimplexSolver().optimize(f, constraints, GoalType.MINIMIZE, true);
    Assert.assertEquals(1.0d, solution.getValue(), epsilon);
    Assert.assertTrue(validSolution(solution, constraints, epsilon));
}
 
Example #9
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares3() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #10
Source File: BOBYQAOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Ignore @Test
public void testConstrainedRosenWithMoreInterpolationPoints() {
    final double[] startPoint = point(DIM, 0.1);
    final double[][] boundaries = boundaries(DIM, -1, 2);
    final PointValuePair expected = new PointValuePair(point(DIM, 1.0), 0.0);

    // This should have been 78 because in the code the hard limit is
    // said to be
    //   ((DIM + 1) * (DIM + 2)) / 2 - (2 * DIM + 1)
    // i.e. 78 in this case, but the test fails for 48, 59, 62, 63, 64,
    // 65, 66, ...
    final int maxAdditionalPoints = 47;

    for (int num = 1; num <= maxAdditionalPoints; num++) {
        doTest(new Rosen(), startPoint, boundaries,
               GoalType.MINIMIZE,
               1e-12, 1e-6, 2000,
               num,
               expected,
               "num=" + num);
    }
}
 
Example #11
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares3() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #12
Source File: Math_33_SimplexTableau_s.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 when checking for optimality
 * @param maxUlps 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,
               final int maxUlps) {
    this.f                      = f;
    this.constraints            = normalizeConstraints(constraints);
    this.restrictToNonNegative  = restrictToNonNegative;
    this.epsilon                = epsilon;
    this.maxUlps                = maxUlps;
    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 #13
Source File: MultivariateFunctionMappingAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOptimumOutsideRange() {

    final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionMappingAdapter wrapped =
            new MultivariateFunctionMappingAdapter(biQuadratic,
                                                       biQuadratic.getLower(),
                                                       biQuadratic.getUpper());

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    optimizer.setSimplex(new NelderMeadSimplex(new double[][] {
        wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
        wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
        wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
    }));

    final PointValuePair optimum
        = optimizer.optimize(100, wrapped, GoalType.MINIMIZE,
                             wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }));
    final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);

}
 
Example #14
Source File: UnivariateMultiStartOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() {
    UnivariateFunction f = new Sin();
    UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    UnivariateMultiStartOptimizer<UnivariateFunction> optimizer =
        new UnivariateMultiStartOptimizer<UnivariateFunction>(underlying, 10, g);
    optimizer.optimize(300, f, GoalType.MINIMIZE, -100.0, 100.0);
    UnivariatePointValuePair[] optima = optimizer.getOptima();
    for (int i = 1; i < optima.length; ++i) {
        double d = (optima[i].getPoint() - optima[i-1].getPoint()) / (2 * FastMath.PI);
        Assert.assertTrue(FastMath.abs(d - FastMath.rint(d)) < 1.0e-8);
        Assert.assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10);
        Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10);
    }
    Assert.assertTrue(optimizer.getEvaluations() > 200);
    Assert.assertTrue(optimizer.getEvaluations() < 300);
}
 
Example #15
Source File: MultivariateFunctionPenaltyAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOptimumOutsideRange() {

    final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionPenaltyAdapter wrapped =
            new MultivariateFunctionPenaltyAdapter(biQuadratic,
                                                       biQuadratic.getLower(),
                                                       biQuadratic.getUpper(),
                                                       1000.0, new double[] { 100.0, 100.0 });

    SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
    optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 }));

    final PointValuePair optimum
        = optimizer.optimize(600, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 });

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);

}
 
Example #16
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRedundantEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1.0,  1.0 },
            { 1.0, -1.0 },
            { 1.0,  3.0 }
    }, new double[] { 3.0, 1.0, 5.0 });

    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE,
                                                new SimpleValueChecker(1e-6, 1e-6));
    PointValuePair optimum =
        optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 1, 1 });
    Assert.assertEquals(2.0, optimum.getPoint()[0], 1.0e-8);
    Assert.assertEquals(1.0, optimum.getPoint()[1], 1.0e-8);

}
 
Example #17
Source File: BOBYQAOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param startPoint Starting point.
 * @param boundaries Upper / lower point limit.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance relative error on the objective function.
 * @param pointTol Tolerance for checking that the optimum is correct.
 * @param maxEvaluations Maximum number of evaluations.
 * @param expected Expected point / value.
 */
private void doTest(MultivariateFunction func,
                    double[] startPoint,
                    double[][] boundaries,
                    GoalType goal,
                    double fTol,
                    double pointTol,
                    int maxEvaluations,
                    PointValuePair expected) {
    doTest(func,
           startPoint,
           boundaries,
           goal,
           fTol,
           pointTol,
           maxEvaluations,
           0,
           expected,
           "");
}
 
Example #18
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleScalar circle = new CircleScalar();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);
    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE,
                                                new SimpleValueChecker(1e-30, 1e-30),
                                                new BrentSolver(1e-15, 1e-13));
    PointValuePair optimum =
        optimizer.optimize(100, circle, GoalType.MINIMIZE, new double[] { 98.680, 47.345 });
    Point2D.Double center = new Point2D.Double(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.960161753, circle.getRadius(center), 1.0e-8);
    Assert.assertEquals(96.075902096, center.x, 1.0e-8);
    Assert.assertEquals(48.135167894, center.y, 1.0e-8);
}
 
Example #19
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testPowell() {
    MultivariateFunction powell =
        new MultivariateFunction() {
            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));
    PointValuePair 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 #20
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath286() {
    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();
    PointValuePair 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 #21
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #22
Source File: BracketFinderTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMinimumIsOnIntervalBoundary() {
    final UnivariateFunction func = new UnivariateFunction() {
            public double value(double x) {
                return x * x;
            }
        };

    final BracketFinder bFind = new BracketFinder();

    bFind.search(func, GoalType.MINIMIZE, 0, 1);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());

    bFind.search(func, GoalType.MINIMIZE, -1, 0);
    Assert.assertTrue(bFind.getLo() <= 0);
    Assert.assertTrue(0 <= bFind.getHi());
}
 
Example #23
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 MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(1000, func, goal, 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 #24
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath864() {
    final CMAESOptimizer optimizer = new CMAESOptimizer();
    final MultivariateFunction fitnessFunction = new MultivariateFunction() {
            public double value(double[] parameters) {
                final double target = 1;
                final double error = target - parameters[0];
                return error * error;
            }
        };

    final double[] start = { 0 };
    final double[] lower = { -1e6 };
    final double[] upper = { 1.5 };
    final double[] result = optimizer.optimize(10000, fitnessFunction, GoalType.MINIMIZE,
                                               start, lower, upper).getPoint();
    Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")",
                      result[0] <= upper[0]);
}
 
Example #25
Source File: AbstractLinearOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public PointValuePair optimize(final LinearObjectiveFunction f,
                               final Collection<LinearConstraint> constraints,
                               final GoalType goalType, final boolean restrictToNonNegative)
    throws MathIllegalStateException {

    // store linear problem characteristics
    this.function          = f;
    this.linearConstraints = constraints;
    this.goal              = goalType;
    this.nonNegative       = restrictToNonNegative;

    iterations  = 0;

    // solve the problem
    return doOptimize();

}
 
Example #26
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInconsistentEquations() {
    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,
                                                new SimpleValueChecker(1e-6, 1e-6));
    PointValuePair optimum =
        optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 1, 1 });
    Assert.assertTrue(optimum.getValue() > 0.1);

}
 
Example #27
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 when checking for optimality
 * @param maxUlps 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,
               final int maxUlps) {
    this.f                      = f;
    this.constraints            = normalizeConstraints(constraints);
    this.restrictToNonNegative  = restrictToNonNegative;
    this.epsilon                = epsilon;
    this.maxUlps                = maxUlps;
    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 #28
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
  public void testEpsilon() {
    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();
    PointValuePair 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 #29
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoDependency() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 2, 0, 0, 0, 0, 0 },
            { 0, 2, 0, 0, 0, 0 },
            { 0, 0, 2, 0, 0, 0 },
            { 0, 0, 0, 2, 0, 0 },
            { 0, 0, 0, 0, 2, 0 },
            { 0, 0, 0, 0, 0, 2 }
    }, new double[] { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5 });
    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE,
                                                new SimpleValueChecker(1e-6, 1e-6));
    PointValuePair optimum =
        optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0, 0 });
    for (int i = 0; i < problem.target.length; ++i) {
        Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10);
    }
}
 
Example #30
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRestrictVariablesToNonNegative() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 409, 523, 70, 204, 339 }, 0);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] {    43,   56, 345,  56,    5 }, Relationship.LEQ,  4567456));
    constraints.add(new LinearConstraint(new double[] {    12,   45,   7,  56,   23 }, Relationship.LEQ,    56454));
    constraints.add(new LinearConstraint(new double[] {     8,  768,   0,  34, 7456 }, Relationship.LEQ,  1923421));
    constraints.add(new LinearConstraint(new double[] { 12342, 2342,  34, 678, 2342 }, Relationship.GEQ,     4356));
    constraints.add(new LinearConstraint(new double[] {    45,  678,  76,  52,   23 }, Relationship.EQ,    456356));

    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true);
    Assert.assertEquals(2902.92783505155, solution.getPoint()[0], .0000001);
    Assert.assertEquals(480.419243986254, solution.getPoint()[1], .0000001);
    Assert.assertEquals(0.0, solution.getPoint()[2], .0000001);
    Assert.assertEquals(0.0, solution.getPoint()[3], .0000001);
    Assert.assertEquals(0.0, solution.getPoint()[4], .0000001);
    Assert.assertEquals(1438556.7491409, solution.getValue(), .0000001);
}