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: object

Base class for any node of a decision tree.

make_decision(agent)[source]

Evaluate this node.

Parameters:

agent (Any) – The agent this decision tree belongs to.

Returns:

Whatever the reached leaf of the tree returns.

Return type:

Any

class gale.ai.decision_tree.ActionNode(action)[source]

Bases: DecisionTreeNode

Leaf node of a decision tree. It represents the outcome of the decision-making process.

Parameters:

action (Callable[[Any], Any])

make_decision(agent)[source]

Evaluate this node.

Parameters:

agent (Any) – The agent this decision tree belongs to.

Returns:

Whatever the reached leaf of the tree returns.

Return type:

Any

class gale.ai.decision_tree.DecisionNode(test, true_branch, false_branch)[source]

Bases: DecisionTreeNode

Node that decides which one of two branches to evaluate, based on a test performed over the agent.

Parameters:
make_decision(agent)[source]

Evaluate this node.

Parameters:

agent (Any) – The agent this decision tree belongs to.

Returns:

Whatever the reached leaf of the tree returns.

Return type:

Any

class gale.ai.decision_tree.RandomDecisionNode(branches)[source]

Bases: DecisionTreeNode

Node that picks one of several branches at random, according to given weights. Useful to make characters less predictable.

Parameters:

branches (Sequence[Tuple[DecisionTreeNode, float]])

make_decision(agent)[source]

Evaluate this node.

Parameters:

agent (Any) – The agent this decision tree belongs to.

Returns:

Whatever the reached leaf of the tree returns.

Return type:

Any

class gale.ai.decision_tree.DecisionTree(root)[source]

Bases: object

Wraps 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