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
9
10
11
12
13
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
140
141
142
143
144
145
146
147
148
149
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':{
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
232
233
234
235
236
237
238
239
240
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
256
257
258
259
260
261
262
263
264
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
274 }
275
276 classHierarchy['Teacher'] = {
277 'parent': ['Entity'],
278
279 'goals': [
280
281
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
307
308
309
310
311
312
313
314
315
316
317 'state':{'power':0.7},
318
319 'widget':'box',
320
321
322
323
324
325
326
327
328
329
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
355
356
357
358
359
360
361
362
363
364
365
366 'widget':'polygon',
367
368 }
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412 classHierarchy['Victim'] = {
413 'parent': ['Student'],
414 'state':{'power':0.2},
415
416
417
418
419
420
421
422
423
424
425
426
427
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
450