@@ -133,3 +133,58 @@ def step(self):
133133 agent .advance (self .model )
134134 self .steps += 1
135135 self .time += 1
136+
137+
138+ class StagedActivation (BaseScheduler ):
139+ '''
140+ A scheduler which allows agent activation to be divided into several stages
141+ instead of a single `step` method. All agents execute one stage before
142+ moving on to the next.
143+
144+ Agents must have all the stage methods implemented. Stage methods take a
145+ model object as their only argument.
146+
147+ This schedule tracks steps and time separately. Time advances in fractional
148+ increments of 1 / (# of stages), meaning that 1 step = 1 unit of time.
149+ '''
150+
151+ stage_list = []
152+ shuffle = False
153+ shuffle_between_stages = False
154+ stage_time = 1
155+
156+ def __init__ (self , model , stage_list = ["step" ], shuffle = False ,
157+ shuffle_between_stages = False ):
158+ '''
159+ Create an empty Staged Activation schedule.
160+
161+ Args:
162+ model: Model object associated with the schedule.
163+ stage_list: List of strings of names of stages to run, in the
164+ order to run them in.
165+ shuffle: If True, shuffle the order of agents each step.
166+ shuffle_between_stages: If True, shuffle the agents after each
167+ stage; otherwise, only shuffle at the start
168+ of each step.
169+ '''
170+ super ().__init__ (model )
171+ self .stage_list = stage_list
172+ self .shuffle = shuffle
173+ self .shuffle_between_stages = shuffle_between_stages
174+ self .stage_time = 1 / len (self .stage_list )
175+
176+ def step (self ):
177+ '''
178+ Executes all the stages of all agents.
179+ '''
180+
181+ if self .shuffle :
182+ random .shuffle (self .agents )
183+ for stage in self .stage_list :
184+ for agent in self .agents :
185+ getattr (agent , stage )(self .model ) # Run stage
186+ if self .shuffle_between_stages :
187+ random .shuffle (self .agents )
188+ self .time += self .stage_time
189+
190+ self .steps += 1
0 commit comments