Skip to content

Commit

Permalink
convert examples to use simple namespace from mesa
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-boyu authored and rht committed Oct 11, 2022
1 parent 7f3e31d commit 4f330fb
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 40 deletions.
10 changes: 4 additions & 6 deletions examples/geo_schelling/model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import random

from mesa import Model
from mesa.datacollection import DataCollector
from mesa.time import RandomActivation
import mesa

from mesa_geo import GeoSpace
from mesa_geo.geoagent import AgentCreator
Expand Down Expand Up @@ -53,19 +51,19 @@ def __repr__(self):
return "Agent " + str(self.unique_id)


class GeoSchelling(Model):
class GeoSchelling(mesa.Model):
"""Model class for the Schelling segregation model."""

def __init__(self, density=0.6, minority_pc=0.2, export_data=False):
self.density = density
self.minority_pc = minority_pc
self.export_data = export_data

self.schedule = RandomActivation(self)
self.schedule = mesa.time.RandomActivation(self)
self.space = GeoSpace(warn_crs_conversion=False)

self.happy = 0
self.datacollector = DataCollector({"happy": "happy"})
self.datacollector = mesa.DataCollector({"happy": "happy"})

self.running = True

Expand Down
8 changes: 3 additions & 5 deletions examples/geo_schelling/server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import mesa
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import ChartModule, TextElement
from mesa_geo.visualization.modules import MapModule

from model import GeoSchelling


class HappyElement(TextElement):
class HappyElement(mesa.visualization.TextElement):
"""
Display a text count of how many happy agents there are.
"""
Expand Down Expand Up @@ -41,7 +39,7 @@ def schelling_draw(agent):

happy_element = HappyElement()
map_element = MapModule(schelling_draw, [52, 12], 4)
happy_chart = ChartModule([{"Label": "happy", "Color": "Black"}])
server = ModularServer(
happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}])
server = mesa.visualization.ModularServer(
GeoSchelling, [map_element, happy_element, happy_chart], "Schelling", model_params
)
12 changes: 6 additions & 6 deletions examples/geo_schelling_points/geo_schelling_points/model.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import random
import uuid

from mesa import Model
from mesa.datacollection import DataCollector
from mesa.time import RandomActivation
import mesa

from mesa_geo.geoagent import AgentCreator
from .agents import PersonAgent, RegionAgent
from .space import Nuts2Eu


class GeoSchellingPoints(Model):
class GeoSchellingPoints(mesa.Model):
def __init__(self, red_percentage=0.5, similarity_threshold=0.5):
super().__init__()

self.red_percentage = red_percentage
PersonAgent.SIMILARITY_THRESHOLD = similarity_threshold

self.schedule = RandomActivation(self)
self.schedule = mesa.time.RandomActivation(self)
self.space = Nuts2Eu()

self.datacollector = DataCollector({"unhappy": "unhappy", "happy": "happy"})
self.datacollector = mesa.DataCollector(
{"unhappy": "unhappy", "happy": "happy"}
)

# Set up the grid with patches for every NUTS region
ac = AgentCreator(RegionAgent, model=self)
Expand Down
10 changes: 4 additions & 6 deletions examples/geo_schelling_points/geo_schelling_points/server.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import mesa
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import ChartModule, TextElement
from mesa_geo.visualization.modules import MapModule

from .agents import PersonAgent, RegionAgent
from .model import GeoSchellingPoints


class HappyElement(TextElement):
class HappyElement(mesa.visualization.TextElement):
def render(self, model):
return f"Happy agents: {model.happy}"


class UnhappyElement(TextElement):
class UnhappyElement(mesa.visualization.TextElement):
def render(self, model):
return f"Unhappy agents: {model.unhappy}"

Expand Down Expand Up @@ -44,7 +42,7 @@ def schelling_draw(agent):
happy_element = HappyElement()
unhappy_element = UnhappyElement()
map_element = MapModule(schelling_draw, [52, 12], 4)
happy_chart = ChartModule(
happy_chart = mesa.visualization.ChartModule(
[
{"Label": "unhappy", "Color": "Orange"},
{
Expand All @@ -53,7 +51,7 @@ def schelling_draw(agent):
},
]
)
server = ModularServer(
server = mesa.visualization.ModularServer(
GeoSchellingPoints,
[map_element, happy_element, unhappy_element, happy_chart],
"Schelling",
Expand Down
10 changes: 4 additions & 6 deletions examples/geo_sir/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from mesa import Model
from mesa.datacollection import DataCollector
from mesa.time import BaseScheduler
import mesa
from shapely.geometry import Point

from mesa_geo import GeoSpace
Expand All @@ -9,7 +7,7 @@
from agents import PersonAgent, NeighbourhoodAgent


class GeoSir(Model):
class GeoSir(mesa.Model):
"""Model class for a simplistic infection model."""

# Geographical parameters for desired map
Expand All @@ -26,7 +24,7 @@ def __init__(
:param exposure_distance: Proximity distance between agents to be exposed to each other
:param infection_risk: Probability of agent to become infected, if it has been exposed to another infected
"""
self.schedule = BaseScheduler(self)
self.schedule = mesa.time.BaseScheduler(self)
self.space = GeoSpace(warn_crs_conversion=False)
self.steps = 0
self.counts = None
Expand All @@ -39,7 +37,7 @@ def __init__(
self.infection_risk = infection_risk

self.running = True
self.datacollector = DataCollector(
self.datacollector = mesa.DataCollector(
{
"infected": get_infected_count,
"susceptible": get_susceptible_count,
Expand Down
8 changes: 3 additions & 5 deletions examples/geo_sir/server.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import mesa
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import ChartModule, TextElement
from mesa_geo.visualization.modules import MapModule

from model import GeoSir
from agents import PersonAgent


class InfectedText(TextElement):
class InfectedText(mesa.visualization.TextElement):
"""
Display a text count of how many steps have been taken
"""
Expand Down Expand Up @@ -50,15 +48,15 @@ def infected_draw(agent):

infected_text = InfectedText()
map_element = MapModule(infected_draw)
infected_chart = ChartModule(
infected_chart = mesa.visualization.ChartModule(
[
{"Label": "infected", "Color": "Red"},
{"Label": "susceptible", "Color": "Green"},
{"Label": "recovered", "Color": "Blue"},
{"Label": "dead", "Color": "Black"},
]
)
server = ModularServer(
server = mesa.visualization.ModularServer(
GeoSir,
[map_element, infected_text, infected_chart],
"Basic agent-based SIR model",
Expand Down
3 changes: 1 addition & 2 deletions examples/population/population/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mesa
from mesa.visualization.ModularVisualization import ModularServer
from mesa_geo.geoagent import GeoAgent
from mesa_geo.visualization.modules import MapModule
from shapely.geometry import Point, Polygon
Expand Down Expand Up @@ -37,6 +36,6 @@ def agent_portrayal(agent):
geospace_element = MapModule(agent_portrayal)
num_agents_element = NumAgentsElement()

server = ModularServer(
server = mesa.visualization.ModularServer(
Population, [geospace_element, num_agents_element], "Population Model"
)
3 changes: 1 addition & 2 deletions examples/rainfall/rainfall/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Tuple

import mesa
from mesa.visualization.ModularVisualization import ModularServer
from mesa_geo.visualization.modules import MapModule

from .model import Rainfall
Expand Down Expand Up @@ -43,6 +42,6 @@ def cell_portrayal(cell: LakeCell) -> Tuple[float, float, float, float]:
]
)

server = ModularServer(
server = mesa.visualization.ModularServer(
Rainfall, [map_module, water_chart], "Rainfall Model", model_params
)
3 changes: 1 addition & 2 deletions examples/urban_growth/urban_growth/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Tuple

import mesa
from mesa.visualization.ModularVisualization import ModularServer
from mesa_geo.visualization.modules import MapModule

from .model import UrbanGrowth
Expand Down Expand Up @@ -55,7 +54,7 @@ def render(self, model):
]
)

server = ModularServer(
server = mesa.visualization.ModularServer(
UrbanGrowth,
[map_module, urbanized_text, urbanized_chart],
"Urban Growth Model",
Expand Down

0 comments on commit 4f330fb

Please sign in to comment.