Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
from mesa_geo.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import ChartModule, TextElement
from mesa.visualization.UserParam import UserSettableParameter
from model import SchellingModel
from mesa_geo.visualization.MapModule import MapModule
class HappyElement(TextElement):
"""
Display a text count of how many happy agents there are.
"""
def __init__(self):
pass
def render(self, model):
return "Happy agents: " + str(model.happy)
model_params = {
"density": UserSettableParameter("slider", "Agent density", 0.6, 0.1, 1.0, 0.1),
"minority_pc": UserSettableParameter(
"slider", "Fraction minority", 0.2, 0.00, 1.0, 0.05
),
'target': target,
'color': edge_color(*get_agents(source, target)),
'width': edge_width(*get_agents(source, target)),
}
for (source, target) in G.edges]
return portrayal
network = NetworkModule(network_portrayal, 500, 500, library='d3')
chart = ChartModule([{'Label': 'Infected', 'Color': '#FF0000'},
{'Label': 'Susceptible', 'Color': '#008000'},
{'Label': 'Resistant', 'Color': '#808080'}])
class MyTextElement(TextElement):
def render(self, model):
ratio = model.resistant_susceptible_ratio()
ratio_text = '∞' if ratio is math.inf else '{0:.2f}'.format(ratio)
infected_text = str(number_infected(model))
return "Resistant/Susceptible Ratio: {}<br>Infected Remaining: {}".format(ratio_text, infected_text)
model_params = {
'num_nodes': UserSettableParameter('slider', 'Number of agents', 10, 10, 100, 1,
description='Choose how many agents to include in the model'),
'avg_node_degree': UserSettableParameter('slider', 'Avg Node Degree', 3, 3, 8, 1,
description='Avg Node Degree'),
'initial_outbreak_size': UserSettableParameter('slider', 'Initial Outbreak Size', 1, 1, 10, 1,
description='Initial Outbreak Size'),
'virus_spread_chance': UserSettableParameter('slider', 'Virus Spread Chance', 0.4, 0.0, 1.0, 0.1,
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):
'''
Minority agents are X, Majority are O.
'''
if a.type == 0:
return 'O'
if a.type == 1:
return 'X'
class HappyElement(TextElement):
'''
Display a text count of how many happy agents there are.
'''
def __init__(self):
pass
def render(self, model):
return "Happy agents: " + str(model.happy)
def schelling_draw(agent):
'''
Portrayal Method for canvas
'''
if agent is None:
from Schelling import SchellingModel
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import CanvasGrid, ChartModule, TextElement
from mesa.visualization.UserParams import UserParam
class HappyElement(TextElement):
'''
Display a text count of how many happy agents there are.
'''
def __init__(self):
pass
def render(self, model):
return "Happy agents: " + str(model.happy)
def schelling_draw(agent):
'''
Portrayal Method for canvas
'''
if agent is None:
return