Skip to content
Snippets Groups Projects
Commit edc6eded authored by LouiseDupuis's avatar LouiseDupuis
Browse files

first prototype

parent 6314c330
No related branches found
No related tags found
No related merge requests found
File added
File added
run.py 0 → 100644
from stade_model import StadeModel
num_agents = 100
num_steps = 10
model = StadeModel(num_agents, height = 10, width = 10)
for i in range(num_steps):
model.step()
\ No newline at end of file
from mesa import Agent
class StadeAgent(Agent):
"""An agent"""
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.state = "N"
def set_state(self, state):
self.state = state
def step(self):
#getting the agent's neighbors :
neighbors = self.model.grid.get_neighbors(self.pos, True, False)
states = [agent.state for agent in neighbors]
if states.count("S") >= len(self.model.grid.get_neighborhood(self.pos, True, False))//2:
self.neighbors_are_shouting = True
else :
self.neighbors_are_shouting = False
def advance(self):
if self.neighbors_are_shouting:
self.set_state("S")
class UltraAgent(Agent):
"""An agent who is always in shouting state"""
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.state = "S"
\ No newline at end of file
from mesa import Model
from mesa.time import SimultaneousActivation
from mesa.space import SingleGrid
from stade_agents import StadeAgent, UltraAgent
class StadeModel(Model):
"""A model with some number of agents."""
def __init__(self, num_agents = 1, width = 2, height= 2):
self.running = True
self.num_agents = num_agents
# Create agents and scheduler
self.schedule = SimultaneousActivation(self)
#creating a grid - for now, default rid
self.grid = SingleGrid(width, height, True)
for i in range(self.num_agents):
a = StadeAgent(i, self)
self.schedule.add(a)
# Add the agent to a random grid cell
self.grid.place_agent(a, self.grid.find_empty())
#set the state randomly
if self.random.random()>0.8:
a.set_state("S")
def step(self):
"""Advance the model by one step."""
self.schedule.step()
from mesa.visualization.modules import CanvasGrid
from mesa.visualization.ModularVisualization import ModularServer
from stade_model import StadeModel
def agent_portrayal(agent):
portrayal = {
"Shape": "circle",
"Filled": "true",
"Layer": 0,
"r": 0.5,
}
if agent.state == "N":
portrayal["Color"] = "grey"
# portrayal["Layer"] = 0
elif agent.state == "S":
portrayal["Color"] = "red"
# portrayal["Layer"] = 1
return portrayal
# ---- Parameters-----
width = 20
height = 5
num_agents = width * height
#----------------------
grid = CanvasGrid(agent_portrayal, width, height, 500, 500)
server = ModularServer(
StadeModel, [grid], "Stade Model", {"num_agents": num_agents, "width": width, "height": height}
)
server.port = 8521 # The default
server.launch()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment