Skip to content

Commit

Permalink
Encoding and Decoding are now avaliable for all variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsch420 committed Nov 6, 2023
1 parent a44ff96 commit 9f97eee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/random_events/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.2'
__version__ = '1.1.3'
54 changes: 36 additions & 18 deletions src/random_events/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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):
"""
Expand Down
2 changes: 2 additions & 0 deletions test/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ 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):
"""
Test that the variables can be decoded.
"""
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__':
Expand Down

0 comments on commit 9f97eee

Please sign in to comment.