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

Виправила баги в SugarscapeCg моделі #221

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions examples/sugarscape_cg/sugarscape_cg/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def get_distance(pos_1, pos_2):


class SsAgent(mesa.Agent):
def __init__(self, model, moore=False, sugar=0, metabolism=0, vision=0):
super().__init__(model)
def __init__(self, agent_id, model, moore=False, sugar=0, metabolism=0, vision=0):
super().__init__(agent_id, model)
self.moore = moore
self.sugar = sugar
self.metabolism = metabolism
Expand Down Expand Up @@ -71,8 +71,8 @@ def step(self):


class Sugar(mesa.Agent):
def __init__(self, model, max_sugar):
super().__init__(model)
def __init__(self, agent_id, model, max_sugar):
super().__init__(agent_id, model)
self.amount = max_sugar
self.max_sugar = max_sugar

Expand Down
37 changes: 25 additions & 12 deletions examples/sugarscape_cg/sugarscape_cg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
"""

from pathlib import Path

import mesa

from .agents import SsAgent, Sugar
import numpy as np


class SugarscapeCg(mesa.Model):
Expand All @@ -38,40 +37,54 @@ def __init__(self, width=50, height=50, initial_population=100):
self.initial_population = initial_population

self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False)

# Initialize agents by type
self.agents_by_type = {SsAgent: [], Sugar: []}

# Set up data collector
self.datacollector = mesa.DataCollector(
{"SsAgent": lambda m: len(m.agents_by_type[SsAgent])}
)
self.steps = 0

# Create sugar
import numpy as np

sugar_distribution = np.genfromtxt(Path(__file__).parent / "sugar-map.txt")
for _, (x, y) in self.grid.coord_iter():
sugar_id = 1
for contents, x, y in self.grid.coord_iter():
max_sugar = sugar_distribution[x, y]
sugar = Sugar(self, max_sugar)
sugar = Sugar(sugar_id, self, max_sugar)
self.grid.place_agent(sugar, (x, y))
self.agents_by_type[Sugar].append(sugar) # Track sugar agents
sugar_id += 1

# Create agent:
# Create agents
agent_id = 1
for i in range(self.initial_population):
x = self.random.randrange(self.width)
y = self.random.randrange(self.height)
sugar = self.random.randrange(6, 25)
metabolism = self.random.randrange(2, 4)
vision = self.random.randrange(1, 6)
ssa = SsAgent(self, False, sugar, metabolism, vision)
ssa = SsAgent(agent_id, self, False, sugar, metabolism, vision)
self.grid.place_agent(ssa, (x, y))
self.agents_by_type[SsAgent].append(ssa) # Track SsAgent agents
agent_id += 1

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

def step(self):
# Step suger and agents
self.agents_by_type[Sugar].do("step")
self.agents_by_type[SsAgent].shuffle_do("step")
# collect data
# Step sugar and agents
for sugar in self.agents_by_type[Sugar]:
sugar.step()
for agent in self.agents_by_type[SsAgent]:
agent.step()

# Collect data
self.datacollector.collect(self)
if self.verbose:
print(f"Step: {self.steps}, SsAgents: {len(self.agents_by_type[SsAgent])}")
self.steps += 1

def run_model(self, step_count=200):
if self.verbose:
Expand Down
Loading