summaryrefslogtreecommitdiffstats
path: root/src/act4e_solutions/maps_representation.py
blob: 39c885c5d9ba2aa904e61905228e321008d88acf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from pprint import pprint
from typing import Any, TypeVar, Dict, List, Tuple

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] # of pairs (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

    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 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


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:
            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 B.contains(b):
                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:
        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