Parsing¶
The parser module provides tools for converting mathematical expressions to callable functions.
- class skagent.parser.ControlToken(args)¶
Represents a parsed Control variable.
- skagent.parser.math_text_to_lambda(text)¶
Returns a function represented by the given mathematical text.
- skagent.parser.skagent_loader()¶
A PyYAML loader that supports tags for scikit-agent, such as random variables and model tags.
Core Parser Functions¶
- skagent.parser.math_text_to_lambda(text)
Returns a function represented by the given mathematical text.
Parser Classes¶
- class skagent.parser.ControlToken(args)
Bases:
objectRepresents a parsed Control variable.
- class skagent.parser.Expression(text)
Bases:
object- func()
Rules¶
The rule module builds on the parser to extract dependencies and formulas from model rules.
Universal rule processing module for scikit-agent models.
This module provides utilities for working with “rules” - the right-hand side expressions in structural statements that define model dynamics, controls, and rewards.
A rule can be: - A callable (function, lambda) - A Control object - A Distribution or tuple with distribution parameters - A string expression - A constant value
Key functions: - format_rule: Get printable string version of a rule - extract_dependencies: Get variables that a rule depends on
- skagent.rule.extract_dependencies(rule)¶
Extract variable dependencies from a model rule.
- Parameters:
rule (various) – Can be Control, Distribution, callable, tuple, or string
- Returns:
List of dependency variable names
- Return type:
- skagent.rule.extract_formula(rule)¶
Extract formula as string from a rule.
- Parameters:
rule (various) – The rule to extract formula from
- Returns:
The formula as string
- Return type:
- skagent.rule.format_rule(var, rule)¶
Get a printable (string) version of a rule.
- skagent.rule.math_text_to_free_variable_names(txt)¶
Extract free variable names from mathematical text using SymPy.
- Parameters:
txt (str) – Mathematical expression as string
- Returns:
List of free variable names
- Return type:
Examples
>>> math_text_to_free_variable_names("10 * x + y **2 - z") ['x', 'y', 'z']
Example Usage¶
Parsing Mathematical Expressions¶
import inspect
from skagent.parser import math_text_to_lambda
# Convert a string expression to a callable function
reward_func = math_text_to_lambda("c**(1-rho)/(1-rho)")
# The positional argument order follows the expression's free symbols.
# Check it before calling the function positionally:
inspect.signature(reward_func) # (c, rho)
reward = reward_func(1.5, 2.0)
Warning
Variable names that collide with built-in SymPy objects are parsed as those
objects rather than as free variables. For example, gamma is parsed as the
gamma function, so math_text_to_lambda("c**(1-gamma)/(1-gamma)") raises a
TypeError. Prefer names like rho or CRRA for curvature parameters.