gale.ai.decision_tree module
This file contains a decision tree implementation, a simple alternative to behavior trees to model the decision making of autonomous characters as a sequence of yes/no questions that lead to an action.
Author: Alejandro Mujica (aledrums@gmail.com)
- class gale.ai.decision_tree.DecisionTreeNode[source]
Bases:
objectBase class for any node of a decision tree.
- class gale.ai.decision_tree.ActionNode(action)[source]
Bases:
DecisionTreeNodeLeaf node of a decision tree. It represents the outcome of the decision-making process.
- Parameters:
action (Callable[[Any], Any])
- class gale.ai.decision_tree.DecisionNode(test, true_branch, false_branch)[source]
Bases:
DecisionTreeNodeNode that decides which one of two branches to evaluate, based on a test performed over the agent.
- Parameters:
test (Callable[[Any], bool])
true_branch (DecisionTreeNode)
false_branch (DecisionTreeNode)
- class gale.ai.decision_tree.RandomDecisionNode(branches)[source]
Bases:
DecisionTreeNodeNode that picks one of several branches at random, according to given weights. Useful to make characters less predictable.
- Parameters:
branches (Sequence[Tuple[DecisionTreeNode, float]])
- class gale.ai.decision_tree.DecisionTree(root)[source]
Bases:
objectWraps a root node so that it can be conveniently evaluated as a whole.
Usage example:
- tree = DecisionTree(
- DecisionNode(
test=lambda agent: agent.health < 0.3, true_branch=ActionNode(lambda agent: agent.flee()), false_branch=ActionNode(lambda agent: agent.attack()),
)
) tree.make_decision(agent)
- Parameters:
root (DecisionTreeNode)
- make_decision(agent)[source]
Evaluate the tree from its root.
- Parameters:
agent (Any) – The agent this tree belongs to.
- Returns:
Whatever the reached leaf of the tree returns.
- Return type:
Any
- tick(agent, dt=0)[source]
Alias of make_decision with a (agent, dt) signature, so a DecisionTree can be plugged in anywhere a tick(agent, dt)-style brain is expected, such as gale.ai.agent.Agent.
- Parameters:
agent (Any) – The agent this tree belongs to.
dt (float) – Ignored. Accepted only for interface compatibility.
- Returns:
Whatever the reached leaf of the tree returns.
- Return type:
Any