From b9609b6a420662b2a09b1fcc06a156282bde030a Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 10 Oct 2023 12:11:31 +0200 Subject: Sketch TestFiniteMapRepresentation --- src/act4e_solutions/maps_representation.py | 64 +++++++++++++++++++++++++++++- src/act4e_solutions/sets_product.py | 6 +-- src/act4e_solutions/sets_representation.py | 7 ++-- 3 files changed, 69 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/act4e_solutions/maps_representation.py b/src/act4e_solutions/maps_representation.py index f532658..92e7924 100644 --- a/src/act4e_solutions/maps_representation.py +++ b/src/act4e_solutions/maps_representation.py @@ -1,14 +1,74 @@ -from typing import Any, TypeVar +from pprint import pprint +from typing import Any, TypeVar, Dict, List import act4e_interfaces as I +from .sets_representation import SolFiniteSetRepresentation +from .sets_product import MyFiniteSetProduct A = TypeVar("A") B = TypeVar("B") +class MyFiniteMap(I.FiniteMap[A, B]): + _source: I.FiniteSet[A] + _target: I.FiniteSet[B] + _values: List[List] + + def __init__(self, source: I.FiniteSet[A], target: I.FiniteSet[B], values: Dict[A, B]): + self._source = source + self._target = target + self._values = values + + def source(self) -> I.FiniteSet[A]: + return self._source + + def target(self) -> I.FiniteSet[B]: + return self._target + + def __call__(self, a: A) -> B: + print("Function call") + print("src: ", self._source.elements()) + print("dst: ", self._target.elements()) + print("val: ", self._values) + print("inp: ", a) + assert a in self._source.elements() + rv = None + for v in self._values: + if v[0] == a: + print(f"{v[0]} equals {a}") + rv = v[1] + + print("ret: ", rv) + print() + return rv + class SolFiniteMapRepresentation(I.FiniteMapRepresentation): def load(self, h: I.IOHelper, s: I.FiniteMap_desc) -> I.FiniteMap[A, B]: - raise NotImplementedError() + fsr = SolFiniteSetRepresentation() + + A = fsr.load(h, s["source"]) + B = fsr.load(h, s["target"]) + values = s["values"] + + # Check that the values are are in the domain and codomain + for v in values: + if not v[0] in A.elements(): + print(f"{v[0]} is not in A: {A.elements()}") + raise I.InvalidFormat() + + if not v[1] in B.elements(): + print(f"{v[1]} is not in B: {B.elements()}") + raise I.InvalidFormat() + + # Check that the domain values are unique + domvals = [] + for d in map(lambda v: v[0], values): + if d in domvals: + raise I.InvalidFormat() + domvals.append(d) + + return MyFiniteMap(A, B, values) + def save(self, h: I.IOHelper, m: I.FiniteMap[Any, Any]) -> I.FiniteMap_desc: raise NotImplementedError() diff --git a/src/act4e_solutions/sets_product.py b/src/act4e_solutions/sets_product.py index 19a8062..42b8640 100644 --- a/src/act4e_solutions/sets_product.py +++ b/src/act4e_solutions/sets_product.py @@ -35,10 +35,10 @@ class MyFiniteSetProduct(I.FiniteSetProduct[C, E]): def all_combinations(last, new): return [l + [n] for l in last for n in new] - parts = map(lambda c: list(c.elements()), self._components) - elements = reduce(all_combinations, parts, [[]]) + parts = list(map(lambda c: list(c.elements()), self._components)) + elements = list(reduce(all_combinations, parts, [[]])) - return list(elements) + return elements def save(self, h: I.IOHelper, x: E) -> I.ConcreteRepr: return cast(I.ConcreteRepr, x) diff --git a/src/act4e_solutions/sets_representation.py b/src/act4e_solutions/sets_representation.py index 32931da..9e231c1 100644 --- a/src/act4e_solutions/sets_representation.py +++ b/src/act4e_solutions/sets_representation.py @@ -8,7 +8,7 @@ E = TypeVar("E") class MyFiniteSet(I.FiniteSet[E]): _elements: List[E] def __init__(self, elements: Collection[E]): - self._elements = list(elements) + self._elements = elements def size(self) -> int: return len(self._elements) @@ -17,7 +17,7 @@ class MyFiniteSet(I.FiniteSet[E]): return any(map(lambda y: y == x, self._elements)) def elements(self) -> Iterator[E]: - return iter(self._elements) + return self._elements def save(self, h: I.IOHelper, x: E) -> List[E]: return cast(I.ConcreteRepr, x) @@ -54,7 +54,8 @@ class SolFiniteSetRepresentation(I.FiniteSetRepresentation): raise I.InvalidFormat() components.append(MyFiniteSet(comp["elements"])) - return MyFiniteSetProduct(components) + + return MyFiniteSetProduct(components) else: raise I.InvalidFormat() -- cgit v1.2.1