Doc: Doctest for Tree generator
This commit is contained in:
parent
4e7ba55970
commit
040dfff0ec
@ -38,16 +38,33 @@ Tree with RdLeaf replaced by generated values
|
||||
:example:
|
||||
|
||||
>>> from ..tree import Tree
|
||||
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k"))
|
||||
>>> rd_t = Tree("/", RdLeaf("a"), RdLeaf("a*k"))
|
||||
>>> print(rd_t)
|
||||
+
|
||||
/
|
||||
> {a}
|
||||
> {a*k}
|
||||
>>> extract_rdleaf(rd_t)
|
||||
>>> leafs = extract_rdleaf(rd_t)
|
||||
>>> leafs
|
||||
['a', 'a*k']
|
||||
>>> rd_varia = extract_rv(leafs)
|
||||
>>> rd_varia # doctest: +SKIP
|
||||
{'a', 'k'}
|
||||
>>> generated = random_generator(rd_varia, conditions=['a%2+1'])
|
||||
>>> generated # doctest: +SKIP
|
||||
{'a': 7, 'k': 4}
|
||||
>>> computed = compute_leafs(leafs, generated)
|
||||
>>> computed # doctest: +SKIP
|
||||
{'a': 7, 'a*k': 28}
|
||||
>>> replaced = replace_rdleaf(rd_t, computed)
|
||||
>>> print(replaced) # doctest: +SKIP
|
||||
/
|
||||
> 7
|
||||
> 28
|
||||
|
||||
"""
|
||||
|
||||
from random import choice
|
||||
from functools import reduce
|
||||
from .leaf import RdLeaf
|
||||
|
||||
|
||||
@ -129,11 +146,10 @@ def replace_rdleaf(tree, computed_leafs):
|
||||
|
||||
return tree.map_on_leaf(replace)
|
||||
|
||||
def random_generator(rd_variables,
|
||||
conditions = [],
|
||||
rejected = [0],
|
||||
min_max = (-10, 10),
|
||||
variables_scope = {}):
|
||||
|
||||
def random_generator(
|
||||
rd_variables, conditions=[], rejected=[0], min_max=(-10, 10), variables_scope={}
|
||||
):
|
||||
""" Generate random variables
|
||||
|
||||
:param rd_variables: list of random variables to generate
|
||||
@ -165,13 +181,23 @@ def random_generator(rd_variables,
|
||||
>>> gene["a"] % gene["b"]
|
||||
0
|
||||
"""
|
||||
complete_scope = build_variable_scope(rd_variables,
|
||||
rejected, min_max,
|
||||
variables_scope)
|
||||
choices_list = {v: list(set(range(
|
||||
complete_scope[v]["min_max"][0], complete_scope[v]["min_max"][1]+1)
|
||||
).difference(complete_scope[v]["rejected"]))
|
||||
for v in rd_variables}
|
||||
complete_scope = build_variable_scope(
|
||||
rd_variables, rejected, min_max, variables_scope
|
||||
)
|
||||
choices_list = {
|
||||
v: list(
|
||||
set(
|
||||
range(
|
||||
complete_scope[v]["min_max"][0], complete_scope[v]["min_max"][1] + 1
|
||||
)
|
||||
).difference(complete_scope[v]["rejected"])
|
||||
)
|
||||
for v in rd_variables
|
||||
}
|
||||
|
||||
# quantity_choices = reduce(lambda x,y : x*y,
|
||||
# [len(choices_list[v]) for v in choices_list])
|
||||
# TODO: améliorer la méthode de rejet avec un cache |dim. mai 12 17:04:11 CEST 2019
|
||||
|
||||
generate_variable = {v: choice(choices_list[v]) for v in rd_variables}
|
||||
|
||||
@ -180,6 +206,7 @@ def random_generator(rd_variables,
|
||||
|
||||
return generate_variable
|
||||
|
||||
|
||||
def build_variable_scope(rd_variables, rejected, min_max, variables_scope):
|
||||
""" Build variables scope from incomplete one
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user