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

Source Code for Module teamwork.examples.Thespian.appraisalDimentions

  1  import string 
  2  import sys 
  3  import copy 
  4  from ThespianAgents import *  
  5  
 
  6  fixedgoals=['sameLocation','actAlive','specialRule','resp-norm'] 
  7  ##fixedgoals=['resp-norm','conversation-flow-norm']
 
  8  
 
9 -class appraisal():
10 initEntities = None 11 curEntities = None 12 history = [] 13
14 - def setupEntities(self,entities):
15 for entityName in entities: 16 self.setupDynamics(entities,entityName)
17 18 19
20 - def setupDynamics(self,entities,entity,copyEntities=None):
21 if copyEntities == None: 22 copyEntities=self.initEntities 23 entity = entities__getitem__(entity) 24 if len(entity.entities)>0: 25 for entityName in entity.entities: 26 self.setupDynamics(entity.entities,entityName,copyEntities) 27 if entity.parent: 28 ##print entity.ancestry() 29 entity.dynamics = copy.deepcopy(copyEntities__getitem__(entity.name).dynamics)
30 31 32
33 - def lookaheadTest(self,lookahead = 2):
34 entities = copy.deepcopy(self.curEntities) 35 self.setupEntities(entities) 36 37 while lookahead>0: 38 nextturn = entities.next() 39 delta = entities.microstep(turns=nextturn,explain=False,hypothetical=False) 40 #print delta['decision'] 41 lookahead = lookahead-1
42 43 44
45 - def updateHistory(self,action):
46 self.history.append(action) 47 turns = [{'name':action['actor'],'choices':[[action]]}] 48 self.curEntities.microstep(turns,explain=False,hypothetical=False)
49 50
51 - def simulatePath(self, history = None):
52 if history == None: 53 history = self.history 54 55 entities = copy.deepcopy(self.initEntities) 56 self.setupEntities(entities) 57 58 59 for preAction in history: 60 turns = [{'name':preAction['actor'],'choices':[[preAction]]}] 61 entities.microstep(turns,hypothetical=False, 62 explain=False, 63 ) 64 return entities
65 66
67 - def calUtilities(self,myname,action,lookahead = 0):
68 # calculate what the utility will be without doing this action 69 entities = copy.deepcopy(self.curEntities) 70 self.setupEntities(entities) 71 entities = entities[myname].entities 72 73 preUtility = entities[myname].applyRealGoals(fixedgoals=fixedgoals).expectation() 74 75 76 # calculate what the utility will be after doing this action 77 # include lookahead 78 79 turns = [{'name':action['actor'],'choices':[[action]]}] 80 entities.microstep(turns,hypothetical=False, explain=False,) 81 82 while lookahead > 0: 83 nextturn = entities.next() 84 delta = entities.microstep(turns=nextturn,hypothetical=False,explain=False) 85 print delta['decision'] 86 #print delta['delta'] 87 lookahead = lookahead - 1 88 89 curUtility = entities[myname].applyRealGoals(fixedgoals=fixedgoals).expectation() 90 ## print preUtility, curUtility 91 92 return preUtility, curUtility
93 94 95
96 - def Relevance(self,myname,preUtility=-999, curUtility=-999,lookahead = 0):
97 98 if preUtility == -999: 99 preUtility, curUtility = self.calUtilities(myname,action,lookahead) 100 101 ## print preUtility, curUtility 102 103 if preUtility == 0 and not curUtility == 0: 104 return 1 105 elif preUtility == 0 and curUtility == 0: 106 return 0 107 else: 108 delta = abs ((curUtility - preUtility)/preUtility) 109 return delta
110 111 112
113 - def Desirability(self,myname,preUtility=-999, curUtility=-999,lookahead = 0):
114 115 if preUtility == -999: 116 preUtility, curUtility = self.calUtilities(myname,action,lookahead) 117 118 if preUtility == 0 and curUtility > 0: 119 return 1 120 elif preUtility == 0 and curUtility < 0: 121 return -1 122 elif preUtility == 0 and curUtility == 0: 123 return 0 124 else: 125 return (curUtility - preUtility)/abs(preUtility)
126 127
128 - def novelty(self,myname,action,lookahead = 0):
129 pass 130 #### turnNovelty = 1 131 #### expectedTurn = self.entities[character].entities.next() 132 #### for nextTurn in expectedTurn: 133 #### if action['actor'] == nextTurn['name']: 134 #### turnNovelty = 0 135 136 entities = copy.deepcopy(self.curEntities) 137 self.setupEntities(entities) 138 entities = entities__getitem__(myname).entities 139 140 141 utiList = [] 142 actionUtility = -9999999999 143 higherUtility = 0.0 144 145 actor = action['actor'] 146 if type(actor) == str: 147 actor = entities[actor] 148 149 150 for actionList in entities[actor.name].actions.getOptions(): 151 152 entitiesTmp = copy.deepcopy(entities) 153 self.setupEntities(entitiesTmp) 154 155 turns = [{'name':action['actor'],'choices':[actionList]}] 156 entitiesTmp.microstep(turns,hypothetical=False, explain=False) 157 158 lookaheadCount = lookahead 159 while lookaheadCount > 0: 160 nextturn = entitiesTmp.next() 161 delta = entitiesTmp.microstep(turns=nextturn,hypothetical=False,explain=False) 162 lookaheadCount = lookaheadCount - 1 163 164 utility = entitiesTmp__getitem__(action['actor']).applyGoals().expectation() 165 166 if actionList == [action]: 167 actionUtility = utility 168 else: 169 utiList.append(utility) 170 171 for utility in utiList: 172 if utility - actionUtility > 0: 173 higherUtility = higherUtility+1 174 175 return higherUtility/(len(utiList)+1)
176 177 178 179 180
181 - def causalAttribution(self,myname,action,lookback = 3,lookahead = 0):
182 responsibleAgent = [] 183 responsibility = [] 184 185 responsibleAgent.append(action['actor']) 186 187 entities = copy.deepcopy(self.curEntities) 188 entities = entities__getitem__(myname).entities 189 190 #actorPreUtility = entities[action['actor']].applyGoals().expectation() 191 #myPreUtility = entities[myname].applyGoals().expectation() 192 actorUtility =0.0 193 myUtility =0.0 194 actorUtilityList=[] 195 myUtilityList=[] 196 197 actor = entities[action['actor']] 198 hasGoalAboutMe = 0 199 excludeList = [] 200 relatedPreUti = 0.0 201 relatedUti = 0.0 202 203 if action['actor'] == myname: 204 hasGoalAboutMe = 1 205 else: 206 for goal in actor.getGoals(): 207 if goal.entity == [myname]: 208 hasGoalAboutMe = 1 209 else: 210 excludeList.append(goal.key) 211 212 if hasGoalAboutMe: 213 relatedPreUti = entities__getitem__(action['actor']).applyRealGoals(fixedgoals=excludeList).expectation() 214 215 # choices that both the actor and I will not lose in utility 216 ggChoice = 0.0 217 218 for actionList in entities__getitem__(action['actor']).actions.getOptions(): 219 #if lookahead>0: 220 # print actionList, 221 entitiesTmp = copy.deepcopy(entities) 222 self.setupEntities(entitiesTmp) 223 224 turns = [{'name':action['actor'],'choices':[actionList]}] 225 entitiesTmp.microstep(turns,hypothetical=False, explain=False) 226 227 lookaheadCount = lookahead 228 while lookaheadCount > 0: 229 nextturn = entitiesTmp.next() 230 delta = entitiesTmp.microstep(turns=nextturn,hypothetical=False, explain=False) 231 #print delta['decision'], 232 lookaheadCount = lookaheadCount - 1 233 #print 234 235 actorCurUtility = entitiesTmp__getitem__(action['actor']).applyGoals().expectation() 236 myCurUtility = entitiesTmp__getitem__(myname).applyGoals().expectation() 237 238 if actionList == [action]: 239 if hasGoalAboutMe: 240 relatedUti = entitiesTmp__getitem__(action['actor']).applyRealGoals(fixedgoals=excludeList).expectation() 241 if relatedUti > relatedPreUti: 242 responsibility.append('Actor has goals about me, and get benifited though his/her action') 243 return responsibleAgent, responsibility 244 actorUtility = actorCurUtility 245 myUtility = myCurUtility 246 else: 247 actorUtilityList.append(actorCurUtility) 248 myUtilityList.append(myCurUtility) 249 250 for i in range(len(actorUtilityList)): 251 if (actorUtilityList[i] >= actorUtility) and (myUtilityList[i] >= myUtility): 252 ggChoice = ggChoice+1 253 254 #actorBeliefMyUtility = 255 256 if ggChoice >0: 257 responsibility.append('not coerced, but do not care about me and accidentally choose the action that hurts my utility') 258 else: 259 responsibility.append('coerced to do the action') 260 261 262 ## look back one step, is the actor forced to act this way? 263 if lookback > 0 and ggChoice == 0: 264 historyTmp = copy.copy(self.history) 265 266 for i in range(1,lookback): 267 if len(historyTmp)<= 0: 268 break 269 270 lastAct = historyTmp.pop() 271 responsibleAgent.append(lastAct['actor']) 272 273 entities = self.simulatePath(historyTmp) 274 entities = entities__getitem__(myname).entities 275 276 actorUtility =0.0 277 myUtility =0.0 278 actorUtilityList=[] 279 myUtilityList=[] 280 281 actor = entities[lastAct['actor']] 282 hasGoalAboutMe = 0 283 excludeList = [] 284 relatedPreUti = 0.0 285 relatedUti = 0.0 286 for goal in actor.getGoals(): 287 if goal.entity == [myname]: 288 hasGoalAboutMe = 1 289 else: 290 excludeList.append(goal.key) 291 if hasGoalAboutMe: 292 relatedPreUti = entities__getitem__(lastAct['actor']).applyRealGoals(fixedgoals=excludeList).expectation() 293 294 # choices that both the actor and I will not lose in utility 295 ggChoice = 0.0 296 297 for actionList in entities__getitem__(lastAct['actor']).actions.getOptions(): 298 299 entitiesTmp = copy.deepcopy(entities) 300 self.setupEntities(entitiesTmp) 301 302 turns = [{'name':lastAct['actor'],'choices':[actionList]}] 303 entitiesTmp.microstep(turns,hypothetical=False, explain=False) 304 305 lookaheadCount = lookahead+i 306 while lookaheadCount > 0: 307 nextturn = entitiesTmp.next() 308 delta = entitiesTmp.microstep(turns=nextturn,hypothetical=False, explain=False) 309 lookaheadCount = lookaheadCount - 1 310 311 actorCurUtility = entitiesTmp__getitem__(lastAct['actor']).applyGoals().expectation() 312 myCurUtility = entitiesTmp__getitem__(myname).applyGoals().expectation() 313 314 if actionList == [lastAct]: 315 if hasGoalAboutMe: 316 relatedUti = entitiesTmp__getitem__(lastAct['actor']).applyRealGoals(fixedgoals=excludeList).expectation() 317 if relatedUti > relatedPreUti: 318 responsibility.append('Actor has goals about me, and get benifited though his/her action') 319 return responsibleAgent, responsibility 320 actorUtility = actorCurUtility 321 myUtility = myCurUtility 322 else: 323 actorUtilityList.append(actorCurUtility) 324 myUtilityList.append(myCurUtility) 325 326 for i in range(len(actorUtilityList)): 327 if (actorUtilityList[i] >= actorUtility) and (myUtilityList[i] >= myUtility): 328 ggChoice = ggChoice+1 329 330 if ggChoice >0: 331 responsibility.append('not coerced, but do not care about me and accidentally choose the action that hurts my utility') 332 else: 333 responsibility.append('coerced to do the action') 334 335 336 337 return responsibleAgent, responsibility
338 339
340 - def Agency(self,myname,agent, action,lookahead = 0,fixSteps = 2):
341 342 entities = copy.deepcopy(self.curEntities) 343 entities = entities[myname].entities 344 345 if len(entities[agent].actions.getOptions()) == 0: 346 return 0 347 348 preUtility = entities__getitem__(myname).applyGoals().expectation() 349 350 turns = [{'name':action['actor'],'choices':[[action]]}] 351 entities.microstep(turns,hypothetical=False,explain=False,) 352 353 lookaheadCount = lookahead 354 while lookaheadCount > 0: 355 turns = entities.next() 356 entities.microstep(turns,hypothetical=False, explain=False,) 357 lookaheadCount = lookaheadCount - 1 358 359 curUtility = entities__getitem__(myname).applyGoals().expectation() 360 361 # test if in the next i steps I can fix the problem 362 Choice1 = 0.0 363 Choice2 = 0.0 364 365 for actionList in entities__getitem__(agent).actions.getOptions(): 366 entitiesTmp = copy.deepcopy(entities) 367 self.setupEntities(entitiesTmp) 368 369 lookaheadCount = fixSteps 370 while lookaheadCount > 0: 371 turns = entitiesTmp.next() 372 for turn in turns: 373 if turn['name'] == agent: 374 turn['choices']=[actionList] 375 entitiesTmp.microstep(turns,hypothetical=False, explain=False,) 376 lookaheadCount = lookaheadCount - 1 377 378 ## need to apply fixed goal first to make sure this is a possible move 379 furUtility = entitiesTmp__getitem__(myname).applyGoals().expectation() 380 381 if furUtility >= preUtility: 382 Choice1 +=1 383 print 'choice1 ',actionList 384 elif furUtility > curUtility: 385 Choice2 +=1 386 print 'choice2 ',actionList 387 388 ## print Choice1,Choice2 389 if Choice1/len(entities__getitem__(agent).actions.getOptions()) >= .5: 390 agency = 1 391 elif (Choice1+Choice2)/len(entities__getitem__(agent).actions.getOptions()) >= .5: 392 agency = .5 393 elif (Choice1+Choice2)/len(entities__getitem__(agent).actions.getOptions()) >= .3: 394 agency = .3 395 elif Choice1+Choice2 > 0: 396 agency = .1 397 else: 398 agency = 0 399 400 return agency
401