Feat: RandomTree, rename generate function ...

This commit is contained in:
2021-10-03 15:36:35 +02:00
parent 1672530179
commit 204c7dffd7
12 changed files with 363 additions and 320 deletions

View File

@@ -12,13 +12,6 @@ Expression
"""
from functools import partial
from ..core import AssocialTree, Tree, compute, typing, TypingError
#from ..core.random import (
# extract_rdleaf,
# extract_rv,
# random_generator,
# compute_leafs,
# replace_rdleaf,
#)
from ..core.MO import moify
from .tokens import factory
from .renders import render
@@ -79,55 +72,6 @@ class Expression(object):
return cls(t)
@classmethod
def random(
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 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
:example:
>>> e = Expression.random("{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 # doctest: +SKIP
<Exp: 18 / 108 - 3 * 9>
>>> e = Expression.random("{a}*x + {b}*x + 3", ["a>b"], rejected=[0, 1])
>>> ee = e.simplify()
>>> print(e) # doctest: +SKIP
10x - 6x + 3
>>> print(ee) # doctest: +SKIP
4x + 3
"""
rd_t = Tree.from_str(template, random=True)
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)
@classmethod
def _post_processing(cls, t):
""" Post process the tree by typing it """