Skip to content

Commit

Permalink
update README.md, KaMIS, and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
heatingma committed Dec 25, 2024
1 parent 86fba7d commit 55bcddd
Show file tree
Hide file tree
Showing 51 changed files with 4,363 additions and 4,360 deletions.
69 changes: 24 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,45 +102,6 @@ matplotlib
pytorch_lightning
```

## Installation

You can install the stable release on PyPI:

```bash
$ pip install ml4co_kit
```

or get the latest version by running:

```bash
$ 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.1
pulp>=2.8.0,
pandas>=2.0.0,
scipy>=1.10.1
aiohttp>=3.9.3
requests>=2.31.0
async_timeout>=4.0.3
pyvrp>=0.6.3
cython>=3.0.8
gurobipy>=11.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
Expand All @@ -154,14 +115,14 @@ We provide base classes with a user-friendly approach for implementing tradition
>>> 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")
>>> tsp_lkh_solver.from_txt("path/to/read/tsp500_concorde.txt", ref=True)

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

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

# save solving results
>>> tsp_lkh_solver.to_txt("path/to/write/tsp500_lkh.txt")
Expand All @@ -177,7 +138,7 @@ tsp_data_lkh = TSPDataGenerator(
num_threads=8,
nodes_num=50,
data_type="uniform",
solver="lkh",
solver="LKH",
train_samples_num=16,
val_samples_num=16,
test_samples_num=16,
Expand Down Expand Up @@ -227,6 +188,24 @@ gr666 3843.137961 3952.535702 -2.767786e+00
AVG 1011.043859 1032.923407 -5.535573e-01
```

### Algorithm

```python
>>> 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.
Expand Down Expand Up @@ -327,6 +306,6 @@ Visualization Results:
<img src="docs/assets/cvrp_solution.png" width="35%" alt="" />
</p>

### Develop ML4CO Algorithms
### 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:). We provide an minimalistic exmple of build a simple ML4TSP project in `docs/project_example`.
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:).
1 change: 1 addition & 0 deletions ml4co_kit/solver/atsp/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
r"""
Basic solver for Asymmetric Traveling Salesman Problem (ATSP).
The ATSP problem requires finding the shortest tour that visits each
vertex of the graph exactly once and returns to the starting node
with the consideration that the travel costs between any two vertices
Expand Down
35 changes: 18 additions & 17 deletions ml4co_kit/solver/cvrp/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
r"""
Basic solver for Capacitated Vehicle Routing Problem (CVRP).
The CVRP problems requires finding the most efficient routes for a fleet of vehicles
with limited capacity to deliver goods to a set of customers while minimizing costs.
"""
Expand Down Expand Up @@ -46,23 +47,23 @@ class CVRPSolver(SolverBase):
:param solver_type: string, the type of the solver or algorithm used in the class.
:param depots_scale: int, the scale of the depots.
:parm points_scale: int, the scale of the customer points.
:parm demands_scale: int, the scale of the demands of customer points.
:parm capacities_scale: int, the scale of the capacities of the car.
:parm ori_depots: np.ndarray, the original depots coordinates data read.
:parm depots: np.ndarray, the depots coordinates data called by the solver during solving,
:param points_scale: int, the scale of the customer points.
:param demands_scale: int, the scale of the demands of customer points.
:param capacities_scale: int, the scale of the capacities of the car.
:param ori_depots: np.ndarray, the original depots coordinates data read.
:param depots: np.ndarray, the depots coordinates data called by the solver during solving,
they may initially be same as ``ori_depots``, but may later undergo standardization
or scaling processing.
:parm ori_points: np.ndarray, the original customer points coordinates data read.
:parm points: np.ndarray, the customer points coordinates data called by the solver
:param ori_points: np.ndarray, the original customer points coordinates data read.
:param points: np.ndarray, the customer points coordinates data called by the solver
during solving, they may initially be same as ``ori_depots``, but may later undergo
standardization or scaling processing.
:parm demands: np.ndarray, the demands of each customer points.
:parm capacities: np.ndarray, the capacities of the car.
:parm tours: np.ndarray, the solution to the problems.
:parm ref_tours: np.ndarray, the reference solutions to the problems.
:parm nodes_num: int, the number of points, i.e. the sum of depots points and customer points.
:parm norm: str, coordinate type. It can be a 2D Euler distance or geographic data type.
:param demands: np.ndarray, the demands of each customer points.
:param capacities: np.ndarray, the capacities of the car.
:param tours: np.ndarray, the solution to the problems.
:param ref_tours: np.ndarray, the reference solutions to the problems.
:param nodes_num: int, the number of points, i.e. the sum of depots points and customer points.
:param norm: str, coordinate type. It can be a 2D Euler distance or geographic data type.
"""
def __init__(
self,
Expand Down Expand Up @@ -1272,14 +1273,14 @@ def solve(
"""
This method will be implemented in subclasses.
:parm depots: np.ndarray, the depots coordinates data called by the solver during solving,
:param depots: np.ndarray, the depots coordinates data called by the solver during solving,
they may initially be same as ``ori_depots``, but may later undergo standardization
or scaling processing.
:parm points: np.ndarray, the customer points coordinates data called by the solver
:param points: np.ndarray, the customer points coordinates data called by the solver
during solving, they may initially be same as ``ori_depots``, but may later undergo
standardization or scaling processing.
:parm demands: np.ndarray, the demands of each customer points.
:parm capacities: np.ndarray, the capacities of the car.
:param demands: np.ndarray, the demands of each customer points.
:param capacities: np.ndarray, the capacities of the car.
:param norm: boolean, the normalization type for node coordinates.
:param normalize: boolean, whether to normalize node coordinates.
:param num_threads: int, number of threads(could also be processes) used in parallel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ inline void graph_access::set_partition_count(PartitionID count) {
}

inline int graph_access::build_from_metis(int n, int* xadj, int* adjncy) {
if(graphref != NULL) {
delete graphref;
}
graphref = new basicGraph();
start_construction(n, xadj[n]);

Expand All @@ -455,6 +458,9 @@ inline int graph_access::build_from_metis(int n, int* xadj, int* adjncy) {
}

inline int graph_access::build_from_metis_weighted(int n, int* xadj, int* adjncy, int * vwgt, int* adjwgt) {
if(graphref != NULL) {
delete graphref;
}
graphref = new basicGraph();
start_construction(n, xadj[n]);

Expand Down
Loading

0 comments on commit 55bcddd

Please sign in to comment.