-
Notifications
You must be signed in to change notification settings - Fork 5
/
ModelSwarm.py
executable file
·662 lines (552 loc) · 23.8 KB
/
ModelSwarm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# ModelSwarm.py
import Tools
from Agent import *
import commonVar as common
common.worldStateExist = True
try:
from WorldState import *
except BaseException:
common.worldStateExist = False
from ActionGroup import *
import random
import os
import numpy as np
from mActions import *
try:
import tkinter # this is only a control, if tkinter is missing, we cannot
# import turtle
from turtle import *
except BaseException:
print("Warning, missing tkinter: Turtle class will not work")
# structure for adding and eliminating tasks
common.addTasks = {}
common.elimTasks = {}
# in this module, a few of the try/except structures are not cotrolled
# for debug
# these try/except constucts, indeed, are not intended to control user
# errors, but a regular flow of inputs
# other try/execpt structures are instead controlled for debug
task = "0 0".split()
class ModelSwarm:
def __init__(self, nAgents, worldXSize, worldYSize, project0):
global task, project
project = project0
# putting in common the address of the ModelSwarm instance
common.modelAddress = self
# the environment
task = "0 0".split() # in case of repeated execution without restarting the shell
self.ff = "" # in case of repeated execution without restarting the shell
self.nAgents = nAgents # bland ones
self.agentList = []
self.worldXSize = worldXSize
self.worldYSize = worldYSize
# types of the agents
agTypeFile = open(project + "/agTypeFile.txt", "r")
self.types = agTypeFile.read().split()
agTypeFile.close()
# print self.types
# classes of the agents
self.classes = {}
try:
agClassFile = open(project + "/agClassFile.txt", "r")
items = agClassFile.read().split()
agClassFile.close()
for i in range(0, len(items), 2):
self.classes[items[i]] = items[i + 1]
except BaseException: # all the types have class Agent
for i in range(len(self.types)):
self.classes[self.types[i]] = "Agent"
# check consistency between types and classes
if len(self.types) != len(self.classes):
print('Mismatch in number of types and classes.')
os.sys.exit(1)
for i in range(len(self.types)):
if self.types[i] not in self.classes:
print('Type', self.types[i], 'has no class.')
os.sys.exit(1)
print('\nAgents and their classes')
for i in range(len(self.classes)):
print('agents', self.types[i], 'have class',
self.classes[self.types[i]])
print("'bland' agents, if any, have always class Agent\n")
# operating sets of the agents
try:
agOperatingSetFile = open(project + "/agOperatingSets.txt", "r")
self.operatingSets = agOperatingSetFile.read().split()
except BaseException:
print('Warning: operating sets not found.')
agOperatingSetFile = False
self.operatingSets = []
if agOperatingSetFile:
agOperatingSetFile.close()
# print self.operatingSets
dictExe = {}
dictExe["project"] = project
exec(
compile(
open("./$$slapp$$/convert_xls_txt.py").read(),
"./$$slapp$$/convert_xls_txt.py",
'exec'),
dictExe)
# objects
def buildObjects(self):
try:
self.verbose = common.verbose
except BaseException:
self.verbose = False
if common.worldStateExist:
self.worldState = WorldState()
else:
self.worldState = 0
# this block is not always useful, but if not necessary does not hurt
# rows commented and new rows added with v. 3.1.1
#self.leftX = int(-self.worldXSize / 2)
self.leftX = -self.worldXSize / 2
#self.rightX = int(self.worldXSize - 1 - self.worldXSize / 2)
self.rightX = self.worldXSize / 2
#self.bottomY = int(-self.worldYSize / 2)
self.bottomY = -self.worldYSize / 2
#self.topY = int(self.worldYSize - 1 - self.worldYSize / 2)
self.topY = self.worldYSize / 2
# internal agents (bland ones)
# the specialized creation function, related to the project,
# stays in mActions.py, in the model folder
for i in range(self.nAgents):
# line='x' creates a dummy input line to keep generalized
# the structure of the createTheAgent function
line = "x"
createTheAgent(self, line, i, agType="bland")
# self.agentList.append(anAgent) internal to mAnctions.py
print()
# external agents, RELATED TO THE SPECIFIC project
files = os.listdir(project)
for agType in self.types:
# extended txt (.txtx)
if agType + ".txtx" in files:
dictExe = {}
dictExe["project"] = project
dictExe["fileName"] = agType
exec(
compile(
open("./$$slapp$$/convert_txtx_txt.py").read(),
"./$$slapp$$/convert_txtx_txt.py",
'exec'),
dictExe)
# update for new .txt file created above
files = os.listdir(project)
for agType in self.types:
if agType + ".txt" not in files:
print("No", agType,
"agents: lacking the specific file", agType + ".txt")
for opSet in self.operatingSets:
if opSet + ".txt" not in files:
print("No", opSet,
"agents: lacking the specific file", opSet + ".txt")
print()
# creating the agents
for agType in self.types:
if agType + ".txt" in files:
f = open(project + "/" + agType + ".txt", "r")
for line in f:
if line.split() != []:
num = int(line.split()[0])
if self.verbose and not common.IPython:
print("creating " + agType + ": agent #", num)
# this output locks IPython when we have large number
# of agents
# print line.split()
# a set of specialized creation function for each model
# are in mActions.py in the model folder
if self.classes[agType] == "Agent":
createTheAgent(self, line, num, agType)
# explictly pass self, here we use a function
else:
# using ad hoc classes
createTheAgent_Class(
self, line, num, agType, self.classes[agType]) # the last is the class
f.close()
for opSet in self.operatingSets:
if opSet + ".txt" in files:
f = open(project + "/" + opSet + ".txt", "r")
for line in f:
if line.split() != []:
num = int(line.split()[0])
for anAgent in self.agentList:
if anAgent.number == num:
anAgent.setAnOperatingSet(opSet)
print("including agent #", num,
"into the operating set", opSet)
f.close()
if self.operatingSets != []:
for anAgent in self.agentList:
anAgent.setContainers()
if self.agentList != []:
for anAgent in self.agentList:
anAgent.setAgentList(self.agentList)
print()
# actions
def buildActions(self):
modelActions = open(project + "/modelActions.txt")
mList = modelActions.read().split()
modelActions.close()
self.actionList = mList
# print self.actionList
# look at basic case schedule, where "move" represents an example of mandatory
# action (in our case generating also a dynamic "jump" action) and
# "read_script" represents an external source of actions
# without reading an external schedule (anyway, in the case
# above, if you do not put a schedule.txt or a schedule.xls file in
# program folder), the "read_script" step simply has no effect)
# basic actionGroup
self.actionGroup0 = ActionGroup("reset")
self.actionGroup0.do = do0 # do is a variable linking a method
self.actionGroup1 = ActionGroup("move")
self.actionGroup1.do = do1 # do is a variable linking a method
# to create other actionGroup ..,
# self.actionGroup2 = ActionGroup (self.actionList[?])
# self.actionGroup2.do = do2 # do is a variable linking a method
# etc.
# this actionGroup is the schedule, which is generalized
# so it is not moved in the actions.py specific to the project
# the task number is huge (100), considering it to be the last one
# the name is identified as the last one, with -1
self.actionGroup100 = ActionGroup("read_script") # the last
def do100(address, cycle):
global task
while True:
if task[0] == "#":
if int(task[1]) > cycle:
break
# check for added tasks
if addedTask(cycle):
task = getAddedTask(cycle)
# regular schedule
else:
task = read_s(self.ff)
# print "***", task
# check for eliminated tasks
if elimTask(cycle):
task = getElimTask(task, cycle)
# print '---', task
if task[0] == "#":
if int(task[1]) > cycle:
break
if task[0] == "0":
break
# if task[0] is all or an agent type
if check(task[0], self.types, address.operatingSets):
# keep safe the original list
localList = []
for ag in address.agentList:
if task[0] == "all":
localList.append(ag)
elif task[0] == ag.getAgentType():
localList.append(ag)
# never in the same order (please comment if you want to keep
# always the same sequence
random.shuffle(localList)
# apply method only to a part of the list, or, which is the
# same, with the given probability to each element of the
# list
self.share = np.nan
try:
self.share = float(task[1]) # does task[1] contains
# an int or float number?
except BaseException:
pass
if self.share >= 0:
tmpList = localList[:]
del localList[:]
for i in range(len(tmpList)):
if random.random() < self.share: # < to avoid the
# extreme case if
# self.share is 0
localList.append(tmpList[i])
# in case, an abs. number of agent *(-1)
if self.share < 0:
tmpList = localList[:]
del localList[:]
for i in range(int(-self.share)):
random.shuffle(tmpList)
if tmpList != []:
localList.append(tmpList.pop(0))
# apply
if len(localList) > 0:
self.applyFromSchedule(localList, task)
# if task[0] is an opSet
if task[0] in address.operatingSets:
# keep safe the original list
localList = []
for ag in address.agentList:
if task[0] in ag.getOperatingSetList():
localList.append(ag)
if localList == []:
print("Warning, no agents in operating set", task[0])
# never in the same order (please comment if you want to keep
# always the same sequence
random.shuffle(localList)
# apply method only to a share of the list
self.share = np.nan
try:
self.share = float(task[1]) # does task[1] contains
# an int or float number?
except BaseException:
pass
if self.share >= 0:
tmpList = localList[:]
del localList[:]
for i in range(len(tmpList)):
if random.random() < self.share: # < to avoid the
# extreme case if
# sef.share is 0
localList.append(tmpList[i])
# in case, an abs. number of agent *(-1)
if self.share < 0:
tmpList = localList[:]
# print "*********************", tmpList
del localList[:]
for i in range(int(-self.share)):
random.shuffle(tmpList)
if tmpList != []:
localList.append(tmpList.pop(0))
# print "*********************", localList
# apply
if len(localList) > 0:
self.applyFromSchedule(localList, task)
if task[0] == 'WorldState':
# self.share=np.nan
if address.worldState == 0:
print(
"WorldState.py is missing, you cannot use WorldState here.")
os.sys.exit(1)
# apply from schedule works on a list
localList = [address.worldState]
self.applyFromSchedule(localList, task)
self.actionGroup100.do = do100 # do is a variable linking a method
# run a step
def step(self, cycle):
global task
step = self.actionList[:]
while len(step) > 0:
subStep = extractASubStep(step)
# print "*****************", subStep
found = False
if subStep == "reset":
found = True
self.actionGroup0.do(self)
if subStep == "move":
found = True
self.actionGroup1.do(self)
# self here is the model env.
# not added automatically
# being do a variable
# external schedule, in pos. -1
if subStep == "read_script":
found = True
if self.ff == "":
# create the dictionary of method probabilities. if any
try:
schedule = open(project + "/schedule.txt", "r")
common.methodProbs = {}
for line in schedule:
lineSplit = line.split()
if len(lineSplit) == 3 and lineSplit[1].find(
'.') >= 0:
if lineSplit[2] not in common.methodProbs:
common.methodProbs[lineSplit[2]] = []
if not float(lineSplit[1]) in \
common.methodProbs[lineSplit[2]] and \
not float(lineSplit[1]) < 0:
common.methodProbs[lineSplit[2]].\
append(float(lineSplit[1]))
if common.methodProbs != {}:
print("methodProbabilities =", common.methodProbs)
schedule.close()
except BaseException:
pass
try:
self.ff = open(project + "/schedule.txt", "r")
except BaseException:
pass
self.actionGroup100.do(self, cycle)
# self here is the model env.
# not added automatically
# being do a variable
# other steps
if not found:
found = otherSubSteps(subStep, self)
if not found:
print("Warning: step %s not found in Model" % subStep)
# from external schedule (script)
def applyFromSchedule(self, localList, task):
if task[0] == 'WorldState':
# computational use
# localList contains a unique worldState instance
if task[1] == "computationalUse" or task[1] == "specialUse":
# localList contains a unique worldState instance
if common.debug:
exec("localList[0]." + task[2] + "()")
else:
try:
exec("localList[0]." + task[2] + "()")
except BaseException:
print(task[2], "undefined in WorldState")
# using WorldState to set/get values
else:
try:
# does task[1] contains a number?
# # a number?
aValue = float(task[1])
except BaseException:
print("After WorldState, in schedule.xls we wait: " +
"'computationalUse' or 'specialUse' or " +
"a number.", task[1], "found.")
os.sys.exit(1)
if task[2] != "":
d = {}
d[task[2]] = float(task[1])
# localList contains a unique worldState instance
if common.debug:
exec("localList[0]." + task[2] + "(**d)")
else:
try:
exec("localList[0]." + task[2] + "(**d)")
except BaseException:
print(task[2], "undefined in WorldState")
else:
print("Unable to handle a missing task in WorldState schedule.")
os.sys.exit(1)
# if task[0] is 'all' or a type of agent
if check(task[0], self.types, self.operatingSets):
#https://stackoverflow.com/questions/1565164/what-
#is-the-rationale-for-all-comparisons-returning-false-
#for-ieee754-nan-values
if id(self.share) != id(np.nan):
# NOTE *** the ask with Agent.method in the form
# "askEachAgentInCollection(localList,Agent"+"."+task[2]+")" is
# still operating for old calls in mActions.py modules but it is
# deprecated here, confusing the execution in presence of
# subclasses of Agent
if common.debug:
exec("askEachAgentInCollection(localList,task[2])")
else:
try:
exec("askEachAgentInCollection(localList,task[2])")
except SystemExit:
print('method', task[2],'raising an exit condition')
os.sys.exit(1)
except BaseException:
print("Warning, method", task[2], "does not exist")
else:
# see NOTE *** above
if common.debug:
exec("askEachAgentInCollection(localList,task[1])")
else:
try:
exec("askEachAgentInCollection(localList,task[1])")
except SystemExit:
print('method', task[1],'raising an exit condition')
os.sys.exit(1)
except BaseException:
print("Warning, method", task[1], "does not exist")
# if task[0] is an opSet
if task[0] in self.operatingSets:
if id(self.share) != id(np.nan):
# see NOTE *** above
if common.debug:
exec("askEachAgentInCollection(localList,task[2])")
else:
try:
exec("askEachAgentInCollection(localList,task[2])")
except SystemExit:
print('method', task[2],'raising an exit condition')
os.sys.exit(1)
except BaseException:
print("Warning, method", task[2], "does not exist")
else:
# see NOTE *** above
if common.debug:
exec("askEachAgentInCollection(localList,task[1])")
else:
try:
exec("askEachAgentInCollection(localList,task[1])")
except SystemExit:
print('method', task[1],'raising an exit condition')
os.sys.exit(1)
except BaseException:
print("Warning, method", task[1], "does not exist")
# agent list
def getAgentList(self):
return self.agentList
# file address
def getFile(self):
return self.ff
# tools, read_s
def read_s(f):
if f != "":
try:
task = f.readline()
if task == '':
task = '0 0'
except BaseException:
task = '0 0'
else:
task = '0 0'
return task.split()
# check if it is an agent
def check(s, aList, opSets):
found = False
if s.find("all") == 0:
found = True
if s.find("bland") == 0:
found = True
for name in aList:
if s.find(name) == 0:
found = True
# agent not found (maybe 'dummy' has been set as a fictitious
# agent name, to eliminate a task due to the action made by
# an agent)
if found and not s not in opSets and s != '#' and not s.find(
"WorldState") == 0:
print("agent", s, 'does not exist')
return found
# look for tasks to be added
def addedTask(t):
if t in common.addTasks:
if common.addTasks[t] == []:
return False
else:
return True
else:
return False
# find tasks to be added
def getAddedTask(t):
# returning the first item and removing it
return common.addTasks[t].pop(0).split()
# look for tasks to be eliminated
def elimTask(t):
if t in common.elimTasks:
if common.elimTasks[t] == []:
return False
else:
return True
else:
return False
# find tasks to be Eliminated
def getElimTask(task, t):
found = False
i = -1
for killTask in common.elimTasks[t]:
i += 1
if task[0] == killTask.split()[0] and \
task[1] == killTask.split()[1]:
if len(killTask.split()) == 3:
if task[2] == killTask.split()[2]:
found = True
else:
found = True
if found:
common.elimTasks[t].pop(i)
print(task[0], 'modified to dummy in:', task)
task[0] = 'dummy'
return task