Merge remote-tracking branch 'origin/main' into issue-4-variable-pool
This commit is contained in:
commit
7f66e3db1f
2 files changed
+44
-2
No files matched your search
@@ -159,12 +159,45 @@ class TreeGen:
|
||||
to = r.choice([Num(0)] * 4 + [INF] * 4 + [Num(1), Var("a"), Neg(INF)])
|
||||
return Lim(self.expr(max(depth, 1), v), v, to)
|
||||
|
||||
def term(self) -> Node:
|
||||
"""One textbook-style term: x^2, 3 x, sin(x)^2, y, 7."""
|
||||
r = self.rng
|
||||
v = self.var()
|
||||
match r.choices(["pow", "coef", "trig", "var", "num"], [5, 2, 1, 2, 1])[0]:
|
||||
case "pow":
|
||||
return Pow(v, Num(r.choice([2, 2, 2, 3])))
|
||||
case "coef":
|
||||
return Mul(Num(r.randint(2, 9)), r.choice([v, Pow(v, Num(2))]))
|
||||
case "trig":
|
||||
return Pow(Func(r.choice(["sin", "cos", "tan"]), v), Num(2))
|
||||
case "var":
|
||||
return v
|
||||
return self.num()
|
||||
|
||||
def terms(self) -> Node:
|
||||
"""A flat sum of 2-3 simple terms, like x^2 + y^2 or a x^2 + b x + c.
|
||||
|
||||
Random trees almost never produce these shapes, yet they are the most common left sides people say.
|
||||
"""
|
||||
r = self.rng
|
||||
out = self.term()
|
||||
for _ in range(r.choice([1, 1, 2])):
|
||||
out = (Add if r.random() < 0.75 else Sub)(out, self.term())
|
||||
return out
|
||||
|
||||
def tree(self) -> Node:
|
||||
r = self.rng
|
||||
depth = r.choices([1, 2, 3, 4], [25, 40, 25, 10])[0]
|
||||
e = self.big(depth - 1) if r.random() < 0.3 else self.expr(depth)
|
||||
if r.random() < 0.2:
|
||||
lhs = r.choice([Var("y"), Var("f"), Var("x"), self.expr(r.choice([0, 1]))])
|
||||
# a long left side too ("x^2 + y^2 = r^2"), not only "y = ...": depth 2+ was never seen before (#2)
|
||||
x = r.random()
|
||||
if x < 0.35:
|
||||
lhs: Node = r.choice([Var("y"), Var("f"), Var("x")])
|
||||
elif x < 0.65:
|
||||
lhs = self.terms()
|
||||
else:
|
||||
lhs = self.expr(r.choices([0, 1, 2, 3], [2, 3, 3, 2])[0])
|
||||
return Eq(lhs, e)
|
||||
return e
|
||||
|
||||
|
||||
Reference in new issue
Block a user