Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, height=50, width=50):
'''
Create a new playing area of (height, width) cells.
'''
# Set up the grid and schedule.
# Use SimultaneousActivation which simulates all the cells
# computing their next state simultaneously. This needs to
# be done because each cell's next state depends on the current
# state of all its neighbors -- before they've changed.
self.schedule = SimultaneousActivation(self)
# Use a hexagonal grid, where edges wrap around.
self.grid = HexGrid(height, width, torus=True)
# Place a dead cell at each location.
for (contents, x, y) in self.grid.coord_iter():
cell = Cell((x, y), self)
self.grid.place_agent(cell, (x, y))
self.schedule.add(cell)
# activate the center(ish) cell.
centerishCell = self.grid[width // 2][height // 2]
centerishCell.state = 1
for a in centerishCell.neighbors:
a.isConsidered = True
def setUp(self):
self.user_params = {
'width': 1,
'height': 1,
'key1': UserSettableParameter('number', "Test Parameter", 101),
'key2': UserSettableParameter('slider', "Test Parameter", 200, 0, 300, 10)
}
self.viz_elements = [
CanvasGrid(self.portrayal, 10, 10, 20, 20),
TextElement(),
# ChartModule([{"Label": "Wolves", "Color": "#AA0000"}, # Todo - test chart module
# {"Label": "Sheep", "Color": "#666666"}])
]
self.server = ModularServer(MockModel, self.viz_elements, "Test Model", model_params=self.user_params)
def test_user_params(self):
print(self.server.user_params)
assert self.server.user_params == {
'key1': UserSettableParameter('number', "Test Parameter", 101).json,
'key2': UserSettableParameter('slider', "Test Parameter", 200, 0, 300, 10).json
}
"""
Test the BatchRunner
"""
from functools import reduce
from operator import mul
import unittest
from mesa import Agent, Model
from mesa.time import BaseScheduler
from mesa.batchrunner import BatchRunner, ParameterProduct, ParameterSampler
NUM_AGENTS = 7
class MockAgent(Agent):
"""
Minimalistic agent implementation for testing purposes
"""
def __init__(self, unique_id, model, val):
super().__init__(unique_id, model)
self.unique_id = unique_id
self.val = val
def step(self):
self.val += 1
class MockModel(Model):
"""
Minimalistic model for testing purposes
"""
from mesa.time import RandomActivation
from mesa.visualization.TextVisualization import TextVisualization, TextGrid
from wolf_sheep.random_walk import RandomWalker
class WalkerAgent(RandomWalker):
'''
Agent which only walks around.
'''
def step(self):
self.random_move()
class WalkerWorld(Model):
'''
Random walker world.
'''
height = 10
width = 10
def __init__(self, height, width, agent_count):
'''
Create a new WalkerWorld.
Args:
height, width: World size.
agent_count: How many agents to create.
'''
self.height = height
self.width = width
class MockAgent(Agent):
"""
Minimalistic agent implementation for testing purposes
"""
def __init__(self, unique_id, model, val):
super().__init__(unique_id, model)
self.unique_id = unique_id
self.val = val
def step(self):
self.val += 1
class MockModel(Model):
"""
Minimalistic model for testing purposes
"""
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_agents(self):
for i in range(self.n_agents):
import unittest
from mesa.time import RandomActivation
from mesa.datacollection import DataCollector
from mesa import Model, Agent
import numpy as np
class LifeTimeModel(Model):
'''Simple model for running models with a finite life'''
def __init__(self, agent_lifetime=1, n_agents=10):
super().__init__()
self.agent_lifetime = agent_lifetime
self.n_agents = n_agents
# keep track of the the remaining life of an agent and
# how many ticks it has seen
self.datacollector = DataCollector(
agent_reporters={"remaining_life": lambda a: a.remaining_life,
"steps": lambda a: a.steps})
self.current_ID = 0
self.schedule = RandomActivation(self)
self.advances = 0
def stage_one(self):
self.model.log.append(self.unique_id + "_1")
def stage_two(self):
self.model.log.append(self.unique_id + "_2")
def advance(self):
self.advances += 1
def step(self):
self.steps += 1
class MockModel(Model):
def __init__(self, shuffle=False, activation=STAGED):
'''
Creates a Model instance with a schedule
Args:
shuffle (Bool): whether or not to instantiate a scheduler
with shuffling.
This option is only used for
StagedActivation schedulers.
activation (str): which kind of scheduler to use.
'random' creates a RandomActivation scheduler.
'staged' creates a StagedActivation scheduler.
The default scheduler is a BaseScheduler.
'''
self.log = []
def get_app(self):
app = ModularServer(Model, [])
return app
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"]})