Package teamwork :: Package examples :: Package Thespian :: Module RedRidingHoodDynamics_director
[hide private]
[frames] | no frames]

Source Code for Module teamwork.examples.Thespian.RedRidingHoodDynamics_director

  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  
 
8 -def dummyDyn(feature):
9 tree1 = ProbabilityTree(IdentityMatrix(feature)) 10 return {'tree':tree1}
11
12 -def actorClassDeltaDyn(identity, feature, delta):
13 tree0 = ProbabilityTree(IdentityMatrix(feature)) 14 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 15 16 plane = KeyedPlane(ClassRow(keys=[{'entity':'actor','value':identity}]),0.5) 17 18 tree3 = createBranchTree(plane,tree0,tree1) 19 20 return {'tree':tree3}
21 22 23 #def setToDyn(feature,entity,efeature,value): 24 # key = makeStateKey(entity,efeature) 25 # tree = ProbabilityTree (SetToFeatureMatrix(feature,key,value)) 26 # return {'tree':tree} 27
28 -def DeltaDyn(identity,feature,delta):
29 tree0 = ProbabilityTree(IdentityMatrix(feature)) 30 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 31 32 plane = KeyedPlane(IdentityRow(keys=[{'entity':identity, 33 'relationship':'equals'}]),0.5) 34 35 tree3 = createBranchTree(plane,tree0,tree1) 36 37 return {'tree':tree3}
38 39
40 -def seflObjectDelta(feature,delta):
41 tree0 = ProbabilityTree(IdentityMatrix(feature)) 42 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 43 44 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 45 'relationship':'equals'}]),0.5) 46 47 tree3 = createBranchTree(plane,tree0,tree1) 48 49 return {'tree':tree3}
50
51 -def seflActorDelta(feature,delta):
52 tree0 = ProbabilityTree(IdentityMatrix(feature)) 53 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 54 55 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 56 'relationship':'equals'}]),0.5) 57 58 tree3 = createBranchTree(plane,tree0,tree1) 59 60 return {'tree':tree3}
61
62 -def seflDelta(feature,delta):
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):
81 tree0 = ProbabilityTree(IdentityMatrix(feature)) 82 tree1 = ProbabilityTree(SetToConstantMatrix(source=feature,value=value)) 83 84 plane = KeyedPlane(IdentityRow(keys=[{'entity':identity, 85 'relationship':'equals'}]),0.5) 86 87 tree3 = createBranchTree(plane,tree0,tree1) 88 89 return {'tree':tree3}
90 91
92 -def SetDyn2(identity, className, feature,value):
93 tree0 = ProbabilityTree(IdentityMatrix(feature)) 94 tree1 = ProbabilityTree(SetToConstantMatrix(source=feature,value=value)) 95 plane = KeyedPlane(ClassRow(keys=[{'entity':identity,'value':className}]),.5) 96 tree3 = createBranchTree(plane,tree0,tree1) 97 return {'tree':tree3}
98 99
100 -def SetDyn3(feature,value):
101 tree1 = ProbabilityTree(SetToConstantMatrix(source=feature,value=value)) 102 return {'tree':tree1}
103 104 105
106 -def SetToFeature(feature1, character, feature2):
107 tree0 = ProbabilityTree(IdentityMatrix(feature2)) 108 key = makeStateKey(character,feature2) 109 tree1 = ProbabilityTree(SetToFeatureMatrix(feature1,key,value=1)) 110 111 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 112 'relationship':'equals'}]),0.5) 113 tree2 = createBranchTree(plane,tree0,tree1) 114 115 return {'tree':tree2}
116 117 118 119
120 -def InitNorm():
121 tree1 = ProbabilityTree(IdentityMatrix('init-norm')) 122 123 tree4 = ProbabilityTree(IncrementMatrix('init-norm',keyConstant,-.1)) 124 125 126 ## if I have obligations that haven't been satisfied 127 weights = {} 128 for feature in [\ 129 #'being-greeted', 130 #'being-byed', 131 'being-enquired', 132 #'being-informed', 133 #'being-enquired-about-granny', 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
149 -def RespNorm():
150 tree0 = ProbabilityTree(IdentityMatrix('resp-norm')) 151 152 #tree4 = ProbabilityTree(IncrementMatrix('resp-norm',keyConstant,-.1)) 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
169 -def IntendImposeNorm(act):
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
194 -def IntendFinishNorm(act):
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
225 -def movePosDyn(dis):
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
248 -def moveNegDyn(dis):
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 #def locationDyn(): 272 # tree0 = ProbabilityTree(IdentityMatrix('sameLocation')) 273 # tree1 = ProbabilityTree(IncrementMatrix('sameLocation',keyConstant,-.1)) 274 # 275 # 276 # plane = KeyedPlane(DifferenceRow(keys=[makeStateKey('actor','location'),makeStateKey('object','location')]),0.001) 277 # 278 # tree2 = createBranchTree(plane,tree0,tree1) 279 # 280 # plane = KeyedPlane(DifferenceRow(keys=[makeStateKey('object','location'),makeStateKey('actor','location')]),0.001) 281 # 282 # tree3 = createBranchTree(plane,tree2,tree1) 283 # 284 # 285 # plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 286 # 'relationship':'equals'}]),0.5) 287 # 288 # tree4 = createBranchTree(plane,tree0,tree3) 289 # 290 # return {'tree':tree4} 291 292 #def specialRuleEscapeDyn(): 293 # tree0 = ProbabilityTree(IdentityMatrix('specialRule')) 294 # tree1 = ProbabilityTree(IncrementMatrix('specialRule',keyConstant,-.1)) 295 # 296 # 297 # plane = KeyedPlane(DifferenceRow(keys=[makeStateKey('actor','location'),makeStateKey('object','location')]),0.001) 298 # 299 # tree2 = createBranchTree(plane,tree0,tree1) 300 # 301 # plane = KeyedPlane(DifferenceRow(keys=[makeStateKey('object','location'),makeStateKey('actor','location')]),0.001) 302 # 303 # tree3 = createBranchTree(plane,tree2,tree1) 304 # 305 # 306 # plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 307 # 'relationship':'equals'}]),0.5) 308 # 309 # tree4 = createBranchTree(plane,tree0,tree3) 310 # 311 # return {'tree':tree4} 312 313
314 -def killAliveDyn():
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 #def AliveDyn(feature): 344 # tree0 = ProbabilityTree(IdentityMatrix(feature)) 345 # tree1 = ProbabilityTree(SetToConstantMatrix(source=feature,value=0)) 346 # 347 # key1 = makeStateKey('actor','power') 348 # key2 = makeStateKey('object','power') 349 # 350 # weights = {key1:-1.,key2:1.} 351 # planeObject = KeyedPlane(KeyedVector(weights),.01) 352 # 353 # tree2 = createBranchTree(planeObject,tree1,tree0) 354 # 355 # return {'tree':tree2} 356 357 358
359 -def HasCakeDyn():
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
378 -def SDNormDyn():
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 #'redEaten':{'eat':{'class':PWLDynamics, 475 # 'args':SetDyn2('object','red','redEaten',1.0)}, 476 # 'escape':{'class':PWLDynamics, 477 # 'args':SetDyn2('actor','red','redEaten',0.0)}, 478 # 'kill':{'class':PWLDynamics, 479 # 'args':SetDyn3('redEaten',0.0)}, 480 #}, 481 # 482 #'grannyEaten':{'eat':{'class':PWLDynamics, 483 # 'args':SetDyn2('object','granny','grannyEaten',1.0)}, 484 # 'escape':{'class':PWLDynamics, 485 # 'args':SetDyn2('actor','granny','grannyEaten',0.0)}, 486 # 'kill':{'class':PWLDynamics, 487 # 'args':SetDyn3('grannyEaten',0.0)}, 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 #'wolfAlive':{\ 538 # 'kill':{'class':PWLDynamics, 539 # 'args':AliveDyn('wolfAlive')}, 540 # }, 541 #'redAlive':{\ 542 # 'kill':{'class':PWLDynamics, 543 # 'args':AliveDyn('redAlive')}, 544 # }, 545 546 #'sameLocation':{'wait':{'class':PWLDynamics, 547 # 'args':dummyDyn('sameLocation')}, 548 # 'move1':{'class':PWLDynamics, 549 # 'args':dummyDyn('sameLocation')}, 550 # 'move2':{'class':PWLDynamics, 551 # 'args':dummyDyn('sameLocation')}, 552 # 'moveto-granny':{'class':PWLDynamics, 553 # 'args':dummyDyn('sameLocation')}, 554 # #'changeIdentity':{'class':PWLDynamics, 555 # #'args':dummyDyn('sameLocation')}, 556 # None:{'class':PWLDynamics, 557 # 'args':locationDyn()}, 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 #'moveto-granny':{'class':PWLDynamics, 584 # 'args':DeltaDyn('actor','likeMove',0.1)}, 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 #'specialRule':{ 608 # 'escape':{'class':PWLDynamics, 609 # 'args':specialRuleDyn('escape')}, 610 #} 611 612 } 613 } 614