Upload Ejecucion.py
Browse files- pages/Ejecucion.py +108 -0
pages/Ejecucion.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from pages.vrp.Origen import Grafo, Vehiculos
|
4 |
+
from pages.vrp.Algoritmo_gen茅tico import Algoritmo_Genetico
|
5 |
+
|
6 |
+
#######################
|
7 |
+
## FUNCIONES AUXILIARES
|
8 |
+
#######################
|
9 |
+
|
10 |
+
def grafar(reparto : list[list[int]]):
|
11 |
+
|
12 |
+
result = "digraph { \n"
|
13 |
+
|
14 |
+
for ruta in reparto:
|
15 |
+
result += "Almac茅n ->"
|
16 |
+
for nodo in ruta:
|
17 |
+
result += str(nodo) + "\n"
|
18 |
+
result += str(nodo) + "->"
|
19 |
+
result += "Almac茅n \n"
|
20 |
+
|
21 |
+
result += "}"
|
22 |
+
|
23 |
+
return result
|
24 |
+
|
25 |
+
def presentar(reparto : list[list[int]]):
|
26 |
+
|
27 |
+
result = ""
|
28 |
+
|
29 |
+
for i in range(len(reparto)):
|
30 |
+
result += "Camion " + str(i) + ": " + "Almac茅n -> "
|
31 |
+
for parada in reparto[i]:
|
32 |
+
result += str(parada) + " -> "
|
33 |
+
result += "Almac茅n \n"
|
34 |
+
|
35 |
+
return result
|
36 |
+
|
37 |
+
st.set_page_config(page_title="LupercAI", page_icon= "馃殯")
|
38 |
+
|
39 |
+
st.title("LupercAI 馃殯")
|
40 |
+
|
41 |
+
valores_vitales = ["distancias", "demandas_clientes", "capacidad_vehiculos"]
|
42 |
+
importado = True
|
43 |
+
for valor in valores_vitales: importado = importado and valor in st.session_state
|
44 |
+
|
45 |
+
if not importado:
|
46 |
+
st.warning("Faltan datos", icon="鈿狅笍")
|
47 |
+
else:
|
48 |
+
#if "num_nodos" not in st.session_state: st.session_state["num_nodos"] = len(st.session_state["demandas_clientes"])
|
49 |
+
#if "num_vehiculos" not in st.session_state: st.session_state["num_vehiculos"] = len(st.session_state["capacidad_vehiculos"])
|
50 |
+
|
51 |
+
grafo = Grafo(st.session_state["distancias"], st.session_state["demandas_clientes"])
|
52 |
+
vehiculos = Vehiculos(st.session_state["capacidad_vehiculos"])
|
53 |
+
|
54 |
+
if "activar_generaciones" in st.session_state:
|
55 |
+
if st.session_state["activar_generaciones"]:
|
56 |
+
if "alg_genetico" not in st.session_state: st.session_state["alg_genetico"] = Algoritmo_Genetico(tamano_poblacion = st.session_state["tamano_poblacion"],
|
57 |
+
generaciones = st.session_state["generaciones"], tamano_torneo = st.session_state["tamano_torneo"],
|
58 |
+
crossover_prob = st.session_state["crossover_prob"], mutation_prob = st.session_state["mutation_prob"])
|
59 |
+
else:
|
60 |
+
if "alg_genetico" not in st.session_state: st.session_state["alg_genetico"] = Algoritmo_Genetico()
|
61 |
+
else:
|
62 |
+
if "alg_genetico" not in st.session_state: st.session_state["alg_genetico"] = Algoritmo_Genetico()
|
63 |
+
|
64 |
+
alg_genetico = st.session_state["alg_genetico"]
|
65 |
+
|
66 |
+
from threading import Thread
|
67 |
+
Thread(target=alg_genetico.generar, args = (grafo, vehiculos)).start()
|
68 |
+
|
69 |
+
|
70 |
+
#Computaci贸n:
|
71 |
+
|
72 |
+
empty_latest_iteration = st.empty()
|
73 |
+
empty_mejor_coste = st.empty()
|
74 |
+
|
75 |
+
st.subheader("Barra de progreso")
|
76 |
+
barra_progreso = st.progress(0)
|
77 |
+
|
78 |
+
st.subheader("Grafo del mejor recorrido actual")
|
79 |
+
|
80 |
+
empty_txt = st.empty()
|
81 |
+
|
82 |
+
empty_grafo = st.empty()
|
83 |
+
|
84 |
+
i = 0
|
85 |
+
while alg_genetico.generacion_actual < alg_genetico.generaciones:
|
86 |
+
#Generaci贸n por escrito
|
87 |
+
empty_latest_iteration.write("Generaci贸n: " + str(alg_genetico.generacion_actual))
|
88 |
+
|
89 |
+
#Barra de progreso
|
90 |
+
barra_progreso.progress( alg_genetico.generacion_actual/alg_genetico.generaciones)
|
91 |
+
|
92 |
+
#Mejor coste actual (texto)
|
93 |
+
empty_mejor_coste.write("**Mejor coste**: " + str(alg_genetico.mejor_candidato[1]))
|
94 |
+
|
95 |
+
#Descarga de grafo
|
96 |
+
empty_txt.download_button("Mejor recorrido 馃斀", presentar(alg_genetico.mejor_candidato[0]), file_name="Mejor reparto - Generacion " + str(alg_genetico.generacion_actual) + ".txt", key=str(i))
|
97 |
+
i += 1
|
98 |
+
|
99 |
+
#Mostrar el grafo
|
100 |
+
empty_grafo.graphviz_chart(grafar(alg_genetico.mejor_candidato[0]))
|
101 |
+
|
102 |
+
|
103 |
+
#Ultima iteracion
|
104 |
+
empty_latest_iteration.write("Generaci贸n: " + str(alg_genetico.generacion_actual))
|
105 |
+
barra_progreso.progress( alg_genetico.generacion_actual/alg_genetico.generaciones)
|
106 |
+
empty_mejor_coste.write("**Mejor coste**: " + str(alg_genetico.mejor_candidato[1]))
|
107 |
+
empty_txt.download_button("Mejor recorrido 馃斀", presentar(alg_genetico.mejor_candidato[0]), file_name="Mejor reparto - Generacion " + str(alg_genetico.generacion_actual) + ".txt", key=str(i))
|
108 |
+
empty_grafo.graphviz_chart(grafar(alg_genetico.mejor_candidato[0]))
|