mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
Remove {get,put}_set and deprecated functions
This commit is contained in:
@@ -56,6 +56,11 @@ class DynamicConstraintsComponent(Component):
|
||||
cids: Dict[ConstraintCategory, List[ConstraintName]] = {}
|
||||
known_cids = np.array(self.known_cids, dtype="S")
|
||||
|
||||
enforced_cids = None
|
||||
enforced_cids_np = sample.get_array(self.attr)
|
||||
if enforced_cids_np is not None:
|
||||
enforced_cids = list(enforced_cids_np)
|
||||
|
||||
# Get user-provided constraint features
|
||||
(
|
||||
constr_features,
|
||||
@@ -72,13 +77,11 @@ class DynamicConstraintsComponent(Component):
|
||||
constr_features,
|
||||
]
|
||||
)
|
||||
assert len(known_cids) == constr_features.shape[0]
|
||||
|
||||
categories = np.unique(constr_categories)
|
||||
for c in categories:
|
||||
x[c] = constr_features[constr_categories == c].tolist()
|
||||
cids[c] = known_cids[constr_categories == c].tolist()
|
||||
enforced_cids = np.array(list(sample.get_set(self.attr)), dtype="S")
|
||||
if enforced_cids is not None:
|
||||
tmp = np.isin(cids[c], enforced_cids).reshape(-1, 1)
|
||||
y[c] = np.hstack([~tmp, tmp]).tolist() # type: ignore
|
||||
@@ -99,7 +102,7 @@ class DynamicConstraintsComponent(Component):
|
||||
assert pre is not None
|
||||
known_cids: Set = set()
|
||||
for cids in pre:
|
||||
known_cids |= cids
|
||||
known_cids |= set(list(cids))
|
||||
self.known_cids.clear()
|
||||
self.known_cids.extend(sorted(known_cids))
|
||||
|
||||
@@ -128,7 +131,7 @@ class DynamicConstraintsComponent(Component):
|
||||
|
||||
@overrides
|
||||
def pre_sample_xy(self, instance: Instance, sample: Sample) -> Any:
|
||||
return sample.get_set(self.attr)
|
||||
return sample.get_array(self.attr)
|
||||
|
||||
@overrides
|
||||
def fit_xy(
|
||||
@@ -150,7 +153,7 @@ class DynamicConstraintsComponent(Component):
|
||||
instance: Instance,
|
||||
sample: Sample,
|
||||
) -> Dict[str, float]:
|
||||
actual = sample.get_set(self.attr)
|
||||
actual = sample.get_array(self.attr)
|
||||
assert actual is not None
|
||||
pred = set(self.sample_predict(instance, sample))
|
||||
tp, tn, fp, fn = 0, 0, 0, 0
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
import logging
|
||||
import pdb
|
||||
from typing import Dict, List, TYPE_CHECKING, Tuple, Any, Optional, Set
|
||||
|
||||
import numpy as np
|
||||
@@ -78,7 +79,10 @@ class DynamicLazyConstraintsComponent(Component):
|
||||
stats: LearningSolveStats,
|
||||
sample: Sample,
|
||||
) -> None:
|
||||
sample.put_set("mip_constr_lazy_enforced", set(self.lazy_enforced))
|
||||
sample.put_array(
|
||||
"mip_constr_lazy_enforced",
|
||||
np.array(list(self.lazy_enforced), dtype="S"),
|
||||
)
|
||||
|
||||
@overrides
|
||||
def iteration_cb(
|
||||
|
||||
@@ -87,7 +87,10 @@ class UserCutsComponent(Component):
|
||||
stats: LearningSolveStats,
|
||||
sample: Sample,
|
||||
) -> None:
|
||||
sample.put_set("mip_user_cuts_enforced", set(self.enforced))
|
||||
sample.put_array(
|
||||
"mip_user_cuts_enforced",
|
||||
np.array(list(self.enforced), dtype="S"),
|
||||
)
|
||||
stats["UserCuts: Added in callback"] = self.n_added_in_callback
|
||||
if self.n_added_in_callback > 0:
|
||||
logger.info(f"{self.n_added_in_callback} user cuts added in callback")
|
||||
|
||||
@@ -61,7 +61,10 @@ class StaticLazyConstraintsComponent(Component):
|
||||
stats: LearningSolveStats,
|
||||
sample: Sample,
|
||||
) -> None:
|
||||
sample.put_set("mip_constr_lazy_enforced", self.enforced_cids)
|
||||
sample.put_array(
|
||||
"mip_constr_lazy_enforced",
|
||||
np.array(list(self.enforced_cids), dtype="S"),
|
||||
)
|
||||
stats["LazyStatic: Restored"] = self.n_restored
|
||||
stats["LazyStatic: Iterations"] = self.n_iterations
|
||||
|
||||
@@ -212,7 +215,7 @@ class StaticLazyConstraintsComponent(Component):
|
||||
constr_names = sample.get_array("static_constr_names")
|
||||
constr_categories = sample.get_array("static_constr_categories")
|
||||
constr_lazy = sample.get_array("static_constr_lazy")
|
||||
lazy_enforced = sample.get_set("mip_constr_lazy_enforced")
|
||||
lazy_enforced = sample.get_array("mip_constr_lazy_enforced")
|
||||
if constr_features is None:
|
||||
constr_features = sample.get_array("static_constr_features")
|
||||
|
||||
|
||||
@@ -46,15 +46,6 @@ class Sample(ABC):
|
||||
def put_scalar(self, key: str, value: Scalar) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_vector(self, key: str) -> Optional[Any]:
|
||||
warnings.warn("Deprecated", DeprecationWarning)
|
||||
return None
|
||||
|
||||
@abstractmethod
|
||||
def put_vector(self, key: str, value: Vector) -> None:
|
||||
warnings.warn("Deprecated", DeprecationWarning)
|
||||
|
||||
@abstractmethod
|
||||
def put_array(self, key: str, value: Optional[np.ndarray]) -> None:
|
||||
pass
|
||||
@@ -71,19 +62,6 @@ class Sample(ABC):
|
||||
def get_sparse(self, key: str) -> Optional[coo_matrix]:
|
||||
pass
|
||||
|
||||
def get_set(self, key: str) -> Set:
|
||||
warnings.warn("Deprecated", DeprecationWarning)
|
||||
v = self.get_vector(key)
|
||||
if v:
|
||||
return set(v)
|
||||
else:
|
||||
return set()
|
||||
|
||||
def put_set(self, key: str, value: Set) -> None:
|
||||
warnings.warn("Deprecated", DeprecationWarning)
|
||||
v = list(value)
|
||||
self.put_vector(key, v)
|
||||
|
||||
def _assert_is_scalar(self, value: Any) -> None:
|
||||
if value is None:
|
||||
return
|
||||
@@ -91,20 +69,13 @@ class Sample(ABC):
|
||||
return
|
||||
assert False, f"scalar expected; found instead: {value} ({value.__class__})"
|
||||
|
||||
def _assert_is_vector(self, value: Any) -> None:
|
||||
assert isinstance(
|
||||
value, (list, np.ndarray)
|
||||
), f"list or numpy array expected; found instead: {value} ({value.__class__})"
|
||||
for v in value:
|
||||
self._assert_is_scalar(v)
|
||||
|
||||
def _assert_supported(self, value: np.ndarray) -> None:
|
||||
def _assert_is_array(self, value: np.ndarray) -> None:
|
||||
assert isinstance(value, np.ndarray)
|
||||
assert value.dtype.kind in "biufS", f"Unsupported dtype: {value.dtype}"
|
||||
|
||||
def _assert_is_sparse(self, value: Any) -> None:
|
||||
assert isinstance(value, coo_matrix)
|
||||
self._assert_supported(value.data)
|
||||
self._assert_is_array(value.data)
|
||||
|
||||
|
||||
class MemorySample(Sample):
|
||||
@@ -113,35 +84,20 @@ class MemorySample(Sample):
|
||||
def __init__(
|
||||
self,
|
||||
data: Optional[Dict[str, Any]] = None,
|
||||
check_data: bool = True,
|
||||
) -> None:
|
||||
if data is None:
|
||||
data = {}
|
||||
self._data: Dict[str, Any] = data
|
||||
self._check_data = check_data
|
||||
|
||||
@overrides
|
||||
def get_scalar(self, key: str) -> Optional[Any]:
|
||||
return self._get(key)
|
||||
|
||||
@overrides
|
||||
def get_vector(self, key: str) -> Optional[Any]:
|
||||
return self._get(key)
|
||||
|
||||
@overrides
|
||||
def put_scalar(self, key: str, value: Scalar) -> None:
|
||||
if value is None:
|
||||
return
|
||||
if self._check_data:
|
||||
self._assert_is_scalar(value)
|
||||
self._put(key, value)
|
||||
|
||||
@overrides
|
||||
def put_vector(self, key: str, value: Vector) -> None:
|
||||
if value is None:
|
||||
return
|
||||
if self._check_data:
|
||||
self._assert_is_vector(value)
|
||||
self._assert_is_scalar(value)
|
||||
self._put(key, value)
|
||||
|
||||
def _get(self, key: str) -> Optional[Any]:
|
||||
@@ -157,7 +113,7 @@ class MemorySample(Sample):
|
||||
def put_array(self, key: str, value: Optional[np.ndarray]) -> None:
|
||||
if value is None:
|
||||
return
|
||||
self._assert_supported(value)
|
||||
self._assert_is_array(value)
|
||||
self._put(key, value)
|
||||
|
||||
@overrides
|
||||
@@ -188,10 +144,8 @@ class Hdf5Sample(Sample):
|
||||
self,
|
||||
filename: str,
|
||||
mode: str = "r+",
|
||||
check_data: bool = True,
|
||||
) -> None:
|
||||
self.file = h5py.File(filename, mode, libver="latest")
|
||||
self._check_data = check_data
|
||||
|
||||
@overrides
|
||||
def get_scalar(self, key: str) -> Optional[Any]:
|
||||
@@ -206,66 +160,20 @@ class Hdf5Sample(Sample):
|
||||
else:
|
||||
return ds[()].tolist()
|
||||
|
||||
@overrides
|
||||
def get_vector(self, key: str) -> Optional[Any]:
|
||||
if key not in self.file:
|
||||
return None
|
||||
ds = self.file[key]
|
||||
assert (
|
||||
len(ds.shape) == 1
|
||||
), f"1-dimensional array expected; found shape {ds.shape}"
|
||||
if h5py.check_string_dtype(ds.dtype):
|
||||
result = ds.asstr()[:].tolist()
|
||||
result = [r if len(r) > 0 else None for r in result]
|
||||
return result
|
||||
else:
|
||||
return ds[:].tolist()
|
||||
|
||||
@overrides
|
||||
def put_scalar(self, key: str, value: Any) -> None:
|
||||
if value is None:
|
||||
return
|
||||
if self._check_data:
|
||||
self._assert_is_scalar(value)
|
||||
self._put(key, value)
|
||||
|
||||
@overrides
|
||||
def put_vector(self, key: str, value: Vector) -> None:
|
||||
if value is None:
|
||||
return
|
||||
if self._check_data:
|
||||
self._assert_is_vector(value)
|
||||
|
||||
for v in value:
|
||||
# Convert strings to bytes
|
||||
if isinstance(v, str) or v is None:
|
||||
value = np.array(
|
||||
[u if u is not None else b"" for u in value],
|
||||
dtype="S",
|
||||
)
|
||||
break
|
||||
|
||||
# Convert all floating point numbers to half-precision
|
||||
if isinstance(v, float):
|
||||
value = np.array(value, dtype=np.dtype("f2"))
|
||||
break
|
||||
|
||||
self._put(key, value, compress=True)
|
||||
|
||||
def _put(self, key: str, value: Any, compress: bool = False) -> Dataset:
|
||||
self._assert_is_scalar(value)
|
||||
if key in self.file:
|
||||
del self.file[key]
|
||||
if compress:
|
||||
ds = self.file.create_dataset(key, data=value, compression="gzip")
|
||||
else:
|
||||
ds = self.file.create_dataset(key, data=value)
|
||||
return ds
|
||||
self.file.create_dataset(key, data=value)
|
||||
|
||||
@overrides
|
||||
def put_array(self, key: str, value: Optional[np.ndarray]) -> None:
|
||||
if value is None:
|
||||
return
|
||||
self._assert_supported(value)
|
||||
self._assert_is_array(value)
|
||||
if key in self.file:
|
||||
del self.file[key]
|
||||
return self.file.create_dataset(key, data=value, compression="gzip")
|
||||
|
||||
Reference in New Issue
Block a user