首页 > 解决方案 > 使用 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 方法或如何修复此示例。

谢谢您的帮助。

标签: iosswiftartificial-intelligencedecision-treegameplay-kit

解决方案


此代码有效:

        // 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 更好的方法吗?


推荐阅读