Skip to content

Commit 3109a0d

Browse files
committed
another batch of models
1 parent 5c382ac commit 3109a0d

File tree

9 files changed

+82
-166
lines changed

9 files changed

+82
-166
lines changed

examples/charts/charts/agents.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
Center for Connected Learning and Computer-Based Modeling,
1010
Northwestern University, Evanston, IL.
1111
"""
12-
13-
from .random_walk import RandomWalker
14-
12+
from mesa.spaces import CellAgent
1513

1614
class Bank:
1715
"""Note that the Bank class is not a Mesa Agent, but just a regular Python
@@ -44,10 +42,10 @@ def bank_balance(self):
4442

4543

4644
# subclass of RandomWalker, which is subclass to Mesa Agent
47-
class Person(RandomWalker):
48-
def __init__(self, model, moore, bank, rich_threshold):
45+
class Person(CellAgent):
46+
def __init__(self, model, bank, rich_threshold):
4947
# init parent class with required parameters
50-
super().__init__(model, moore=moore)
48+
super().__init__(model)
5149
# the amount each person has in savings
5250
self.savings = 0
5351
# total loan amount person has outstanding
@@ -67,14 +65,14 @@ def do_business(self):
6765
bank can loan them any money"""
6866
if self.savings > 0 or self.wallet > 0 or self.bank.bank_to_loan > 0:
6967
# create list of people at my location (includes self)
70-
my_cell = self.model.grid.get_cell_list_contents([self.pos])
68+
my_cell = [a for a in self.cell.agents if not a == self]
7169
# check if other people are at my location
7270
if len(my_cell) > 1:
7371
# set customer to self for while loop condition
7472
customer = self
7573
while customer == self:
76-
"""select a random person from the people at my location
77-
to trade with"""
74+
# select a random person from the people at my location
75+
# to trade with
7876
customer = self.random.choice(my_cell)
7977
# 50% chance of trading with customer
8078
if self.random.randint(0, 1) == 0:
@@ -178,7 +176,7 @@ def take_out_loan(self, amount):
178176

179177
def step(self):
180178
# move to a cell in my Moore neighborhood
181-
self.random_move()
179+
self.move_to(self.cell.neighborhood().select_random_cell())
182180
# trade
183181
self.do_business()
184182
# deposit money or take out a loan

examples/charts/charts/model.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import numpy as np
1515

1616
from .agents import Bank, Person
17+
from mesa.spaces import OrthogonalMooreGrid
1718

1819
"""
1920
If you want to perform a parameter sweep, call batch_run.py instead of run.py.
@@ -100,7 +101,7 @@ def __init__(
100101
self.width = width
101102
self.init_people = init_people
102103

103-
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
104+
self.grid = OrthogonalMooreGrid((self.width, self.height), torus=True)
104105
# rich_threshold is the amount of savings a person needs to be considered "rich"
105106
self.rich_threshold = rich_threshold
106107
self.reserve_percent = reserve_percent
@@ -126,9 +127,9 @@ def __init__(
126127
# set x, y coords randomly within the grid
127128
x = self.random.randrange(self.width)
128129
y = self.random.randrange(self.height)
129-
p = Person(self, True, self.bank, self.rich_threshold)
130+
p = Person(self, self.bank, self.rich_threshold)
130131
# place the Person object on the grid at coordinates (x, y)
131-
self.grid.place_agent(p, (x, y))
132+
p.move_to(self.grid[(x, y)])
132133

133134
self.running = True
134135
self.datacollector.collect(self)

examples/charts/charts/random_walk.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

examples/color_patches/color_patches/model.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,28 @@
77
import mesa
88

99

10-
class ColorCell(mesa.Agent):
10+
class ColorCell(mesa.spaces.CellAgent):
1111
"""
1212
Represents a cell's opinion (visualized by a color)
1313
"""
1414

1515
OPINIONS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
1616

17-
def __init__(self, pos, model, initial_state):
17+
def __init__(self, model, initial_state):
1818
"""
1919
Create a cell, in the given state, at the given row, col position.
2020
"""
2121
super().__init__(model)
22-
self._row = pos[0]
23-
self._col = pos[1]
24-
self._state = initial_state
25-
self._next_state = None
22+
self.state = initial_state
23+
self.next_state = None
2624

2725
def get_col(self):
2826
"""Return the col location of this cell."""
29-
return self._col
27+
return self.cell.coordinate[0]
3028

3129
def get_row(self):
3230
"""Return the row location of this cell."""
33-
return self._row
34-
35-
def get_state(self):
36-
"""Return the current state (OPINION) of this cell."""
37-
return self._state
31+
return self.cell.coordinate[1]
3832

3933
def determine_opinion(self):
4034
"""
@@ -43,22 +37,22 @@ def determine_opinion(self):
4337
A choice is made at random in case of a tie
4438
The next state is stored until all cells have been polled
4539
"""
46-
_neighbor_iter = self.model.grid.iter_neighbors((self._row, self._col), True)
47-
neighbors_opinion = Counter(n.get_state() for n in _neighbor_iter)
40+
neighbors = self.cell.neighborhood().agents
41+
neighbors_opinion = Counter(n.state for n in neighbors)
4842
# Following is a a tuple (attribute, occurrences)
4943
polled_opinions = neighbors_opinion.most_common()
5044
tied_opinions = []
5145
for neighbor in polled_opinions:
5246
if neighbor[1] == polled_opinions[0][1]:
5347
tied_opinions.append(neighbor)
5448

55-
self._next_state = self.random.choice(tied_opinions)[0]
49+
self.next_state = self.random.choice(tied_opinions)[0]
5650

5751
def assume_opinion(self):
5852
"""
5953
Set the state of the agent to the next state
6054
"""
61-
self._state = self._next_state
55+
self.state = self.next_state
6256

6357

6458
class ColorPatches(mesa.Model):
@@ -72,18 +66,17 @@ def __init__(self, width=20, height=20):
7266
The agents next state is first determined before updating the grid
7367
"""
7468
super().__init__()
75-
self._grid = mesa.space.SingleGrid(width, height, torus=False)
69+
self._grid = mesa.spaces.OrthogonalMooreGrid((width, height), torus=False)
7670

7771
# self._grid.coord_iter()
7872
# --> should really not return content + col + row
7973
# -->but only col & row
8074
# for (contents, col, row) in self._grid.coord_iter():
8175
# replaced content with _ to appease linter
82-
for _, (row, col) in self._grid.coord_iter():
83-
cell = ColorCell(
84-
(row, col), self, ColorCell.OPINIONS[self.random.randrange(0, 16)]
76+
for cell in self._grid.all_cells:
77+
agent = ColorCell(self, ColorCell.OPINIONS[self.random.randrange(0, 16)]
8578
)
86-
self._grid.place_agent(cell, (row, col))
79+
agent.move_to(cell)
8780

8881
self.running = True
8982

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import mesa
22

33

4-
class Cell(mesa.Agent):
4+
class Cell(mesa.spaces.CellAgent):
55
"""Represents a single ALIVE or DEAD cell in the simulation."""
66

77
DEAD = 0
88
ALIVE = 1
99

10-
def __init__(self, pos, model, init_state=DEAD):
10+
def __init__(self, model, init_state=DEAD):
1111
"""
1212
Create a cell, in the given state, at the given x, y position.
1313
"""
1414
super().__init__(model)
15-
self.x, self.y = pos
1615
self.state = init_state
17-
self._nextState = None
16+
self._next_state = None
1817

1918
@property
20-
def isAlive(self):
19+
def is_alive(self):
2120
return self.state == self.ALIVE
2221

2322
@property
2423
def neighbors(self):
25-
return self.model.grid.iter_neighbors((self.x, self.y), True)
24+
return self.cell.neighborhood().agents
2625

2726
def determine_state(self):
2827
"""
@@ -35,19 +34,19 @@ def determine_state(self):
3534

3635
# Get the neighbors and apply the rules on whether to be alive or dead
3736
# at the next tick.
38-
live_neighbors = sum(neighbor.isAlive for neighbor in self.neighbors)
37+
live_neighbors = sum(neighbor.is_alive for neighbor in self.neighbors)
3938

4039
# Assume nextState is unchanged, unless changed below.
41-
self._nextState = self.state
42-
if self.isAlive:
40+
self._next_state = self.state
41+
if self.is_alive:
4342
if live_neighbors < 2 or live_neighbors > 3:
44-
self._nextState = self.DEAD
43+
self._next_state = self.DEAD
4544
else:
4645
if live_neighbors == 3:
47-
self._nextState = self.ALIVE
46+
self._next_state = self.ALIVE
4847

4948
def assume_state(self):
5049
"""
5150
Set the state to the new computed state -- computed in step().
5251
"""
53-
self.state = self._nextState
52+
self.state = self._next_state

examples/conways_game_of_life/conways_game_of_life/model.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ def __init__(self, width=50, height=50):
1515
"""
1616
super().__init__()
1717
# Use a simple grid, where edges wrap around.
18-
self.grid = mesa.space.SingleGrid(width, height, torus=True)
18+
self.grid = mesa.spaces.OrthogonalMooreGrid((width, height), torus=True)
1919

2020
# Place a cell at each location, with some initialized to
2121
# ALIVE and some to DEAD.
22-
for contents, (x, y) in self.grid.coord_iter():
23-
cell = Cell((x, y), self)
22+
for cell in self.grid.all_cells:
23+
cell_agent = Cell(self)
2424
if self.random.random() < 0.1:
25-
cell.state = cell.ALIVE
26-
self.grid.place_agent(cell, (x, y))
25+
cell_agent.state = cell_agent.ALIVE
26+
cell_agent.move_to(cell)
2727

2828
self.running = True
2929

0 commit comments

Comments
 (0)