mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-08 18:38:51 -06:00
Fix failing tests
This commit is contained in:
2
Makefile
2
Makefile
@@ -47,6 +47,6 @@ test:
|
||||
# rm -rf .mypy_cache
|
||||
$(MYPY) -p miplearn
|
||||
$(MYPY) -p tests
|
||||
$(PYTEST) $(PYTEST_ARGS)
|
||||
$(PYTEST) $(PYTEST_ARGS) .
|
||||
|
||||
.PHONY: test test-watch docs install dist
|
||||
|
||||
@@ -81,6 +81,7 @@ class MaxCutGenerator:
|
||||
else:
|
||||
graph = self._generate_graph()
|
||||
weights = self._generate_weights(graph)
|
||||
assert weights is not None
|
||||
return MaxCutData(graph, weights)
|
||||
|
||||
return [_sample() for _ in range(n_samples)]
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from glob import glob
|
||||
from glob import glob, escape
|
||||
from os.path import dirname, basename, isfile
|
||||
from tempfile import NamedTemporaryFile
|
||||
from typing import List, Any
|
||||
@@ -21,8 +21,8 @@ def _h5_fixture(pattern: str, request: Any) -> List[str]:
|
||||
.pkl.gz files, and return the path to the copy. Also register a finalizer,
|
||||
so that the temporary folder is removed after the tests.
|
||||
"""
|
||||
filenames = glob(f"{dirname(__file__)}/fixtures/{pattern}")
|
||||
print(filenames)
|
||||
fixtures_dir = escape(os.path.join(dirname(__file__), "fixtures"))
|
||||
filenames = glob(os.path.join(fixtures_dir, pattern))
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
|
||||
def cleanup() -> None:
|
||||
@@ -30,7 +30,6 @@ def _h5_fixture(pattern: str, request: Any) -> List[str]:
|
||||
|
||||
request.addfinalizer(cleanup)
|
||||
|
||||
print(tmpdir)
|
||||
for f in filenames:
|
||||
fbase, _ = os.path.splitext(f)
|
||||
for ext in [".h5", ".pkl.gz"]:
|
||||
|
||||
@@ -14,9 +14,10 @@ from miplearn.problems.maxcut import (
|
||||
build_maxcut_model_gurobipy,
|
||||
build_maxcut_model_pyomo,
|
||||
)
|
||||
from miplearn.solvers.abstract import AbstractModel
|
||||
|
||||
|
||||
def _set_seed():
|
||||
def _set_seed() -> None:
|
||||
random.seed(42)
|
||||
np.random.seed(42)
|
||||
|
||||
@@ -71,18 +72,18 @@ def test_maxcut_generator_fixed() -> None:
|
||||
assert data[2].weights.tolist() == [1, 1, -1, -1, -1, 1]
|
||||
|
||||
|
||||
def test_maxcut_model():
|
||||
def test_maxcut_model() -> None:
|
||||
_set_seed()
|
||||
data = MaxCutGenerator(
|
||||
n=randint(low=10, high=11),
|
||||
p=uniform(loc=0.5, scale=0.0),
|
||||
fix_graph=True,
|
||||
).generate(1)[0]
|
||||
for build_model in [
|
||||
build_maxcut_model_gurobipy,
|
||||
build_maxcut_model_pyomo,
|
||||
for model in [
|
||||
build_maxcut_model_gurobipy(data),
|
||||
build_maxcut_model_pyomo(data),
|
||||
]:
|
||||
model = build_model(data)
|
||||
assert isinstance(model, AbstractModel)
|
||||
with TemporaryDirectory() as tempdir:
|
||||
with H5File(f"{tempdir}/data.h5", "w") as h5:
|
||||
model.extract_after_load(h5)
|
||||
|
||||
@@ -21,6 +21,7 @@ def test_generator() -> None:
|
||||
min_power=uniform(loc=0.25, scale=0.5),
|
||||
cost_startup=uniform(loc=1, scale=1),
|
||||
cost_prod=uniform(loc=1, scale=1),
|
||||
cost_prod_quad=uniform(loc=1, scale=1),
|
||||
cost_fixed=uniform(loc=1, scale=1),
|
||||
min_uptime=randint(low=1, high=8),
|
||||
min_downtime=randint(low=1, high=8),
|
||||
@@ -28,25 +29,16 @@ def test_generator() -> None:
|
||||
demand_jitter=uniform(loc=0.9, scale=0.2),
|
||||
fix_units=True,
|
||||
)
|
||||
data = gen.generate(2)
|
||||
|
||||
assert data[0].demand.tolist() == [430.3, 518.65, 448.16, 860.61]
|
||||
data = gen.generate(1)
|
||||
assert data[0].demand.tolist() == [430.3, 511.29, 484.91, 860.61]
|
||||
assert data[0].min_power.tolist() == [120.05, 156.73, 124.44]
|
||||
assert data[0].max_power.tolist() == [218.54, 477.82, 379.4]
|
||||
assert data[0].min_uptime.tolist() == [3, 3, 5]
|
||||
assert data[0].min_downtime.tolist() == [4, 3, 6]
|
||||
assert data[0].cost_startup.tolist() == [1.06, 1.72, 1.94]
|
||||
assert data[0].cost_prod.tolist() == [1.0, 1.99, 1.62]
|
||||
assert data[0].cost_fixed.tolist() == [1.61, 1.01, 1.02]
|
||||
|
||||
assert data[1].demand.tolist() == [407.3, 476.18, 458.77, 840.38]
|
||||
assert data[1].min_power.tolist() == [120.05, 156.73, 124.44]
|
||||
assert data[1].max_power.tolist() == [218.54, 477.82, 379.4]
|
||||
assert data[1].min_uptime.tolist() == [3, 3, 5]
|
||||
assert data[1].min_downtime.tolist() == [4, 3, 6]
|
||||
assert data[1].cost_startup.tolist() == [1.32, 1.69, 2.29]
|
||||
assert data[1].cost_prod.tolist() == [1.09, 1.94, 1.23]
|
||||
assert data[1].cost_fixed.tolist() == [1.97, 1.04, 0.96]
|
||||
assert data[0].cost_prod_quad.tolist() == [1.6117, 1.0071, 1.0231]
|
||||
assert data[0].cost_fixed.tolist() == [1.52, 1.4, 1.05]
|
||||
|
||||
|
||||
def test_uc() -> None:
|
||||
@@ -59,6 +51,7 @@ def test_uc() -> None:
|
||||
cost_startup=np.array([100, 120, 200]),
|
||||
cost_prod=np.array([1.0, 1.25, 1.5]),
|
||||
cost_fixed=np.array([10, 12, 9]),
|
||||
cost_prod_quad=np.array([0, 0, 0]),
|
||||
)
|
||||
model = build_uc_model_gurobipy(data)
|
||||
model.optimize()
|
||||
|
||||
Reference in New Issue
Block a user