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

Source Code for Module teamwork.test.math.testKeyedPlane

  1  from teamwork.math.Keys import * 
  2  from teamwork.math.KeyedMatrix import * 
  3  from teamwork.math.ProbabilityTree import * 
  4  from testPWL import TestPWL,makePlane,makeState 
  5   
  6  import random 
  7  import unittest 
  8   
9 -class TestKeyedPlane(TestPWL):
10 """ 11 @cvar iterations: the number of times to repeat each test 12 @type iterations: int 13 @cvar maxReps: maximum number of random instances to test ambiguous cases 14 @type maxReps: int 15 @cvar features: a list of state features to use as test case 16 @type features: C{str} 17 @cvar agents: a list of entity names to use as test cases 18 @type agents: C{str} 19 """ 20 iterations = 100 21 maxReps = 1000 22 features = [ 23 'strength', 24 'dexterity', 25 'constitution', 26 'intelligence', 27 'charisma', 28 'wisdom', 29 ] 30 agents = [ 31 'warrior', 32 'wizard', 33 ## 'cleric', 34 ## 'thief', 35 ] 36
37 - def setUp(self):
38 self.keys = [] 39 for agent in self.agents: 40 self.keys += map(lambda f:makeStateKey(agent,f),self.features) 41 self.keys.sort()
42
43 - def testAlways(self):
44 # Test some random planes, too 45 for index in range(self.iterations): 46 plane = makePlane(self.keys) 47 if plane.always() == True: 48 for iteration in range(100): 49 state = makeState(self.keys) 50 self.assert_(plane.test(state)) 51 elif plane.always() == False: 52 for iteration in range(100): 53 state = makeState(self.keys) 54 self.assert_(not plane.test(state)) 55 else: 56 # First try to test using randomly selected vectors 57 last = None 58 count = 1 59 while count < self.maxReps: 60 state = makeState(self.keys) 61 if plane.test(state): 62 if last == 'false': 63 # Got values on both side 64 break 65 elif not last: 66 last = 'true' 67 else: 68 if last == 'true': 69 # Got values on both side 70 break 71 elif not last: 72 last = 'false' 73 count += 1 74 else: 75 # Test using extreme vectors 76 lo = KeyedVector() 77 hi = KeyedVector() 78 for key in self.keys+[keyConstant]: 79 if plane.weights[key] > 0.: 80 lo[key] = -1. 81 hi[key] = 1. 82 else: 83 lo[key] = 1. 84 hi[key] = -1. 85 self.assertEqual(plane.weights.keys(),hi.keys()) 86 self.assertEqual(plane.weights.keys(),lo.keys()) 87 self.assert_(plane.test(hi),'%s comes up False on %s [%s*%s=%f]' % (plane.simpleText(),hi.simpleText(),str(plane.weights.getArray()),str(hi.getArray()),plane.weights*hi)) 88 self.assert_(not plane.test(lo),'%s comes up True on %s' % (plane.simpleText(),lo.simpleText()))
89
90 - def testComparison(self):
91 for index in range(self.iterations): 92 plane1 = makePlane(self.keys) 93 plane1.weights.freeze() 94 plane2 = copy.deepcopy(plane1) 95 comparison = plane1.compare(plane2) 96 self.assertEqual(comparison,'equal', 97 '%s relationship between %s and %s?' % \ 98 (comparison,plane1.simpleText(), 99 plane2.simpleText())) 100 self.assertEqual(plane2.compare(plane1),'equal') 101 for index in range(self.iterations): 102 plane1 = makePlane(self.keys) 103 plane1.weights.freeze() 104 plane2 = copy.deepcopy(plane1) 105 plane2.weights = plane1.weights * 0.5 106 self.assertEqual(plane1.compare(plane2),'less') 107 self.assertEqual(plane2.compare(plane1),'greater') 108 plane2.threshold = plane1.threshold * 0.25 109 self.assertEqual(plane1.compare(plane2),'greater') 110 self.assertEqual(plane2.compare(plane1),'less') 111 plane2.threshold = plane1.threshold * 0.5 112 self.assertEqual(plane1.compare(plane2),'equal') 113 self.assertEqual(plane2.compare(plane1),'equal') 114 plane2.weights = -plane1.weights 115 plane2.threshold = -plane1.threshold 116 self.assertEqual(plane1.compare(plane2),'inverse') 117 self.assertEqual(plane2.compare(plane1),'inverse') 118 gtCount = 0 119 ltCount = 0 120 invCount = 0 121 eqCount = 0 122 for index in range(self.iterations): 123 plane1 = makePlane(self.keys) 124 plane1.weights.freeze() 125 plane2 = makePlane(self.keys) 126 plane2.weights.freeze() 127 comparison = plane1.compare(plane2) 128 if comparison == 'greater': 129 gtCount += 1 130 for iteration in range(100): 131 state = makeState(self.keys) 132 if plane1.test(state): 133 self.assert_(plane2.test(state)) 134 if not plane2.test(state): 135 self.assert_(not plane1.test(state)) 136 elif comparison == 'less': 137 ltCount += 1 138 for iteration in range(100): 139 state = makeState(self.keys) 140 if plane2.test(state): 141 self.assert_(plane1.test(state)) 142 if not plane1.test(state): 143 self.assert_(not plane2.test(state)) 144 elif comparison == 'inverse': 145 for iteration in range(100): 146 state = makeState(self.keys) 147 self.assertNotEqual(plane1.test(state),plane2.test(state)) 148 elif comparison == 'equal': 149 for iteration in range(100): 150 state = makeState(self.keys) 151 self.assertEqual(plane1.test(state),plane2.test(state), 152 '%s differs from %s on %s' % \ 153 (plane1.simpleText(),plane2.simpleText(), 154 state.simpleText())) 155 else: 156 self.assertEqual(comparison,'indeterminate', 157 'Unknown comparison: %s' % (comparison)) 158 # Ignore always true/false planes 159 if not plane1.always() is None: 160 continue 161 if not plane2.always() is None: 162 continue 163 eqCount += 1 164 # Test using extreme vectors 165 for stateIndex in xrange(pow(3,len(self.keys)+1)): 166 state = KeyedVector() 167 state[keyConstant] = float(stateIndex % 3 - 1) 168 stateIndex /= 3 169 for index in range(len(self.keys)): 170 state[self.keys[index]] = float(stateIndex % 3 - 1) 171 stateIndex /= 3 172 if plane1.test(state) != plane2.test(state): 173 # OK, we've verified that these two planes differ on at least one point 174 break 175 else: 176 self.fail('Complete agreement between %s and %s [%s]' % \ 177 (plane1.simpleText(),plane2.simpleText(),state.simpleText()))
178
179 - def testXML(self):
180 for index in range(self.iterations): 181 old = makePlane(self.keys) 182 doc = old.__xml__() 183 new = KeyedPlane({},0.) 184 new.parse(doc.documentElement) 185 self.assertAlmostEqual(old.threshold,new.threshold,8) 186 self.verifyVector(old.weights,new.weights)
187
188 - def testInstantiate(self):
189 from teamwork.examples.PSYOP.oil import oilPhysical 190 from teamwork.agent.Agent import Agent 191 tree = oilPhysical()['tree'] 192 branch = tree.split[0].weights 193 table = dict({'actor':'Turkomen', 194 'object':'GeographicArea', 195 'type':'attack', 196 'self':Agent('GeographicArea') 197 }) 198 branch.instantiateKeys(table) 199 ok = False 200 for key in branch.keys(): 201 if key == keyConstant: 202 self.assertAlmostEqual(branch[key],1.,8) 203 ok = True 204 else: 205 self.assertAlmostEqual(branch[key],0.,8) 206 self.assert_(ok)
207
208 - def testRelationshipRow(self):
209 from teamwork.agent.RecursiveAgent import RecursiveAgent 210 teacher = RecursiveAgent('MrsThompson') 211 bully = RecursiveAgent('Bill') 212 victim = RecursiveAgent('Victor') 213 table = {'actor':bully, 214 'object':victim, 215 'type':'pickOn', 216 'self':teacher} 217 row = RelationshipRow(keys=[{'feature':'student', 218 'relatee':'actor'}]) 219 plane = KeyedPlane(row,0.5) 220 plane = plane.instantiateKeys(table) 221 self.assertEqual(plane,-1) 222 teacher.relationships['student'] = [bully.name] 223 row = RelationshipRow(keys=[{'feature':'student', 224 'relatee':'actor'}]) 225 plane = KeyedPlane(row,0.5) 226 label = plane.simpleText() 227 result = plane.instantiateKeys(table) 228 self.assertEqual(result,1,'Plane %s does not evaluate to always True' % (label))
229
230 - def testORPlane(self):
231 for index in range(self.iterations): 232 keys = {} 233 for i in range(len(self.keys)/2): 234 key = random.choice(self.keys) 235 keys[key] = True 236 keyList = map(lambda k:(k,random.choice([True,False])), 237 keys.keys()) 238 trueTree = ProbabilityTree() 239 falseTree = ProbabilityTree() 240 tree = createORTree(keyList,falseTree,trueTree) 241 state = KeyedVector() 242 state[keyConstant] = 1. 243 for key,truth in keyList: 244 if truth: 245 state[key] = 0. 246 else: 247 state[key] = 1. 248 tree.fill([keyConstant]) 249 self.assertEqual(tree.split[0].keys(),state.keys()) 250 self.assert_(not reduce(lambda x,y:x and y, 251 map(lambda p:p.test(state),tree.split)), 252 '%s comes up True on %s' % \ 253 (string.join(map(lambda p:p.simpleText(),tree.split),' and '),str(state))) 254 for key,truth in keyList: 255 if truth: 256 state[key] = 1. 257 self.assert_(reduce(lambda x,y:x and y, 258 map(lambda p:p.test(state),tree.split))) 259 state[key] = 0. 260 else: 261 state[key] = 0. 262 self.assert_(reduce(lambda x,y:x and y, 263 map(lambda p:p.test(state),tree.split))) 264 state[key] = 1.
265
266 - def testANDPlane(self):
267 for index in range(self.iterations): 268 keys = {} 269 for i in range(len(self.keys)/2): 270 key = random.choice(self.keys) 271 keys[key] = True 272 keyList = map(lambda k:(k,random.choice([True,False])), 273 keys.keys()) 274 trueTree = ProbabilityTree() 275 falseTree = ProbabilityTree() 276 tree = createANDTree(keyList,falseTree,trueTree) 277 tree.fill([keyConstant]) 278 new = copy.deepcopy(tree) 279 state = KeyedVector() 280 state[keyConstant] = 1. 281 for key,truth in keyList: 282 if truth: 283 state[key] = 1. 284 else: 285 state[key] = 0. 286 287 self.assert_(reduce(lambda x,y:x and y, 288 map(lambda p:p.test(state),tree.split)), 289 '%s comes up False on %s' % \ 290 (string.join(map(lambda p:p.simpleText(),tree.split),' and '),str(state))) 291 self.assert_(reduce(lambda x,y:x and y, 292 map(lambda p:p.test(state),new.split)), 293 '%s comes up False on %s' % \ 294 (string.join(map(lambda p:p.simpleText(),new.split),' and '),str(state))) 295 for key,truth in keyList: 296 if truth: 297 state[key] = 0. 298 self.assert_(not reduce(lambda x,y:x and y, 299 map(lambda p:p.test(state),tree.split))) 300 self.assert_(not reduce(lambda x,y:x and y, 301 map(lambda p:p.test(state),new.split))) 302 state[key] = 1. 303 else: 304 state[key] = 1. 305 self.assert_(not reduce(lambda x,y:x and y, 306 map(lambda p:p.test(state),tree.split))) 307 self.assert_(not reduce(lambda x,y:x and y, 308 map(lambda p:p.test(state),new.split))) 309 state[key] = 0.
310 311 if __name__ == '__main__': 312 unittest.main() 313