本文整理汇总了Java中org.apache.commons.math3.distribution.EnumeratedDistribution类的典型用法代码示例。如果您正苦于以下问题:Java EnumeratedDistribution类的具体用法?Java EnumeratedDistribution怎么用?Java EnumeratedDistribution使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EnumeratedDistribution类属于org.apache.commons.math3.distribution包,在下文中一共展示了EnumeratedDistribution类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getEnumerated
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
@Override
public <T> RandomDistribution<T> getEnumerated(
final RandomNumberStream rng,
final List<ProbabilityMass<T, Number>> probabilities)
{
final List<Pair<T, Double>> pmf = new ArrayList<>();
if (probabilities != null)
for (ProbabilityMass<T, Number> p : probabilities)
pmf.add(Pair.create(p.getValue(), p.getMass().doubleValue()));
final EnumeratedDistribution<T> dist = new EnumeratedDistribution<T>(
RandomNumberStream.Util.asCommonsRandomGenerator(rng), pmf);
return new RandomDistribution<T>()
{
@Override
public T draw()
{
return dist.sample();
}
};
}
示例2: getFactory
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public static DataGenFactory getFactory(File file) throws IOException
{
final List<Pair<byte[], Double>> words = new ArrayList<>();
try (final BufferedReader reader = new BufferedReader(new FileReader(file)))
{
String line;
while ( null != (line = reader.readLine()) )
{
String[] pair = line.split(" +");
if (pair.length != 2)
throw new IllegalArgumentException("Invalid record in dictionary: \"" + line + "\"");
words.add(new Pair<>(pair[1].getBytes(UTF_8), Double.parseDouble(pair[0])));
}
final EnumeratedDistribution<byte[]> dist = new EnumeratedDistribution<byte[]>(words);
return new DataGenFactory()
{
@Override
public DataGen get()
{
return new DataGenStringDictionary(dist);
}
};
}
}
示例3: selectEntities
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
@Override
public List<Pair<E, E>> selectEntities(TObjectDoubleMap<E> entityFitnessMap, int count,
RandomGenerator random) {
Preconditions.checkNotNull(entityFitnessMap);
Preconditions.checkNotNull(random);
EnumeratedDistribution<E> distribution = getFitnessDistribution(entityFitnessMap, random);
List<Pair<E, E>> selectedPairs = new ArrayList<>();
if (distribution != null) {
for (int cnt = 1; cnt <= count; cnt++) {
E parent1 = distribution.sample();
E parent2 = distribution.sample();
if (distribution.getPmf().size() > 1) {
// Ensure that parent1 != parent2.
while (parent2 == parent1) {
parent2 = distribution.sample();
}
}
selectedPairs.add(new Pair<>(parent1, parent2));
}
}
return selectedPairs;
}
示例4: getFitnessDistribution
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
private EnumeratedDistribution<E> getFitnessDistribution(
final TObjectDoubleMap<E> entityFitnessMap, RandomGenerator random) {
if (entityFitnessMap.isEmpty()) {
return null;
}
double sum = 0;
for (Double fitness : entityFitnessMap.values()) {
sum += fitness;
}
final double fitnessSum = sum;
final List<Pair<E, Double>> pmf = new ArrayList<>();
entityFitnessMap.forEachEntry(new TObjectDoubleProcedure<E>() {
@Override
public boolean execute(E entity, double value) {
if (fitnessSum != 0) {
pmf.add(new Pair<>(entity, value));
} else {
pmf.add(new Pair<>(entity, 1.0D / entityFitnessMap.size()));
}
return true;
}
});
return new EnumeratedDistribution<>(random, pmf);
}
示例5: getWeightedRandomSample
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public static <T extends HasWeightedProbability> T getWeightedRandomSample(Collection<T> col) {
ArrayList<Pair<T, Double>> list = new ArrayList<>();
col.forEach(i -> {
list.add(new Pair<>(i, i.getWeightedProbability()));
});
return new EnumeratedDistribution<>(list).sample();
}
示例6: get
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public OpDistribution get(Timing timing, int sampleCount)
{
PartitionGenerator generator = newGenerator();
List<Pair<Operation, Double>> operations = new ArrayList<>();
for (Map.Entry<T, Double> ratio : ratios.entrySet())
operations.add(new Pair<>(get(timing.newTimer(ratio.getKey().toString(), sampleCount), generator, ratio.getKey()),
ratio.getValue()));
return new SampledOpDistribution(new EnumeratedDistribution<>(operations), clustering.get());
}
示例7: get
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public OpDistribution get(Timing timing, int sampleCount, boolean isWarmup)
{
PartitionGenerator generator = newGenerator();
List<Pair<Operation, Double>> operations = new ArrayList<>();
for (Map.Entry<T, Double> ratio : ratios.entrySet())
{
List<? extends Operation> ops = get(timing.newTimer(ratio.getKey().toString(), sampleCount),
generator, ratio.getKey(), isWarmup);
for (Operation op : ops)
operations.add(new Pair<>(op, ratio.getValue() / ops.size()));
}
return new SampledOpDistribution(new EnumeratedDistribution<>(operations), clustering.get());
}
示例8: generateRandomCities
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
private String[] generateRandomCities(int sampleSize) {
EnumeratedDistribution<String> cityDistribution =
new EnumeratedDistribution<String>(
newList(
new Pair<String, Double>("Madrid", 1.0),
new Pair<String, Double>("London", 1.0),
new Pair<String, Double>("New York", 1.0),
new Pair<String, Double>("Boston", 1.0),
new Pair<String, Double>("Paris", 1.0),
new Pair<String, Double>("Rome", 1.0),
new Pair<String, Double>("Oslo", 1.0)
));
return Arrays.stream(cityDistribution.sample(sampleSize)).map(city -> (String)city).collect(Collectors.toList()).toArray(new String[0]);
}
示例9: generateRandomCountries
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
private String[] generateRandomCountries(int sampleSize) {
EnumeratedDistribution<String> cityDistribution =
new EnumeratedDistribution<String>(
newList(
new Pair<String, Double>("Spain", 1.0),
new Pair<String, Double>("England", 1.0),
new Pair<String, Double>("USA", 1.0),
new Pair<String, Double>("France", 1.0),
new Pair<String, Double>("Italy", 1.0),
new Pair<String, Double>("Norway", 1.0),
new Pair<String, Double>("Germany", 1.0)
));
return Arrays.stream(cityDistribution.sample(sampleSize)).map(city -> (String)city).collect(Collectors.toList()).toArray(new String[0]);
}
示例10: get
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public OpDistribution get(Timer timer)
{
PartitionGenerator generator = newGenerator();
List<Pair<Operation, Double>> operations = new ArrayList<>();
for (Map.Entry<T, Double> ratio : ratios.entrySet())
operations.add(new Pair<>(get(timer, generator, ratio.getKey()), ratio.getValue()));
return new SampledOpDistribution(new EnumeratedDistribution<>(operations), clustering.get());
}
示例11: randomSelect
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public static <E> E randomSelect(final List<E> choices, final Function<E, Double> probabilityFunction, final RandomGenerator rng) {
Utils.nonNull(choices);
Utils.nonNull(probabilityFunction);
Utils.nonNull(rng);
final List<Pair<E, Double>> pmf = choices.stream()
.map(e -> new Pair<>(e, probabilityFunction.apply(e))).collect(Collectors.toList());
return new EnumeratedDistribution<>(rng, pmf).sample();
}
示例12: CommandSelector
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public CommandSelector(List<Pair<Command, Double>> ratios, Distribution count)
{
selector = new EnumeratedDistribution<>(ratios);
this.count = count;
}
示例13: generateAnt
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public Ant generateAnt(AntSimulationState state, RandomGenerator random) {
Ant ant = generateRandomAnt(state, random);
Collection<Ant> reproductiveAnts = getReproductiveAnts();
if (reproductiveAnts.size() < 2) {
logger.debug("Generated ant {}", ant.getId());
return ant;
}
EnumeratedDistribution<Ant> antFitnessDistribution =
fitnessCalculator.getAntFitnessDistribution(reproductiveAnts, random);
Ant parent1 = antFitnessDistribution.sample();
Ant parent2 = antFitnessDistribution.sample();
// Ensure that parent1 != parent2.
while (ants.size() > 1 && parent2 == parent1) {
parent2 = antFitnessDistribution.sample();
}
// Get the parents genetic information.
GeneticDataStore parent1GeneticDataStore = state.getEntityGeneticDataStore(parent1);
GeneticDataStore parent2GeneticDataStore = state.getEntityGeneticDataStore(parent2);
GeneticDataStore childGeneticDataStore = state.getEntityGeneticDataStore(ant);
for (String key : parent1GeneticDataStore.keySet()) {
GeneticData parent1GeneticData = parent1GeneticDataStore.get(key);
GeneticData parent2GeneticData = parent2GeneticDataStore.get(key);
GeneticData childData = geneticRecombinator.recombine(
parent1GeneticData, parent2GeneticData, random);
childGeneticDataStore.put(key, childData);
}
logger.debug("Generated child ant {} with parents {} and {}",
ant.getId(), parent1.getId(), parent2.getId());
return ant;
}
示例14: EntityDistribution
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public EntityDistribution(List<Pair<T, Double>> entityProbabilities, RandomGenerator random) {
List<Pair<Optional<T>, Double>> pmf = new ArrayList<>();
double absentProbability = 1.0;
for (Pair<T, Double> entityProbability : entityProbabilities) {
absentProbability -= entityProbability.getSecond();
pmf.add(new Pair<>(Optional.of(entityProbability.getKey()), entityProbability.getValue()));
}
pmf.add(new Pair<>(Optional.<T>absent(), absentProbability));
this.distribution = new EnumeratedDistribution<>(random, pmf);
}
示例15: SampledOpDistribution
import org.apache.commons.math3.distribution.EnumeratedDistribution; //导入依赖的package包/类
public SampledOpDistribution(EnumeratedDistribution<Operation> operations, Distribution clustering)
{
this.operations = operations;
this.clustering = clustering;
}