Newer
Older
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""Knowledge in learning, Chapter 19"""
from random import shuffle
from utils import powerset
from collections import defaultdict
# ______________________________________________________________________________
def current_best_learning(examples, h, examples_so_far=[]):
""" [Figure 19.2]
The hypothesis is a list of dictionaries, with each dictionary representing
a disjunction."""
if not examples:
return h
e = examples[0]
if is_consistent(e, h):
return current_best_learning(examples[1:], h, examples_so_far + [e])
elif false_positive(e, h):
for h2 in specializations(examples_so_far + [e], h):
h3 = current_best_learning(examples[1:], h2, examples_so_far + [e])
if h3 != 'FAIL':
return h3
elif false_negative(e, h):
for h2 in generalizations(examples_so_far + [e], h):
h3 = current_best_learning(examples[1:], h2, examples_so_far + [e])
if h3 != 'FAIL':
return h3
return 'FAIL'
def specializations(examples_so_far, h):
"""Specialize the hypothesis by adding AND operations to the disjunctions"""
hypotheses = []
for i, disj in enumerate(h):
for e in examples_so_far:
for k, v in e.items():
if k in disj or k == 'GOAL':
continue
h2 = h[i].copy()
h2[k] = '!' + v
h3 = h.copy()
h3[i] = h2
if check_all_consistency(examples_so_far, h3):
hypotheses.append(h3)
shuffle(hypotheses)
return hypotheses
def generalizations(examples_so_far, h):
"""Generalize the hypothesis. First delete operations
(including disjunctions) from the hypothesis. Then, add OR operations."""
hypotheses = []
# Delete disjunctions
disj_powerset = powerset(range(len(h)))
for disjs in disj_powerset:
h2 = h.copy()
for d in reversed(list(disjs)):
del h2[d]
if check_all_consistency(examples_so_far, h2):
hypotheses += h2
# Delete AND operations in disjunctions
for i, disj in enumerate(h):
a_powerset = powerset(disj.keys())
for attrs in a_powerset:
h2 = h[i].copy()
for a in attrs:
del h2[a]
if check_all_consistency(examples_so_far, [h2]):
h3 = h.copy()
h3[i] = h2.copy()
hypotheses += h3
# Add OR operations
hypotheses.extend(add_or(examples_so_far, h))
shuffle(hypotheses)
return hypotheses
def add_or(examples_so_far, h):
"""Adds an OR operation to the hypothesis. The AND operations in the disjunction
are generated by the last example (which is the problematic one)."""
ors = []
e = examples_so_far[-1]
attrs = {k: v for k, v in e.items() if k != 'GOAL'}
a_powerset = powerset(attrs.keys())
for c in a_powerset:
h2 = {}
for k in c:
h2[k] = attrs[k]
if check_negative_consistency(examples_so_far, h2):
h3 = h.copy()
h3.append(h2)
ors.append(h3)
return ors
# ______________________________________________________________________________
def check_all_consistency(examples, h):
"""Check for the consistency of all examples under h"""
for e in examples:
if not is_consistent(e, h):
return False
return True
def check_negative_consistency(examples, h):
"""Check if the negative examples are consistent under h"""
for e in examples:
if e['GOAL']:
continue
if not is_consistent(e, [h]):
return False
return True
def disjunction_value(e, d):
"""The value of example e under disjunction d"""
for k, v in d.items():
if v[0] == '!':
# v is a NOT expression
# e[k], thus, should not be equal to v
if e[k] == v[1:]:
return False
elif e[k] != v:
return False
return True
def guess_value(e, h):
"""Guess value of example e under hypothesis h"""
for d in h:
if disjunction_value(e, d):
return True
return False
def is_consistent(e, h):
return e["GOAL"] == guess_value(e, h)
def false_positive(e, h):
if e["GOAL"] == False:
if guess_value(e, h):
return True
return False
def false_negative(e, h):
if e["GOAL"] == True:
if not guess_value(e, h):
return True
return False