6 kinds of decision-making methods of game artificial intelligence development

Source: Internet
Author: User


Artificial intelligence follows: perception---action
Decision Making methods: finite state machine (finite-state machines), layered state machine (hierarchical finite-state machines), Behavior tree (Behavior Trees), utility system (Utility Systems), goal-oriented action plan (goal-oriented action Planners), tiered task Network (hierarchical task Networks)

Finite state machineThe finite state machine is the most common behavior model in game AI at present. The code of the state machine is simple and fast, it is powerful, flexible and has little computational overhead.
One benefit of the state machine is that it can be visualized as shown in the following:

There are four states in the figure: Patrol (Patrol), view (investigate), attack (attack), Escape (flee), we take the solid circle as the initial state.
Brief process: Assume that the NPC soldier is defending his position, the current state is patrol, when he hears what movement will go to the view state, run to the sound source to see, if you see the enemy will go to attack state, if not seen a period of time and will return to patrol state. In the attack state, if the blood value is low, it will enter the escape state. If you defeat the enemy, you will return to patrol.
One of the main structures of state machine state classes is as follows: The OnEnter function is equivalent to the start () function in unity, which is called at the beginning of the class, as the beginning of an excessive and new state of the old State, such as when moving from patrol to attack, the NPC can be shouted "discover the enemy" at the beginning of the attack state. Attack! And so on OnUpdate () is equivalent to update () in unity, and you can make it execute every frame, or execute it once in a few seconds, in a loop, with each execution interval determined by you. OnExit () is performed before exiting a state, for example, before the enemy is killed and then moved from the attacking state to the patrol state, allowing the NPC to make a cheering gesture and shout victory. The Fsmtransition list is all the possible states that will go to.
Class Fsmstate
{
virtual void onEnter ();
virtual void onUpdate ();
virtual void onExit ();
List<fsmtransition> transitions;
};
Each state also stores the Fsmtransition class, which represents the state that can be transferred from the current state
Class Fsmtransition
{
virtual bool IsValid ();
Virtual fsmstate* getnextstate ();
virtual void ontransition ();
}
IsValid () returns True when the conversion condition is met, such as when an enemy NPC is found to go from patrol to attack, Getnextstate () returns the state to be transferred, Ontransition () is the transition between States, and the onenter () above says Almost.
Finally, the finite state machine class Finitestatemachine
Class Finitestatemachine
{
void Update ();
list<fsmstate> states;
Fsmstate* initialstate;
fsmstate* ActiveState;
}
The finite state machine class contains a list of all States states,initialstate the initial state, ActiveState is the current state.
The pseudo code is as follows:
Loop call IsValid () in Activestate.transtitions to detect compliance with conditions that reach the next state
If the conversion criteria are met
Call Activestate.on exit () to exit the current state
Set ActiveState to Validtransition.getnextstate () to assign the current state to the next state
Call Activestate.onenter (), the beginning of the next state
If the conversion condition is not met, call Activestate.onupdate () and let the NPC do what the current state needs to do


It is best to draw an upper sketch before writing the finite state machine code, so that you can clearly transform the relationship and not miss out on the state.


layered finite state machine Although the finite state machine is good, but it has a great disadvantage, when the state of a few times can be used freely, when the state of more than 10 is already very complex structure, and error prone.
If we let NPC patrol two places, such as safe indoor, and Doorway
 
If we want to attach a situation to a state, such as when NPC is patrolling, ask him to take a call and then resume Patrol, At this time if the use of finite state machine we have to create a new call state to do the transition, but at this time the patrol has two, so can receive the state of the phone has two, and then added two the same state, a lot of such a small state of repetition makes the state machine more and more complex. such as
 
At this point, we can use a hierarchical finite state machine to solve this problem, a plurality of state machines into a layer, such as the patrol security and the door into the guard building, so we just need to have a call status on it.
 
Hierarchical finite state machine adds a lag, in a finite state machine is not, in a normal finite state machine, is starting from the initial state, in a hierarchical finite state machine is a nested state. Note that there is a circle of H, which represents the historical state, and when we first enter the guarded building in a nested state, the historical state H is represented as the initial state, and then the historical state H is represented as the nearest state.
In our example: the initial state is to guard the building, and then enter to see the phone hold the nesting, patrol security is the initial state. When switching from patrol security to patrol doorway this state, H history state changed to patrol the door state, at this time to call, switch to answer the phone state, answer the phone end, we return to the state of the state of nesting in the history, this time for the patrol doorway, visible h history State is a temporary, It makes it a big mistake to make it easier for the outside of the nesting state to return to a small state within the previous nesting so as not to go wrong, or to change back to a different state, if the call comes back to patrol safety.
Layered finite state machines, which avoids duplicate states and enables larger, more complex states.


Instance:
Halo2 uses this technique, such as
visible: the use of grenades, masking, defense into self-defense, the warring part of the use of multi-layered nesting, but the principle is the same, to the body design and search bodies classified as post-war treatment. Only one behavior is nested in the retreat and idle sections, but you can continue to add behaviors later, and extensibility is good.
as to how to choose a behavior in a nested layer, you can do it in that order, or you can add a weight priority, or you want him to execute which code to control.
 

behavior tree The behavior tree is a tree structure, each node represents a behavior, each behavior can have sub-behavior.
All behaviors have a prerequisite, which is the condition of the resulting behavior.
The entire algorithm starts at the root of the tree, and then begins to examine each of the prerequisites. Each layer of the tree can only perform one behavior, so when a behavior is executing, its sibling nodes are not checked, but their child nodes are still checked. Conversely, if a precondition for a behavior is not currently satisfied, skip judging its child nodes and continue to judge its sibling node. Once a tree has been fully checked, it decides to perform the highest priority, and then executes each action in turn.
Pseudo code:
when the current node exists
     Determine the prerequisites for the current node
     If the prerequisites return True
         Add nodes to the execution list
         Make a child node the current node
     Otherwise
         make the sibling node
perform all the behaviors on the manifest for the current node
The
differs from the state machine in that the behavior tree is stateless and does not need to write down the behavior that was performed before, just to determine if the behavior should not be performed.
The nodes of the behavior tree are irrelevant, deleting or adding nodes, and having no effect on other nodes. Therefore, extensibility is also an advantage of the behavior tree. In addition, you can add flexibility and randomness to the decision tree, and parent nodes can randomly decide whether to check child nodes.
Cons: Decision tree choices are not necessarily optimal, and the results are not necessarily what we want. And the decision every time from the root to determine the choice of behavior node, than the state of the time-consuming. Each decision is subject to a large number of conditional judgments, which can become very slow.
There is another problem, such as: a farmer to harvest crops, the enemy appeared, the farmer fled, escaped from the enemy's range, and then go back to reap the crops, go to the enemy's range and escape, so back and forth, is a disadvantage, can be based on the situation to write code to avoid, otherwise will be player * * *.



utility system Logic of artificial intelligence and the logic of the computer is based on simple bool problems, such as: "Can I see the enemy?" "Do I have ammunition?" is simply or not a problem, so the act of making is usually extreme, a single action. For example:
if (Canseeenemy ())
{
  attackenemy ();
}
if (Outofammo ())
{
  Reload ();
}
Real-time, multi-conditional behavior, the bool judgment brings a single action.
if (Outofammo () && canseeenemy ())
{
  Hide ();
}
So in some cases, it is inappropriate to make these boolean judgments, to omit a lot of situations and to judge. For example: we may need to consider the distance to the enemy, how much ammunition, how hungry, the HP value, and so on. These judging conditions can map many actions, which is much better than our single judgment to do this action. utility-based System, utility-based systems make optimal choices based on weights, ratios, queues, and many things to consider, making AI more intelligent than normal behavior trees. According to the above example, using the utility system our AI can make the action we want, and according to the current situation to do different intensity of action, so that the AI is real, more possibilities, is no longer only the right choice. The decision tree is to say to AI, "just a behavior that you're going to do", and the utility system is saying to AI, "These are the behaviors you might want to do"
Sims Sims Artificial Intelligence is the utility system (Sims AI let me worship so far), in Sims, The villain combines the current environment and its own state to make the choice of action. For example: The villain "very hungry" in combination with the environment "no food" will be more attractive than the only "a little hungry" more eye-catching. If "A little bit hungry" villain will be close to "food" as the first act of execution. Note that the "delicacy of food", "food is scarce", "a little hungry", are a range of values (usually 0-1 floating-point values).
When a new behavior needs to be selected, we select the relative optimal choice by the score (the various degrees mentioned above), or by adding a random value to the selection, so that several options close to the selection have a chance (odds can be determined according to the random value) is selected.
goal-oriented action planGoap comes from the strips approach, both of which allow AI to create their own methods to solve problems, and we provide it with a range of possible actions as a description of the world, and the prerequisites for each action use, and the impact of action. Ai has an initial state and the goals he needs to achieve. There is a set of goals that AI can select by priority or current state. The planning system determines an action sequence that satisfies the current target, and plans a sequence of actions that is as simple as the path to reach the target state.
Goap is a reverse link search, starting with the goal to be achieved, finding what moves to achieve the goal, finding the prerequisites for just the action, pushing forward and knowing that your current (initial) state is reached. This reverse link search replaces the heuristic's forward link search.
Pseudo code:
Add the target to the unresolved events list
For each resolution event (for)
Remove this to resolve the event
Find the action to reach the event
If the prerequisites for the action have been met
Add action to the plan
Pushing back requires a prerequisite action into the plan
Otherwise
Add this prerequisite to the unresolved time


For example: We set up an NPC soldier, and set its target to kill other enemies, and our goal is to target.dead. In order for the target to die, NPC must have a weapon to shoot, this is a prerequisite, but now the NPC is not equipped with weapons, NPC will need to carry out the action to find weapons, if the NPC has an arsenal, he will take one from the arsenal, if the disease has no arsenal, We need to find a way to get a weapon and equip it. Armed with weapons to find the enemy, the way to achieve a variety of ways, hiking, or NPC around the car can also drive to find. I found that we gave NPC a large number of action choices, so that NPCs decide what to do, and thus produce dynamic unpredictable and interesting behavior, and behave naturally, much better than the developer behavior.
Here's a simple example: http://gamerboom.com/archives/83622

Tiered Task Networkhtn is also looking for a plan to get the AI to execute, the difference is how to find out the plan. Start having an initial state and a problem with the task that represents what we need to solve. The principle is that the most advanced task breaks down into smaller tasks and continues to decompose until we solve the problem. Each high-level task is completed in many ways, and the current world State determines which group of tasks the high-level task is to break down into. HTN In contrast to Goap, HTN is a forward link search that is pushed from the current state to the target State and pushed forward until the problem is resolved. The state of the world is dispersed into several properties, and its HP, energy, enemy HP, distance, are planned according to these.
We have two tasks: the original task and the composite task. The original task is a task that can only solve the problem, that is, the task that can reach the goal directly. In the game, it can fire, reload bullets, and move to a cloak. These characters can affect the state of the world, fire this task requires bullets first, and carry out the task of filling bullets. A composite task is a high-level task and can be considered a method. One way is for a group of tasks to complete a composite task, a set of tasks that are determined by the prerequisites. The compound task lets HTN infer the world and decide what action to make.
Using composite tasks, we can build a HTN domain, which is a large-level task that represents our approach to solving the problem.
Pseudo code:
increase the root compound task to the exploded list
for each task in our exploded list (for)
     Remove Task
     If the task is a composite task
         Find a way to meet the current condition state and be able to handle the composite task
         If the method is found, add the task of the method to the exploded list
         If not found, revert to the state of the previously exploded task
     If the task is the original task
         Perform tasks in the current state
          Add task to final plan list


HTN is the decomposition of a smaller task from the highest-level root task into a smaller one, and decomposition is needed to determine the current state and condition. When we finally break down to the original task, we add the original task to the final plan, and each original task is an actionable step that we can execute directly.


-----by wolf96 http://blog.csdn.net/wolf96






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

6 kinds of decision-making methods of game artificial intelligence development

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.