我想用一个简单的AI做一个简单的应用程序。我做了大量的研究,找到了一些关于决策树、规则和行为树的文章。
我在展示新GKDecisionTree的WWDC2016上看到了一段视频。也许这对我的应用程序来说是一个简单的解决方案。
我尝试了这段代码,但在这一行得到了一个错误:
let tree = GKDecisionTree(attribute: "anrgy?")
参数类型“String”不符合预期的类型“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)请让我知道,如果有更好的方法,一个简单的人工智能或如何解决这个例子。
谢谢你的帮助。
发布于 2019-11-30 19:10:28
这段代码正在运行:
// 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更好的方法吗?
发布于 2020-08-16 09:49:26
以下是基于您的示例使用学习决策树的替代方案。手动定义的决策树和学习的决策树都使用GKDecisionTree类,但初始化器不同:
let attributes = [ "angry?", "goAway" ]
let examples = [
[true, 0],
[false, 9],
[false, 1],
]
let actions = [
"attack",
"Left",
"Right",
]
let tree = GKDecisionTree(examples: examples as NSArray as! [[NSObjectProtocol]],
actions: actions as NSArray as! [NSObjectProtocol],
attributes: attributes as NSArray as! [NSObjectProtocol])
let answers = ["angry?" : true as NSObjectProtocol,
"goAway": 1 as NSObjectProtocol]
let decisionAction2 = tree.findAction(forAnswers: answers)https://stackoverflow.com/questions/59115383
复制相似问题