You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.0 KiB
40 lines
1.0 KiB
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
|
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
|
# Released under the modified BSD license. See COPYING.md for more details.
|
|
|
|
from abc import ABC, abstractmethod
|
|
from typing import Dict, Optional, Any
|
|
|
|
|
|
class Sample(ABC):
|
|
"""Abstract dictionary-like class that stores training data."""
|
|
|
|
@abstractmethod
|
|
def get(self, key: str) -> Optional[Any]:
|
|
pass
|
|
|
|
@abstractmethod
|
|
def put(self, key: str, value: Any) -> None:
|
|
pass
|
|
|
|
|
|
class MemorySample(Sample):
|
|
"""Dictionary-like class that stores training data in-memory."""
|
|
|
|
def __init__(
|
|
self,
|
|
data: Optional[Dict[str, Any]] = None,
|
|
) -> None:
|
|
if data is None:
|
|
data = {}
|
|
self._data: Dict[str, Any] = data
|
|
|
|
def get(self, key: str) -> Optional[Any]:
|
|
if key in self._data:
|
|
return self._data[key]
|
|
else:
|
|
return None
|
|
|
|
def put(self, key: str, value: Any) -> None:
|
|
self._data[key] = value
|