Skip to main content

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

Project description

ML4CO-Kit

PyPi version PyPI pyversions Downloads GitHub stars

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.

This repository 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:

  • The skeleton of framework organization for ML4CO projects;
  • Implemented base classes that facilitate method development;
  • Mainstream traditional solver baselines and reference solution acquisition;
  • Data generation of various distributions;
  • Problem and solution visualization;
  • Evaluators for different problems.
Problem Data solver Supervision evaluator visualization
TSP Uniform, Gaussian, Cluster, TSPLIB LKH, Concorde Solution, Edge Regret Problem Graph, Solution
MIS SATLIB, ER, BA, HK, WS KaMIS, Gurobi Solution Problem Graph, Solution
CVRP Uniform, Gaussian, VRPLIB LKH, PyVRP Solution Problem Graph, Solution
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 coming soon.

Installation

You can install the stable release on PyPI:

$ pip install ml4co_kit

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
tsplib95==0.7.1
tqdm>=4.66.1
pulp>=2.8.0, 
pandas>=2.0.0,
scipy>=1.10.1
requests>=2.31.0
aiohttp>=3.9.3
async_timeout>=4.0.3

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

>>> 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")

# lkh solving
>>> tsp_lkh_solver.solve()

# evaluate
>>> tsp_lkh_solver.evaluate(calculate_gap=True)
(16.583557978549532, 0.21424058722308548, 0.09031979488795724)

# 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
att48      33523.708507   33523.708507  0.000000e+00
eil51        429.983312     429.983312  0.000000e+00
berlin52    7544.365902    7544.365902  3.616585e-14
st70         678.557469     678.597452 -5.892021e-03
eil76        545.229738     545.387552 -2.893612e-02
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.856874     642.309536  8.521414e-02
lin105     14382.995933   14382.995933  0.000000e+00
ch130       6110.900592    6110.860950  6.487238e-04
ch150       6530.902722    6532.280933 -2.109847e-02
tsp225      3859.000000    3859.000000  0.000000e+00
a280        2588.301213    2586.769648  5.920765e-02
pr1002    260277.189980  259066.663053  4.672646e-01
pr2392    384469.093320  378062.826191  1.694498e+00
AVG        50054.634253   49631.448887  1.250504e-01

>>> eva.evaluate(lkh_solver, norm="GEO")
           solved_costs  ref_costs      gaps
ulysses16        6859.0    6859.0  0.000000
ulysses22        7013.0    7013.0  0.000000
gr96            55209.0   55209.0  0.000000
gr202           40160.0   40160.0  0.000000
gr666          295012.0  294358.0  0.222178
AVG             80850.6   80719.8  0.044436

Visualization

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_tsp("examples/tsp/kroA150.tsp")
solver.solve(norm="EUC_2D")

# draw
draw_tsp_problem(
    save_path="docs/assets/kroA150_problem.png",
    points=solver.ori_points,
)
draw_tsp_solution(
    save_path="docs/assets/kroA150_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()
mis_solver.solve(src="examples/mis_example")

# draw
draw_mis_problem(
    save_path="docs/assets/mis_problem.png", 
    gpickle_path="examples/mis/mis_example.gpickle"
)
draw_mis_solution(
    save_path="docs/mis_solution.png",
    gpickle_path="examples/mis/mis_example.gpickle",
    result_path="examples/mis/solve/mis_example_unweighted.result"
)

Visualization Results:

CVRP

from ml4co_kit import CVRPPyVRPSolver
from ml4co_kit import draw_cvrp_problem, draw_cvrp_solution

# use KaMISSolver to solve the problem
solver = CVRPPyVRPSolver(
    depots_scale=1,
    points_scale=1,
    time_limit=1
)
solver.from_vrp("examples/cvrp/A-n32-k5.vrp")
solver.solve()

# draw
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 Algorithms

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.

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.0.2.tar.gz (3.7 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ml4co_kit-0.0.2-cp311-cp311-manylinux2014_x86_64.whl (13.8 MB view details)

Uploaded CPython 3.11

ml4co_kit-0.0.2-cp310-cp310-manylinux2014_x86_64.whl (13.7 MB view details)

Uploaded CPython 3.10

ml4co_kit-0.0.2-cp39-cp39-manylinux2014_x86_64.whl (13.7 MB view details)

Uploaded CPython 3.9

ml4co_kit-0.0.2-cp38-cp38-manylinux2014_x86_64.whl (13.8 MB view details)

Uploaded CPython 3.8

File details

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

File metadata

  • Download URL: ml4co_kit-0.0.2.tar.gz
  • Upload date:
  • Size: 3.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for ml4co_kit-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f5033dacc27a60ae6f6914fe5822279050eaf5ed3ce9e5d05d2d83032eae1c63
MD5 c0a5bc06f8a8e6467671fb1b8a89529d
BLAKE2b-256 f94d4e43a1164c48b2cf0dff7648763eb02dc40818860a0b66781839e16ee3f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ml4co_kit-0.0.2-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a01d88355394ce493821a21ff830563a7dcc738889f774003eedb798b770c5b0
MD5 3701770b137e45d0344ce494c1f91789
BLAKE2b-256 6b51eed677f868c73c62007d4235cc9b555bd21e4df8869ba9ce694a17948c95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ml4co_kit-0.0.2-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2045589b0ad45b8e88e2ab4f014413a1c511a0bf4a2e6a73494ff70dc8f7ea2e
MD5 5513239fee53f3aea7546d012528d931
BLAKE2b-256 3c8e042aecdf2703771d4069e07b1a701ec08eb865d9fe0b6657625f3ecb7fcc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ml4co_kit-0.0.2-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 87c30b55769bbd261f06600526aaaa380c1716ea696fbb96f027a0d4fca925a3
MD5 0f2d2bd72b20f4030abe5fb49a60b2aa
BLAKE2b-256 3b8e4e5b09ded69975d9073d88a96abd921b286f33bda03497a69c7dcd980693

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ml4co_kit-0.0.2-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bd07daaa212408ed0d15566b277608cd930740d868b1409980f67369f84f5e15
MD5 67ca8e42fd6089f221c58f9817c3c4cc
BLAKE2b-256 785ec8260e9d731659b1cc228ff99e4ffb540faa6f23fb494de1506566c4ac27

See more details on using hashes here.

Supported by

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