Feat: random expression generator is working
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Bertrand Benjamin 2021-10-06 16:17:50 +02:00
parent 5d909a5f81
commit a267acd7b3
3 changed files with 24 additions and 44 deletions

View File

@ -1,4 +1,5 @@
from ...core.tree import MutableTree, Tree from ...core.tree import MutableTree, Tree
from ...core.MO import moify
from .grammar import extract_letters, eval_words from .grammar import extract_letters, eval_words
from .generate import random_generator from .generate import random_generator
from .str2 import rdstr2 from .str2 import rdstr2
@ -97,9 +98,9 @@ class RandomTree(MutableTree):
return leaf.replace(leaves_value) return leaf.replace(leaves_value)
except AttributeError: except AttributeError:
return leaf return leaf
return self.map_on_leaf(replace) return self.map_on_leaf(replace).map_on_leaf(moify)
def generate(self, conditions:list[str]=[], config:dict={} , configs:dict={}) -> Tree: def generate(self, conditions:list[str]=[], global_config:dict={} , configs:dict={}) -> Tree:
""" Generate a random version of self """ Generate a random version of self
:param conditions: list of conditions :param conditions: list of conditions
@ -107,6 +108,6 @@ class RandomTree(MutableTree):
:param configs: specific configuration for each generated values :param configs: specific configuration for each generated values
""" """
generated_values = random_generator(self.random_values, config, configs) generated_values = random_generator(self.random_value, conditions, global_config, configs)
leaves = eval_words(self.random_leaves, generated_values) leaves = eval_words(self.random_leaves, generated_values)
return self.eval_random_leaves(leaves) return self.eval_random_leaves(leaves)

View File

@ -1,55 +1,35 @@
from ..API.expression import Expression from ..API.expression import Expression
from ..core.tree import Tree from .core.random_tree import RandomTree
def expression(): DEFAUTL_CONFIG = {
""" Generate a random expression """ "rejected": [0, 1],
pass "min_max": (-10, 10),
}
@classmethod def expression_generator(template:str, conditions:list[str]=[], global_config:dict={}, configs:dict={}):
def random( """ Generate a random expression
cls,
template,
conditions=[],
rejected=[0],
min_max=(-10, 10),
variables_scope={},
shuffle=False,
):
""" Initiate randomly the expression
:param template: the template of the expression :param template: the template of the expression
:param conditions: conditions on randomly generate variable :param conditions: conditions on randomly generate variable
:param rejected: Values to reject for all random variables :param global_config: configuration for all variables
:param min_max: Min and max value for all random variables :param configs: configuration for each variables
:param variables_scope: Dictionnary for each random varaibles to fic rejected and min_max :return: Expression or Token generated
:param shuffle: allowing to shuffle the tree
:returns: TODO
:example: :example:
>>> e = Expression.random("{a}/{a*k}") >>> e = expression_generator("{a}/{a*k}")
>>> e # doctest: +SKIP >>> e # doctest: +SKIP
<Exp: -3 / -15> <Exp: -3 / -15>
>>> e = Expression.random("{a}/{a*k} - 3*{b}", variables_scope={'a':{'min_max':(10, 30)}}) >>> e = expression_generator("{a}/{a*k} - 3*{b}", configs={'a':{'min_max':(10, 30)}})
>>> e # doctest: +SKIP >>> e # doctest: +SKIP
<Exp: 18 / 108 - 3 * 9> <Exp: 18 / 108 - 3 * 9>
>>> e = Expression.random("{a}*x + {b}*x + 3", ["a>b"], rejected=[0, 1]) >>> e = expression_generator("{a}*x + {b}*x + 3", conditions=["a>b"], global_config={"rejected":[0, 1]})
>>> ee = e.simplify()
>>> print(e) # doctest: +SKIP >>> print(e) # doctest: +SKIP
10x - 6x + 3 10x - 6x + 3
>>> ee = e.simplify()
>>> print(ee) # doctest: +SKIP >>> print(ee) # doctest: +SKIP
4x + 3 4x + 3
""" """
rd_t = Tree.from_str(template) rd_tree = RandomTree.from_str(template)
leafs = extract_rdleaf(rd_t) generated_tree = rd_tree.generate(conditions, dict(DEFAUTL_CONFIG, **global_config), configs)
rd_varia = extract_rv(leafs) return Expression._post_processing(generated_tree)
generated = random_generator(
rd_varia, conditions, rejected, min_max, variables_scope
)
computed = compute_leafs(leafs, generated)
t = replace_rdleaf(rd_t, computed).map_on_leaf(moify)
if shuffle:
raise NotImplemented("Can't suffle expression yet")
return cls._post_processing(t)

View File

@ -22,10 +22,9 @@ def list_generator(var_list:list[str], conditions:list[str]=[], global_config:di
:param rd_variables: list of random variables to generate (can be computed value - "a*b") :param rd_variables: list of random variables to generate (can be computed value - "a*b")
:param conditions: condition over variables :param conditions: condition over variables
:param rejected Rejected values for the generator (default [0]) :param global_config: configuration for all variables
:param min_max: (min, max) limits in between variables will be generated :param configs: configuration for each variables
:param variables_scope: rejected and min_max define for individual variables :return: list of generated variables
:return: dictionnary of generated variables
:example: :example:
>>> a, ab, b, c = list_generator(["a", "a*b", "b", "c"]) >>> a, ab, b, c = list_generator(["a", "a*b", "b", "c"])