-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
142 lines (127 loc) · 5.29 KB
/
Copy pathserver.py
File metadata and controls
142 lines (127 loc) · 5.29 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File: server.py
# Authors: Artjom Plaunov and Daniel Mallia
# Class: Modeling and Simulation (CSCI 74000)
# Professor: Professor Vazquez-Abad
# Assignment: Final Project
# Description: This file contains the code necessary to run the simulation in
# interactive and visual fashion in the browser.
# Run: python3 server.py
import argparse, json
from CatModel import *
from Utilities import get_mesa_visualization_element
# Function to define how each agent should be rendered
def agent_portrayal(agent):
# Uses modified versions of the image from:
# https://www.flaticon.com/free-icon/cat_220124
if isinstance(agent, CatAgent):
img_file = "Images/cat_male.PNG" if agent.sex else \
"Images/cat_female.PNG"
portrayal = {
"Shape" : img_file,
"Layer" : 1,
"scale" : 0.7
}
if isinstance(agent, HouseAgent):
portrayal = { # Basic house rendering
"Shape" : "rect",
"Color" : ["#00EEBB"],
"Layer" : 0,
"w" : 0.8,
"h" : 0.8,
"Filled" : "false",
}
# Modify color if house puts out food
if agent.puts_food:
portrayal["Color"] = "#00EEEE"
if agent.food: # Put text if there is food there now
portrayal["text"] = "F"
portrayal["text_color"] = "black"
if isinstance(agent, StreetAgent):
portrayal = {
"Shape" : "rect",
"Color" : ["#B8B8B8"],
"Layer" : 0,
"w" : 0.8,
"h" : 0.8,
"Filled" : "false",
}
if isinstance(agent, BackyardAgent):
portrayal = {
"Shape" : "rect",
"Color" : ["#84e184", "#adebad", "#d6f5d6"],
"Layer" : 1 ,
"w" : 0.8,
"h" : 0.8,
"Filled" : "true",
}
if isinstance(agent, ShopAgent):
portrayal = {
"Shape" : "rect",
"Color" : "green",
"Layer" : 0,
"w" : 0.8,
"h" : 0.8,
"Filled" : "false",
}
# Uses image from:
# https://www.clipartmax.com/png/middle/30-303462_rat-icon-enviropest-mouse.png
if isinstance(agent, RestaurantAgent):
portrayal = {
"Shape" : "Images/Mouse.png",
"Layer" : 0,
"scale" : 0.5,
}
return portrayal
if __name__ == "__main__":
# Read in JSON file with simulation parameters
with open("simulation_params.json", "r") as f:
sim_params = json.load(f)
parser = argparse.ArgumentParser(
description="Run interactive Cat ABM simulation")
parser.add_argument("--hunger_chart", action="store_true")
parser.add_argument("--mice_pop_chart", action="store_true")
parser.add_argument("--max_hunger_chart", action="store_true")
parser.add_argument("--cat_pop_chart", action="store_true")
parser.add_argument("--cat_pregnancies_chart", action="store_true")
parser.add_argument("--cats_hit_chart", action="store_true")
parser.add_argument("--cats_removed_under_policy_chart", action="store_true")
parser.add_argument("--cat_fights_chart", action="store_true")
parser.add_argument("--all_charts", action="store_true")
parser.add_argument("--grid_px_width", type=int, default=1000,
help="Width of the grid display in pixels")
parser.add_argument("--grid_px_height", type=int, default=1000,
help="Height of the grid display in pixels")
args = parser.parse_args()
model_parameters = {
k : get_mesa_visualization_element(sim_params,k) for k in sim_params}
grid = mesa.visualization.CanvasGrid(agent_portrayal, GRID_WIDTH,
GRID_HEIGHT, args.grid_px_width, args.grid_px_height)
charts=[]
if args.all_charts or args.hunger_chart:
charts.append(mesa.visualization.ChartModule(
[{"Label" : "Hunger", "Color" : "Black"}]))
if args.all_charts or args.mice_pop_chart:
charts.append(mesa.visualization.ChartModule(
[{"Label" : "Mice Pop.", "Color" : "Black"}]))
if args.all_charts or args.max_hunger_chart:
charts.append(mesa.visualization.ChartModule(
[{"Label" : "Max Hunger", "Color" : "Black"}]))
if args.all_charts or args.cat_pop_chart:
charts.append(mesa.visualization.ChartModule(
[{"Label" : "Cat Pop.", "Color" : "Black"}]))
if args.all_charts or args.cat_pregnancies_chart:
charts.append(mesa.visualization.ChartModule(
[{"Label" : "Cats Pregnant", "Color" : "Black"}]))
if args.all_charts or args.cats_hit_chart:
charts.append(mesa.visualization.ChartModule(
[{"Label" : "Cats Hit", "Color" : "Black"}]))
if args.all_charts or args.cats_removed_under_policy_chart:
charts.append(mesa.visualization.ChartModule(
[{"Label" : "Cats Removed", "Color" : "Black"}]))
if args.all_charts or args.cat_fights_chart:
charts.append(mesa.visualization.ChartModule(
[{"Label" : "Cat Fights", "Color" : "Black"}]))
elements = [grid] + charts
server = mesa.visualization.ModularServer(
CatModel, elements, "Cat Model", model_parameters)
server.launch()