Package teamwork :: Package math :: Module matrices :: Class DecisionTree
[hide private]
[frames] | no frames]

Class DecisionTree

source code

Known Subclasses:

Represents a decision tree with hyperplane branches that divide an n-dimensional space, and unrestricted values stored at the leaf nodes (e.g., matrices for dynamics, actions for policies, etc.)

Nested Classes [hide private]
  planeClass
the class used to instantiate the branches
Instance Methods [hide private]
 
__init__(self, value=None)
Creates a DecisionTree
source code
 
makeLeaf(self, value)
Marks this tree as a leaf node
source code
 
getValue(self)
Returns: the value of this tree
source code
boolean
isLeaf(self)
Returns: True iff this tree is a leaf node
source code
list of DecisionTree instances
children(self)
Returns: all subtrees rooted at this node
source code
 
leaves(self)
Returns: list of all leaf values (not necessarily unique) from L to R
source code
DecisionTree[]
leafNodes(self)
Returns: list of all leaf nodes (not necessarily unique) from L to R
source code
int
depth(self)
Returns: the maximum distance between this node and the leaf nodes of the tree rooted at this node (a leaf node has a depth of 0, a branch node with two leaf nodes as children has a depth of 1, etc.)
source code
int→Hyperplane
branches(self, result=None)
Returns: all branches (not necessarily unique)
source code
 
branch(self, plane, falseTree, trueTree, pruneF=True, pruneT=True, debug=False)
Marks this tree as a deterministic branching node
source code
(Hyperplane,boolean)[]
getPath(self)
Returns: the conditions under which this node will be reached, as a list of (plane,True/False) tuples
source code
 
createIndex(self, start=0) source code
 
removeTautologies(self, negative=True) source code
 
prune(self, comparisons=None, debug=False, negative=True) source code
 
count(self)
Returns: a dictionary of statistics about the decision tree rooted at this node:
source code
 
rebalance(self, debug=False)
Uses ID3 heuristic to reorder branches
source code
 
_extractTree(self, tree, attributes, values)
Extracts the rules from the given dtree structure into this tree
source code
 
makeRules(self, attributes=None, values=None, conditions=None, debug=False, comparisons=None)
Represents this tree as a list of rules
source code
 
fromRules(self, rules, attributes, values, comparisons=None) source code
 
generateAlternatives(self, index, value, test=None) source code
 
__getitem__(self, index) source code
 
replace(self, orig, new, comparisons=None, conditions=[])
Replaces any leaf nodes that match the given original value with the provided new value, followed by a pruning phase
source code
a new DecisionTree instance
merge(self, other, op)
Merges the two trees together using the given operator to combine leaf values
source code
a new DecisionTree instance
_merge(self, other, op, comparisons=None, conditions=[])
Helper method that merges the two trees together using the given operator to combine leaf values, without pruning
source code
 
__add__(self, other) source code
 
__mul__(self, other) source code
 
_multiply(self, other, comparisons=None, conditions=[]) source code
 
__sub__(self, other) source code
 
__neg__(self) source code
 
__eq__(self, other) source code
 
__hash__(self) source code
 
__str__(self) source code
 
__copy__(self) source code
 
__xml__(self) source code
 
parse(self, element, valueClass=None, debug=False)
Extracts the tree from the given XML element
source code
Class Variables [hide private]
boolean checkTautology = False
flag that, if True, activates check for hyperplanes that are either always True or always False in branch.
boolean checkPrune = True
flag that, if True, activates the prune method.
Method Details [hide private]

__init__(self, value=None)
(Constructor)

source code 

Creates a DecisionTree

Parameters:
  • value - the optional leaf node value

makeLeaf(self, value)

source code 

Marks this tree as a leaf node

Parameters:
  • value - the new value of this leaf node

getValue(self)

source code 
Returns:
the value of this tree
  • If a leaf node, as a single object
  • If a branch, as a tuple (falseTree,trueTree)

isLeaf(self)

source code 
Returns: boolean
True iff this tree is a leaf node

children(self)

source code 
Returns: list of DecisionTree instances
all subtrees rooted at this node

leaves(self)

source code 
Returns:
list of all leaf values (not necessarily unique) from L to R

Note: the leaf value is the result of calling getValue, not an actual DecisionTree instance

leafNodes(self)

source code 
Returns: DecisionTree[]
list of all leaf nodes (not necessarily unique) from L to R

depth(self)

source code 
Returns: int
the maximum distance between this node and the leaf nodes of the tree rooted at this node (a leaf node has a depth of 0, a branch node with two leaf nodes as children has a depth of 1, etc.)

branches(self, result=None)

source code 
Returns: int→Hyperplane
all branches (not necessarily unique)

branch(self, plane, falseTree, trueTree, pruneF=True, pruneT=True, debug=False)

source code 

Marks this tree as a deterministic branching node

Parameters:
  • plane (Hyperplane or Hyperplane[]) - the branchpoint(s) separating the False and True subtrees
  • falseTree (DecisionTree instance) - the False subtree
  • trueTree (DecisionTree instance) - the True subtree
  • pruneF (bool) - if true, will prune the False subtree
  • pruneT (bool) - if true, will prune the True subtree
  • debug (bool) - if True, some debugging statements will be written to stdout (default is False)

Note: setting either prune flag to false will save time (though may lead to more inefficient trees)

getPath(self)

source code 
Returns: (Hyperplane,boolean)[]
the conditions under which this node will be reached, as a list of (plane,True/False) tuples

count(self)

source code 
Returns:
a dictionary of statistics about the decision tree rooted at this node:
  • leaf: # of leaves
  • branch: # of branch nodes
  • depth: depth of tree

rebalance(self, debug=False)

source code 

Uses ID3 heuristic to reorder branches

Returns:
True, iff a rebalancing was applied at this level

makeRules(self, attributes=None, values=None, conditions=None, debug=False, comparisons=None)

source code 

Represents this tree as a list of rules

Returns:
dict[]

replace(self, orig, new, comparisons=None, conditions=[])

source code 

Replaces any leaf nodes that match the given original value with the provided new value, followed by a pruning phase

Parameters:
  • orig - leaf value to be replaced
  • new - leaf value with which to replace

Warning: the replacement modifies this tree in place

merge(self, other, op)

source code 

Merges the two trees together using the given operator to combine leaf values

Parameters:
  • other (DecisionTree instance) - the other tree to merge with
  • op - the operator used to generate the new leaf values, lambda x,y:f(x,y) where x and y are leaf values
Returns: a new DecisionTree instance

_merge(self, other, op, comparisons=None, conditions=[])

source code 

Helper method that merges the two trees together using the given operator to combine leaf values, without pruning

Parameters:
  • other (DecisionTree instance) - the other tree to merge with
  • op - the operator used to generate the new leaf values, lambda x,y:f(x,y) where x and y are leaf values
Returns: a new DecisionTree instance

parse(self, element, valueClass=None, debug=False)

source code 

Extracts the tree from the given XML element

Parameters:
  • element (Element) - The XML Element object specifying the plane
  • valueClass - The class used to generate the leaf values
Returns:
the KeyedTree instance

Class Variable Details [hide private]

checkTautology

flag that, if True, activates check for hyperplanes that are either always True or always False in branch. This can lead to smaller trees, but decreases efficiency
Type:
boolean
Value:
False

checkPrune

flag that, if True, activates the prune method. This will lead to much smaller trees, but increases the overhead required to check for pruneability
Type:
boolean
Value:
True