User:Emmanuel Battesti/sandbox

Un "arbre de comportement" ("behavior tree" en anglais) est un modèle mathématique d'exécution de plan utilisé en informatique, en robotique, dans les systèmes de contrôle et dans les jeux vidéo. Ils décrivent les commutations entre un ensemble fini de tâches de manière modulaire. Leur force vient de leur capacité à créer des tâches très complexes composées de tâches simples, sans se soucier de la manière dont les tâches simples sont mises en œuvre. Les arbres de comportement présentent certaines similitudes avec les automates à états hiérarchiques, à la différence près que le principal élément constitutif d'un comportement est une tâche plutôt qu'un état. Leur facilité de compréhension par l'homme rend les arbres de comportement moins sujets aux erreurs et très populaires dans la communauté des développeurs de jeux. Il a été démontré que les arbres de comportement généralisent plusieurs autres architectures de contrôle. Mathématiquement, ce sont des graphes orientés acycliques.



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 Unity (game engine) and Unreal Engine (see links below).

Behavior trees became popular for their development paradigm: being able to create a complex behavior by only programming the NPC's actions and then designing a tree structure (usually through drag and drop) whose leaf nodes are actions and whose inner nodes determine the NPC's decision making. Behavior trees are visually intuitive and easy to design, test, and debug, and provide more modularity, scalability, and reusability than other behavior creation methods.

Over the years, the diverse implementations of behavior trees kept improving both in efficiency and capabilities to satisfy the demands of the industry, until they evolved into event-driven behavior trees. Event-driven behavior trees solved some scalability issues of classical behavior trees by changing how the tree internally handles its execution, and by introducing a new type of node that can react to events and abort running nodes. Nowadays, the concept of event-driven behavior tree is a standard and used in most of the implementations, even though they are still called "behavior trees" for simplicity.

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 a 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.

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$$) 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} $$