### Install Mathy and Parse Expression Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/tree_to_list.ipynb Installs the Mathy library using pip and demonstrates parsing a basic mathematical expression '4 + 2x' into a list of nodes. It also includes an assertion to verify the number of nodes. ```python # This file is generated from a Mathy (https://mathy.ai) code example. !pip install mathy --upgrade from typing import List from mathy_core import ExpressionParser, MathExpression parser = ExpressionParser() expression: MathExpression = parser.parse("4 + 2x") nodes: List[MathExpression] = expression.to_list() # len([4,+,2,*,x]) assert len(nodes) == 5 ``` -------------------------------- ### Initialize and Use Mathy Polynomial Simplification Environment Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/lists_to_observations.ipynb Demonstrates how to install the Mathy library, initialize the `PolySimplify` environment, and retrieve the initial state and observation. It also includes assertions to validate the structure of the observation, ensuring the number of nodes matches values and the mask dimensions are correct. ```python # This file is generated from a Mathy (https://mathy.ai) code example. !pip install mathy --upgrade from mathy_envs import MathyEnv, MathyEnvState, MathyObservation, envs env: MathyEnv = envs.PolySimplify() state: MathyEnvState = env.get_initial_state()[0] observation: MathyObservation = env.state_to_observation(state) # As many nodes as values assert len(observation.nodes) == len(observation.values) # Mask is a binary validity mask of size (num_rules, num_nodes) assert len(observation.mask) == len(env.rules) assert len(observation.mask[0]) == len(observation.nodes) ``` -------------------------------- ### List and Reset Mathy Gym Environments Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/openai_gym.ipynb This snippet demonstrates how to install required libraries (mathy and gymnasium), import them, retrieve all registered Gymnasium environments, filter for Mathy-specific environments, and then iterate through them to create, reset, and assert basic functionality of each environment. ```python # This file is generated from a Mathy (https://mathy.ai) code example. !pip install mathy --upgrade !pip install gymnasium import gymnasium as gym from mathy_envs.gym import MathyGymEnv all_envs = gym.registry.values() # Filter to just mathy registered envs mathy_envs = [e for e in all_envs if e.id.startswith("mathy-")] assert len(mathy_envs) > 0 # Each env can be created and produce an initial observation without # special configuration. for gym_env_spec in mathy_envs: wrapper_env: MathyGymEnv = gym.make(gym_env_spec.id) # type:ignore assert wrapper_env is not None observation = wrapper_env.reset() assert observation is not None ``` -------------------------------- ### Install Mathy and Parse Expression Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/text_to_tree.ipynb Installs the Mathy library using pip, then tokenizes and parses a simple mathematical expression '4 + 2x'. It also asserts that the parsed expression contains exactly one variable. ```python # This file is generated from a Mathy (https://mathy.ai) code example. !pip install mathy --upgrade from typing import List from mathy_core import ExpressionParser, MathExpression, Token, VariableExpression problem = "4 + 2x" parser = ExpressionParser() tokens: List[Token] = parser.tokenize(problem) expression: MathExpression = parser.parse(problem) assert len(expression.find_type(VariableExpression)) == 1 ``` -------------------------------- ### Install mathy_envs Source: https://github.com/mathy/mathy_envs/blob/master/README.md Installs the mathy_envs package from PyPI using pip. ```bash pip install mathy_envs ``` -------------------------------- ### Install Mathy Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/custom_actions.ipynb Installs or upgrades the Mathy library using pip. ```bash # This file is generated from a Mathy (https://mathy.ai) code example. !pip install mathy --upgrade ``` -------------------------------- ### Semantic Versioning Example Source: https://github.com/mathy/mathy_envs/blob/master/README.md Illustrates semantic versioning changes for mathy_envs, showing how patch and minor version increments affect compatibility. It also provides an example of how to pin a version to avoid breaking changes. ```python ## Semantic Versioning Mathy Envs tries to be predictable when it comes to breaking changes, so the project uses semantic versioning to help users avoid breakage. Specifically, new releases increase the `patch` semver component for new features and fixes, and the `minor` component when there are breaking changes. If you don't know much about semver strings, they're usually formatted `{major}.{minor}.{patch}` so increasing the `patch` component means incrementing the last number. Consider a few examples: | From Version | To Version | Changes are Breaking | | :----------: | :--------: | :------------------: | | 0.2.0 | 0.2.1 | No | | 0.3.2 | 0.3.6 | No | | 0.3.1 | 0.3.17 | No | | 0.2.2 | 0.3.0 | Yes | If you are concerned about breaking changes, you can pin the version in your requirements so that it does not go beyond the current semver `minor` component, for example if the current version was `0.1.37`: ``` mathy_envs>=0.1.37,<0.2.0 ``` ``` -------------------------------- ### Example Episode Input and Solution Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/complex_simplify.md Demonstrates an example episode of simplifying a complex algebraic expression, showing the input and the final simplified solution. ```python Input: 4a^4 * 5a^4 * 2b^4 mathy:4a^4 * 5a^4 * 2b^4 Solution: 40b^4 * a^8 mathy:40b^4 * a^8 ``` -------------------------------- ### Install mathy_envs Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/index.md Installs the mathy_envs package using pip. Requires Python 3.6+. ```bash pip install mathy_envs ``` -------------------------------- ### Binomial Multiplication Examples Source: https://github.com/mathy/mathy_envs/blob/master/mathy_envs/envs/binomial_distribute.md Demonstrates various examples of binomial multiplications and their simplified forms, illustrating the distribution and simplification rules. ```python `(4 + g^2)(9 + e^3)` must be simplified to `36 + (4e^3 + (9g^2 + g^2 * e^3))` `(a + a) * a` must be simplified to `2a^2` `(c + 5) * c` must be simplified to `c^2 + 5c` `(i^3 + 2)(i^3 + 9)` must be simplified to `i^6 + (11i^3 + 18)` `(3 + 12o)(10 + 8o)` must be simplified to `30 + (144o + 96o^2)` ``` -------------------------------- ### Example Episode: Binomial and Monomial Multiplication Source: https://github.com/mathy/mathy_envs/blob/master/mathy_envs/envs/binomial_distribute.md Walks through an example episode where a trained agent distributes and simplifies binomial and monomial multiplications, showing the steps from input to solution. ```python Input: `(k^4 + 7)(4 + h^2)` `mathy:(k^4 + 7)(4 + h^2)` Steps: | Step | Text | | --------------------- | ----------------------------------- | | initial | (k^4 + 7)(4 + h^2) | | distributive multiply | (4 + h^2) \* k^4 + (4 + h^2) \* 7 | | distributive multiply | 4k^4 + k^4 \* h^2 + (4 + h^2) \* 7 | | commutative swap | 4k^4 + k^4 \* h^2 + 7 \* (4 + h^2) | | distributive multiply | 4k^4 + k^4 \* h^2 + (7 \* 4 + 7h^2) | | constant arithmetic | 4k^4 + k^4 \* h^2 + (28 + 7h^2) | | solution | **4k^4 + k^4 \* h^2 + 28 + 7h^2** | Solution: `4k^4 + k^4 * h^2 + 28 + 7h^2` `mathy:4k^4 + k^4 * h^2 + 28 + 7h^2` ``` -------------------------------- ### Development Environment Tools Source: https://github.com/mathy/mathy_envs/blob/master/requirements-dev.txt Lists essential development tools for the Mathy project, including type checkers, test runners, code formatters, and linters. ```shell mypy pytest pytest-cov black flake8 isort autoflake mock gymnasium # for tests ``` -------------------------------- ### Environment Integration Example Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/observations.md Shows how to integrate the observation system with a Mathy environment, allowing models to receive observations in a consistent format. ```python from mathy_envs.envs.mathy_env import MathyEnv from mathy_envs.envs.unified_observations import UnifiedObservations # Initialize environment with a specific observation type env = MathyEnv(observation_type="message_passing", normalize=True) # Reset environment to get initial observation observation, info = env.reset() # The observation is already a UnifiedObservations object print(f"Observation Type: {observation.observation_type}") print(f"Node Features: {observation.get_node_features()}") print(f"Edge Index: {observation.get_edge_index()}") print(f"Action Mask: {observation.get_action_mask()}") # Example of taking a step # action = env.action_space.sample() # Sample a random action # next_observation, reward, terminated, truncated, info = env.step(action) env.close() ``` -------------------------------- ### Example Episode: Polynomial Simplification Steps Source: https://github.com/mathy/mathy_envs/blob/master/mathy_envs/envs/poly_simplify.md Shows a step-by-step process an agent might take to simplify a given polynomial expression, including commutative swaps and distributive factoring. ```python # Input: 1k + 210r + 7z + 11k + 10z # mathy:1k + 210r + 7z + 11k + 10z # Step 1: Commutative swap # 11k + (1k + 210r + 7z) + 10z # Step 2: Distributive factoring # 11k + (1k + 210r) + (7 + 10) * z # Step 3: Distributive factoring # (11 + 1) * k + 210r + (7 + 10) * z # Step 4: Constant arithmetic # (11 + 1) * k + 210r + 17z # Step 5: Constant arithmetic # 12k + 210r + 17z # Solution: 12k + 210r + 17z # mathy:12k + 210r + 17z print('Example episode demonstrates simplification steps for: 1k + 210r + 7z + 11k + 10z') print('Final simplified expression: 12k + 210r + 17z') ``` -------------------------------- ### Custom Mathy Environment with Distributive Factor Out Rule Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/custom_win_conditions.ipynb Defines a custom Mathy environment that uses the `DistributiveFactorOutRule` to determine terminal states. The environment transitions to a terminal state if the rule can find any applicable nodes in the expression. It includes example states to test terminal and non-terminal conditions. ```python # This file is generated from a Mathy (https://mathy.ai) code example. !pip install mathy --upgrade """Custom environment with win conditions that are met whenever two nodes are adjacent to each other that can have the distributive property applied to factor out a common term """ from typing import Optional from mathy_core import MathExpression, rules from mathy_envs import ( MathyEnv, MathyEnvState, MathyObservation, is_terminal_transition, time_step, ) class CustomWinConditions(MathyEnv): rule = rules.DistributiveFactorOutRule() def transition_fn( self, env_state: MathyEnvState, expression: MathExpression, features: MathyObservation, ) -> Optional[time_step.TimeStep]: # If the rule can find any applicable nodes if self.rule.find_node(expression) is not None: # Return a terminal transition with reward return time_step.termination(features, self.get_win_signal(env_state)) # None does nothing return None env = CustomWinConditions() # This state is not terminal because none of the nodes can have the distributive # factoring rule applied to them. state_one = MathyEnvState(problem="4x + y + 2x") transition = env.get_state_transition(state_one) assert is_terminal_transition(transition) is False # This is a terminal state because the nodes representing "4x + 2x" can # have the distributive factoring rule applied to them. state_two = MathyEnvState(problem="4x + 2x + y") transition = env.get_state_transition(state_two) assert is_terminal_transition(transition) is True ``` -------------------------------- ### ndarray Constructor Examples Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/time_step.md Illustrates the low-level `ndarray` constructor with two modes: one where `buffer` is None, and another where `buffer` is an object exposing the buffer interface. ```python >>> import numpy as np # First mode, buffer is None: >>> np.ndarray(shape=(2,2), dtype=float, order='F') array([[0.0e+000, 0.0e+000], # random [ nan, 2.5e-323]]) # Second mode: >>> np.ndarray((2,), buffer=np.array([1,2,3]), ... offset=np.int_().itemsize, ... dtype=int) # offset = 1*itemsize, i.e. skip first element array([2, 3]) ``` -------------------------------- ### MathyEnv Get Initial State Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Generates and returns the initial state for a new episode, along with problem details. Allows for optional parameters and printing of the initial problem. ```python MathyEnv.get_initial_state( self, params: Optional[mathy_envs.types.MathyEnvProblemArgs] = None, print_problem: bool = True, ) -> Tuple[mathy_envs.state.MathyEnvState, mathy_envs.types.MathyEnvProblem] ``` -------------------------------- ### Polynomial Simplification Examples Source: https://github.com/mathy/mathy_envs/blob/master/mathy_envs/envs/poly_simplify.md Demonstrates how like terms in polynomial expressions should be combined. Like terms share the same variable and exponent. ```python print('4x + 2y + 2x simplifies to 6x + 2y') print('23j + 7 + 12x + 2j simplifies to 25j + 7 + 12x') print('1.3j + 2j - 7 simplifies to 3.3j - 7') ``` -------------------------------- ### Complex Term Simplification Examples Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/complex_simplify.md Illustrates how complex algebraic terms should be simplified by combining coefficients and like variables. ```python 1x * 2x^1 simplifies to 2x^2 7j * y^1 * y^2 simplifies to 7j * y^3 ``` -------------------------------- ### Example Episode: Simplifying a Complex Expression Source: https://github.com/mathy/mathy_envs/blob/master/mathy_envs/envs/complex_simplify.md Demonstrates the step-by-step process an agent follows to simplify a given complex algebraic expression. ```mathy Input: 4a^4 * 5a^4 * 2b^4 mathy:4a^4 * 5a^4 * 2b^4 Steps: initial: 4a^4 * 5a^4 * 2b^4 constant arithmetic: 20a^4 * a^4 * 2b^4 variable multiplication: 20 * a^(4 + 4) * 2b^4 constant arithmetic: 20 * a^8 * 2b^4 commutative swap: (a^8 * 2b^4) * 20 commutative swap: (2b^4 * a^8) * 20 commutative swap: 20 * 2b^4 * a^8 constant arithmetic: 40b^4 * a^8 solution: 40b^4 * a^8 Solution: 40b^4 * a^8 mathy:40b^4 * a^8 ``` -------------------------------- ### Complex Term Simplification Examples Source: https://github.com/mathy/mathy_envs/blob/master/mathy_envs/envs/complex_simplify.md Illustrates how complex algebraic terms should be simplified by combining coefficients and like variables. ```mathy 1x * 2x^1 -> 2x^2 7j * y^1 * y^2 -> 7j * y^3 ``` -------------------------------- ### MathyEnv Get Next State Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Applies a given action to the current environment state and returns the resulting next state, the time step transition, and a descriptor of the change. ```python MathyEnv.get_next_state( self, env_state: mathy_envs.state.MathyEnvState, action: Union[int, numpy.int64, Tuple[int, int]], ) -> Tuple[mathy_envs.state.MathyEnvState, mathy_envs.time_step.TimeStep, mathy_core.rule.ExpressionChangeRule] ``` -------------------------------- ### Mathy Environment Win Condition Test Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/custom_episode_rewards.ipynb Tests the win condition of the custom Mathy environment. It sets up an initial problem, applies a rule that simplifies constants, and asserts that the transition is terminal and the reward is the custom win signal (20.0). ```python env = CustomEpisodeRewards() # Win by simplifying constants and yielding a single simple term form state = MathyEnvState(problem="(4 + 2) * x") expression = env.parser.parse(state.agent.problem) action = env.random_action(expression, ConstantsSimplifyRule) out_state, transition, _ = env.get_next_state(state, action) assert is_terminal_transition(transition) is True assert transition.reward == 20.0 assert out_state.agent.problem == "6x" ``` -------------------------------- ### NumPy ndarray Documentation Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/time_step.md Provides detailed information about NumPy's ndarray object, including its constructor, attributes, and usage examples. This is relevant as TimeStep likely uses ndarrays for its data. ```APIDOC ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None) An array object represents a multidimensional, homogeneous array of fixed-size items. An associated data-type object describes the format of each element in the array. Parameters: shape : tuple of ints Shape of created array. dtype : data-type, optional Any object that can be interpreted as a numpy data type. buffer : object exposing buffer interface, optional Used to fill the array with data. offset : int, optional Offset of array data in buffer. strides : tuple of ints, optional Strides of data in memory. order : {'C', 'F'}, optional Row-major (C-style) or column-major (Fortran-style) order. Attributes: T : ndarray Transpose of the array. data : buffer The array's elements, in memory. dtype : dtype object Describes the format of the elements in the array. flags : dict Dictionary containing information related to memory use. flat : numpy.flatiter object Flattened version of the array as an iterator. imag : ndarray Imaginary part of the array. real : ndarray Real part of the array. size : int Number of elements in the array. itemsize : int The memory use of each array element in bytes. nbytes : int The total number of bytes required to store the array data. ndim : int The array's number of dimensions. shape : tuple of ints Shape of the array. strides : tuple of ints The step-size required to move from one element to the next in memory. ctypes : ctypes object Class containing properties of the array needed for interaction with ctypes. base : ndarray If the array is a view into another array, that array is its `base`. See Also: array : Construct an array. zeros : Create an array, each element of which is zero. empty : Create an array, but leave its allocated memory unchanged. dtype : Create a data-type. numpy.typing.NDArray : An ndarray alias generic w.r.t. its dtype.type. Notes: There are two modes of creating an array using ``__new__``: 1. If `buffer` is None, then only `shape`, `dtype`, and `order` are used. 2. If `buffer` is an object exposing the buffer interface, then all keywords are interpreted. No ``__init__`` method is needed because the array is fully initialized after the ``__new__`` method. Examples: First mode, `buffer` is None: >>> np.ndarray(shape=(2,2), dtype=float, order='F') array([[0.0e+000, 0.0e+000], # random [ nan, 2.5e-323]]) Second mode: >>> np.ndarray((2,), buffer=np.array([1,2,3]), ... offset=np.int_().itemsize, ... dtype=int) # offset = 1*itemsize, i.e. skip first element array([2, 3]) ``` -------------------------------- ### Mathy Environment Lose Condition Test Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/custom_episode_rewards.ipynb Tests the lose condition of the custom Mathy environment. It sets up a problem with limited moves, applies a rule, and asserts that the transition is terminal and the reward is the custom lose signal (-20.0). ```python # Lose by applying a rule with only 1 move remaining state = MathyEnvState(problem="2x + (4 + 2) + 4x", max_moves=1) expression = env.parser.parse(state.agent.problem) action = env.random_action(expression, ConstantsSimplifyRule) out_state, transition, _ = env.get_next_state(state, action) assert is_terminal_transition(transition) is True assert transition.reward == -20.0 assert out_state.agent.problem == "2x + 6 + 4x" ``` -------------------------------- ### MathyEnv Class Initialization Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Initializes the MathyEnv with various parameters to configure the math solving game, including rules, move limits, and reward settings. ```python MathyEnv( self, rules: Optional[List[mathy_core.rule.BaseRule]] = None, max_moves: int = 20, verbose: bool = False, invalid_action_response: Literal['raise', 'penalize', 'terminal'] = 'raise', reward_discount: float = 0.99, max_seq_len: int = 128, previous_state_penalty: bool = True, preferred_term_commute: bool = False, ) ``` -------------------------------- ### MathyEnv Get Token at Index Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Retrieves the mathematical token located at a specific index from the left of a given expression. ```python MathyEnv.get_token_at_index( self, expression: mathy_core.expressions.MathExpression, index: int, ) -> Optional[mathy_core.expressions.MathExpression] ``` -------------------------------- ### Using CustomTimestepRewards Environment Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/custom_timestep_rewards.ipynb Demonstrates how to instantiate and use the CustomTimestepRewards environment. It shows parsing a problem, creating an initial state, and applying actions to observe rewards. ```python env = CustomTimestepRewards() problem = "4x + y + 2x" expression = env.parser.parse(problem) state = MathyEnvState(problem=problem) action = env.random_action(expression, rules.AssociativeSwapRule) _, transition, _ = env.get_next_state(state, action,) # Expect positive reward assert transition.reward > 0.0 _, transition, _ = env.get_next_state( state, env.random_action(expression, rules.CommutativeSwapRule), ) # Expect neagative reward assert transition.reward < 0.0 ``` -------------------------------- ### PolyCombineInPlace Class Initialization Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/poly_combine_in_place.md Initializes the PolyCombineInPlace environment. It takes an optional list of operations and arbitrary keyword arguments. ```python PolyCombineInPlace(self, ops: Optional[List[str]] = None, kwargs: Any) ``` -------------------------------- ### MathyEnv Get State Transition Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Calculates and returns the time step transition value for the current environment state. ```python MathyEnv.get_state_transition( self, env_state: mathy_envs.state.MathyEnvState, ) -> mathy_envs.time_step.TimeStep ``` -------------------------------- ### MathyEnv Get Rewarding Actions Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Retrieves a list of action types that result in a positive reward when executed by the agent. ```python MathyEnv.get_rewarding_actions( self, state: mathy_envs.state.MathyEnvState, ) -> List[Type[mathy_core.rule.BaseRule]] ``` -------------------------------- ### PolyHaystackLikeTerms Class Initialization Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/poly_haystack_like_terms.md Initializes the PolyHaystackLikeTerms environment. It takes keyword arguments for configuration. ```python PolyHaystackLikeTerms(self, kwargs: Any) ``` -------------------------------- ### MathyEnv Get Penalizing Actions Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Retrieves a list of action types that result in a negative reward when executed by the agent. ```python MathyEnv.get_penalizing_actions( self, state: mathy_envs.state.MathyEnvState, ) -> List[Type[mathy_core.rule.BaseRule]] ``` -------------------------------- ### MathyEnv Get Environment Namespace Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Returns a unique, dot-namespaced string that identifies the current environment, useful for registration and referencing. ```python MathyEnv.get_env_namespace(self) -> str ``` -------------------------------- ### PolySimplify API Documentation Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/poly_simplify.md Provides details on the PolySimplify environment for polynomial simplification, including its constructor, problem generation function, and transition function. ```APIDOC PolySimplify: __init__(self, ops: Optional[List[str]] = None, kwargs: Any) A Mathy environment for simplifying polynomial expressions. NOTE: This environment only generates polynomial problems with addition operations. Subtraction, Multiplication and Division operators are excluded. This is a good area for improvement. problem_fn(self, params: mathy_envs.types.MathyEnvProblemArgs) -> mathy_envs.types.MathyEnvProblem Given a set of parameters to control term generation, produce a polynomial problem with (n) total terms divided among (m) groups of like terms. A few examples of the form: f(n, m) = p - (3, 1) = "4x + 2x + 6x" - (6, 4) = "4x + v^3 + y + 5z + 12v^3 + x" - (4, 2) = "3x^3 + 2z + 12x^3 + 7z" transition_fn(self, env_state: mathy_envs.state.MathyEnvState, expression: mathy_core.expressions.MathExpression, features: mathy_envs.state.MathyObservation) -> Optional[mathy_envs.time_step.TimeStep] If there are no like terms. ``` -------------------------------- ### MathyEnv Get Agent Actions Count Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Returns the total count of all possible actions that can be taken within the current environment state. ```python MathyEnv.get_agent_actions_count( self, env_state: mathy_envs.state.MathyEnvState, ) -> int ``` -------------------------------- ### BinomialDistribute Class Initialization Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/binomial_distribute.md Initializes the BinomialDistribute environment with configurable parameters for rule sets, move limits, verbosity, action response, reward discounting, sequence length, state penalties, and term commutation preference. ```python BinomialDistribute( self, rules: Optional[List[mathy_core.rule.BaseRule]] = None, max_moves: int = 20, verbose: bool = False, invalid_action_response: Literal['raise', 'penalize', 'terminal'] = 'raise', reward_discount: float = 0.99, max_seq_len: int = 128, previous_state_penalty: bool = True, preferred_term_commute: bool = False, ) ``` -------------------------------- ### PolyCommuteLikeTerms Class Initialization Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/poly_commute_like_terms.md Initializes the PolyCommuteLikeTerms environment. It takes an optional list of operations and other keyword arguments for configuration. ```python PolyCommuteLikeTerms(self, ops: Optional[List[str]] = None, kwargs: Any) ``` -------------------------------- ### MathyEnv Get Lose Signal Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Calculates and returns the reward value associated with failing to complete an episode, allowing for problem-type specific penalties. ```python MathyEnv.get_lose_signal( self, env_state: mathy_envs.state.MathyEnvState, ) -> float ``` -------------------------------- ### Mathy Environment Dependencies Source: https://github.com/mathy/mathy_envs/blob/master/website/requirements.txt Lists the Python packages and system dependencies required for the Mathy project environments. This includes visualization tools, documentation generators, and core libraries. ```shell # System dependencies graphviz # Profiling snakeviz # Documentation Generation mkdocs mkdocs-material>=9.5.2,<10.0.0 mkdocs-git-revision-date-localized-plugin>=1.2.1,<2.0.0 mkdocs-material[imaging] mkdocs-git-committers-plugin markdown-include mkdocs-minify-plugin ruamel.yaml # Notebook Conversion nbformat # Mathy Core Components mathy_pydoc>=0.7.18 mathy_core # Local Development (from GitHub) git+https://github.com/mathy/mathy_mkdocs.git # TODO: remove this when published # Reinforcement Learning Environment gym ``` -------------------------------- ### PolySimplifyBlockers Initialization Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/poly_simplify_blockers.md Initializes the PolySimplifyBlockers environment. It takes optional operations and keyword arguments. The environment is designed to simplify polynomials by commuting and combining like terms. ```python PolySimplifyBlockers(self, ops: Optional[List[str]] = None, kwargs: Any) ``` -------------------------------- ### MathyEnvState Action and Observation Methods Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/state.md Includes methods for managing the environment's progression and generating observations. `get_out_state` calculates the next state after an action, `get_problem_hash` returns a hashed representation of the problem, and `to_observation` converts the state into a featurized observation. ```python MathyEnvState.get_out_state( self, problem: str, action: Tuple[int, int], moves_remaining: int, ) -> 'MathyEnvState' ``` ```python MathyEnvState.get_problem_hash(self) -> List[int] ``` ```python MathyEnvState.to_observation( self, move_mask: Optional[List[List[int]]] = None, hash_type: Optional[List[int]] = None, parser: Optional[mathy_core.parser.ExpressionParser] = None, normalize: bool = True, max_seq_len: Optional[int] = None, ) -> mathy_envs.state.MathyObservation ``` -------------------------------- ### PolyHaystackLikeTerms API Documentation Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/poly_haystack_like_terms.md API documentation for the PolyHaystackLikeTerms environment, detailing its core components and their functionalities. ```APIDOC PolyHaystackLikeTerms: __init__(self, kwargs: Any) Initializes the environment with provided keyword arguments. transition_fn(self, env_state: mathy_envs.state.MathyEnvState, expression: mathy_core.expressions.MathExpression, features: mathy_envs.state.MathyObservation) -> Optional[mathy_envs.time_step.TimeStep] Processes the environment state, expression, and features to determine the next time step. Assumes like terms are siblings in the expression tree. ``` -------------------------------- ### Gymnasium Integration for Mathy Envs Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/index.md Demonstrates how to import and register Mathy environments with Gymnasium for use in reinforcement learning tasks. This allows agents to interact with the math environments using the Gymnasium API. ```python from mathy_envs.gym import register_mathy_envs register_mathy_envs() # Now you can create mathy environments using gymnasium.make() # Example: # import gymnasium as gym # env = gym.make('mathy-easy-v0') ``` -------------------------------- ### BinomialDistribute transition_fn Method Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/binomial_distribute.md Handles the state transitions within the environment. It returns a TimeStep if there are no like terms remaining in the expression, indicating a potential end state or a step towards simplification. ```python BinomialDistribute.transition_fn( self, env_state: mathy_envs.state.MathyEnvState, expression: mathy_core.expressions.MathExpression, features: mathy_envs.state.MathyObservation, ) -> Optional[mathy_envs.time_step.TimeStep] ``` -------------------------------- ### MathyEnv Get Actions for Node Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Generates a valid action mask for a given mathematical expression and an optional list of rules. The mask indicates which actions are valid for specific nodes. ```python MathyEnv.get_actions_for_node( self, expression: mathy_core.expressions.MathExpression, rule_list: Optional[List[Type[mathy_core.rule.BaseRule]]] = None, ) -> List[List[int]] ``` -------------------------------- ### MathyEnvStateStep Constructor and Attributes Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/state.md Captures summarized environment state for a previous timestep. It stores the action taken (action) and the raw input text (raw) for historical context. ```python MathyEnvStateStep(self, args, kwargs) ``` -------------------------------- ### MathyAgentState Constructor Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/state.md Initializes the state related to an agent for a given environment state. It takes moves remaining, the problem string, problem type, and optionally reward and history. ```python MathyAgentState( self, moves_remaining: int, problem: str, problem_type: str, reward: float = 0.0, history: Optional[List[mathy_envs.state.MathyEnvStateStep]] = None, ) ``` -------------------------------- ### MathyEnv Get Valid Moves Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Returns a 2D list indicating the valid moves for the current state. The list is structured by rules and sequence length, marking valid nodes with 1 and invalid with 0. ```python MathyEnv.get_valid_moves( self, env_state: mathy_envs.state.MathyEnvState, ) -> List[List[int]] ``` -------------------------------- ### Import Mathy Environment Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/poly_simplify.md Imports the Poly Simplify environment from the mathy_envs library. ```python import mathy_envs.envs.poly_simplify ``` -------------------------------- ### ComplexSimplify problem_fn Method Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/complex_simplify.md Generates a complex term that requires simplification, given specific parameters to control term generation. Examples of generated terms include '4x * 2y^2 * 7q' and 'x * 2y^7 * 8z * 2x'. ```python ComplexSimplify.problem_fn( self, params: mathy_envs.types.MathyEnvProblemArgs, ) -> mathy_envs.types.MathyEnvProblem ``` -------------------------------- ### Custom Episode Rewards in Mathy Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/snippets/envs/custom_episode_rewards.ipynb Defines a custom environment 'CustomEpisodeRewards' that inherits from Mathy's 'PolySimplify' environment. It overrides the 'get_win_signal' and 'get_lose_signal' methods to provide custom reward values (20.0 for win, -20.0 for lose). ```python # This file is generated from a Mathy (https://mathy.ai) code example. !pip install mathy --upgrade """Environment with user-defined terminal rewards""" from mathy_core.rules import ConstantsSimplifyRule from mathy_envs import MathyEnvState, envs, is_terminal_transition class CustomEpisodeRewards(envs.PolySimplify): def get_win_signal(self, env_state: MathyEnvState) -> float: return 20.0 def get_lose_signal(self, env_state: MathyEnvState) -> float: return -20.0 ``` -------------------------------- ### PolyGroupLikeTerms Constructor Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/envs/poly_grouping.md Initializes the PolyGroupLikeTerms environment for grouping polynomial terms. It allows configuration of rules, move limits, verbosity, action response, reward discount, sequence length, and state penalties. ```python PolyGroupLikeTerms( self, rules: Optional[List[mathy_core.rule.BaseRule]] = None, max_moves: int = 20, verbose: bool = False, invalid_action_response: Literal['raise', 'penalize', 'terminal'] = 'raise', reward_discount: float = 0.99, max_seq_len: int = 128, previous_state_penalty: bool = True, preferred_term_commute: bool = False, ) ``` -------------------------------- ### MathyEnv Core Rules Source: https://github.com/mathy/mathy_envs/blob/master/website/docs/api/env.md Retrieves the core agent actions (rules) available within the Mathy environment. Optionally considers term commutation preference. ```python MathyEnv.core_rules( preferred_term_commute: bool = False, ) -> List[mathy_core.rule.BaseRule] ```