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

The following examples show how to use org.apache.commons.math3.optimization.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 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 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 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 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 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 MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                            fLineTol, 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 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 MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                            fLineTol, 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 5
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 6
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 7
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 MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                            fLineTol, 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 8
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 9
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 10
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 11
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 MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                            fLineTol, 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 12
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 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(f, constraints, GoalType.MAXIMIZE, 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(f, constraints, GoalType.MAXIMIZE, 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
@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(f, constraints, GoalType.MAXIMIZE, 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(f, constraints, GoalType.MAXIMIZE, true);
  Assert.assertEquals(40.57143, solution2.getValue(), .0001);
}
 
Example 15
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 16
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(f, constraints, GoalType.MAXIMIZE, 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(f, constraints, GoalType.MAXIMIZE, true);
  Assert.assertEquals(40.57143, solution2.getValue(), .0001);
}
 
Example 17
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 18
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 19
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(f, constraints, GoalType.MAXIMIZE, 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(f, constraints, GoalType.MAXIMIZE, true);
  Assert.assertEquals(40.57143, solution2.getValue(), .0001);
}
 
Example 20
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;
}