Skip to content

Commit 6818af5

Browse files
committed
add tests
1 parent f89e635 commit 6818af5

91 files changed

Lines changed: 6178 additions & 2 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ml4co_kit/solver/lib/fem/mcl_fem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import torch
1818
from torch import Tensor
1919
from typing import Callable
20+
from ml4co_kit.task.graph.mcl import MClTask
2021
from ml4co_kit.utils import to_tensor, to_numpy
21-
from ml4co_kit.task.graph.mcl import MCLTask
2222

2323

2424
def energy_mcl(adj: Tensor, p: Tensor, penalty: float = 10.0) -> Tensor:
@@ -54,7 +54,7 @@ def entropy_mcl(p: Tensor) -> Tensor:
5454

5555

5656
def mcl_fem(
57-
task_data: MCLTask,
57+
task_data: MClTask,
5858
num_trials: int,
5959
betas: Tensor,
6060
grad_opt_class: Callable,

tests/__init__.py

Whitespace-only changes.

tests/extension_test/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
r"""
2+
Extension Test Module.
3+
"""
4+
5+
# Copyright (c) 2024 Thinklab@SJTU
6+
# ML4CO-Kit is licensed under Mulan PSL v2.
7+
# You can use this software according to the terms and conditions of the Mulan PSL v2.
8+
# You may obtain a copy of Mulan PSL v2 at:
9+
# http://license.coscl.org.cn/MulanPSL2
10+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11+
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12+
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13+
# See the Mulan PSL v2 for more details.
14+
15+
16+
from .mis_mcmc import MISMCMCTester

tests/extension_test/mis_mcmc.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
r"""
2+
Test MIS MCMC Module.
3+
"""
4+
5+
# Copyright (c) 2024 Thinklab@SJTU
6+
# ML4CO-Kit is licensed under Mulan PSL v2.
7+
# You can use this software according to the terms and conditions of the Mulan PSL v2.
8+
# You may obtain a copy of Mulan PSL v2 at:
9+
# http://license.coscl.org.cn/MulanPSL2
10+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11+
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12+
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13+
# See the Mulan PSL v2 for more details.
14+
15+
16+
17+
import numpy as np
18+
from ml4co_kit import MISTask
19+
from ml4co_kit.extension.mcmc import mis_mcmc
20+
21+
22+
class MISMCMCTester(object):
23+
"""Test cases for MIS MCMC."""
24+
25+
def __init__(self) -> None:
26+
pass
27+
28+
def test(self):
29+
# Get MIS Task
30+
mis_task = MISTask()
31+
mis_task.from_pickle("test_dataset/graph/mis/task/mis_er-700-800_no-weighted_task.pkl")
32+
33+
# Test final solution and cost list (Basic)
34+
final_sol = mis_mcmc(
35+
task_data=mis_task,
36+
ref=True,
37+
penalty_coeff=1.001,
38+
tau=0.50,
39+
steps=1000,
40+
return_type="final_sol",
41+
return_cost_list=False
42+
)
43+
44+
# Test final solution and cost list (Variable Temperature)
45+
tau_list_1 = np.array([3.0] * 20)
46+
tau_list_2 = np.linspace(0.10, 0.01, 980)
47+
tau_list = np.concatenate([tau_list_1, tau_list_2])
48+
49+
# Test best solution
50+
best_sol = mis_mcmc(
51+
task_data=mis_task,
52+
ref=True,
53+
penalty_coeff=1.001,
54+
tau=tau_list,
55+
steps=1000,
56+
return_type="best_sol",
57+
return_cost_list=False
58+
)
59+
60+
# Test mean solution
61+
mean_sol = mis_mcmc(
62+
task_data=mis_task,
63+
ref=True,
64+
penalty_coeff=1.001,
65+
tau=tau_list,
66+
steps=1000,
67+
return_type="mean_sol",
68+
return_cost_list=False
69+
)
70+
71+
# Test mean solution
72+
better_sol_list, cost_list = mis_mcmc(
73+
task_data=mis_task,
74+
ref=True,
75+
penalty_coeff=1.001,
76+
tau=tau_list,
77+
steps=1000,
78+
return_type="better_sol_list",
79+
return_cost_list=True
80+
)
81+
82+
# Test mean solution
83+
all_sol_list = mis_mcmc(
84+
task_data=mis_task,
85+
ref=True,
86+
penalty_coeff=1.001,
87+
tau=tau_list,
88+
steps=1000,
89+
return_type="all_sol_list",
90+
return_cost_list=False
91+
)

tests/generator_test/__init__.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
r"""
2+
Generator Test Module.
3+
"""
4+
5+
# Copyright (c) 2024 Thinklab@SJTU
6+
# ML4CO-Kit is licensed under Mulan PSL v2.
7+
# You can use this software according to the terms and conditions of the Mulan PSL v2.
8+
# You may obtain a copy of Mulan PSL v2 at:
9+
# http://license.coscl.org.cn/MulanPSL2
10+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11+
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12+
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13+
# See the Mulan PSL v2 for more details.
14+
15+
16+
# Base Class
17+
from .base import GenTesterBase
18+
19+
20+
# Routing Problems
21+
from .routing.atsp import ATSPGenTester
22+
from .routing.cvrp import CVRPGenTester
23+
from .routing.op import OPGenTester
24+
from .routing.pctsp import PCTSPGenTester
25+
from .routing.spctsp import SPCTSPGenTester
26+
from .routing.tsp import TSPGenTester
27+
28+
29+
# Graph Problems
30+
from .graph.mcut import MCutGenTester
31+
from .graph.mcl import MClGenTester
32+
from .graph.mis import MISGenTester
33+
from .graph.mvc import MVCGenTester
34+
35+
36+
# Portfolio Problems
37+
from .portfolio.minvarpo import MinVarPOGenTester
38+
from .portfolio.maxretpo import MaxRetPOGenTester
39+
from .portfolio.mopo import MOPOGenTester
40+
41+
42+
# QAP Problems
43+
from .qap.gm import GMGenTester
44+
45+
46+
# SAT Problems
47+
from .sat.satp import SATPGenTester
48+
from .sat.sata import SATAGenTester
49+
from .sat.usatc import USATCGenTester

tests/generator_test/base.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
r"""
2+
Base class for generator testers.
3+
"""
4+
5+
# Copyright (c) 2024 Thinklab@SJTU
6+
# ML4CO-Kit is licensed under Mulan PSL v2.
7+
# You can use this software according to the terms and conditions of the Mulan PSL v2.
8+
# You may obtain a copy of Mulan PSL v2 at:
9+
# http://license.coscl.org.cn/MulanPSL2
10+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11+
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12+
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13+
# See the Mulan PSL v2 for more details.
14+
15+
16+
from tqdm import tqdm
17+
from typing import Type, List
18+
from ml4co_kit import GeneratorBase
19+
20+
21+
class GenTesterBase(object):
22+
def __init__(
23+
self,
24+
test_gen_class: Type[GeneratorBase],
25+
test_args_list: List[dict]
26+
):
27+
self.test_gen_class = test_gen_class
28+
self.test_args_list = test_args_list
29+
30+
def test(self):
31+
# Test for each distribution type
32+
for test_args in tqdm(
33+
self.test_args_list,
34+
desc=f"Testing {self.test_gen_class.__name__}"
35+
):
36+
try:
37+
generator = self.test_gen_class(**test_args)
38+
generator.generate()
39+
except:
40+
raise ValueError(
41+
f"Error occurred when testing {self.test_gen_class}\n"
42+
f"Test args: {test_args} "
43+
)

tests/generator_test/graph/mcl.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
r"""
2+
Tester for MCl generator.
3+
"""
4+
5+
# Copyright (c) 2024 Thinklab@SJTU
6+
# ML4CO-Kit is licensed under Mulan PSL v2.
7+
# You can use this software according to the terms and conditions of the Mulan PSL v2.
8+
# You may obtain a copy of Mulan PSL v2 at:
9+
# http://license.coscl.org.cn/MulanPSL2
10+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11+
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12+
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13+
# See the Mulan PSL v2 for more details.
14+
15+
16+
from ml4co_kit import MClGenerator, GRAPH_TYPE
17+
from ml4co_kit.generator.graph.base import (
18+
GraphWeightGenerator, GRAPH_WEIGHT_TYPE
19+
)
20+
from tests.generator_test.base import GenTesterBase
21+
22+
23+
class MClGenTester(GenTesterBase):
24+
def __init__(self):
25+
super(MClGenTester, self).__init__(
26+
test_gen_class=MClGenerator,
27+
test_args_list=[
28+
# Uniform (w uniform weighted)
29+
{
30+
"distribution_type": GRAPH_TYPE.ER,
31+
"node_weighted": True,
32+
"node_weighted_gen": GraphWeightGenerator(
33+
weighted_type=GRAPH_WEIGHT_TYPE.UNIFORM),
34+
},
35+
# Uniform (w gaussian weighted)
36+
{
37+
"distribution_type": GRAPH_TYPE.ER,
38+
"node_weighted": True,
39+
"node_weighted_gen": GraphWeightGenerator(
40+
weighted_type=GRAPH_WEIGHT_TYPE.GAUSSIAN),
41+
},
42+
# Uniform (w poisson weighted)
43+
{
44+
"distribution_type": GRAPH_TYPE.ER,
45+
"node_weighted": True,
46+
"node_weighted_gen": GraphWeightGenerator(
47+
weighted_type=GRAPH_WEIGHT_TYPE.POISSON),
48+
},
49+
# Uniform (w exponential weighted)
50+
{
51+
"distribution_type": GRAPH_TYPE.ER,
52+
"node_weighted": True,
53+
"node_weighted_gen": GraphWeightGenerator(
54+
weighted_type=GRAPH_WEIGHT_TYPE.EXPONENTIAL),
55+
},
56+
# Uniform (w lognormal weighted)
57+
{
58+
"distribution_type": GRAPH_TYPE.ER,
59+
"node_weighted": True,
60+
"node_weighted_gen": GraphWeightGenerator(
61+
weighted_type=GRAPH_WEIGHT_TYPE.LOGNORMAL),
62+
},
63+
# Uniform (w powerlaw weighted)
64+
{
65+
"distribution_type": GRAPH_TYPE.ER,
66+
"node_weighted": True,
67+
"node_weighted_gen": GraphWeightGenerator(
68+
weighted_type=GRAPH_WEIGHT_TYPE.POWERLAW),
69+
},
70+
# Uniform (w binomial weighted)
71+
{
72+
"distribution_type": GRAPH_TYPE.ER,
73+
"node_weighted": True,
74+
"node_weighted_gen": GraphWeightGenerator(
75+
weighted_type=GRAPH_WEIGHT_TYPE.BINORMIAL),
76+
},
77+
# Watts-Strogatz (w/o weighted)
78+
{
79+
"distribution_type": GRAPH_TYPE.WS,
80+
"node_weighted": False,
81+
},
82+
# Barabasi-Albert (w/o weighted)
83+
{
84+
"distribution_type": GRAPH_TYPE.BA,
85+
"node_weighted": False,
86+
},
87+
# Holme-Kim (w/o weighted)
88+
{
89+
"distribution_type": GRAPH_TYPE.HK,
90+
"node_weighted": False,
91+
},
92+
# RB (w/o weighted)
93+
{
94+
"distribution_type": GRAPH_TYPE.RB,
95+
"node_weighted": False,
96+
},
97+
]
98+
)

0 commit comments

Comments
 (0)