Skip to content

Commit 84fd5e0

Browse files
author
Nathan Miller
committedJul 21, 2016
Have Agent use self.model instead of passing it around
1 parent eb73faa commit 84fd5e0

File tree

24 files changed

+233
-238
lines changed

24 files changed

+233
-238
lines changed
 

‎docs/tutorials/intro_tutorial.ipynb

+43-43
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@
104104
"\n",
105105
"class MoneyAgent(Agent):\n",
106106
" \"\"\"An agent with fixed initial wealth.\"\"\"\n",
107-
" def __init__(self, unique_id):\n",
108-
" self.unique_id = unique_id\n",
107+
" def __init__(self, unique_id, model):\n",
108+
" super().__init__(unique_id, model)\n",
109109
" self.wealth = 1\n",
110110
"\n",
111111
"class MoneyModel(Model):\n",
@@ -114,7 +114,7 @@
114114
" self.num_agents = N\n",
115115
" # Create agents\n",
116116
" for i in range(self.num_agents):\n",
117-
" a = MoneyAgent(i)"
117+
" a = MoneyAgent(i, self)"
118118
]
119119
},
120120
{
@@ -145,11 +145,11 @@
145145
"\n",
146146
"class MoneyAgent(Agent):\n",
147147
" \"\"\" An agent with fixed initial wealth.\"\"\"\n",
148-
" def __init__(self, unique_id):\n",
149-
" self.unique_id = unique_id\n",
148+
" def __init__(self, unique_id, model):\n",
149+
" super().__init__(unique_id, model)\n",
150150
" self.wealth = 1\n",
151151
"\n",
152-
" def step(self, model):\n",
152+
" def step(self):\n",
153153
" # The agent's step will go here.\n",
154154
" pass\n",
155155
"\n",
@@ -160,7 +160,7 @@
160160
" self.schedule = RandomActivation(self)\n",
161161
" # Create agents\n",
162162
" for i in range(self.num_agents):\n",
163-
" a = MoneyAgent(i)\n",
163+
" a = MoneyAgent(i, self)\n",
164164
" self.schedule.add(a)\n",
165165
"\n",
166166
" def step(self):\n",
@@ -242,14 +242,14 @@
242242
"source": [
243243
"class MoneyAgent(Agent):\n",
244244
" \"\"\" An agent with fixed initial wealth.\"\"\"\n",
245-
" def __init__(self, unique_id):\n",
246-
" self.unique_id = unique_id\n",
245+
" def __init__(self, unique_id, model):\n",
246+
" super().__init__(unique_id, model)\n",
247247
" self.wealth = 1\n",
248248
"\n",
249-
" def step(self, model):\n",
249+
" def step(self):\n",
250250
" if self.wealth == 0:\n",
251251
" return\n",
252-
" other_agent = random.choice(model.schedule.agents)\n",
252+
" other_agent = random.choice(self.model.schedule.agents)\n",
253253
" other_agent.wealth += 1\n",
254254
" self.wealth -= 1"
255255
]
@@ -449,7 +449,7 @@
449449
" \n",
450450
" # Create agents\n",
451451
" for i in range(self.num_agents):\n",
452-
" a = MoneyAgent(i)\n",
452+
" a = MoneyAgent(i, self)\n",
453453
" self.schedule.add(a)\n",
454454
" \n",
455455
" # Add the agent to a random grid cell\n",
@@ -483,22 +483,22 @@
483483
"```python\n",
484484
"class MoneyAgent(Agent):\n",
485485
" #...\n",
486-
" def move(self, model):\n",
487-
" possible_steps = model.grid.get_neighborhood(\n",
486+
" def move(self):\n",
487+
" possible_steps = self.model.grid.get_neighborhood(\n",
488488
" self.pos, \n",
489489
" moore=True,\n",
490490
" include_center=False)\n",
491491
" new_position = random.choice(possible_steps)\n",
492-
" model.grid.move_agent(self, new_position)\n",
492+
" self.model.grid.move_agent(self, new_position)\n",
493493
"```\n",
494494
"\n",
495495
"Next, we need to get all the other agents present in a cell, and give one of them some money. We can get the contents of one or more cells using the grid's `get_cell_list_contents` method, or by accessing a cell directly. The method accepts a list of cell coordinate tuples, or a single tuple if we only care about one cell.\n",
496496
"\n",
497497
"```python\n",
498498
"class MoneyAgent(Agent):\n",
499499
" #...\n",
500-
" def give_money(self, model):\n",
501-
" cellmates = model.grid.get_cell_list_contents([self.pos])\n",
500+
" def give_money(self):\n",
501+
" cellmates = self.model.grid.get_cell_list_contents([self.pos])\n",
502502
" if len(cellmates) > 1:\n",
503503
" other = random.choice(cellmates)\n",
504504
" other.wealth += 1\n",
@@ -510,10 +510,10 @@
510510
"```python\n",
511511
"class MoneyAgent(Agent):\n",
512512
" # ...\n",
513-
" def step(self, model):\n",
514-
" self.move(model)\n",
513+
" def step(self):\n",
514+
" self.move()\n",
515515
" if self.wealth > 0:\n",
516-
" self.give_money(model)\n",
516+
" self.give_money()\n",
517517
"```\n",
518518
"\n",
519519
"Now, putting that all together should look like this:"
@@ -535,7 +535,7 @@
535535
" self.schedule = RandomActivation(self)\n",
536536
" # Create agents\n",
537537
" for i in range(self.num_agents):\n",
538-
" a = MoneyAgent(i)\n",
538+
" a = MoneyAgent(i, self)\n",
539539
" self.schedule.add(a)\n",
540540
" # Add the agent to a random grid cell\n",
541541
" x = random.randrange(self.grid.width)\n",
@@ -547,29 +547,29 @@
547547
"\n",
548548
"class MoneyAgent(Agent):\n",
549549
" \"\"\" An agent with fixed initial wealth.\"\"\"\n",
550-
" def __init__(self, unique_id):\n",
551-
" self.unique_id = unique_id\n",
550+
" def __init__(self, unique_id, model):\n",
551+
" super().__init__(unique_id, model)\n",
552552
" self.wealth = 1\n",
553553
"\n",
554-
" def move(self, model):\n",
555-
" possible_steps = model.grid.get_neighborhood(\n",
554+
" def move(self):\n",
555+
" possible_steps = self.model.grid.get_neighborhood(\n",
556556
" self.pos, \n",
557557
" moore=True, \n",
558558
" include_center=False)\n",
559559
" new_position = random.choice(possible_steps)\n",
560-
" model.grid.move_agent(self, new_position)\n",
560+
" self.model.grid.move_agent(self, new_position)\n",
561561
"\n",
562-
" def give_money(self, model):\n",
563-
" cellmates = model.grid.get_cell_list_contents([self.pos])\n",
562+
" def give_money(self):\n",
563+
" cellmates = self.model.grid.get_cell_list_contents([self.pos])\n",
564564
" if len(cellmates) > 1:\n",
565565
" other = random.choice(cellmates)\n",
566566
" other.wealth += 1\n",
567567
" self.wealth -= 1\n",
568568
"\n",
569-
" def step(self, model):\n",
570-
" self.move(model)\n",
569+
" def step(self):\n",
570+
" self.move()\n",
571571
" if self.wealth > 0:\n",
572-
" self.give_money(model)"
572+
" self.give_money()"
573573
]
574574
},
575575
{
@@ -676,29 +676,29 @@
676676
"\n",
677677
"class MoneyAgent(Agent):\n",
678678
" \"\"\" An agent with fixed initial wealth.\"\"\"\n",
679-
" def __init__(self, unique_id):\n",
680-
" self.unique_id = unique_id\n",
679+
" def __init__(self, unique_id, model):\n",
680+
" super().__init__(unique_id, model)\n",
681681
" self.wealth = 1\n",
682682
"\n",
683-
" def move(self, model):\n",
684-
" possible_steps = model.grid.get_neighborhood(\n",
683+
" def move(self):\n",
684+
" possible_steps = self.model.grid.get_neighborhood(\n",
685685
" self.pos, \n",
686686
" moore=True, \n",
687687
" include_center=False)\n",
688688
" new_position = random.choice(possible_steps)\n",
689-
" model.grid.move_agent(self, new_position)\n",
689+
" self.model.grid.move_agent(self, new_position)\n",
690690
"\n",
691-
" def give_money(self, model):\n",
692-
" cellmates = model.grid.get_cell_list_contents([self.pos])\n",
691+
" def give_money(self):\n",
692+
" cellmates = self.model.grid.get_cell_list_contents([self.pos])\n",
693693
" if len(cellmates) > 1:\n",
694694
" other = random.choice(cellmates)\n",
695695
" other.wealth += 1\n",
696696
" self.wealth -= 1\n",
697697
"\n",
698-
" def step(self, model):\n",
699-
" self.move(model)\n",
698+
" def step(self):\n",
699+
" self.move()\n",
700700
" if self.wealth > 0:\n",
701-
" self.give_money(model)\n",
701+
" self.give_money()\n",
702702
"\n",
703703
"class MoneyModel(Model):\n",
704704
" \"\"\"A model with some number of agents.\"\"\"\n",
@@ -709,7 +709,7 @@
709709
" \n",
710710
" # Create agents\n",
711711
" for i in range(self.num_agents):\n",
712-
" a = MoneyAgent(i)\n",
712+
" a = MoneyAgent(i, self)\n",
713713
" self.schedule.add(a)\n",
714714
" # Add the agent to a random grid cell\n",
715715
" x = random.randrange(self.grid.width)\n",
@@ -989,7 +989,7 @@
989989
" \n",
990990
" # Create agents\n",
991991
" for i in range(self.num_agents):\n",
992-
" a = MoneyAgent(i)\n",
992+
" a = MoneyAgent(i, self)\n",
993993
" self.schedule.add(a)\n",
994994
" # Add the agent to a random grid cell\n",
995995
" x = random.randrange(self.grid.width)\n",

‎docs/tutorials/intro_tutorial.rst

+43-43
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ The beginning of both classes looks like this:
116116
117117
class MoneyAgent(Agent):
118118
"""An agent with fixed initial wealth."""
119-
def __init__(self, unique_id):
120-
self.unique_id = unique_id
119+
def __init__(self, unique_id, model):
120+
super().__init__(unique_id, model)
121121
self.wealth = 1
122122
123123
class MoneyModel(Model):
@@ -126,7 +126,7 @@ The beginning of both classes looks like this:
126126
self.num_agents = N
127127
# Create agents
128128
for i in range(self.num_agents):
129-
a = MoneyAgent(i)
129+
a = MoneyAgent(i, self)
130130
131131
Adding the scheduler
132132
~~~~~~~~~~~~~~~~~~~~
@@ -164,11 +164,11 @@ this:
164164
165165
class MoneyAgent(Agent):
166166
""" An agent with fixed initial wealth."""
167-
def __init__(self, unique_id):
168-
self.unique_id = unique_id
167+
def __init__(self, unique_id, model):
168+
super().__init__(unique_id, model)
169169
self.wealth = 1
170170
171-
def step(self, model):
171+
def step(self):
172172
# The agent's step will go here.
173173
pass
174174
@@ -179,7 +179,7 @@ this:
179179
self.schedule = RandomActivation(self)
180180
# Create agents
181181
for i in range(self.num_agents):
182-
a = MoneyAgent(i)
182+
a = MoneyAgent(i, self)
183183
self.schedule.add(a)
184184
185185
def step(self):
@@ -234,14 +234,14 @@ With that in mind, we rewrite the agent's ``step`` method, like this:
234234
235235
class MoneyAgent(Agent):
236236
""" An agent with fixed initial wealth."""
237-
def __init__(self, unique_id):
238-
self.unique_id = unique_id
237+
def __init__(self, unique_id, model):
238+
super().__init__(unique_id, model)
239239
self.wealth = 1
240240
241-
def step(self, model):
241+
def step(self):
242242
if self.wealth == 0:
243243
return
244-
other_agent = random.choice(model.schedule.agents)
244+
other_agent = random.choice(self.model.schedule.agents)
245245
other_agent.wealth += 1
246246
self.wealth -= 1
247247
@@ -404,7 +404,7 @@ coordinates to place the agent.
404404
405405
# Create agents
406406
for i in range(self.num_agents):
407-
a = MoneyAgent(i)
407+
a = MoneyAgent(i, self)
408408
self.schedule.add(a)
409409
410410
# Add the agent to a random grid cell
@@ -448,13 +448,13 @@ With that in mind, the agent's ``move`` method looks like this:
448448
449449
class MoneyAgent(Agent):
450450
#...
451-
def move(self, model):
452-
possible_steps = model.grid.get_neighborhood(
451+
def move(self):
452+
possible_steps = self.model.grid.get_neighborhood(
453453
self.pos,
454454
moore=True,
455455
include_center=False)
456456
new_position = random.choice(possible_steps)
457-
model.grid.move_agent(self, new_position)
457+
self.model.grid.move_agent(self, new_position)
458458
459459
Next, we need to get all the other agents present in a cell, and give
460460
one of them some money. We can get the contents of one or more cells
@@ -466,8 +466,8 @@ single tuple if we only care about one cell.
466466
467467
class MoneyAgent(Agent):
468468
#...
469-
def give_money(self, model):
470-
cellmates = model.grid.get_cell_list_contents([self.pos])
469+
def give_money(self):
470+
cellmates = self.model.grid.get_cell_list_contents([self.pos])
471471
if len(cellmates) > 1:
472472
other = random.choice(cellmates)
473473
other.wealth += 1
@@ -479,10 +479,10 @@ And with those two methods, the agent's ``step`` method becomes:
479479
480480
class MoneyAgent(Agent):
481481
# ...
482-
def step(self, model):
483-
self.move(model)
482+
def step(self):
483+
self.move()
484484
if self.wealth > 0:
485-
self.give_money(model)
485+
self.give_money()
486486
487487
Now, putting that all together should look like this:
488488

@@ -496,7 +496,7 @@ Now, putting that all together should look like this:
496496
self.schedule = RandomActivation(self)
497497
# Create agents
498498
for i in range(self.num_agents):
499-
a = MoneyAgent(i)
499+
a = MoneyAgent(i, self)
500500
self.schedule.add(a)
501501
# Add the agent to a random grid cell
502502
x = random.randrange(self.grid.width)
@@ -508,29 +508,29 @@ Now, putting that all together should look like this:
508508
509509
class MoneyAgent(Agent):
510510
""" An agent with fixed initial wealth."""
511-
def __init__(self, unique_id):
512-
self.unique_id = unique_id
511+
def __init__(self, unique_id, model):
512+
super().__init__(unique_id, model)
513513
self.wealth = 1
514514
515-
def move(self, model):
516-
possible_steps = model.grid.get_neighborhood(
515+
def move(self):
516+
possible_steps = self.model.grid.get_neighborhood(
517517
self.pos,
518518
moore=True,
519519
include_center=False)
520520
new_position = random.choice(possible_steps)
521-
model.grid.move_agent(self, new_position)
521+
self.model.grid.move_agent(self, new_position)
522522
523-
def give_money(self, model):
524-
cellmates = model.grid.get_cell_list_contents([self.pos])
523+
def give_money(self):
524+
cellmates = self.model.grid.get_cell_list_contents([self.pos])
525525
if len(cellmates) > 1:
526526
other = random.choice(cellmates)
527527
other.wealth += 1
528528
self.wealth -= 1
529529
530-
def step(self, model):
531-
self.move(model)
530+
def step(self):
531+
self.move()
532532
if self.wealth > 0:
533-
self.give_money(model)
533+
self.give_money()
534534
535535
Let's create a model with 50 agents on a 10x10 grid, and run it for 20
536536
steps.
@@ -622,29 +622,29 @@ measure of wealth inequality.
622622
623623
class MoneyAgent(Agent):
624624
""" An agent with fixed initial wealth."""
625-
def __init__(self, unique_id):
626-
self.unique_id = unique_id
625+
def __init__(self, unique_id, model):
626+
super().__init__(unique_id, model)
627627
self.wealth = 1
628628
629-
def move(self, model):
630-
possible_steps = model.grid.get_neighborhood(
629+
def move(self):
630+
possible_steps = self.model.grid.get_neighborhood(
631631
self.pos,
632632
moore=True,
633633
include_center=False)
634634
new_position = random.choice(possible_steps)
635-
model.grid.move_agent(self, new_position)
635+
self.model.grid.move_agent(self, new_position)
636636
637-
def give_money(self, model):
638-
cellmates = model.grid.get_cell_list_contents([self.pos])
637+
def give_money(self):
638+
cellmates = self.model.grid.get_cell_list_contents([self.pos])
639639
if len(cellmates) > 1:
640640
other = random.choice(cellmates)
641641
other.wealth += 1
642642
self.wealth -= 1
643643
644-
def step(self, model):
645-
self.move(model)
644+
def step(self):
645+
self.move()
646646
if self.wealth > 0:
647-
self.give_money(model)
647+
self.give_money()
648648
649649
class MoneyModel(Model):
650650
"""A model with some number of agents."""
@@ -655,7 +655,7 @@ measure of wealth inequality.
655655
656656
# Create agents
657657
for i in range(self.num_agents):
658-
a = MoneyAgent(i)
658+
a = MoneyAgent(i, self)
659659
self.schedule.add(a)
660660
# Add the agent to a random grid cell
661661
x = random.randrange(self.grid.width)
@@ -832,7 +832,7 @@ indefinitely.
832832
833833
# Create agents
834834
for i in range(self.num_agents):
835-
a = MoneyAgent(i)
835+
a = MoneyAgent(i, self)
836836
self.schedule.add(a)
837837
# Add the agent to a random grid cell
838838
x = random.randrange(self.grid.width)

‎examples/Basic/basic/model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77

88
class Walker(Agent):
9-
def __init__(self, unique_id, pos, heading=(1, 0)):
10-
self.unique_id = unique_id
9+
def __init__(self, unique_id, model, pos, heading=(1, 0)):
10+
super().__init__(unique_id, model)
1111
self.pos = pos
1212
self.heading = heading
1313
self.headings = {(1, 0), (0, 1), (-1, 0), (0, -1)}
@@ -35,7 +35,7 @@ def make_walker_agents(self):
3535
if self.grid.is_cell_empty(pos):
3636
print("Creating agent {2} at ({0}, {1})"
3737
.format(x, y, unique_id))
38-
a = Walker(unique_id, pos, heading)
38+
a = Walker(unique_id, self, pos, heading)
3939
self.schedule.add(a)
4040
self.grid.place_agent(a, pos)
4141
unique_id += 1

‎examples/Boltzmann_Wealth_Model/MoneyModel.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, N, width, height):
2828
)
2929
# Create agents
3030
for i in range(self.num_agents):
31-
a = MoneyAgent(i)
31+
a = MoneyAgent(i, self)
3232
self.schedule.add(a)
3333
# Add the agent to a random grid cell
3434
x = random.randrange(self.grid.width)
@@ -46,26 +46,25 @@ def run_model(self, n):
4646

4747
class MoneyAgent(Agent):
4848
""" An agent with fixed initial wealth."""
49-
50-
def __init__(self, unique_id):
51-
self.unique_id = unique_id
49+
def __init__(self, unique_id, model):
50+
super().__init__(unique_id, model)
5251
self.wealth = 1
5352

54-
def move(self, model):
55-
possible_steps = model.grid.get_neighborhood(
53+
def move(self):
54+
possible_steps = self.model.grid.get_neighborhood(
5655
self.pos, moore=True, include_center=False
5756
)
5857
new_position = random.choice(possible_steps)
59-
model.grid.move_agent(self, new_position)
58+
self.model.grid.move_agent(self, new_position)
6059

61-
def give_money(self, model):
62-
cellmates = model.grid.get_cell_list_contents([self.pos])
60+
def give_money(self):
61+
cellmates = self.model.grid.get_cell_list_contents([self.pos])
6362
if len(cellmates) > 1:
6463
other = random.choice(cellmates)
6564
other.wealth += 1
6665
self.wealth -= 1
6766

68-
def step(self, model):
69-
self.move(model)
67+
def step(self):
68+
self.move()
7069
if self.wealth > 0:
71-
self.give_money(model)
70+
self.give_money()

‎examples/ColorPatches/color_patches/model.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, pos, model, initial_state):
2222
'''
2323
Create a cell, in the given state, at the given row, col position.
2424
'''
25-
Agent.__init__(self, pos, model)
25+
super().__init__(pos, model)
2626
self._row = pos[0]
2727
self._col = pos[1]
2828
self._state = initial_state
@@ -40,14 +40,14 @@ def get_state(self):
4040
'''Return the current state (OPINION) of this cell.'''
4141
return self._state
4242

43-
def step(self, model):
43+
def step(self):
4444
'''
4545
Determines the agent opinion for the next step by polling its neighbors
4646
The opinion is determined by the majority of the 8 neighbors' opinion
4747
A choice is made at random in case of a tie
4848
The next state is stored until all cells have been polled
4949
'''
50-
neighbor_iter_ = model.grid.neighbor_iter((self._row, self._col), True)
50+
neighbor_iter_ = self.model.grid.neighbor_iter((self._row, self._col), True)
5151
neighbors_opinion = Counter(n.get_state() for n in neighbor_iter_)
5252
# Following is a a tuple (attribute, occurrences)
5353
polled_opinions = neighbors_opinion.most_common()
@@ -58,8 +58,7 @@ def step(self, model):
5858

5959
self._next_state = random.choice(tied_opinions)[0]
6060

61-
# model argument is unused
62-
def advance(self, model):
61+
def advance(self):
6362
'''
6463
Set the state of the agent to the next state
6564
'''

‎examples/ConwaysGameOfLife/game_of_life/cell.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, pos, model, init_state=DEAD):
1111
'''
1212
Create a cell, in the given state, at the given x, y position.
1313
'''
14-
Agent.__init__(self, pos, model)
14+
super().__init__(pos, model)
1515
self.x, self.y = pos
1616
self.state = init_state
1717
self._nextState = None
@@ -24,7 +24,7 @@ def isAlive(self):
2424
def neighbors(self):
2525
return self.model.grid.neighbor_iter((self.x, self.y), True)
2626

27-
def step(self, model):
27+
def step(self):
2828
'''
2929
Compute if the cell will be dead or alive at the next tick. This is
3030
based on the number of alive or dead neighbors. The state is not
@@ -46,7 +46,7 @@ def step(self, model):
4646
if live_neighbors == 3:
4747
self._nextState = self.ALIVE
4848

49-
def advance(self, model):
49+
def advance(self):
5050
'''
5151
Set the state to the new computed state -- computed in step().
5252
'''

‎examples/EpsteinCivilViolence/civil_violence/agent.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Citizen(Agent):
3030
3131
"""
3232

33-
def __init__(self, unique_id, pos, hardship, regime_legitimacy,
34-
risk_aversion, threshold, vision, model):
33+
def __init__(self, unique_id, model, pos, hardship, regime_legitimacy,
34+
risk_aversion, threshold, vision):
3535
"""
3636
Create a new Citizen.
3737
Args:
@@ -61,37 +61,37 @@ def __init__(self, unique_id, pos, hardship, regime_legitimacy,
6161
self.grievance = self.hardship * (1 - self.regime_legitimacy)
6262
self.arrest_probability = None
6363

64-
def step(self, model):
64+
def step(self):
6565
"""
6666
Decide whether to activate, then move if applicable.
6767
"""
6868
if self.jail_sentence:
6969
self.jail_sentence -= 1
7070
return # no other changes or movements if agent is in jail.
71-
self.update_neighbors(model)
72-
self.update_estimated_arrest_probability(model)
71+
self.update_neighbors()
72+
self.update_estimated_arrest_probability()
7373
net_risk = self.risk_aversion * self.arrest_probability
7474
if self.condition == 'Quiescent' and (
7575
self.grievance - net_risk) > self.threshold:
7676
self.condition = 'Active'
7777
elif self.condition == 'Active' and (
7878
self.grievance - net_risk) <= self.threshold:
7979
self.condition = 'Quiescent'
80-
if model.movement and self.empty_neighbors:
80+
if self.model.movement and self.empty_neighbors:
8181
new_pos = random.choice(self.empty_neighbors)
82-
model.grid.move_agent(self, new_pos)
82+
self.model.grid.move_agent(self, new_pos)
8383

84-
def update_neighbors(self, model):
84+
def update_neighbors(self):
8585
"""
8686
Look around and see who my neighbors are
8787
"""
88-
self.neighborhood = model.grid.get_neighborhood(self.pos,
88+
self.neighborhood = self.model.grid.get_neighborhood(self.pos,
8989
moore=False, radius=1)
90-
self.neighbors = model.grid.get_cell_list_contents(self.neighborhood)
90+
self.neighbors = self.model.grid.get_cell_list_contents(self.neighborhood)
9191
self.empty_neighbors = [c for c in self.neighborhood if
92-
model.grid.is_cell_empty(c)]
92+
self.model.grid.is_cell_empty(c)]
9393

94-
def update_estimated_arrest_probability(self, model):
94+
def update_estimated_arrest_probability(self):
9595
"""
9696
Based on the ratio of cops to actives in my neighborhood, estimate the
9797
p(Arrest | I go active).
@@ -105,7 +105,7 @@ def update_estimated_arrest_probability(self, model):
105105
c.jail_sentence == 0):
106106
actives_in_vision += 1
107107
self.arrest_probability = 1 - math.exp(
108-
-1 * model.arrest_prob_constant * (
108+
-1 * self.model.arrest_prob_constant * (
109109
cops_in_vision / actives_in_vision))
110110

111111

@@ -121,7 +121,7 @@ class Cop(Agent):
121121
able to inspect
122122
"""
123123

124-
def __init__(self, unique_id, pos, vision, model):
124+
def __init__(self, unique_id, model, pos, vision):
125125
"""
126126
Create a new Cop.
127127
Args:
@@ -136,12 +136,12 @@ def __init__(self, unique_id, pos, vision, model):
136136
self.pos = pos
137137
self.vision = vision
138138

139-
def step(self, model):
139+
def step(self):
140140
"""
141141
Inspect local vision and arrest a random active agent. Move if
142142
applicable.
143143
"""
144-
self.update_neighbors(model)
144+
self.update_neighbors()
145145
active_neighbors = []
146146
for agent in self.neighbors:
147147
if agent.breed == 'citizen' and \
@@ -150,18 +150,18 @@ def step(self, model):
150150
active_neighbors.append(agent)
151151
if active_neighbors:
152152
arrestee = random.choice(active_neighbors)
153-
sentence = random.randint(0, model.max_jail_term)
153+
sentence = random.randint(0, self.model.max_jail_term)
154154
arrestee.jail_sentence = sentence
155-
if model.movement and self.empty_neighbors:
155+
if self.model.movement and self.empty_neighbors:
156156
new_pos = random.choice(self.empty_neighbors)
157-
model.grid.move_agent(self, new_pos)
157+
self.model.grid.move_agent(self, new_pos)
158158

159-
def update_neighbors(self, model):
159+
def update_neighbors(self):
160160
"""
161161
Look around and see who my neighbors are.
162162
"""
163-
self.neighborhood = model.grid.get_neighborhood(self.pos,
163+
self.neighborhood = self.model.grid.get_neighborhood(self.pos,
164164
moore=False, radius=1)
165-
self.neighbors = model.grid.get_cell_list_contents(self.neighborhood)
165+
self.neighbors = self.model.grid.get_cell_list_contents(self.neighborhood)
166166
self.empty_neighbors = [c for c in self.neighborhood if
167-
model.grid.is_cell_empty(c)]
167+
self.model.grid.is_cell_empty(c)]

‎examples/EpsteinCivilViolence/civil_violence/model.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,18 @@ def __init__(self, height, width, citizen_density, cop_density,
7777
'Cop density + citizen density must be less than 1')
7878
for (contents, x, y) in self.grid.coord_iter():
7979
if random.random() < self.cop_density:
80-
cop = Cop(unique_id, (x, y), vision=self.cop_vision,
81-
model=self)
80+
cop = Cop(unique_id, self, (x, y), vision=self.cop_vision)
8281
unique_id += 1
8382
self.grid[y][x] = cop
8483
self.schedule.add(cop)
8584
elif random.random() < (
8685
self.cop_density + self.citizen_density):
87-
citizen = Citizen(unique_id, (x, y),
86+
citizen = Citizen(unique_id, self, (x, y),
8887
hardship=random.random(),
8988
regime_legitimacy=self.legitimacy,
9089
risk_aversion=random.random(),
9190
threshold=self.active_threshold,
92-
vision=self.citizen_vision, model=self)
91+
vision=self.citizen_vision)
9392
unique_id += 1
9493
self.grid[y][x] = citizen
9594
self.schedule.add(citizen)

‎examples/Flockers/flockers/boid.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Boid(Agent):
1717
define their movement. Separation is their desired minimum distance from
1818
any other Boid.
1919
'''
20-
def __init__(self, unique_id, pos, speed=5, heading=None,
20+
def __init__(self, unique_id, model, pos, speed=5, heading=None,
2121
vision=5, separation=1):
2222
'''
2323
Create a new Boid flocker agent.
@@ -30,7 +30,7 @@ def __init__(self, unique_id, pos, speed=5, heading=None,
3030
vision: Radius to look around for nearby Boids.
3131
separation: Minimum distance to maintain from other Boids.
3232
'''
33-
self.unique_id = unique_id
33+
super().__init__(unique_id, model)
3434
self.pos = pos
3535
self.speed = speed
3636
if heading is not None:
@@ -72,12 +72,12 @@ def match_heading(self, neighbors):
7272
mean_heading += np.int64(neighbor.heading)
7373
return mean_heading / len(neighbors)
7474

75-
def step(self, model):
75+
def step(self):
7676
'''
7777
Get the Boid's neighbors, compute the new vector, and move accordingly.
7878
'''
7979

80-
neighbors = model.space.get_neighbors(self.pos, self.vision, False)
80+
neighbors = self.model.space.get_neighbors(self.pos, self.vision, False)
8181
if len(neighbors) > 0:
8282
cohere_vector = self.cohere(neighbors)
8383
separate_vector = self.separate(neighbors)
@@ -88,4 +88,4 @@ def step(self, model):
8888
self.heading /= np.linalg.norm(self.heading)
8989
new_pos = np.array(self.pos) + self.heading * self.speed
9090
new_x, new_y = new_pos
91-
model.space.move_agent(self, (new_x, new_y))
91+
self.model.space.move_agent(self, (new_x, new_y))

‎examples/Flockers/flockers/model.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def make_agents(self):
5353
pos = (x, y)
5454
heading = np.random.random(2) * 2 - np.array((1, 1))
5555
heading /= np.linalg.norm(heading)
56-
boid = Boid(i, pos, self.speed, heading,
57-
self.vision, self.separation)
56+
boid = Boid(i, self, pos, self.speed, heading, self.vision,
57+
self.separation)
5858
self.space.place_agent(boid, pos)
5959
self.schedule.add(boid)
6060

‎examples/ForestFire/forest_fire/agent.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ class TreeCell(Agent):
1313
unique_id isn't strictly necessary here, but it's good
1414
practice to give one to each agent anyway.
1515
"""
16-
def __init__(self, pos):
16+
def __init__(self, pos, model):
1717
"""
1818
Create a new tree.
1919
Args:
2020
pos: The tree's coordinates on the grid.
21+
model: standard model reference for agent.
2122
"""
23+
super().__init__(pos, model)
2224
self.pos = pos
23-
self.unique_id = pos
2425
self.condition = "Fine"
2526

26-
def step(self, model):
27+
def step(self):
2728
"""
2829
If the tree is on fire, spread it to fine trees nearby.
2930
"""
3031
if self.condition == "On Fire":
31-
for neighbor in model.grid.neighbor_iter(self.pos):
32+
for neighbor in self.model.grid.neighbor_iter(self.pos):
3233
if neighbor.condition == "Fine":
3334
neighbor.condition = "On Fire"
3435
self.condition = "Burned Out"

‎examples/ForestFire/forest_fire/model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, height, width, density):
3838
for (contents, x, y) in self.grid.coord_iter():
3939
if random.random() < self.density:
4040
# Create a tree
41-
new_tree = TreeCell((x, y))
41+
new_tree = TreeCell((x, y), self)
4242
# Set all trees in the first column on fire.
4343
if x == 0:
4444
new_tree.condition = "On Fire"

‎examples/PD_Grid/pd_grid.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class PD_Agent(Agent):
1919

20-
def __init__(self, pos, starting_move=None):
20+
def __init__(self, pos, model, starting_move=None):
2121
'''
2222
Create a new Prisoner's Dilemma agent.
2323
@@ -26,6 +26,7 @@ def __init__(self, pos, starting_move=None):
2626
starting_move: If provided, determines the agent's initial state:
2727
C(ooperating) or D(efecting). Otherwise, random.
2828
'''
29+
super().__init__(pos, model)
2930
self.pos = pos
3031
self.score = 0
3132
if starting_move:
@@ -34,29 +35,29 @@ def __init__(self, pos, starting_move=None):
3435
self.move = random.choice(["C", "D"])
3536
self.next_move = None
3637

37-
def step(self, model):
38+
def step(self):
3839
'''
3940
Get the neighbors' moves, and change own move accordingly.
4041
'''
41-
neighbors = model.grid.get_neighbors(self.pos, True,
42-
include_center=True)
42+
neighbors = self.model.grid.get_neighbors(self.pos, True,
43+
include_center=True)
4344
best_neighbor = max(neighbors, key=lambda a: a.score)
4445
self.next_move = best_neighbor.move
4546

46-
if model.schedule_type != "Simultaneous":
47-
self.advance(model)
47+
if self.model.schedule_type != "Simultaneous":
48+
self.advance()
4849

49-
def advance(self, model):
50+
def advance(self):
5051
self.move = self.next_move
51-
self.score += self.increment_score(model)
52+
self.score += self.increment_score()
5253

53-
def increment_score(self, model):
54-
neighbors = model.grid.get_neighbors(self.pos, True)
55-
if model.schedule_type == "Simultaneous":
54+
def increment_score(self):
55+
neighbors = self.model.grid.get_neighbors(self.pos, True)
56+
if self.model.schedule_type == "Simultaneous":
5657
moves = [neighbor.next_move for neighbor in neighbors]
5758
else:
5859
moves = [neighbor.move for neighbor in neighbors]
59-
return sum(model.payoff[(self.move, move)] for move in moves)
60+
return sum(self.model.payoff[(self.move, move)] for move in moves)
6061

6162

6263
class PD_Model(Model):
@@ -94,7 +95,7 @@ def __init__(self, height, width, schedule_type, payoffs=None):
9495
# Create agents
9596
for x in range(width):
9697
for y in range(height):
97-
agent = PD_Agent((x, y))
98+
agent = PD_Agent((x, y), self)
9899
self.grid.place_agent(agent, (x, y))
99100
self.schedule.add(agent)
100101

‎examples/Schelling/model.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SchellingAgent(Agent):
1010
'''
1111
Schelling segregation agent
1212
'''
13-
def __init__(self, pos, agent_type):
13+
def __init__(self, pos, model, agent_type):
1414
'''
1515
Create a new Schelling agent.
1616
@@ -19,21 +19,21 @@ def __init__(self, pos, agent_type):
1919
x, y: Agent initial location.
2020
agent_type: Indicator for the agent's type (minority=1, majority=0)
2121
'''
22-
self.unique_id = pos
22+
super().__init__(pos, model)
2323
self.pos = pos
2424
self.type = agent_type
2525

26-
def step(self, model):
26+
def step(self):
2727
similar = 0
28-
for neighbor in model.grid.neighbor_iter(self.pos):
28+
for neighbor in self.model.grid.neighbor_iter(self.pos):
2929
if neighbor.type == self.type:
3030
similar += 1
3131

3232
# If unhappy, move:
33-
if similar < model.homophily:
34-
model.grid.move_to_empty(self)
33+
if similar < self.model.homophily:
34+
self.model.grid.move_to_empty(self)
3535
else:
36-
model.happy += 1
36+
self.model.happy += 1
3737

3838

3939
class SchellingModel(Model):
@@ -75,7 +75,7 @@ def __init__(self, height, width, density, minority_pc, homophily):
7575
else:
7676
agent_type = 0
7777

78-
agent = SchellingAgent((x, y), agent_type)
78+
agent = SchellingAgent((x, y), self, agent_type)
7979
self.grid.position_agent(agent, (x, y))
8080
self.schedule.add(agent)
8181

‎examples/WolfSheep/wolf_sheep/agents.py

+30-29
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,42 @@ class Sheep(RandomWalker):
1414

1515
energy = None
1616

17-
def __init__(self, grid, pos, moore, energy=None):
18-
super().__init__(grid, pos, moore)
17+
def __init__(self, pos, model, moore, energy=None):
18+
super().__init__(pos, model, moore=moore)
1919
self.energy = energy
2020

21-
def step(self, model):
21+
def step(self):
2222
'''
2323
A model step. Move, then eat grass and reproduce.
2424
'''
2525
self.random_move()
2626
living = True
2727

28-
if model.grass:
28+
if self.model.grass:
2929
# Reduce energy
3030
self.energy -= 1
3131

3232
# If there is grass available, eat it
33-
this_cell = model.grid.get_cell_list_contents([self.pos])
33+
this_cell = self.model.grid.get_cell_list_contents([self.pos])
3434
grass_patch = [obj for obj in this_cell
3535
if isinstance(obj, GrassPatch)][0]
3636
if grass_patch.fully_grown:
37-
self.energy += model.sheep_gain_from_food
37+
self.energy += self.model.sheep_gain_from_food
3838
grass_patch.fully_grown = False
3939

4040
# Death
4141
if self.energy < 0:
42-
model.grid._remove_agent(self.pos, self)
43-
model.schedule.remove(self)
42+
self.model.grid._remove_agent(self.pos, self)
43+
self.model.schedule.remove(self)
4444
living = False
4545

46-
if living and random.random() < model.sheep_reproduce:
46+
if living and random.random() < self.model.sheep_reproduce:
4747
# Create a new sheep:
48-
if model.grass:
48+
if self.model.grass:
4949
self.energy /= 2
50-
lamb = Sheep(self.grid, self.pos, self.moore, self.energy)
51-
model.grid.place_agent(lamb, self.pos)
52-
model.schedule.add(lamb)
50+
lamb = Sheep(self.pos, self.model, self.moore, self.energy)
51+
self.model.grid.place_agent(lamb, self.pos)
52+
self.model.schedule.add(lamb)
5353

5454

5555
class Wolf(RandomWalker):
@@ -59,60 +59,61 @@ class Wolf(RandomWalker):
5959

6060
energy = None
6161

62-
def __init__(self, grid, pos, moore, energy):
63-
super().__init__(grid, pos, moore)
62+
def __init__(self, pos, model, moore, energy=None):
63+
super().__init__(pos, model, moore=moore)
6464
self.energy = energy
6565

66-
def step(self, model):
66+
def step(self):
6767
self.random_move()
6868
self.energy -= 1
6969

7070
# If there are sheep present, eat one
7171
x, y = self.pos
72-
this_cell = model.grid.get_cell_list_contents([self.pos])
72+
this_cell = self.model.grid.get_cell_list_contents([self.pos])
7373
sheep = [obj for obj in this_cell if isinstance(obj, Sheep)]
7474
if len(sheep) > 0:
7575
sheep_to_eat = random.choice(sheep)
76-
self.energy += model.wolf_gain_from_food
76+
self.energy += self.model.wolf_gain_from_food
7777

7878
# Kill the sheep
79-
model.grid._remove_agent(self.pos, sheep_to_eat)
80-
model.schedule.remove(sheep_to_eat)
79+
self.model.grid._remove_agent(self.pos, sheep_to_eat)
80+
self.model.schedule.remove(sheep_to_eat)
8181

8282
# Death or reproduction
8383
if self.energy < 0:
84-
model.grid._remove_agent(self.pos, self)
85-
model.schedule.remove(self)
84+
self.model.grid._remove_agent(self.pos, self)
85+
self.model.schedule.remove(self)
8686
else:
87-
if random.random() < model.wolf_reproduce:
87+
if random.random() < self.model.wolf_reproduce:
8888
# Create a new wolf cub
8989
self.energy /= 2
90-
cub = Wolf(self.grid, self.pos, self.moore, self.energy)
91-
model.grid.place_agent(cub, cub.pos)
92-
model.schedule.add(cub)
90+
cub = Wolf(self.pos, self.model, self.moore, self.energy)
91+
self.model.grid.place_agent(cub, cub.pos)
92+
self.model.schedule.add(cub)
9393

9494

9595
class GrassPatch(Agent):
9696
'''
9797
A patch of grass that grows at a fixed rate and it is eaten by sheep
9898
'''
9999

100-
def __init__(self, fully_grown, countdown):
100+
def __init__(self, pos, model, fully_grown, countdown):
101101
'''
102102
Creates a new patch of grass
103103
104104
Args:
105105
grown: (boolean) Whether the patch of grass is fully grown or not
106106
countdown: Time for the patch of grass to be fully grown again
107107
'''
108+
super().__init__(pos, model)
108109
self.fully_grown = fully_grown
109110
self.countdown = countdown
110111

111-
def step(self, model):
112+
def step(self):
112113
if not self.fully_grown:
113114
if self.countdown <= 0:
114115
# Set as fully grown
115116
self.fully_grown = True
116-
self.countdown = model.grass_regrowth_time
117+
self.countdown = self.model.grass_regrowth_time
117118
else:
118119
self.countdown -= 1

‎examples/WolfSheep/wolf_sheep/model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __init__(self, height=20, width=20,
8484
x = random.randrange(self.width)
8585
y = random.randrange(self.height)
8686
energy = random.randrange(2 * self.sheep_gain_from_food)
87-
sheep = Sheep(self.grid, (x, y), True, energy)
87+
sheep = Sheep((x, y), self, True, energy)
8888
self.grid.place_agent(sheep, (x, y))
8989
self.schedule.add(sheep)
9090

@@ -93,7 +93,7 @@ def __init__(self, height=20, width=20,
9393
x = random.randrange(self.width)
9494
y = random.randrange(self.height)
9595
energy = random.randrange(2 * self.wolf_gain_from_food)
96-
wolf = Wolf(self.grid, (x, y), True, energy)
96+
wolf = Wolf((x, y), self, True, energy)
9797
self.grid.place_agent(wolf, (x, y))
9898
self.schedule.add(wolf)
9999

@@ -108,7 +108,7 @@ def __init__(self, height=20, width=20,
108108
else:
109109
countdown = random.randrange(self.grass_regrowth_time)
110110

111-
patch = GrassPatch(fully_grown, countdown)
111+
patch = GrassPatch((x, y), self, fully_grown, countdown)
112112
self.grid.place_agent(patch, (x, y))
113113
self.schedule.add(patch)
114114

‎examples/WolfSheep/wolf_sheep/random_walk.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ class RandomWalker(Agent):
2121
y = None
2222
moore = True
2323

24-
def __init__(self, grid, pos, moore=True):
24+
def __init__(self, pos, model, moore=True):
2525
'''
2626
grid: The MultiGrid object in which the agent lives.
2727
x: The agent's current x coordinate
2828
y: The agent's current y coordinate
2929
moore: If True, may move in all 8 directions.
3030
Otherwise, only up, down, left, right.
3131
'''
32-
self.grid = grid
32+
super().__init__(pos, model)
3333
self.pos = pos
3434
self.moore = moore
3535

@@ -38,7 +38,7 @@ def random_move(self):
3838
Step one cell in any allowable direction.
3939
'''
4040
# Pick the next cell from the adjacent cells.
41-
next_moves = self.grid.get_neighborhood(self.pos, self.moore, True)
41+
next_moves = self.model.grid.get_neighborhood(self.pos, self.moore, True)
4242
next_move = random.choice(next_moves)
4343
# Now move:
44-
self.grid.move_agent(self, next_move)
44+
self.model.grid.move_agent(self, next_move)

‎examples/WolfSheep/wolf_sheep/schedule.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RandomActivationByBreed(RandomActivation):
1212
This is equivalent to the NetLogo 'ask breed...' and is generally the
1313
default behavior for an ABM.
1414
15-
Assumes that all agents have a step(model) method.
15+
Assumes that all agents have a step() method.
1616
'''
1717
agents_by_breed = defaultdict(list)
1818

@@ -70,7 +70,7 @@ def step_breed(self, breed):
7070
agents = self.agents_by_breed[breed]
7171
random.shuffle(agents)
7272
for agent in agents:
73-
agent.step(self.model)
73+
agent.step()
7474

7575
def get_breed_count(self, breed_class):
7676
'''

‎examples/WolfSheep/wolf_sheep/test_random_walk.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WalkerAgent(RandomWalker):
1818
Agent which only walks around.
1919
'''
2020

21-
def step(self, model):
21+
def step(self):
2222
self.random_move()
2323

2424

@@ -47,7 +47,7 @@ def __init__(self, height, width, agent_count):
4747
for i in range(self.agent_count):
4848
x = random.randrange(self.width)
4949
y = random.randrange(self.height)
50-
a = WalkerAgent(self.grid, (x, y), True)
50+
a = WalkerAgent((x, y), self, True)
5151
self.schedule.add(a)
5252
self.grid.place_agent(a, (x, y))
5353

‎mesa/agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ def __init__(self, unique_id, model):
1414
self.unique_id = unique_id
1515
self.model = model
1616

17-
def step(self, model):
17+
def step(self):
1818
""" A single step of the agent. """
1919
pass

‎mesa/time.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class BaseScheduler:
3333
""" Simplest scheduler; activates agents one at a time, in the order
3434
they were added.
3535
36-
Assumes that each agent added has a *step* method, which accepts a model
37-
object as its single argument.
36+
Assumes that each agent added has a *step* method which takes no arguments.
3837
3938
(This is explicitly meant to replicate the scheduler in MASON).
4039
@@ -56,7 +55,7 @@ def add(self, agent):
5655
5756
Args:
5857
agent: An Agent to be added to the schedule. NOTE: The agent must
59-
have a step(model) method.
58+
have a step() method.
6059
6160
"""
6261
self.agents.append(agent)
@@ -74,7 +73,7 @@ def remove(self, agent):
7473
def step(self):
7574
""" Execute the step of all the agents, one at a time. """
7675
for agent in self.agents:
77-
agent.step(self.model)
76+
agent.step()
7877
self.steps += 1
7978
self.time += 1
8079

@@ -99,9 +98,8 @@ def step(self):
9998
10099
"""
101100
random.shuffle(self.agents)
102-
103101
for agent in self.agents:
104-
agent.step(self.model)
102+
agent.step()
105103
self.steps += 1
106104
self.time += 1
107105

@@ -110,16 +108,16 @@ class SimultaneousActivation(BaseScheduler):
110108
""" A scheduler to simulate the simultaneous activation of all the agents.
111109
112110
This scheduler requires that each agent have two methods: step and advance.
113-
step(model) activates the agent and stages any necessary changes, but does
114-
not apply them yet. advance(model) then applies the changes.
111+
step() activates the agent and stages any necessary changes, but does not
112+
apply them yet. advance() then applies the changes.
115113
116114
"""
117115
def step(self):
118116
""" Step all agents, then advance them. """
119117
for agent in self.agents:
120-
agent.step(self.model)
118+
agent.step()
121119
for agent in self.agents:
122-
agent.advance(self.model)
120+
agent.advance()
123121
self.steps += 1
124122
self.time += 1
125123

@@ -167,7 +165,7 @@ def step(self):
167165
random.shuffle(self.agents)
168166
for stage in self.stage_list:
169167
for agent in self.agents:
170-
getattr(agent, stage)(self.model) # Run stage
168+
getattr(agent, stage)() # Run stage
171169
if self.shuffle_between_stages:
172170
random.shuffle(self.agents)
173171
self.time += self.stage_time

‎tests/test_batchrunner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, unique_id, val):
1818
self.unique_id = unique_id
1919
self.val = val
2020

21-
def step(self, model):
21+
def step(self):
2222
"""
2323
increment val by 1
2424
"""

‎tests/test_datacollector.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ class MockAgent(Agent):
1212
'''
1313
Minimalistic agent for testing purposes.
1414
'''
15-
def __init__(self, unique_id, val):
16-
self.unique_id = unique_id
15+
def __init__(self, unique_id, model, val=0):
16+
super().__init__(unique_id, model)
1717
self.val = val
1818

19-
def step(self, model):
19+
def step(self):
2020
'''
2121
Increment val by 1.
2222
'''
2323
self.val += 1
2424

25-
def write_final_values(self, model):
25+
def write_final_values(self):
2626
'''
2727
Write the final value to the appropriate table.
2828
'''
2929
row = {"agent_id": self.unique_id, "final_value": self.val}
30-
model.datacollector.add_table_row("Final_Values", row)
30+
self.model.datacollector.add_table_row("Final_Values", row)
3131

3232

3333
class MockModel(Model):
@@ -40,7 +40,7 @@ class MockModel(Model):
4040
def __init__(self):
4141
self.schedule = BaseScheduler(self)
4242
for i in range(10):
43-
a = MockAgent(i, i)
43+
a = MockAgent(i, self, val=i)
4444
self.schedule.add(a)
4545
self.datacollector = DataCollector(
4646
{"total_agents": lambda m: m.schedule.get_agent_count()},
@@ -62,7 +62,7 @@ def setUp(self):
6262
self.model.step()
6363
# Write to table:
6464
for agent in self.model.schedule.agents:
65-
agent.write_final_values(self.model)
65+
agent.write_final_values()
6666

6767
def test_model_vars(self):
6868
'''

‎tests/test_time.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ class MockAgent(Agent):
1818
Minimalistic agent for testing purposes.
1919
'''
2020

21-
def __init__(self, name):
22-
self.unique_id = name
21+
def stage_one(self):
22+
self.model.log.append(self.unique_id + "_1")
2323

24-
def stage_one(self, model):
25-
model.log.append(self.unique_id + "_1")
24+
def stage_two(self):
25+
self.model.log.append(self.unique_id + "_2")
2626

27-
def stage_two(self, model):
28-
model.log.append(self.unique_id + "_2")
29-
30-
def advance(self, model):
27+
def advance(self):
3128
pass
3229

3330

@@ -64,7 +61,7 @@ def __init__(self, shuffle=False, activation=STAGED):
6461

6562
# Make agents
6663
for name in ["A", "B"]:
67-
agent = MockAgent(name)
64+
agent = MockAgent(name, self)
6865
self.schedule.add(agent)
6966

7067
def step(self):

0 commit comments

Comments
 (0)
Please sign in to comment.