Skip to content

Commit 86df2c4

Browse files
committed
move bank model to new OrthogonalMooreGrid
1 parent 9876862 commit 86df2c4

File tree

5 files changed

+14
-197
lines changed

5 files changed

+14
-197
lines changed

examples/bank_reserves/bank_reserves/agents.py

Lines changed: 8 additions & 11 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):
45+
class Person(CellAgent):
4846
def __init__(self, model, moore, 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,15 +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+
69+
7170
# check if other people are at my location
72-
if len(my_cell) > 1:
71+
if len(self.cell.agents) > 1:
7372
# set customer to self for while loop condition
7473
customer = self
7574
while customer == self:
76-
"""select a random person from the people at my location
77-
to trade with"""
78-
customer = self.random.choice(my_cell)
75+
customer = self.random.choice(self.cell.agents)
7976
# 50% chance of trading with customer
8077
if self.random.randint(0, 1) == 0:
8178
# 50% chance of trading $5
@@ -178,7 +175,7 @@ def take_out_loan(self, amount):
178175

179176
def step(self):
180177
# move to a cell in my Moore neighborhood
181-
self.random_move()
178+
self.move_to(self.cell.neighborhood().select_random_cell())
182179
# trade
183180
self.do_business()
184181
# deposit money or take out a loan

examples/bank_reserves/bank_reserves/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.
@@ -78,7 +79,7 @@ def get_total_loans(model):
7879
return np.sum(agent_loans)
7980

8081

81-
class BankReserves(mesa.Model):
82+
class BankReservesModel(mesa.Model):
8283
"""
8384
This model is a Mesa implementation of the Bank Reserves model from NetLogo.
8485
It is a highly abstracted, simplified model of an economy, with only one
@@ -117,7 +118,7 @@ def __init__(
117118
self.width = width
118119
self.init_people = init_people
119120

120-
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
121+
self.grid = OrthogonalMooreGrid((self.width, self.height), torus=True, random=self.random)
121122
# rich_threshold is the amount of savings a person needs to be considered "rich"
122123
self.rich_threshold = rich_threshold
123124
self.reserve_percent = reserve_percent
@@ -145,7 +146,7 @@ def __init__(
145146
y = self.random.randrange(self.height)
146147
p = Person(self, True, self.bank, self.rich_threshold)
147148
# place the Person object on the grid at coordinates (x, y)
148-
self.grid.place_agent(p, (x, y))
149+
p.move_to(self.grid[(x, y)])
149150

150151
self.running = True
151152
self.datacollector.collect(self)

examples/bank_reserves/bank_reserves/random_walk.py

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

examples/bank_reserves/bank_reserves/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import mesa
22

33
from .agents import Person
4-
from .model import BankReserves
4+
from .model import BankReservesModel
55

66
"""
77
Citation:

examples/bank_reserves/batch_run.py

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -29,70 +29,8 @@
2929
import mesa
3030
import numpy as np
3131
import pandas as pd
32-
from bank_reserves.agents import Bank, Person
33-
34-
# Start of datacollector functions
35-
36-
37-
def get_num_rich_agents(model):
38-
"""list of rich agents"""
39-
40-
rich_agents = [a for a in model.agents if a.savings > model.rich_threshold]
41-
# return number of rich agents
42-
return len(rich_agents)
43-
44-
45-
def get_num_poor_agents(model):
46-
"""list of poor agents"""
47-
48-
poor_agents = [a for a in model.agents if a.loans > 10]
49-
# return number of poor agents
50-
return len(poor_agents)
51-
52-
53-
def get_num_mid_agents(model):
54-
"""list of middle class agents"""
55-
56-
mid_agents = [
57-
a for a in model.agents if a.loans < 10 and a.savings < model.rich_threshold
58-
]
59-
# return number of middle class agents
60-
return len(mid_agents)
61-
62-
63-
def get_total_savings(model):
64-
"""list of amounts of all agents' savings"""
65-
66-
agent_savings = [a.savings for a in model.agents]
67-
# return the sum of agents' savings
68-
return np.sum(agent_savings)
69-
70-
71-
def get_total_wallets(model):
72-
"""list of amounts of all agents' wallets"""
73-
74-
agent_wallets = [a.wallet for a in model.agents]
75-
# return the sum of all agents' wallets
76-
return np.sum(agent_wallets)
77-
78-
79-
def get_total_money(model):
80-
"""sum of all agents' wallets"""
81-
82-
wallet_money = get_total_wallets(model)
83-
# sum of all agents' savings
84-
savings_money = get_total_savings(model)
85-
# return sum of agents' wallets and savings for total money
86-
return wallet_money + savings_money
87-
88-
89-
def get_total_loans(model):
90-
"""list of amounts of all agents' loans"""
91-
92-
agent_loans = [a.loans for a in model.agents]
93-
# return sum of all agents' loans
94-
return np.sum(agent_loans)
9532

33+
from bank_reserves.model import BankReservesModel
9634

9735
def track_params(model):
9836
return (model.init_people, model.rich_threshold, model.reserve_percent)
@@ -101,79 +39,6 @@ def track_params(model):
10139
def track_run(model):
10240
return model.uid
10341

104-
105-
class BankReservesModel(mesa.Model):
106-
# id generator to track run number in batch run data
107-
id_gen = itertools.count(1)
108-
109-
# grid height
110-
grid_h = 20
111-
# grid width
112-
grid_w = 20
113-
114-
"""init parameters "init_people", "rich_threshold", and "reserve_percent"
115-
are all set via Slider"""
116-
117-
def __init__(
118-
self,
119-
height=grid_h,
120-
width=grid_w,
121-
init_people=2,
122-
rich_threshold=10,
123-
reserve_percent=50,
124-
):
125-
super().__init__()
126-
self.uid = next(self.id_gen)
127-
self.height = height
128-
self.width = width
129-
self.init_people = init_people
130-
131-
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
132-
# rich_threshold is the amount of savings a person needs to be considered "rich"
133-
self.rich_threshold = rich_threshold
134-
self.reserve_percent = reserve_percent
135-
# see datacollector functions above
136-
self.datacollector = mesa.DataCollector(
137-
model_reporters={
138-
"Rich": get_num_rich_agents,
139-
"Poor": get_num_poor_agents,
140-
"Middle Class": get_num_mid_agents,
141-
"Savings": get_total_savings,
142-
"Wallets": get_total_wallets,
143-
"Money": get_total_money,
144-
"Loans": get_total_loans,
145-
"Model Params": track_params,
146-
"Run": track_run,
147-
},
148-
agent_reporters={"Wealth": "wealth"},
149-
)
150-
151-
# create a single bank object for the model
152-
self.bank = Bank(self, self.reserve_percent)
153-
154-
# create people for the model according to number of people set by user
155-
for i in range(self.init_people):
156-
# set x coordinate as a random number within the width of the grid
157-
x = self.random.randrange(self.width)
158-
# set y coordinate as a random number within the height of the grid
159-
y = self.random.randrange(self.height)
160-
p = Person(i, (x, y), self, True, self.bank, self.rich_threshold)
161-
# place the Person object on the grid at coordinates (x, y)
162-
self.grid.place_agent(p, (x, y))
163-
164-
self.running = True
165-
166-
def step(self):
167-
# collect data
168-
self.datacollector.collect(self)
169-
# tell all the agents in the model to run their step function
170-
self.agents.shuffle().do("step")
171-
172-
def run_model(self):
173-
for i in range(self.run_time):
174-
self.step()
175-
176-
17742
# parameter lists for each parameter to be tested in batch run
17843
br_params = {
17944
"init_people": [25, 100],

0 commit comments

Comments
 (0)