Instance Method

initWithExamples:actions:attributes:

Creates an automatically learned decision tree using the specified attributes, example items, and actions.

Declaration

- (instancetype)initWithExamples:(NSArray<NSArray<id<NSObject>> *> *)examples actions:(NSArray<id<NSObject>> *)actions attributes:(NSArray<id<NSObject>> *)attributes;

Parameters

examples

An array of arrays. Each entry in the outer array is a list of example inputs, one for each attribute in the attributes array.

actions

An array of actions. Each item in this array is the action, or final decision, corresponding to the example input at the same index in the examples array.

attributes

An array of questions, or attributes to be tested by the decision tree. In the inner arrays of the examples array, each entry corresponds to the attribute at the same index in this array.

Return Value

A new decision tree.

Discussion

When you use this initializer, the GKDecisionTree class automatically infers a decision tree structure that chooses actions based on the attribute values in the examples array. That is, if you later use the findActionForAnswers: method with a set of attribute values (or answers to questions) matching one of the entries in the examples array, the tree will choose the action at the corresponding index in the actions array. However, a decision tree is a predictive model, so the created tree is not limited to handling cases that exactly match the examples—if you call the findActionForAnswers: with a set of answers not in the examples array, the tree predicts an action by generalizing from similar examples.

For example the following code creates a tree with data similar to that shown in Table 1:

NSArray *attributes = @[@"Type?", @"HP?", @"Special?"];
NSArray *examples = @[
    // Each sub-array has a value for each attribute:
    @[@(MyEnemyTypeElectric), @10, @YES],  // Note: enum value for the "Type?" attribute
    @[@(MyEnemyTypeElectric), @30, @NO],
    @[@(MyEnemyTypeElectric), @40, @YES],
    @[@(MyEnemyTypeFire), @20, @NO],
    @[@(MyEnemyTypeFire), @30, @NO],
    @[@(MyEnemyTypeWater), @50, @YES],
    @[@(MyEnemyTypeWater), @40, @NO],
];
NSArray *actions = @[ // One for each entry in the examples array
    @"Psychic Strike",
    @"Pound",
    @"Barrier",
    @"Pound",
    @"Tackle",
    @"Pound",
    @"Tackle",
];
 
GKDecisionTree *decisionTree = [[GKDecisionTree alloc] initWithExamples:examples actions:actions attributes:attributes];

When creating an automatically learned decision tree, be sure to provide enough examples to predict actions for all of the possible answer sets you expect to handle, and no contradictory examples.

For example, Figure 1 shows the tree inferred from the input in the above code example. This tree always chooses the “Psychic Strike” action when the opponent’s health is below 15, because the only example with that action fits that condition. If you want to choose that action based on answers to both the “HP?” and “Special?” attributes, you’d need to provide multiple examples where that action corresponds to different sets of attribute values.