由于TSP问题数据类型多样,储存方式、评价指标多样,因此想成功加载所有数据需要写较多逻辑。本项目就解决了这个问题。我还封装了类来简化使用。
项目配合写了latex小论文
代码地址
https://github.com/LT1st/System_engineering_programm/tree/master/code
项目地址
https://github.com/LT1st/System_engineering_programm
# -*- coding: utf-8 -*-
# 数据加载
from dataloader.Dataloader_for_TSP_datasets import TSP_DATA
from dataloader.load_TSP_from_floder import get_all_TSP_and_ATSP_in_floder
# 算法加载
from collection.SA import SA
from collection.PSO import PSO
from collection.TS import TS
from dataloader.DP import DP
# 可视化加载
from visualization.from_matrix import from_matrix,root_from_list
# 加载数据
samples,samples_atsp = get_all_TSP_and_ATSP_in_floder()
# 从网页爬取最佳数据
from dataloader.get_best_solution import get_best_result_from_web
samples_name_list = []
# 构建字典,储存各个算法结果
SA_dict = {}
DP_dict = {}
PSO_dict = {}
TS_dict = {}
#data = TSP_DATA('D:\\0latex\\System_engineering_programm\\TSP_tst_data\\lin318.tsp.gz',debug=True)
# 遍历测试样例
for sample in samples:
data = TSP_DATA(sample)
print(data.NAME,"successful",data.EDGE_WEIGHT_TYPE)
samples_name_list.append(data.NAME)
model = SA(num_city = data.DIMENSION , mat = data.matrix)
path, path_len = model.run()
SA_dict[data.NAME] = path_len
# 路径可视化
#root_from_list(path,data.matrix)
model = DP(num_city=data.DIMENSION, num_total=25, iteration=500, data=data.matrix)
path, path_len = model.run()
DP_dict[data.NAME] = path_len
# 路径可视化
#root_from_list(path,data.matrix)
model = PSO(num_city = data.DIMENSION , mat = data.matrix)
path, path_len = model.run()
PSO_dict[data.NAME] = path_len
# 路径可视化
#root_from_list(path,data.matrix)
model = TS(num_city = data.DIMENSION , mat = data.matrix)
path, path_len = model.run()
TS_dict[data.NAME] = path_len
# 路径可视化
#root_from_list(path,data.matrix)
get_best_result_from_web()
详细看readme.md
主文件
存放我的开源pip库:官方网站
测量函数运行时间,用于后续训练
final_report 最终报告latex文档代码
mid_term 中期文档latex代码
proposal 开题latex代码
code 代码
TSP_tst_data TSP测试数据
读取失败的测试样例 由于维度太大导致加载不出来的失败情况
20220421-TSP 任务要求
存放我的开源pip库:官方网站
测量函数运行时间,用于后续训练
::在控制台输入安装
pip install -i https://test.pypi.org/simple/ TSP-dataloader
::在控制台输入
python setup.py sdist bdist_wheel
::在控制台输入 当前文件夹路径与setpy.py一致
python -m twine upload --repository testpypi dist/*
::使用测试环境
python -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-pkg-YOUR-USERNAME #其中 example-pkg-YOUR-USERNAME 即自己指定的包名
python -m twine upload --repository testpypi dist/* -u __token__ -p pypi-密码在qq收藏备份 --verbose
This is an implementation of TSP dataloader.
Chose a method to get a $TSP_DATA class $ at first.
Use g e t a l l T S P a n d A T S P i n f l o d e r ( ) get_all_TSP_and_ATSP_in_floder() getallTSPandATSPinfloder() to get all atsp and tsp file path in a floder.
import TSP_dataloader as DL
# get all paths of all files in floder
tsp_files , atsp_files = get_all_TSP_and_ATSP_in_floder("Your dataset floder here")
# go throuht all samples
for tsp_file in tsp_files:
data_class = DL.TSP_load("Your simgle TSP file path here")
Use T S P l o a d ( ) TSP_load() TSPload() to get a single T S P D A T A TSP_DATA TSPDATA class.
import TSP_dataloader as DL
data_class = DL.TSP_load("Your simgle TSP file path here")
Use $TSP_DATA class $ directly.
import TSP_dataloader as TSP_DATA
data_class = TSP_DATA("Your simgle TSP file path here")
Get adjacency matrix.
data_class.get_matrix()
Get adjacency table.
data_class.get_table()
Get coorodinate list.
data_class.get_coorodinate_list()
Basic elements.
类内变量:
必有:
self.NAME 测试样例名称
self.TYPE 测试样例类型 TSP ATSP
self.DIMENSION. 维度
self.EDGE_WEIGHT_TYPE 边权值计算方式 决定读取方式
self.matrix 矩阵形式数据
self.table . 邻接表形式数据
可能有:
self.EDGE_WEIGHT_FORMAT
self.EDGE_DATA_FORMAT
self.NODE_COORD_TYPE required if EDGE_WEIGHT_TYPE is not WeightKind::Explicit
from TSP_dataloader import SOM,DP,SA
samples = get_all_TSP_and_ATSP_in_floder()
samples_name_list = []
SOM_dict = {}
SA_dict = {}
DP_dict = {}
# go through
for sample in samples:
data = TSP_DATA(sample)
samples_name_list.append(data.NAME)
model = SOM(num_city=data.DIMENSION, data=data.matrix)
path, path_len = model.run()
SOM_dict[data.NAME] = path_len
model = SA(num_city = data.DIMENSION , mat = data.matrix)
path, path_len = model.run()
SA_dict[data.NAME] = path_len
model = DP(num_city = data.DIMENSION , mat = data.matrix)
path, path_len = model.run()
DP_dict[data.NAME] = path_len
alternative input to make load quicker
def TSP_load(path, requireTable=True, requireMatrix=True, load_now =True):
"""传入单个数据地址,读取并且加载数据的表头
path:
单个测试样例的数据地址
requireTable:
需要邻接表?
requireMatrix:
需要邻接矩阵?
"""
check if the matrix is summetry.
data_class.check_if_summetry(matrix_to_be_checked)
Get best result from web. Return a dict indexed by NAME.
data_class.dict_best_result = get_best_result_from_web()
Convert ATSP to TSP.
data_class.ATSP2TSP_np()
check if any inner-class variable wrong
data_class.check_if_reasonable()