-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add cell-centric discrete spaces (experimental) #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 23 commits
dd54f21
7b58d8e
aef4a8b
3240a2b
d3df44e
c80a0bd
93d7db5
be7b843
bd788ec
cb0ef18
94f06b1
be6c1af
875d0ed
e4d121d
5769a05
82a64d9
5fdc787
152995c
c524034
972ecc4
a656d93
8df9276
f4a1089
1ebcf06
d362bae
c7d82d0
b2842c0
740f003
2d8fcb5
99ff668
565ae0a
ab87b70
ab13bbc
8f2e3dd
ac8ff31
aa03962
9b28f9d
bbca3ac
fe5a93f
2c80330
c79aaf0
8384cf1
253531c
ebdee8e
bb2fc52
b3efdee
8c7ad05
819e903
0f49dc7
da94fb1
b210686
c35591c
065e5ea
1bfa009
6b1c454
e0d5673
cf789f2
50666c1
e520d3c
85251f0
3a247cb
86127f2
fb19879
5ea5d35
b7ac86c
76ca4a5
45e3e9f
3d8cdfd
ce8ca0b
18bd2a3
62c844e
adff7aa
7d5e6b0
b32af9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
from mesa import Agent, Model | ||
from mesa.space import SingleGrid | ||
from mesa import Model | ||
from mesa.experimental.cell_space import CellAgent, OrthogonalGrid | ||
from mesa.time import RandomActivation | ||
|
||
|
||
class SchellingAgent(Agent): | ||
class SchellingAgent(CellAgent): | ||
""" | ||
Schelling segregation agent | ||
""" | ||
|
||
def __init__(self, unique_id, model, agent_type): | ||
def __init__(self, unique_id, model, agent_type, radius): | ||
""" | ||
Create a new Schelling agent. | ||
Args: | ||
|
@@ -18,18 +18,17 @@ def __init__(self, unique_id, model, agent_type): | |
""" | ||
super().__init__(unique_id, model) | ||
self.type = agent_type | ||
self.radius = radius | ||
|
||
def step(self): | ||
similar = 0 | ||
for neighbor in self.model.grid.iter_neighbors( | ||
self.pos, moore=True, radius=self.model.radius | ||
): | ||
for neighbor in self.cell.neighborhood(radius=self.radius).agents: | ||
if neighbor.type == self.type: | ||
similar += 1 | ||
|
||
# If unhappy, move: | ||
if similar < self.model.homophily: | ||
self.model.grid.move_to_empty(self) | ||
self.move_to(self.model.grid.select_random_empty_cell()) | ||
else: | ||
self.model.happy += 1 | ||
|
||
|
@@ -49,22 +48,21 @@ def __init__( | |
self.density = density | ||
self.minority_pc = minority_pc | ||
self.homophily = homophily | ||
self.radius = radius | ||
|
||
self.schedule = RandomActivation(self) | ||
self.grid = SingleGrid(height, width, torus=True) | ||
self.grid = OrthogonalGrid(height, width, torus=True, capacity=1) | ||
|
||
self.happy = 0 | ||
|
||
# Set up agents | ||
# We use a grid iterator that returns | ||
# the coordinates of a cell as well as | ||
# its contents. (coord_iter) | ||
for _cont, pos in self.grid.coord_iter(): | ||
for cell in self.grid: | ||
if self.random.random() < self.density: | ||
agent_type = 1 if self.random.random() < self.minority_pc else 0 | ||
agent = SchellingAgent(self.next_id(), self, agent_type) | ||
self.grid.place_agent(agent, pos) | ||
agent = SchellingAgent(self.next_id(), self, agent_type, radius) | ||
|
||
agent.move_to(cell) | ||
self.schedule.add(agent) | ||
|
||
def step(self): | ||
|
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.