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

Source Code for Module teamwork.examples.Thespian.ThespianAgents

  1  from teamwork.agent.DefaultBased import * 
  2  from teamwork.agent.Entities import * 
  3  from teamwork.multiagent.sequential import * 
  4  from teamwork.multiagent.GenericSociety import * 
  5  from teamwork.action.PsychActions import * 
  6  from ThespianActions import ThespianAction 
  7  from ThespianMessage import * 
  8  from ThespianGeneric import * 
  9  
 
 10  from teamwork.dynamics.pwlDynamics import * 
 11                   
 
12 -class ThespianAgents(SequentialAgents):
13 14 sceneID = 6 15
16 - def generateOrder(self,entities=None):
17 """Creates a new order vector. Orders the agents according to the comparison method on the member agents 18 @return: the turn state vector suitable for the initial state of the simulation 19 @rtype: L{KeyedVector} 20 """ 21 if entities is None: 22 entities = self.activeMembers() 23 24 # Create ordered list of relevant agents 25 if self.sceneID == '-1': 26 entities = ['student','Kamela','Xaled','Abasin','Hamed'] 27 elif self.sceneID == '6': 28 newEntities = list(range(len(entities))) 29 for index in range(len(entities)): 30 agent = entities[index] 31 if agent.name == 'usr': 32 newEntities[0] = agent 33 elif agent.name == 'streetrat': 34 newEntities[1] = agent 35 elif agent.name == 'labrat1': 36 newEntities[2] = agent 37 elif agent.name == 'labrat2': 38 newEntities[3] = agent 39 elif agent.name == 'otherTeam': 40 newEntities[4] = agent 41 elif agent.name == 'timer': 42 newEntities[5] = agent 43 else: 44 entities.sort() 45 46 return SequentialAgents.generateOrder(self,entities)
47 48 49
50 - def createTurnDynamics(self,actions):
51 """Computes the change in turn due to the specified actions 52 @param actions: the actions being performed, indexed by actor name 53 @type actions: C{dict:strS{->}L{teamwork.action.PsychActions.Action}[]} 54 @return: a decision tree representing the changes to the standing turn order vector based on the specified actions 55 @rtype: L{KeyedTree} 56 """ 57 58 assert(len(actions) == 1) 59 ## Thespian, update position if activeMembers changes 60 #need_to_update_generateOrder = 0 61 #active = self.activeMembers() 62 #for activeEntity in active: 63 # if not self.positions.has_key(activeEntity.name): 64 # need_to_update_generateOrder = 1 65 # break 66 # 67 #if need_to_update_generateOrder: 68 # self.generateOrder() 69 70 ## updat need_to_update_turn 71 ## special for Thespian 72 actor,action = actions.items()[0] 73 need_to_update_turn = 1 74 ## for act in action: 75 #### if isinstance(act,ThespianMessage) or isinstance(act,ThespianAction): 76 ## try: 77 ## if act['type']!='wait': 78 ## need_to_update_turn = 0 79 ## except: 80 ## try: 81 ## if not string.find(act,'wait')>-1: 82 ## need_to_update_turn = 0 83 ## except: 84 ## pass 85 86 # Unless the actor is taking another turn, then no change 87 88 ## hack for now, not sure how to not compile actions that not possible to happen 89 actor = actions.keys()[0] 90 if not actor in self.activeMembers(): 91 need_to_update_turn = 0 92 93 unchangedMatrix = KeyedMatrix() 94 for agent in self.activeMembers(): 95 key = StateKey({'entity':agent.name, 96 'feature':self.turnFeature}) 97 row = KeyedVector() 98 row[key] = 1. 99 unchangedMatrix[key] = row 100 row = KeyedVector() 101 row[keyConstant] = 1. 102 unchangedMatrix[keyConstant] = row 103 # Check whether anyone ever has a turn 104 ## if len(self) == 0: 105 if len(self) == 0 or need_to_update_turn == 0: 106 tree = KeyedTree() 107 tree.makeLeaf(unchangedMatrix) 108 return tree 109 actor = actions.keys()[0] 110 # Test whether anybody's left to act after these new actions 111 resetWeights = KeyedVector() 112 for agent in self.activeMembers(): 113 if not actions.has_key(agent.name): 114 resetWeights[StateKey({'entity':agent.name, 115 'feature':self.turnFeature})] = 1. 116 resetPlane = KeyedPlane(resetWeights,0.0001) 117 # If so, move the leftover actors up in the turn order 118 updateMatrix = KeyedMatrix() 119 for agent in self.activeMembers(): 120 key = StateKey({'entity':agent.name, 121 'feature':self.turnFeature}) 122 row = KeyedVector() 123 if actions.has_key(agent.name): 124 # Reset activation to 0. 125 pass 126 elif self.positions[agent.name] < self.positions[actor]: 127 # If we're behind the actor, then move up in the order 128 row[key] = 2. 129 else: 130 # If we're ahead of the actor, stay where we are 131 row[key] = 1. 132 updateMatrix[key] = row 133 row = KeyedVector() 134 row[keyConstant] = 1. 135 updateMatrix[keyConstant] = row 136 # Test whether actor is sneaking in a second turn 137 alreadyActed = ThresholdRow(keys=[{'entity':actor, 138 'feature':self.turnFeature}]) 139 updateTree = KeyedTree() 140 updateTree.branch(KeyedPlane(alreadyActed,0.), 141 unchangedMatrix,updateMatrix) 142 # If nobody left to act, start the turn order from the beginning 143 resetMatrix = KeyedMatrix() 144 for agent in self.activeMembers(): 145 key = StateKey({'entity':agent.name, 146 'feature':self.turnFeature}) 147 row = KeyedVector() 148 row[keyConstant] = self.positions[agent.name] 149 resetMatrix[key] = row 150 row = KeyedVector() 151 row[keyConstant] = 1. 152 resetMatrix[keyConstant] = row 153 # Create branch on number of people left to act 154 tree = KeyedTree() 155 tree.branch(resetPlane,resetMatrix,updateTree) 156 return tree
157