Skip to content

Commit

Permalink
examples: Convert sugarscape_cg to simple namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored and tpike3 committed May 24, 2022
1 parent 59e238e commit 8077cdf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
6 changes: 3 additions & 3 deletions examples/sugarscape_cg/sugarscape_cg/agents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math

from mesa import Agent
import mesa


def get_distance(pos_1, pos_2):
Expand All @@ -17,7 +17,7 @@ def get_distance(pos_1, pos_2):
return math.sqrt(dx**2 + dy**2)


class SsAgent(Agent):
class SsAgent(mesa.Agent):
def __init__(self, pos, model, moore=False, sugar=0, metabolism=0, vision=0):
super().__init__(pos, model)
self.pos = pos
Expand Down Expand Up @@ -72,7 +72,7 @@ def step(self):
self.model.schedule.remove(self)


class Sugar(Agent):
class Sugar(mesa.Agent):
def __init__(self, pos, model, max_sugar):
super().__init__(pos, model)
self.amount = max_sugar
Expand Down
13 changes: 5 additions & 8 deletions examples/sugarscape_cg/sugarscape_cg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
Northwestern University, Evanston, IL.
"""

from mesa import Model
from mesa.space import MultiGrid
from mesa.datacollection import DataCollector
from mesa.time import RandomActivationByType
import mesa

from .agents import SsAgent, Sugar


class SugarscapeCg(Model):
class SugarscapeCg(mesa.Model):
"""
Sugarscape 2 Constant Growback
"""
Expand All @@ -37,9 +34,9 @@ def __init__(self, width=50, height=50, initial_population=100):
self.height = height
self.initial_population = initial_population

self.schedule = RandomActivationByType(self)
self.grid = MultiGrid(self.width, self.height, torus=False)
self.datacollector = DataCollector(
self.schedule = mesa.time.RandomActivationByType(self)
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False)
self.datacollector = mesa.DataCollector(
{"SsAgent": lambda m: m.schedule.get_type_count(SsAgent)}
)

Expand Down
11 changes: 6 additions & 5 deletions examples/sugarscape_cg/sugarscape_cg/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import CanvasGrid, ChartModule
import mesa

from .agents import SsAgent, Sugar
from .model import SugarscapeCg
Expand Down Expand Up @@ -32,10 +31,12 @@ def SsAgent_portrayal(agent):
return portrayal


canvas_element = CanvasGrid(SsAgent_portrayal, 50, 50, 500, 500)
chart_element = ChartModule([{"Label": "SsAgent", "Color": "#AA0000"}])
canvas_element = mesa.visualization.CanvasGrid(SsAgent_portrayal, 50, 50, 500, 500)
chart_element = mesa.visualization.ChartModule(
[{"Label": "SsAgent", "Color": "#AA0000"}]
)

server = ModularServer(
server = mesa.visualization.ModularServer(
SugarscapeCg, [canvas_element, chart_element], "Sugarscape 2 Constant Growback"
)
# server.launch()

0 comments on commit 8077cdf

Please sign in to comment.