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.MO import moify
from .grammar import extract_letters, eval_words
from .generate import random_generator
from .str2 import rdstr2
@ -97,9 +98,9 @@ class RandomTree(MutableTree):
return leaf.replace(leaves_value)
except AttributeError:
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
:param conditions: list of conditions
@ -107,6 +108,6 @@ class RandomTree(MutableTree):
: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)
return self.eval_random_leaves(leaves)

View File

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

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 conditions: condition over variables
:param rejected Rejected values for the generator (default [0])
:param min_max: (min, max) limits in between variables will be generated
:param variables_scope: rejected and min_max define for individual variables
:return: dictionnary of generated variables
:param global_config: configuration for all variables
:param configs: configuration for each variables
:return: list of generated variables
:example:
>>> a, ab, b, c = list_generator(["a", "a*b", "b", "c"])