diff --git a/src/random_events/__init__.py b/src/random_events/__init__.py index 7b344ec..7bb021e 100644 --- a/src/random_events/__init__.py +++ b/src/random_events/__init__.py @@ -1 +1 @@ -__version__ = '1.1.2' +__version__ = '1.1.3' diff --git a/src/random_events/variables.py b/src/random_events/variables.py index 15f2cd6..25f0a8e 100644 --- a/src/random_events/variables.py +++ b/src/random_events/variables.py @@ -38,6 +38,42 @@ def __gt__(self, other: "Variable") -> bool: def __hash__(self) -> int: return self.name.__hash__() + def encode(self, value: Any) -> Any: + """ + Encode an element of the domain to a representation that is usable for computations. + + :param value: The element to encode + :return: The encoded element + """ + return value + + def decode(self, value: Any) -> Any: + """ + Decode an element to the domain from a representation that is usable for computations. + + :param value: The element to decode + :return: The decoded element + """ + return value + + def encode_many(self, elements: Iterable) -> Iterable[Any]: + """ + Encode many elements of the domain to representations that are usable for computations. + + :param elements: The elements to encode + :return: The encoded elements + """ + return tuple(map(self.encode, elements)) + + def decode_many(self, indices: Iterable[int]) -> Iterable[Any]: + """ + Decode many elements from the representations that are usable for computations to their domains. + + :param indices: The encoded elements + :return: The decoded elements + """ + return tuple(map(self.decode, indices)) + class Continuous(Variable): """ @@ -88,15 +124,6 @@ def encode(self, element: Any) -> int: """ return self.domain.index(element) - def encode_many(self, elements: Iterable) -> Iterable[int]: - """ - Encode many elements of the domain to their indices. - - :param elements: The elements to encode - :return: The indices of the elements - """ - return tuple(map(self.encode, elements)) - def decode(self, index: int) -> Any: """ Decode an index to its element of the domain. @@ -106,15 +133,6 @@ def decode(self, index: int) -> Any: """ return self.domain[index] - def decode_many(self, indices: Iterable[int]) -> Iterable[Any]: - """ - Decode many indices to their elements of the domain. - - :param indices: The indices to decode - :return: The elements themselves - """ - return tuple(map(self.decode, indices)) - class Symbolic(Discrete): """ diff --git a/test/test_variables.py b/test/test_variables.py index 570355a..994c0fb 100644 --- a/test/test_variables.py +++ b/test/test_variables.py @@ -85,6 +85,7 @@ def test_encode(self): """ self.assertEqual(self.integer.encode(1), 1) self.assertEqual(self.symbol.encode("b"), 1) + self.assertEqual(self.real.encode(1.0), 1.0) def test_decode(self): """ @@ -92,6 +93,7 @@ def test_decode(self): """ self.assertEqual(self.integer.decode(1), 1) self.assertEqual(self.symbol.decode(1), "b") + self.assertEqual(self.real.decode(1.0), 1.0) if __name__ == '__main__':