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

Source Code for Module teamwork.examples.Thespian.ThespianUtils

  1  import string 
  2  import sys 
  3  
 
  4  from teamwork.math.KeyedTree import * 
  5  from teamwork.math.KeyedMatrix import * 
  6  from teamwork.dynamics.pwlDynamics import * 
  7  from teamwork.dynamics.arbitraryDynamics import * 
  8  
 
  9  ##ObligList = ['greet_resp','greet2_init','greet2_resp','thank','urwelcome','bye_resp','OK','accept']
 
 10  ObligList = ['greet_resp','bye_resp'] 
 11  
 
12 -def createObsStates(entities):
13 for entity in entities: 14 state_value = entity.getState('type') 15 for Entity_type, prob in state_value.items(): 16 break 17 18 if Entity_type==0: 19 continue 20 21 for other in entities: 22 for act1 in other.actions.getOptions(): 23 for act in act1: 24 for addressee in act['addressee']: 25 obsstate='obs-'+act['actor']+'-'+act['sact_type']+'-to-'+addressee 26 27 if act['sact_type'] in ['enquiry','inform','request','accept','reject','inform_info']: 28 obsstate += '-about-'+act['factors'][0]['lhs'][1]+ '-'+act['factors'][0]['lhs'][3] 29 30 entity.setState(obsstate,0)
31
32 -def ObsStateDyns(entities):
33 for entity in entities: 34 for feature in entity.getStateFeatures(): 35 if string.find(feature,'obs')==0 : 36 37 ## won't overwrite existing dynamics 38 if entity.dynamics.has_key(feature): 39 continue 40 41 key =feature 42 try: 43 dummy1, actor, sact_type, dummy2, addr, dummy3,about_entity,about_feature = string.split(feature,'-') 44 ## 'student, any, inform_info, 0.0, 0.0, entities:student:state:feature_identity=.1' 45 actstr=actor+', '+addr+', '+sact_type+', 0.0, 0.0, entities:'+about_entity+':state:'+about_feature+'= 0.0' 46 entity.dynamics[key]={actstr:apply(PWLDynamics, (setDyn(feature,1),))} 47 except: 48 try: 49 dummy1, actor, sact_type, dummy2, addr = string.split(feature,'-') 50 actstr=actor+', '+addr+', '+sact_type+', 0.0, 0.0, entities:student:state:norm= 0.0' 51 entity.dynamics[key]={actstr:apply(PWLDynamics, (setDyn(feature,1),))} 52 except ValueError: 53 print string.split(feature,'-')
54 55 56 57
58 -def createObligations(entities,level=1,type=0):
59 depth = level 60 for entity in entities: 61 62 state_value = entity.getState('type') 63 for Entity_type, prob in state_value.items(): 64 break 65 66 if Entity_type==0: 67 continue 68 for other in entities: 69 70 state_value = other.getState('type') 71 for Other_type, prob in state_value.items(): 72 break 73 74 if Other_type==0: 75 continue 76 77 state_value = entity.getState('group') 78 for Entity_group, prob in state_value.items(): 79 break 80 81 state_value = other.getState('group') 82 for Other_group, prob in state_value.items(): 83 break 84 85 if not Entity_group == Other_group: 86 for act in ObligList: 87 obligation='obli-'+act+'-to-'+other.name 88 entity.setState(obligation,0) 89 90 ## we do not need to handle the content of inform 91 if type == 0: 92 obligation='obli-inform-to-'+other.name 93 entity.setState(obligation,0) 94 continue 95 96 ## otherwise we will add obligation of informs with content 97 depth = level 98 while depth>0: 99 for subEntity in entities: 100 if not entity.name == subEntity.name: 101 102 state_value = subEntity.getState('group') 103 for subEntity_group, prob in state_value.items(): 104 break 105 106 state_value = entity.getState('group') 107 for Entity_group, prob in state_value.items(): 108 break 109 110 111 for feature in subEntity.getStateFeatures(): 112 if string.find(feature,'feature')==0: 113 obligation='obli-'+'inform-to-'+other.name+'-about-'+subEntity.name+'-'+feature 114 entity.setState(obligation,0) 115 else: 116 oldfeatures=[] 117 for feature in subEntity.getStateFeatures(): 118 oldfeatures.append(feature) 119 for feature in oldfeatures: 120 if string.find(feature,'feature')==0: 121 obligation='obli-'+'inform-to-'+other.name+'-about-'+subEntity.name+'-'+feature 122 entity.setState(obligation,0) 123 depth-=1
124 125 126
127 -def SatisfyObliPolicies(entities):
128 ## add obligation triggered policies like this 129 ## "belief entities self state should_thank -.9 1 -> student, Rahim, thank, 0.0, 0.0, entities:student:state:feature_identity = .1:.1", 130 p = ['']*17 131 p[0] = '' 132 p[1] = 'belief entities self state ' 133 p[2] = '' ##should_thank 134 p[3] = ' -.9 1 -> ' 135 p[4] = '' ##student 136 p[5] = ', ' 137 p[6]='' ## Rahim 138 p[7]= ', ' 139 p[8]= '' ## thank 140 p[9] = ', 0.0, 0.0, entities:' 141 p[10] = 'stuent' ##can be set 142 p[11] = ':state:' 143 p[12] = 'norm' ##can be set 144 p[13] = ' = ' 145 p[14] = '.1'##can be set 146 p[15] = ':' 147 p[16] = '.1'##can be set 148 149 for entity in entities: 150 state_value = entity.getState('type') 151 for Entity_type, prob in state_value.items(): 152 break 153 154 if Entity_type==0: 155 continue 156 try: 157 ## by default add these policies to model simple 158 model = entity.models['simple'] 159 except: 160 print 'entity ',entity.name,' has no model named simple' 161 continue 162 entries = [] 163 164 for feature in entity.getStateFeatures(): 165 if string.find(feature,'obli')==0 and not feature == 'obligNorm': 166 167 ## for scene 1, special rules are suplied 168 if feature == 'obli-accept-to-student' and entity.name in ['Baad','Rahim']: 169 continue 170 171 ## obli-inform-to-Hamed-about-student-feature_nationality 172 ## obli-bye_resp-to-Xaled 173 about_entity = None 174 try: 175 dummy1,sact_type,dummy2,toobject,dummy3,about_entity,about_feature = string.split(feature,'-') 176 except ValueError: 177 try: 178 dummy1,sact_type,dummy2,toobject = string.split(feature,'-') 179 except: 180 print 'WARNING: Illegal obligation name: ',feature 181 continue 182 183 p[2] = feature 184 p[4] = entity.name 185 p[6] = toobject 186 p[8] = sact_type 187 188 if about_entity: 189 p[10] = about_entity 190 p[12] = about_feature 191 else: 192 p[10] = 'student' 193 p[12] = 'norm' 194 ## don't know how to set rhs yet 195 196 ## p[12] = entity.getBelief(about_entity,about_feature)['lo'] 197 ## p[14] = entity.getBelief(about_entity,about_feature)['hi'] 198 199 200 entry = string.join(p) 201 ## print entry 202 ## entries.append(entry) 203 ## model.policy.extend(entries,entity.actionClass,entity) 204 model.policy.append(entry) 205 206 model.policy.append("default -> {'type':'wait'}")
207
208 -def ObligationDyns(entities):
209 for entity in entities: 210 for feature in entity.getStateFeatures(): 211 if string.find(feature,'obli')==0 and not feature == 'obligNorm': 212 213 ## won't overwrite existing dynamics 214 if entity.dynamics.has_key(feature): 215 continue 216 217 auto_update_ogli=0 218 for act in ObligList.append('inform'): 219 if feature.find(act)>-1: 220 auto_update_ogli=1 221 222 if auto_update_ogli==0: 223 continue 224 225 key =feature 226 ## entity.dynamics[key]={'any':apply(PWLDynamics, (ObligationDyn(feature),))} 227 entity.dynamics[key]={'self-any-to-any':apply(PWLDynamics, (SatisfyObligationDyn(feature),)), 228 'any-any-to-self':apply(PWLDynamics, (CreateObligationDyn(feature),))}
229 230 231
232 -def addNormToAgents(society,normClasses):
233 ## print society 234 for entity in society: 235 ## add state 236 for feature,value in normClasses['NormAgent']['state'].items(): 237 society[entity].setState(feature,value) 238 239 ## add dynamics 240 if type(normClasses['NormAgent']['dynamics']) == dict: 241 dyndict = [normClasses['NormAgent']['dynamics']] 242 else: 243 dyndict = normClasses['NormAgent']['dynamics'] 244 245 for dynamics in dyndict: 246 for feature,dynDict in dynamics.items(): 247 for act,dyn in dynDict.items(): 248 if isinstance(dyn,DecisionTree): 249 dyn = {'class':PWLDynamics, 250 'args':{'tree':dyn}} 251 fun = dyn['class'] 252 args = dyn['args'] 253 if not society[entity].dynamics.has_key(feature): 254 society[entity].dynamics[feature] = {} 255 if not society[entity].dynamics[feature].has_key(act): 256 society[entity].dynamics[feature][act] = apply(fun,(args,)) 257 ## add goals 258 for goal in normClasses['NormAgent']['goals']: 259 try: 260 key = goal['key'] 261 except KeyError: 262 key = goal['feature'] 263 print 'Warning: Use "key" instead of "feature" when specifying goals' 264 goalObj = MinMaxGoal(entity=goal['entity'], 265 direction=goal['direction'], 266 goalType=goal['type'], 267 key=key) 268 try: 269 ## norm goals are twice as important as other goals 270 goalObj.weight = goal['weight']*2 271 except KeyError: 272 raise KeyError,'%s has goal "%s" with no weight' % \ 273 (self.ancestry(),`goalObj`) 274 society[entity].setGoalWeight(goalObj,goalObj.weight,False) 275 society[entity].normalizeGoals() 276 277 ## add model 278 for name,model in normClasses['NormAgent']['models'].items(): 279 agent = GenericModel(name) 280 agent.setGoals(model['goals']) 281 agent.policy = model['policy'] 282 society[entity].models[name] = agent 283 return society
284 285 286
287 -def stringToAct(act):
288 try: 289 actor,type,obj = act.split('-') 290 except: 291 actor,type1,type2,obj = act.split('-') 292 type=type1+'-'+type2 293 return actor,type,obj
294