# -*- coding: utf-8 -*- """ Created on Tue Jul 20 07:43:45 2021 @author: danie """ import random import csv nodes = [] with open('nodes.csv', 'r') as file: reader = csv.reader(file) for row in reader: nodes.append([int(row[0]),float(row[1]),float(row[2])]) # nodes = [[36266.6667, 62550.0000], # [34600.0000, 58633.3333], # [51650.0000, 72300.0000], # [37800.0000, 67683.3333], # [35428.0556, 60174.1667], # [34583.3333, 68550.0000], # [27383.3333, 54766.6667]] graph = [] for j in range(len(nodes)-1): graph_temp = [] for i in range(len(nodes)-1): dist = pow(pow((nodes[j][1] - nodes[i][1]),2) + pow((nodes[j][2] - nodes[i][2]),2),0.5) graph_temp.append(dist) graph.append(graph_temp) distancia_best = 999999999 caminho_best = [] for k in range(100): caminho = list(range(len(graph))) random.shuffle(caminho) caminho.append(caminho[0]) distancia = 0 for i in range(len(caminho)-1): distancia += graph[caminho[i]][caminho[i+1]] if (distancia_best > distancia): distancia_best = distancia caminho_best = caminho print(k, caminho_best ,distancia_best)