Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.agent_count = agent_count
self.schedule = RandomActivation(self)
# Create agents
for i in range(self.agent_count):
x = self.random.randrange(self.width)
y = self.random.randrange(self.height)
a = WalkerAgent((x, y), self, True)
self.schedule.add(a)
self.grid.place_agent(a, (x, y))
def step(self):
self.schedule.step()
class WalkerWorldViz(TextVisualization):
'''
ASCII Visualization for a WalkerWorld agent.
Each cell is displayed as the number of agents currently in that cell.
'''
def __init__(self, model):
'''
Create a new visualization for a WalkerWorld instance.
args:
model: An instance of a WalkerWorld model.
'''
self.model = model
grid_viz = TextGrid(self.model.grid, None)
grid_viz.converter = lambda x: str(len(x))
self.elements = [grid_viz]
self.type = agent_type
def step(self, model):
similar = 0
for neighbor in model.grid.neighbor_iter(self.pos):
if neighbor.type == self.type:
similar += 1
# If unhappy, move:
if similar < model.homophily:
model.grid.move_to_empty(self)
else:
model.happy += 1
class SchellingTextVisualization(TextVisualization):
'''
ASCII visualization for schelling model
'''
def __init__(self, model):
'''
Create new Schelling ASCII visualization.
'''
self.model = model
grid_viz = TextGrid(self.model.grid, self.ascii_agent)
happy_viz = TextData(self.model, 'happy')
self.elements = [grid_viz, happy_viz]
@staticmethod
def ascii_agent(a):
'''
Visualization for the Wolf-Sheep Predation
'''
from WolfSheep import Wolf, Sheep
from mesa.visualization.TextVisualization import TextVisualization, TextGrid
class WolfSheepVisualization(TextVisualization):
'''
ASCII visualization of the WolfSheepPredation model.
Each cell displays S if only sheep, W if only wolves, or X if both.
(blank if none)
'''
def __init__(self, model):
self.model = model
grid_viz = TextGrid(self.model.grid, self.draw_cell)
self.elements = [grid_viz]
@staticmethod
def draw_cell(cell):
if len(cell) == 0:
return " "
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import CanvasGrid, ChartModule, TextElement
from mesa.visualization.UserParam import UserSettableParameter
from mesa.visualization.TextVisualization import (
TextData, TextGrid, TextVisualization
)
from model import Schelling
class SchellingTextVisualization(TextVisualization):
'''
ASCII visualization for schelling model
'''
def __init__(self, model):
'''
Create new Schelling ASCII visualization.
'''
self.model = model
grid_viz = TextGrid(self.model.grid, self.ascii_agent)
happy_viz = TextData(self.model, 'happy')
self.elements = [grid_viz, happy_viz]
@staticmethod
def ascii_agent(a):
from mesa.visualization.TextVisualization import (
TextData, TextGrid, TextVisualization
)
from model import Schelling
class SchellingTextVisualization(TextVisualization):
'''
ASCII visualization for schelling model
'''
def __init__(self, model):
'''
Create new Schelling ASCII visualization.
'''
self.model = model
grid_viz = TextGrid(self.model.grid, self.print_ascii_agent)
happy_viz = TextData(self.model, 'happy')
self.elements = [grid_viz, happy_viz]
@staticmethod
def print_ascii_agent(a):