3

I want to make a simple app with a simple AI. I did allot of research and found some articles about decision-trees, Rules and behavior-trees.

I saw a video of the WWDC2016 where the new GKDecisionTree was presented. Maybe this could be simple solution for my app.

I tried this code but I got an error in this line:

let tree = GKDecisionTree(attribute: "anrgy?")

Argument type 'String' does not conform to expected type '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)

Please let me know if there is a better way for a simple AI or how to fix this example.

Thank you for your help.

Boke
  • 75
  • 2
  • 7

2 Answers2

2

This code is working:

        // 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!))")
    }

But is this a good coding style? And are there better ways than using GKDecisionTree?

Boke
  • 75
  • 2
  • 7
0

Here is an alternative using a learned decision tree based on your example. Both manually defined decision trees and learned decision trees use the GKDecisionTree class, but different initializers:

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)
John
  • 232
  • 3
  • 9