Skip to content

Generator

opulse.expression.generator

Random expression trees.

Shape control A tree has at most max_depth levels and at most leaf_budget leaves. Binary nodes split the budget between their children; a node whose budget is 1 can only be unary or an atom. A call-form node (an operator with k >= 3 arguments) splits the budget into k positive parts, so it needs a budget of at least k. Its weight is type_weights["call"] (default: the binary weight).

Numbers Values are drawn as randint(lo, randint(lo, hi)), which favours small numbers (as in the legacy generator).

Definition bodies :meth:ExpressionGenerator.definition_bodies builds the right-hand sides for a new operator at a target recursive layer L: trees use operators of layer <= L, then one operator occurring in them is swapped for a randomly chosen operator of layer exactly L. This guarantees the new operator depends on layer L.

OperatorPool dataclass

with_frequency

with_frequency(
    spec: str | None, seed: int = 0
) -> OperatorPool

Sampling frequencies over the defined operators: uniform or zipf:ALPHA.

An operator is drawn in two stages: first its kind (binary, prefix or postfix, by expressions.type_weights), then one operator of that kind. With zipf:ALPHA the second stage follows a long tail: the defined operators of each kind are ranked in a seeded random order and the one of rank r is drawn with weight proportional to r^-ALPHA, so a few concepts are frequent and most are rare. Base operations keep weight 1, and the defined operators' weights of each kind sum to their number, as under uniform; so the expected mix of base and defined operators is unchanged and only how the draws spread over the defined operators changes. zipf:0 is uniform.

frequency_ranking

frequency_ranking(seed: int = 0) -> dict[str, list[str]]

The defined operators of each kind, most frequent first, as with_frequency ranks them.

ExpressionGenerator

typed_tree

typed_tree(
    pool: OperatorPool, max_depth: int | None = None
) -> Node

Draw a closed, well-typed tree with a defined operator at its root.

single_operator_tree

single_operator_tree(
    operator: Operator, max_depth: int | None = None
) -> Node

A tree whose only operator is operator (for per-operator datasets).

definition_bodies

definition_bodies(
    registry: OperatorRegistry,
    layer: int,
    count: int,
    variables: tuple[str, ...],
    max_attempts: int = 1000,
    max_depth: int | None = None,
) -> list[Node]

count right-hand sides that together use an operator of exactly layer.