Implement TravelingSalesmanPerturber

This commit is contained in:
2025-12-08 15:10:24 -06:00
parent 4137378bb8
commit 1d44980a7b
23 changed files with 128 additions and 90 deletions

View File

@@ -22,7 +22,7 @@ def test_mem_component(
for h5 in [tsp_gp_h5, tsp_pyo_h5]:
clf = Mock(wraps=DummyClassifier())
comp = MemorizingLazyComponent(clf=clf, extractor=default_extractor)
comp.fit(tsp_gp_h5)
comp.fit(h5)
# Should call fit method with correct arguments
clf.fit.assert_called()
@@ -43,7 +43,7 @@ def test_mem_component(
# Call before-mip
stats: Dict[str, Any] = {}
model = Mock()
comp.before_mip(tsp_gp_h5[0], model, stats)
comp.before_mip(h5[0], model, stats)
# Should call predict with correct args
clf.predict.assert_called()

View File

@@ -7,6 +7,7 @@ from miplearn.collectors.basic import BasicCollector
from miplearn.io import write_pkl_gz
from miplearn.problems.tsp import (
TravelingSalesmanGenerator,
TravelingSalesmanPerturber,
build_tsp_model_gurobipy,
build_tsp_model_pyomo,
)
@@ -16,12 +17,19 @@ gen = TravelingSalesmanGenerator(
x=uniform(loc=0.0, scale=1000.0),
y=uniform(loc=0.0, scale=1000.0),
n=randint(low=20, high=21),
gamma=uniform(loc=1.0, scale=0.25),
fix_cities=True,
gamma=uniform(loc=1.0, scale=0.0),
round=True,
)
data = gen.generate(3)
# Generate a reference instance with fixed cities
reference_instance = gen.generate(1)[0]
# Generate perturbed instances with same cities but different distance scaling
perturber = TravelingSalesmanPerturber(
gamma=uniform(loc=1.0, scale=0.25),
round=True,
)
data = perturber.perturb(reference_instance, 3)
params = {"seed": 42, "threads": 1}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -17,56 +17,30 @@ def test_tsp_generator() -> None:
gen = TravelingSalesmanGenerator(
x=uniform(loc=0.0, scale=1000.0),
y=uniform(loc=0.0, scale=1000.0),
n=randint(low=3, high=4),
n=randint(low=5, high=6),
gamma=uniform(loc=1.0, scale=0.25),
fix_cities=True,
round=True,
)
data = gen.generate(2)
data = gen.generate(1)
assert data[0].distances.tolist() == [
[0.0, 591.0, 996.0],
[591.0, 0.0, 765.0],
[996.0, 765.0, 0.0],
]
assert data[1].distances.tolist() == [
[0.0, 556.0, 853.0],
[556.0, 0.0, 779.0],
[853.0, 779.0, 0.0],
[0.0, 525.0, 950.0, 392.0, 382.0],
[525.0, 0.0, 752.0, 761.0, 178.0],
[950.0, 752.0, 0.0, 809.0, 721.0],
[392.0, 761.0, 809.0, 0.0, 700.0],
[382.0, 178.0, 721.0, 700.0, 0.0],
]
def test_tsp() -> None:
data = TravelingSalesmanData(
n_cities=6,
distances=squareform(
pdist(
[
[0.0, 0.0],
[1.0, 0.0],
[2.0, 0.0],
[3.0, 0.0],
[0.0, 1.0],
[3.0, 1.0],
]
)
),
)
model = build_tsp_model_gurobipy(data)
model = build_tsp_model_gurobipy(data[0])
model.optimize()
assert model.inner.getAttr("x", model.inner.getVars()) == [
1.0,
0.0,
0.0,
1.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0,
1.0,
0.0,
0.0,
]