Class CompositeSamplers
- java.lang.Object
-
- org.apache.commons.rng.sampling.CompositeSamplers
-
public final class CompositeSamplers extends Object
Factory class to create a sampler that combines sampling from multiple samplers.The composite sampler is constructed using a
builder
for the type of samplers that will form the composite. Each sampler has a weight in the composition. Samples are returned using a 2 step algorithm:- Select a sampler based on its weighting
- Return a sample from the selected sampler
The weights used for each sampler create a discrete probability distribution. This is sampled using a discrete probability distribution sampler. The builder provides methods to change the default implementation.
The following example will create a sampler to uniformly sample the border of a triangle using the line segment lengths as weights:
UniformRandomProvider rng = RandomSource.KISS.create(); double[] a = {1.23, 4.56}; double[] b = {6.78, 9.01}; double[] c = {3.45, 2.34}; ObjectSampler<double[]> sampler = CompositeSamplers.<double[]>newObjectSamplerBuilder() .add(LineSampler.of(rng, a, b), Math.hypot(a[0] - b[0], a[1] - b[1])) .add(LineSampler.of(rng, b, c), Math.hypot(b[0] - c[0], b[1] - c[1])) .add(LineSampler.of(rng, c, a), Math.hypot(c[0] - a[0], c[1] - a[1])) .build(rng);
- Since:
- 1.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
CompositeSamplers.Builder<S>
Builds a composite sampler.static class
CompositeSamplers.DiscreteProbabilitySampler
The DiscreteProbabilitySampler class defines implementations that sample from a user-defined discrete probability distribution.static interface
CompositeSamplers.DiscreteProbabilitySamplerFactory
A factory for creating a sampler of a user-defined discrete probability distribution.
-
Method Summary
-
-
-
Method Detail
-
newObjectSamplerBuilder
public static <T> CompositeSamplers.Builder<ObjectSampler<T>> newObjectSamplerBuilder()
Create a new builder for a compositeObjectSampler
.Note: If the compiler cannot infer the type parameter of the sampler it can be specified within the diamond operator
<T>
preceding the call tonewObjectSamplerBuilder()
, for example:CompositeSamplers.<double[]>newObjectSamplerBuilder()
- Type Parameters:
T
- Type of the sample.- Returns:
- the builder
-
newSharedStateObjectSamplerBuilder
public static <T> CompositeSamplers.Builder<SharedStateObjectSampler<T>> newSharedStateObjectSamplerBuilder()
Create a new builder for a compositeSharedStateObjectSampler
.Note: If the compiler cannot infer the type parameter of the sampler it can be specified within the diamond operator
<T>
preceding the call tonewSharedStateObjectSamplerBuilder()
, for example:CompositeSamplers.<double[]>newSharedStateObjectSamplerBuilder()
- Type Parameters:
T
- Type of the sample.- Returns:
- the builder
-
newDiscreteSamplerBuilder
public static CompositeSamplers.Builder<DiscreteSampler> newDiscreteSamplerBuilder()
Create a new builder for a compositeDiscreteSampler
.- Returns:
- the builder
-
newSharedStateDiscreteSamplerBuilder
public static CompositeSamplers.Builder<SharedStateDiscreteSampler> newSharedStateDiscreteSamplerBuilder()
Create a new builder for a compositeSharedStateDiscreteSampler
.- Returns:
- the builder
-
newContinuousSamplerBuilder
public static CompositeSamplers.Builder<ContinuousSampler> newContinuousSamplerBuilder()
Create a new builder for a compositeContinuousSampler
.- Returns:
- the builder
-
newSharedStateContinuousSamplerBuilder
public static CompositeSamplers.Builder<SharedStateContinuousSampler> newSharedStateContinuousSamplerBuilder()
Create a new builder for a compositeSharedStateContinuousSampler
.- Returns:
- the builder
-
newLongSamplerBuilder
public static CompositeSamplers.Builder<LongSampler> newLongSamplerBuilder()
Create a new builder for a compositeLongSampler
.- Returns:
- the builder
-
newSharedStateLongSamplerBuilder
public static CompositeSamplers.Builder<SharedStateLongSampler> newSharedStateLongSamplerBuilder()
Create a new builder for a compositeSharedStateLongSampler
.- Returns:
- the builder
-
-