Commit aa2573772f1fc1fef653f1c808cd822d113fa56d
1 parent
7ae9c686
Exists in
master
Post meeting 2022-12-05
Showing
6 changed files
with
152 additions
and
2 deletions
Show diff stats
No preview for this file type
code/python/api_01.py
| ... | ... | @@ -0,0 +1,145 @@ |
| 1 | +import math | |
| 2 | +from functools import cache | |
| 3 | +from itertools import accumulate | |
| 4 | +import operator | |
| 5 | + | |
| 6 | + | |
| 7 | +def uniform_op(x): | |
| 8 | + n = len(list(x)) | |
| 9 | + return 0.0 if n == 0 else 1.0/n | |
| 10 | + | |
| 11 | + | |
| 12 | +def max_op(x): | |
| 13 | + return max(x) | |
| 14 | + | |
| 15 | + | |
| 16 | +def min_op(x): | |
| 17 | + return min(x) | |
| 18 | + | |
| 19 | + | |
| 20 | +def sum_op(x): | |
| 21 | + return sum(x) | |
| 22 | + | |
| 23 | + | |
| 24 | +def prod_op(x): | |
| 25 | + log_x = map(math.log, x) | |
| 26 | + return math.exp(sum(log_x)) | |
| 27 | + | |
| 28 | + | |
| 29 | +def co(x): | |
| 30 | + if isinstance(x, float) or isinstance(x, int): | |
| 31 | + return 1 - x | |
| 32 | + elif isinstance(x, str): | |
| 33 | + return x.swapcase() | |
| 34 | + else: | |
| 35 | + return x | |
| 36 | + | |
| 37 | + | |
| 38 | +def parse(d): | |
| 39 | + """Structures a string as an Event and a dict as a base of stable models.""" | |
| 40 | + if isinstance(d, str): | |
| 41 | + return frozenset(d) | |
| 42 | + elif isinstance(d, dict): | |
| 43 | + result = dict() | |
| 44 | + for k, v in d.items(): | |
| 45 | + key = parse(k) | |
| 46 | + result[key] = v | |
| 47 | + return result | |
| 48 | + else: | |
| 49 | + return d | |
| 50 | + | |
| 51 | + | |
| 52 | +def is_consistent(event): | |
| 53 | + return all(x.swapcase() not in event for x in event) | |
| 54 | + | |
| 55 | + | |
| 56 | +class EventsLattice: | |
| 57 | + | |
| 58 | + @staticmethod | |
| 59 | + def close_literals(base_literals): | |
| 60 | + base_lits = list(accumulate(base_literals, func=operator.or_))[-1] | |
| 61 | + lits = set([]) | |
| 62 | + for x in base_lits: | |
| 63 | + lits.add(x) | |
| 64 | + lits.add(x.swapcase()) | |
| 65 | + return lits | |
| 66 | + | |
| 67 | + def __init__(self, smodels): | |
| 68 | + """Create base for Events Lattice.""" | |
| 69 | + self._smodels = smodels | |
| 70 | + self._literals = EventsLattice.close_literals(self._smodels.keys()) | |
| 71 | + | |
| 72 | + def literals(self): | |
| 73 | + return self._literals | |
| 74 | + | |
| 75 | + def stable_models(self): | |
| 76 | + return list(map(set, self._smodels.keys())) | |
| 77 | + | |
| 78 | + @cache | |
| 79 | + def lower_bound(self, event): | |
| 80 | + return set(filter(lambda sm: sm <= event, self._smodels)) | |
| 81 | + | |
| 82 | + @cache | |
| 83 | + def upper_bound(self, event): | |
| 84 | + return set(filter(lambda sm: event <= sm, self._smodels)) | |
| 85 | + | |
| 86 | + def related(self, u, v): | |
| 87 | + u_consistent = is_consistent(u) | |
| 88 | + v_consistent = is_consistent(v) | |
| 89 | + if u_consistent and (u_consistent == v_consistent): | |
| 90 | + return \ | |
| 91 | + self.lower_bound(u) == self.lower_bound(v) and \ | |
| 92 | + self.upper_bound(u) == self.upper_bound(v) | |
| 93 | + else: | |
| 94 | + return u_consistent == v_consistent | |
| 95 | + | |
| 96 | + def factors(self, event): | |
| 97 | + pass | |
| 98 | + | |
| 99 | + def propagated_value(self, event, lower_op=sum_op, upper_op=prod_op): | |
| 100 | + value = 0.0 | |
| 101 | + | |
| 102 | + lb = self.lower_bound(event) | |
| 103 | + len_lb = len(lb) | |
| 104 | + if len_lb > 1: | |
| 105 | + value = lower_op(map(lambda sm: self._smodels[sm], lb)) | |
| 106 | + elif len_lb == 1: | |
| 107 | + value = self._smodels[event] | |
| 108 | + else: | |
| 109 | + ub = self.upper_bound(event) | |
| 110 | + len_ub = len(ub) | |
| 111 | + if len_ub > 1: | |
| 112 | + value = upper_op(map(lambda sm: self._smodels[sm], ub)) | |
| 113 | + elif len_ub == 1: | |
| 114 | + value = self._smodels[event] | |
| 115 | + | |
| 116 | + return value | |
| 117 | + | |
| 118 | + | |
| 119 | +def zoom_event(event_str, lattice, lower_op=sum_op, upper_op=prod_op): | |
| 120 | + event = parse(event_str) | |
| 121 | + lower_bound = lattice.lower_bound(event) | |
| 122 | + upper_bound = lattice.upper_bound(event) | |
| 123 | + propagated = lattice.propagated_value( | |
| 124 | + event, lower_op=lower_op, upper_op=upper_op) | |
| 125 | + print( | |
| 126 | + f"Event: {event}\n\tLB: {lower_bound}\n\tUB: {upper_bound}\n\tProp: {propagated}") | |
| 127 | + | |
| 128 | + | |
| 129 | +if __name__ == "__main__": | |
| 130 | + | |
| 131 | + smodels = parse({ | |
| 132 | + "A": 0.7, | |
| 133 | + "ab": 2 * 3, | |
| 134 | + "ac": 5 * 7 | |
| 135 | + }) | |
| 136 | + | |
| 137 | + lattice = EventsLattice(smodels) | |
| 138 | + | |
| 139 | + print( | |
| 140 | + f"Literals: {lattice.literals()}\nStable Models: {lattice.stable_models()}") | |
| 141 | + | |
| 142 | + zoom_event("abc", lattice, upper_op=min_op, lower_op=max_op) | |
| 143 | + | |
| 144 | + print(is_consistent(parse("aBacc"))) | |
| 145 | + print(is_consistent(parse("aBabc"))) | ... | ... |
code/python/explore_01.py
| ... | ... | @@ -6,7 +6,7 @@ ctl_a = Control() |
| 6 | 6 | ctl_a.register_observer(ProgramObserver(prg)) |
| 7 | 7 | print(f"<1>\n{prg}\n</1>") |
| 8 | 8 | |
| 9 | -prog = "/home/fc/sci/projetos/plp/code/asp/tutotial_01.lp" | |
| 9 | +prog = "code/asp/alarm.lp" | |
| 10 | 10 | ctl_a.load(prog) |
| 11 | 11 | ctl_a.ground([('base', [])]) |
| 12 | 12 | print(f"<2>\n{prg}\n</2>") | ... | ... |