forked from popabczhang/pud-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kendall_server.py
124 lines (107 loc) · 3.8 KB
/
kendall_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
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
import mesa
from agent.kendall_agents import Floor, Building, Project, Resident
from model.kendall_model import Kendall
from shapely.geometry import mapping
from flask import Flask,jsonify,request
from flask_cors import CORS
import os,sys
from munch import Munch
from util import global_config
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(dir_path)
app = Flask(__name__)
CORS(app)
model = None
#Agent Style
def get_agent_property(agent):
properties = {}
if isinstance(agent, Floor):
properties["category"] = agent.Category
properties["floor"] = agent.floor
properties["area"] = agent.area
properties["bld"] = agent.bld
if isinstance(agent, Project):
properties["unique_id"] = agent.unique_id
properties["status"] = agent.status
if hasattr(agent,"round"):
properties["develop_round"] = agent.round
if hasattr(agent,"profit"):
properties["profit"] = agent.profit
properties["endowment"] = agent.endowment
if hasattr(agent,"building_plan"):
properties["building_plan"] = agent.building_plan
if hasattr(agent,"demand_gap"):
properties["demand_gap"] = agent.demand_gap
return properties
def get_agent_geometry(agent):
if agent.render:
transformed_geometry = agent.get_transformed_geometry(
model.space.transformer
)
return mapping(transformed_geometry)
def get_geojson(model,geometry_method=get_agent_geometry,properties_method=get_agent_property):
floor_data = {"type": "FeatureCollection", "features": []}
project_data = []
path_data = []
for agent in model.agents[Floor]:
properties = properties_method(agent)
geometry = geometry_method(agent)
data = {
"type": "Feature",
"geometry": geometry,
"properties": properties,
"new" : agent.new,
"is_project" : agent.is_project
}
floor_data["features"].append(data)
num_built_project = 0
stop = False
for agent in model.agents[Project]:
properties = properties_method(agent)
geometry = geometry_method(agent)
project_data.append({
"coordinates":geometry["coordinates"],
"properties": properties,
})
num_built_project += int(agent.status == 'built')
if num_built_project == len(model.agents[Project]):
stop = True
for agent in model.agents[Resident]:
transformed_path = [model.space.transformer.transform(x, y) for x, y in agent.my_path]
path_data.append({"path":transformed_path,"timestamps":[i for i in range(len(transformed_path))]})
return jsonify({'floor_data':floor_data,
'project_data':project_data,
'path_data':path_data,
'collected_data':model.datacollector.data,
'config_data': model.config,
'stop':stop,
})
@app.route('/init')
def init():
return get_geojson(model)
@app.route('/step')
def step():
global model
model.step()
return get_geojson(model)
@app.route('/reset',methods=['POST','GET'])
def reset():
global model
if request.method == 'POST':
config = request.json
config = Munch.fromDict(config)
model = Kendall(config=config)
else:
model = Kendall(config=global_config)
model.step()
return get_geojson(model)
if __name__ == '__main__':
model = Kendall(config=global_config)
print("Model loaded")
app.run(host='0.0.0.0',debug=True, port=5001, use_reloader=False)
# import time
# for i in range(100):
# start = time.time()
# model.step()
# stop = time.time()
# print(stop-start)