-
Notifications
You must be signed in to change notification settings - Fork 840
/
server.py
60 lines (50 loc) · 2.67 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
53
54
55
56
57
58
59
60
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import CanvasGrid, ChartModule
from mesa.visualization.UserParam import UserSettableParameter
from wolf_sheep.agents import Wolf, Sheep, GrassPatch
from wolf_sheep.model import WolfSheepPredation
def wolf_sheep_portrayal(agent):
if agent is None:
return
portrayal = {}
if type(agent) is Sheep:
portrayal["Shape"] = "wolf_sheep/resources/sheep.png"
# https://icons8.com/web-app/433/sheep
portrayal["scale"] = 0.9
portrayal["Layer"] = 1
elif type(agent) is Wolf:
portrayal["Shape"] = "wolf_sheep/resources/wolf.png"
# https://icons8.com/web-app/36821/German-Shepherd
portrayal["scale"] = 0.9
portrayal["Layer"] = 2
portrayal["text"] = round(agent.energy, 1)
portrayal["text_color"] = "White"
elif type(agent) is GrassPatch:
if agent.fully_grown:
portrayal["Color"] = "#00AA00"
else:
portrayal["Color"] = "#D6F5D6"
portrayal["Shape"] = "rect"
portrayal["Filled"] = "true"
portrayal["Layer"] = 0
portrayal["w"] = 1
portrayal["h"] = 1
return portrayal
canvas_element = CanvasGrid(wolf_sheep_portrayal, 20, 20, 500, 500)
chart_element = ChartModule([{"Label": "Wolves", "Color": "#AA0000"},
{"Label": "Sheep", "Color": "#666666"}])
server = ModularServer(WolfSheepPredation, [canvas_element, chart_element],
"Wolf Sheep Predation",
model_params=dict(
grass=UserSettableParameter('checkbox', 'Grass Enabled', True),
grass_regrowth_time=UserSettableParameter('slider', 'Grass Regrowth Time', 20, 1, 50),
initial_sheep=UserSettableParameter('slider', 'Initial Sheep Population', 100, 10, 300),
sheep_reproduce=UserSettableParameter('slider', 'Sheep Reproduction Rate', 0.04, 0.01, 1.0, 0.01),
initial_wolves=UserSettableParameter('slider', 'Initial Wolf Population', 50, 10, 300),
wolf_reproduce=UserSettableParameter('slider', 'Wolf Reproduction Rate', 0.05, 0.01, 1.0, 0.01,
description="The rate at which wolf agents reproduce."),
wolf_gain_from_food=UserSettableParameter('slider', 'Wolf Gain From Food Rate', 20, 1, 50),
sheep_gain_from_food=UserSettableParameter('slider', 'Sheep Gain From Food', 4, 1, 10)
)
)
# server.launch()