Behavior Tree Practice

Since I started my blog, I have been paying attention to blog visits every day. I am delighted to see some of my friends’ subscriptions or visits. It also motivates me to write some better blog posts to share and communicate with you. From the visit statistics, A considerable part of the traffic comes from search engines, and the keyword is "Behavior Tree" or "Behavior Tree" at the top. I think everyone may be interested in this. Plus, it has been repeatedly used in AI for the past few years. Research and use behavior trees, so this time I will talk about some things about behavior trees (Behavior Tree). I have written some articles ( 1 , 2 , 3 ) to discuss behavior trees before, but it was a year or two ago. Things have gone, this time will be more systematic than before, and will also add some new thoughts and insights of mine. The so-called behavior tree practice, in fact, in my mind is Practice in Behavior Tree, there is no way, it is too influenced by English textbooks :)

I want to introduce the basic concept of behavior tree through an example, it will be easier to understand, see the following figure:

bv-tree-1

This is a behavior tree we define for a soldier (you can ignore what these green circles and red circles are for), first of all, you can see that this is a tree-structured graph, with root nodes, branches, and child The number of nodes can be arbitrary, and then there are three branches, namely Patrol, Attack, and Retreat. These three branches can be regarded as the three major behaviors (Behavior) we define for this soldier. , and of course, if there is more behavior, we can keep adding new branches in the root node. When we want to decide what kind of behavior the current soldier should do, we will search the tree from top to bottom through some conditions, finally determine the behavior (leaf node) that needs to be done, and execute it . This is the basic principle of behavior trees.

It is worth noting that the three behaviors we identified are not actually the result of the real decision, it is just a type to help us understand that some behaviors of this branch belong to this category, and the behaviors of the real behavior tree are in the leaves. On the node, it is generally called the Action Node, which is represented by the red circle in the following figure.

bv-tree-action-node

These leaf nodes are the results that we really make through the behavior tree. If we use the hierarchical AI structure I mentioned before , these behavior results are equivalent to a defined "Request" (Request) , such as Move, Idle, Shoot, etc. So a behavior tree is a decision tree to help us search for a certain behavior we want.

Behavior nodes are game-related. We need to define different behavior nodes for different games, but for a certain game, behavior nodes on the behavior tree can be reused, such as moving, on the patrol branch, you need to It is used, and it will also be used on the escape branch. In this case, we can reuse this node. Behavior nodes are generally divided into two operating states:

  1. Executing: The action is still being processed
  2. Completed (Completed): The behavior processing is completed, success or failure

In addition to the behavior node, the rest are generally called the control node (Control Node). If the "scientific name" of the tree is used, it is the parent node, as shown by the green circle in the following figure.

bv-tree-control-node

The control node is actually the essence of the behavior tree. We want to search for a behavior, how to search? In fact, it is defined by these control nodes. From the control nodes, we can see the logical direction of the entire behavior tree. Therefore, one of the characteristics of the behavior tree is the visibility of its logic.

We can define a variety of control nodes for the behavior tree (this is also one of the interesting parts of the behavior tree). Generally speaking, the commonly used control nodes are the following three

  1. Selector: Select one of its child nodes to execute
  2. Sequence: Execute all its child nodes in sequence, that is to say, after the current one returns to the "completed" state, then run the first child node
  3. Parallel (Parallel): run all its child nodes once

Graphically, that's it, in that order, selection, sequence, and parallelism

bv-tree-sel

bv-tree-seq

bv-tree-pal

可以看到,控制节点其实就是“控制”其子节点(子节点可以是叶节点,也可以是控制节点,所谓“执行控制节点”,就是执行其定义的控制逻辑)如何被执行,所以,我们可以扩展出很多其他的控制节点,比如循环(Loop)等,与行为节点不同的是,控制节点是与游戏无关的,因为他只负责行为树逻辑的控制,而不牵涉到任何的游戏代码。如果是作为一个行为树的库的话,其中就一定会包含定义好的控制节点库。

如果我们继续考察选择节点,会产生一个问题,如何从子节点中选择呢?选择的依据是什么呢?这里就要引入另一个概念,一般称之为前提(Precondition),每一个节点,不管是行为节点还是控制节点,都会包含一个前提的部分,如下图

bv-tree-precondition

前提就提供了“选择”的依据,它包含了进入,或者说选择这个节点的条件,当我们用到选择节点的时候,它就是去依次测试每一个子节点的前提,如果满足,则选择此节点。由于我们最终返回的是某个行为节点(叶节点),所以,当前行为的“总”前提就可以看成是:

当前行为节点的前提 And 父节点的前提 And 父节点的父节点的前提 And….And 根节点的前提(一般是不设,直接返回True)

行为树就是通过行为节点,控制节点,以及每个节点上的前提,把整个AI的决策逻辑描述了出来,对于每次的Tick,可以用如下的流程来描述:

action = root.FindNextAction(input);
if action is not empty then
action.Execute(request,  input)  //request是输出的请求
else
print “no action is available”

Conceptually, the behavior tree is relatively simple, but it is very attractive to AI programmers. Some of its features, such as visual decision logic, reusable control nodes, low logic and implementation Coupling, etc., compared with traditional state machines, can greatly help us organize our behavioral decisions quickly and easily. I hope this brief introduction will be helpful to everyone. I have limited ability and may not be able to express it clearly. If you have any questions or advice, please communicate with me a lot. Finally, I have drawn the patrol branch of this soldier. A schematic diagram for your reference:

S — Select Node Se — Sequence Node

bv-tree-patrol-example

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326053608&siteId=291194637