org.apache.commons.math.random.JDKRandomGenerator Java Examples

The following examples show how to use org.apache.commons.math.random.JDKRandomGenerator. 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: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #2
Source File: MultiStartMultivariateRealOptimizerTest.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 underlying = new NelderMead();
  underlying.setStartConfiguration(new double[][] {
                                       { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
                                   });
  JDKRandomGenerator g = new JDKRandomGenerator();
  g.setSeed(16069223052l);
  RandomVectorGenerator generator =
      new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
  MultiStartMultivariateRealOptimizer optimizer =
      new MultiStartMultivariateRealOptimizer(underlying, 10, generator);
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
  optimizer.setMaxIterations(100);
  RealPointValuePair optimum =
      optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });

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

}
 
Example #3
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example #4
Source File: MultiStartMultivariateRealOptimizerTest.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 underlying = new NelderMead();
  underlying.setStartConfiguration(new double[][] {
                                       { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
                                   });
  JDKRandomGenerator g = new JDKRandomGenerator();
  g.setSeed(16069223052l);
  RandomVectorGenerator generator =
      new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
  MultiStartMultivariateRealOptimizer optimizer =
      new MultiStartMultivariateRealOptimizer(underlying, 10, generator);
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
  optimizer.setMaxIterations(100);
  RealPointValuePair optimum =
      optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });

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

}
 
Example #5
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example #6
Source File: MultiStartUnivariateRealOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() throws MathException {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealOptimizer underlying = new BrentOptimizer();
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    MultiStartUnivariateRealOptimizer minimizer =
        new MultiStartUnivariateRealOptimizer(underlying, 10, g);
    minimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0);
    double[] optima = minimizer.getOptima();
    double[] optimaValues = minimizer.getOptimaValues();
    for (int i = 1; i < optima.length; ++i) {
        double d = (optima[i] - optima[i-1]) / (2 * Math.PI);
        assertTrue (Math.abs(d - Math.rint(d)) < 1.0e-8);
        assertEquals(-1.0, f.value(optima[i]), 1.0e-10);
        assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
    }
    assertTrue(minimizer.getEvaluations() > 2900);
    assertTrue(minimizer.getEvaluations() < 3100);
}
 
Example #7
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #8
Source File: MultiStartUnivariateRealOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() throws MathException {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealOptimizer underlying = new BrentOptimizer();
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    MultiStartUnivariateRealOptimizer minimizer =
        new MultiStartUnivariateRealOptimizer(underlying, 10, g);
    minimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0);
    double[] optima = minimizer.getOptima();
    double[] optimaValues = minimizer.getOptimaValues();
    for (int i = 1; i < optima.length; ++i) {
        double d = (optima[i] - optima[i-1]) / (2 * Math.PI);
        assertTrue (Math.abs(d - Math.rint(d)) < 1.0e-8);
        assertEquals(-1.0, f.value(optima[i]), 1.0e-10);
        assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
    }
    assertTrue(minimizer.getEvaluations() > 2900);
    assertTrue(minimizer.getEvaluations() < 3100);
}
 
Example #9
Source File: MultiStartMultivariateRealOptimizerTest.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 underlying = new NelderMead();
  underlying.setStartConfiguration(new double[][] {
                                       { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
                                   });
  JDKRandomGenerator g = new JDKRandomGenerator();
  g.setSeed(16069223052l);
  RandomVectorGenerator generator =
      new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
  MultiStartMultivariateRealOptimizer optimizer =
      new MultiStartMultivariateRealOptimizer(underlying, 10, generator);
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
  optimizer.setMaxIterations(100);
  RealPointValuePair optimum =
      optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });

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

}
 
Example #10
Source File: MultiStartUnivariateRealOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    MultiStartUnivariateRealOptimizer<UnivariateRealFunction> optimizer =
        new MultiStartUnivariateRealOptimizer<UnivariateRealFunction>(underlying, 10, g);
    optimizer.optimize(300, f, GoalType.MINIMIZE, -100.0, 100.0);
    UnivariateRealPointValuePair[] 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 #11
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example #12
Source File: MultiStartUnivariateRealOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() throws MathException {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealOptimizer underlying = new BrentOptimizer();
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    MultiStartUnivariateRealOptimizer minimizer =
        new MultiStartUnivariateRealOptimizer(underlying, 10, g);
    minimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0);
    double[] optima = minimizer.getOptima();
    double[] optimaValues = minimizer.getOptimaValues();
    for (int i = 1; i < optima.length; ++i) {
        double d = (optima[i] - optima[i-1]) / (2 * Math.PI);
        assertTrue (Math.abs(d - Math.rint(d)) < 1.0e-8);
        assertEquals(-1.0, f.value(optima[i]), 1.0e-10);
        assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
    }
    assertTrue(minimizer.getEvaluations() > 2900);
    assertTrue(minimizer.getEvaluations() < 3100);
}
 
Example #13
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #14
Source File: MultiStartUnivariateRealOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() throws MathException {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealOptimizer underlying = new BrentOptimizer();
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    MultiStartUnivariateRealOptimizer minimizer =
        new MultiStartUnivariateRealOptimizer(underlying, 10, g);
    minimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0);
    double[] optima = minimizer.getOptima();
    double[] optimaValues = minimizer.getOptimaValues();
    for (int i = 1; i < optima.length; ++i) {
        double d = (optima[i] - optima[i-1]) / (2 * Math.PI);
        assertTrue (Math.abs(d - Math.rint(d)) < 1.0e-8);
        assertEquals(-1.0, f.value(optima[i]), 1.0e-10);
        assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
    }
    assertTrue(minimizer.getEvaluations() > 2900);
    assertTrue(minimizer.getEvaluations() < 3100);
}
 
Example #15
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #16
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #17
Source File: MultiStartMultivariateRealOptimizerTest.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 underlying = new NelderMead();
  underlying.setStartConfiguration(new double[][] {
                                       { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
                                   });
  JDKRandomGenerator g = new JDKRandomGenerator();
  g.setSeed(16069223052l);
  RandomVectorGenerator generator =
      new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
  MultiStartMultivariateRealOptimizer optimizer =
      new MultiStartMultivariateRealOptimizer(underlying, 10, generator);
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
  optimizer.setMaxIterations(100);
  RealPointValuePair optimum =
      optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });

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

}
 
Example #18
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example #19
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #20
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #21
Source File: MultiStartMultivariateRealOptimizerTest.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 underlying = new NelderMead();
  underlying.setStartConfiguration(new double[][] {
                                       { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
                                   });
  JDKRandomGenerator g = new JDKRandomGenerator();
  g.setSeed(16069223052l);
  RandomVectorGenerator generator =
      new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
  MultiStartMultivariateRealOptimizer optimizer =
      new MultiStartMultivariateRealOptimizer(underlying, 10, generator);
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
  optimizer.setMaxIterations(100);
  RealPointValuePair optimum =
      optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });

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

}
 
Example #22
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example #23
Source File: MultiStartUnivariateRealOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() throws MathException {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealOptimizer underlying = new BrentOptimizer();
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    MultiStartUnivariateRealOptimizer minimizer =
        new MultiStartUnivariateRealOptimizer(underlying, 10, g);
    minimizer.optimize(f, GoalType.MINIMIZE, -100.0, 100.0);
    double[] optima = minimizer.getOptima();
    double[] optimaValues = minimizer.getOptimaValues();
    for (int i = 1; i < optima.length; ++i) {
        double d = (optima[i] - optima[i-1]) / (2 * Math.PI);
        assertTrue (Math.abs(d - Math.rint(d)) < 1.0e-8);
        assertEquals(-1.0, f.value(optima[i]), 1.0e-10);
        assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
    }
    assertTrue(minimizer.getEvaluations() > 2900);
    assertTrue(minimizer.getEvaluations() < 3100);
}
 
Example #24
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #25
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example #26
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example #27
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = OptimizationException.class)
public void testNoOptimum() throws FunctionEvaluationException, OptimizationException {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setMaxIterations(100);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) throws FunctionEvaluationException {
                throw new FunctionEvaluationException(point[0]);
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
 
Example #28
Source File: MultiStartMultivariateRealOptimizerTest.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 underlying = new NelderMead();
  underlying.setStartConfiguration(new double[][] {
                                       { -1.2,  1.0 }, { 0.9, 1.2 } , {  3.5, -2.3 }
                                   });
  JDKRandomGenerator g = new JDKRandomGenerator();
  g.setSeed(16069223052l);
  RandomVectorGenerator generator =
      new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
  MultiStartMultivariateRealOptimizer optimizer =
      new MultiStartMultivariateRealOptimizer(underlying, 10, generator);
  optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1, 1.0e-3));
  optimizer.setMaxIterations(100);
  RealPointValuePair optimum =
      optimizer.optimize(rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });

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

}
 
Example #29
Source File: NaturalRankingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testNaNsFixedTiesRandom() {
    RandomGenerator randomGenerator = new JDKRandomGenerator();
    randomGenerator.setSeed(1000);
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
            randomGenerator);
    double[] ranks = ranking.rank(exampleData);
    double[] correctRanks = { 5, 4, 6, 7, 3, 8, Double.NaN, 1, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesFirst);
    correctRanks = new double[] { 1, 1, 4, 3, 5 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(tiesLast);
    correctRanks = new double[] { 3, 4, 2, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleNaNs);
    correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(multipleTies);
    correctRanks = new double[] { 3, 2, 5, 5, 7, 6, 1 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
    ranks = ranking.rank(allSame);
    correctRanks = new double[] { 1, 3, 4, 4 };
    TestUtils.assertEquals(correctRanks, ranks, 0d);
}
 
Example #30
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected = MathUserException.class)
public void testNoOptimum() {
    DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
        new GaussNewtonOptimizer(true);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator =
        new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartDifferentiableMultivariateVectorialOptimizer optimizer =
        new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer,
                                                                   10, generator);
    optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6));
    optimizer.optimize(100, new DifferentiableMultivariateVectorialFunction() {
            public MultivariateMatrixFunction jacobian() {
                return null;
            }
            public double[] value(double[] point) {
                throw new MathUserException();
            }
        }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}