How to use the mathy.agent.training.problems.rand_var function in mathy

To help you get started, we’ve selected a few mathy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github justindujardin / mathy / mathy / agent / curriculum / combine_like_terms_2.py View on Github external
def two_variable_terms():
    variable = rand_var()
    problem = "{}{} + {}{}".format(rand_int(), variable, rand_int(), variable)
    return problem, 2
github justindujardin / mathy / mathy / agent / curriculum / level1.py View on Github external
def move_around_blockers_one(number_blockers):
    # two like terms separated by (n) blocker terms, e.g. 2 ~ "4x + (y + f) + x"
    var = rand_var()
    complexity = 2 + number_blockers
    blockers = get_blocker(number_blockers, [var])
    problem = "{}{} + {} + {}{}".format(maybe_int(), var, blockers, maybe_int(), var)
    return problem, complexity
github justindujardin / mathy / mathy / agent / curriculum / level1.py View on Github external
def combine_terms_after_commuting(
    min_terms=5, max_terms=8, commute_blockers=1, easy=True, powers=False
):
    """A problem with a bunch of terms that have no matches, and a single 
    set of two terms that do match, but are separated by one other term.

    The challenge is to commute the terms to each other and simplify in 
    only a few moves. 
    
    Example:  "4y + 12j + 73q + 19k + 13z + 24x + 56l + 12x  + 43n + 17j"
                                             ^-----------^  
    """
    total_terms = random.randint(min_terms, max_terms)
    num_noise_terms = total_terms - 2
    var = rand_var()
    noise_vars = get_rand_vars(num_noise_terms, [var])
    power_chance = 80 if powers == True else 0
    power = maybe_power(power_chance)

    # Build up the blockers to put between the like terms
    blockers = []
    for i in range(commute_blockers):
        current = noise_vars.pop()
        blockers.append(f"{maybe_int()}{current}{maybe_power(power_chance)}")

    focus_chunk = f"{maybe_int()}{var}{power} + {' + '.join(blockers)} + {maybe_int()}{var}{power}"
    # About half of the time focus the agent by grouping the subtree for them
    if rand_bool(50 if easy else 10):
        focus_chunk = f"({focus_chunk})"

    out_terms = []
github justindujardin / mathy / mathy / agent / curriculum / combine_like_terms_2.py View on Github external
def two_variable_terms_separated_by_constant():
    variable = rand_var()
    problem = "{}{} + {} + {}{}".format(
        rand_int(), variable, const, rand_int(), variable
    )
    return problem, 3
github justindujardin / mathy / mathy / agent / curriculum / level1.py View on Github external
few moves count when combined with a very small number of maximum moves.

    The hope is that by focusing the agent on selecting the right moves inside of a 
    ridiculously large expression it will learn to select actions to combine like terms
    invariant of the sequence length.
    
    Example:
      "4y + 12j + 73q + 19k + 13z + 56l + (24x + 12x)  + 43n + 17j"
      max_turns=3  actions=[DistributiveFactorOut, ConstantArithmetic]

    NOTE: we usually add one more move than may strictly be necessary to help with 
    exploration where we inject Dirichlet noise in the root tree search node.
    """

    total_terms = random.randint(min_terms, max_terms)
    var = rand_var()
    power_chance = 80 if powers == True else 0
    power = maybe_power(power_chance)
    focus_chunk = f"{maybe_int()}{var}{power} + {maybe_int()}{var}{power}"
    if easy:
        focus_chunk = f"({focus_chunk})"
    num_noise_terms = total_terms - 2
    noise_vars = get_rand_vars(num_noise_terms, [var])

    out_terms = []

    # We take the larger value for the left side to push the terms
    # that have to be matched to the right side of expression. This is
    # so that the model cannot use its existing knowledge about distributive
    # factoring on smaller problems to solve this problem.
    right_num, left_num = split_in_two_random(num_noise_terms)
    for i in range(left_num):
github justindujardin / mathy / mathy / agent / curriculum / combine_like_terms.py View on Github external
def two_variable_terms():
    variable = rand_var()
    problem = "{}{} + {}{}".format(rand_int(), variable, rand_int(), variable)
    return problem, 2
github justindujardin / mathy / mathy / agent / curriculum / combine_like_terms.py View on Github external
def two_variable_terms_separated_by_constant():
    variable = rand_var()
    problem = "{}{} + {} + {}{}".format(
        rand_int(), variable, const, rand_int(), variable
    )
    return problem, 3