# Behavior tree (artificial intelligence, robotics and control)

A Behavior Tree is a mathematical model of plan execution used in computer science, robotics, control systems and video games. They describe switchings between a finite set of tasks in a modular fashion. Their strength comes from their ability to create very complex tasks composed of simple tasks, without worrying how the simple tasks are implemented. Behavior trees 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. Its ease of human understanding make behavior trees less error prone and very popular in the game developer community. Behavior trees have been shown to generalize several other control architectures. 

## Background

Behavior trees originate from the computer game industry as a powerful tool to model the behavior of non-player characters (NPCs). They have been extensively used in high-profile video games such as Halo, Bioshock, and Spore. Recent works propose behavior trees as a multi-mission control framework for UAV, complex robots, robotic manipulation, and multi-robot systems. Behavior trees have now reached the maturity to be treated in Game AI textbooks   as well as generic game environments such as PyGame and Unreal Engine (see links below).

## Key concepts

A behavior tree is graphically represented as a directed tree in which the nodes are classified as root, control flow nodes, or execution nodes (tasks). For each pair of connected nodes the outgoing node is called parent and the incoming node is called child. The root has no parents and exactly one child, the control flow nodes have one parent and at least one child, and the execution nodes have one parent and no children. Graphically, the children of a control flow node are placed below it, ordered from left to right.

The execution of a behavior tree starts from the root which sends ticks with a certain frequency to its child. A tick is an enabling signal that allows the execution of a child. When the execution of a node in the behavior tree is allowed, it returns to the parent a status running if its execution has not finished yet, success if it has achieved its goal, or failure otherwise.

### Control flow node

A control flow node is used to control the subtasks of which it is composed. A control flow node may be either a selector (fallback) node or a sequence node. They run each of their subtasks in turn. When a subtask is completed and returns its status (success or failure), the control flow node decides whether to execute the next subtask or not.

#### Selector (fallback) node

Fallback nodes are used to find and execute the first child that does not fail. A fallback node will return immediately with a status code of success or running when one of its children returns success or running (see Figure I and the pseudocode below). The children are ticked in order of importance, from left to right.

In pseudocode, the algorithm for a fallback composition is:

1 for i from 1 to n do
2     childstatus ← Tick(child(i))
3     if childstatus = running
4        return running
5     else if childstatus = success
6        return success
7 end
8 return failure


#### Sequence node

Sequence nodes are used to find and execute the first child that has not yet succeeded. A sequence node will return immediately with a status code of failure or running when one of its children returns failure or running (see Figure II and the pseudocode below). The children are ticked in order, from left to right.

In pseudocode, the algorithm for a sequence composition is:

1 for i from 1 to n do
2     childstatus ← Tick(child(i))
3     if childstatus = running
4        return running
5     else if childstatus = failure
6        return failure
7 end
8 return success


## Mathematical state space definition

In order to apply control theory tools to the analysis of behavior trees, they can be defined as three-tuple.

$T_{i}=\{f_{i},r_{i},\Delta t\},$ where $i\in \mathbb {N}$ is the index of the tree, $f_{i}:\mathbb {R} ^{n}\rightarrow \mathbb {R} ^{n}$ is a vector field representing the right hand side of an ordinary difference equation, $\Delta t$ is a time step and $r_{i}:\mathbb {R} ^{n}\rightarrow \{R_{i},S_{i},F_{i}\}$ is the return status, that can be equal to either Running $R_{i}$ , Success $S_{i}$ , or Failure $F_{i}$ .

Note: A task is degenerate behavior tree with no parent and no child.

### Behavior tree execution

The execution of a behavior tree is described by the following standard ordinary difference equations:

$x_{k+t}(t_{k+1})=f_{i}(x_{k}(t_{k}))$ $t_{k+1}=t_{k}+\Delta t$ where $k\in \mathbb {N}$ represent the discrete time, and $x\in \mathbb {R} ^{n}$ is the state space of the system modelled by the behavior tree.

### Fallback composition

Two behavior trees $T_{i}$ and $T_{j}$ can be composed into a more complex behavior tree $T_{0}$ using a Fallback operator.

$T_{0}={\mbox{fallback}}(T_{i},T_{j}).$ Then return status $r_{0}$ and the vector field $f_{0}$ associated with $T_{0}$ are defined (for ${\mathcal {F}}_{1}$ [definition needed]) as follows:

$r_{0}(x_{k})={\begin{cases}r_{j}(x_{k})&{\text{ if }}x_{k}\in {\mathcal {F}}_{1}\\r_{i}(x_{k})&{\text{ otherwise }}.\end{cases}}$ $f_{0}(x_{k})={\begin{cases}f_{j}(x_{k})&{\text{ if }}x_{k}\in {\mathcal {F}}_{1}\\f_{i}(x_{k})&{\text{ otherwise }}.\end{cases}}$ ### Sequence composition

Two behavior trees $T_{i}$ and $T_{j}$ can be composed into a more complex behavior tree $T_{0}$ using a Sequence operator.

$T_{0}={\mbox{sequence}}(T_{i},T_{j}).$ Then return status $r_{0}$ and the vector field $f_{0}$ associated with $T_{0}$ are defined (for ${\mathcal {S}}_{1}$ [definition needed]) as follows:

$r_{0}(x_{k})={\begin{cases}r_{j}(x_{k})&{\text{ if }}x_{k}\in {\mathcal {S}}_{1}\\r_{i}(x_{k})&{\text{ otherwise }}.\end{cases}}$ $f_{0}(x_{k})={\begin{cases}f_{j}(x_{k})&{\text{ if }}x_{k}\in {\mathcal {S}}_{1}\\f_{i}(x_{k})&{\text{ otherwise }}.\end{cases}}$
{"ft_lang_label":"__label__en","ft_lang_prob":0.8591606,"math_prob":0.9830724,"size":7378,"snap":"2019-51-2020-05","text_gpt3_token_len":1675,"char_repetition_ratio":0.12950909,"word_repetition_ratio":0.16310845,"special_character_ratio":0.22933044,"punctuation_ratio":0.12915128,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9985516,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64],"im_url_duplicate_count":[null,5,null,null,null,5,null,null,null,5,null,null,null,null,null,null,null,3,null,8,null,null,null,null,null,null,null,null,null,null,null,1,null,null,null,null,null,null,null,null,null,1,null,1,null,null,null,null,null,null,null,5,null,null,null,null,null,null,null,7,null,5,null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-19T05:00:01Z\",\"WARC-Record-ID\":\"<urn:uuid:f5bde58f-3900-47b6-9b88-74505fbe26ab>\",\"Content-Length\":\"95181\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:128ac7b8-b383-4fd7-b4f1-c0f36ceddb95>\",\"WARC-Concurrent-To\":\"<urn:uuid:07134f54-9165-48ac-8ce9-2d2dac65c395>\",\"WARC-IP-Address\":\"208.80.154.224\",\"WARC-Target-URI\":\"https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control)\",\"WARC-Payload-Digest\":\"sha1:OXFSVG7EKRMD4GP75HPQE3C3BBSOR7RK\",\"WARC-Block-Digest\":\"sha1:F2LRCP3YJIMDY54FEAEOUPP6ZAP3AVMP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250594209.12_warc_CC-MAIN-20200119035851-20200119063851-00521.warc.gz\"}"}