diff --git a/__pycache__/stade_agents.cpython-39.pyc b/__pycache__/stade_agents.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ce5e413b1868c52548db1b7b4c6dcd81e3709ee2 Binary files /dev/null and b/__pycache__/stade_agents.cpython-39.pyc differ diff --git a/__pycache__/stade_model.cpython-39.pyc b/__pycache__/stade_model.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0eb213c5c1cf1e5466bdf6a7c3a5be0a9654c4fd Binary files /dev/null and b/__pycache__/stade_model.cpython-39.pyc differ diff --git a/run.py b/run.py new file mode 100644 index 0000000000000000000000000000000000000000..7949daaf4d54bb42807679ad64c35df32236891e --- /dev/null +++ b/run.py @@ -0,0 +1,10 @@ +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 diff --git a/stade_agents.py b/stade_agents.py new file mode 100644 index 0000000000000000000000000000000000000000..ce76d6e1f10e6796d0f6f39aedc7b9e62cfc9f64 --- /dev/null +++ b/stade_agents.py @@ -0,0 +1,35 @@ +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 diff --git a/stade_model.py b/stade_model.py new file mode 100644 index 0000000000000000000000000000000000000000..52d4d1e19c9ce7232b54946aa48ee20b65568422 --- /dev/null +++ b/stade_model.py @@ -0,0 +1,34 @@ +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() + + diff --git a/stade_model_viz.py b/stade_model_viz.py new file mode 100644 index 0000000000000000000000000000000000000000..f9e14ae91200b02f3950dc101f96555ea18a1d34 --- /dev/null +++ b/stade_model_viz.py @@ -0,0 +1,35 @@ +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