Package teamwork :: Package examples :: Package school :: Module SchoolClasses
[hide private]
[frames] | no frames]

Source Code for Module teamwork.examples.school.SchoolClasses

  1  from teamwork.widgets.bigwidgets import * 
  2  from teamwork.dynamics.pwlDynamics import * 
  3  from teamwork.agent.AgentClasses import * 
  4  from violence import * 
  5  from laugh import * 
  6  from teamwork.math.Interval import * 
  7   
  8  ##teacher: 
  9  ##weak 
 10  ##hates me 
 11  ##likes victim 
 12  ##abusive 
 13  ##fair 
 14   
 15  teacher = {'nothing': 
 16             {'class':'default','action':{"type":"wait"}}, 
 17   
 18             'abusive': 
 19             {'class':'observation', 'depth': 4, 'actor': 'bully', 'type': 'pickOn', 
 20              'action':{"type":"punishAll"}}, 
 21   
 22             'hates-me': 
 23             {'class':'default','action':{"type":"punish","object":"bully"}}, 
 24   
 25             'punish-laughter': 
 26             {'class':'observation', 'depth': 4, 'type': 'laughAt', 
 27              'action':{"type":"punishAll"}}, 
 28   
 29             'retaliate': 
 30             {'class':'observation', 'depth': 4, 'actor': 'bully', 'type': 'pickOn', 
 31              'action':{"type":"punish","object":"bully"}}, 
 32   
 33             'severe': 
 34             {'class':'observation', 'depth': 4, 'actor': 'bully', 'type': 'pickOn', 
 35              'action':{"type":"severelyPunish","object":"bully"}}, 
 36   
 37             'conditional': 
 38             {'class':'conjunction', 
 39              'clauses':[{'class':'belief','keys':['entities','self','state','power'], 
 40                          'range':Interval(0.5,1.0,)}, 
 41                         {'class':'observation','depth': 4, 'actor': 'bully', 
 42                          'type': 'pickOn'}], 
 43              'action':{"type":"punish","object":"bully"}}, 
 44              
 45             } 
 46   
 47  TeacherModels = {'mean': 
 48                   {'goals':[{'entity':['self'],'direction':'min','type':'state', 
 49                              'key':'power','weight':0.5}], 
 50                    'policy':[teacher['retaliate'], 
 51                              teacher['nothing']] 
 52                    }, 
 53                   'fed-up': 
 54                   {'goals':[{'entity':['Student'],'direction':'max', 
 55                              'type':'state','key':'power','weight':0.5}], 
 56                    'policy':[teacher['punish-laughter'], 
 57                              teacher['severe'], 
 58                              teacher['nothing']] 
 59                    }, 
 60                   'fair': 
 61                   {'goals':[{'entity':['Student'],'direction':'max', 
 62                              'type':'state','key':'power','weight':0.5}], 
 63                    'policy':[teacher['punish-laughter'], 
 64                              teacher['retaliate'], 
 65                              teacher['nothing']] 
 66                    }, 
 67                   'weak': 
 68                   {'goals':[{'entity':['Student'],'direction':'max', 
 69                              'type':'state','key':'power','weight':0.5}], 
 70                    'policy':[teacher['conditional'], 
 71                              teacher['nothing']] 
 72                    } 
 73                   } 
 74       
 75  rules = {'attention': 
 76           {'class':'conjunction', 
 77            'clauses':[{'class':'observation', 'depth': 4, 'type': 'laughAt', 
 78                        'object': 'victim',}, 
 79                       {'class':'observation', 'depth': 4, 'actor': 'self', 
 80                        'type': 'pickOn', 'object': 'victim',},], 
 81            'action':{"type":"pickOn","object":"victim"}}, 
 82   
 83           'retaliate': 
 84           {'class':'observation', 'depth': 4, 'actor': 'teacher', 
 85            'type': 'punish', 'object': 'self', 
 86            'action':{"type":"pickOn","object":"victim"}}, 
 87   
 88           'encourage': 
 89           {'class':'observation', 'depth': 4, 'type': 'pickOn', 
 90            'object': 'victim', 'action': {"type":"laughAt","object":"victim"}}, 
 91   
 92           'cower-individual': 
 93           {'class':'observation', 'depth': 4, 'actor': 'teacher', 
 94            'type': 'punish', 'object': 'self', 'action': {"type":"wait"}}, 
 95   
 96           'cower': 
 97           {'class':'observation', 'depth': 4, 'actor': 'teacher', 
 98            'type': 'punishAll', 'action': {"type":"wait"}}, 
 99            
100           'defaultNOP': 
101           {'class':'default', 'action': {"type":"wait"}}, 
102   
103           'defaultFight': 
104           {'class':'default', 'action': {"type":"pickOn","object":"victim"}}, 
105           } 
106   
107  OnlookerModels = {'discouraging': 
108                    {'goals':[{'entity':['self'],'direction':'max','type':'state', 
109                               'key':'power','weight':1.}], 
110                     'policy':[rules['defaultNOP']]}, 
111                    'encouraging': 
112                    {'goals':[{'entity':['victim'],'direction':'min','type':'state', 
113                               'key':'power','weight':0.2}, 
114                              {'entity':['self'],'direction':'max','type':'state', 
115                               'key':'power','weight':0.8}], 
116                     'policy':[rules['encourage'], 
117                               rules['defaultNOP']]}, 
118                    'scared': 
119                    {'goals':[{'entity':['self'],'direction':'max','type':'state', 
120                               'key':'power','weight':1.}], 
121                     'policy':[rules['cower-individual'], 
122                               rules['cower'], 
123                               rules['encourage'], 
124                               rules['defaultNOP']]} 
125                    }            
126   
127   
128  VictimModels = {'passive': 
129                    {'goals':[{'entity':['self'],'direction':'max','type':'state', 
130                               'key':'power','weight':1.}], 
131                     'policy':[rules['defaultNOP']]}, 
132                  'vindictive': 
133                  {'goals':[{'entity':['self'],'direction':'max','type':'state', 
134                               'key':'power','weight':.7}, 
135                              {'entity':['bully'],'direction':'min','type':'state', 
136                               'key':'power','weight':.3}], 
137                   'policy':[rules['defaultNOP']]} 
138                  } 
139  ##victim: 
140  ##    hates me 
141  ##    thinks he is better than me / special / etc. 
142   
143  ##onlooker: 
144  ##    do not care 
145  ##    funny to beat on victim 
146  ##    sad to beat on victim 
147  ##    scared 
148   
149  ##principal: 
150  BullyModels = {'sadistic': 
151                 {'goals':[{'entity':['self'], 
152                            'direction':'max','type':'state', 
153                            'key':'power','weight':.1}, 
154                           {'entity':['Onlooker'], 
155                            'direction':'max','type':'act','object':'victim', 
156                            'key':'laughAt','weight':.1}, 
157                           {'entity':['victim'], 
158                            'direction':'min','type':'state', 
159                            'key':'power','weight':.8}, 
160                           ], 
161                  'policy':[rules['cower-individual'], 
162                            rules['cower'], 
163                            rules['defaultFight']]}, 
164                  
165                 'attention-seeking': 
166                 {'goals':[{'entity':['self'], 
167                            'direction':'max','type':'state', 
168                            'key':'power','weight':.1}, 
169                           {'entity':['Onlooker'], 
170                            'direction':'max','type':'act','object':'victim', 
171                            'key':'laughAt','weight':.8}, 
172                           {'entity':['victim'], 
173                            'direction':'min','type':'state', 
174                            'key':'power','weight':.1}, 
175                           ], 
176                  'policy':[rules['attention'], 
177                            rules['cower-individual'], 
178                            rules['cower'], 
179                            rules['defaultFight']]}, 
180                 'rebellious': 
181                 {'goals':[{'entity':['teacher'],'direction':'min','type':'state', 
182                            'key':'power','weight':0.4}, 
183                           {'entity':['self'],'direction':'max','type':'state', 
184                            'key':'power','weight':0.6}, 
185                           {'entity':['Onlooker'],'direction':'max','type':'act', 
186                            'key':'laughAt','weight':0.2}], 
187                  'policy':[rules['cower-individual'], 
188                            rules['defaultFight']]}, 
189                 }                      
190   
191   
192   
193  classHierarchy['Entity']['horizon'] = 1 
194  classHierarchy['Entity']['depth'] = 2 
195   
196  classHierarchy['Student'] = { 
197      'parent': ['Entity'], 
198      'actions':{'type':None}, 
199      'state':{'power':0.3}, 
200      'relationships':{#'teacher':['Teacher'], 
201                       'victim':['Student']}, 
202      'dynamics':{'power':{'pickOn':{'class':PWLDynamics, 
203                                     'args':genPunishClass('Victim','power',.5)}, 
204                           'laughAt':{'class':PWLDynamics, 
205                                    'args':genPunishClass('Victim','power',.25)}, 
206                           'punish':{'class':PWLDynamics, 
207                                     'args':genAttackDyn('power')}, 
208                           'punishAll':{'class':PWLDynamics, 
209                                        'args':genPunishClass('Student', 
210                                                              'power')}, 
211                           'punishBully':{'class':PWLDynamics, 
212                                          'args':genPunishClass('Bully', 
213                                                              'power')}, 
214                           'punishOnlooker':{'class':PWLDynamics, 
215                                             'args':genPunishClass('Onlooker', 
216                                                                   'power')}, 
217                           'scoldAll':{'class':PWLDynamics, 
218                                        'args':genPunishClass('Student', 
219                                                              'power',.5)}, 
220                           'scoldBully':{'class':PWLDynamics, 
221                                          'args':genPunishClass('Bully', 
222                                                                'power',.5)}, 
223                           'scoldOnlooker':{'class':PWLDynamics, 
224                                            'args':genPunishClass('Onlooker', 
225                                                                  'power',.5)}, 
226                           'severelyPunish':{'class':PWLDynamics, 
227                                             'args':genAttackDyn(fromFeature='power', 
228                                                         scale=3.)} 
229                           } 
230                  }, 
231  ##    'beliefs':{'Teacher':{'_trustworthiness':0.3, 
232  ##                          '_likeability':-0.2, 
233  ##                          'model':'fair' 
234  ##                          }, 
235  ##               'Bully':{'_trustworthiness':-0.4, 
236  ##                        '_likeability':-0.1, 
237  ##                        'model':'rebellious' 
238  ##                        }, 
239  ##               'Victim':{'_trustworthiness':-0.2, 
240  ##                         '_likeability':-0.1} 
241  ##               }, 
242      } 
243   
244  classHierarchy['Onlooker'] = { 
245      'parent': ['Student'], 
246      'actions':{'type':'AND', 
247                 'key':'object', 
248                 'values':[{'type':'generic','value':'Victim'}], 
249                 'base': {'type':'XOR', 
250                          'key':'type', 
251                          'values':[{'type':'literal','value':'laughAt'}, 
252                                    {'type':'literal','value':'wait'}], 
253                          }, 
254                 }, 
255  ##     'beliefs':{'self':{'model':'encouraging'}, 
256  ##                'Teacher':{'_trustworthiness':0.3, 
257  ##                           '_likeability':-0.2, 
258  ##                           'model':'fair'}, 
259  ##                'Bully':{'_trustworthiness':-0.4, 
260  ##                         '_likeability':0.1, 
261  ##                         'model':'rebellious' 
262  ##                         }, 
263  ##                'Victim':{'_trustworthiness':-0.2, 
264  ##                          '_likeability':-0.1} 
265  ##                }, 
266      'goals': [{'entity':['self'], 
267                'direction':'max','type':'state', 
268                'key':'power','weight':1.0}, 
269                {'entity':['Victim'], 
270                 'direction':'min','type':'state', 
271                 'key':'power','weight':0.3} 
272                ], 
273  ##    'models':OnlookerModels 
274      } 
275   
276  classHierarchy['Teacher'] = { 
277      'parent': ['Entity'], 
278  ##    'relationships':{'bully':['Bully']}, 
279      'goals': [ 
280  ##    {'entity':['Onlooker'],'direction':'min','type':'act', 
281  ##     'key':'pickOn','weight':0.8}, 
282      {'entity':['Student'],'type':'state', 
283       'direction':'max','key':'power','weight':0.2} 
284               ], 
285      'dynamics':{'power':{'punish':{'class':PWLDynamics, 
286                                     'args':genAttackDyn('power')}, 
287                           'punishAll':{'class':PWLDynamics, 
288                                        'args':genPunishClass('Student', 
289                                                              'power')}, 
290                           'severelyPunish':{'class':PWLDynamics, 
291                                             'args':genAttackDyn(fromFeature='power', 
292                                                         scale=3.)} 
293                           } 
294                  }, 
295      'actions':{'type':'XOR', 
296                 'key':'type', 
297                 'values':[{'type':'literal','value':'punishBully'}, 
298                           {'type':'literal','value':'punishAll'}, 
299                           {'type':'literal','value':'punishOnlooker'}, 
300                           {'type':'literal','value':'scoldBully'}, 
301                           {'type':'literal','value':'scoldAll'}, 
302                           {'type':'literal','value':'scoldOnlooker'}, 
303                           {'type':'literal','value':'wait'}, 
304                           ], 
305                 }, 
306  ##    'actions':{'type':'AND', 
307  ##               'key':'object', 
308  ##               'values':[{'type':'generic','value':'Bully'}], 
309  ####                'values':[{'type':'generic','value':'Student'}], 
310  ##               'base': {'type':'XOR', 
311  ##                        'key':'type', 
312  ##                        'values':[{'type':'literal','value':'punish'}, 
313  ##                                  {'type':'literal','value':'severelyPunish'}, 
314  ##                                  {'type':'literal','value':'wait'}], 
315  ##                        }, 
316  ##               }, 
317      'state':{'power':0.7}, 
318  ##    'models':TeacherModels, 
319      'widget':'box', 
320  ##     'beliefs':{'self':{'model':'fair'}, 
321  ##                'Bully':{'model':'attention-seeking', 
322  ##                         '_trustworthiness':-0.2, 
323  ##                         '_likeability':-0.3}, 
324  ##                'Onlooker':{'model':'scared', 
325  ##                           '_trustworthiness':0.1, 
326  ##                           '_likeability':0.3}, 
327  ##                'Victim':{'model':'passive', 
328  ##                          '_trustworthiness':0.1, 
329  ##                          '_likeability':0.3}} 
330      } 
331   
332  classHierarchy['Bully'] = { 
333      'parent': ['Student'], 
334      'state':{'power':0.4}, 
335      'actions':{'type':'AND', 
336                 'key':'object', 
337                 'values':[{'type':'relationship','value':'victim'}], 
338                 'base': {'type':'XOR', 
339                          'key':'type', 
340                          'values':[{'type':'literal','value':'pickOn'}, 
341                                    {'type':'literal','value':'wait'}], 
342                          }, 
343                 }, 
344      'goals': [{'entity':['self'], 
345                 'direction':'max','type':'state', 
346                 'key':'power','weight':.4}, 
347                {'entity':['Onlooker'], 
348                 'direction':'max','type':'act','object':'victim', 
349                 'key':'laughAt','weight':.3}, 
350                {'entity':['victim'], 
351                 'direction':'min','type':'state', 
352                 'key':'power','weight':.3}, 
353                ], 
354  ##     'beliefs':{'self':{'model':'rebellious'}, 
355  ##                'Teacher':{'_trustworthiness':-0.3, 
356  ##                           '_likeability':-0.5, 
357  ##                           'model':'fair'}, 
358  ##                'Victim':{'model':'passive', 
359  ##                          '_trustworthiness':-0.4, 
360  ##                          '_likeability':-0.6}, 
361  ##                'Onlooker':{'_trustworthiness':-0.2, 
362  ##                           '_likeability':0.1, 
363  ##                           'model':'encouraging' 
364  ##                           } 
365  ##                }, 
366      'widget':'polygon', 
367  ##    'models':BullyModels 
368      } 
369   
370  ##classHierarchy['AttentionSeekingBully'] = { 
371  ##    'parent':['Bully'], 
372  ##    'goals': [{'entity':['self'], 
373  ##               'direction':'max','type':'state', 
374  ##               'key':'power','weight':.1}, 
375  ##              {'entity':['Onlooker'], 
376  ##               'direction':'max','type':'act','object':'victim', 
377  ##               'key':'laughAt','weight':.8}, 
378  ##              {'entity':['victim'], 
379  ##               'direction':'min','type':'state', 
380  ##               'key':'power','weight':.1}, 
381  ##              ] 
382  ##    } 
383   
384  ##classHierarchy['SadisticBully'] = { 
385  ##    'parent':['Bully'], 
386  ##    'goals': [{'entity':['self'], 
387  ##               'direction':'max','type':'state', 
388  ##               'key':'power','weight':.1}, 
389  ##              {'entity':['Onlooker'], 
390  ##               'direction':'max','type':'act','object':'victim', 
391  ##               'key':'laughAt','weight':.1}, 
392  ##              {'entity':['victim'], 
393  ##               'direction':'min','type':'state', 
394  ##               'key':'power','weight':.8}, 
395  ##              ] 
396  ##    } 
397   
398  ##classHierarchy['DominatingBully'] = { 
399  ##    'parent':['Bully'], 
400  ##    'goals': [{'entity':['Onlooker'], 
401  ##               'direction':'max','type':'act','object':'victim', 
402  ##               'key':'laughAt','weight':.1}, 
403  ##              {'entity':['self'], 
404  ##               'direction':'max','type':'state', 
405  ##               'key':'power','weight':.8}, 
406  ##              {'entity':['victim'], 
407  ##               'direction':'min','type':'state', 
408  ##               'key':'power','weight':.1}, 
409  ##              ] 
410  ##    } 
411   
412  classHierarchy['Victim'] = { 
413      'parent': ['Student'], 
414      'state':{'power':0.2}, 
415  ##     'beliefs':{'self':{'model':'passive'}, 
416  ##                'Teacher':{'_likeability':0.2, 
417  ##                           'model':'fair' 
418  ##                           }, 
419  ##                'Bully':{'_trustworthiness':-0.5, 
420  ##                         '_likeability':-0.5, 
421  ##                         'model':'sadistic' 
422  ##                         }, 
423  ##                'Onlooker':{'model':'encouraging', 
424  ##                           '_trustworthiness':-0.2, 
425  ##                           '_likeability':-0.2} 
426  ##                }, 
427  ##    'models':VictimModels, 
428      'goals': [{'entity':['self'], 
429                'direction':'max','type':'state', 
430                'key':'power','weight':1.0} 
431                ] 
432      } 
433   
434                  
435  if __name__ == '__main__': 
436      import os.path 
437      from teamwork.multiagent.GenericSociety import * 
438   
439      soc = GenericSociety() 
440      soc.importDict(classHierarchy) 
441       
442      name = '/tmp/%s.xml' % (os.path.basename(__file__)) 
443      f = open(name,'w') 
444      f.write(soc.__xml__().toxml()) 
445      f.close() 
446   
447      new = GenericSociety() 
448      new.parse(parse(name)) 
449  ##    print new 
450