parent
021a71f60c
commit
0a399deeee
@ -1,31 +1,44 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
# Copyright (C) 2020-2021, 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 typing import Any
|
||||||
|
|
||||||
from miplearn.features.sample import MemorySample, Sample
|
from miplearn.features.sample import MemorySample, Sample, Hdf5Sample
|
||||||
|
|
||||||
|
|
||||||
def _test_sample(sample: Sample) -> None:
|
def _test_sample(sample: Sample) -> None:
|
||||||
# Strings
|
_assert_roundtrip(sample, "A")
|
||||||
sample.put("str", "hello")
|
_assert_roundtrip(sample, True)
|
||||||
assert sample.get("str") == "hello"
|
_assert_roundtrip(sample, 1)
|
||||||
|
_assert_roundtrip(sample, 1.0)
|
||||||
# Numbers
|
_assert_roundtrip(sample, ["A", "BB", "CCC", "こんにちは"])
|
||||||
sample.put("int", 1)
|
_assert_roundtrip(sample, [True, True, False])
|
||||||
sample.put("float", 5.0)
|
_assert_roundtrip(sample, [1, 2, 3])
|
||||||
assert sample.get("int") == 1
|
_assert_roundtrip(sample, [1.0, 2.0, 3.0])
|
||||||
assert sample.get("float") == 5.0
|
|
||||||
|
|
||||||
# List of strings
|
def _assert_roundtrip(sample: Sample, expected: Any) -> None:
|
||||||
sample.put("strlist", ["hello", "world"])
|
sample.put("key", expected)
|
||||||
assert sample.get("strlist") == ["hello", "world"]
|
actual = sample.get("key")
|
||||||
|
assert actual == expected
|
||||||
# List of numbers
|
assert actual is not None
|
||||||
sample.put("intlist", [1, 2, 3])
|
if isinstance(actual, list):
|
||||||
sample.put("floatlist", [4.0, 5.0, 6.0])
|
assert isinstance(actual[0], expected[0].__class__), (
|
||||||
assert sample.get("intlist") == [1, 2, 3]
|
f"Expected class {expected[0].__class__}, "
|
||||||
assert sample.get("floatlist") == [4.0, 5.0, 6.0]
|
f"found {actual[0].__class__} instead"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
assert isinstance(actual, expected.__class__), (
|
||||||
|
f"Expected class {expected.__class__}, "
|
||||||
|
f"found class {actual.__class__} instead"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_memory_sample() -> None:
|
def test_memory_sample() -> None:
|
||||||
_test_sample(MemorySample())
|
_test_sample(MemorySample())
|
||||||
|
|
||||||
|
|
||||||
|
def test_hdf5_sample() -> None:
|
||||||
|
file = NamedTemporaryFile()
|
||||||
|
_test_sample(Hdf5Sample(file.name))
|
||||||
|
Loading…
Reference in new issue