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

The following examples show how to use org.apache.commons.math.optimization.RealPointValuePair. 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: JGenProg2017_0039_t.java    From coming with MIT License 6 votes vote down vote up
/** Compute and evaluate a new simplex.
 * @param original original simplex (to be preserved)
 * @param coeff linear coefficient
 * @param comparator comparator to use to sort simplex vertices from best to poorest
 * @return best point in the transformed simplex
 * @exception FunctionEvaluationException if the function cannot be evaluated at
 * some point
 * @exception OptimizationException if the maximal number of evaluations is exceeded
 */
private RealPointValuePair evaluateNewSimplex(final RealPointValuePair[] original,
                                          final double coeff,
                                          final Comparator<RealPointValuePair> comparator)
    throws FunctionEvaluationException, OptimizationException {

    final double[] xSmallest = original[0].getPointRef();
    final int n = xSmallest.length;

    // create the linearly transformed simplex
    simplex = new RealPointValuePair[n + 1];
    simplex[0] = original[0];
    for (int i = 1; i <= n; ++i) {
        final double[] xOriginal    = original[i].getPointRef();
        final double[] xTransformed = new double[n];
        for (int j = 0; j < n; ++j) {
            xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
        }
        simplex[i] = new RealPointValuePair(xTransformed, Double.NaN, false);
    }

    // evaluate it
    evaluateSimplex(comparator);
    return simplex[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(MultivariateRealFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double pointTol) {
    final MultivariateRealOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));

    final RealPointValuePair result = optim.optimize(1000, func, goal, init);
    final double[] found = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals(optimum[i], found[i], pointTol);
    }
}
 
Example #3
Source File: NelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRosenbrock()
  throws FunctionEvaluationException, ConvergenceException {

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

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

}
 
Example #4
Source File: MultiDirectional.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Compute and evaluate a new simplex.
 * @param original original simplex (to be preserved)
 * @param coeff linear coefficient
 * @param comparator comparator to use to sort simplex vertices from best to poorest
 * @return best point in the transformed simplex
 * @exception FunctionEvaluationException if the function cannot be evaluated at
 * some point
 * @exception OptimizationException if the maximal number of evaluations is exceeded
 */
private RealPointValuePair evaluateNewSimplex(final RealPointValuePair[] original,
                                          final double coeff,
                                          final Comparator<RealPointValuePair> comparator)
    throws FunctionEvaluationException, OptimizationException {

    final double[] xSmallest = original[0].getPointRef();
    final int n = xSmallest.length;

    // create the linearly transformed simplex
    simplex = new RealPointValuePair[n + 1];
    simplex[0] = original[0];
    for (int i = 1; i <= n; ++i) {
        final double[] xOriginal    = original[i].getPointRef();
        final double[] xTransformed = new double[n];
        for (int j = 0; j < n; ++j) {
            xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
        }
        simplex[i] = new RealPointValuePair(xTransformed, Double.NaN, false);
    }

    // evaluate it
    evaluateSimplex(comparator);
    return simplex[0];

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

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

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

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

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

    return doOptimize();

}
 
Example #7
Source File: NPEfix_00130_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Get the current solution.
 * 
 * @return current solution
 */
protected RealPointValuePair getSolution() {
  double[] coefficients = new double[getOriginalNumDecisionVariables()];
  Integer negativeVarBasicRow = getBasicRowForSolution(getNegativeDecisionVariableOffset());
  double mostNegative = negativeVarBasicRow == null ? 0 : getEntry(negativeVarBasicRow, getRhsOffset());
  Set<Integer> basicRows = new HashSet<Integer>();
  for (int i = 0; i < coefficients.length; i++) {
      Integer basicRow = getBasicRowForSolution(getNumObjectiveFunctions() + i);
      if (basicRows.contains(basicRow)) {
          // if multiple variables can take a given value 
          // then we choose the first and set the rest equal to 0
          coefficients[i] = 0;
      } else {
          basicRows.add(basicRow);
          coefficients[i] =
              (basicRow == null ? 0 : getEntry(basicRow, getRhsOffset())) -
              (restrictToNonNegative ? 0 : mostNegative);
      }
  }
    return new RealPointValuePair(coefficients, f.getValue(coefficients));
}
 
Example #8
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testMoreEstimatedParametersSimple()
    throws FunctionEvaluationException, OptimizationException {

    LinearProblem problem = new LinearProblem(new double[][] {
            { 3.0, 2.0,  0.0, 0.0 },
            { 0.0, 1.0, -1.0, 1.0 },
            { 2.0, 0.0,  1.0, 0.0 }
    }, new double[] { 7.0, 3.0, 5.0 });

    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair optimum =
        optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 7, 6, 5, 4 });
    assertEquals(0, optimum.getValue(), 1.0e-10);

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

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

}
 
Example #10
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNoDependency() throws FunctionEvaluationException, OptimizationException {
    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);
    optimizer.setMaxEvaluations(100);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair optimum =
        optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0, 0 });
    for (int i = 0; i < problem.target.length; ++i) {
        assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10);
    }
}
 
Example #11
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

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

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

    final double EPSILON = 1e-5;

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

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

}
 
Example #12
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);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair 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 #13
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
  public void testEpsilon() throws OptimizationException {
    LinearObjectiveFunction f =
        new LinearObjectiveFunction(new double[] { 10, 5, 1 }, 0);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] {  9, 8, 0 }, Relationship.EQ,  17));
    constraints.add(new LinearConstraint(new double[] {  0, 7, 8 }, Relationship.LEQ,  7));
    constraints.add(new LinearConstraint(new double[] { 10, 0, 2 }, Relationship.LEQ, 10));

    SimplexSolver solver = new SimplexSolver();
    RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false);
    Assert.assertEquals(1.0, solution.getPoint()[0], 0.0);
    Assert.assertEquals(1.0, solution.getPoint()[1], 0.0);
    Assert.assertEquals(0.0, solution.getPoint()[2], 0.0);
    Assert.assertEquals(15.0, solution.getValue(), 0.0);
}
 
Example #14
Source File: NelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRosenbrock()
  throws FunctionEvaluationException, ConvergenceException {

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

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

}
 
Example #15
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNoDependency() throws FunctionEvaluationException, OptimizationException {
    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);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair optimum =
        optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0, 0 });
    for (int i = 0; i < problem.target.length; ++i) {
        assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10);
    }
}
 
Example #16
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testPowell() {
    MultivariateRealFunction powell =
        new MultivariateRealFunction() {
            public double value(double[] x) {
                ++count;
                double a = x[0] + 10 * x[1];
                double b = x[2] - x[3];
                double c = x[1] - 2 * x[2];
                double d = x[0] - x[3];
                return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
            }
        };

    count = 0;
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
    optimizer.setSimplex(new MultiDirectionalSimplex(4));
    RealPointValuePair optimum =
        optimizer.optimize(1000, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 });
    Assert.assertEquals(count, optimizer.getEvaluations());
    Assert.assertTrue(optimizer.getEvaluations() > 800);
    Assert.assertTrue(optimizer.getEvaluations() < 900);
    Assert.assertTrue(optimum.getValue() > 1e-2);
}
 
Example #17
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testMoreEstimatedParametersUnsorted()
    throws FunctionEvaluationException, OptimizationException {
    LinearProblem problem = new LinearProblem(new double[][] {
             { 1.0, 1.0,  0.0,  0.0, 0.0,  0.0 },
             { 0.0, 0.0,  1.0,  1.0, 1.0,  0.0 },
             { 0.0, 0.0,  0.0,  0.0, 1.0, -1.0 },
             { 0.0, 0.0, -1.0,  1.0, 0.0,  1.0 },
             { 0.0, 0.0,  0.0, -1.0, 1.0,  0.0 }
    }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 });
    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
    optimizer.setMaxEvaluations(100);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair optimum =
        optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 2, 2, 2, 2, 2, 2 });
    assertEquals(0, optimum.getValue(), 1.0e-10);
}
 
Example #18
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testColumnsPermutation() {
    LinearProblem problem =
        new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 } },
                          new double[] { 4.0, 6.0, 1.0 });

    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair optimum =
        optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0 });
    Assert.assertEquals(7.0, optimum.getPoint()[0], 1.0e-10);
    Assert.assertEquals(3.0, optimum.getPoint()[1], 1.0e-10);
    Assert.assertEquals(0.0, optimum.getValue(), 1.0e-10);

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

        LinearProblem problem = new LinearProblem(new double[][] {
                {  1,  0, 0 },
                { -1,  1, 0 },
                {  0, -1, 1 }
        }, new double[] { 1, 1, 1});
        NonLinearConjugateGradientOptimizer optimizer =
            new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
        optimizer.setMaxIterations(100);
        optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
        RealPointValuePair optimum =
            optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 });
        assertEquals(1.0, optimum.getPoint()[0], 1.0e-10);
        assertEquals(2.0, optimum.getPoint()[1], 1.0e-10);
        assertEquals(3.0, optimum.getPoint()[2], 1.0e-10);

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

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

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

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

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

}
 
Example #22
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testInconsistentEquations() throws FunctionEvaluationException, OptimizationException {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1.0,  1.0 },
            { 1.0, -1.0 },
            { 1.0,  3.0 }
    }, new double[] { 3.0, 1.0, 4.0 });

    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair optimum =
        optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 1, 1 });
    assertTrue(optimum.getValue() > 0.1);

}
 
Example #23
Source File: jMutRepair_0044_t.java    From coming with MIT License 6 votes vote down vote up
/** Compute and evaluate a new simplex.
 * @param original original simplex (to be preserved)
 * @param coeff linear coefficient
 * @param comparator comparator to use to sort simplex vertices from best to poorest
 * @return best point in the transformed simplex
 * @exception FunctionEvaluationException if the function cannot be evaluated at
 * some point
 * @exception OptimizationException if the maximal number of evaluations is exceeded
 */
private RealPointValuePair evaluateNewSimplex(final RealPointValuePair[] original,
                                          final double coeff,
                                          final Comparator<RealPointValuePair> comparator)
    throws FunctionEvaluationException, OptimizationException {

    final double[] xSmallest = original[0].getPointRef();
    final int n = xSmallest.length;

    // create the linearly transformed simplex
    simplex = new RealPointValuePair[n + 1];
    simplex[0] = original[0];
    for (int i = 1; i <= n; ++i) {
        final double[] xOriginal    = original[i].getPointRef();
        final double[] xTransformed = new double[n];
        for (int j = 0; j < n; ++j) {
            xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
        }
        simplex[i] = new RealPointValuePair(xTransformed, Double.NaN, false);
    }

    // evaluate it
    evaluateSimplex(comparator);
    return simplex[0];

}
 
Example #24
Source File: NPEfix12_twelve_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Get the current solution.
 *
 * @return current solution
 */
protected RealPointValuePair getSolution() {
    double[] coefficients = new double[getOriginalNumDecisionVariables()];
    Integer negativeVarBasicRow = getBasicRowForSolution(getNegativeDecisionVariableOffset());
    double mostNegative = negativeVarBasicRow == null ? 0 : getEntry(negativeVarBasicRow, getRhsOffset());
    Set<Integer> basicRows = new HashSet<Integer>();
    for (int i = 0; i < coefficients.length; i++) {
        Integer basicRow = getBasicRowForSolution(getNumObjectiveFunctions() + i);
        if (basicRows.contains(basicRow)) {
            // if multiple variables can take a given value
            // then we choose the first and set the rest equal to 0
            coefficients[i] = 0;
        } else {
            basicRows.add(basicRow);
            coefficients[i] =
                    (basicRow == null ? 0 : getEntry(basicRow, getRhsOffset())) -
                            (restrictToNonNegative ? 0 : mostNegative);
        }
    }
    return new RealPointValuePair(coefficients, f.getValue(coefficients));
}
 
Example #25
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
  public void testEpsilon() throws OptimizationException {
    LinearObjectiveFunction f =
        new LinearObjectiveFunction(new double[] { 10, 5, 1 }, 0);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] {  9, 8, 0 }, Relationship.EQ,  17));
    constraints.add(new LinearConstraint(new double[] {  0, 7, 8 }, Relationship.LEQ,  7));
    constraints.add(new LinearConstraint(new double[] { 10, 0, 2 }, Relationship.LEQ, 10));

    SimplexSolver solver = new SimplexSolver();
    RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false);
    Assert.assertEquals(1.0, solution.getPoint()[0], 0.0);
    Assert.assertEquals(1.0, solution.getPoint()[1], 0.0);
    Assert.assertEquals(0.0, solution.getPoint()[2], 0.0);
    Assert.assertEquals(15.0, solution.getValue(), 0.0);
}
 
Example #26
Source File: AbstractScalarDifferentiableOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
                                     final GoalType goalType,
                                     final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException, IllegalArgumentException {

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

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

    return doOptimize();

}
 
Example #27
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath272() throws OptimizationException {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 2, 1 }, 0);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 1, 0 }, Relationship.GEQ,  1));
    constraints.add(new LinearConstraint(new double[] { 1, 0, 1 }, Relationship.GEQ,  1));
    constraints.add(new LinearConstraint(new double[] { 0, 1, 0 }, Relationship.GEQ,  1));

    SimplexSolver solver = new SimplexSolver();
    RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true);
    
    assertEquals(0.0, solution.getPoint()[0], .0000001);
    assertEquals(1.0, solution.getPoint()[1], .0000001);
    assertEquals(1.0, solution.getPoint()[2], .0000001);
    assertEquals(3.0, solution.getValue(), .0000001);
}
 
Example #28
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Evaluate all the non-evaluated points of the simplex.
 * @param comparator comparator to use to sort simplex vertices from best to worst
 * @exception FunctionEvaluationException if no value can be computed for the parameters
 * @exception OptimizationException if the maximal number of evaluations is exceeded
 */
protected void evaluateSimplex(final Comparator<RealPointValuePair> comparator)
    throws FunctionEvaluationException, OptimizationException {

    // evaluate the objective function at all non-evaluated simplex points
    for (int i = 0; i < simplex.length; ++i) {
        final RealPointValuePair vertex = simplex[i];
        final double[] point = vertex.getPointRef();
        if (Double.isNaN(vertex.getValue())) {
            simplex[i] = new RealPointValuePair(point, evaluate(point), false);
        }
    }

    // sort the simplex from best to worst
    Arrays.sort(simplex, comparator);

}
 
Example #29
Source File: AbstractLinearOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final LinearObjectiveFunction f,
                                   final Collection<LinearConstraint> constraints,
                                   final GoalType goalType, final boolean restrictToNonNegative)
     throws OptimizationException {

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

    iterations  = 0;

    // solve the problem
    return doOptimize();

}
 
Example #30
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOneSet() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1,  0, 0 },
            { -1,  1, 0 },
            {  0, -1, 1 }
    }, new double[] { 1, 1, 1});
    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
    RealPointValuePair optimum =
        optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 });
    Assert.assertEquals(1.0, optimum.getPoint()[0], 1.0e-10);
    Assert.assertEquals(2.0, optimum.getPoint()[1], 1.0e-10);
    Assert.assertEquals(3.0, optimum.getPoint()[2], 1.0e-10);

}