Package: ordered
Python moduleorderedorderedmodule is the opposite torandom- it maintains order in the program.importrandomx=5defincrease():globalxx+=7defdecrease():globalxx-=2whilex!=22:random.choice([increase,decrease])()# takes long time to exit ...vs.importrandom,orderedx=5defincrease():globalxx+=7defdecrease():globalxx-=2withordered.orderedcontext():# entropy-controlled contextwhilex!=22:random.choice([increase,decrease])()# exits immediately with correct resultUsageorderedis a Python 3.8+ module. Usecases include automated decisionmaking, manufacturing control, robotics, automated design, automated programming and others.You describe the problem in Python language formalism in form of functions and methods that model some business process - these methods modify the state of the world. By defining the exit criteria of entropy-controlled context you ask the system to reach the goal state by applying available functions and methods as many times as needed.orderedwill automatically decide which objects available in memory to use with each call.pip install orderedHaving pypy3 installed on the system is recommended for performance and will be automatically detected and used.Entropy Context ObjectsOrdered 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.# ... normal python codewithordered.orderedcontext():# ... entropy-controlled context, guaranteed to exit without exceptions# ... normal python codeordered.orderedcontext()Return a context manager and enter the context.SchedulingErrorwill be raised if exit is not possible.Inside ordered context functionsrandom.choiceandordered.choiceare equivalent and no randomness is possible. Ifchoice()is called without parameters thengc.get_objects()(all objects in Python heap) is considered by default.Optional returned context object allows to set parameters and limits such astimeoutandmax_states.Warning:not all Python features are currently supported and thusorderedmight 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:orderedrequires all entropy-controlled code to be type-hinted.# ...defdecrease():globalxassertx>25# when run inside context this excludes cases when x <= 25# thus increasing amount of overall steps needed to completex-=2# ...withordered.orderedcontext():# entropy-controlled contextwhilex<21:# exit if x >= 21random.choice([increase,decrease])()assertx<23# only x == 21 or 22 matches overallordered.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.choiceis equivalent toordered.choice.objectsis a list of objects to choose from. IfobjectsisNonethengc.get_objects()is assumed by default.Warning:current implementation ofwhile ... orderedloop is hard-coded to the form shown in examples.whileloops with other statements than a single-linechoice()are not supported. Add your code to other parts of context and/or functions and methods in your programordered.side_effect(lambda: <code>)ordered.side_effect(lamdba=[lambda function])Execute the supplied lambda function as a side-effect avoiding the compilation and subsequent effect analysis byordered. 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 usingfrom ordered import side_effectfromorderedimportside_effectdefmove(t:Truck,l:Location):"Move truck to any adjacent location"assertlint.location.adjacentt.locaiton=lt.distance+=1side_effect(lambda:print(f"This{__name__}code can have any Python construct and is not analysed. Current value is{t.distance}"))Examples:Object Oriented CodePreferred way of implementing software models withorderedis object-oriented:importorderedclassMyVars:x:intsteps:intdef__init__(self)->None:self.x=0self.steps=0defplus_x(self):self.x+=3self.count_steps()defminus_x(self):self.x-=2self.count_steps()defcount_steps(self):self.steps+=1m=MyVars()m.x=5withordered.orderedcontext():whilem.x!=12:ordered.choice()()print("Steps:",steps)Pouring problemA 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?fromorderedimportorderedcontext,choiceclassBottle:volume:intfill:intdef__init__(self,volume:int):self.volume=volumeself.fill=0deffill_in(self):self.fill+=self.volumeassertself.fill==self.volumedefpour_out(self,bottle:"Bottle"):assertself!=bottlecan_fit=bottle.volume-bottle.fillsf=self.fillbf=bottle.fillifself.fill<=can_fit:bottle.fill+=self.fillself.fill=0assertself.fill==0assertbottle.fill==bf+sfelse:bottle.fill+=can_fitself.fill-=can_fitassertbottle.fill==bottle.volumeassertself.fill==sf-can_fitdefempty(self):self.fill=0b1=Bottle(3)b2=Bottle(5)withorderedcontext():whileb2.fill!=4:choice([Bottle])()passNOTE:Be careful with importing from a module into global namespace and usingchoice()()without parameters in global scope. Current implementation load all global objects including theorderedcontextandchoiceand cause an errorLearning a functionorderedcan be usedfromorderedimportchoice,orderedcontextfromdataclassesimportdataclass@dataclassclassPoint:x:inty:intdata=[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 resultwithorderedcontext():f=create_function()forpointindata:assertrun_function(f,point.x)==point.y# context exit guarantees that create_function() constructs a correct function to describe input# TODO: approximate function learning exampleWork-in-progress functionsordered.relaxedcontext()Guaranteed to find an exit. Modifies the program if required.Methodordered.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.StatusAlthough the system is in use by several industry organizations,orderedis under heavy development. Expect rapid changes in language support, performance and bugs.LimitationsPython LanguageOverall 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 explicitside_effect()calls.None of the "ordered data structures" are supported: this includeslist,dictandtuple. Usesetor create your own data structures based on objects and classes.Loops are not supported, includingwhileandforbesides the mainwhile..choice()loop as described above - define your problem by creating functions that can be iteratively called bywhile.. choice()to overcome this.Support of missing features is a current work in progress.Integer MathMath 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 PerformanceCurrent 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 UniversalityCurrent 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 behindorderedorderedis based on translating a Python program toAI planningproblem and uses a customizedfast-downwardas a backend. Additionally, we're implementing machine learning and pre-computed matrices on various levels to vastly improve performance on larger problems.CreditsModuleorderedis developed and maintained by HyperC team,https://hyperc.com(CriticalHop Inc.)For any questions and inquries please contact Andrew Gree,andrewg@hyperc.com.Investor RelationsHyperC is fundraising! Please contact atandrewg@hyperc.com.