在 C# 中创建 AI 行为树 - 如何?
我正在尝试使用 C# 创建“行为树”。
对于任何不知道的人来说,行为树基本上是一个可以围绕它构建人工智能的框架。有序列器、选择器、装饰器、复合动作和其他东西。
我找到了一个在 C# 中实现“行为树”的库,位于此处 (http:// code.google.com/p/treesharp/),但我无法理解如何实际使用它,因为没有可以借鉴的示例代码。这里有人可能会制作一些简单的示例代码来展示如何实际使用这个框架..或者也许您知道在 C# 中实现行为树的另一种方法?
非常感谢!
I am attempting to create a "behavior tree" using C#.
For anyone who doesn't know, a behavior tree is basically a framework that you can construct an AI around. There are Sequencers, Selectors, Decorators, composite actions, and other things.
I have found a single library that has implimented a "behavior tree" in C#, located here (http://code.google.com/p/treesharp/) but I cannot understand how to actually use it since there is no example code I can draw from. Could anyone here perhaps make some simple example code that shows how to actually use this framework.. or perhaps you know of another way to impliment a behavior tree in C#?
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚查看了该实现,我发现自己想知道为什么相对简单的东西需要这么多代码。
从你所说的来看,你想要一种简单的方式来组成行为。我认为这里的行为是代理从状态到零个或多个动作的映射。您可以使用 C# lambda 非常轻松地对此进行建模。例如:
树的叶子是简单的操作,可以执行适合状态的操作。您只需执行它即可“运行”一棵树。
如果您想变得更奇特,您可以参数化此方案以使状态显式化。
希望这有帮助。
---- 附录 ----
Jason 要求提供一个如何使用这种方法的示例,所以这里有一个简单的“AI”巡逻守卫示例(我假设 WorldState 对应于行为树生成时的环境描述)评估):
要让守卫做某事,只需调用
patrollingGuardBehaviour()
。请注意,各种子操作和测试可以作为具有正确签名的方法来实现,而不是作为 lambda 内联。您可以向Selector
和Sequencer
添加其他组合器,例如用于并行活动。I just looked at that implementation and I find myself wondering why so much code is needed for something relatively simple.
From what you say, you want a simple way of composing behaviours. A behaviour here, I presume, is a mapping from a state to zero or more actions by an agent. You can model this very easily using C# lambdas. For example:
The leaves of your tree are simple Actions that do something appropriate to the state. You "run" a tree simply by executing it.
If you want to get fancy, you can parameterise this scheme to make the state explicit.
Hope this helps.
---- Addendum ----
Jason asked for an example of how you could use this approach, so here's a simple "AI" patrolling guard example (I assume WorldState corresponds to a description of the environment at the time the behaviour tree is evaluated):
To make the guard do something, just call
patrollingGuardBehaviour()
. Note that the various subactions and tests can be implemented as methods with the right signatures rather than inline as lambdas. You can add other combinators toSelector
andSequencer
, e.g., for parallel activity.看起来 TreeSharp 背后的开发人员之一 apocdev 有一些 使用 TreeSharp 进行某种魔兽世界玩家施法的代码。
这是一个片段:
我不确定,但这里的用法似乎非常简单:
Decorator
类看起来在尝试执行之前检查谓词 (Spells.CanCast
)一些动作(Spells.Cast
)。因此,
Composite
可能是一个可以执行多项操作的Action
,例如,预先检查谓词或按顺序执行多个操作。apocdev 的博客 提到 < a href="http://aigamedev.com/open/articles/bt-overview/" rel="nofollow">行为树概述,链接到 序列,选择器和装饰器。
It looks like one of the devs behind TreeSharp, apocdev, has some code that uses TreeSharp for some kind of spell-casting World of Warcraft player.
Here's a snippit:
I'm not certain, but the usage here seems pretty simple: the
Decorator
class looks like it checks a predicate (Spells.CanCast
) before trying to execute some action (Spells.Cast
).So a
Composite
is perhaps anAction
that can do several things, e.g. check a predicate beforehand or execute several actions in sequence.apocdev's blog mentions this overview of behavior trees, which links to more general descriptions of sequences, selectors, and decorators.
当 C# lambda 涉及闭包时,它们会变得昂贵,因为这将导致 BT 的每个帧/迭代都进行分配。
您可以使用黑板来避免关闭,但还有一种更简单的方法。
您可以使用短路条件运算符
&&
和||
来实现行为树。这种方法如下所示:
https://github.com/eelstork
那么巡逻示例将如下所示:
C# lambdas get expensive when they involve closures as this will cause allocations at every frame/iteration of your BT.
You can avoid closures using a blackboard, but there is an easier approach.
You can implement behavior trees using the short-circuiting conditional operators
&&
and||
.This approach is illustrated here:
https://github.com/eelstork
Then the patrol example would look like: