Package teamwork :: Package test :: Package math :: Module testKeyedTree
[hide private]
[frames] | no frames]

Source Code for Module teamwork.test.math.testKeyedTree

  1  from teamwork.math.Keys import * 
  2  from teamwork.math.KeyedMatrix import * 
  3  from teamwork.math.KeyedTree import * 
  4  from testPWL import TestPWL,makeTree,makeState,makePlane 
  5   
  6  import copy 
  7  import hotshot,hotshot.stats 
  8  import time 
  9  import unittest 
 10           
11 -class TestKeyedTree(TestPWL):
12 13 """TestCase for L{teamwork.math.KeyedTree} class 14 @cvar iterations: the number of times to repeat each test 15 @type iterations: int 16 @cvar horizon: the number of steps to consider in a projection 17 @type horizon: int 18 @cvar filename: name of temporary file to store profiling data 19 @cvar agents: a list of entity names to use as test cases 20 @type agents: str[] 21 @cvar features: a list of state features to use as test case 22 @type features: str[] 23 """ 24 filename = '/tmp/keyedtree.prof' 25 iterations = 10 26 horizon = 4 27 features = [ 28 'strength', 29 'dexterity', 30 'constitution', 31 'intelligence', 32 'charisma', 33 'wisdom', 34 ] 35 agents = [ 36 'warrior', 37 'wizard', 38 'cleric', 39 'thief', 40 ] 41
42 - def testXML(self):
43 old = makeTree(self.keys,5) 44 doc = old.__xml__() 45 new = KeyedTree() 46 new.parse(doc.documentElement,valueClass=KeyedMatrix) 47 self.verifyTree(old,new)
48
49 - def testCopy(self):
50 old = makeTree(self.keys,4) 51 new = copy.copy(old) 52 self.verifyTree(old,new) 53 self.assert_(not old is new) 54 new = copy.deepcopy(old) 55 self.verifyTree(old,new) 56 self.assert_(not old is new)
57
58 - def testTest(self):
59 for treeIndex in range(self.iterations): 60 tree = makeTree(self.keys,4) 61 for stateIndex in range(self.iterations): 62 state = makeState(self.keys,1.) 63 matrix = tree[state] 64 self.assert_(isinstance(matrix,KeyedMatrix))
65
66 - def testMultiply(self):
67 vecLength = 4 # len(self.keys) 68 for index in range(self.iterations): 69 # Create a sequence of dynamics trees 70 trees = [] 71 for step in range(self.horizon): 72 tree = makeTree(self.keys[:vecLength],3) 73 ## print step,tree.toNumeric() 74 trees.append(tree) 75 # Accumulate the trees into a single tree 76 product = trees[0] 77 for step in range(1,self.horizon): 78 ## prof = hotshot.Profile(self.filename) 79 ## prof.start() 80 product = trees[step] * product 81 ## prof.stop() 82 ## prof.close() 83 ## print 'loading stats...' 84 ## stats = hotshot.stats.load(self.filename) 85 ## stats.strip_dirs() 86 ## stats.sort_stats('time', 'calls') 87 ## stats.print_stats() 88 ## print 'product:',product.toNumeric() 89 for rep in range(self.iterations): 90 # Test on a variety of initial states 91 state = makeState(self.keys[:vecLength],1.) 92 state.freeze() 93 ## print 'start:',state.array.transpose() 94 # Explicitly iterate through the individual dynamics trees 95 iterative = state 96 for step in range(0,self.horizon): 97 iterative = trees[step][iterative] * iterative 98 # Compare against cumulative state 99 cumulative = product[state] * state 100 self.verifyVector(iterative,cumulative)
101
102 - def DONTtestRebalance(self):
103 vecLength = 1 104 for index in range(self.iterations): 105 # We need a good candidate for rebalancing. This is our 106 # oh-so-clever way of randomly generating one. 107 tree1 = makeTree(self.keys[:vecLength],1,1.) 108 tree2 = makeTree(self.keys[:vecLength],1,1.) 109 subFalse,subTrue = tree2.getValue() 110 subFalse.makeLeaf('F') 111 subTrue.makeLeaf('T') 112 falseTree,trueTree = tree1.getValue() 113 falseTree.branch(copy.deepcopy(tree2.split), 114 copy.deepcopy(subFalse), 115 copy.deepcopy(subTrue)) 116 trueTree.branch(copy.deepcopy(tree2.split), 117 copy.deepcopy(subFalse), 118 copy.deepcopy(subTrue)) 119 tree4 = copy.deepcopy(tree1) 120 tree4.rebalance() 121 self.assert_(len(tree4.leaves()) < len(tree1.leaves()), 122 'Rebalancing did not reduce tree size') 123 for index in range(self.iterations): 124 tree1 = makeTree(self.keys,6,1.) 125 tree2 = copy.deepcopy(tree1) 126 tree2.rebalance() 127 for rep in range(self.iterations): 128 state = makeState(self.keys,1.) 129 self.assertEqual(tree1[state],tree2[state])
130 131 if __name__ == '__main__': 132 unittest.main() 133