1 from teamwork.math.ProbabilityTree import *
2 from teamwork.math.KeyedMatrix import *
3 from teamwork.dynamics.pwlDynamics import *
4 from teamwork.dynamics.arbitraryDynamics import *
5
6
7
11
21
22
23
24
25
26
27
38
39
50
61
63 tree0 = ProbabilityTree(IdentityMatrix(feature))
64 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta))
65
66 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
67 'relationship':'equals'}]),0.5)
68
69 tree2 = createBranchTree(plane,tree0,tree1)
70
71 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object',
72 'relationship':'equals'}]),0.5)
73
74 tree3 = createBranchTree(plane,tree2,tree1)
75
76 return {'tree':tree3}
77
78
79
80 -def SetDyn(identity,feature,value):
90
91
92 -def SetDyn2(identity, className, feature,value):
98
99
103
104
105
116
117
118
119
121 tree1 = ProbabilityTree(IdentityMatrix('init-norm'))
122
123 tree4 = ProbabilityTree(IncrementMatrix('init-norm',keyConstant,-.1))
124
125
126
127 weights = {}
128 for feature in [\
129
130
131 'being-enquired',
132
133
134 ]:
135 key = makeStateKey('self',feature)
136 weights[key] = 1
137
138 plane = KeyedPlane(KeyedVector(weights),.001)
139
140 tree5 = createBranchTree(plane,tree1,tree4)
141
142 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
143 'relationship':'equals'}]),0.5)
144 tree0 = createBranchTree(plane,tree1,tree5)
145
146 return {'tree':tree0}
147
148
150 tree0 = ProbabilityTree(IdentityMatrix('resp-norm'))
151
152
153 tree3 = ProbabilityTree(IncrementMatrix('resp-norm',keyConstant,.1))
154 tree4 = tree0
155
156 varname = 'being-enquired'
157
158 weights = {makeStateKey('self',varname):1.}
159 plane = KeyedPlane(KeyedVector(weights),.001)
160 tree2 = createBranchTree(plane,tree4,tree3)
161
162 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
163 'relationship':'equals'}]),0.5)
164 tree1 = createBranchTree(plane,tree0,tree2)
165
166 return {'tree':tree1}
167
168
170
171 if act == 'enquiry':
172 varname = 'being-enquired'
173 elif act == 'inform-info':
174 varname = 'being-informed'
175 elif act == 'enquiry-about-granny':
176 varname = 'being-enquired-about-granny'
177 else:
178 varname = 'being-'+act
179 if act[len(act)-1] == 'e':
180 varname += 'd'
181 else:
182 varname += 'ed'
183 tree0 = ProbabilityTree(IdentityMatrix(varname))
184 tree3 = ProbabilityTree(SetToConstantMatrix(source=varname,value=1))
185
186 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object',
187 'relationship':'equals'}]),0.5)
188
189 tree1 = createBranchTree(plane,tree0,tree3)
190
191 return {'tree':tree1}
192
193
195
196 if act == 'inform':
197 varname = 'being-enquired'
198 elif act in ['accept','reject']:
199 varname = 'being-requested'
200 elif act in ['OK']:
201 varname = 'being-informed'
202 elif act == 'enquiry-about-granny':
203 varname = 'being-enquired-about-granny'
204 else:
205 varname = 'being-'+act
206 if act[len(act)-1] == 'e':
207 varname += 'd'
208 else:
209 varname += 'ed'
210 tree0 = ProbabilityTree(IdentityMatrix(varname))
211 tree3 = ProbabilityTree(IncrementMatrix(varname,keyConstant,-1))
212
213 weights = {makeStateKey('self',varname):1.}
214 plane = KeyedPlane(KeyedVector(weights),.1)
215 tree2 = createBranchTree(plane,tree0,tree3)
216
217 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
218 'relationship':'equals'}]),0.5)
219
220 tree1 = createBranchTree(plane,tree0,tree2)
221 return {'tree':tree1}
222
223
224
226 if dis == 1:
227 val1=.1
228 val2=-.1
229 else:
230 val1=.2
231 val2=-.2
232
233 tree0 = ProbabilityTree(IdentityMatrix('location'))
234 tree1 = ProbabilityTree(IncrementMatrix('location',keyConstant,val1))
235 tree2 = ProbabilityTree(IncrementMatrix('location',keyConstant,val2))
236
237 weights = {makeStateKey('actor','location'):1.}
238 plane = KeyedPlane(KeyedVector(weights),.9)
239 tree3 = createBranchTree(plane,tree1,tree2)
240
241 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
242 'relationship':'equals'}]),0.5)
243
244 tree4 = createBranchTree(plane,tree0,tree3)
245 return {'tree':tree4}
246
247
249 if dis == -1:
250 val1=-.1
251 val2=.1
252 else:
253 val1=-.2
254 val2=.2
255
256 tree0 = ProbabilityTree(IdentityMatrix('location'))
257 tree1 = ProbabilityTree(IncrementMatrix('location',keyConstant,val1))
258 tree2 = ProbabilityTree(IncrementMatrix('location',keyConstant,val2))
259
260 weights = {makeStateKey('actor','location'):1.}
261 plane = KeyedPlane(KeyedVector(weights),-.9)
262 tree3 = createBranchTree(plane,tree2,tree1)
263
264 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
265 'relationship':'equals'}]),0.5)
266
267 tree4 = createBranchTree(plane,tree0,tree3)
268 return {'tree':tree4}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
315 tree0 = ProbabilityTree(IdentityMatrix('alive'))
316 tree1 = ProbabilityTree(SetToConstantMatrix(source='alive',value=0))
317
318 key1 = makeStateKey('actor','power')
319 key2 = makeStateKey('object','power')
320
321 weights = {key1:1.,key2:-1.}
322 planeActor = KeyedPlane(KeyedVector(weights),.01)
323
324 tree2 = createBranchTree(planeActor,tree1,tree0)
325
326 weights = {key1:-1.,key2:1.}
327 planeObject = KeyedPlane(KeyedVector(weights),.01)
328
329 tree3 = createBranchTree(planeObject,tree1,tree0)
330
331 plane2 = KeyedPlane(IdentityRow(keys=[{'entity':'object',
332 'relationship':'equals'}]),0.5)
333
334 tree4 = createBranchTree(plane2,tree0,tree3)
335
336 plane1 = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
337 'relationship':'equals'}]),0.5)
338
339 tree5 = createBranchTree(plane1,tree4,tree2)
340 return {'tree':tree5}
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
360 tree0 = ProbabilityTree(IdentityMatrix('has-cake'))
361 tree1 = ProbabilityTree(SetToConstantMatrix(source='has-cake',value=0))
362 tree2 = ProbabilityTree(SetToConstantMatrix(source='has-cake',value=1))
363
364
365 plane2 = KeyedPlane(IdentityRow(keys=[{'entity':'object',
366 'relationship':'equals'}]),0.5)
367
368 tree4 = createBranchTree(plane2,tree0,tree2)
369
370 plane1 = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
371 'relationship':'equals'}]),0.5)
372
373 tree5 = createBranchTree(plane1,tree4,tree1)
374 return {'tree':tree5}
375
376
377
379 tree0 = ProbabilityTree(IdentityMatrix('SD-norm'))
380 tree1 = ProbabilityTree(IncrementMatrix('SD-norm',keyConstant,-.5))
381
382 weights = {makeStateKey('wolf','SD'):1.}
383 plane = KeyedPlane(KeyedVector(weights),.8)
384 tree2 = createBranchTree(plane,tree1,tree0)
385
386 plane1 = KeyedPlane(IdentityRow(keys=[{'entity':'actor',
387 'relationship':'equals'}]),0.5)
388
389 tree5 = createBranchTree(plane1,tree0,tree2)
390 return {'tree':tree5}
391
392
393 DynFun = {
394 'noneDyn': {},
395 'basicDyn': {
396
397 'init-norm':{\
398 'enquiry':{'class':PWLDynamics,
399 'args':InitNorm()},
400 'move1':{'class':PWLDynamics,
401 'args':InitNorm()},
402 'move2':{'class':PWLDynamics,
403 'args':InitNorm()},
404 'wait':{'class':PWLDynamics,
405 'args':InitNorm()},
406 'kill':{'class':PWLDynamics,
407 'args':InitNorm()},
408 'eat':{'class':PWLDynamics,
409 'args':InitNorm()},
410 'give-cake':{'class':PWLDynamics,
411 'args':InitNorm()},
412 'moveto-granny':{'class':PWLDynamics,
413 'args':InitNorm()},
414 'help':{'class':PWLDynamics,
415 'args':InitNorm()},
416
417 },
418
419 'resp-norm':{\
420 'inform':{'class':PWLDynamics,
421 'args':RespNorm()},
422 'talkabout-granny':{'class':PWLDynamics,
423 'args':RespNorm()},
424 },
425
426 'being-enquired':{'enquiry':{'class':PWLDynamics,
427 'args':IntendImposeNorm('enquiry')},
428 'inform':{'class':PWLDynamics,
429 'args':IntendFinishNorm('inform')},
430 'talkabout-granny':{'class':PWLDynamics,
431 'args':IntendFinishNorm('inform')},
432 },
433 'enquired':{'enquiry':{'class':PWLDynamics,
434 'args':SetDyn('actor','enquired',1.0)},
435 'inform':{'class':PWLDynamics,
436 'args':SetDyn('object','enquired',0.0)},
437 'talkabout-granny':{'class':PWLDynamics,
438 'args':SetDyn('object','enquired',0.0)},
439 },
440 },
441 'RedSelfDyn': {
442 'eaten':{'eat':{'class':PWLDynamics,
443 'args':SetDyn('object','eaten',1.0)},
444 'escape':{'class':PWLDynamics,
445 'args':SetDyn('actor','eaten',0.0)},
446 'kill':{'class':PWLDynamics,
447 'args':dummyDyn('eaten')},
448
449 },
450
451 },
452
453 'grannySelfDyn': {
454 'eaten':{'eat':{'class':PWLDynamics,
455 'args':SetDyn('object','eaten',1.0)},
456 'escape':{'class':PWLDynamics,
457 'args':SetDyn('actor','eaten',0.0)},
458 'kill':{'class':PWLDynamics,
459 'args':dummyDyn('eaten')},
460 },
461 },
462
463 'woodcutterSelfDyn': {
464
465 },
466
467 'RedDyn': {
468 'preferWait':{'wait':{'class':PWLDynamics,
469 'args':seflActorDelta('preferWait',.1)},
470 },
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490 'SD':{'enquiry':{'class':PWLDynamics,
491 'args':seflDelta('SD',.05)},
492 'inform':{'class':PWLDynamics,
493 'args':seflDelta('SD',.05)},
494 'help':{'class':PWLDynamics,
495 'args':seflActorDelta('SD',.5)},
496 'give-cake':{'class':PWLDynamics,
497 'args':seflActorDelta('SD',.5)},
498 'eat':{'class':PWLDynamics,
499 'args':SetDyn('actor','SD',-1.0)},
500 },
501
502 'helped':{'help':{'class':PWLDynamics,
503 'args':SetDyn('actor','helped',1.0)},
504 },
505
506 'SD-norm':{'talkabout-granny':{'class':PWLDynamics,
507 'args':SDNormDyn()},
508 },
509 'eaten':{'eat':{'class':PWLDynamics,
510 'args':SetDyn('object','eaten',1.0)},
511 'escape':{'class':PWLDynamics,
512 'args':SetDyn('actor','eaten',0.0)},
513 'kill':{'class':PWLDynamics,
514 'args':SetDyn3('eaten',0.0)},
515 },
516 'power':{\
517 'give-gun':{'class':PWLDynamics,
518 'args':DeltaDyn('object','power',1.0)},
519 },
520
521
522 'full':{'eat':{'class':PWLDynamics,
523 'args':DeltaDyn('actor','full',0.5)},
524 'eat-cake':{'class':PWLDynamics,
525 'args':DeltaDyn('actor','full',0.1)},
526 },
527
528 'has-cake':{'give-cake':{'class':PWLDynamics,
529 'args':HasCakeDyn()},
530 'eat-cake':{'class':PWLDynamics,
531 'args':SetDyn('actor','has-cake',0.0)},
532 },
533 'alive':{'kill':{'class':PWLDynamics,
534 'args':killAliveDyn()},
535 },
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560 'location':{\
561 'move1':{'class':PWLDynamics,
562 'args':movePosDyn(1)},
563 'move2':{'class':PWLDynamics,
564 'args':movePosDyn(2)},
565 'move-1':{'class':PWLDynamics,
566 'args':moveNegDyn(-1)},
567 'move-2':{'class':PWLDynamics,
568 'args':moveNegDyn(-2)},
569 'escape':{'class':PWLDynamics,
570 'args':SetToFeature('location','wolf','location')},
571 'moveto-granny':{'class':PWLDynamics,
572 'args':SetToFeature('location','granny','location')},
573 },
574
575 'likeMove':{'move1':{'class':PWLDynamics,
576 'args':DeltaDyn('actor','likeMove',0.1)},
577 'move2':{'class':PWLDynamics,
578 'args':DeltaDyn('actor','likeMove',0.1)},
579 'move-1':{'class':PWLDynamics,
580 'args':DeltaDyn('actor','likeMove',0.1)},
581 'move-2':{'class':PWLDynamics,
582 'args':DeltaDyn('actor','likeMove',0.1)},
583
584
585
586 },
587 'likeTalk':{
588 'inform':{'class':PWLDynamics,
589 'args':DeltaDyn('actor','likeTalk',0.1)},
590 'enquiry':{'class':PWLDynamics,
591 'args':DeltaDyn('actor','likeTalk',0.1)},
592 'talkabout-granny':{'class':PWLDynamics,
593 'args':DeltaDyn('actor','likeTalk',0.1)},
594 },
595
596 'know-granny':{
597 'talkabout-granny':{'class':PWLDynamics,
598 'args':SetDyn('object','know-granny',1.0)},
599 },
600
601 'indoor':{
602 'enter-house':{'class':PWLDynamics,
603 'args':SetDyn('actor','indoor',1.0)},
604 'exist-house':{'class':PWLDynamics,
605 'args':SetDyn('actor','indoor',0.0)},
606 },
607
608
609
610
611
612 }
613 }
614