diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3b7e413 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "smodels" + ] +} \ No newline at end of file diff --git a/code/python/__init__.py b/code/python/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/code/python/__init__.py diff --git a/code/python/__pycache__/event_lattice.cpython-39.pyc b/code/python/__pycache__/event_lattice.cpython-39.pyc new file mode 100644 index 0000000..e287276 Binary files /dev/null and b/code/python/__pycache__/event_lattice.cpython-39.pyc differ diff --git a/code/python/api_01.py b/code/python/api_01.py index cce340c..f119ed2 100644 --- a/code/python/api_01.py +++ b/code/python/api_01.py @@ -23,4 +23,4 @@ ctl.ground( s = ctl.solve(on_model=on_model) -print(dir(s)) \ No newline at end of file +print(s) \ No newline at end of file diff --git a/code/python/event_lattice.py b/code/python/event_lattice.py new file mode 100644 index 0000000..b38f543 --- /dev/null +++ b/code/python/event_lattice.py @@ -0,0 +1,145 @@ +import math +from functools import cache +from itertools import accumulate +import operator + + +def uniform_op(x): + n = len(list(x)) + return 0.0 if n == 0 else 1.0/n + + +def max_op(x): + return max(x) + + +def min_op(x): + return min(x) + + +def sum_op(x): + return sum(x) + + +def prod_op(x): + log_x = map(math.log, x) + return math.exp(sum(log_x)) + + +def co(x): + if isinstance(x, float) or isinstance(x, int): + return 1 - x + elif isinstance(x, str): + return x.swapcase() + else: + return x + + +def parse(d): + """Structures a string as an Event and a dict as a base of stable models.""" + if isinstance(d, str): + return frozenset(d) + elif isinstance(d, dict): + result = dict() + for k, v in d.items(): + key = parse(k) + result[key] = v + return result + else: + return d + + +def is_consistent(event): + return all(x.swapcase() not in event for x in event) + + +class EventsLattice: + + @staticmethod + def close_literals(base_literals): + base_lits = list(accumulate(base_literals, func=operator.or_))[-1] + lits = set([]) + for x in base_lits: + lits.add(x) + lits.add(x.swapcase()) + return lits + + def __init__(self, smodels): + """Create base for Events Lattice.""" + self._smodels = smodels + self._literals = EventsLattice.close_literals(self._smodels.keys()) + + def literals(self): + return self._literals + + def stable_models(self): + return list(map(set, self._smodels.keys())) + + @cache + def lower_bound(self, event): + return set(filter(lambda sm: sm <= event, self._smodels)) + + @cache + def upper_bound(self, event): + return set(filter(lambda sm: event <= sm, self._smodels)) + + def related(self, u, v): + u_consistent = is_consistent(u) + v_consistent = is_consistent(v) + if u_consistent and (u_consistent == v_consistent): + return \ + self.lower_bound(u) == self.lower_bound(v) and \ + self.upper_bound(u) == self.upper_bound(v) + else: + return u_consistent == v_consistent + + def factors(self, event): + pass + + def propagated_value(self, event, lower_op=sum_op, upper_op=prod_op): + value = 0.0 + + lb = self.lower_bound(event) + len_lb = len(lb) + if len_lb > 1: + value = lower_op(map(lambda sm: self._smodels[sm], lb)) + elif len_lb == 1: + value = self._smodels[event] + else: + ub = self.upper_bound(event) + len_ub = len(ub) + if len_ub > 1: + value = upper_op(map(lambda sm: self._smodels[sm], ub)) + elif len_ub == 1: + value = self._smodels[event] + + return value + + +def zoom_event(event_str, lattice, lower_op=sum_op, upper_op=prod_op): + event = parse(event_str) + lower_bound = lattice.lower_bound(event) + upper_bound = lattice.upper_bound(event) + propagated = lattice.propagated_value( + event, lower_op=lower_op, upper_op=upper_op) + print( + f"Event: {event}\n\tLB: {lower_bound}\n\tUB: {upper_bound}\n\tProp: {propagated}") + + +if __name__ == "__main__": + + smodels = parse({ + "A": 0.7, + "ab": 2 * 3, + "ac": 5 * 7 + }) + + lattice = EventsLattice(smodels) + + print( + f"Literals: {lattice.literals()}\nStable Models: {lattice.stable_models()}") + + zoom_event("abc", lattice, upper_op=min_op, lower_op=max_op) + + print(is_consistent(parse("aBacc"))) + print(is_consistent(parse("aBabc"))) diff --git a/code/python/explore_01.py b/code/python/explore_01.py index 6732b15..f894bdd 100644 --- a/code/python/explore_01.py +++ b/code/python/explore_01.py @@ -6,7 +6,7 @@ ctl_a = Control() ctl_a.register_observer(ProgramObserver(prg)) print(f"<1>\n{prg}\n") -prog = "/home/fc/sci/projetos/plp/code/asp/tutotial_01.lp" +prog = "code/asp/alarm.lp" ctl_a.load(prog) ctl_a.ground([('base', [])]) print(f"<2>\n{prg}\n") -- libgit2 0.21.2