From a267acd7b3af5e80e8b6fab03a926a619b551931 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 6 Oct 2021 16:17:50 +0200 Subject: [PATCH] Feat: random expression generator is working --- mapytex/calculus/random/core/random_tree.py | 7 +-- mapytex/calculus/random/expression.py | 54 +++++++-------------- mapytex/calculus/random/list.py | 7 ++- 3 files changed, 24 insertions(+), 44 deletions(-) diff --git a/mapytex/calculus/random/core/random_tree.py b/mapytex/calculus/random/core/random_tree.py index cf17ee2..891953e 100644 --- a/mapytex/calculus/random/core/random_tree.py +++ b/mapytex/calculus/random/core/random_tree.py @@ -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) diff --git a/mapytex/calculus/random/expression.py b/mapytex/calculus/random/expression.py index b3d85ef..758f128 100644 --- a/mapytex/calculus/random/expression.py +++ b/mapytex/calculus/random/expression.py @@ -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 - >>> 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 = 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) diff --git a/mapytex/calculus/random/list.py b/mapytex/calculus/random/list.py index a1af58a..bb6378c 100644 --- a/mapytex/calculus/random/list.py +++ b/mapytex/calculus/random/list.py @@ -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"])