Skip to main content

ml4co-kit provides convenient dataset generators for the combinatorial optimization problem

Project description

PyPi version PyPI pyversions Downloads Documentation Status GitHub stars

Combinatorial Optimization (CO) is a mathematical optimization area that involves finding the best solution from a large set of discrete possibilities, often under constraints. Widely applied in routing, logistics, hardware design, and biology, CO addresses NP-hard problems critical to computer science and industrial engineering.

ML4CO-Kit aims to provide foundational support for machine learning practices on CO problems, including the follow aspects.

  • algorithm: common post-processing algorithms.
  • data: common test datasets and our generated traning dataset.
  • draw: visualization of problems and solutions.
  • evaluate: evaluator for problems and solvers.
  • generator: data generation of various distributions.
  • learning: implemented base classes that facilitate method development for ML4CO.
  • solver: solvers' base classes and mainstream traditional solvers.
  • utils: general or commonly used functions and classes.

Official Documentation: https://ml4co-kit.readthedocs.io/en/latest/

Source Code: https://github.com/Thinklab-SJTU/ML4CO-Kit

Development status

Basic

Problem Generator Basic Solver (IO) Traditional Solver
ATSP sat, hcp, uniform tsplib, txt LKH
CVRP uniform, gaussian vrplib, txt LKH, HGS, PyVRP
LP uniform txt Gurobi
MCl er, ba, hk, ws, rb gpickle, txt, networkx Gurobi
MCut er, ba, hk, ws, rb gpickle, txt, networkx Gurobi
MIS er, ba, hk, ws, rb gpickle, txt, networkx Gurobi, KaMIS
MVC er, ba, hk, ws, rb gpickle, txt, networkx Gurobi
TSP uniform, gaussian, cluster tsplib, txt LKH, Concorde, GA-EAX

Extension

Problem Visualization Algorithm Test Dataset Train Dataset
ATSP 📆 2 📆 📆
CVRP 1 vrplib, uniform 📆
LP 📆 📆 📆 📆
MCl 6 📆 📆
MCut 3 📆 📆
MIS 6 📆 📆
MVC 5 📆 📆
TSP 5 satlib, uniform uniform

1~9: Number of supports; ✔: Supported; 📆: Planned for future versions (contributions welcomed!).

ML4CO Organization:

Organization

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 is coming soon.

Installation

You can install the stable release on PyPI:

$ pip install ml4co-kit
pip

or get the latest version by running:

$ pip install -U https://github.com/Thinklab-SJTU/ML4CO-Kit/archive/master.zip # with --user for user install (no root)

The following packages are required and shall be automatically installed by pip:

Python>=3.8
numpy>=1.24.4
networkx>=2.8.8
tqdm>=4.66.3
pulp>=2.8.0, 
pandas>=2.0.0,
scipy>=1.10.1
aiohttp>=3.10.11
requests>=2.32.0
async_timeout>=4.0.3
pyvrp>=0.6.3
cython>=3.0.8
gurobipy>=11.0.3
scikit-learn>=1.3.0

To ensure you have access to all functions, such as visualization, you'll need to install the following packages using pip:

matplotlib
pytorch_lightning

Usage Examples

Solve with Traditional Solver Baselines

We provide base classes with a user-friendly approach for implementing traditional and learning-based solvers. Taking TSPSolver as an example, it 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.

>>> from ml4co_kit.solver import TSPLKHSolver

# initialization
>>> tsp_lkh_solver = TSPLKHSolver(lkh_max_trials=500)

# input instances and reference solutions by a .txt file
>>> tsp_lkh_solver.from_txt("path/to/read/tsp500_concorde.txt", ref=True)

# lkh solving
>>> tsp_lkh_solver.solve(num_threads=10, show_time=True)

# evaluate
>>> tsp_lkh_solver.evaluate(calculate_gap=True)
(16.546304871926175, 16.545805334644392, 0.0030213676759083515, 0.009747905769875538)

# save solving results
>>> tsp_lkh_solver.to_txt("path/to/write/tsp500_lkh.txt")

Data Generation

from ml4co_kit import TSPDataGenerator

# initialization
tsp_data_lkh = TSPDataGenerator(
    num_threads=8,
    nodes_num=50,
    data_type="uniform",
    solver="LKH",
    train_samples_num=16,
    val_samples_num=16,
    test_samples_num=16,
    save_path="path/to/save/"
)

# generate
tsp_data_lkh.generate()

Evaluate

>>> from ml4co_kit.evaluate import TSPLIBOriEvaluator
>>> from ml4co_kit.solver import TSPLKHSolver

>>> lkh_solver = TSPLKHSolver(scale=1)
>>> evaluator = TSPLIBOriEvaluator()
>>> evaluator.evaluate(lkh_solver, norm="EUC_2D")
           solved_costs      ref_costs          gaps
eil51        429.983312     429.983312  0.000000e+00
berlin52    7544.365902    7544.365902  3.616585e-14
st70         677.881928     678.597452 -1.054416e-01
eil76        544.837795     545.387552 -1.008012e-01
pr76      108159.438274  108159.438274 -1.345413e-14
kroA100    21285.443182   21285.443182  0.000000e+00
kroC100    20750.762504   20750.762504  0.000000e+00
kroD100    21294.290821   21294.290821  3.416858e-14
rd100       7910.396210    7910.396210  0.000000e+00
eil101       642.244814     642.309536 -1.007642e-02
lin105     14382.995933   14382.995933  0.000000e+00
ch130       6110.739012    6110.860950 -1.995428e-03
ch150       6532.280933    6532.280933 -2.784616e-14
tsp225      3859.000000    3859.000000  0.000000e+00
a280        2587.930486    2586.769648  4.487600e-02
pr1002    259066.663053  259066.663053  0.000000e+00
pr2392    378062.826191  378062.826191  0.000000e+00
AVG        50578.945903   50578.963027 -1.020227e-02

>>> evaluator.evaluate(lkh_solver, norm="GEO")
           solved_costs    ref_costs          gaps
ulysses16     74.108736    74.108736  1.917568e-14
ulysses22     75.665149    75.665149  3.756248e-14
gr96         512.309380   512.309380  0.000000e+00
gr202        549.998070   549.998070 -8.268163e-14
gr666       3843.137961  3952.535702 -2.767786e+00
AVG         1011.043859  1032.923407 -5.535573e-01

Algorithm

>>> from ml4co-kit import TSPSolver, tsp_insertion_decoder

# create solver and load data
>>> solver = TSPSolver()
>>> solver.from_txt("your/txt/data/path", ref=True)

# use insertion algorithm to solve the problems
>>> tours = tsp_insertion_decoder(points=solver.points)
>>> solver.from_data(tours=tours, ref=False)

# evaluate (average length, ground truth, gap, std)
>>> solver.evaluate(calculate_gap=True)
(6.299320133465173, 5.790133693543183, 8.816004478345556, 3.605743337834312)

Visualization

Below, we use TSP, MIS, and CVRP as representative examples for illustration.

TSP

>>> from ml4co_kit.solver import TSPConcordeSolver
>>> from ml4co_kit.draw.tsp import draw_tsp_solution, draw_tsp_problem

# use TSPConcordeSolver to solve the problem
>>> solver = TSPConcordeSolver(scale=1)
>>> solver.from_tsplib(tsp_file_path="examples/tsp/tsplib/kroA150.tsp")
>>> solver.solve(norm="EUC_2D")

# draw images of problem and solution
>>> draw_tsp_problem(
        save_path="docs/assets/tsp_problem.png",
        points=solver.ori_points,
    )
>>> draw_tsp_solution(
        save_path="docs/assets/tsp_solution.png",
        points=solver.ori_points,
        tours=solver.tours
    )

Visualization Results:

MIS

>>> from ml4co_kit.solver import KaMISSolver
>>> from ml4co_kit import draw_mis_problem, draw_mis_solution

# use KaMISSolver to solve the problem
>>> mis_solver = KaMISSolver(time_limit=10)
>>> mis_solver.solve(
        src="examples/mis/instance",
        out="examples/mis/solution"
    )

# draw images of problem and solution
>>> draw_mis_problem(
        save_path="docs/assets/mis_problem.png", 
        graph_data=mis_solver.graph_data[0],
        self_loop=False
    )
>>> draw_mis_solution(
        save_path="docs/mis_solution.png",
        graph_data=mis_solver.graph_data[0],
        self_loop=False
    )

Visualization Results:

CVRP

>>> from ml4co_kit import CVRPHGSSolver
>>> from ml4co_kit import draw_cvrp_problem, draw_cvrp_solution

# use CVRPHGSSolver to solve the problem
>>> solver = CVRPHGSSolver(
    depots_scale=1,
    points_scale=1,
    time_limit=1.0
)
>>> solver.from_vrplib("examples/cvrp/vrplib_1/problem/A-n32-k5.vrp")
>>> solver.solve()

# draw images of problem and solution
>>> draw_cvrp_problem(
        save_path="docs/assets/cvrp_problem.png",
        depots=solver.depots[0],
        points=solver.points[0]
    )
>>> draw_cvrp_solution(
        save_path="docs/assets/cvrp_solution.png",
        depots=solver.depots[0],
        points=solver.points[0],
        tour=solver.tours
    )

Visualization Results:

Develop ML4CO Methods

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:).

Citation

If you find our code helpful in your research, please cite

@inproceedings{li2025streamlining,
    title={Streamlining the Design Space of ML4TSP Suggests Principles for Learning and Search}, 
    author={Yang Li and Jiale Ma and Wenzheng Pan and Runzhong Wang and Haoyu Geng and Nianzu Yang and Junchi Yan},
    year={2025},
    booktitle={International Conference on Learning Representations}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ml4co_kit-0.3.3.tar.gz (4.9 MB view details)

Uploaded Source

Built Distributions

ml4co_kit-0.3.3-cp311-cp311-manylinux2014_x86_64.whl (14.4 MB view details)

Uploaded CPython 3.11

ml4co_kit-0.3.3-cp310-cp310-manylinux2014_x86_64.whl (14.2 MB view details)

Uploaded CPython 3.10

ml4co_kit-0.3.3-cp39-cp39-manylinux2014_x86_64.whl (14.2 MB view details)

Uploaded CPython 3.9

ml4co_kit-0.3.3-cp38-cp38-manylinux2014_x86_64.whl (14.3 MB view details)

Uploaded CPython 3.8

File details

Details for the file ml4co_kit-0.3.3.tar.gz.

File metadata

  • Download URL: ml4co_kit-0.3.3.tar.gz
  • Upload date:
  • Size: 4.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for ml4co_kit-0.3.3.tar.gz
Algorithm Hash digest
SHA256 10b67fd5a58204b93304ef6a3890b1fedc0cc83e46d7101535cb29225629eb21
MD5 400cff21f1a9ab34ddaa271a292a6dae
BLAKE2b-256 67b301a18b4adf9a74da454818484a5080dae9b9299fb53ebc363d5c65d2c4f5

See more details on using hashes here.

File details

Details for the file ml4co_kit-0.3.3-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ml4co_kit-0.3.3-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78c736feb02748da07ff2eec6778f953e61828749791c4c4e5cf7012fe38c784
MD5 6e38ee0afada605d71ee346dcbbad328
BLAKE2b-256 44eb824768e2aa3029d4cacba9a38b859cf587b0d16eba2049caeac048b3add4

See more details on using hashes here.

File details

Details for the file ml4co_kit-0.3.3-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ml4co_kit-0.3.3-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9793e7e61c8b03a82ab5e97a65432e7987cc7ac029d5c3ccc6e203b5e5854116
MD5 202bf18fef7270b0905ca6f7108950c5
BLAKE2b-256 2e3a9e73319541034e5f416643ad1222f829287a39929189ab693ee1de6564a1

See more details on using hashes here.

File details

Details for the file ml4co_kit-0.3.3-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ml4co_kit-0.3.3-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7080697797a193146c295aaf125fed4e2d8277bfeceb1cddc35581164d359592
MD5 25ff4ca9864614b3f1bf983b58ea5f8f
BLAKE2b-256 186472cf9d0df4a6e9a9a57b328b62417868b20e4cf2a52fdbbccf331c6ec972

See more details on using hashes here.

File details

Details for the file ml4co_kit-0.3.3-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ml4co_kit-0.3.3-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cc139c1e45dc4a4fe169589b43e140b40049c2182673c6a6ac70e991ce5dcd54
MD5 c7f7e3318fe3316b20cdfa5d16a1a724
BLAKE2b-256 cf39db608cd2b6099ebfd4ef4ce8db24ce295739b29e257814930dce66c5b9e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page