Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.val2 += 1
def write_final_values(self):
'''
Write the final value to the appropriate table.
'''
row = {"agent_id": self.unique_id, "final_value": self.val}
self.model.datacollector.add_table_row("Final_Values", row)
class MockModel(Model):
'''
Minimalistic model for testing purposes.
'''
schedule = BaseScheduler(None)
def __init__(self):
self.schedule = BaseScheduler(self)
self.model_val = 100
for i in range(10):
a = MockAgent(i, self, val=i)
self.schedule.add(a)
self.datacollector = DataCollector(
{"total_agents": lambda m: m.schedule.get_agent_count(),
"model_value": "model_val"},
{"value": lambda a: a.val, "value2": "val2"},
{"Final_Values": ["agent_id", "final_value"]})
def step(self):
self.schedule.step()
'staged' creates a StagedActivation scheduler.
The default scheduler is a BaseScheduler.
'''
self.log = []
# Make scheduler
if activation == STAGED:
model_stages = ["stage_one", "stage_two"]
self.schedule = StagedActivation(self, model_stages,
shuffle=shuffle)
elif activation == RANDOM:
self.schedule = RandomActivation(self)
elif activation == SIMULTANEOUS:
self.schedule = SimultaneousActivation(self)
else:
self.schedule = BaseScheduler(self)
# Make agents
for name in ["A", "B"]:
agent = MockAgent(name, self)
self.schedule.add(agent)
def __init__(self, variable_model_param, variable_agent_param,
fixed_model_param=None, schedule=None, **kwargs):
super().__init__()
self.schedule = BaseScheduler(None) if schedule is None else schedule
self.variable_model_param = variable_model_param
self.variable_agent_param = variable_agent_param
self.fixed_model_param = fixed_model_param
self.n_agents = kwargs.get('n_agents', NUM_AGENTS)
self.running = True
self.init_agents()
def __init__(self):
self.schedule = BaseScheduler(self)
self.model_val = 100
for i in range(10):
a = MockAgent(i, self, val=i)
self.schedule.add(a)
self.datacollector = DataCollector(
{"total_agents": lambda m: m.schedule.get_agent_count(),
"model_value": "model_val"},
{"value": lambda a: a.val, "value2": "val2"},
{"Final_Values": ["agent_id", "final_value"]})
if end_date is not None:
params['end_date'] = end_date
if stay_prob_genuine is not None:
params['stay_prob'][0] = stay_prob_genuine
if stay_prob_fraud is not None:
params['stay_prob'][1] = stay_prob_fraud
if seed is not None:
params['seed'] = seed
if random_schedule:
self.model = TransactionModel(params)
else:
self.model = TransactionModel(params, scheduler=BaseScheduler(None))
self.params = params
self.aggregate_feature_constructor = None
self.apate_graph_feature_constructor = None
step() activates the agent and stages any necessary changes, but does not
apply them yet. advance() then applies the changes.
"""
def step(self):
""" Step all agents, then advance them. """
agent_keys = list(self._agents.keys())
for agent_key in agent_keys:
self._agents[agent_key].step()
for agent_key in agent_keys:
self._agents[agent_key].advance()
self.steps += 1
self.time += 1
class StagedActivation(BaseScheduler):
""" A scheduler which allows agent activation to be divided into several
stages instead of a single `step` method. All agents execute one stage
before moving on to the next.
Agents must have all the stage methods implemented. Stage methods take a
model object as their only argument.
This schedule tracks steps and time separately. Time advances in fractional
increments of 1 / (# of stages), meaning that 1 step = 1 unit of time.
"""
def __init__(self, model, stage_list=None, shuffle=False,
shuffle_between_stages=False):
""" Create an empty Staged Activation schedule.
Args:
def agent_buffer(self, shuffled=False):
""" Simple generator that yields the agents while letting the user
remove and/or add agents during stepping.
"""
agent_keys = list(self._agents.keys())
if shuffled:
self.model.random.shuffle(agent_keys)
for key in agent_keys:
if key in self._agents:
yield self._agents[key]
class RandomActivation(BaseScheduler):
""" A scheduler which activates each agent once per step, in random order,
with the order reshuffled every step.
This is equivalent to the NetLogo 'ask agents...' and is generally the
default behavior for an ABM.
Assumes that all agents have a step(model) method.
"""
def step(self):
""" Executes the step of all agents, one at a time, in
random order.
"""
for agent in self.agent_buffer(shuffled=True):
agent.step()
Assumes that all agents have a step(model) method.
"""
def step(self):
""" Executes the step of all agents, one at a time, in
random order.
"""
for agent in self.agent_buffer(shuffled=True):
agent.step()
self.steps += 1
self.time += 1
class SimultaneousActivation(BaseScheduler):
""" A scheduler to simulate the simultaneous activation of all the agents.
This scheduler requires that each agent have two methods: step and advance.
step() activates the agent and stages any necessary changes, but does not
apply them yet. advance() then applies the changes.
"""
def step(self):
""" Step all agents, then advance them. """
agent_keys = list(self._agents.keys())
for agent_key in agent_keys:
self._agents[agent_key].step()
for agent_key in agent_keys:
self._agents[agent_key].advance()
self.steps += 1
self.time += 1
from mesa import Model
from mesa.time import BaseScheduler, RandomActivation, SimultaneousActivation
from mesa.space import SingleGrid
from mesa.datacollection import DataCollector
from .agent import PDAgent
class PdGrid(Model):
''' Model class for iterated, spatial prisoner's dilemma model. '''
schedule_types = {"Sequential": BaseScheduler,
"Random": RandomActivation,
"Simultaneous": SimultaneousActivation}
# This dictionary holds the payoff for this agent,
# keyed on: (my_move, other_move)
payoff = {("C", "C"): 1,
("C", "D"): 0,
("D", "C"): 1.6,
("D", "D"): 0}
def __init__(self, height=50, width=50, schedule_type="Random", payoffs=None):
'''
Create a new Spatial Prisoners' Dilemma Model.
Args: