ios - 使用 Swift 在 iOS 中做出决策的最佳方式是什么
问题描述
我想用简单的 AI 制作一个简单的应用程序。我做了很多研究,发现了一些关于决策树、规则和行为树的文章。
我看到了 WWDC2016 的视频,其中展示了新的 GKDecisionTree。也许这可能是我的应用程序的简单解决方案。
我尝试了这段代码,但在这一行出现错误:
let tree = GKDecisionTree(attribute: "anrgy?")
参数类型“字符串”不符合预期类型“NSObjectProtocol”
// SETUP TREE
let tree = GKDecisionTree(attribute: "anrgy?")
let root = tree?.rootNode
// ADD BRANCH
// Create branches
root.createBranch(value: true, attribute: "attack")
let goAway = root.createBranch(value: false, attribute: "goAway")
// Create actions for when nearby
goAway.createBranch(withWeight: 9, attribute: "Left")
goAway.createBranch(withWeight: 1, attribute: "Right")
// Find action for answers
// Find action for answers
let answers = ["anrgy?" : true]
tree.findActionForAnswers(answers: answers)
请让我知道是否有更好的简单 AI 方法或如何修复此示例。
谢谢您的帮助。
解决方案
此代码有效:
// SETUP TREE
let tree = GKDecisionTree(attribute: "anrgy?" as NSObjectProtocol)
let root = tree.rootNode
// ADD BRANCH
// Create branches
root?.createBranch(value: true, attribute: "attack" as NSObjectProtocol)
let goAway = root?.createBranch(value: false, attribute: "goAway" as NSObjectProtocol)
// Create actions for when nearby
goAway?.createBranch(weight: 9, attribute: "Left" as NSObjectProtocol)
goAway?.createBranch(weight: 1, attribute: "Right" as NSObjectProtocol)
// Find action for answers
// Find action for answers
let answers = ["anrgy?" : false]
let decisionAction = tree.findAction(forAnswers: answers as [AnyHashable : NSObjectProtocol])
print("Answer: \(String(describing: decisionAction!))")
}
但这是一种好的编码风格吗?还有比使用 GKDecisionTree 更好的方法吗?
推荐阅读
- microsoft-graph-api - 架构扩展:“为资源 'User' 的属性 'companyName' 指定了不受支持或无效的查询过滤器子句。”
- c++ - 如何从命名空间转发声明枚举?
- php - 用于 API 响应的 Laravel 自定义包装器
- sql - 在 postgresql 中创建 N 天间隔函数
- r - 在R中的另一个列表中迭代一个列表
- r - 为 geom_line 和 geom_point 指定组
- google-apps-script - Apps Scripts Google Drive API 可以与共享驱动器对话吗
- dictionary - 如何获取 DolphinDB 中字典选定键值的最后一个元素?
- node.js - Express.js 应用程序中的 Typescript Http.Post 或 Fetch
- sql - 在 Sqlite 中,为不同列中返回的每个名称获取前 2 个