import pandas as pd
import numpy as np
tsp_file_path=r'C:\Users\Administrator\berlin52.tsp'
df=pd.read_csv(tsp_file_path,delim_whitespace=True,skiprows=6,names=['None','X','Y'])
node_coordinates=df[['X','Y']].to_numpy()
num_nodes=len(node_coordinates)
distance_matrix=np.zeros((num_nodes,num_nodes))
for i in range(num_nodes):
for j in range(num_nodes):
if i!=j:
distance_matrix[i][j]=np.sqrt((node_coordinates[i][0]-node_coordinates[j][0])**2+(node_coordinates[i][1]-node_coordinates[j][1])**2)
distance_matrix_df=pd.DataFrame(distance_matrix,columns=range(num_nodes),index=range(num_nodes))
pd.set_option('display.max_columns',None)
print(distance_matrix_df)
output_file_path='distance_matrix.csv'
distance_matrix_df.to_csv(output_file_path,index=False)
算法设计分析---初始种群1
最新推荐文章于 2024-08-24 11:00:49 发布