moptipyapps.utils package¶
Some shared utilities. Some utilities for random sampling. The goal that we follow with class Bases: A distribution that is lower-bounded. the inner distribution to sample from Bases: A class representing a uniform choice. the choices Bases: A constant value. Bases: A class representing a Gamma distribution. Bases: A distribution that is lower and upper-bounded. the inner distribution to sample from Bases: A base class for integer distributions. Bases: The multiplication of several distributions. the distributions to sum over Bases: A class representing a normal distribution. Bases: The sum of several distributions. the distributions to sum over Bases: A class representing a uniform distribution. Get the distribution from the parameter. d ( the canonicalized distributionSubmodules¶
moptipyapps.utils.sampling module¶
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]
IntDistribution>>> 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))
IntDistribution¶IntDistributiontuple[int | IntDistribution, ...]¶IntDistributionIntDistributionIntDistribution>>> 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))
IntDistribution¶objectIntDistribution>>> 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)
tuple[IntDistribution, ...]¶IntDistributionIntDistribution>>> 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)))
tuple[IntDistribution, ...]¶IntDistributionint | IntDistribution) – the integer value or distribution