本文整理汇总了Java中org.apache.commons.math3.random.JDKRandomGenerator类的典型用法代码示例。如果您正苦于以下问题:Java JDKRandomGenerator类的具体用法?Java JDKRandomGenerator怎么用?Java JDKRandomGenerator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JDKRandomGenerator类属于org.apache.commons.math3.random包,在下文中一共展示了JDKRandomGenerator类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadConfig
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
public void loadConfig(String filename, String username) throws FileNotFoundException {
ItemConfigs[] loadedConfigSettings = ItemConfigs.build(filename);
for (ItemConfigs itemConfigs : loadedConfigSettings) {
if (itemConfigs.getUsers().contains(username)) {
for (ItemConfig itemConfig : itemConfigs.getConfig()) {
itemsCount.put(itemConfig.getName(), Integer.parseInt(itemConfig.getCount()));
for (Relationship relationship : itemConfig.getRelationships()) {
JDKRandomGenerator jdkRandomGenerator = new JDKRandomGenerator();
jdkRandomGenerator.setSeed(filename.hashCode());
LinkedHashMap<Integer, Integer> relationConfig = new LinkedHashMap<>();
for (Percent percents : relationship.getPercent()) {
relationConfig.put(percents.getVertex(), percents.getPercentage());
System.out.println(percents.getVertex() + " / " + percents.getPercentage());
}
if (relationship.isUnique()) {
uniqueRelations.add(relationship.getName());
}
relDesc.put(relationship.getName(), new PercentageDistro(jdkRandomGenerator, relationConfig));
}
}
}
}
}
示例2: testNaNsFixedTiesRandom
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
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, 3, 6, 7, 3, 8, Double.NaN, 1, 2 };
TestUtils.assertEquals(correctRanks, ranks, 0d);
ranks = ranking.rank(tiesFirst);
correctRanks = new double[] { 1, 2, 4, 3, 5 };
TestUtils.assertEquals(correctRanks, ranks, 0d);
ranks = ranking.rank(tiesLast);
correctRanks = new double[] { 3, 3, 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, 4, 4, 6, 7, 1 };
TestUtils.assertEquals(correctRanks, ranks, 0d);
ranks = ranking.rank(allSame);
correctRanks = new double[] { 2, 3, 3, 3 };
TestUtils.assertEquals(correctRanks, ranks, 0d);
}
示例3: testStoredVsDirect
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
public void testStoredVsDirect() {
final RandomGenerator rand= new JDKRandomGenerator();
rand.setSeed(Long.MAX_VALUE);
for (final int sampleSize:sampleSizes) {
final double[] data = new NormalDistribution(rand,4000, 50)
.sample(sampleSize);
for (final double p:new double[] {50d,95d}) {
for (final Percentile.EstimationType e : Percentile.EstimationType.values()) {
reset(p, e);
final Percentile pStoredData = getUnivariateStatistic();
pStoredData.setData(data);
final double storedDataResult=pStoredData.evaluate();
pStoredData.setData(null);
final Percentile pDirect = getUnivariateStatistic();
Assert.assertEquals("Sample="+sampleSize+",P="+p+" e="+e,
storedDataResult,
pDirect.evaluate(data),0d);
}
}
}
}
示例4: testSinMin
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
public void testSinMin() {
UnivariateFunction f = new Sin();
UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
JDKRandomGenerator g = new JDKRandomGenerator();
g.setSeed(44428400075l);
UnivariateMultiStartOptimizer<UnivariateFunction> optimizer =
new UnivariateMultiStartOptimizer<UnivariateFunction>(underlying, 10, g);
optimizer.optimize(300, f, GoalType.MINIMIZE, -100.0, 100.0);
UnivariatePointValuePair[] optima = optimizer.getOptima();
for (int i = 1; i < optima.length; ++i) {
double d = (optima[i].getPoint() - optima[i-1].getPoint()) / (2 * FastMath.PI);
Assert.assertTrue(FastMath.abs(d - FastMath.rint(d)) < 1.0e-8);
Assert.assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10);
Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10);
}
Assert.assertTrue(optimizer.getEvaluations() > 200);
Assert.assertTrue(optimizer.getEvaluations() < 300);
}
示例5: testQuinticMin
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
public void testQuinticMin() {
// The quintic function has zeros at 0, +-0.5 and +-1.
// The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
UnivariateFunction f = new QuinticFunction();
UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
JDKRandomGenerator g = new JDKRandomGenerator();
g.setSeed(4312000053L);
UnivariateMultiStartOptimizer<UnivariateFunction> optimizer =
new UnivariateMultiStartOptimizer<UnivariateFunction>(underlying, 5, g);
UnivariatePointValuePair optimum
= optimizer.optimize(300, f, GoalType.MINIMIZE, -0.3, -0.2);
Assert.assertEquals(-0.2719561293, optimum.getPoint(), 1e-9);
Assert.assertEquals(-0.0443342695, optimum.getValue(), 1e-9);
UnivariatePointValuePair[] optima = optimizer.getOptima();
for (int i = 0; i < optima.length; ++i) {
Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1e-9);
}
Assert.assertTrue(optimizer.getEvaluations() >= 50);
Assert.assertTrue(optimizer.getEvaluations() <= 100);
}
示例6: testBadFunction
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
public void testBadFunction() {
UnivariateFunction f = new UnivariateFunction() {
public double value(double x) {
if (x < 0) {
throw new LocalException();
}
return 0;
}
};
UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
JDKRandomGenerator g = new JDKRandomGenerator();
g.setSeed(4312000053L);
UnivariateMultiStartOptimizer<UnivariateFunction> optimizer =
new UnivariateMultiStartOptimizer<UnivariateFunction>(underlying, 5, g);
try {
optimizer.optimize(300, f, GoalType.MINIMIZE, -0.3, -0.2);
Assert.fail();
} catch (LocalException e) {
// Expected.
}
// Ensure that the exception was thrown because no optimum was found.
Assert.assertTrue(optimizer.getOptima()[0] == null);
}
示例7: testRosenbrock
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
public void testRosenbrock() {
Rosenbrock rosenbrock = new Rosenbrock();
SimplexOptimizer underlying
= new SimplexOptimizer(new SimpleValueChecker(-1, 1.0e-3));
NelderMeadSimplex simplex = new NelderMeadSimplex(new double[][] {
{ -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 }
});
underlying.setSimplex(simplex);
JDKRandomGenerator g = new JDKRandomGenerator();
g.setSeed(16069223052l);
RandomVectorGenerator generator =
new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
MultivariateMultiStartOptimizer optimizer =
new MultivariateMultiStartOptimizer(underlying, 10, generator);
PointValuePair optimum =
optimizer.optimize(1100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 });
Assert.assertEquals(rosenbrock.getCount(), optimizer.getEvaluations());
Assert.assertTrue(optimizer.getEvaluations() > 900);
Assert.assertTrue(optimizer.getEvaluations() < 1200);
Assert.assertTrue(optimum.getValue() < 8.0e-4);
}
示例8: testSinMin
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
public void testSinMin() {
UnivariateFunction f = new Sin();
UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
JDKRandomGenerator g = new JDKRandomGenerator();
g.setSeed(44428400075l);
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g);
optimizer.optimize(new MaxEval(300),
new UnivariateObjectiveFunction(f),
GoalType.MINIMIZE,
new SearchInterval(-100.0, 100.0));
UnivariatePointValuePair[] optima = optimizer.getOptima();
for (int i = 1; i < optima.length; ++i) {
double d = (optima[i].getPoint() - optima[i-1].getPoint()) / (2 * FastMath.PI);
Assert.assertTrue(FastMath.abs(d - FastMath.rint(d)) < 1.0e-8);
Assert.assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10);
Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10);
}
Assert.assertTrue(optimizer.getEvaluations() > 200);
Assert.assertTrue(optimizer.getEvaluations() < 300);
}
示例9: testQuinticMin
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
public void testQuinticMin() {
// The quintic function has zeros at 0, +-0.5 and +-1.
// The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
UnivariateFunction f = new QuinticFunction();
UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14);
JDKRandomGenerator g = new JDKRandomGenerator();
g.setSeed(4312000053L);
MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g);
UnivariatePointValuePair optimum
= optimizer.optimize(new MaxEval(300),
new UnivariateObjectiveFunction(f),
GoalType.MINIMIZE,
new SearchInterval(-0.3, -0.2));
Assert.assertEquals(-0.27195613, optimum.getPoint(), 1e-9);
Assert.assertEquals(-0.0443342695, optimum.getValue(), 1e-9);
UnivariatePointValuePair[] optima = optimizer.getOptima();
for (int i = 0; i < optima.length; ++i) {
Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1e-9);
}
Assert.assertTrue(optimizer.getEvaluations() >= 50);
Assert.assertTrue(optimizer.getEvaluations() <= 100);
}
示例10: testNoOptimum
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
/**
* Test demonstrating that the user exception is finally thrown if none
* of the runs succeed.
*/
@Test(expected=TestException.class)
public void testNoOptimum() {
JacobianMultivariateVectorOptimizer underlyingOptimizer
= new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6));
JDKRandomGenerator g = new JDKRandomGenerator();
g.setSeed(12373523445l);
RandomVectorGenerator generator
= new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
MultiStartMultivariateVectorOptimizer optimizer
= new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator);
optimizer.optimize(new MaxEval(100),
new Target(new double[] { 0 }),
new Weight(new double[] { 1 }),
new InitialGuess(new double[] { 0 }),
new ModelFunction(new MultivariateVectorFunction() {
public double[] value(double[] point) {
throw new TestException();
}
}));
}
示例11: GenerateInitCentroids
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
private void GenerateInitCentroids() {
centroids = loadCentroids();
Random random = new Random(12397238947287L);
List<Point> initCentroids = new ArrayList<>();
RandomGenerator point_random = new JDKRandomGenerator();
for (Point centroid : centroids) {
MultivariateNormalDistribution distribution = new MultivariateNormalDistribution(point_random, means, covariances);
double[] point = distribution.sample();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dimension - 1; i++) {
point[i] += centroid.location[i];
sb.append(point[i]).append(", ");
}
point[dimension - 1] += centroid.location[dimension - 1];
sb.append(point[dimension - 1]);
System.out.println(sb.toString());
}
}
示例12: testMultivariate
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test public void testMultivariate() throws IncompleteSolutionException {
Objenome o = Objenome.solve(new OptimizeMultivariate(ExampleMultivariateFunction.class, new Function<ExampleMultivariateFunction, Double>() {
public Double apply(ExampleMultivariateFunction s) {
double v = s.output(0.0) + s.output(0.5) + s.output(1.0);
return v;
}
}) {
@Override protected RandomGenerator getRandomGenerator() {
JDKRandomGenerator j = new JDKRandomGenerator(); j.setSeed(0); return j;
}
} .minimize(), ExampleMultivariateFunction.class);
double bestParam = ((Number)o.getSolutions().get(1)).doubleValue();
assertEquals(-2.5919, bestParam, 0.001);
}
示例13: testNaNsFixedTiesRandom
import org.apache.commons.math3.random.JDKRandomGenerator; //导入依赖的package包/类
@Test
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);
}