#planning #ai #gamedev #goap

bin+lib goap-ai

面向目标的行为规划(GOAP)AI 库

1 个不稳定版本

0.0.0 2023 年 12 月 24 日

#43#planning

MIT/Apache

1MB
108

GOAP

GOAP logo

GOAP(面向目标的行为规划)是人工智能系统中使用的一种声明式编程形式。它涉及定义一组由代理执行的动作,每个动作都具有前提条件和效果,以在动态环境中实现指定的目标。

使用 A* 搜索算法进行动作规划,以找到实现所需状态的优化动作方案。通过使用启发式函数来估计每个动作的成本来提高性能。

示例

导入库

from goap import Action, create_plan

并将当前世界的 State 定义为

initial_state = {
    "music_playing": False,
    "wood_count": 0,
    "has_fire": False,
    "is_warm": False,
}

以及我们想要实现的目标为

goal_state = {
    "is_warm": True,
}

然后定义一组可能 Actions

actions = [
    Action("play_the_piano", 3, {}, {"music_playing": True}),
    Action("gather_wood", 2, {}, {"wood_count": 1}),
    Action("build_fire", 1, {"wood_count": 3}, {"has_fire": True}),
    Action("sit_by_fire", 1, {"has_fire": True}, {"is_warm": True}),
]

注意:Action 类接受四个参数:namecostpreconditionseffects

最后,我们可以使用 create_plan 创建方案,以找到实现所需状态的优化动作方案

plan, cost = create_plan(goal, actions, initial_state)

这个特定的示例将返回以下方案

Plan found: gather_wood -> gather_wood -> gather_wood -> build_fire -> sit_by_fire | Total Cost: 8

快速入门

克隆存储库并将根文件夹设置为当前工作目录

git clone https://github.com/FreddyWordingham/GOAP.git goap
cd goap

使用 Poetry 安装包

poetry env use python@3.10
poetry install

然后运行其中一个示例脚本

poetry run python scripts/run.py

无运行时依赖