summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2023-10-17 15:03:23 +0200
committerNao Pross <np@0hm.ch>2023-10-17 15:03:23 +0200
commitda2e878bf982ea2a0fe7727b68dfa211409973bb (patch)
tree4031f5794976911c96ba36742c0c32156a73c0b0
parentSketch TestFiniteMapRepresentation (diff)
downloadact4e-da2e878bf982ea2a0fe7727b68dfa211409973bb.tar.gz
act4e-da2e878bf982ea2a0fe7727b68dfa211409973bb.zip
Something weird is going on hereimpl_fail
-rw-r--r--Makefile3
-rw-r--r--src/act4e_solutions/maps_representation.py39
-rw-r--r--src/act4e_solutions/sets_representation.py11
3 files changed, 42 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index 084e1f5..eb622ff 100644
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,8 @@ docker-check-%: build
act4e-test --collections act4e_checks --module act4e_solutions --group $*
check-latest: Homeworks.md build
- docker run -it --rm $(tag) act4e-test --collections act4e_checks --module \
+ docker run -it -v $(PWD)/out-results:/ACT4E/out-results --rm $(tag) \
+ act4e-test --collections act4e_checks --module \
act4e_solutions --group $(shell grep -e "- \[ \] " $< | tr -d "\- [ ]" | head -n1)
# check:
diff --git a/src/act4e_solutions/maps_representation.py b/src/act4e_solutions/maps_representation.py
index 92e7924..39c885c 100644
--- a/src/act4e_solutions/maps_representation.py
+++ b/src/act4e_solutions/maps_representation.py
@@ -1,5 +1,5 @@
from pprint import pprint
-from typing import Any, TypeVar, Dict, List
+from typing import Any, TypeVar, Dict, List, Tuple
import act4e_interfaces as I
from .sets_representation import SolFiniteSetRepresentation
@@ -11,9 +11,9 @@ B = TypeVar("B")
class MyFiniteMap(I.FiniteMap[A, B]):
_source: I.FiniteSet[A]
_target: I.FiniteSet[B]
- _values: List[List]
+ _values: List[List] # of pairs (A, B)
- def __init__(self, source: I.FiniteSet[A], target: I.FiniteSet[B], values: Dict[A, B]):
+ def __init__(self, source: I.FiniteSet[A], target: I.FiniteSet[B], values: List[Tuple[A, B]]):
self._source = source
self._target = target
self._values = values
@@ -33,12 +33,17 @@ class MyFiniteMap(I.FiniteMap[A, B]):
assert a in self._source.elements()
rv = None
for v in self._values:
- if v[0] == a:
- print(f"{v[0]} equals {a}")
+ if self._source.equal(v[0], a):
+ # print(f"{v[0]} equals {a}")
rv = v[1]
+ assert self._target.contains(rv)
+
print("ret: ", rv)
+ print("the type of rv is", type(rv))
print()
+
+ # assert type(rv) != bool
return rv
@@ -46,17 +51,22 @@ class SolFiniteMapRepresentation(I.FiniteMapRepresentation):
def load(self, h: I.IOHelper, s: I.FiniteMap_desc) -> I.FiniteMap[A, B]:
fsr = SolFiniteSetRepresentation()
+ if not all([k in s.keys() for k in ["source", "target", "values"]]):
+ raise I.InvalidFormat()
+
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():
+ a, b = A.load(h, v[0]), B.load(h, v[1])
+
+ if not A.contains(a):
print(f"{v[0]} is not in A: {A.elements()}")
raise I.InvalidFormat()
- if not v[1] in B.elements():
+ if not B.contains(b):
print(f"{v[1]} is not in B: {B.elements()}")
raise I.InvalidFormat()
@@ -71,4 +81,17 @@ class SolFiniteMapRepresentation(I.FiniteMapRepresentation):
def save(self, h: I.IOHelper, m: I.FiniteMap[Any, Any]) -> I.FiniteMap_desc:
- raise NotImplementedError()
+ assert False
+ fsr = SolFiniteSetRepresentation()
+ d = {
+ "source": fsr.save(m.source()),
+ "target": fsr.save(m.target()),
+ "values": []
+ }
+
+ for a in m.source().elements():
+ b = m(a)
+ d["values"].append([m.source().save(h, a), m.target().save(h, b)])
+
+
+ return d
diff --git a/src/act4e_solutions/sets_representation.py b/src/act4e_solutions/sets_representation.py
index 9e231c1..627b408 100644
--- a/src/act4e_solutions/sets_representation.py
+++ b/src/act4e_solutions/sets_representation.py
@@ -61,5 +61,12 @@ class SolFiniteSetRepresentation(I.FiniteSetRepresentation):
raise I.InvalidFormat()
def save(self, h: I.IOHelper, f: I.FiniteSet[Any]) -> I.FiniteSet_desc:
- all_elements = [f.save(h, e) for e in f.elements()]
- return {"elements": all_elements}
+ if isinstance(f, MyFiniteSetProduct):
+ return {"product": [{"elements": c.elements()} for c in f.components()]}
+
+ elif isinstance(f, MyFiniteSet):
+ all_elements = [f.save(h, e) for e in f.elements()]
+ return {"elements": all_elements}
+
+ else:
+ raise I.InvalidFormat()