-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
66 lines (63 loc) · 1.98 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
import os
import errno
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
from Arbol import Arbol
#crea la heuristica del problema en una matriz de nxn
def crearHeuristica(n):
#los valores de la heuristica son al azar del 0 al 100
heuristica = np.random.randint(1, 100, (cantCiudades,cantCiudades))
# se reemplaza la diagonal por 0s
np.fill_diagonal(heuristica,0)
for i in range(len(heuristica)):
for j in range(len(heuristica[0])):
# se copia el triangulo superior de la matriz en el inferior
if i > j :
heuristica[i][j] = heuristica[j][i]
return heuristica
# grafica cantidad de ciudades vs tiempo[ms] y lo guarda en plots
def graficar(x,y):
plt.plot(x, y)
plt.xlabel("Cantidad de ciudades")
plt.ylabel("Tiempo [ms]")
plt.title("Ciudades vs tiempo")
plt.xticks(x)
plt.savefig(f"plots/plot{len(x)}.png")
#crea un csv con la cantidad de ciudades y el tiempo[ms]
def crearCSV(x,y):
df =pd.DataFrame(x,y)
df.to_csv("plots/data.csv", header=False)
#Crea la carpeta plots para guardar los graficos y los datos
def crearCarpeta():
try:
os.mkdir('plots')
except OSError as e:
if e.errno != errno.EEXIST:
raise
crearCarpeta()
cantCiudades = 4
x= []
y =[]
while True:
# Se calcula la heuristica
heuristica = crearHeuristica(cantCiudades)
# Se arma el arbol y se obtiene el recorrido con best first
arbol = Arbol(cantCiudades)
print(arbol.foo,arbol.__var)
inicio = datetime.now()
recorrido = arbol.bestFirst(heuristica)
fin = datetime.now()
tiempo = round((fin - inicio).total_seconds()*1000,2)
# Se muestra el recorrido
print(recorrido)
print(f"Tiempo total: {tiempo} [ms]")
# Se guardan los resultados
x.append(cantCiudades)
y.append(tiempo)
# Se grafican y se guardan los datos
graficar(x,y)
crearCSV(x,y)
# Se incrementan las ciudades en 2
cantCiudades += 2