-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplications.py
151 lines (127 loc) · 4.7 KB
/
implications.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
"""
Author: Gaurav Sahu, 23:20 15th January, 2018
Implements the implication related methods for a given context.
"""
class Implication(object):
"""
An Implication consists of two sets: *premise* and *conclusion*
Examples
========
>>> imp = Implication(set(('a', 'b',)), set(('c',)))
>>> imp
a, b => c
>>> print imp
a, b => c
>>> imp.is_respected(set(('a', 'b',)))
False
>>> imp.is_respected(set(('a', 'b', 'd')))
False
>>> imp.is_respected(set(('a', 'b', 'c',)))
True
>>> imp.is_respected(set(('a', 'c',)))
True
>>> imp.is_respected(set(('b')))
True
>>> imp.is_respected(set(('c')))
True
"""
def __init__(self, premise_=frozenset(), conclusion_=frozenset()):
"""
Create implication from two sets of attributes
"""
self.premise = premise_
self.conclusion = conclusion_
def __deepcopy__(self, memo):
return Implication(self.premise.copy(), self.conclusion.copy())
def get_premise(self):
"""
Return premise of implication
"""
return self.premise
def get_conclusion(self):
"""
Return conclusion of implication
"""
return self.conclusion
def get_reduced_conclusion(self):
return self.conclusion - self.premise
def __repr__(self):
try:
premise = ", ".join([element for element in self.premise])
short_conclusion = self.conclusion - self.premise
conclusion = ", ".join([element for element in short_conclusion])
except BaseException:
premise = ", ".join([str(element) for element in self.premise])
short_conclusion = self.conclusion - self.premise
conclusion = ", ".join([str(element)
for element in short_conclusion])
return " => ".join((premise, conclusion,))
def __unicode__(self):
return self.__repr__()
def __cmp__(self, other):
if ((self.premise == other.premise) and
(self.conclusion == other.conclusion)):
return 0
else:
return -1
def __hash__(self):
return hash(frozenset((frozenset(self.premise),
frozenset(self.conclusion))))
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return(self.premise == other.premise and
self.conclusion == other.conclusion)
def __lt__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return(len(self.premise) < len(other.premise))
def __gt__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return(len(self.premise) > len(other.premise))
def __le__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return(not len(other.premise) < len(self.premise))
def __ge__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return(not len(self.premise) < len(other.premise))
def is_respected(self, some_set):
"""Checks whether implication repects `some_set or not.
In other words, is `some_set` a model of implication?"""
# if some_set contains every element from premise and not every
# element from conclusion then it doesn't respect an implication
# TODO: refactor
if isinstance(some_set, set):
return(self.conclusion.issubset(some_set) or not
self.premise.issubset(some_set))
else:
# Assume a partial example
return (self.conclusion.issubset(some_set[1]) or
not self.premise.issubset(some_set[0]))
def findSpecialImplication(implications, membership_oracle,
closure_operator, counter_example):
"""Returns first implication (A --> B) such that it's premise(A)
is not a subset of counter_example(C) and
member(C ∩ A) is false. The latter condition can also be interpreted as
C ∩ A is not a model of context(K).
"""
for implication in implications:
if not implication.premise.issubset(counter_example) \
and not membership_oracle(
counter_example.intersection(implication.premise),
closure_operator):
return implication
return None
def is_respected(implications, some_set):
"""Checks where some_set respects a set of implications
"""
for impl in implications:
if not impl.is_respected(some_set):
return False
else:
continue
return True