|
| 1 | +tags:: [[Diataxis/Explanation]] |
| 2 | + |
| 3 | +- # Behavior Trees Conceptual Overview |
| 4 | + - ## Overview |
| 5 | + - **Behavior trees (BTs)** are a well-established concept in **computer science and AI**, completely independent of any specific implementation |
| 6 | + - They are hierarchical, composable control structures used to define agent behavior through a tree of decision nodes |
| 7 | + - Designed to be **reactive**, **deterministic**, and **readable by humans** |
| 8 | + - Provide a structured way to encode both control flow and policy together |
| 9 | + -  |
| 10 | + -  |
| 11 | + -  |
| 12 | + -  |
| 13 | + - ## Context |
| 14 | + - **Historical Development**: Behavior trees emerged in the early 2000s, primarily from **game AI**, as a response to limitations in: |
| 15 | + - Finite State Machines (FSMs) |
| 16 | + - Hierarchical FSMs |
| 17 | + - Hard-coded decision logic |
| 18 | + - **Problem Addressed**: Traditional FSMs became unwieldy for complex NPC behaviors, leading to "spaghetti state machines" that were difficult to maintain and debug |
| 19 | + - **Adoption**: Popularized by studios like **LucasArts**, **Bungie**, and **EA**, with a canonical reference being Isla, D. "Handling Complexity in the Halo 2 AI." GDC 2005 |
| 20 | + - **Current Relevance**: Now widely used in: |
| 21 | + - Game engines (Unreal Engine, Unity) |
| 22 | + - Robotics and autonomous systems (ROS/ROS2 ecosystems) |
| 23 | + - Safety-critical applications requiring deterministic, explainable behavior |
| 24 | + - Modern AI systems as high-level controllers for reinforcement learning and planning systems |
| 25 | + - ## Key Principles |
| 26 | + - **Hierarchical Structure**: Behavior trees organize decisions in a tree structure, allowing complex behaviors to be decomposed into simpler components |
| 27 | + - **Composability**: Simple behaviors can be combined into more complex behaviors through composition |
| 28 | + - **Reactivity**: BTs can respond to changing conditions and environment states |
| 29 | + - **Determinism**: Given the same inputs and state, a behavior tree produces the same outputs |
| 30 | + - **Human Readability**: The tree structure is more intuitive and maintainable than large state machines |
| 31 | + - **Tick-Based Execution**: BTs use a tick-based execution semantics where the tree is evaluated periodically |
| 32 | + - **Small Node Set**: Uses a closed set of node types (Sequence, Selector, Parallel, Decorators, Actions/Conditions) that can express complex behaviors |
| 33 | + - ## Mechanism |
| 34 | + - Behavior trees execute through a **tick-based evaluation** process: |
| 35 | + - Starting from the root, nodes are evaluated in order |
| 36 | + - Each node returns a status: Success, Failure, or Running |
| 37 | + - Control flow is determined by composite nodes (Sequence, Selector, Parallel) |
| 38 | + - **Core Node Types**: |
| 39 | + - **Sequence**: Executes children in order until one fails (all must succeed) |
| 40 | + - **Selector**: Executes children in order until one succeeds (at least one must succeed) |
| 41 | + - **Parallel**: Executes multiple children simultaneously |
| 42 | + - **Decorators**: Modify the behavior of child nodes (invert, repeat, etc.) |
| 43 | + - **Actions/Conditions**: Leaf nodes that perform actual work or check conditions |
| 44 | + - **Execution Flow**: |
| 45 | + - The tree is ticked from the root |
| 46 | + - Status propagates up the tree based on node semantics |
| 47 | + - Running nodes maintain state between ticks |
| 48 | + - The tree can be interrupted and resumed based on conditions |
| 49 | + - **Control Flow + Policy**: Unlike pure control structures, BTs encode both the decision logic (control flow) and the policy (what actions to take) in a single structure |
| 50 | + - ## Examples |
| 51 | + - ### Game AI |
| 52 | + - NPCs in games use BTs to determine behaviors like: |
| 53 | + - Patrol routes (Sequence of waypoints) |
| 54 | + - Combat decisions (Selector between attack, defend, retreat) |
| 55 | + - Complex behaviors combining movement, combat, and interaction |
| 56 | + - ### Robotics |
| 57 | + - Robot control architectures use BTs for: |
| 58 | + - Task decomposition (breaking complex tasks into simpler actions) |
| 59 | + - Safety-critical behaviors (ensuring deterministic responses) |
| 60 | + - Explainable autonomy (humans can understand and verify robot decisions) |
| 61 | + - ### Modern AI Systems |
| 62 | + - **BT + RL Hybrids**: Behavior trees provide high-level structure while reinforcement learning optimizes low-level actions |
| 63 | + - **BT Compilation from Planners**: Automated planners generate behavior trees for execution |
| 64 | + - **Formal Verification**: BTs can be formally verified for safety properties |
| 65 | + - **Runtime BT Synthesis**: Systems that generate or modify BTs at runtime based on conditions |
| 66 | + - ### Implementation Example: [[Person/Kristoffer Rakstad/GitHub/bonsai]] |
| 67 | + - The **Bonsai** project provides a **Rust-native**, strongly typed implementation |
| 68 | + - Demonstrates BTs' suitability for: |
| 69 | + - Embedded systems |
| 70 | + - Robotics applications |
| 71 | + - Game engines |
| 72 | + - Deterministic execution with concurrency-aware semantics |
| 73 | + - ## Misconceptions |
| 74 | + - Behavior trees are only for games → **False**. BTs are widely used in robotics, autonomous systems, and modern AI architectures |
| 75 | + - BTs are just fancy state machines → **False**. While related, BTs offer hierarchical composition, better readability, and different execution semantics |
| 76 | + - BTs are outdated compared to neural networks → **False**. BTs are actively used alongside and in combination with modern AI techniques like RL |
| 77 | + - BTs can only express simple behaviors → **False**. Through composition, BTs can express very complex behaviors while remaining maintainable |
| 78 | + - BTs are always deterministic → **True, but context matters**. BTs themselves are deterministic, but they can interact with non-deterministic systems (like RL policies) |
| 79 | + - BTs replace all other AI techniques → **False**. BTs are often used as high-level controllers that coordinate with other AI systems |
| 80 | + - ## Related |
| 81 | + - [[AI/Behavior/Tree]] - Quick reference for behavior trees |
| 82 | + - [Sollimann/bonsai: Rust implementation of behavior trees for deterministic AI](https://github.com/Sollimann/bonsai) |
| 83 | + - [[Person/Kristoffer Rakstad]] - Creator of the Bonsai implementation |
0 commit comments