Note
Click here to download the full example code
Combinatorial generators¶
In this example we are going to expose the various desing of experiments that allow to generate all the integer collections satisfying a given combinatorial constraint:
The Tuples generator, which allows one to generate all the elements of a Cartesian product .
The total number of generated points is .
The K-permutations generator, which allows one to generate all the injective functions from into
The total number of generated points is .
The Combinations generator, which allows one to generate all the subsets of size of
The total number of generated points is .
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
import math as m
ot.Log.Show(ot.Log.NONE)
Tuples¶
experiment = ot.Tuples([2, 3, 5])
print(experiment.generate())
Out:
[[0,0,0],[1,0,0],[0,1,0],[1,1,0],[0,2,0],[1,2,0],[0,0,1],[1,0,1],[0,1,1],[1,1,1],[0,2,1],[1,2,1],[0,0,2],[1,0,2],[0,1,2],[1,1,2],[0,2,2],[1,2,2],[0,0,3],[1,0,3],[0,1,3],[1,1,3],[0,2,3],[1,2,3],[0,0,4],[1,0,4],[0,1,4],[1,1,4],[0,2,4],[1,2,4]]#30
K-permutations¶
experiment = ot.KPermutations(3, 4)
print(experiment.generate())
Out:
[[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0],[0,1,3],[0,3,1],[1,0,3],[1,3,0],[3,0,1],[3,1,0],[0,2,3],[0,3,2],[2,0,3],[2,3,0],[3,0,2],[3,2,0],[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]#24
Combinations¶
experiment = ot.Combinations(4, 6)
print(experiment.generate())
Out:
[[0,1,2,3],[0,1,2,4],[0,1,2,5],[0,1,3,4],[0,1,3,5],[0,1,4,5],[0,2,3,4],[0,2,3,5],[0,2,4,5],[0,3,4,5],[1,2,3,4],[1,2,3,5],[1,2,4,5],[1,3,4,5],[2,3,4,5]]#15
Total running time of the script: ( 0 minutes 0.002 seconds)