-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean_conj_predictor.py
More file actions
70 lines (58 loc) · 2.07 KB
/
boolean_conj_predictor.py
File metadata and controls
70 lines (58 loc) · 2.07 KB
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
class Var:
"""A simple example class"""
def __init__(self, isPositive=True, ind=0):
self.isPositiveForm = isPositive
self.index = ind
def calcVal(self, val):
return (self.isPositiveForm and val == 1) or (not (self.isPositiveForm) and val == 0)
class Hypothesis:
def __init__(self, d):
self.list = []
for i in range(1, d):
self.list.append(Var(True, i))
self.list.append(Var(False, i))
def calcHypothesis(self):
for item in self.list:
if (not (item.calcVal(1))):
return 0
return 1
def removeElemInHypo(self, isPos, index):
pie = 0
for elem in self.list:
if elem.index == index and isPos == elem.isPositiveForm:
del self.list[pie]
pie = pie + 1
def printHypo(self):
string_answer = '';
for elem in self.list:
if elem.isPositiveForm:
string_answer += 'x{0},'.format(elem.index)
else:
string_answer += 'not(x{0}),'.format(elem.index)
return string_answer[0:-1]
def main():
import numpy as np
import sys as sys
training_examples = np.loadtxt(sys.argv[1], dtype=int)
d = len(training_examples[0])
num_examples = len(training_examples)
training_examples.reshape((num_examples, d))
X, Y = training_examples[:, :d - 1], training_examples[:, d - 1]
# Consistency Algorithms
# Intialize all variables with pos form and neg form
hypo = Hypothesis(d)
for item, result in zip(X, Y):
curr_hypo = hypo
if curr_hypo.calcHypothesis() == 0 and result == 1:
indexush = 1
for itemInX in item:
if itemInX == 1:
curr_hypo.removeElemInHypo(False, indexush)
elif itemInX == 0:
curr_hypo.removeElemInHypo(True, indexush)
indexush = indexush + 1
elif result == 0:
hypothesis = curr_hypo
with open('output.txt', 'w') as f:
f.write(hypothesis.printHypo())
main()