Skip to content

Commit

Permalink
Merge pull request projectmesa#510 from projectmesa/schedule.agent.dict
Browse files Browse the repository at this point in the history
Schedule.agent.dict
  • Loading branch information
jackiekazil authored May 14, 2018
2 parents 1505f16 + a3668ae commit 644a992
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 95 deletions.
70 changes: 51 additions & 19 deletions examples/forest_fire/Forest Fire Model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import random\n",
Expand Down Expand Up @@ -57,7 +59,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class TreeCell(Agent):\n",
Expand Down Expand Up @@ -113,7 +117,9 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class ForestFire(Model):\n",
Expand Down Expand Up @@ -187,7 +193,9 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fire = ForestFire(100, 100, 0.6)"
Expand All @@ -203,7 +211,9 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fire.run_model()"
Expand All @@ -223,7 +233,9 @@
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"results = fire.dc.get_model_vars_dataframe()"
Expand All @@ -239,7 +251,9 @@
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -278,7 +292,9 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -324,7 +340,9 @@
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fixed_params = dict(height=50, # Height and width are constant\n",
Expand All @@ -336,7 +354,9 @@
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# At the end of each model run, calculate the fraction of trees which are Burned Out\n",
Expand All @@ -347,7 +367,9 @@
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create the batch runner\n",
Expand All @@ -365,7 +387,9 @@
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
Expand All @@ -389,7 +413,9 @@
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = param_run.get_model_vars_dataframe()"
Expand All @@ -398,7 +424,9 @@
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -489,7 +517,9 @@
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -531,7 +561,9 @@
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
Expand Down Expand Up @@ -574,9 +606,9 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [mesa_dev]",
"display_name": "Python 3",
"language": "python",
"name": "Python [mesa_dev]"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -588,7 +620,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
"version": "3.4.2"
},
"widgets": {
"state": {},
Expand Down
23 changes: 10 additions & 13 deletions examples/sugarscape_cg/sugarscape/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ class RandomActivationByBreed(RandomActivation):
Assumes that all agents have a step() method.
'''
agents_by_breed = defaultdict(list)

def __init__(self, model):
super().__init__(model)
self.agents_by_breed = defaultdict(list)
self.agents_by_breed = defaultdict(dict)

def add(self, agent):
'''
Expand All @@ -28,21 +27,19 @@ def add(self, agent):
agent: An Agent to be added to the schedule.
'''

self.agents.append(agent)
self._agents[agent.unique_id] = agent
agent_class = type(agent)
self.agents_by_breed[agent_class].append(agent)
self.agents_by_breed[agent_class][agent.unique_id] = agent

def remove(self, agent):
'''
Remove all instances of a given agent from the schedule.
'''

while agent in self.agents:
self.agents.remove(agent)
del self._agents[agent.unique_id]

agent_class = type(agent)
while agent in self.agents_by_breed[agent_class]:
self.agents_by_breed[agent_class].remove(agent)
del self.agents_by_breed[agent_class][agent.unique_id]

def step(self, by_breed=True):
'''
Expand All @@ -67,13 +64,13 @@ def step_breed(self, breed):
Args:
breed: Class object of the breed to run.
'''
agents = self.agents_by_breed[breed]
random.shuffle(agents)
for agent in agents:
agent.step()
agent_keys = list(self.agents_by_breed[breed].keys())
random.shuffle(agent_keys)
for agent_key in agent_keys:
self.agents_by_breed[breed][agent_key].step()

def get_breed_count(self, breed_class):
'''
Returns the current number of agents of certain breed in the queue.
'''
return len(self.agents_by_breed[breed_class])
return len(self.agents_by_breed[breed_class].values())
19 changes: 11 additions & 8 deletions examples/wolf_sheep/wolf_sheep/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Sheep(RandomWalker):

energy = None

def __init__(self, pos, model, moore, energy=None):
super().__init__(pos, model, moore=moore)
def __init__(self, unique_id, pos, model, moore, energy=None):
super().__init__(unique_id, pos, model, moore=moore)
self.energy = energy

def step(self):
Expand Down Expand Up @@ -47,7 +47,8 @@ def step(self):
# Create a new sheep:
if self.model.grass:
self.energy /= 2
lamb = Sheep(self.pos, self.model, self.moore, self.energy)
lamb = Sheep(self.model.next_id(), self.pos, self.model,
self.moore, self.energy)
self.model.grid.place_agent(lamb, self.pos)
self.model.schedule.add(lamb)

Expand All @@ -59,8 +60,8 @@ class Wolf(RandomWalker):

energy = None

def __init__(self, pos, model, moore, energy=None):
super().__init__(pos, model, moore=moore)
def __init__(self, unique_id, pos, model, moore, energy=None):
super().__init__(unique_id, pos, model, moore=moore)
self.energy = energy

def step(self):
Expand All @@ -87,7 +88,8 @@ def step(self):
if random.random() < self.model.wolf_reproduce:
# Create a new wolf cub
self.energy /= 2
cub = Wolf(self.pos, self.model, self.moore, self.energy)
cub = Wolf(self.model.next_id(), self.pos, self.model,
self.moore, self.energy)
self.model.grid.place_agent(cub, cub.pos)
self.model.schedule.add(cub)

Expand All @@ -97,17 +99,18 @@ class GrassPatch(Agent):
A patch of grass that grows at a fixed rate and it is eaten by sheep
'''

def __init__(self, pos, model, fully_grown, countdown):
def __init__(self, unique_id, pos, model, fully_grown, countdown):
'''
Creates a new patch of grass
Args:
grown: (boolean) Whether the patch of grass is fully grown or not
countdown: Time for the patch of grass to be fully grown again
'''
super().__init__(pos, model)
super().__init__(unique_id, model)
self.fully_grown = fully_grown
self.countdown = countdown
self.pos = pos

def step(self):
if not self.fully_grown:
Expand Down
9 changes: 5 additions & 4 deletions examples/wolf_sheep/wolf_sheep/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, height=20, width=20,
once it is eaten
sheep_gain_from_food: Energy sheep gain from grass, if enabled.
'''

super().__init__()
# Set parameters
self.height = height
self.width = width
Expand All @@ -86,7 +86,7 @@ def __init__(self, height=20, width=20,
x = random.randrange(self.width)
y = random.randrange(self.height)
energy = random.randrange(2 * self.sheep_gain_from_food)
sheep = Sheep((x, y), self, True, energy)
sheep = Sheep(self.next_id(), (x, y), self, True, energy)
self.grid.place_agent(sheep, (x, y))
self.schedule.add(sheep)

Expand All @@ -95,7 +95,7 @@ def __init__(self, height=20, width=20,
x = random.randrange(self.width)
y = random.randrange(self.height)
energy = random.randrange(2 * self.wolf_gain_from_food)
wolf = Wolf((x, y), self, True, energy)
wolf = Wolf(self.next_id(), (x, y), self, True, energy)
self.grid.place_agent(wolf, (x, y))
self.schedule.add(wolf)

Expand All @@ -110,7 +110,8 @@ def __init__(self, height=20, width=20,
else:
countdown = random.randrange(self.grass_regrowth_time)

patch = GrassPatch((x, y), self, fully_grown, countdown)
patch = GrassPatch(self.next_id(), (x, y), self,
fully_grown, countdown)
self.grid.place_agent(patch, (x, y))
self.schedule.add(patch)

Expand Down
4 changes: 2 additions & 2 deletions examples/wolf_sheep/wolf_sheep/random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class RandomWalker(Agent):
y = None
moore = True

def __init__(self, pos, model, moore=True):
def __init__(self, unique_id, pos, model, moore=True):
'''
grid: The MultiGrid object in which the agent lives.
x: The agent's current x coordinate
y: The agent's current y coordinate
moore: If True, may move in all 8 directions.
Otherwise, only up, down, left, right.
'''
super().__init__(pos, model)
super().__init__(unique_id, model)
self.pos = pos
self.moore = moore

Expand Down
Loading

0 comments on commit 644a992

Please sign in to comment.