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 __init__(self, width, height, key1=103, key2=104):
self.width = width
self.height = height
self.key1 = key1,
self.key2 = key2
self.schedule = SimultaneousActivation(self)
self.grid = Grid(width, height, torus=True)
for (c, x, y) in self.grid.coord_iter():
a = MockAgent(x + y * 100, self, x * y * 3)
self.grid.place_agent(a, (x, y))
self.schedule.add(a)
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 = []
# 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, 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 simple grid, where edges wrap around.
self.grid = Grid(height, width, torus=True)
# Place a cell at each location, with some initialized to
# ALIVE and some to DEAD.
for (contents, x, y) in self.grid.coord_iter():
cell = Cell((x, y), self)
if random() < .1:
cell.state = cell.ALIVE
self.grid.place_agent(cell, (x, y))
self.schedule.add(cell)
self.running = True
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:
height, width: Grid size. There will be one agent per grid cell.
schedule_type: Can be "Sequential", "Random", or "Simultaneous".
def __init__(self, width=20, height=20):
'''
Create a 2D lattice with strict borders where agents live
The agents next state is first determined before updating the grid
'''
self._grid = Grid(width, height, torus=False)
self._schedule = SimultaneousActivation(self)
# self._grid.coord_iter()
# --> should really not return content + col + row
# -->but only col & row
# for (contents, col, row) in self._grid.coord_iter():
# replaced content with _ to appease linter
for (_, row, col) in self._grid.coord_iter():
cell = ColorCell((row, col), self,
ColorCell.OPINIONS[random.randrange(0, 16)])
self._grid.place_agent(cell, (row, col))
self._schedule.add(cell)
self.running = True