ml4co-kit 0.0.1a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ml4co_kit-0.0.1a1/PKG-INFO +248 -0
- ml4co_kit-0.0.1a1/README.md +214 -0
- ml4co_kit-0.0.1a1/ml4co_kit/__init__.py +23 -0
- ml4co_kit-0.0.1a1/ml4co_kit/data/__init__.py +2 -0
- ml4co_kit-0.0.1a1/ml4co_kit/data/mis/__init__.py +1 -0
- ml4co_kit-0.0.1a1/ml4co_kit/data/mis/satlib_original.py +14 -0
- ml4co_kit-0.0.1a1/ml4co_kit/data/tsp/__init__.py +2 -0
- ml4co_kit-0.0.1a1/ml4co_kit/data/tsp/tsp_uniform.py +60 -0
- ml4co_kit-0.0.1a1/ml4co_kit/data/tsp/tsplib_original.py +182 -0
- ml4co_kit-0.0.1a1/ml4co_kit/evaluate/__init__.py +2 -0
- ml4co_kit-0.0.1a1/ml4co_kit/evaluate/mis/__init__.py +1 -0
- ml4co_kit-0.0.1a1/ml4co_kit/evaluate/mis/satlib_eval.py +23 -0
- ml4co_kit-0.0.1a1/ml4co_kit/evaluate/tsp/__init__.py +3 -0
- ml4co_kit-0.0.1a1/ml4co_kit/evaluate/tsp/base.py +98 -0
- ml4co_kit-0.0.1a1/ml4co_kit/evaluate/tsp/tsplib_original_eval.py +62 -0
- ml4co_kit-0.0.1a1/ml4co_kit/evaluate/tsp/uniform_eval.py +21 -0
- ml4co_kit-0.0.1a1/ml4co_kit/generator/__init__.py +2 -0
- ml4co_kit-0.0.1a1/ml4co_kit/generator/mis_data.py +205 -0
- ml4co_kit-0.0.1a1/ml4co_kit/generator/tsp_data.py +350 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/__init__.py +2 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/mis/__init__.py +3 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/mis/base.py +172 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/mis/gurobi.py +295 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/mis/kamis.py +245 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/__init__.py +3 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/base.py +405 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/concorde.py +148 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/lkh.py +142 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/lkh_solver/__init__.py +1 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/lkh_solver/problems.py +106 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/lkh_solver/solve.py +96 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/pyconcorde/__init__.py +16 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/pyconcorde/concorde/__init__.py +0 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/pyconcorde/concorde/solve.py +127 -0
- ml4co_kit-0.0.1a1/ml4co_kit/solver/tsp/pyconcorde/setup.py +183 -0
- ml4co_kit-0.0.1a1/ml4co_kit/utils/__init__.py +1 -0
- ml4co_kit-0.0.1a1/ml4co_kit/utils/file_utils.py +147 -0
- ml4co_kit-0.0.1a1/ml4co_kit/utils/mis_utils.py +188 -0
- ml4co_kit-0.0.1a1/ml4co_kit.egg-info/PKG-INFO +248 -0
- ml4co_kit-0.0.1a1/ml4co_kit.egg-info/SOURCES.txt +48 -0
- ml4co_kit-0.0.1a1/ml4co_kit.egg-info/dependency_links.txt +1 -0
- ml4co_kit-0.0.1a1/ml4co_kit.egg-info/requires.txt +9 -0
- ml4co_kit-0.0.1a1/ml4co_kit.egg-info/top_level.txt +1 -0
- ml4co_kit-0.0.1a1/setup.cfg +4 -0
- ml4co_kit-0.0.1a1/setup.py +126 -0
- ml4co_kit-0.0.1a1/tests/test_draw.py +46 -0
- ml4co_kit-0.0.1a1/tests/test_evaluate.py +78 -0
- ml4co_kit-0.0.1a1/tests/test_generator.py +196 -0
- ml4co_kit-0.0.1a1/tests/test_solver.py +121 -0
- ml4co_kit-0.0.1a1/tests/test_utils.py +41 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: ml4co-kit
|
|
3
|
+
Version: 0.0.1a1
|
|
4
|
+
Summary: ml4co-kit provides convenient dataset generators for the combinatorial optimization problem
|
|
5
|
+
Home-page: https://github.com/Thinklab-SJTU/ML4CO-Kit
|
|
6
|
+
Author: SJTU-ReThinkLab
|
|
7
|
+
License: Mulan PSL v2
|
|
8
|
+
Classifier: License :: OSI Approved :: Mulan Permissive Software License v2 (MulanPSL-2.0)
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
17
|
+
Classifier: Operating System :: Unix
|
|
18
|
+
Classifier: Environment :: Console
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
Requires-Dist: numpy>=1.24.4
|
|
25
|
+
Requires-Dist: networkx>=2.8.8
|
|
26
|
+
Requires-Dist: tsplib95>=0.7.1
|
|
27
|
+
Requires-Dist: tqdm>=4.66.1
|
|
28
|
+
Requires-Dist: requests>=2.31.0
|
|
29
|
+
Requires-Dist: pulp>=2.8.0
|
|
30
|
+
Requires-Dist: pandas>=2.0.0
|
|
31
|
+
Requires-Dist: scipy>=1.10.1
|
|
32
|
+
Requires-Dist: aiohttp>=3.9.3
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
<img src="docs/assets/ml4co-kit-logo.png" alt="ML4CO-Kit" width="800"/>
|
|
36
|
+
|
|
37
|
+
[](https://pypi.org/pypi/data4co/) [](https://pypi.python.org/pypi/data4co/) [](https://pepy.tech/project/data4co) [](https://GitHub.com/Thinklab-SJTU/ML4CO-Kit/stargazers/)
|
|
38
|
+
|
|
39
|
+
`ML4CO-Kit` is a in-development toolkit for machine learning practices on combinatorial optimization problems, which is a by-product of our research for a unified modular framework that integrates existing ML4CO practices, minimizing disparities among methods and supporting the investigations via in-depth analysis and transparent ablation.
|
|
40
|
+
|
|
41
|
+
This reposity focuses on the supporting code for method development instead of implementing core technologies, which will be presented in the future in our full implementation and organization. `ML4CO-Kit` has the following features:
|
|
42
|
+
|
|
43
|
+
* The skeleton of framework organization for ML4CO projects;
|
|
44
|
+
* Implemented base classes that facilitate method development;
|
|
45
|
+
* Mainstream traditional solver baselines and reference solution acquisition;
|
|
46
|
+
* Data generation of various distributions;
|
|
47
|
+
* Problem and solution visualization;
|
|
48
|
+
* Evaluators for different problems.
|
|
49
|
+
|
|
50
|
+
| Problem | Data | solver | Supervision | evaluator | visualization |
|
|
51
|
+
| :-----: | :--------------------------------: | :-----------: | :-------------------: | :-------: | :---------------------: |
|
|
52
|
+
| TSP | Uniform, Gaussian, Cluster, TSPLIB | LKH, Concorde | Solution, Edge Regret | ✔ | Problem Graph, Solution |
|
|
53
|
+
| MIS | SATLIB, ER, BA, HK, WS | KaMIS, Gurobi | Solution | ✔ | Problem Graph, Solution |
|
|
54
|
+
|
|
55
|
+
###### ML4CO Organization:
|
|
56
|
+
|
|
57
|
+
<img src="docs/assets/organization.jpg" alt="Organization" width="800"/>
|
|
58
|
+
|
|
59
|
+
We are still enriching the library and we welcome any contributions/ideas/suggestions from the community. A comprehensive modular framework built upon this library that integrates core ML4CO technologies coming soon.
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
You can install the stable release on PyPI:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
$ pip install ml4co_kit
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
or get the latest version by running:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
$ pip install -U https://github.com/Thinklab-SJTU/ML4CO-Kit/archive/master.zip # with --user for user install (no root)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The following packages are required, and shall be automatically installed by ``pip``:
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
Python >= 3.8
|
|
79
|
+
numpy>=1.24.4
|
|
80
|
+
networkx==2.8.8
|
|
81
|
+
tsplib95==0.7.1
|
|
82
|
+
tqdm>=4.66.1
|
|
83
|
+
pulp>=2.8.0,
|
|
84
|
+
pandas>=2.0.0,
|
|
85
|
+
scipy>=1.10.1
|
|
86
|
+
requests>=2.31.0
|
|
87
|
+
aiohttp>=3.9.3
|
|
88
|
+
async_timeout>=4.0.3
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
To ensure you have access to all functions, such as visualization, you'll need to install the following packages using `pip`:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
$ pip install matplotlib>=3.7.5
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Usage Examples
|
|
98
|
+
|
|
99
|
+
### Solve with Traditional Solver Baselines
|
|
100
|
+
|
|
101
|
+
We provide base classes that offer a user-friendly approach for implementing traditional and learning-based solvers. Taking `TSPSolver` as an example, which includes functionalities for data input and output, as well as an evaluation function. The solver supports different data inputs, such as Numpy arrays and .txt and .tsp files. The outputs can be saved to corresponding types of files as needed. Additionally, the solver offers an evaluation function, by which users can quickly obtain the average tour length, average gap, and standard deviation of the test dataset. Traditional solvers are directly incorporated in our library inheriting `TSPSolver`.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
>>> from ml4co_kit.solver import TSPLKHSolver
|
|
105
|
+
|
|
106
|
+
# initialization
|
|
107
|
+
>>> tsp_lkh_solver = TSPLKHSolver(lkh_max_trials=500)
|
|
108
|
+
|
|
109
|
+
# input instances and reference solutions by a .txt file
|
|
110
|
+
>>> tsp_lkh_solver.from_txt("path/to/read/tsp500_concorde.txt")
|
|
111
|
+
|
|
112
|
+
# lkh solving
|
|
113
|
+
>>> tsp_lkh_solver.solve()
|
|
114
|
+
|
|
115
|
+
# evaluate
|
|
116
|
+
>>> tsp_lkh_solver.evaluate(caculate_gap=True)
|
|
117
|
+
(16.583557978549532, 0.21424058722308548, 0.09031979488795724)
|
|
118
|
+
|
|
119
|
+
# save solving results
|
|
120
|
+
>>> tsp_lkh_solver.to_txt("path/to/write/tsp500_lkh.txt")
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Data Generation
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from ml4co_kit import TSPDataGenerator
|
|
127
|
+
|
|
128
|
+
# initialization
|
|
129
|
+
tsp_data_lkh = TSPDataGenerator(
|
|
130
|
+
num_threads=8,
|
|
131
|
+
nodes_num=50,
|
|
132
|
+
data_type="uniform",
|
|
133
|
+
solver="lkh",
|
|
134
|
+
train_samples_num=16,
|
|
135
|
+
val_samples_num=16,
|
|
136
|
+
test_samples_num=16,
|
|
137
|
+
save_path="path/to/save/"
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
# generate
|
|
141
|
+
tsp_data_lkh.generate()
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Evaluate
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
>>> from ml4co_kit.evaluate import TSPLIBOriEvaluator
|
|
148
|
+
>>> from ml4co_kit.solver import TSPLKHSolver
|
|
149
|
+
|
|
150
|
+
>>> lkh_solver = TSPLKHSolver(scale=1)
|
|
151
|
+
>>> evaluator = TSPLIBOriEvaluator()
|
|
152
|
+
>>> evaluator.evaluate(lkh_solver, norm="EUC_2D")
|
|
153
|
+
solved_costs ref_costs gaps
|
|
154
|
+
att48 33523.708507 33523.708507 0.000000e+00
|
|
155
|
+
eil51 429.983312 429.983312 0.000000e+00
|
|
156
|
+
berlin52 7544.365902 7544.365902 3.616585e-14
|
|
157
|
+
st70 678.557469 678.597452 -5.892021e-03
|
|
158
|
+
eil76 545.229738 545.387552 -2.893612e-02
|
|
159
|
+
pr76 108159.438274 108159.438274 -1.345413e-14
|
|
160
|
+
kroA100 21285.443182 21285.443182 0.000000e+00
|
|
161
|
+
kroC100 20750.762504 20750.762504 0.000000e+00
|
|
162
|
+
kroD100 21294.290821 21294.290821 3.416858e-14
|
|
163
|
+
rd100 7910.396210 7910.396210 0.000000e+00
|
|
164
|
+
eil101 642.856874 642.309536 8.521414e-02
|
|
165
|
+
lin105 14382.995933 14382.995933 0.000000e+00
|
|
166
|
+
ch130 6110.900592 6110.860950 6.487238e-04
|
|
167
|
+
ch150 6530.902722 6532.280933 -2.109847e-02
|
|
168
|
+
tsp225 3859.000000 3859.000000 0.000000e+00
|
|
169
|
+
a280 2588.301213 2586.769648 5.920765e-02
|
|
170
|
+
pr1002 260277.189980 259066.663053 4.672646e-01
|
|
171
|
+
pr2392 384469.093320 378062.826191 1.694498e+00
|
|
172
|
+
AVG 50054.634253 49631.448887 1.250504e-01
|
|
173
|
+
|
|
174
|
+
>>> eva.evaluate(lkh_solver, norm="GEO")
|
|
175
|
+
solved_costs ref_costs gaps
|
|
176
|
+
ulysses16 6859.0 6859.0 0.000000
|
|
177
|
+
ulysses22 7013.0 7013.0 0.000000
|
|
178
|
+
gr96 55209.0 55209.0 0.000000
|
|
179
|
+
gr202 40160.0 40160.0 0.000000
|
|
180
|
+
gr666 295012.0 294358.0 0.222178
|
|
181
|
+
AVG 80850.6 80719.8 0.044436
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Visualization
|
|
185
|
+
|
|
186
|
+
#### TSP
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
from data4co.solver import TSPConcordeSolver
|
|
190
|
+
from data4co.draw.tsp import draw_tsp_solution, draw_tsp_problem
|
|
191
|
+
|
|
192
|
+
# use TSPConcordeSolver to solve the problem
|
|
193
|
+
solver = TSPConcordeSolver(scale=1)
|
|
194
|
+
solver.from_tsp("examples/tsp/kroA150.tsp")
|
|
195
|
+
solver.solve(norm="EUC_2D")
|
|
196
|
+
|
|
197
|
+
# draw
|
|
198
|
+
draw_tsp_problem(
|
|
199
|
+
save_path="docs/assets/kroA150_problem.png",
|
|
200
|
+
points=solver.ori_points,
|
|
201
|
+
)
|
|
202
|
+
draw_tsp_solution(
|
|
203
|
+
save_path="docs/assets/kroA150_solution.png",
|
|
204
|
+
points=solver.ori_points,
|
|
205
|
+
tours=solver.tours
|
|
206
|
+
)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Visualization Results:
|
|
210
|
+
|
|
211
|
+
<p>
|
|
212
|
+
<img src="docs/assets/kroA150_problem.png" width="35%" alt="" />
|
|
213
|
+
<img src="docs/assets/kroA150_solution.png" width="35%" alt="" />
|
|
214
|
+
</p>
|
|
215
|
+
|
|
216
|
+
#### MIS
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
from data4co.solver import KaMISSolver
|
|
220
|
+
from data4co import draw_mis_problem, draw_mis_solution
|
|
221
|
+
|
|
222
|
+
# use KaMISSolver to solve the problem
|
|
223
|
+
mis_solver = KaMISSolver()
|
|
224
|
+
mis_solver.solve(src="examples/mis_example")
|
|
225
|
+
|
|
226
|
+
# draw
|
|
227
|
+
draw_mis_problem(
|
|
228
|
+
save_path="docs/assets/mis_problem.png",
|
|
229
|
+
gpickle_path="examples/mis/mis_example.gpickle"
|
|
230
|
+
)
|
|
231
|
+
draw_mis_solution(
|
|
232
|
+
save_path="docs/mis_solution.png",
|
|
233
|
+
gpickle_path="examples/mis/mis_example.gpickle",
|
|
234
|
+
result_path="examples/mis/solve/mis_example_unweighted.result"
|
|
235
|
+
)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Visualization Results:
|
|
239
|
+
|
|
240
|
+
<p>
|
|
241
|
+
<img src="docs/assets/mis_problem.png" width="35%" alt="" />
|
|
242
|
+
<img src="docs/assets/mis_solution.png" width="35%" alt="" />
|
|
243
|
+
</p>
|
|
244
|
+
|
|
245
|
+
### Develop ML4CO Algorithms
|
|
246
|
+
|
|
247
|
+
Please refer to `ml4co_kit/learning` for the base classes that facilitate a quick establishment of a ML4CO project. You can easily build a project by inheriting the base classes and additionally implement task-specific and methodology-specific functions according to [ML4CO Organization](#ML4CO Organization:). We provide an minimalistic exmple of build a simple ML4TSP project in `docs/project_example`.
|
|
248
|
+
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
<img src="docs/assets/ml4co-kit-logo.png" alt="ML4CO-Kit" width="800"/>
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/pypi/data4co/) [](https://pypi.python.org/pypi/data4co/) [](https://pepy.tech/project/data4co) [](https://GitHub.com/Thinklab-SJTU/ML4CO-Kit/stargazers/)
|
|
4
|
+
|
|
5
|
+
`ML4CO-Kit` is a in-development toolkit for machine learning practices on combinatorial optimization problems, which is a by-product of our research for a unified modular framework that integrates existing ML4CO practices, minimizing disparities among methods and supporting the investigations via in-depth analysis and transparent ablation.
|
|
6
|
+
|
|
7
|
+
This reposity focuses on the supporting code for method development instead of implementing core technologies, which will be presented in the future in our full implementation and organization. `ML4CO-Kit` has the following features:
|
|
8
|
+
|
|
9
|
+
* The skeleton of framework organization for ML4CO projects;
|
|
10
|
+
* Implemented base classes that facilitate method development;
|
|
11
|
+
* Mainstream traditional solver baselines and reference solution acquisition;
|
|
12
|
+
* Data generation of various distributions;
|
|
13
|
+
* Problem and solution visualization;
|
|
14
|
+
* Evaluators for different problems.
|
|
15
|
+
|
|
16
|
+
| Problem | Data | solver | Supervision | evaluator | visualization |
|
|
17
|
+
| :-----: | :--------------------------------: | :-----------: | :-------------------: | :-------: | :---------------------: |
|
|
18
|
+
| TSP | Uniform, Gaussian, Cluster, TSPLIB | LKH, Concorde | Solution, Edge Regret | ✔ | Problem Graph, Solution |
|
|
19
|
+
| MIS | SATLIB, ER, BA, HK, WS | KaMIS, Gurobi | Solution | ✔ | Problem Graph, Solution |
|
|
20
|
+
|
|
21
|
+
###### ML4CO Organization:
|
|
22
|
+
|
|
23
|
+
<img src="docs/assets/organization.jpg" alt="Organization" width="800"/>
|
|
24
|
+
|
|
25
|
+
We are still enriching the library and we welcome any contributions/ideas/suggestions from the community. A comprehensive modular framework built upon this library that integrates core ML4CO technologies coming soon.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
You can install the stable release on PyPI:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
$ pip install ml4co_kit
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
or get the latest version by running:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
$ pip install -U https://github.com/Thinklab-SJTU/ML4CO-Kit/archive/master.zip # with --user for user install (no root)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The following packages are required, and shall be automatically installed by ``pip``:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
Python >= 3.8
|
|
45
|
+
numpy>=1.24.4
|
|
46
|
+
networkx==2.8.8
|
|
47
|
+
tsplib95==0.7.1
|
|
48
|
+
tqdm>=4.66.1
|
|
49
|
+
pulp>=2.8.0,
|
|
50
|
+
pandas>=2.0.0,
|
|
51
|
+
scipy>=1.10.1
|
|
52
|
+
requests>=2.31.0
|
|
53
|
+
aiohttp>=3.9.3
|
|
54
|
+
async_timeout>=4.0.3
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
To ensure you have access to all functions, such as visualization, you'll need to install the following packages using `pip`:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
$ pip install matplotlib>=3.7.5
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Usage Examples
|
|
64
|
+
|
|
65
|
+
### Solve with Traditional Solver Baselines
|
|
66
|
+
|
|
67
|
+
We provide base classes that offer a user-friendly approach for implementing traditional and learning-based solvers. Taking `TSPSolver` as an example, which includes functionalities for data input and output, as well as an evaluation function. The solver supports different data inputs, such as Numpy arrays and .txt and .tsp files. The outputs can be saved to corresponding types of files as needed. Additionally, the solver offers an evaluation function, by which users can quickly obtain the average tour length, average gap, and standard deviation of the test dataset. Traditional solvers are directly incorporated in our library inheriting `TSPSolver`.
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
>>> from ml4co_kit.solver import TSPLKHSolver
|
|
71
|
+
|
|
72
|
+
# initialization
|
|
73
|
+
>>> tsp_lkh_solver = TSPLKHSolver(lkh_max_trials=500)
|
|
74
|
+
|
|
75
|
+
# input instances and reference solutions by a .txt file
|
|
76
|
+
>>> tsp_lkh_solver.from_txt("path/to/read/tsp500_concorde.txt")
|
|
77
|
+
|
|
78
|
+
# lkh solving
|
|
79
|
+
>>> tsp_lkh_solver.solve()
|
|
80
|
+
|
|
81
|
+
# evaluate
|
|
82
|
+
>>> tsp_lkh_solver.evaluate(caculate_gap=True)
|
|
83
|
+
(16.583557978549532, 0.21424058722308548, 0.09031979488795724)
|
|
84
|
+
|
|
85
|
+
# save solving results
|
|
86
|
+
>>> tsp_lkh_solver.to_txt("path/to/write/tsp500_lkh.txt")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Data Generation
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from ml4co_kit import TSPDataGenerator
|
|
93
|
+
|
|
94
|
+
# initialization
|
|
95
|
+
tsp_data_lkh = TSPDataGenerator(
|
|
96
|
+
num_threads=8,
|
|
97
|
+
nodes_num=50,
|
|
98
|
+
data_type="uniform",
|
|
99
|
+
solver="lkh",
|
|
100
|
+
train_samples_num=16,
|
|
101
|
+
val_samples_num=16,
|
|
102
|
+
test_samples_num=16,
|
|
103
|
+
save_path="path/to/save/"
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# generate
|
|
107
|
+
tsp_data_lkh.generate()
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Evaluate
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
>>> from ml4co_kit.evaluate import TSPLIBOriEvaluator
|
|
114
|
+
>>> from ml4co_kit.solver import TSPLKHSolver
|
|
115
|
+
|
|
116
|
+
>>> lkh_solver = TSPLKHSolver(scale=1)
|
|
117
|
+
>>> evaluator = TSPLIBOriEvaluator()
|
|
118
|
+
>>> evaluator.evaluate(lkh_solver, norm="EUC_2D")
|
|
119
|
+
solved_costs ref_costs gaps
|
|
120
|
+
att48 33523.708507 33523.708507 0.000000e+00
|
|
121
|
+
eil51 429.983312 429.983312 0.000000e+00
|
|
122
|
+
berlin52 7544.365902 7544.365902 3.616585e-14
|
|
123
|
+
st70 678.557469 678.597452 -5.892021e-03
|
|
124
|
+
eil76 545.229738 545.387552 -2.893612e-02
|
|
125
|
+
pr76 108159.438274 108159.438274 -1.345413e-14
|
|
126
|
+
kroA100 21285.443182 21285.443182 0.000000e+00
|
|
127
|
+
kroC100 20750.762504 20750.762504 0.000000e+00
|
|
128
|
+
kroD100 21294.290821 21294.290821 3.416858e-14
|
|
129
|
+
rd100 7910.396210 7910.396210 0.000000e+00
|
|
130
|
+
eil101 642.856874 642.309536 8.521414e-02
|
|
131
|
+
lin105 14382.995933 14382.995933 0.000000e+00
|
|
132
|
+
ch130 6110.900592 6110.860950 6.487238e-04
|
|
133
|
+
ch150 6530.902722 6532.280933 -2.109847e-02
|
|
134
|
+
tsp225 3859.000000 3859.000000 0.000000e+00
|
|
135
|
+
a280 2588.301213 2586.769648 5.920765e-02
|
|
136
|
+
pr1002 260277.189980 259066.663053 4.672646e-01
|
|
137
|
+
pr2392 384469.093320 378062.826191 1.694498e+00
|
|
138
|
+
AVG 50054.634253 49631.448887 1.250504e-01
|
|
139
|
+
|
|
140
|
+
>>> eva.evaluate(lkh_solver, norm="GEO")
|
|
141
|
+
solved_costs ref_costs gaps
|
|
142
|
+
ulysses16 6859.0 6859.0 0.000000
|
|
143
|
+
ulysses22 7013.0 7013.0 0.000000
|
|
144
|
+
gr96 55209.0 55209.0 0.000000
|
|
145
|
+
gr202 40160.0 40160.0 0.000000
|
|
146
|
+
gr666 295012.0 294358.0 0.222178
|
|
147
|
+
AVG 80850.6 80719.8 0.044436
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Visualization
|
|
151
|
+
|
|
152
|
+
#### TSP
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
from data4co.solver import TSPConcordeSolver
|
|
156
|
+
from data4co.draw.tsp import draw_tsp_solution, draw_tsp_problem
|
|
157
|
+
|
|
158
|
+
# use TSPConcordeSolver to solve the problem
|
|
159
|
+
solver = TSPConcordeSolver(scale=1)
|
|
160
|
+
solver.from_tsp("examples/tsp/kroA150.tsp")
|
|
161
|
+
solver.solve(norm="EUC_2D")
|
|
162
|
+
|
|
163
|
+
# draw
|
|
164
|
+
draw_tsp_problem(
|
|
165
|
+
save_path="docs/assets/kroA150_problem.png",
|
|
166
|
+
points=solver.ori_points,
|
|
167
|
+
)
|
|
168
|
+
draw_tsp_solution(
|
|
169
|
+
save_path="docs/assets/kroA150_solution.png",
|
|
170
|
+
points=solver.ori_points,
|
|
171
|
+
tours=solver.tours
|
|
172
|
+
)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Visualization Results:
|
|
176
|
+
|
|
177
|
+
<p>
|
|
178
|
+
<img src="docs/assets/kroA150_problem.png" width="35%" alt="" />
|
|
179
|
+
<img src="docs/assets/kroA150_solution.png" width="35%" alt="" />
|
|
180
|
+
</p>
|
|
181
|
+
|
|
182
|
+
#### MIS
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
from data4co.solver import KaMISSolver
|
|
186
|
+
from data4co import draw_mis_problem, draw_mis_solution
|
|
187
|
+
|
|
188
|
+
# use KaMISSolver to solve the problem
|
|
189
|
+
mis_solver = KaMISSolver()
|
|
190
|
+
mis_solver.solve(src="examples/mis_example")
|
|
191
|
+
|
|
192
|
+
# draw
|
|
193
|
+
draw_mis_problem(
|
|
194
|
+
save_path="docs/assets/mis_problem.png",
|
|
195
|
+
gpickle_path="examples/mis/mis_example.gpickle"
|
|
196
|
+
)
|
|
197
|
+
draw_mis_solution(
|
|
198
|
+
save_path="docs/mis_solution.png",
|
|
199
|
+
gpickle_path="examples/mis/mis_example.gpickle",
|
|
200
|
+
result_path="examples/mis/solve/mis_example_unweighted.result"
|
|
201
|
+
)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Visualization Results:
|
|
205
|
+
|
|
206
|
+
<p>
|
|
207
|
+
<img src="docs/assets/mis_problem.png" width="35%" alt="" />
|
|
208
|
+
<img src="docs/assets/mis_solution.png" width="35%" alt="" />
|
|
209
|
+
</p>
|
|
210
|
+
|
|
211
|
+
### Develop ML4CO Algorithms
|
|
212
|
+
|
|
213
|
+
Please refer to `ml4co_kit/learning` for the base classes that facilitate a quick establishment of a ML4CO project. You can easily build a project by inheriting the base classes and additionally implement task-specific and methodology-specific functions according to [ML4CO Organization](#ML4CO Organization:). We provide an minimalistic exmple of build a simple ML4TSP project in `docs/project_example`.
|
|
214
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import importlib.util
|
|
2
|
+
|
|
3
|
+
# base
|
|
4
|
+
from .data import TSPLIBOriDataset, TSPUniformDataset
|
|
5
|
+
from .data import SATLIBOriDataset
|
|
6
|
+
from .evaluate import TSPEvaluator, TSPLIBOriEvaluator, TSPUniformEvaluator
|
|
7
|
+
from .evaluate import SATLIBEvaluator
|
|
8
|
+
from .generator import TSPDataGenerator, MISDataGenerator
|
|
9
|
+
from .solver import TSPSolver, TSPLKHSolver, TSPConcordeSolver
|
|
10
|
+
from .solver import MISSolver, KaMISSolver, MISGurobi
|
|
11
|
+
from .utils import download, compress_folder, extract_archive, _get_md5
|
|
12
|
+
|
|
13
|
+
# expand
|
|
14
|
+
found_matplotlib = importlib.util.find_spec("matplotlib")
|
|
15
|
+
if found_matplotlib is not None:
|
|
16
|
+
from .draw.tsp import draw_tsp_problem, draw_tsp_solution
|
|
17
|
+
from .draw.mis import draw_mis_problem, draw_mis_solution
|
|
18
|
+
else:
|
|
19
|
+
print("matplotlib not installed")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
__version__ = "0.0.1a1"
|
|
23
|
+
__author__ = "SJTU-ReThinkLab"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .satlib_original import SATLIBOriDataset
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from ml4co_kit.utils import download, extract_archive
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SATLIBOriDataset:
|
|
6
|
+
def __init__(self) -> None:
|
|
7
|
+
self.url = "https://huggingface.co/datasets/ML4CO/SATLIBOriDataset/resolve/main/satlib_original.tar.gz?download=true"
|
|
8
|
+
self.md5 = "0da8a73e2b79a6b5e6156005959ce509"
|
|
9
|
+
self.dir = "dataset/satlib_original/"
|
|
10
|
+
if not os.path.exists("dataset"):
|
|
11
|
+
os.mkdir("dataset")
|
|
12
|
+
if not os.path.exists(self.dir):
|
|
13
|
+
download(filename="dataset/satlib_original.tar.gz", url=self.url, md5=self.md5)
|
|
14
|
+
extract_archive(archive_path="dataset/satlib_original.tar.gz", extract_path=self.dir)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from ml4co_kit.utils import download, extract_archive
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
TSP_UNIFORM_50 = [
|
|
6
|
+
"dataset/tsp_uniform/tsp50_lkh_100_5.68760.txt",
|
|
7
|
+
"dataset/tsp_uniform/tsp50_lkh_500_5.68759.txt",
|
|
8
|
+
"dataset/tsp_uniform/tsp50_concorde_5.68759.txt",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
TSP_UNIFORM_100 = [
|
|
12
|
+
"dataset/tsp_uniform/tsp100_lkh_100_7.75653.txt",
|
|
13
|
+
"dataset/tsp_uniform/tsp100_lkh_500_7.75598.txt",
|
|
14
|
+
"dataset/tsp_uniform/tsp100_lkh_1k_7.75592.txt",
|
|
15
|
+
"dataset/tsp_uniform/tsp100_lkh_5k_7.75588.txt",
|
|
16
|
+
"dataset/tsp_uniform/tsp100_concorde_5_7.75585.txt",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
TSP_UNIFORM_500 = [
|
|
20
|
+
"dataset/tsp_uniform/tsp500_lkh_100_16.65813.txt"
|
|
21
|
+
"dataset/tsp_uniform/tsp500_lkh_500_16.59910.txt",
|
|
22
|
+
"dataset/tsp_uniform/tsp500_lkh_5k_16.55766.txt",
|
|
23
|
+
"dataset/tsp_uniform/tsp500_lkh_10k_16.55283.txt",
|
|
24
|
+
"dataset/tsp_uniform/tsp500_lkh_100k_16.54753.txt",
|
|
25
|
+
"dataset/tsp_uniform/tsp500_concorde_16.54581.txt",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
TSP_UNIFORM_1000 = [
|
|
29
|
+
"dataset/tsp_uniform/tsp1000_concorde_23.11812.txt",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
TSP_UNIFORM_10000 = [
|
|
33
|
+
"dataset/tsp_uniform/tsp10000_lkh_100k_72.23920.txt",
|
|
34
|
+
"dataset/tsp_uniform/tsp10000_lkh_1m_72.00978.txt",
|
|
35
|
+
"dataset/tsp_uniform/tsp10000_lkh_5m_71.93551.txt",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class TSPUniformDataset:
|
|
40
|
+
def __init__(self):
|
|
41
|
+
self.url = "https://huggingface.co/datasets/ML4CO/TSPUniformDataset/resolve/main/tsp_uniform.tar.gz?download=true"
|
|
42
|
+
self.md5 = "494766b3ab67105563e855c7a1f22d80"
|
|
43
|
+
self.dir = "dataset/tsp_uniform"
|
|
44
|
+
self.raw_data_path = "dataset/tsp_uniform.tar.gz"
|
|
45
|
+
if not os.path.exists("dataset"):
|
|
46
|
+
os.mkdir("dataset")
|
|
47
|
+
if not os.path.exists(self.dir):
|
|
48
|
+
download(filename=self.raw_data_path, url=self.url, md5=self.md5)
|
|
49
|
+
extract_archive(archive_path=self.raw_data_path, extract_path=self.dir)
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def supported(self):
|
|
53
|
+
supported_files = {
|
|
54
|
+
50: TSP_UNIFORM_50,
|
|
55
|
+
100: TSP_UNIFORM_100,
|
|
56
|
+
500: TSP_UNIFORM_500,
|
|
57
|
+
1000: TSP_UNIFORM_1000,
|
|
58
|
+
10000: TSP_UNIFORM_10000,
|
|
59
|
+
}
|
|
60
|
+
return supported_files
|