mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-09 02:48:52 -06:00
problems: Allow correlated arguments in random problem generators
This commit is contained in:
@@ -32,6 +32,21 @@ def test_knapsack_generator() -> None:
|
||||
]
|
||||
|
||||
|
||||
def test_knapsack_generator_callable() -> None:
|
||||
np.random.seed(42)
|
||||
gen = MultiKnapsackGenerator(
|
||||
n=randint(low=10, high=11),
|
||||
m=lambda n: n // 3,
|
||||
w=randint(low=0, high=1000),
|
||||
K=randint(low=500, high=501),
|
||||
u=uniform(loc=0.0, scale=1.0),
|
||||
alpha=uniform(loc=0.25, scale=0.0),
|
||||
)
|
||||
data = gen.generate(1)[0]
|
||||
assert data.weights.shape[1] == 10
|
||||
assert data.weights.shape[0] == 3
|
||||
|
||||
|
||||
def test_knapsack_model() -> None:
|
||||
data = MultiKnapsackData(
|
||||
prices=np.array([344.0, 527.0, 658.0, 519.0, 460.0]),
|
||||
|
||||
@@ -36,3 +36,18 @@ def test_pmedian() -> None:
|
||||
assert model.inner.numConstrs == 11
|
||||
model.optimize()
|
||||
assert round(model.inner.objVal) == 107
|
||||
|
||||
|
||||
def test_pmedian_generator_callable() -> None:
|
||||
np.random.seed(42)
|
||||
gen = PMedianGenerator(
|
||||
x=uniform(loc=0.0, scale=100.0),
|
||||
y=uniform(loc=0.0, scale=100.0),
|
||||
n=randint(low=10, high=11),
|
||||
p=lambda n: n // 5,
|
||||
demands=uniform(loc=0, scale=20),
|
||||
capacities=uniform(loc=0, scale=100),
|
||||
)
|
||||
data = gen.generate(1)
|
||||
assert data[0].p == 2
|
||||
assert len(data[0].demands) == 10
|
||||
|
||||
@@ -35,6 +35,22 @@ def test_set_cover_generator() -> None:
|
||||
]
|
||||
|
||||
|
||||
def test_set_cover_generator_callable() -> None:
|
||||
np.random.seed(42)
|
||||
gen = SetCoverGenerator(
|
||||
n_elements=randint(low=4, high=5),
|
||||
n_sets=lambda n: n * 2,
|
||||
costs=uniform(loc=10.0, scale=0.0),
|
||||
density=uniform(loc=0.5, scale=0),
|
||||
K=uniform(loc=0, scale=0),
|
||||
)
|
||||
data = gen.generate(1)
|
||||
n_elements, n_sets = data[0].incidence_matrix.shape
|
||||
assert n_elements == 4
|
||||
assert n_sets == 8
|
||||
assert len(data[0].costs) == 8
|
||||
|
||||
|
||||
def test_set_cover() -> None:
|
||||
data = SetCoverData(
|
||||
costs=np.array([5, 10, 12, 6, 8]),
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
import numpy as np
|
||||
from scipy.stats import randint, uniform
|
||||
|
||||
from miplearn.problems.setpack import (
|
||||
SetPackData,
|
||||
SetPackGenerator,
|
||||
build_setpack_model_gurobipy,
|
||||
)
|
||||
|
||||
@@ -24,3 +26,19 @@ def test_setpack() -> None:
|
||||
model = build_setpack_model_gurobipy(data)
|
||||
model.optimize()
|
||||
assert model.inner.objval == -22.0
|
||||
|
||||
|
||||
def test_set_pack_generator_callable() -> None:
|
||||
np.random.seed(42)
|
||||
gen = SetPackGenerator(
|
||||
n_elements=randint(low=4, high=5),
|
||||
n_sets=lambda n: n * 2,
|
||||
costs=uniform(loc=10.0, scale=0.0),
|
||||
density=uniform(loc=0.5, scale=0),
|
||||
K=uniform(loc=0, scale=0),
|
||||
)
|
||||
data = gen.generate(1)
|
||||
n_elements, n_sets = data[0].incidence_matrix.shape
|
||||
assert n_elements == 4
|
||||
assert n_sets == 8
|
||||
assert len(data[0].costs) == 8
|
||||
|
||||
Reference in New Issue
Block a user