Game Development Reference
In-Depth Information
Branch evaluation
To evaluate a branch, we execute the evaluator and use the return value to further process
the tree. Once a choice is made, we either return an action node if the selected child is a
leaf, otherwise we recursively evaluate another branch until an action is found.
Note
Every branch within a decision tree must eventually end with an action node; trees without
actions as leafs are malformed and will not evaluate properly.
To implement evaluation, we'll use the type_ field to determine if a child should be con-
sidered as a branch or as an action to return.
DecisionBranch.lua :
function DecisionBranch.Evaluate(self)
-- Execute the branch's evaluator function, this much
return a
-- numeric value which indicates what child should
execute.
local eval = self.evaluator_();
local choice = self.children_[eval];
if (choice.type_ == DecisionBranch.Type) then
-- Recursively evaluate children that are decisions
-- branches.
return choice:Evaluate();
else
-- Return the leaf action.
return choice;
end
end
Search WWH ::




Custom Search