Creates a new Rule object with the given id.
Rule is a simple class type that carries a weight indicator and arbitrary code data for usage in the dynamic script generation process.
Creates a new, empty RuleSet.
RuleSet is a rule container class that manages rules, their weights and the weight distribution for the rules. The minweight and maxweight parameters are the minimum and maximum weight boundaries, each rule’s weight has to stay in.
Gets or sets the maximum weight to use for rules.
Gets or sets the minimum weight to use for rules.
Calculates the reward or penalty, each of the activated rules recives. fitness hereby can be used as measure of the performance or whatever is suitable in the implementation.
This must be implemented by inheriting classes.
Distributes the remainder of the weight differences between the last weights and current weights.
The method must return a value. This must be implemented by inheriting classes.
Tries to find the Rule with the matching id and returns it. In case no Rule with the passed id exists, None is returned.
Updates the weights of all contained rules.
Adapted from Pieter Spronck’s algorithm as explained in Spronck et al: 2005, ‘Adaptive Game AI with Dynamic Scripting’.
The RuleManager class takes care of loading and saving rules from arbitrary data sources. The base is an abstract class, which’s load_rules() method must be implemented according to the specific needs of the application.
Gets the maximum amount of rules to manage.
Loads rules from the underlying data source and returns them as list. The maxrules argument defines the amount of rules to load. If it is smaller than 0, all existing rules should be returned.
This must be implemented by inheriting classes.
Saves the passed rules to the underlying data source.
This must be implemented by inheriting classes.
Saves a LearnSystem/RuleSet combination to a physical file.
A simple memory-mapped RuleManager implementation that does not load its rules from an external data source.
It is an extremely useful class for testing rules and basic algorithms, but due to the in-memory management of all rules, it should not be used in a productive environment, especially if large rule sets have to be managed.
By default, the MMapRuleManager class will reserve enough memory for the rules to manage, when it is constructed. It will not fill the rules with useful values though. It is up to caller to use load_rules() afterwards and fill the returned Rule instances with the necessary data.
Gets the maximum amount of rules to manage.
Returns the internally managed rules or a certain subset.
This does nothing and will always return True.
Saves a LearnSystem/RuleSet combination to a physical file.
Creates a new LearnSystem using a specific RuleSet.
The LearnSystem class takes care of creating new scripts based on a predefined RuleSet. It does not evaluate the scripts nor modifies the rules written to them.
The procedure of creating scripts is done using three phases:
The header and footer are freely choosable. You can simple override or reassign the create_header() and create_footer() methods to let them return your required code.
Gets or sets the maximum script size (in bytes) for inserting rules.
Gets or sets the maximum amount of tries to insert a script rule.
Creates the footer for the script file.
The default implementation does nothing.
Creates the header for the script file.
The default implementation does nothing.
Creates a rule list from the currently active RuleSet. Gets maxrules rules from the set RuleSet and passes their code back as string for the script file.
Adapted from Pieter Spronck’s algorithm as explained in Spronck et al: 2005, ‘Adaptive Game AI with Dynamic Scripting’.
Creates a script from the available RuleSet using the passed script file. A maximum of maxrules rules will be written. scriptfile can be either a file object or filename. In case of a file object it is assumed to be writeable and won’t be closed on leaving the function (but flushed).