Bonsai - Behavior Tree
You can serialize the behavior tree using Serde, Ron and graphviz
A Behavior Tree (BT) is a data structure in which we can set the rules of how certain behavior's can occur, and the order in which they would execute. BTs are a very efficient way of creating complex systems that are both modular and reactive. These properties are crucial in many applications, which has led to the spread of BT from computer game programming to many branches of AI and Robotics.
How to use a Behavior tree?
A Behavior Tree forms a tree structure where each node represents a process.
When the process terminates, it signals Success or Failure. This can then
be used by the parent node to select the next process.
A signal Running is used to tell the process is not done yet.
For example, if you have a state A and a state B:
- Move from state
Ato stateBifAsucceeds:Sequence([A, B]) - Try
Afirst and then tryBifAfails:Select([A, B]) - If
conditionsucceedes doA, else doB:If(condition, A, B) - If
Asucceeds, return failure (and vice-versa):Invert(A) - Do
Brepeatedly whileAruns:While(A, [B]) - Do
A,Bforever:While(WaitForever, [A, B]) - Run
AandBin parallell and wait for both to succeed:WhenAll([A, B]) - Run
AandBin parallell and wait for any to succeed:WhenAny([A, B]) - Run
AandBin parallell, butAhas to succeed beforeB:After([A, B])
See the Behavior enum for more information.
Example of use
This is a simple example with two possible Actions: Increment a number, Decrement a number. We
construct a BT where we increment a number twice, one second apart. Then wait 0.5 seconds before we
then decrement the same number again. Additionally we use a Blackboard to store/persist the immediate
state of the number accessed by the key count.
use ;
// Some test actions.
// A test state machine that can increment and decrement.