-
Notifications
You must be signed in to change notification settings - Fork 840
/
server.py
52 lines (40 loc) · 1.51 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import CanvasGrid, ChartModule, TextElement
from mesa.visualization.UserParam import UserSettableParameter
from model import Schelling
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
portrayal = {"Shape": "circle", "r": 0.5, "Filled": "true", "Layer": 0}
if agent.type == 0:
portrayal["Color"] = ["#FF0000", "#FF9999"]
portrayal["stroke_color"] = "#00FF00"
else:
portrayal["Color"] = ["#0000FF", "#9999FF"]
portrayal["stroke_color"] = "#000000"
return portrayal
happy_element = HappyElement()
canvas_element = CanvasGrid(schelling_draw, 20, 20, 500, 500)
happy_chart = ChartModule([{"Label": "happy", "Color": "Black"}])
model_params = {
"height": 20,
"width": 20,
"density": UserSettableParameter("slider", "Agent density", 0.8, 0.1, 1.0, 0.1),
"minority_pc": UserSettableParameter("slider", "Fraction minority", 0.2, 0.00, 1.0, 0.05),
"homophily": UserSettableParameter("slider", "Homophily", 3, 0, 8, 1)
}
server = ModularServer(Schelling,
[canvas_element, happy_element, happy_chart],
"Schelling", model_params)
server.launch()