Skip to content
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

додали новий параметр до моделі move_probability #212

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions examples/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@


class SchellingAgent(mesa.Agent):
"""
Schelling segregation agent
"""

def __init__(self, model, agent_type):
def __init__(self, unique_id, model, agent_type):
"""
Create a new Schelling agent.

Args:
x, y: Agent initial location.
unique_id: A unique identifier for the agent.
agent_type: Indicator for the agent's type (minority=1, majority=0)
"""
super().__init__(model)
super().__init__(unique_id, model)
self.type = agent_type

def step(self):
Expand All @@ -25,9 +21,15 @@ def step(self):
if neighbor.type == self.type:
similar += 1

# If unhappy, move:
# If unhappy, decide whether to move:
if similar < self.model.homophily:
self.model.grid.move_to_empty(self)
if (
self.random.random() < self.model.move_probability
): # Add randomness to moving
self.model.grid.move_to_empty(self)
else:
# Agent decides to stay even if unhappy
pass
else:
self.model.happy += 1

Expand All @@ -45,27 +47,17 @@ def __init__(
radius=1,
density=0.8,
minority_pc=0.2,
move_probability=0.9,
seed=None,
):
"""
Create a new Schelling model.

Args:
width, height: Size of the space.
density: Initial Chance for a cell to populated
minority_pc: Chances for an agent to be in minority class
homophily: Minimum number of agents of same class needed to be happy
radius: Search radius for checking similarity
seed: Seed for Reproducibility
"""

super().__init__(seed=seed)
self.height = height
self.width = width
self.density = density
self.minority_pc = minority_pc
self.homophily = homophily
self.radius = radius
self.move_probability = move_probability

self.grid = mesa.space.SingleGrid(width, height, torus=True)

Expand All @@ -74,15 +66,14 @@ def __init__(
model_reporters={"happy": "happy"}, # Model-level count of happy agents
)

# Set up agents
# We use a grid iterator that returns
# the coordinates of a cell as well as
# its contents. (coord_iter)
# Set up agents with unique IDs
unique_id = 0 # Initialize unique_id counter
for _, pos in self.grid.coord_iter():
if self.random.random() < self.density:
agent_type = 1 if self.random.random() < self.minority_pc else 0
agent = SchellingAgent(self, agent_type)
agent = SchellingAgent(unique_id, self, agent_type) # Pass unique_id
self.grid.place_agent(agent, pos)
unique_id += 1 # Increment the unique_id for the next agent

self.datacollector.collect(self)

Expand Down
4 changes: 2 additions & 2 deletions examples/schelling/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ def schelling_draw(agent):
name="Fraction minority", value=0.2, min_value=0.00, max_value=1.0, step=0.05
),
"homophily": mesa.visualization.Slider(
name="Homophily", value=3, min_value=0, max_value=8, step=1
name="Homophily", value=3, min_value=0, max_value=100, step=1
),
"radius": mesa.visualization.Slider(
name="Search Radius", value=1, min_value=1, max_value=5, step=1
name="Search Radius", value=1, min_value=1, max_value=25, step=1
),
}

Expand Down
Loading