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

Source Code for Module teamwork.examples.Thespian.NormAgentDynamics

  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 thresholdDecrease(feature):
8 tree0 = ProbabilityTree(IdentityMatrix(feature)) 9 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,-.1)) 10 weights = {makeStateKey('self',feature): 1.} 11 plane = KeyedPlane(KeyedVector(weights),.001) 12 13 tree = createBranchTree(plane,tree1,tree0) 14 return {'tree':tree}
15
16 -def thresholdObjectSetDyn(thresholdF,fea,val):
17 tree0 = ProbabilityTree(IdentityMatrix(fea)) 18 tree1 = ProbabilityTree(SetToConstantMatrix(feature=fea,value=val)) 19 weights = {makeStateKey('self',thresholdF): 1.} 20 plane = KeyedPlane(KeyedVector(weights),.001) 21 22 tree = createBranchTree(plane,tree0,tree1) 23 return {'tree':tree}
24 25
26 -def deltaDyn(feature,delta):
27 tree = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 28 return {'tree':tree}
29
30 -def deltaActorOrObjectDyn(feature,delta):
31 tree0 = ProbabilityTree(IdentityMatrix(feature)) 32 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 33 34 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 35 'relationship':'equals'}]),0.5) 36 37 tree2 = createBranchTree(plane,tree0,tree1) 38 39 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 40 'relationship':'equals'}]),0.5) 41 42 tree3 = createBranchTree(plane,tree2,tree1) 43 44 return {'tree':tree3}
45 46
47 -def deltaActorDyn(feature,delta):
48 49 tree0 = ProbabilityTree(IdentityMatrix(feature)) 50 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,delta)) 51 52 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 53 'relationship':'equals'}]),0.5) 54 55 tree3 = createBranchTree(plane,tree0,tree1) 56 57 return {'tree':tree3}
58 59
60 -def dummyDyn(feature):
61 tree1 = ProbabilityTree(IdentityMatrix(feature)) 62 63 return {'tree':tree1}
64
65 -def ActorSetDyn(fea, val):
66 tree0 = ProbabilityTree(IdentityMatrix(fea)) 67 tree1 = ProbabilityTree(SetToConstantMatrix(feature=fea,value=val)) 68 69 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 70 'relationship':'equals'}]),0.5) 71 72 tree3 = createBranchTree(plane,tree0,tree1) 73 74 return {'tree':tree3}
75 76
77 -def ObjectSetDyn(fea, val):
78 tree0 = ProbabilityTree(IdentityMatrix(fea)) 79 tree1 = ProbabilityTree(SetToConstantMatrix(feature=fea,value=val)) 80 81 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 82 'relationship':'equals'}]),0.5) 83 84 tree3 = createBranchTree(plane,tree0,tree1) 85 86 return {'tree':tree3}
87 88 89
90 -def conversationFlowNorm(act):
91 tree1 = ProbabilityTree(IdentityMatrix('conversation-flow-norm')) 92 tree4 = ProbabilityTree(IncrementMatrix('conversation-flow-norm',keyConstant,-.1)) 93 94 ## action shouldn't happen after closed conversation, or before conversation opened 95 96 if not act in ['greet-init','bye-resp']: 97 weights = {makeStateKey('self','conversation'): 1.} 98 plane = KeyedPlane(KeyedVector(weights),.001) 99 tree2 = createBranchTree(plane,tree4,tree1) 100 else: 101 weights = {makeStateKey('self','conversation'): 1.} 102 plane = KeyedPlane(KeyedVector(weights),.001) 103 tree2 = createBranchTree(plane,tree1,tree4) 104 105 106 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 107 'relationship':'equals'}]),0.5) 108 tree0 = createBranchTree(plane,tree1,tree2) 109 110 return {'tree':tree0}
111 112
113 -def InitNorm():
114 tree1 = ProbabilityTree(IdentityMatrix('init-norm')) 115 116 tree4 = ProbabilityTree(IncrementMatrix('init-norm',keyConstant,-.1)) 117 118 119 ## if I have obligations that haven't been satisfied 120 weights = {} 121 for feature in [\ 122 'being-greeted', 123 'being-byed', 124 'being-enquired', 125 'being-informed', 126 'being-enquired-about-granny', 127 ]: 128 key = makeStateKey('self',feature) 129 weights[key] = 1 130 131 plane = KeyedPlane(KeyedVector(weights),.001) 132 133 tree5 = createBranchTree(plane,tree1,tree4) 134 135 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 136 'relationship':'equals'}]),0.5) 137 tree0 = createBranchTree(plane,tree1,tree5) 138 139 return {'tree':tree0}
140 141
142 -def RespNorm(act):
143 tree0 = ProbabilityTree(IdentityMatrix('resp-norm')) 144 145 tree4 = ProbabilityTree(IncrementMatrix('resp-norm',keyConstant,-.1)) 146 tree3 = ProbabilityTree(IdentityMatrix('resp-norm')) 147 148 ## tree3 = ProbabilityTree(IncrementMatrix('resp-norm',keyConstant,.1)) 149 150 if act == 'inform': 151 varname = 'being-enquired' 152 elif act in ['accept','reject']: 153 varname = 'being-requested' 154 elif act in ['OK']: 155 varname = 'being-informed' 156 else: 157 varname = 'being-'+act 158 if act[len(act)-1] == 'e': 159 varname += 'd' 160 else: 161 varname += 'ed' 162 163 weights = {makeStateKey('self',varname):1.} 164 plane = KeyedPlane(KeyedVector(weights),.001) 165 tree2 = createBranchTree(plane,tree4,tree3) 166 167 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 168 'relationship':'equals'}]),0.5) 169 tree1 = createBranchTree(plane,tree0,tree2) 170 171 return {'tree':tree1}
172
173 -def MakeRespNorm(act):
174 tree0 = ProbabilityTree(IdentityMatrix('makeResp-norm')) 175 176 tree4 = ProbabilityTree(IdentityMatrix('makeResp-norm')) 177 tree3 = ProbabilityTree(IncrementMatrix('makeResp-norm',keyConstant,.1)) 178 179 ## tree3 = ProbabilityTree(IncrementMatrix('resp-norm',keyConstant,.1)) 180 181 if act == 'inform': 182 varname = 'being-enquired' 183 elif act in ['accept','reject']: 184 varname = 'being-requested' 185 elif act in ['OK']: 186 varname = 'being-informed' 187 else: 188 varname = 'being-'+act 189 if act[len(act)-1] == 'e': 190 varname += 'd' 191 else: 192 varname += 'ed' 193 194 weights = {makeStateKey('self',varname):1.} 195 plane = KeyedPlane(KeyedVector(weights),.001) 196 tree2 = createBranchTree(plane,tree4,tree3) 197 198 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 199 'relationship':'equals'}]),0.5) 200 tree1 = createBranchTree(plane,tree0,tree2) 201 202 203 return {'tree':tree1}
204 205
206 -def IntendImposeNorm(act):
207 208 if act == 'enquiry': 209 varname = 'being-enquired' 210 elif act == 'inform-info': 211 varname = 'being-informed' 212 else: 213 varname = 'being-'+act 214 if act[len(act)-1] == 'e': 215 varname += 'd' 216 else: 217 varname += 'ed' 218 tree0 = ProbabilityTree(IdentityMatrix(varname)) 219 tree3 = ProbabilityTree(SetToConstantMatrix(feature=varname,value=1)) 220 221 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 222 'relationship':'equals'}]),0.5) 223 224 tree1 = createBranchTree(plane,tree0,tree3) 225 226 return {'tree':tree1}
227 228
229 -def IntendFinishNorm(act):
230 231 if act == 'inform': 232 varname = 'being-enquired' 233 elif act in ['accept','reject']: 234 varname = 'being-requested' 235 elif act in ['OK']: 236 varname = 'being-informed' 237 else: 238 varname = 'being-'+act 239 if act[len(act)-1] == 'e': 240 varname += 'd' 241 else: 242 varname += 'ed' 243 tree0 = ProbabilityTree(IdentityMatrix(varname)) 244 tree3 = ProbabilityTree(IncrementMatrix(varname,keyConstant,-1)) 245 246 weights = {makeStateKey('self',varname):1.} 247 plane = KeyedPlane(KeyedVector(weights),.1) 248 tree2 = createBranchTree(plane,tree0,tree3) 249 250 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 251 'relationship':'equals'}]),0.5) 252 253 tree1 = createBranchTree(plane,tree0,tree2) 254 return {'tree':tree1}
255
256 -def conversationDyn(intend):
257 tree0 = ProbabilityTree(IdentityMatrix('conversation')) 258 if intend == 'greet': 259 tree1 = ProbabilityTree(SetToConstantMatrix(feature='conversation',value=1)) 260 elif intend == 'bye': 261 tree1 = ProbabilityTree(SetToConstantMatrix(feature='conversation',value=0)) 262 263 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 264 'relationship':'equals'}]),0.5) 265 266 tree2 = createBranchTree(plane,tree0,tree1) 267 268 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 269 'relationship':'equals'}]),0.5) 270 271 tree3 = createBranchTree(plane,tree2,tree1) 272 273 return {'tree':tree3}
274 275
276 -def noRepeatDyn(feature):
277 278 tree0 = ProbabilityTree(IdentityMatrix('noRepeat-norm')) 279 tree1 = ProbabilityTree(IncrementMatrix('noRepeat-norm',keyConstant,-.1)) 280 281 282 weights = {makeStateKey('actor',feature):1.} 283 plane = KeyedPlane(KeyedVector(weights),0) 284 tree3 = createBranchTree(plane,tree0,tree1) 285 286 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 287 'relationship':'equals'}]),0.5) 288 289 tree4 = createBranchTree(plane,tree0,tree3) 290 return {'tree':tree4}
291 292 293 294
295 -def SDDyn(feature):
296 tree0 = ProbabilityTree(IdentityMatrix(feature)) 297 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,.05)) 298 299 name = feature.strip('SDwith') 300 if name == 'Wolf': 301 name = 'wolf' 302 303 plane = KeyedPlane(ClassRow(keys=[{'entity':'object','value':name}]),0) 304 305 tree3 = createBranchTree(plane,tree0,tree1) 306 307 plane = KeyedPlane(ClassRow(keys=[{'entity':'actor','value':name}]),0) 308 309 tree2 = createBranchTree(plane,tree0,tree1) 310 311 plane = KeyedPlane(IdentityRow(keys=[{'entity':'object', 312 'relationship':'equals'}]),0.5) 313 314 tree5 = createBranchTree(plane,tree0,tree2) 315 316 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 317 'relationship':'equals'}]),0.5) 318 319 tree4 = createBranchTree(plane,tree5,tree3) 320 return {'tree':tree4}
321 322
323 -def SDnormDyn(act):
324 tree0 = ProbabilityTree(IdentityMatrix('SDnorm')) 325 tree1 = ProbabilityTree(IncrementMatrix('SDnorm',keyConstant,-.1)) 326 327 328 weights = {makeStateKey('object','SDwithWolf'):1.} 329 plane = KeyedPlane(KeyedVector(weights),0) 330 tree3 = createBranchTree(plane,tree1,tree0) 331 332 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 333 'relationship':'equals'}]),0.5) 334 335 tree4 = createBranchTree(plane,tree0,tree3) 336 return {'tree':tree4}
337 338
339 -def likeActDyn(feature):
340 341 tree0 = ProbabilityTree(IdentityMatrix(feature)) 342 tree1 = ProbabilityTree(IncrementMatrix(feature,keyConstant,.01)) 343 344 plane = KeyedPlane(IdentityRow(keys=[{'entity':'actor', 345 'relationship':'equals'}]),0.5) 346 347 tree3 = createBranchTree(plane,tree0,tree1) 348 349 return {'tree':tree3}
350 351 DynFun = { 352 'noneDyn': {}, 353 'basicDyn': { 354 'resp-norm':{\ 355 'greet-resp':{'class':PWLDynamics, 356 'args':RespNorm('greet')}, 357 358 'bye-resp':{'class':PWLDynamics, 359 'args':RespNorm('bye')}, 360 361 'inform':{'class':PWLDynamics, 362 'args':RespNorm('inform')}, 363 364 }, 365 'makeResp-norm':{\ 366 'greet-resp':{'class':PWLDynamics, 367 'args':MakeRespNorm('greet')}, 368 369 'bye-resp':{'class':PWLDynamics, 370 'args':MakeRespNorm('bye')}, 371 372 'inform':{'class':PWLDynamics, 373 'args':MakeRespNorm('inform')}, 374 375 376 }, 377 'init-norm':{'greet-init':{'class':PWLDynamics, 378 'args':InitNorm()}, 379 380 'bye-init':{'class':PWLDynamics, 381 'args':InitNorm()}, 382 383 'enquiry':{'class':PWLDynamics, 384 'args':InitNorm()}, 385 386 'wait':{'class':PWLDynamics, 387 'args':InitNorm()}, 388 389 }, 390 391 'conversation-flow-norm':{'greet-init':{'class':PWLDynamics, 392 'args':conversationFlowNorm('greet-init')}, 393 'greet-resp':{'class':PWLDynamics, 394 'args':conversationFlowNorm('greet')}, 395 'bye-init':{'class':PWLDynamics, 396 'args':conversationFlowNorm('bye-init')}, 397 'bye-resp':{'class':PWLDynamics, 398 'args':conversationFlowNorm('bye-resp')}, 399 'enquiry':{'class':PWLDynamics, 400 'args':conversationFlowNorm('enquiry')}, 401 'inform':{'class':PWLDynamics, 402 'args':conversationFlowNorm('inform')}, 403 'inform-info':{'class':PWLDynamics, 404 'args':conversationFlowNorm('inform-info')}, 405 'OK':{'class':PWLDynamics, 406 'args':conversationFlowNorm('OK')}, 407 408 }, 409 410 'noRepeat-norm':{\ 411 ## 'enquiry':{'class':PWLDynamics, 412 ## 'args':noRepeatDyn('enquired')}, 413 'greet-init':{'class':PWLDynamics, 414 'args':noRepeatDyn('greeted')}, 415 'bye-init':{'class':PWLDynamics, 416 'args':noRepeatDyn('byed')}, 417 }, 418 419 420 421 'greeted':{'greet-init':{'class':PWLDynamics, 422 'args':deltaActorOrObjectDyn('greeted', 1)}, 423 'greet-resp':{'class':PWLDynamics, 424 'args':deltaActorOrObjectDyn('greeted', 1)}, 425 'bye-init':{'class':PWLDynamics, 426 'args':deltaActorOrObjectDyn('greeted', 1)}, 427 'bye-resp':{'class':PWLDynamics, 428 'args':deltaActorOrObjectDyn('greeted', 1)}, 429 'enquiry':{'class':PWLDynamics, 430 'args':deltaActorOrObjectDyn('greeted', 1)}, 431 'inform':{'class':PWLDynamics, 432 'args':deltaActorOrObjectDyn('greeted', 1)}, 433 434 }, 435 436 'byed':{'bye-init':{'class':PWLDynamics, 437 'args':deltaActorOrObjectDyn('byed', 1)}, 438 'bye-resp':{'class':PWLDynamics, 439 'args':deltaActorOrObjectDyn('byed', 1)}, 440 }, 441 442 'enquired':{'enquiry':{'class':PWLDynamics, 443 'args':deltaActorDyn('enquired', 1)}, 444 }, 445 446 'being-greeted':{'greet-init':{'class':PWLDynamics, 447 'args':IntendImposeNorm('greet')}, 448 'greet-resp':{'class':PWLDynamics, 449 'args':IntendFinishNorm('greet')}, 450 }, 451 'being-byed':{'bye-init':{'class':PWLDynamics, 452 'args':IntendImposeNorm('bye')}, 453 'bye-resp':{'class':PWLDynamics, 454 'args':IntendFinishNorm('bye')}, 455 }, 456 457 'being-enquired':{'enquiry':{'class':PWLDynamics, 458 'args':IntendImposeNorm('enquiry')}, 459 'inform':{'class':PWLDynamics, 460 'args':IntendFinishNorm('inform')}, 461 }, 462 'being-informed':{'inform-info':{'class':PWLDynamics, 463 'args':IntendImposeNorm('inform-info')}, 464 'OK':{'class':PWLDynamics, 465 'args':IntendFinishNorm('OK')}, 466 }, 467 468 'conversation':{\ 469 470 'wait':{'class':PWLDynamics, 471 'args':dummyDyn('conversation')}, 472 'bye-resp':{'class':PWLDynamics, 473 'args':dummyDyn('conversation')}, 474 475 None:{'class':PWLDynamics, 476 'args':conversationDyn('greet')}, 477 'bye-init':{'class':PWLDynamics, 478 'args':conversationDyn('bye')}, 479 }, 480 481 'likeTalk':{\ 482 None:{'class':PWLDynamics, 483 'args':likeActDyn('likeTalk')}, 484 'wait':{'class':PWLDynamics, 485 'args':dummyDyn('likeTalk')}, 486 }, 487 488 }, 489 490 } 491