Skip to content
Merged
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
27 changes: 8 additions & 19 deletions examples/sugarscape_g1mt/sugarscape_g1mt/model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mesa
import numpy as np

from .resource_agents import Spice, Sugar
from .resource_agents import Resource
from .trader_agents import Trader


Expand Down Expand Up @@ -91,18 +91,11 @@ def __init__(
agent_id = 0
for _, (x, y) in self.grid.coord_iter():
max_sugar = sugar_distribution[x, y]
if max_sugar > 0:
sugar = Sugar(agent_id, self, (x, y), max_sugar)
self.schedule.add(sugar)
self.grid.place_agent(sugar, (x, y))
agent_id += 1

max_spice = spice_distribution[x, y]
if max_spice > 0:
spice = Spice(agent_id, self, (x, y), max_spice)
self.schedule.add(spice)
self.grid.place_agent(spice, (x, y))
agent_id += 1
resource = Resource(agent_id, self, (x, y), max_sugar, max_spice)
self.schedule.add(resource)
self.grid.place_agent(resource, (x, y))
agent_id += 1

for i in range(self.initial_population):
# get agent position
Expand Down Expand Up @@ -155,13 +148,9 @@ def step(self):
Unique step function that does staged activation of sugar and spice
and then randomly activates traders
"""
# step Sugar agents
for sugar in self.schedule.agents_by_type[Sugar].values():
sugar.step()

# step Spice agents
for spice in self.schedule.agents_by_type[Spice].values():
spice.step()
# step Resource agents
for resource in self.schedule.agents_by_type[Resource].values():
resource.step()

# step trader agents
# to account for agent death and removal we need a seperate data strcuture to
Expand Down
37 changes: 10 additions & 27 deletions examples/sugarscape_g1mt/sugarscape_g1mt/resource_agents.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,26 @@
import mesa


class Sugar(mesa.Agent):
class Resource(mesa.Agent):
"""
Sugar:
- contains an amount of sugar
Resource:
- contains an amount of sugar and spice
- grows 1 amount of sugar at each turn
"""

def __init__(self, unique_id, model, pos, max_sugar):
super().__init__(unique_id, model)
self.pos = pos
self.amount = max_sugar
self.max_sugar = max_sugar

def step(self):
"""
Sugar growth function, adds one unit of sugar each step until
max amount
"""
self.amount = min([self.max_sugar, self.amount + 1])


class Spice(mesa.Agent):
"""
Spice:
- contains an amount of spice
- grows 1 amount of spice at each turn
"""

def __init__(self, unique_id, model, pos, max_spice):
def __init__(self, unique_id, model, pos, max_sugar, max_spice):
super().__init__(unique_id, model)
self.pos = pos
self.amount = max_spice
self.sugar_amount = max_sugar
self.max_sugar = max_sugar
self.spice_amount = max_spice
self.max_spice = max_spice

def step(self):
"""
Spice growth function, adds one unit of spice each step until
Growth function, adds one unit of sugar and spice each step up to
max amount
"""
self.amount = min([self.max_spice, self.amount + 1])
self.sugar_amount = min([self.max_sugar, self.sugar_amount + 1])
self.spice_amount = min([self.max_spice, self.spice_amount + 1])
66 changes: 13 additions & 53 deletions examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import mesa

from .resource_agents import Spice, Sugar
from .resource_agents import Resource


# Helper function
Expand Down Expand Up @@ -50,47 +50,12 @@ def __init__(
self.prices = []
self.trade_partners = []

def get_sugar(self, pos):
"""
used in self.get_sugar_amount()
"""

this_cell = self.model.grid.get_cell_list_contents(pos)
for agent in this_cell:
if type(agent) is Sugar:
return agent
return None

def get_sugar_amount(self, pos):
"""
used in self.move() as part of self.calculate_welfare()
"""

sugar_patch = self.get_sugar(pos)
if sugar_patch:
return sugar_patch.amount
return 0

def get_spice(self, pos):
"""
used in self.get_spice_amount()
"""

def get_resource(self, pos):
this_cell = self.model.grid.get_cell_list_contents(pos)
for agent in this_cell:
if type(agent) is Spice:
if type(agent) is Resource:
return agent
return None

def get_spice_amount(self, pos):
"""
used in self.move() as part of self.calculate_welfare()
"""

spice_patch = self.get_spice(pos)
if spice_patch:
return spice_patch.amount
return 0
raise Exception(f"Resource agent not found in the position {pos}")

def get_trader(self, pos):
"""
Expand Down Expand Up @@ -292,8 +257,8 @@ def move(self):

welfares = [
self.calculate_welfare(
self.sugar + self.get_sugar_amount(pos),
self.spice + self.get_spice_amount(pos),
self.sugar + self.get_resource(pos).sugar_amount,
self.spice + self.get_resource(pos).spice_amount,
)
for pos in neighbors
]
Expand Down Expand Up @@ -323,20 +288,15 @@ def move(self):
self.model.grid.move_agent(self, final_candidate)

def eat(self):
# get sugar
sugar_patch = self.get_sugar(self.pos)

if sugar_patch:
self.sugar += sugar_patch.amount
sugar_patch.amount = 0
patch = self.get_resource(self.pos)
if patch.sugar_amount > 0:
self.sugar += patch.sugar_amount
patch.sugar_amount = 0
self.sugar -= self.metabolism_sugar

# get_spice
spice_patch = self.get_spice(self.pos)

if spice_patch:
self.spice += spice_patch.amount
spice_patch.amount = 0
if patch.spice_amount > 0:
self.spice += patch.spice_amount
patch.spice_amount = 0
self.spice -= self.metabolism_spice

def maybe_die(self):
Expand Down