Behavior Tree Basic Node Types
We have defined a number of basic node types.
These are the building blocks of any behavior tree.
They are defined in the vultron.bt.base
module.
vultron.bt.base.node_status
This module defines a Behavior Tree Node Status object.
NodeStatus
Bases: Enum
NodeStatus is the return value of a node's tick() method. Nodes can only return one of these values.
Nodes return SUCCESS if they have completed their task successfully. Nodes return FAILURE if they have completed their task unsuccessfully. Nodes return RUNNING if they are still in the process of completing their task.
Source code in vultron/bt/base/node_status.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
vultron.bt.base.bt_node
This module provides the base class for all nodes in the Behavior Tree.
It also provides a number of core node types that can be used to build a Behavior Tree.
ActionNode
Bases: LeafNode
ActionNode is the base class for all action nodes in the Behavior Tree. Action nodes are leaf nodes that perform an action. An action node's func() method returns True for success, False for failure, and None for running.
Source code in vultron/bt/base/bt_node.py
331 332 333 334 335 336 337 338 |
|
BtNode
BtNode is the base class for all nodes in the Behavior Tree.
Source code in vultron/bt/base/bt_node.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
add_child(child)
Adds a child to the node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
child |
BtNode
|
the child node to add |
required |
Returns:
Type | Description |
---|---|
BtNode
|
the child node |
Source code in vultron/bt/base/bt_node.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
add_children()
Adds children to the node. Loops through the _children list and creates a new instance of each child class.
Source code in vultron/bt/base/bt_node.py
108 109 110 111 112 113 114 115 116 117 118 |
|
setup()
Sets up the node and its children.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in vultron/bt/base/bt_node.py
80 81 82 83 84 85 86 87 88 89 90 91 |
|
tick(depth=0)
Ticks the node. Performs the following actions:
- calls
_pre_tick()
- calls
_tick()
- calls
_post_tick()
- sets the node's status based on the return value of
_tick()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
depth |
int
|
the node's depth in the tree |
0
|
Returns:
Type | Description |
---|---|
NodeStatus
|
the node's status (as a NodeStatus enum) |
Source code in vultron/bt/base/bt_node.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
to_mermaid(depth=0, topdown=True)
Returns a string representation of the tree rooted at this node in mermaid format.
Source code in vultron/bt/base/bt_node.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
to_str(depth=0)
Returns a string representation of the tree rooted at this node.
Source code in vultron/bt/base/bt_node.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
ConditionCheck
Bases: LeafNode
ConditionCheck is the base class for all condition check nodes in the Behavior Tree. Condition check nodes are leaf nodes that check a condition. A condition check node's func() method returns True for success, False for failure. Although it is possible to return None for running, this is not recommended.
Source code in vultron/bt/base/bt_node.py
341 342 343 344 345 346 347 348 349 350 |
|
CountTicks
Bases: BtNode
CountTicks is a decorator node that counts the number of times it is ticked.
Source code in vultron/bt/base/bt_node.py
353 354 355 356 357 358 359 360 361 362 363 364 |
|
LeafNode
Bases: BtNode
LeafNode is the base class for all leaf nodes in the Behavior Tree. Leaf nodes are nodes that do not have children.
Source code in vultron/bt/base/bt_node.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
|
__init__()
Raises a LeafNodeError if the node has children.
Source code in vultron/bt/base/bt_node.py
295 296 297 298 299 300 301 |
|
func()
Override this method in your subclass. Return True for success, False for failure, and None for running.
Source code in vultron/bt/base/bt_node.py
303 304 305 306 307 308 309 |
|
SnapshotState
Bases: BtNode
SnapshotState is a decorator node that takes a snapshot of the blackboard and appends it to the STATELOG list.
Source code in vultron/bt/base/bt_node.py
370 371 372 373 374 375 376 377 378 379 380 381 |
|
vultron.bt.base.composites
This module implements Composite Nodes for a Behavior Tree.
FallbackNode
Bases: BtNode
FallbackNode is a composite node that ticks its children in order.
- If a child returns SUCCESS, the FallbackNode returns SUCCESS.
- If a child returns RUNNING, the FallbackNode returns RUNNING.
- If a child returns FAILURE, the FallbackNode ticks the next child.
- If all children return FAILURE, the FallbackNode returns FAILURE.
Source code in vultron/bt/base/composites.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
ParallelNode
Bases: BtNode
ParallelNode is a composite node that ticks its children in parallel.
When subclassing, you must set the m
attribute indicating the minimum number of successes.
The maximum failures is then calculated as N - m
, where N is the number of children.
- When a child returns SUCCESS, the ParallelNode increments a success counter.
- When a child returns FAILURE, the ParallelNode increments a failure counter.
- If the success counter reaches the minimum number of successes, the ParallelNode returns SUCCESS.
- If the failure counter reaches the maximum number of failures, the ParallelNode returns FAILURE.
- If neither of the above conditions are met, the ParallelNode returns RUNNING.
Not Fully Implemented
In the current implementation, the ParallelNode does not actually tick its children in parallel. Instead, it ticks them in a random order and counts the results until either SUCCESS or FAILURE is indicated as above. This is good enough for demonstration purposes, but it should be replaced with a proper parallel implementation.
Source code in vultron/bt/base/composites.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
SequenceNode
Bases: BtNode
SequenceNode is a composite node that ticks its children in order.
- If a child returns SUCCESS, the SequenceNode ticks the next child.
- If a child returns RUNNING, the SequenceNode returns RUNNING.
- If a child returns FAILURE, the SequenceNode returns FAILURE.
- If all children return SUCCESS, the SequenceNode returns SUCCESS.
Source code in vultron/bt/base/composites.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
vultron.bt.base.decorators
This module defines a number of Behavior Tree Decorator Nodes.
BtDecorator
Bases: BtNode
BtDecorator is the base class for all decorators in the Behavior Tree.
Source code in vultron/bt/base/decorators.py
25 26 27 28 29 30 |
|
ForceFailure
Bases: BtDecorator
ForceFailure decorator returns FAILURE no matter what the child node returns.
Source code in vultron/bt/base/decorators.py
94 95 96 97 98 99 100 101 102 103 |
|
ForceRunning
Bases: BtDecorator
ForceRunning decorator returns RUNNING no matter what the child node returns.
Source code in vultron/bt/base/decorators.py
106 107 108 109 110 111 112 113 114 115 |
|
ForceSuccess
Bases: BtDecorator
ForceSuccess decorator returns SUCCESS no matter what the child node returns.
Source code in vultron/bt/base/decorators.py
82 83 84 85 86 87 88 89 90 91 |
|
Invert
Bases: BtDecorator
Inverts the result of the child node.
- If the child node returns SUCCESS, the Invert decorator will return FAILURE.
- If the child node returns FAILURE, the Invert decorator will return SUCCESS.
- If the child node returns RUNNING, the Invert decorator will return RUNNING.
Source code in vultron/bt/base/decorators.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
LoopDecorator
Bases: BtDecorator
LoopDecorator is the base class for all decorators that loop.
Source code in vultron/bt/base/decorators.py
118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
RepeatN
Bases: LoopDecorator
Repeat up to n times until the child returns failure or running.
When subclassing RepeatN, set the n
class variable to the number of repeats.
Source code in vultron/bt/base/decorators.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
RepeatUntilFail
Bases: LoopDecorator
Repeat until the child returns FAILURE, then return SUCCESS.
Source code in vultron/bt/base/decorators.py
178 179 180 181 182 183 184 185 186 187 188 189 |
|
RetryN
Bases: LoopDecorator
Retry up to n times until the child returns success or running.
When subclassing RetryN, set the n
class variable to the number of retries.
Source code in vultron/bt/base/decorators.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
RunningIsFailure
Bases: BtDecorator
RunningIsFailure decorator returns FAILURE if the child node returns RUNNING. Otherwise, it returns the result of the child node.
Source code in vultron/bt/base/decorators.py
54 55 56 57 58 59 60 61 62 63 64 65 |
|
RunningIsSuccess
Bases: BtDecorator
RunningIsSuccess decorator returns SUCCESS if the child node returns RUNNING. Otherwise, it returns the result of the child node.
Source code in vultron/bt/base/decorators.py
68 69 70 71 72 73 74 75 76 77 78 79 |
|
vultron.bt.base.fuzzer
This module provides fuzzer node classes for a Behavior Tree. It is intended to be used for testing, simulation, and debugging.
Many of the classes provided are subclasses of the WeightedSuccess class. Their names are based in part on the Words of Estimative Probability from Wikipedia and the Probability Survey (github) by Mauboussin and Mauboussin.
AlmostAlwaysFail
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.10.
Source code in vultron/bt/base/fuzzer.py
184 185 186 187 |
|
AlmostAlwaysSucceed
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.90.
Source code in vultron/bt/base/fuzzer.py
136 137 138 139 |
|
AlmostCertainlyFail
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.07.
Source code in vultron/bt/base/fuzzer.py
190 191 192 193 |
|
AlmostCertainlySucceed
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.93.
Source code in vultron/bt/base/fuzzer.py
130 131 132 133 |
|
AlwaysFail
Bases: FuzzerNode
Always returns NodeStatus.FAILURE
Source code in vultron/bt/base/fuzzer.py
40 41 42 43 44 |
|
AlwaysRunning
Bases: FuzzerNode
Always returns NodeStatus.RUNNING
Source code in vultron/bt/base/fuzzer.py
47 48 49 50 51 |
|
AlwaysSucceed
Bases: FuzzerNode
Always returns NodeStatus.SUCCESS
Source code in vultron/bt/base/fuzzer.py
33 34 35 36 37 |
|
FortyNineInFifty
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.98.
Source code in vultron/bt/base/fuzzer.py
112 113 114 115 |
|
NineteenInTwenty
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.95.
Source code in vultron/bt/base/fuzzer.py
124 125 126 127 |
|
NinetyNineInOneHundred
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.99.
Source code in vultron/bt/base/fuzzer.py
106 107 108 109 |
|
OftenFail
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.30.
Source code in vultron/bt/base/fuzzer.py
172 173 174 175 |
|
OftenSucceed
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.70.
Source code in vultron/bt/base/fuzzer.py
148 149 150 151 |
|
OneInFifty
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.02.
Source code in vultron/bt/base/fuzzer.py
208 209 210 211 |
|
OneInOneHundred
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.01.
Source code in vultron/bt/base/fuzzer.py
214 215 216 217 |
|
OneInThirty
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.033333.
Source code in vultron/bt/base/fuzzer.py
202 203 204 205 |
|
OneInTwenty
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.05.
Source code in vultron/bt/base/fuzzer.py
196 197 198 199 |
|
OneInTwoHundred
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.005.
Source code in vultron/bt/base/fuzzer.py
220 221 222 223 |
|
OneNinetyNineInTwoHundred
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.995.
Source code in vultron/bt/base/fuzzer.py
100 101 102 103 |
|
ProbablyFail
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.3333.
Source code in vultron/bt/base/fuzzer.py
166 167 168 169 |
|
ProbablySucceed
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.6667.
Source code in vultron/bt/base/fuzzer.py
154 155 156 157 |
|
RandomActionNodeWithRunning
Bases: FuzzerNode
Returns a random NodeStatus, including NodeStatus.RUNNING, with equal probability.
Source code in vultron/bt/base/fuzzer.py
54 55 56 57 58 |
|
SuccessOrRunning
Bases: FuzzerNode
Returns NodeStatus.SUCCESS or NodeStatus.RUNNING with equal probability.
Source code in vultron/bt/base/fuzzer.py
61 62 63 64 65 |
|
TwentyNineInThirty
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.96667.
Source code in vultron/bt/base/fuzzer.py
118 119 120 121 |
|
UniformSucceedFail
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.50.
Source code in vultron/bt/base/fuzzer.py
160 161 162 163 |
|
UsuallyFail
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.25.
Source code in vultron/bt/base/fuzzer.py
178 179 180 181 |
|
UsuallySucceed
Bases: WeightedSuccess
Returns NodeStatus.SUCCESS with a probability of 0.75.
Source code in vultron/bt/base/fuzzer.py
142 143 144 145 |
|
WeightedSuccess
Bases: FuzzerNode
Returns NodeStatus.SUCCESS with a probability of success_rate. Otherwise, returns NodeStatus.FAILURE.
When subclassing, set the success_rate
class attribute to the desired
probability of success.
Source code in vultron/bt/base/fuzzer.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|