mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
Replace NamedTemporaryFile with TemporaryDirectory in tests for better compatibility
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
# Copyright (C) 2020-2022, UChicago Argonne, LLC. All rights reserved.
|
# Copyright (C) 2020-2022, UChicago Argonne, LLC. All rights reserved.
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
|
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy.stats import randint, uniform
|
from scipy.stats import randint, uniform
|
||||||
@@ -86,8 +86,8 @@ def test_set_cover() -> None:
|
|||||||
build_setcover_model_gurobipy(data),
|
build_setcover_model_gurobipy(data),
|
||||||
]:
|
]:
|
||||||
assert isinstance(model, AbstractModel)
|
assert isinstance(model, AbstractModel)
|
||||||
with NamedTemporaryFile() as tempfile:
|
with TemporaryDirectory() as tempdir:
|
||||||
with H5File(tempfile.name) as h5:
|
with H5File(f"{tempdir}/data.h5", "w") as h5:
|
||||||
model.optimize()
|
model.optimize()
|
||||||
model.extract_after_mip(h5)
|
model.extract_after_mip(h5)
|
||||||
assert h5.get_scalar("mip_obj_value") == 11.0
|
assert h5.get_scalar("mip_obj_value") == 11.0
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
# Copyright (C) 2020-2022, UChicago Argonne, LLC. All rights reserved.
|
# Copyright (C) 2020-2022, UChicago Argonne, LLC. All rights reserved.
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -25,8 +25,8 @@ def test_stab() -> None:
|
|||||||
build_stab_model_pyomo(data),
|
build_stab_model_pyomo(data),
|
||||||
]:
|
]:
|
||||||
assert isinstance(model, AbstractModel)
|
assert isinstance(model, AbstractModel)
|
||||||
with NamedTemporaryFile() as tempfile:
|
with TemporaryDirectory() as tempdir:
|
||||||
with H5File(tempfile.name) as h5:
|
with H5File(f"{tempdir}/data.h5", "w") as h5:
|
||||||
model.optimize()
|
model.optimize()
|
||||||
model.extract_after_mip(h5)
|
model.extract_after_mip(h5)
|
||||||
assert h5.get_scalar("mip_obj_value") == -2.0
|
assert h5.get_scalar("mip_obj_value") == -2.0
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
# Copyright (C) 2020-2022, UChicago Argonne, LLC. All rights reserved.
|
# Copyright (C) 2020-2022, UChicago Argonne, LLC. All rights reserved.
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import TemporaryDirectory
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -11,31 +11,31 @@ from miplearn.h5 import H5File
|
|||||||
|
|
||||||
|
|
||||||
def test_h5() -> None:
|
def test_h5() -> None:
|
||||||
file = NamedTemporaryFile()
|
with TemporaryDirectory() as tempdir:
|
||||||
h5 = H5File(file.name)
|
with H5File(f"{tempdir}/data.h5", "w") as h5:
|
||||||
_assert_roundtrip_scalar(h5, "A")
|
_assert_roundtrip_scalar(h5, "A")
|
||||||
_assert_roundtrip_scalar(h5, True)
|
_assert_roundtrip_scalar(h5, True)
|
||||||
_assert_roundtrip_scalar(h5, 1)
|
_assert_roundtrip_scalar(h5, 1)
|
||||||
_assert_roundtrip_scalar(h5, 1.0)
|
_assert_roundtrip_scalar(h5, 1.0)
|
||||||
assert h5.get_scalar("unknown-key") is None
|
assert h5.get_scalar("unknown-key") is None
|
||||||
|
|
||||||
_assert_roundtrip_array(h5, np.array([True, False]))
|
_assert_roundtrip_array(h5, np.array([True, False]))
|
||||||
_assert_roundtrip_array(h5, np.array([1, 2, 3]))
|
_assert_roundtrip_array(h5, np.array([1, 2, 3]))
|
||||||
_assert_roundtrip_array(h5, np.array([1.0, 2.0, 3.0]))
|
_assert_roundtrip_array(h5, np.array([1.0, 2.0, 3.0]))
|
||||||
_assert_roundtrip_array(h5, np.array(["A", "BB", "CCC"], dtype="S"))
|
_assert_roundtrip_array(h5, np.array(["A", "BB", "CCC"], dtype="S"))
|
||||||
assert h5.get_array("unknown-key") is None
|
assert h5.get_array("unknown-key") is None
|
||||||
|
|
||||||
_assert_roundtrip_sparse(
|
_assert_roundtrip_sparse(
|
||||||
h5,
|
h5,
|
||||||
coo_matrix(
|
coo_matrix(
|
||||||
[
|
[
|
||||||
[1.0, 0.0, 0.0],
|
[1.0, 0.0, 0.0],
|
||||||
[0.0, 2.0, 3.0],
|
[0.0, 2.0, 3.0],
|
||||||
[0.0, 0.0, 4.0],
|
[0.0, 0.0, 4.0],
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
assert h5.get_sparse("unknown-key") is None
|
assert h5.get_sparse("unknown-key") is None
|
||||||
|
|
||||||
|
|
||||||
def _assert_roundtrip_array(h5: H5File, original: np.ndarray) -> None:
|
def _assert_roundtrip_array(h5: H5File, original: np.ndarray) -> None:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
# Copyright (C) 2020-2022, UChicago Argonne, LLC. All rights reserved.
|
# Copyright (C) 2020-2022, UChicago Argonne, LLC. All rights reserved.
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
|
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import TemporaryDirectory
|
||||||
from typing import Callable, Any
|
from typing import Callable, Any
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -49,8 +49,8 @@ def _test_solver(build_model: Callable, data: Any) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def _test_extract(model: AbstractModel) -> None:
|
def _test_extract(model: AbstractModel) -> None:
|
||||||
with NamedTemporaryFile() as tempfile:
|
with TemporaryDirectory() as tempdir:
|
||||||
with H5File(tempfile.name) as h5:
|
with H5File(f"{tempdir}/data.h5", "w") as h5:
|
||||||
|
|
||||||
def test_scalar(key: str, expected_value: Any) -> None:
|
def test_scalar(key: str, expected_value: Any) -> None:
|
||||||
actual_value = h5.get_scalar(key)
|
actual_value = h5.get_scalar(key)
|
||||||
@@ -129,7 +129,6 @@ def _test_extract(model: AbstractModel) -> None:
|
|||||||
test_scalar("mip_obj_value", 11.0)
|
test_scalar("mip_obj_value", 11.0)
|
||||||
mip_wallclock_time = h5.get_scalar("mip_wallclock_time")
|
mip_wallclock_time = h5.get_scalar("mip_wallclock_time")
|
||||||
assert mip_wallclock_time is not None
|
assert mip_wallclock_time is not None
|
||||||
assert mip_wallclock_time > 0
|
|
||||||
if model._supports_node_count:
|
if model._supports_node_count:
|
||||||
count = h5.get_scalar("mip_node_count")
|
count = h5.get_scalar("mip_node_count")
|
||||||
assert count is not None
|
assert count is not None
|
||||||
@@ -145,8 +144,8 @@ def _test_extract(model: AbstractModel) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def _test_add_constr(model: AbstractModel) -> None:
|
def _test_add_constr(model: AbstractModel) -> None:
|
||||||
with NamedTemporaryFile() as tempfile:
|
with TemporaryDirectory() as tempdir:
|
||||||
with H5File(tempfile.name) as h5:
|
with H5File(f"{tempdir}/data.h5", "w") as h5:
|
||||||
model.add_constrs(
|
model.add_constrs(
|
||||||
np.array([b"x[2]", b"x[3]"], dtype="S"),
|
np.array([b"x[2]", b"x[3]"], dtype="S"),
|
||||||
np.array([[0, 1], [1, 0]]),
|
np.array([[0, 1], [1, 0]]),
|
||||||
@@ -161,8 +160,8 @@ def _test_add_constr(model: AbstractModel) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def _test_fix_vars(model: AbstractModel) -> None:
|
def _test_fix_vars(model: AbstractModel) -> None:
|
||||||
with NamedTemporaryFile() as tempfile:
|
with TemporaryDirectory() as tempdir:
|
||||||
with H5File(tempfile.name) as h5:
|
with H5File(f"{tempdir}/data.h5", "w") as h5:
|
||||||
model.fix_variables(
|
model.fix_variables(
|
||||||
var_names=np.array([b"x[2]", b"x[3]"], dtype="S"),
|
var_names=np.array([b"x[2]", b"x[3]"], dtype="S"),
|
||||||
var_values=np.array([0, 0]),
|
var_values=np.array([0, 0]),
|
||||||
@@ -175,8 +174,8 @@ def _test_fix_vars(model: AbstractModel) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def _test_infeasible(model: AbstractModel) -> None:
|
def _test_infeasible(model: AbstractModel) -> None:
|
||||||
with NamedTemporaryFile() as tempfile:
|
with TemporaryDirectory() as tempdir:
|
||||||
with H5File(tempfile.name) as h5:
|
with H5File(f"{tempdir}/data.h5", "w") as h5:
|
||||||
model.fix_variables(
|
model.fix_variables(
|
||||||
var_names=np.array([b"x[0]", b"x[3]"], dtype="S"),
|
var_names=np.array([b"x[0]", b"x[3]"], dtype="S"),
|
||||||
var_values=np.array([0, 0]),
|
var_values=np.array([0, 0]),
|
||||||
|
|||||||
Reference in New Issue
Block a user