Python API reference

class Rule(id : object)

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.

code

Gets or sets the code of the Rule.

id

Gets the id of the Rule.

used

Indicates whether the Rule was used or not.

weight

Gets or sets the weight of the Rule.

class RuleSet(minweight : float, maxweight : float)

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.

maxweight

Gets or sets the maximum weight to use for rules.

minweight

Gets or sets the minimum weight to use for rules.

rules

Gets the list of currently managed Rule objects.

weight

Gets the total weight of all managed Rule objects.

add(rule : Rule)

Adds a Rule to the RuleSet.

calculate_adjustment(fitness : float) → float

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.

clear()

Removes all rules from the RuleSet.

distribute_remainder(remainder : float) → float

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.

find(rid : float) → Rule

Tries to find the Rule with the matching id and returns it. In case no Rule with the passed id exists, None is returned.

remove(rule : Rule)

Removes a Rule from the RuleSet.

update_weights(fitness : float)

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’.

class RuleManager(id : object)

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.

maxrules

Gets the maximum amount of rules to manage.

load_rules([maxrules=-1]) → [Rule, Rule ...]

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.

save_rules(rules : iterable)

Saves the passed rules to the underlying data source.

This must be implemented by inheriting classes.

save_rules_hint_file(filename : string, learnsystem : LearnSystem)

Saves a LearnSystem/RuleSet combination to a physical file.

class MMapRuleManager(maxrules : int)

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.

maxrules

Gets the maximum amount of rules to manage.

load_rules([maxrules=-1]) → [Rule, Rule ...]

Returns the internally managed rules or a certain subset.

save_rules(rules : iterable)

This does nothing and will always return True.

save_rules_hint_file(filename : string, learnsystem : LearnSystem)

Saves a LearnSystem/RuleSet combination to a physical file.

class LearnSystem(ruleset : RuleSet)

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:

  • header creation
  • rule code creation
  • footer creation

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.

maxscriptsize

Gets or sets the maximum script size (in bytes) for inserting rules.

maxtries

Gets or sets the maximum amount of tries to insert a script rule.

ruleset

Gets or sets the RuleSet to use.

Creates the footer for the script file.

The default implementation does nothing.

create_header() → str

Creates the header for the script file.

The default implementation does nothing.

create_rules(maxrules : int) → str

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’.

create_script(scriptfile : object, maxrules : int)

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).

Project Versions

Previous topic

Getting started

Next topic

C++ Implementation

This Page