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
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
34
35 ]
36
42
89
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
159 if not plane1.always() is None:
160 continue
161 if not plane2.always() is None:
162 continue
163 eqCount += 1
164
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
174 break
175 else:
176 self.fail('Complete agreement between %s and %s [%s]' % \
177 (plane1.simpleText(),plane2.simpleText(),state.simpleText()))
178
187
207
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
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
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