AI logic Implementation-Choose a behavior tree or a state machine?

Source: Internet
Author: User

Friends interested in AI may have seen the Rai Yonghao translation of the "Finite state machine era of the end of the 10 reasons", which discussed the state machine many drawbacks. At the same time in the PPT (attached) described the behavior of the many advantages of the tree, here is not to repeat. More to want to summarize their own play for a while after the behavior of some of the tree experience.


Personal experience:

State machine to implement AI more in line with our naïve expression of thought, I think any experienced coder can intuitively write an AI state machine. It is used for some simple AI in fact there is no big problem, (search for enemies, proximity, attack, death) with the state machine is actually more convenient. But in the face of some complex AI logic implementation will appear more complicated. At the same time, when the existing behavior logic needs to be extended, the code will be more difficult, because the amount of state to maintain will multiply.



Behavior tree, the implementation of the AI process is more technical, the framework designers more comprehensive consideration of the various situations we may encounter, each case is abstracted into a type of node, and we have to do is to follow the specification to write the node, and then connect the node into a behavior tree. More object-oriented flavor, the behavior of the module between the Lotus is relatively low.


Give a rough example of the difference between the two:

AI behavior: Eat and sleep to play peas (very physical and mental energy;)

1. Hit Peas HP-= 5/s MP-= 3/sec
2. Eat HP + = 10/s MP-= 1/sec
3. Sleep MP + + 15/sec. HP-= 2/sec
4. Eating and sleeping are non-disruptive movements (pending) and must be performed to satiety (HP = +) or full (MP = 100)
5. Hit the bean is the instantaneous action, each frame can be executed once


The implementation logic diagram of the state machine:




Implementation logic diagram for the behavior tree:



In fact, whether you know what is selector,condition is not matter, at least from, should be able to see that the relationship between the behavior tree node is not as "close" as the state machine.


Choosing two different methods of AI implementation also determines the implementation logic of the specific behavior.

For example, for the implementation of the sleep action, if it is a state machine:

function sleep () = if Y = = Thenawakeevent () returnendhp-= XMP + yend
Then each frame performs sleep ()


If you select the behavior tree:

function sleep () local sleeptime = (100/15) <span style= "White-space:pre" ></span>-- Embarrassed into a section of COCOS2DX code self:runaction (CC. Sequence:create (CC. Delaytime:create (Sleeptime), CC. Callfunc:create (cancelpending))) Local cancelpending = function () pending = Falseendend


List the concept of the behavior tree:

In the case of finite state machines, it is necessary to clarify the state of transition, and for the behavior tree, the state premise must be clarified: prerequisites
Each action must have a "precondition", which determines whether the behavior is selected.
The operation of the behavior tree is also driven by the update of the frame loop, not necessarily the update every frame, but the periodic update.
Each run is run from the root node (root), and each operation selects a viable child node, which can be either random or pre-set.
The behavior tree consists of the leaf node and the middle node, the leaf node is the most basic behavior (such as running, attacking), the middle node represents the logical unit (patrol, Escape).
When a leaf node is selected, its corresponding basic behavior is activated.
The most basic behavior may or may not fail to perform successfully.
High-level behavior (intermediate nodes) whether the execution succeeds depends on whether or not their child nodes are performing successfully.
One child node failure can cause a parent node to select another child.
In addition to selecting (selector) A single child node behavior, a node may also be in order (sequence) or parallel (concurrent) to run all of his child nodes.
In addition to a precondition, there may be a context condition (parent node or child node may store a certain state variable).
High-priority behavior may preempt low-priority behavior


Reproduced a section of Akara's Behavior Tree Implementation scheme:

* Composite Node combination nodes
* Decorator Node decoration nodes
* Condition Node Conditional nodes
* Action node behavior nodes

Detailed description of the various nodes:
* Selector Node Select nodes
Description: From start to finish, select the first child node that performs the condition as true, in order to stop.
Process Flow: When executing this type of node, it executes its own child node from begin to end iteration: Returns True when a child node executes, that stops the iteration, and this node also returns true to its parent node Otherwise all child node returns false, and the node returns false to its parent node.


* Sequence node sequence nodes
Description: From beginning to end, each child node is executed sequentially, and a false stop is encountered.
Processing flow: When this type of Node is executed, it executes its own child node from begin to end iteration: if it encounters a child node, returns False, and then stops iterating, this node also returns false to its parent node Otherwise all child node returns True, and the node returns true to its parent node.


* Parallel Node Parallel nodes
Description: Executes all of its child nodes in parallel from beginning to end.
Parallel Selector node: Has a child node true returns True, otherwise false is returned.
Parallel Sequence node: false returns False if there is a child node, otherwise true.
Parallel Fall on all node: false returns false for all child nodes, otherwise true.
Parallel succeed on all node: True only returns true for all child nodes, otherwise false is returned.
Parallel hybird node: The specified number of child nodes returns TRUE or false before the result is determined.

* Decorator Node decoration nodes
Description: Decorative nodes are generally used as additional conditions. For example, the time interval control, the number control, the frequency control, the result inversion, the error processing and so on.

* Condition Node Conditional nodes
Description: As the name implies, is the node corresponding to the condition.

* Action node behavior nodes
Description: As the name implies, is the node used to complete some kind of action.


Discuss the pros and cons from the point of view of code implementation

Advantages:
1. Behavior logic and state data separation, any node can be reused after writing.
2. High reusability, can be used to reorganize different nodes to implement different behavior tree
3. Linear expansion, easy to expand
4. Configurable to give work to designer
5. Capable of "AI", "Lost treasure" and so on scene.

Disadvantages:
1. Each frame starts from root and may have access to the node, which consumes more CPU
2. Any simple operation must use a node


Later, we'll talk about the node implementation details in the behavior tree, as well as several important pieces of it: blankboard, configurable, etc.


Data address

AI logic Implementation-Choose a behavior tree or a state machine?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.