Skip to main content

Describe and manipulate programs as Meta-Algorithms in Python.

Project description

Meta Algorithm in Python (MetalgPy)

What if we could write a program that generates programs? Inspired by Automated Machine Learning research such as PyGlove.

:warning: Experimental: Contributions are welcome!

Install

pip install metalgpy

Example

A simple but detailed example:

import metalgpy as mpy

# the @mpy.meta decorator transform an original python code 
# into a meta-program. f is now symbolizing the original python code
@mpy.meta
def f(x):
    return x

# program is a symbol representing the call to f (original python code)
# where the input is a symbol representing a variable List (categorical decision variable)
program = f(mpy.List([0,1,2,3,4]))
print("Program: ", program, end="\n\n")

# the choice method returns the variable symbols of the symbolized program
choices = program.choice()
print("Variable Space: ", choices)

# mpy.sample(n, program) generates clones of the symbolized program
for sample_program in mpy.sample(5, program):

    print("\n ** new program **")

    # we iterate over all variables of the variable space and randomly sample each of them
    choice = [v.sample() for v in choices]
    print("choice: ", choice)

    # we freeze the sampled program with a choice for each variable
    sample_program.freeze(choice)
    print("frozen program: ", sample_program)

    # we can now evaluate the program
    res = sample_program.evaluate()
    print("evaluation: ", res)

gives the following output:

Program:  f(List(0, 1, 2, 3, 4))

Variable Space:  [List(0, 1, 2, 3, 4)]

 ** new program **
choice:  [3]
frozen program:  f(3)
evaluation:  3

 ** new program **
choice:  [4]
frozen program:  f(4)
evaluation:  4

 ** new program **
choice:  [3]
frozen program:  f(3)
evaluation:  3

 ** new program **
choice:  [2]
frozen program:  f(2)
evaluation:  2

 ** new program **
choice:  [4]
frozen program:  f(4)
evaluation:  4

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metalgpy-0.0.1.tar.gz (14.3 kB view hashes)

Uploaded Source

Built Distribution

metalgpy-0.0.1-py2.py3-none-any.whl (11.1 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page