Upload Entrada.py
Browse files- pages/Entrada.py +206 -0
pages/Entrada.py
ADDED
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
# ######################
|
4 |
+
# Configuración de página
|
5 |
+
# ######################
|
6 |
+
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="LupercAI",
|
9 |
+
page_icon= "🚛",
|
10 |
+
layout="centered", #"wide" or "centered"
|
11 |
+
initial_sidebar_state="collapsed", #"auto" or "expanded" or "collapsed"
|
12 |
+
menu_items={
|
13 |
+
'Get Help': "https://www.inta.es/INTA/es/index.html",
|
14 |
+
'Report a bug': "https://www.inta.es/INTA/es/index.html",
|
15 |
+
'About': "Si necesitas ayuda, puedes dirigirte a *[email protected]*."
|
16 |
+
}
|
17 |
+
)
|
18 |
+
|
19 |
+
######################
|
20 |
+
# Page Title
|
21 |
+
######################
|
22 |
+
|
23 |
+
if "alg_genetico" in st.session_state: del st.session_state["alg_genetico"]
|
24 |
+
|
25 |
+
st.title("LupercAI 🚛")
|
26 |
+
|
27 |
+
st.header("Entrada de archivos")
|
28 |
+
|
29 |
+
import pandas as pd
|
30 |
+
|
31 |
+
tab1, tab2 = st.tabs(["Excel", "Manual"])
|
32 |
+
|
33 |
+
with tab1:
|
34 |
+
uploaded_file = st.file_uploader("Elige un archivo", accept_multiple_files=False, type=["xlsx"], help="Solo acepta excel")
|
35 |
+
if uploaded_file is not None:
|
36 |
+
st.write(uploaded_file.name)
|
37 |
+
|
38 |
+
datos = pd.read_excel(uploaded_file, sheet_name=None)
|
39 |
+
nombres_hojas = list(datos.keys())
|
40 |
+
|
41 |
+
if "Distancias" not in nombres_hojas or "Demandas_clientes" not in nombres_hojas or "Capacidad_vehiculos" not in nombres_hojas:
|
42 |
+
st.error("Falta una hoja", icon = "❌")
|
43 |
+
|
44 |
+
else:
|
45 |
+
#names permite añadir nombres, si no pandas añade directamente int desde 0.
|
46 |
+
valores = dict()
|
47 |
+
for hoja in ["Num_nodos", "Distancias", "Demandas_clientes", "Num_vehiculos", "Capacidad_vehiculos"]: # Usa 'openpyxl' como motor para leer archivos xlsx
|
48 |
+
#dataframe = pd.read_excel('6. Algoritmo genético.py\Datos.xlsx', sheet_name= hoja, header=None, names=[]) #Si el archivo estuviera en la carpeta, no cargado
|
49 |
+
if hoja in nombres_hojas:
|
50 |
+
dataframe = pd.read_excel(uploaded_file, engine='openpyxl', sheet_name=hoja, header=None, names=[])
|
51 |
+
matriz = dataframe.values.tolist()
|
52 |
+
valores[hoja] = matriz
|
53 |
+
|
54 |
+
if "Num_nodos" in nombres_hojas: num_nodos = valores["Num_nodos"][0][0]
|
55 |
+
distancias = valores["Distancias"]
|
56 |
+
demandas_clientes = [valor[0] for valor in valores["Demandas_clientes"]]
|
57 |
+
if "Num_vehiculos" in nombres_hojas: num_vehiculos = valores["Num_vehiculos"][0][0]
|
58 |
+
capacidad_vehiculos = [valor[0] for valor in valores["Capacidad_vehiculos"]]
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
with tab2:
|
63 |
+
|
64 |
+
left_column, right_column = st.columns(2)
|
65 |
+
|
66 |
+
with left_column:
|
67 |
+
form1 = st.form("Grafo")
|
68 |
+
num_nodosB = form1.number_input('Número de paradas', value = 1, help="Incluye el almacén")
|
69 |
+
form1.form_submit_button("OK")
|
70 |
+
|
71 |
+
st.subheader("Demandas")
|
72 |
+
form2 = st.form("Demanda de cada parada")
|
73 |
+
demandas_clientesB = []
|
74 |
+
demandas_clientesB.append(form2.number_input('Demanda del Almacén', value = 0, max_value=0, min_value=0))
|
75 |
+
for i in range(1, num_nodosB):
|
76 |
+
demandas_clientesB.append(form2.number_input('Demanda de la parada ' + str(i), value = 1))
|
77 |
+
boton2 = form2.form_submit_button("OK")
|
78 |
+
|
79 |
+
if boton2:
|
80 |
+
form2.success("Demandas actualizado")
|
81 |
+
|
82 |
+
|
83 |
+
with right_column:
|
84 |
+
form3 = st.form("número de vehiculos")
|
85 |
+
num_vehiculosB = form3.number_input('Número de vehiculos', value = 1)
|
86 |
+
form3.form_submit_button("OK")
|
87 |
+
|
88 |
+
st.subheader("Vehiculos")
|
89 |
+
form4 = st.form("Carga de vehiculos")
|
90 |
+
capacidad_vehiculosB = []
|
91 |
+
for i in range(1, num_vehiculosB + 1):
|
92 |
+
capacidad_vehiculosB.append(form4.number_input('Carga del vehiculo ' + str(i), value = 1))
|
93 |
+
boton4 = form4.form_submit_button("OK")
|
94 |
+
|
95 |
+
if boton4:
|
96 |
+
form4.success("Cargas actualizado")
|
97 |
+
|
98 |
+
st.subheader("Distancias")
|
99 |
+
form5 = st.form("Distancias")
|
100 |
+
|
101 |
+
|
102 |
+
data_inicial = [dict([(str(i),None) for i in range(num_nodosB)])] * num_nodosB #los elementos están vinculados
|
103 |
+
data_df = pd.DataFrame(data_inicial)
|
104 |
+
for i in range(num_nodosB): data_df[str(i)][i] = 0
|
105 |
+
edited_df = form5.data_editor(data_df)
|
106 |
+
|
107 |
+
(fila, columna) = (0, 0)
|
108 |
+
favorite_command = edited_df[str(columna)][fila]
|
109 |
+
form5.write(f"Fila {fila} Columna {columna}: **{favorite_command}** ")
|
110 |
+
|
111 |
+
distanciasB = edited_df.values.tolist()
|
112 |
+
|
113 |
+
boton5 = form5.form_submit_button("OK")
|
114 |
+
|
115 |
+
if boton5:
|
116 |
+
form5.success("Distancias actualizado")
|
117 |
+
|
118 |
+
if boton2 or boton4 or boton5:
|
119 |
+
uploaded_file = None
|
120 |
+
|
121 |
+
if uploaded_file == None:
|
122 |
+
num_nodos = num_nodosB
|
123 |
+
distancias = distanciasB
|
124 |
+
demandas_clientes = demandas_clientesB
|
125 |
+
num_vehiculos = num_vehiculosB
|
126 |
+
capacidad_vehiculos = capacidad_vehiculosB
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
st.header("Variables genéticas")
|
132 |
+
|
133 |
+
chosen = st.radio(
|
134 |
+
'¿Desea incluir alguna variable genética?',
|
135 |
+
("Variables predefinidas", "Escoger variables"),
|
136 |
+
0)
|
137 |
+
|
138 |
+
if not chosen == "Escoger variables":
|
139 |
+
st.session_state["activar_generaciones"] = False
|
140 |
+
|
141 |
+
else:
|
142 |
+
|
143 |
+
c1, c2, c3 = st.columns(3)
|
144 |
+
|
145 |
+
|
146 |
+
with c1:
|
147 |
+
tamano_poblacion_bool = st.checkbox('Tamaño poblacion')
|
148 |
+
tamano_poblacion = 50
|
149 |
+
if tamano_poblacion_bool: tamano_poblacion = st.number_input('Tamaño poblacion', value = 50, min_value = 1, label_visibility = "hidden")
|
150 |
+
|
151 |
+
tamano_torneo_bool = st.checkbox('Tamaño torneo')
|
152 |
+
tamano_torneo = 5
|
153 |
+
if tamano_torneo_bool: tamano_torneo = st.number_input('Tamaño poblacion', value = 5, min_value = 1,
|
154 |
+
help = 'El tamaño del torneo debe ser menor al tamaño de la población', label_visibility = "hidden")
|
155 |
+
|
156 |
+
if tamano_poblacion <= tamano_torneo:
|
157 |
+
st.warning('El tamaño del torneo debe ser menor al tamaño de la población.', icon="⚠️")
|
158 |
+
tamano_torneo = 1
|
159 |
+
|
160 |
+
with c2:
|
161 |
+
generaciones_bool = st.checkbox('Generaciones')
|
162 |
+
generaciones = 100
|
163 |
+
if generaciones_bool: generaciones = st.number_input('Generaciones', value = 100, min_value = 1, label_visibility = "hidden")
|
164 |
+
|
165 |
+
st.session_state['generaciones'] = generaciones
|
166 |
+
|
167 |
+
with c3:
|
168 |
+
crossover_prob_bool = st.checkbox('Probabilidad de cruce')
|
169 |
+
crossover_prob = 0.8
|
170 |
+
if crossover_prob_bool: crossover_prob = st.number_input('Probabilidad de cruce', value = 0.8, min_value = 0.0, max_value = 1.0, label_visibility = "hidden")
|
171 |
+
|
172 |
+
mutation_prob_bool = st.checkbox('Probabilidad de mutación')
|
173 |
+
mutation_prob = 0.2
|
174 |
+
if mutation_prob_bool: mutation_prob = st.number_input('Probabilidad de mutación', value = 0.2, min_value = 0.0, max_value = 1.0, label_visibility = "hidden")
|
175 |
+
|
176 |
+
if tamano_poblacion_bool or tamano_torneo_bool or generaciones_bool or crossover_prob_bool or mutation_prob_bool:
|
177 |
+
st.session_state["activar_generaciones"] = True
|
178 |
+
st.session_state["tamano_poblacion"] = tamano_poblacion
|
179 |
+
st.session_state["tamano_torneo"] = tamano_torneo
|
180 |
+
st.session_state["generaciones"] = generaciones
|
181 |
+
st.session_state["crossover_prob"] = crossover_prob
|
182 |
+
st.session_state["mutation_prob"] = mutation_prob
|
183 |
+
else:
|
184 |
+
st.session_state["activar_generaciones"] = False
|
185 |
+
|
186 |
+
st.header("Guardar")
|
187 |
+
boton = st.button("¿Guardar variables?", type="primary")
|
188 |
+
|
189 |
+
if boton:
|
190 |
+
|
191 |
+
if distancias == None or demandas_clientes == None or capacidad_vehiculos == None: st.error(f"Falta un valor fundamental")
|
192 |
+
elif len(distancias) != len(demandas_clientes): st.error(f"Distancias: {distancias} no coincide con Demandas: {demandas_clientes}")
|
193 |
+
else:
|
194 |
+
st.session_state["distancias"] = distancias
|
195 |
+
st.session_state["demandas_clientes"] = demandas_clientes
|
196 |
+
st.session_state["capacidad_vehiculos"] = capacidad_vehiculos
|
197 |
+
|
198 |
+
if num_nodos != None:
|
199 |
+
if num_nodos != len(distancias): st.warning(f"Número de paradas: {num_nodos} no coincide con Distancias: {distancias}")
|
200 |
+
else: st.session_state["num_nodos"] = num_nodos
|
201 |
+
|
202 |
+
if num_vehiculos != None:
|
203 |
+
if num_vehiculos != len(capacidad_vehiculos): st.warning(f"Número de vehiculos: {num_vehiculos} no coincide con Cargas Vehiculos: {capacidad_vehiculos}")
|
204 |
+
else: st.session_state["num_vehiculos"] = num_vehiculos
|
205 |
+
|
206 |
+
st.success(f"Valores actualizados")
|