moptipyapps.utils package

Some shared utilities.

Submodules

moptipyapps.utils.sampling module

Some utilities for random sampling.

The goal that we follow with class IntDistribution is to have clearly defined integer-producing random distributions. We want to be able to say exactly how to generate some random numbers.

>>> from moptipy.utils.nputils import rand_generator
>>> rnd = rand_generator(0)
>>> nd = Normal(1, 2)
>>> nd
Normal(mu=1, sd=2)
>>> [nd.sample(rnd) for _ in range(20)]
[1, 1, 2, 1, 0, 2, 4, 3, 0, -2, 0, 1, -4, 1, -1, 0, 0, 0, 2, 3]
>>> lb = AtLeast(2, nd)
>>> lb
AtLeast(lb=2, d=Normal(mu=1, sd=2))
>>> [lb.sample(rnd) for _ in range(20)]
[4, 2, 3, 2, 2, 3, 4, 4, 4, 3, 2, 4, 5, 5, 4, 2, 2, 2, 2, 2]
>>> u = Uniform(10, 17)
>>> u
Uniform(low=10, high=17)
>>> [u.sample(rnd) for _ in range(10)]
[15, 11, 16, 10, 14, 13, 17, 11, 17, 10]
>>> s = Sum((u, nd, ))
>>> s
Sum(ds=(Uniform(low=10, high=17), Normal(mu=1, sd=2)))
>>> [s.sample(rnd) for _ in range(10)]
[16, 14, 21, 13, 16, 12, 13, 18, 12, 20]
>>> g = Gamma(2, 8)
>>> g
Gamma(k=2, scale=8)
>>> [g.sample(rnd) for _ in range(10)]
[14, 15, 10, 4, 20, 7, 11, 9, 16, 15]
>>> b = In(5, 10, g)
>>> b
In(lb=5, ub=10, d=Gamma(k=2, scale=8))
>>> [b.sample(rnd) for _ in range(10)]
[7, 8, 8, 5, 7, 6, 9, 9, 9, 9]
>>> s2 = Sum((Const(10), nd, ))
>>> s2
Sum(ds=(Const(v=10), Normal(mu=1, sd=2)))
>>> [s2.sample(rnd) for _ in range(10)]
[10, 10, 8, 12, 13, 10, 11, 8, 10, 14]
>>> ch = Choice((2, 20, 3000, g))
>>> ch
Choice(ch=(2, 20, 3000, Gamma(k=2, scale=8)))
>>> [ch.sample(rnd) for _ in range(10)]
[20, 20, 7, 11, 20, 2, 3000, 2, 2, 2]
class moptipyapps.utils.sampling.AtLeast(lb, d)[source]

Bases: IntDistribution

A distribution that is lower-bounded.

>>> AtLeast(5, Const(7))
AtLeast(lb=5, d=Const(v=7))
>>> AtLeast(5, AtLeast(8, Const(17)))
AtLeast(lb=8, d=Const(v=17))
>>> AtLeast(8, AtLeast(5, Const(17)))
AtLeast(lb=8, d=Const(v=17))
d: IntDistribution

the inner distribution to sample from

lb: int

the inclusive lower bound

sample(random)[source]

Sample from the lower-bounded distribution.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the result

simplify()[source]

Try to simplify this distribution.

Return type:

IntDistribution

Returns:

a simplified version of this distribution

class moptipyapps.utils.sampling.Choice(ch)[source]

Bases: IntDistribution

A class representing a uniform choice.

ch: tuple[int | IntDistribution, ...]

the choices

sample(random)[source]

Sample from the uniform distribution.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the result

class moptipyapps.utils.sampling.Const(v)[source]

Bases: IntDistribution

A constant value.

sample(random)[source]

Sample the constant integer.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the integer

v: int
class moptipyapps.utils.sampling.Gamma(k, scale)[source]

Bases: IntDistribution

A class representing a Gamma distribution.

k: int | float

the shape parameter

sample(random)[source]

Sample from the Gamma distribution.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the result

scale: int | float

the scale parameter

class moptipyapps.utils.sampling.In(lb, ub, d)[source]

Bases: IntDistribution

A distribution that is lower and upper-bounded.

>>> In(1, 10, Const(6))
In(lb=1, ub=10, d=Const(v=6))
>>> In(1, 10, In(5, 12, Const(6)))
In(lb=5, ub=10, d=Const(v=6))
>>> In(1, 10, AtLeast(6, Const(6)))
In(lb=6, ub=10, d=Const(v=6))
d: IntDistribution

the inner distribution to sample from

lb: int

the inclusive lower bound

sample(random)[source]

Sample from the lower-bounded distribution.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the result

simplify()[source]

Simplify this distribution.

Return type:

IntDistribution

Returns:

the simplified distribution

ub: int

the exclusive upper bound

class moptipyapps.utils.sampling.IntDistribution[source]

Bases: object

A base class for integer distributions.

sample(random)[source]

Sample an integer from a random number generator.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the integer

simplify()[source]

Try to simplify this distribution.

Return type:

IntDistribution

Returns:

a simplified version of this distribution

class moptipyapps.utils.sampling.Mul(ds)[source]

Bases: IntDistribution

The multiplication of several distributions.

>>> Mul((Const(1), Const(2))).simplify()
Const(v=2)
>>> Mul((Const(2), Normal(2, 3))).simplify()
Mul(ds=(Const(v=2), Normal(mu=2, sd=3)))
>>> Mul((Const(1), Normal(2, 3))).simplify()
Normal(mu=2, sd=3)
>>> Mul((Const(1), Normal(2, 3), Const(5))).simplify()
Mul(ds=(Normal(mu=2, sd=3), Const(v=5)))
>>> Mul((Const(1), Normal(2, 3), Const(0))).simplify()
Const(v=0)
ds: tuple[IntDistribution, ...]

the distributions to sum over

sample(random)[source]

Sample from the sum distribution.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the result

simplify()[source]

Simplify this distribution.

Return type:

IntDistribution

Returns:

the simplified distribution

class moptipyapps.utils.sampling.Normal(mu, sd)[source]

Bases: IntDistribution

A class representing a normal distribution.

mu: int | float

the expected value and center of the distribution

sample(random)[source]

Sample from the normal distribution.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the result

sd: int | float

the standard deviation of the distribution

class moptipyapps.utils.sampling.Sum(ds)[source]

Bases: IntDistribution

The sum of several distributions.

>>> Sum((Const(1), Const(2))).simplify()
Const(v=3)
>>> Sum((Const(1), Normal(2, 3))).simplify()
Sum(ds=(Const(v=1), Normal(mu=2, sd=3)))
>>> Sum((Const(0), Normal(2, 3))).simplify()
Normal(mu=2, sd=3)
>>> Sum((Const(0), Normal(2, 3), Const(5))).simplify()
Sum(ds=(Normal(mu=2, sd=3), Const(v=5)))
ds: tuple[IntDistribution, ...]

the distributions to sum over

sample(random)[source]

Sample from the sum distribution.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the result

simplify()[source]

Simplify this distribution.

Return type:

IntDistribution

Returns:

the simplified distribution

class moptipyapps.utils.sampling.Uniform(low, high)[source]

Bases: IntDistribution

A class representing a uniform distribution.

high: int

the highest permitted value

low: int

the lowest permitted value

sample(random)[source]

Sample from the uniform distribution.

Parameters:

random (Generator) – the random number generator

Return type:

int

Returns:

the result

simplify()[source]

Try to simplify this distribution.

Return type:

IntDistribution

Returns:

a simplified version of this distribution

moptipyapps.utils.sampling.distribution(d)[source]

Get the distribution from the parameter.

Parameters:

d (int | IntDistribution) – the integer value or distribution

Return type:

IntDistribution

Returns:

the canonicalized distribution