A behavior tree organizes behavior as a hierarchy of nodes. Some nodes check conditions or perform actions; others control which nodes run. This pattern can guide robots, game characters, or AI assistants through a task. section 6.1 section 6.1.1
Execution proceeds from top to bottom and left to right. Nodes report success or failure, allowing a parent node to decide whether to continue or try another branch. section 6.1.1
The primary nodes have different jobs:
- A selector, or fallback, tries children in order and succeeds when the first child succeeds. If none succeeds, it fails. section 6.1
- A sequence runs children in order. It succeeds if all succeed and stops if a child fails. section 6.1 section 6.1.1
- A condition checks a state; an action performs work. Both report a result to the tree. section 6.1.1 section 6.7
- A decorator wraps a node, while a parallel node allows dual execution. section 6.7
Choose a fruit
A root selector can contain two sequences: check for an apple and eat it, or check for a pear and eat it. If the apple check fails, that sequence stops and the selector tries the pear branch. section 6.1 section 6.1.1
Listing 6.1 first_btree.py
import py_trees
class HasApple(py_trees.behaviour.Behaviour): #1
def __init__(self, name):
super(HasApple, self).__init__(name)
def update(self):
if True:
return py_trees.common.Status.SUCCESS
else:
return py_trees.common.Status.FAILURE
# Other classes omitted…
has_apple = HasApple(name="Has apple") #2
eat_apple = EatApple(name="Eat apple") #2
sequence_1 = py_trees.composites.Sequence(name="Sequence 1", memory=True)
sequence_1.add_children([has_apple, eat_apple]) #3
has_pear = HasPear(name="Has pear") #4
eat_pear = EatPear(name="Eat pear") #4
sequence_2 = py_trees.composites.Sequence(name="Sequence 2", memory=True)
sequence_2.add_children([has_pear, eat_pear]) #3
root = py_trees.composites.Selector(name="Selector", memory=True)
root.add_children([sequence_1, sequence_2]) #3
behavior_tree = py_trees.trees.BehaviourTree(root) #5
py_trees.logging.level = py_trees.logging.Level.DEBUG
for i in range(1, 4): #6
print("\n------------------ Tick {0} ------------------".format(i))
behavior_tree.tick() #6
### Start of output
------------------ Tick 1 ------------------
[DEBUG] Selector : Selector.tick()
[DEBUG] Selector : Selector.tick() [!RUNNING->reset current_child]
[DEBUG] Sequence 1 : Sequence.tick()
[DEBUG] Has apple : HasApple.tick()
[DEBUG] Has apple : HasApple.stop(Status.INVALID->Status.SUCCESS)
[DEBUG] Eat apple : EatApple.tick()
Eating apple
[DEBUG] Eat apple : EatApple.stop(Status.INVALID->Status.SUCCESS)
[DEBUG] Sequence 1 : Sequence.stop()[Status.INVALID->Status.SUCCESS]
See how Python and py_trees assemble the fruit checks and actions into sequences under a selector. Change a condition’s result or remove a sequence to experiment with the flow. section 6.1.3
For assistant workflows, actions and conditions can direct different assistants. A coding-challenge tree has one assistant generate a solution, another review it, and a separate verifier check it. The verifier uses its own message thread to stay isolated from the solution discussion. section 6.3.2
To design a tree with back chaining, work backward from the desired result:
- Name the goal behavior, then identify the actions that lead to it. section 6.5
- Identify the conditions needed for each action and how assistants or nodes will exchange information. section 6.5
- Construct the tree from the goal, adding actions and conditions until the necessary conditions connect to known states or facts. section 6.5
Trees make behavior easier to organize, reuse, and visualize as a system grows. They are not the only control pattern: finite state machines, decision trees, and utility-based systems offer alternatives or can be combined with behavior trees. section 6.1.2
