-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleRBES.py
executable file
·141 lines (120 loc) · 3.55 KB
/
SimpleRBES.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import collections
import sys
__author__ = "Krzysztof Wróbel"
class SimpleRBES:
"""
SimpleRBES (Rule-Based Expert System) is one level rule system with rules in format:
IF symptom [AND symptom] THEN hypothesis
Additionally have abbility to learn confidential factors (CF) of rules.
"""
def readStructure(self, path):
"""Reads rules from file with format: each line represents one rule, elements are splitted by comma and the last one is hypothesis.
Args:
path - path to file with test cases
"""
f = open(path)
self.structure = []
for line in f:
s = line.strip().split(',')
h = s[-1]
a = frozenset(s[0:-1])
self.structure.append([a,h,None])
f.close()
def learnCF(self, path):
"""Reads test cases from file with format: each line represents one rule, elements are splitted by comma and the last one is hypothesis. Then learns confidential factors of rules.
Args:
path - path to file with test cases
"""
f = open(path)
db = []
db_rules = set()
for line in f:
s = line.strip().split(',')
h = s[-1]
a = frozenset(s[0:-1])
db.append((a,h))
db_rules.add(a)
f.close()
if len(db_rules) != len(set(db)):
print 'not consistent'
self.__learn(db)
def __learn(self, db):
for record in self.structure:
a = record[0]
h = record[1]
true = 0
false = 0
for a2,h2 in db:
if a<=a2: #rule works
if h==h2:
true += 1
else:
false += 1
if true+false>0:
record[2] = float(true)/(true+false)
else:
record[2] = 0.0
#print record
def diagnose(self, symptomy):
"""Returns dictionary of diagnosed hipothesis with CF.
Args:
symptomy - A set of symptoms
"""
hipotezy = collections.defaultdict(float)
for record in self.structure:
a = record[0]
if a<=symptomy:
h = record[1]
cf = record[2]
hipotezy[h] = max(hipotezy[h], cf)
return hipotezy
def diagnoseCF(self, symptomyCF):
"""Returns dictionary of diagnosed hipothesis with CF.
Args:
symptomyCF - A dictionary of symptoms witf CF.
"""
symptomy=frozenset(symptomyCF.keys())
hipotezy = collections.defaultdict(float)
for record in self.structure:
a = record[0]
if a<=symptomy:
h = record[1]
cf = record[2]
mini = 1.0
for au in a:
mini = min(mini, symptomyCF[au])
hipotezy[h] = max(hipotezy[h], cf*mini)
return hipotezy
def printRules(self):
"""Prints rules woth CF."""
for record in self.structure:
a = record[0]
h = record[1]
cf = record[2]
print ', '.join(list(a)) + ' -> ' + h + ' (' + str(cf) + ')'
def printDiagnoseCF(self, symptomyCF):
"""Prints sorted hypothesis for symptoms with CF.
Args:
symptomyCF - A dictionary of symptoms witf CF.
"""
for h,cf in sorted(self.diagnoseCF(symptomyCF).items(), key=lambda (k,v): (v,k), reverse=True):
print h, cf
if __name__ == "__main__":
srbes = SimpleRBES()
srbes.readStructure(sys.argv[1])
srbes.learnCF(sys.argv[1])
srbes.printRules()
print
srbes.printDiagnoseCF({'AU15':0.8, 'AU17':0.9})
print
srbes.printDiagnoseCF({'AU9':1.0, 'AU10':1.0, 'AU27':0.3})
print
srbes.printDiagnoseCF({'AU12':1.0, 'AU6':0.8})
print
srbes.printDiagnoseCF({'AU4':0.9, 'AU10':1.0, 'AU9':0.2})
print
srbes.printDiagnoseCF({'AU1':0.9, 'AU5':0.8, 'AU20':0.6, 'AU25':0.4})
print
srbes.printDiagnoseCF({'AU27':0.9, 'AU26':0.9, 'AU5':0.9, 'AU1':0.7, 'AU2':0.7})