-
Notifications
You must be signed in to change notification settings - Fork 5
/
ObserverSwarm.py
executable file
·174 lines (135 loc) · 5.41 KB
/
ObserverSwarm.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
# ObserverSwarm.py
from ModelSwarm import *
from Tools import *
from Agent import *
from ActionGroup import *
from oActions import *
try:
import tkinter # this is only a control, if tkinter is missing, we cannot
# import Pen
from Pen import *
except BaseException:
print("Warning, missing tkinter: Turtle class will not work")
import penPosition
try:
import graphicDisplayGlobalVarAndFunctions as gvf
except BaseException:
pass
from parameters import *
import os
common.cycle = 1
class ObserverSwarm:
# creation step
def __init__(self, project0):
global project
project = project0
self.v_ = []
self.n_ = []
penPosition.setPen(self)
# create objects
def buildObjects(self):
#global cycle
common.cycle = 1
loadParameters(self) # a function, to which we pass self
self.conclude = False
self.modelSwarm = ModelSwarm(self.nAgents,
self.worldXSize, self.worldYSize, project)
self.modelSwarm.buildObjects()
# actions
def buildActions(self):
global project
print("#### Project", project, "starting.")
print()
observerActions = open(project + "/observerActions.txt")
self.actionList = observerActions.read().split()
# print self.actionList
observerActions.close()
self.modelSwarm.buildActions()
# clock, do not remove or move from here; the position in
# list can be also different
self.actionGroup1 = ActionGroup("clock")
def do1(address, nCycles, actionList):
#global cycle
common.cycle += 1 # the clock running
print("Time =%2d" % common.cycle)
if common.cycle > nCycles:
insertElementNextPosition(actionList, "end")
self.actionGroup1.do = do1 # do is a variable linking a method
self.actionGroup1b = ActionGroup("visualizeNet")
self.actionGroup1b.do = do1b # do is a variable linking a method
self.actionGroup2a = ActionGroup("ask_all")
self.actionGroup2a.do = do2a # do is a variable linking a method
self.actionGroup2b = ActionGroup("ask_one")
self.actionGroup2b.do = do2b # do is a variable linking a method
# previous steps, if empty, are defined in the specific
# oAction.py file
# do the same for new or different steps
# do not remove or move from here and do not rename
self.actionGroup3 = ActionGroup("end")
def do3(address):
self.conclude = True
self.actionGroup3.do = do3 # do is a variable linking a method
# run
def run(self):
#global cycle
print("Time =%2d" % common.cycle)
if self.nCycles == 0:
print("The # of required cycles is 0. ")
while not self.conclude and self.nCycles > 0:
localEventList = self.actionList[:]
while len(localEventList) > 0 and not self.conclude:
subStep = extractASubStep(localEventList)
found = False
if subStep == "modelStep":
found = True
self.modelSwarm.step(common.cycle)
# in a reasonable way, the model makes a step before
# that the observer looks at the effects of model actions
if subStep == "clock":
found = True
self.actionGroup1.do(self, self.nCycles, localEventList)
# self here is the model env.
# not added automatically
# being do a variable
if subStep == "visualizeNet":
found = True
self.actionGroup1b.do(self)
if subStep == "ask_all":
found = True
self.actionGroup2a.do(self, common.cycle)
if subStep == "ask_one":
found = True
self.actionGroup2b.do(self, common.cycle)
# other steps
if not found:
found = otherSubSteps(subStep, self)
if subStep == "specialAction":
found=True
#print "**",common.specialAction
try:
tmp=common.specialAction
except: tmp=""
#print "***",tmp
exec(tmp)
if subStep == "end":
found = True
self.actionGroup3.do(self)
input("enter to conclude")
# look at graphicDisplayGlobalVarAndFunctions.py (if it
# exists) in the folder of the project
try:
gvf.closeNetworkXdisplay()
except BaseException:
pass
# run what left to be executed by the 'end' substep (if any)
# in oActions.py
try:
common.toBeExecuted
tmp = common.toBeExecuted
except BaseException:
tmp = ""
exec(tmp)
if not found:
print("Warning: step %s not found in Observer" % subStep)
if self.modelSwarm.getFile() != "":
self.modelSwarm.getFile().close()