Entropy-controlled contexts in Python

Overview

Build Status PyPI version Linux PyPI status

Twitter Follow Twitter URL

Python module ordered

ordered module is the opposite to random - it maintains order in the program.

import random 
x = 5
def increase():
    global x
    x += 7
def decrease():
    global x
    x -= 2

while x != 22:  
    random.choice([increase, decrease])()  
    # takes long time to exit ...

vs.

import random, ordered
x = 5
def increase():
    global x
    x += 7
def decrease():
    global x
    x -= 2

with ordered.orderedcontext(): # entropy-controlled context
    while x != 22: 
        random.choice([increase, decrease])()  
    # exits immediately with correct result
pass 

Ordered contexts are environments of controlled entropy. Contexts allow you to control which portions of the program will be guaranteed to exit with minimum state-changing steps. Raising any exceptions is also avoided by providing the correct "anti-random" choice() results.

Usage

ordered is a Python 3.8+ module. Use cases include automated decisionmaking, manufacturing control, robotics, automated design, automated programming and others.

You describe the world as Python objects and state-modifying methods. Defining an entropy-controlled context allows you to set up a goal for the system to satisfy all constraints and reach the desired state.

To define constraints you add assert statements in your code and inside ordered context. Then you add a function-calling loop while : random.choice()(random.choice()) . To exit the context the engine will have to call correct functions with correct arguments and end up with a staisfying state (see examples below).

Requirements

  • Linux (tested on Ubuntu 20.04+)
  • Python 3.8 in virtualenv
  • Recommended: PyPy compatible with Python 3.7+, installed globally.
# In Python3.8 virtualenv on Linux:
$ pip install ordered

Entropy Context Objects

# ... normal python code
with ordered.orderedcontext():  
    # ... entropy-controlled context, guaranteed to exit without exceptions
# ... normal python code
  • ordered.orderedcontext()

    Return a context manager and enter the context. SchedulingError will be raised if exit is not possible.

    Inside ordered context functions random.choice and ordered.choice are equivalent and no randomness is possible. If choice() is called without parameters then gc.get_objects() (all objects in Python heap) is considered by default.

    Optional returned context object allows to set parameters and limits such as timeout and max_states.

    Warning: not all Python features are currently supported and thus ordered might fail with internal exception. In this case a rewrite of user code is needed to remove the usage of unsupported features (such as I/O, lists and for loops.)

    Warning: ordered requires all entropy-controlled code to be type-hinted.

# ...
def decrease():
    global x
    assert x > 25  # when run inside context this excludes cases when x <= 25
                   # thus increasing amount of overall steps needed to complete
    x -= 2
# ...
with ordered.orderedcontext(): # entropy-controlled context
    while x < 21:  # exit if x >= 21
        random.choice([increase, decrease])()  
    assert x < 23  # only x == 21 or 22 matches overall

ordered.choice()

  • ordered.choice(objects=None)

    Choose and return the object that maintains maximum order in the program (minimum entropy). Any exception increases entropy to infinity so choices leading to exceptions will be avoided. Inside the entropy controlled context, random.choice is equivalent to ordered.choice (and also random.choices in the sense that it may return any amount of parameters when used as argument-generator in choice(*choice())).

    objects is a list of objects to choose from. If objects is None then gc.get_objects() is assumed by default.

    Warning: current implementation of while ... ordered loop is hard-coded to the form shown in examples. while loops with other statements than a single-line choice() are not supported. Add your code to other parts of context and/or functions and methods in your program

ordered.side_effect(lambda: )

  • ordered.side_effect(lamdba=[lambda function])

    Execute the supplied lambda function as a side-effect avoiding the compilation and subsequent effect analysis by ordered. This is useful when I/O is easier schdeuled right within the entropy-controlled part of the program or when you know that the code to be executed has no useful effect on the state of the problem of interest.

    side_effect may only be used when importred into global namespace using from ordered import side_effect

    from ordered import side_effect
    
    def move(t: Truck, l: Location):
        "Move truck to any adjacent location"
        assert l in t.location.adjacent
        t.locaiton = l
        t.distance += 1
        side_effect(lambda: print(f"This {__name__} code can have any Python construct and is not analysed. Current value is {t.distance}"))

Examples:

Object Oriented Code

Preferred way of implementing software models with ordered is object-oriented:

import ordered

class MyVars:
    x: int
    steps: int
    def __init__(self) -> None:
        self.x = 0
        self.steps = 0

    def plus_x(self):
        self.x += 3
        self.count_steps()

    def minus_x(self):
        self.x -= 2
        self.count_steps()
    
    def count_steps(self):
        self.steps += 1

m = MyVars()
m.x = 5
with ordered.orderedcontext():
    while m.x != 12:  
        ordered.choice()()  

print("Steps:", steps)

Pouring problem

A classic bottle pouring puzzle. You are in the possession of two bottles, one with a capacity of 3 litres and one with a capacity of 5 litres. Next to you is an infinitely large tub of water. You need to measure exactly 4 litres in one of the bottles. You are only allowed to entirely empty or fill the bottles. You can't fill them partially since there is no indication on the bottles saying how much liquid is in them. How do you measure exactly 4 litres?

from ordered import orderedcontext, choice
class Bottle:
    volume: int
    fill: int
    def __init__(self, volume: int):
        self.volume = volume
        self.fill = 0
    def fill_in(self):
        self.fill += self.volume
        assert self.fill == self.volume
    def pour_out(self, bottle: "Bottle"):
        assert self != bottle
        can_fit = bottle.volume - bottle.fill
        sf = self.fill
        bf = bottle.fill
        if self.fill <= can_fit:
            bottle.fill += self.fill
            self.fill = 0
            assert self.fill == 0
            assert bottle.fill == bf + sf
        else:
            bottle.fill += can_fit
            self.fill -= can_fit
            assert bottle.fill == bottle.volume
            assert self.fill == sf - can_fit
    def empty(self):
        self.fill = 0
b1 = Bottle(3)
b2 = Bottle(5)
with orderedcontext():
  while b2.fill != 4: 
      choice([Bottle])()
pass

NOTE: Be careful with importing from a module into global namespace and using choice()() without parameters in global scope. Current implementation load all global objects including the orderedcontext and choice and cause an error

Learning a function

ordered can be used

from ordered import choice, orderedcontext
from dataclasses import dataclass 

@dataclass
class Point:
   x: int
   y: int
   
data = [Point(1,1), Point(2,4), Point(3,9)]

# TODO: create_function creates a nonrandom function out of Node objects with `ordered.choice`
# TODO: run_function runs a node tree with a value and returns result
    
with orderedcontext():
    f = create_function()
    for point in data:
        assert run_function(f, point.x) == point.y
# context exit guarantees that create_function() constructs a correct function to describe input

# TODO: approximate function learning example

Work-in-progress functions

ordered.relaxedcontext()

Guaranteed to find an exit. Modifies the program if required.

Method ordered.def(heap_in_out: List)

Defines a function from a list of input and output heaps. The more examples of heaps are supplied, the better is the function.

Status

Although the system is in use by several industry organizations, ordered is under heavy development. Expect rapid changes in language support, performance and bugs.

Limitations

Python Language

Overall we have a relatively complete support of 'basic' use of object-oriented programming style. However, there are some hard limitaions and work-in-progress items that are yet to be documented.

Try to avoid multiline code as we have several places where line continuation may break during compilation.

Built-ins support is minimal. No I/O can be executed except for in explicit side_effect() calls.

None of the "ordered data structures" are supported: this includes list, dict and tuple. Use set or create your own data structures based on objects and classes.

Loops are not supported, including while and for besides the main while..choice() loop as described above - define your problem by creating functions that can be iteratively called by while.. choice() to overcome this.

Support of missing features is a current work in progress.

Integer Math

Math implementation is simple and works up to count 20-50 depedning on available resources. Future development includes switching to register-based math and monotonic-increase heuristics to support any numbers.

Symbolic Execution Performance

Current implementaion of Python code compilation is naive and doesn't scale well. The simpler your code, the faster it will compile. Future development includes implementing smarter symboic execution heuristics, pre-calculated database and statistical methods.

Model Universality

Current model can efficiently handle a limited set of problem classes and might require significantly more resources than would be needed with a more complete model. HyperC team provides more complete models for specific industry per request. Future development includes adding a universal pruning based on statistical methods as amount of data available to HyperC team grows.

Science behind ordered

ordered is based on translating a Python program to AI planning problem and uses a customized fast-downward as a backend. Additionally, we're implementing machine learning and pre-computed matrices on various levels to vastly improve performance on larger problems.

Contributing

For any questions and inquries please feel free contact Andrew Gree, [email protected].

Support

Module ordered is maintained by HyperC team, https://hyperc.com (CriticalHop Inc.) and is implemented in multiple production envorinments.

Investor Relations

HyperC is fundraising! Please contact at [email protected].

You might also like...
Simple yet flexible natural sorting in Python.

natsort Simple yet flexible natural sorting in Python. Source Code: https://github.com/SethMMorton/natsort Downloads: https://pypi.org/project/natsort

A Python utility belt containing simple tools, a stdlib like feel, and extra batteries. Hashing, Caching, Timing, Progress, and more made easy!
A Python utility belt containing simple tools, a stdlib like feel, and extra batteries. Hashing, Caching, Timing, Progress, and more made easy!

Ubelt is a small library of robust, tested, documented, and simple functions that extend the Python standard library. It has a flat API that all behav

Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything.

Retrying Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just

Pampy: The Pattern Matching for Python you always dreamed of.
Pampy: The Pattern Matching for Python you always dreamed of.

Pampy: Pattern Matching for Python Pampy is pretty small (150 lines), reasonably fast, and often makes your code more readable and hence easier to rea

Hot reloading for Python
Hot reloading for Python

Hot reloading for Python

isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type.
isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type.

isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type. It provides a command line utility, Python library and plugins for various editors to quickly sort all your imports.

Functional UUIDs for Python.

🏷️FUUID stands for Functional Universally Unique IDentifier. FUUIDs are compatible with regular UUIDs but are naturally ordered by generation time, collision-free and support succinct representations such as raw binary and base58-encoded strings.

✨ Une calculatrice totalement faite en Python par moi, et en français.
✨ Une calculatrice totalement faite en Python par moi, et en français.

Calculatrice ❗ Une calculatrice totalement faite en Python par moi, et en français. 🔮 Voici une calculatrice qui vous permet de faire vos additions,

✨ Un juste prix totalement fait en Python par moi, et en français.
✨ Un juste prix totalement fait en Python par moi, et en français.

Juste Prix ❗ Un juste prix totalement fait en Python par moi, et en français. 🔮 Avec l'utilisation du module "random", j'ai pu faire un choix aléatoi

Comments
  • Can't resolve globals within ordered context

    Can't resolve globals within ordered context

    See test file at tests/test_global_var.py

    import ordered
    import pytest
    
    x = 0
    def inc():
        global x
        x += 1
    
    def run_inc():
        global x
        with ordered.orderedcontext():
            while x != 1:
                ordered.choice()()
        pass  # required due to current Python limitation
    
    
    def test_endofframe():
        "Setting the breakpoint to line 13 should work"
        run_inc()
    
    
    bug 
    opened by grandrew 0
  • Resolve class methods from context-local variables

    Resolve class methods from context-local variables

    In this situation:

    import pytest, gc
    import ordered, random
    
    __author__ = "Andrew Gree"
    __copyright__ = "CriticalHop Inc."
    __license__ = "MIT"
    
    
    class MyVars:
        x: int
        steps: int
        def __init__(self) -> None:
            self.x = 0
            self.steps = 0
    
        def plus_x(self):
            self.x += 3
            self.count_steps()
    
        def minus_x(self):
            self.x -= 2
            self.count_steps()
        
        def count_steps(self):
            self.steps += 1
    
    @pytest.mark.skip(reason="TODO")
    def test_partial_context_oo_full():
        m = MyVars()
        m.x = 5
        with ordered.orderedcontext():
            # exit ordered context without exceptions with minimum steps
            while m.x != 12:  
                ordered.choice()()  
        assert m.x == 12
        assert m.steps == 4
    

    we're calling choice() with no parameters. This code fails as no methods for m are generated.

    A solution needed to have a logically expected behaviour:

    1. all variables explicitly used in the context are scanned for their class' methods
    2. if choice() is selecting all functions from heap then it's huge as Python always has a lot of stuff loaded. Maybe choice() must actually scan this module's globals() for functions and methods?
    bug question 
    opened by grandrew 0
Owner
HyperC
Production-ready artificial Intelligence-based scheduling and planning ("AI planning")
HyperC
✨ Voici un code en Python par moi, et en français qui permet d'exécuter du Javascript en Python.

JavaScript In Python ❗ Voici un code en Python par moi, et en français qui permet d'exécuter du Javascript en Python. ?? Une vidéo pour vous expliquer

MrGabin 4 Mar 28, 2022
Simple python module to get the information regarding battery in python.

Battery Stats A python3 module created for easily reading the current parameters of Battery in realtime. It reads battery stats from /sys/class/power_

Shreyas Ashtamkar 5 Oct 21, 2022
ticktock is a minimalist library to view Python time performance of Python code.

ticktock is a minimalist library to view Python time performance of Python code.

Victor Benichoux 30 Sep 28, 2022
Python @deprecat decorator to deprecate old python classes, functions or methods.

deprecat Decorator Python @deprecat decorator to deprecate old python classes, functions or methods. Installation pip install deprecat Usage To use th

null 12 Dec 12, 2022
A python package containing all the basic functions and classes for python. From simple addition to advanced file encryption.

A python package containing all the basic functions and classes for python. From simple addition to advanced file encryption.

PyBash 11 May 22, 2022
Find dependent python scripts of a python script in a project directory.

Find dependent python scripts of a python script in a project directory.

null 2 Dec 5, 2021
A functional standard library for Python.

Toolz A set of utility functions for iterators, functions, and dictionaries. See the PyToolz documentation at https://toolz.readthedocs.io LICENSE New

null 4.1k Dec 30, 2022
Python Classes Without Boilerplate

attrs is the Python package that will bring back the joy of writing classes by relieving you from the drudgery of implementing object protocols (aka d

The attrs Cabal 4.6k Jan 6, 2023
🔩 Like builtins, but boltons. 250+ constructs, recipes, and snippets which extend (and rely on nothing but) the Python standard library. Nothing like Michael Bolton.

Boltons boltons should be builtins. Boltons is a set of over 230 BSD-licensed, pure-Python utilities in the same spirit as — and yet conspicuously mis

Mahmoud Hashemi 6k Jan 4, 2023
Retrying library for Python

Tenacity Tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just

Julien Danjou 4.3k Jan 5, 2023