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

Source Code for Module teamwork.examples.Thespian.ratDynamics

  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 -def dummyDyn(feature):
8 tree1 = ProbabilityTree(IdentityMatrix(feature)) 9 return {'tree':tree1}
10
11 -def deltaDyn(feature,delta):
12 tree = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 13 return {'tree':tree}
14
15 -def setDyn(feature,delta):
16 tree = ProbabilityTree(SetToConstantMatrix(feature=feature,value=delta)) 17 return {'tree':tree}
18
19 -def objectDeltaDyn(feature,delta):
20 tree0 = ProbabilityTree(IdentityMatrix(feature)) 21 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 22 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 23 'relationship':'equals'}]),0.5) 24 tree = createBranchTree(plane,tree0,tree1) 25 return {'tree':tree}
26 27
28 -def actorDeltaDyn(feature,delta):
29 tree0 = ProbabilityTree(IdentityMatrix(feature)) 30 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 31 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 32 'relationship':'equals'}]),0.5) 33 tree3 = createBranchTree(plane,tree0,tree1) 34 return {'tree':tree3}
35 36
37 -def actorSetDyn(fea, val):
38 tree0 = ProbabilityTree(IdentityMatrix(fea)) 39 tree1 = ProbabilityTree(SetToConstantMatrix(feature=fea,value=val)) 40 41 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 42 'relationship':'equals'}]),0.5) 43 44 tree3 = createBranchTree(plane,tree0,tree1) 45 46 return {'tree':tree3}
47 48
49 -def objectSetDyn(fea, val):
50 tree0 = ProbabilityTree(IdentityMatrix(fea)) 51 tree1 = ProbabilityTree(SetToConstantMatrix(feature=fea,value=val)) 52 53 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 54 'relationship':'equals'}]),0.5) 55 56 tree3 = createBranchTree(plane,tree0,tree1) 57 58 return {'tree':tree3}
59 60
61 -def thresholdDeltaDyn(thresholdFeature,threshold,deltaFeature,val):
62 tree0 = ProbabilityTree(IdentityMatrix(deltaFeature)) 63 tree1 = ProbabilityTree(IncrementMatrix(deltaFeature,keyConstant,val)) 64 weights = {makeStateKey('self',thresholdFeature): 1.} 65 plane = KeyedPlane(KeyedVector(weights),threshold) 66 67 tree2 = createBranchTree(plane,tree0,tree1) 68 69 return {'tree':tree2}
70
71 -def thresholdActorDeltaDyn(thresholdFeature,threshold,deltaFeature,val):
72 tree0 = ProbabilityTree(IdentityMatrix(deltaFeature)) 73 tree1 = ProbabilityTree(IncrementMatrix(deltaFeature,keyConstant,val)) 74 weights = {makeStateKey('self',thresholdFeature): 1.} 75 plane = KeyedPlane(KeyedVector(weights),threshold) 76 77 tree2 = createBranchTree(plane,tree0,tree1) 78 79 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 80 'relationship':'equals'}]),0.5) 81 82 tree3 = createBranchTree(plane,tree0,tree2) 83 84 return {'tree':tree3}
85
86 -def lessThresholdActorDeltaDyn(thresholdFeature,threshold,deltaFeature,val):
87 tree0 = ProbabilityTree(IdentityMatrix(deltaFeature)) 88 tree1 = ProbabilityTree(IncrementMatrix(deltaFeature,keyConstant,val)) 89 90 weights = {makeStateKey('self',thresholdFeature): 1.} 91 plane = KeyedPlane(KeyedVector(weights),threshold) 92 93 tree2 = createBranchTree(plane,tree1,tree0) 94 95 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 96 'relationship':'equals'}]),0.5) 97 98 tree3 = createBranchTree(plane,tree0,tree2) 99 100 return {'tree':tree3}
101
102 -def lessThresholdObjectDeltaDyn(thresholdFeature,threshold,deltaFeature,val):
103 tree0 = ProbabilityTree(IdentityMatrix(deltaFeature)) 104 tree1 = ProbabilityTree(IncrementMatrix(deltaFeature,keyConstant,val)) 105 106 weights = {makeStateKey('self',thresholdFeature): 1.} 107 plane = KeyedPlane(KeyedVector(weights),threshold) 108 109 tree2 = createBranchTree(plane,tree1,tree0) 110 111 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 112 'relationship':'equals'}]),0.5) 113 114 tree3 = createBranchTree(plane,tree0,tree2) 115 116 return {'tree':tree3}
117
118 -def actObjectNotAliveDyn(aliveFeature='ThespianType',deltaFeature='actAlive'):
119 tree0 = ProbabilityTree(IdentityMatrix(deltaFeature)) 120 tree1 = ProbabilityTree(IncrementMatrix(deltaFeature,keyConstant,-.1)) 121 122 weights = {makeStateKey('object',aliveFeature):1.} 123 plane = KeyedPlane(KeyedVector(weights),.001) 124 125 tree2 = createBranchTree(plane,tree0,tree1) 126 127 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 128 'relationship':'equals'}]),0.5) 129 130 tree4 = createBranchTree(plane,tree0,tree2) 131 132 return {'tree':tree4}
133 134
135 -def actActorAliveDyn(aliveFeature='ThespianType',deltaFeature='actAlive'):
136 137 tree0 = ProbabilityTree(IdentityMatrix(deltaFeature)) 138 tree1 = ProbabilityTree(IncrementMatrix(deltaFeature,keyConstant,-.1)) 139 140 weights = {makeStateKey('actor',aliveFeature):1.} 141 plane = KeyedPlane(KeyedVector(weights),.001) 142 143 tree2 = createBranchTree(plane,tree1,tree0) 144 145 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 146 'relationship':'equals'}]),0.5) 147 148 tree4 = createBranchTree(plane,tree0,tree2) 149 150 return {'tree':tree4}
151 152
153 -def actBothAliveDyn(aliveFeature='ThespianType',deltaFeature='actAlive'):
154 tree0 = ProbabilityTree(IdentityMatrix(deltaFeature)) 155 tree1 = ProbabilityTree(IncrementMatrix(deltaFeature,keyConstant,-.1)) 156 157 weights = {makeStateKey('actor',aliveFeature):1.} 158 plane = KeyedPlane(KeyedVector(weights),.001) 159 160 tree2 = createBranchTree(plane,tree1,tree0) 161 162 weights = {makeStateKey('object',aliveFeature):1.} 163 plane = KeyedPlane(KeyedVector(weights),.001) 164 165 tree3 = createBranchTree(plane,tree1,tree2) 166 167 168 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 169 'relationship':'equals'}]),0.5) 170 171 tree4 = createBranchTree(plane,tree0,tree3) 172 173 return {'tree':tree4}
174 175
176 -def InitNorm(feature='request'):
177 tree1 = ProbabilityTree(IdentityMatrix('init-norm')) 178 tree4 = ProbabilityTree(IncrementMatrix('init-norm',keyConstant,-.1)) 179 180 ## if either actor or object has obligations that haven't been satisfied 181 weights = {} 182 for feature in [\ 183 'being-requested-to-exercise', 184 'being-requested-to-match', 185 ]: 186 key = makeStateKey('object',feature) 187 weights[key] = 1 188 key = makeStateKey('actor',feature) 189 weights[key] = 1 190 191 plane = KeyedPlane(KeyedVector(weights),.001) 192 193 tree5 = createBranchTree(plane,tree1,tree4) 194 195 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 196 'relationship':'equals'}]),0.5) 197 tree0 = createBranchTree(plane,tree1,tree5) 198 199 return {'tree':tree0}
200 201
202 -def RespNorm(feature='request'):
203 tree0 = ProbabilityTree(IdentityMatrix('resp-norm')) 204 tree3 = ProbabilityTree(IncrementMatrix('resp-norm',keyConstant,.001)) 205 tree4 = ProbabilityTree(IncrementMatrix('resp-norm',keyConstant,-.1)) 206 207 weights = {} 208 if feature=='request': 209 for feature in [\ 210 'being-requested-to-exercise', 211 'being-requested-to-match', 212 ]: 213 key = makeStateKey('self',feature) 214 weights[key] = 1 215 elif feature=='match': 216 key = makeStateKey('self','being-requested-for-match-result') 217 weights[key] = 1 218 219 plane = KeyedPlane(KeyedVector(weights),.001) 220 tree2 = createBranchTree(plane,tree4,tree3) 221 222 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 223 'relationship':'equals'}]),0.5) 224 tree1 = createBranchTree(plane,tree0,tree2) 225 226 return {'tree':tree1}
227 228
229 -def CommitImpose(feature = 'exercise'):
230 obl = 'being-requested-to-'+feature 231 state = 'commit-to-'+feature 232 233 tree0 = ProbabilityTree(IdentityMatrix(state)) 234 tree1 = ProbabilityTree(SetToConstantMatrix(feature=state,value=1)) 235 236 weights = {} 237 weights = {makeStateKey('self',obl): 1.} 238 plane = KeyedPlane(KeyedVector(weights),.001) 239 240 tree2 = createBranchTree(plane,tree0,tree1) 241 242 if feature == 'match': 243 weights = {} 244 obl1 = 'requested-to-match' 245 weights[makeStateKey('self',obl1)]=1. 246 plane = KeyedPlane(KeyedVector(weights),.001) 247 tree3 = createBranchTree(plane,tree0,tree1) 248 else: 249 tree3 = tree0 250 251 252 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 253 'relationship':'equals'}]),0.5) 254 tree4 = createBranchTree(plane,tree0,tree3) 255 256 257 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 258 'relationship':'equals'}]),0.5) 259 tree1 = createBranchTree(plane,tree4,tree2) 260 261 return {'tree':tree1}
262
263 -def commitNormDyn (punishList=[],rewardList=[]):
264 tree0 = ProbabilityTree(IdentityMatrix('commitNorm')) 265 tree1 = ProbabilityTree(IncrementMatrix('commitNorm',keyConstant,-.1)) 266 267 weights = {} 268 for obl in punishList: 269 key = makeStateKey('self',obl) 270 weights[key] = 1 271 plane = KeyedPlane(KeyedVector(weights),.001) 272 tree3 = createBranchTree(plane,tree0,tree1) 273 274 if len(rewardList)>0: 275 weights = {} 276 for obl in rewardList: 277 key = makeStateKey('self',obl) 278 weights[key] = 1 279 plane = KeyedPlane(KeyedVector(weights),.001) 280 tree2 = createBranchTree(plane,tree3,tree0) 281 else: 282 tree2 = tree3 283 284 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 285 'relationship':'equals'}]),0.5) 286 tree4 = createBranchTree(plane,tree0,tree2) 287 288 return {'tree':tree4}
289 290 DynFun = { 291 'noneDyn': {}, 292 'basicDyn': { 293 'init-norm':{\ 294 'request-exercise':{'class':PWLDynamics, 295 'args':InitNorm('request')}, 296 'request-match':{'class':PWLDynamics, 297 'args':InitNorm('request')}, 298 }, 299 300 'resp-norm':{\ 301 'accept':{'class':PWLDynamics, 302 'args':RespNorm('request')}, 303 #'exercise':{'class':PWLDynamics, 304 # 'args':RespNorm('request')}, 305 #'match':{'class':PWLDynamics, 306 # 'args':RespNorm('request')}, 307 'reject':{'class':PWLDynamics, 308 'args':RespNorm('request')}, 309 'win':{'class':PWLDynamics, 310 'args':RespNorm('match')}, 311 'lose':{'class':PWLDynamics, 312 'args':RespNorm('match')}, 313 }, 314 315 'being-requested-for-match-result':{\ 316 'match':{'class':PWLDynamics, 317 'args':setDyn('being-requested-for-match-result',1.0)}, 318 'lose':{'class':PWLDynamics, 319 'args':setDyn('being-requested-for-match-result',0.0)}, 320 'win':{'class':PWLDynamics, 321 'args':setDyn('being-requested-for-match-result',0.0)}, 322 }, 323 324 'preferWait':{'wait':{'class':PWLDynamics, 325 'args':actorDeltaDyn('preferWait',.001)}, 326 }, 327 'winChance':{'increasewinChance':{'class':PWLDynamics, 328 'args':actorDeltaDyn('winChance',.001)}, 329 'decreasewinChance':{'class':PWLDynamics, 330 'args':actorDeltaDyn('winChance',-0.001)}, 331 }, 332 333 'actAliveNorm':{\ 334 'buyV':{'class':PWLDynamics, 335 'args':dummyDyn('actAliveNorm')}, 336 'buyLR':{'class':PWLDynamics, 337 'args':actObjectNotAliveDyn('ThespianType','actAliveNorm')}, 338 'catchSR':{'class':PWLDynamics, 339 'args':actObjectNotAliveDyn('ThespianType','actAliveNorm')}, 340 'wait':{'class':PWLDynamics, 341 'args':dummyDyn('actAliveNorm')}, 342 'exercise':{'class':PWLDynamics, 343 'args':actActorAliveDyn(aliveFeature='ThespianType',deltaFeature='actAliveNorm')}, 344 'match':{'class':PWLDynamics, 345 'args':actActorAliveDyn(aliveFeature='ThespianType',deltaFeature='actAliveNorm')}, 346 None:{'class':PWLDynamics, 347 'args':actBothAliveDyn(aliveFeature='ThespianType',deltaFeature='actAliveNorm')}, 348 }, 349 }, 350 'usrDyn':{'specialRule':{\ 351 'buyLR':{'class':PWLDynamics, 352 'args':lessThresholdActorDeltaDyn('money',.99,'specialRule',-.1)}, 353 'feedV':{'class':PWLDynamics, 354 'args':lessThresholdActorDeltaDyn('money',.04,'specialRule',-.1)}, 355 }, 356 'money':{\ 357 'buyLR':{'class':PWLDynamics, 358 'args':deltaDyn('money',-1)}, 359 'feedV':{'class':PWLDynamics, 360 'args':deltaDyn('money',-.05)}, 361 'win':{'class':PWLDynamics, 362 'args':deltaDyn('money',.1)}, 363 }, 364 365 'being-requested-to-match':{\ 366 'request-match':{'class':PWLDynamics, 367 'args':objectSetDyn('being-requested-to-match',1)}, 368 'accept':{'class':PWLDynamics, 369 'args':actorSetDyn('being-requested-to-match',0)}, 370 'reject':{'class':PWLDynamics, 371 'args':actorSetDyn('being-requested-to-match',0)}, 372 }, 373 374 }, 375 376 'ratDyn': { 377 'being-requested-to-exercise':{'request-exercise':{'class':PWLDynamics, 378 'args':objectSetDyn('being-requested-to-exercise',1)}, 379 'request-match':{'class':PWLDynamics, 380 'args':objectSetDyn('being-requested-to-exercise',0)}, 381 'accept':{'class':PWLDynamics, 382 'args':actorSetDyn('being-requested-to-exercise',0)}, 383 'exercise':{'class':PWLDynamics, 384 'args':actorSetDyn('being-requested-to-exercise',0)}, 385 'reject':{'class':PWLDynamics, 386 'args':actorSetDyn('being-requested-to-exercise',0)}, 387 }, 388 389 'being-requested-to-match':{'request-exercise':{'class':PWLDynamics, 390 'args':objectSetDyn('being-requested-to-match',0)}, 391 'request-match':{'class':PWLDynamics, 392 'args':objectSetDyn('being-requested-to-match',1)}, 393 'accept':{'class':PWLDynamics, 394 'args':actorSetDyn('being-requested-to-match',0)}, 395 'match':{'class':PWLDynamics, 396 'args':actorSetDyn('being-requested-to-match',0)}, 397 'reject':{'class':PWLDynamics, 398 'args':actorSetDyn('being-requested-to-match',0)}, 399 }, 400 401 'requested-to-match':{\ 402 'request-match':{'class':PWLDynamics, 403 'args':actorSetDyn('requested-to-match',1)}, 404 'accept':{'class':PWLDynamics, 405 'args':objectSetDyn('requested-to-match',0)}, 406 'reject':{'class':PWLDynamics, 407 'args':objectSetDyn('requested-to-match',0)}, 408 }, 409 410 'commit-to-exercise':{'accept':{'class':PWLDynamics, 411 'args':CommitImpose('exercise')}, 412 None:{'class':PWLDynamics, 413 'args':actorSetDyn('commit-to-exercise',0)}, 414 }, 415 'commit-to-match':{'accept':{'class':PWLDynamics, 416 'args':CommitImpose('match')}, 417 None:{'class':PWLDynamics, 418 'args':actorSetDyn('commit-to-match',0)}, 419 }, 420 'force-to-commit':{'eshock':{'class':PWLDynamics, 421 'args':objectSetDyn('force-to-commit',1)}, 422 'accept':{'class':PWLDynamics, 423 'args':actorSetDyn('force-to-commit',0)}, 424 }, 425 'avoidShock':{'reject':{'class':PWLDynamics, 426 'args':thresholdActorDeltaDyn('force-to-commit',0.001,'avoidShock',-.1)}, 427 }, 428 'SD':{'feedV':{'class':PWLDynamics, 429 'args':lessThresholdObjectDeltaDyn('SD',.9,'health',.1)}, 430 'eshock':{'class':PWLDynamics, 431 'args':objectDeltaDyn('SD',-.5)}, 432 'bad-mouth':{'class':PWLDynamics, 433 'args':objectDeltaDyn('SD',-.3)}, 434 }, 435 'commitNorm':{\ 436 'exercise':{'class':PWLDynamics, 437 'args':commitNormDyn(['commit-to-match'],['commit-to-exercise'])}, 438 'match':{'class':PWLDynamics, 439 'args':commitNormDyn(['commit-to-exercise'],['commit-to-match'])}, 440 None:{'class':PWLDynamics, 441 'args':commitNormDyn(['commit-to-match','commit-to-exercise'],[])}, 442 443 }, 444 445 'like-to-exercise':{'exercise':{'class':PWLDynamics, 446 'args':actorDeltaDyn('like-to-exercise',.01)}, 447 }, 448 449 'like-to-match':{'request-match':{'class':PWLDynamics, 450 'args':actorDeltaDyn('like-to-match',.01)}, 451 }, 452 453 'just-matched':{'match':{'class':PWLDynamics, 454 'args':actorSetDyn('just-matched',1)}, 455 'win':{'class':PWLDynamics, 456 'args':setDyn('just-matched',0)}, 457 'lose':{'class':PWLDynamics, 458 'args':setDyn('just-matched',0)}, 459 }, 460 461 }, 462 'labratDyn': { 463 'ThespianType':{'buyLR':{'class':PWLDynamics, 464 'args':objectSetDyn('ThespianType',1)}, 465 }, 466 'health':{'feedV':{'class':PWLDynamics, 467 'args':lessThresholdObjectDeltaDyn('health',.9,'health',.1)}, 468 'eshock':{'class':PWLDynamics, 469 'args':objectDeltaDyn('health',-.5)}, 470 'lose':{'class':PWLDynamics, 471 'args':thresholdDeltaDyn('just-matched',.5,'health',-.5)}, 472 'exercise':{'class':PWLDynamics, 473 'args':lessThresholdActorDeltaDyn('health',.9,'health',.1)}, 474 'daypass':{'class':PWLDynamics, 475 'args':thresholdDeltaDyn('ThespianType',0.5,'health',-.1)}, 476 }, 477 }, 478 'streetratDyn': { 479 'ThespianType':{'catchSR':{'class':PWLDynamics, 480 'args':objectSetDyn('ThespianType',1)}, 481 'escape':{'class':PWLDynamics, 482 'args':actorSetDyn('ThespianType',0)}, 483 }, 484 'health':{'feedV':{'class':PWLDynamics, 485 'args':lessThresholdObjectDeltaDyn('health',.9,'health',.1)}, 486 'eshock':{'class':PWLDynamics, 487 'args':objectDeltaDyn('health',-.5)}, 488 'lose':{'class':PWLDynamics, 489 'args':thresholdDeltaDyn('just-matched',.5,'health',-.5)}, 490 'exercise':{'class':PWLDynamics, 491 'args':lessThresholdActorDeltaDyn('health',.9,'health',.1)}, 492 }, 493 494 }, 495 496 'timerDyn': { 497 'specialRule':{'wait':{'class':PWLDynamics, 498 'args':thresholdActorDeltaDyn('time',.9,'specialRule',.02)}, 499 'daypass':{'class':PWLDynamics, 500 'args':thresholdActorDeltaDyn('time',-3,'specialRule',.01)}, 501 }, 502 'time':{\ 503 'catchSR':{'class':PWLDynamics, 504 'args':deltaDyn('time',-4)}, 505 'request-exercise':{'class':PWLDynamics, 506 'args':deltaDyn('time',-1)}, 507 'request-match':{'class':PWLDynamics, 508 'args':deltaDyn('time',-1)}, 509 'daypass':{'class':PWLDynamics, 510 'args':setDyn('time',4)}, 511 }, 512 'day':{'daypass':{'class':PWLDynamics, 513 'args':deltaDyn('day',1)}, 514 }, 515 }, 516 } 517