-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (101 loc) · 3.79 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Importamos el modelo y los agentes
from MultiCleaningSystem.Model.Model import Office
from MultiCleaningSystem.Ugraph.Ugraph import dijkstra, BreadthFirstSearch
# Librerias para mandar informacion al servidor
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging # To log messages ( DEBUG | INFO | WARNING | ERROR | CRITICAL )
from sys import argv
# Importamos matplotlib y seaborn para poder visualizar los resultados
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rcParams["animation.html"] = "jshtml"
matplotlib.rcParams['animation.embed_limit'] = 2**128
width = 0
height = 0
office = []
stage = 0 # 0:Start | 1:Exploración y Recolección
flag = True
with open('Tests/input3.txt', 'r') as input: # Abriendo el mapa
for linea in input:
if flag:
width, height = [int(num) for num in linea.split(" ")]
flag = False
else:
office.append(linea.strip().split(" "))
model = Office(width, height, office) # Inicializamos el modelo
class Server(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200) # Success status
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_response()
self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
def do_POST(self):
global model
global stage
global office
modelEnv = None
if stage != 0: # Unity update
model.step()
modelEnv = model.environment
else: # Unity start -> No steps
stage = 1
modelEnv = office
mapa = ""
for row in range( len(modelEnv) ):
for col in range( len(modelEnv[row]) ):
if col < len(modelEnv[row]) - 1:
mapa += str(modelEnv[row][col]).strip() + "*"
else:
mapa += str(modelEnv[row][col]).strip()
if row < len(modelEnv) - 1:
mapa += ","
pos = ""
for elem in range( len(model.robots_positions) ):
pos += str(model.robots_positions[elem][0]) + "*" + str(model.robots_positions[elem][1])
if elem < len(model.robots_positions) - 1:
pos += ","
info = {
"width": model.grid.width,
"height": model.grid.height,
"cells": model.cells,
"garbage": model.garbage,
"robots": len(model.robots_positions),
"positions": pos, #model.robots_positions,
"steps": model.steps,
"environment" : mapa
}
self._set_response()
self.wfile.write(str(info).encode('utf-8'))
def run(server_class = HTTPServer, handler_class = Server, port = 8585):
logging.basicConfig(level = logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info("Starting httpd...\n") # HTTPD is HTTP Daemon!
try:
httpd.serve_forever()
except KeyboardInterrupt: # CTRL+C stops the server
pass
httpd.server_close()
logging.info("Stopping httpd...\n")
if len(argv) == 2:
run(port = int(argv[1])) # Receives port
else:
run() # Default port
"""
while not model.clean:
model.step()
# Visualizacion
all_grid = model.datacollector.get_model_vars_dataframe() # Arreglo de matrices
fig, axis = plt.subplots(figsize= (width, height))
axis.set_xticks([])
axis.set_yticks([])
patch = plt.imshow(all_grid.iloc[0][0], cmap=sns.color_palette("Paired", as_cmap=True))
def animate(i):
patch.set_data(all_grid.iloc[i][0])
anim = animation.FuncAnimation(fig, animate, frames = model.steps)
plt.show()
"""