Behavior Tree
Behavior Tree is a modular, hierarchical decision model that is widely used in robot control, and game development. It present some similarities to hierarchical state machines with the key difference that the main building block of a behavior is a task rather than a state. Behavior Tree have been shown to generalize several other control architectures (https://ieeexplore.ieee.org/document/7790863)
Core Concepts
Control Node
- class MissionPlanning.BehaviorTree.behavior_tree.ControlNode(name)[source]
Base class for all control nodes in a behavior tree.
Control nodes manage the execution flow of their child nodes according to specific rules. They typically have multiple children and determine which children to execute and in what order.
- class MissionPlanning.BehaviorTree.behavior_tree.SequenceNode(name)[source]
Executes child nodes in sequence until one fails or all succeed.
- Returns:
Returns FAILURE if any child returns FAILURE
Returns SUCCESS when all children have succeeded
Returns RUNNING when a child is still running or when moving to the next child
Example
<Sequence> <Action1 /> <Action2 /> </Sequence>
- class MissionPlanning.BehaviorTree.behavior_tree.SelectorNode(name)[source]
Executes child nodes in sequence until one succeeds or all fail.
- Returns:
Returns SUCCESS if any child returns SUCCESS
Returns FAILURE when all children have failed
Returns RUNNING when a child is still running or when moving to the next child
Examples
<Selector> <Action1 /> <Action2 /> </Selector>
- class MissionPlanning.BehaviorTree.behavior_tree.WhileDoElseNode(name)[source]
Conditional execution node with three parts: condition, do, and optional else.
- Returns:
First executes the condition node (child[0]) If condition succeeds, executes do node (child[1]) and returns RUNNING If condition fails, executes else node (child[2]) if present and returns result of else node If condition fails and there is no else node, returns SUCCESS
Example
<WhileDoElse> <Condition /> <Do /> <Else /> </WhileDoElse>
Action Node
- class MissionPlanning.BehaviorTree.behavior_tree.ActionNode(name)[source]
Base class for all action nodes in a behavior tree.
Action nodes are responsible for performing specific tasks or actions. They do not have children and are typically used to execute logic or operations.
Decorator Node
- class MissionPlanning.BehaviorTree.behavior_tree.DecoratorNode(name)[source]
Base class for all decorator nodes in a behavior tree.
Decorator nodes modify the behavior of their child node. They must have a single child and can alter the status of the child node.
- class MissionPlanning.BehaviorTree.behavior_tree.InverterNode(name)[source]
Inverter node that inverts the status of its child node.
- Returns:
Returns SUCCESS if the child returns FAILURE
Returns FAILURE if the child returns SUCCESS
Returns RUNNING if the child returns RUNNING
Examples
<Inverter> <Action /> </Inverter>
- class MissionPlanning.BehaviorTree.behavior_tree.TimeoutNode(name, timeout)[source]
Timeout node that fails if the child node takes too long to execute
- Returns:
If the timeout duration has been exceeded - Child’s status: Otherwise, passes through the status of the child node
- Return type:
FAILURE
Example
<Timeout sec="1.5"> <Action /> </Timeout>
- class MissionPlanning.BehaviorTree.behavior_tree.DelayNode(name, delay)[source]
Delay node that delays the execution of its child node for a specified duration.
- Returns:
Returns RUNNING if the duration has not yet passed
Returns child’s status after the duration has passed
Example
<Delay sec="1.5"> <Action /> </Delay>
Behavior Tree Factory
- class MissionPlanning.BehaviorTree.behavior_tree.BehaviorTreeFactory[source]
Factory class for creating behavior trees from XML strings.
- build_node(node)[source]
Build a node from an XML element.
- Parameters:
node (Element) – The XML element to build the node from.
- Returns:
the built node
- Return type:
BehaviorTree Node
- build_tree(xml_string)[source]
Build a behavior tree from an XML string.
- Parameters:
xml_string (str) – The XML string containing the behavior tree.
- Returns:
The behavior tree.
- Return type:
- build_tree_from_file(file_path)[source]
Build a behavior tree from a file.
- Parameters:
file_path (str) – The path to the file containing the behavior tree.
- Returns:
The behavior tree.
- Return type:
- register_node_builder(node_name, builder)[source]
Register a builder for a node
- Parameters:
node_name (str) – The name of the node.
builder (function) – The builder function.
Example
factory = BehaviorTreeFactory() factory.register_node_builder( "MyNode", lambda node: MyNode( node.attrib.get("name", MyNode.__name__), node.attrib["my_param"], ), )
Behavior Tree
- class MissionPlanning.BehaviorTree.behavior_tree.BehaviorTree(root)[source]
Behavior tree class that manages the execution of a behavior tree.
- print_tree()[source]
Print the behavior tree.
- Node print format:
Action: <Action> Decorator: (Decorator) Control: [Control]
- Node status colors:
Yellow: RUNNING Green: SUCCESS Red: FAILURE
Example
Visualize the behavior tree by xml-tree-visual.
Print the behavior tree
Behavior Tree
[Robot Main Controller]
[Battery Management]
(Low Battery Detection)
<Check Battery>
<Low Battery Warning>
<Charge Battery>
[Patrol Task]
<Start Task>
[Move to Position A]
<Move to A>
[Obstacle Handling A]
[Obstacle Present]
<Detect Obstacle>
<Avoid Obstacle>
<No Obstacle>
<Position A Task>
[Move to Position B]
(Short Wait)
<Prepare Movement>
<Move to B>
(Limited Time Obstacle Handling)
[Obstacle Present]
<Detect Obstacle>
<Avoid Obstacle>
<Position B Task>
[Conditional Move to C]
<Check Sufficient Battery>
[Perform Position C Task]
<Move to C>
(Ensure Completion)
<Position C Task>
<Skip Position C>
<Complete Patrol>
<Return to Charging Station>
Behavior Tree