如何在 Matlab 中使用 TSPLIB
How to use TSPLIB in Matlab
有没有简单的方法可以将*.tsp 文件导入到Matlab 中?我一直没能找到现有的解决方案,但由于 TSPLIB 被提及的次数如此之多,我希望一定已经有一个现有的解决方案。
否则:
使用Java(例如使用正则表达式)或直接在 Matlab 中写入数据会更容易吗?
编辑:
建议改用 .xml 文件。但是,较大的实例读取速度非常慢。我猜这也与文件更大有关:对于 pcb3038,.tsp 文件约为 100kb,而 .xml 文件接近 500 mb。
我不知道原来的TSPLIB数据文件是什么结构来的,但是根据TSBLIB website, TSPLIB data files are also available in XML format,例如:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<travellingSalesmanProblemInstance>
<name>a280</name>
<source>TSPLIB</source>
<description>drilling problem (Ludwig)</description>
<doublePrecision>15</doublePrecision>
<ignoredDigits>5</ignoredDigits>
<graph>
<vertex>
<edge cost="2.000000000000000e+01">1</edge>
<edge cost="2.408318915758459e+01">2</edge>
<edge cost="3.298484500494128e+01">3</edge>
<edge cost="3.298484500494128e+01">4</edge>
<edge cost="4.275511665286390e+01">5</edge>
<edge cost="5.571355310873648e+01">6</edge>
...
<edge cost="4.560701700396552e+01">275</edge>
<edge cost="3.492849839314596e+01">276</edge>
<edge cost="2.912043955712207e+01">277</edge>
<edge cost="2.039607805437114e+01">278</edge>
</vertex>
</graph>
</travellingSalesmanProblemInstance>
您可以在 MATLAB 中 import XML files using xmlread
, then parse them using the example code provided by MATLAB. Alternatively, you can try the xml2struct
utility from FEX. Lastly, if you prefer working with python, you can also use BeautifulSoup 使用 python API (py.bs4.BeautifulSoup(...)
).
我发现在 Matlab 中执行此操作的最简单方法是使用 fopen() 打开 xml 文件,然后使用 fgetl() 逐行读取它们。
除此之外,我还使用 regexp() 来解释这些行并采取相应的行动。
这使我能够读取 +- 500mb xml 文件并将相关数据放入距离矩阵中。
有没有简单的方法可以将*.tsp 文件导入到Matlab 中?我一直没能找到现有的解决方案,但由于 TSPLIB 被提及的次数如此之多,我希望一定已经有一个现有的解决方案。
否则: 使用Java(例如使用正则表达式)或直接在 Matlab 中写入数据会更容易吗?
编辑: 建议改用 .xml 文件。但是,较大的实例读取速度非常慢。我猜这也与文件更大有关:对于 pcb3038,.tsp 文件约为 100kb,而 .xml 文件接近 500 mb。
我不知道原来的TSPLIB数据文件是什么结构来的,但是根据TSBLIB website, TSPLIB data files are also available in XML format,例如:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<travellingSalesmanProblemInstance>
<name>a280</name>
<source>TSPLIB</source>
<description>drilling problem (Ludwig)</description>
<doublePrecision>15</doublePrecision>
<ignoredDigits>5</ignoredDigits>
<graph>
<vertex>
<edge cost="2.000000000000000e+01">1</edge>
<edge cost="2.408318915758459e+01">2</edge>
<edge cost="3.298484500494128e+01">3</edge>
<edge cost="3.298484500494128e+01">4</edge>
<edge cost="4.275511665286390e+01">5</edge>
<edge cost="5.571355310873648e+01">6</edge>
...
<edge cost="4.560701700396552e+01">275</edge>
<edge cost="3.492849839314596e+01">276</edge>
<edge cost="2.912043955712207e+01">277</edge>
<edge cost="2.039607805437114e+01">278</edge>
</vertex>
</graph>
</travellingSalesmanProblemInstance>
您可以在 MATLAB 中 import XML files using xmlread
, then parse them using the example code provided by MATLAB. Alternatively, you can try the xml2struct
utility from FEX. Lastly, if you prefer working with python, you can also use BeautifulSoup 使用 python API (py.bs4.BeautifulSoup(...)
).
我发现在 Matlab 中执行此操作的最简单方法是使用 fopen() 打开 xml 文件,然后使用 fgetl() 逐行读取它们。 除此之外,我还使用 regexp() 来解释这些行并采取相应的行动。
这使我能够读取 +- 500mb xml 文件并将相关数据放入距离矩阵中。