This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruleset.py
228 lines (193 loc) · 7.81 KB
/
ruleset.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Name: Mason Wilde
# KUID: 2645990
# Course: EECS 690
# Professor: Dr Jerzy Gryzmala-Busse
# Semester: Fall 2017
# Project: AQ Rule Inducer
# File: ruleset.py
# Date Modified: 2017-11-19
"""Structure and methods for rules, conditions, decisions, and rulesets"""
import sys
def get_available_attribute_values(dataset, rule):
"""Returns a dictionary of attributes and their available values given rule with negation"""
attribute_values = {}
for condition in rule.conditions:
if condition.attribute not in attribute_values:
attribute_values[condition.attribute] = dataset.attribute_value_ranges[condition.attribute]
attribute_values[condition.attribute] = list(set(attribute_values[condition.attribute]) - set([condition.value]))
return attribute_values
def expand_ruleset(ruleset, attr, values):
"""Expands a ruleset to include the given attribute and values"""
new_ruleset = []
for rule in ruleset:
# print "Starting with Rule:", rule.to_string()
starting_conditions = rule.conditions
for val in values:
# print "\tAdd rule with", attr, val
new_rule = Rule(decision=rule.decision)
# print "\trule to build on is", new_rule.to_string()
new_rule.conditions = starting_conditions + [Condition(attribute=attr, value=val)]
# print "\tappending", new_rule.to_string()
new_ruleset.append(new_rule)
return new_ruleset
def unnegate_rule(dataset, rule):
"""Returns a new ruleset without negation built from a rule with negation"""
available_attribute_values = get_available_attribute_values(dataset, rule)
new_ruleset = [Rule(decision=rule.decision)]
for attribute in available_attribute_values:
new_ruleset = expand_ruleset(new_ruleset, attribute, available_attribute_values[attribute])
return new_ruleset
def read_rules(filename):
"""Returns a ruleset read fror a .rul file"""
ruleset = Ruleset()
rule_file = open(filename, 'r')
blank_rule = Rule()
for line in rule_file:
new_rule = blank_rule
conditions, arrow, decision = line.partition("->")
decision = decision.replace("(", "").replace(")", "").strip()
decision = decision.split(",")
decision[0] = decision[0].strip()
decision[1] = decision[1].strip()
conditions = conditions.replace("(", "").replace(")", "").strip()
conditions = conditions.split("&")
conditions_list = []
for condition in conditions:
condition = condition.split(",")
condition[1] = condition[1].replace("not", "").strip()
condition = Condition(attribute=condition[0].strip(), value=condition[1].strip())
conditions_list.append(condition)
# print "new rule is", new_rule.to_string()
ruleset.rules.append(Rule(conditions=conditions_list, decision=Decision(label=decision[0], value=decision[1])))
# ruleset.display()
return ruleset
class Condition(object):
def __init__(self, attribute = None, value = None):
self._attribute = attribute
self._value = value
def __eq__(self, other):
return self.attribute == other.attribute and self.value == other.value
@property
def attribute(self):
return self._attribute
@attribute.setter
def attribute(self, value):
self._attribute = value
@property
def value(self):
return self._value
@value.setter
def value(self, n_value):
self._value = n_value
class Decision(object):
def __init__(self, label = None, value = None):
self._label = label
self._value = value
@property
def label(self):
return self._label
@label.setter
def label(self, value):
self._label = value
@property
def value(self):
return self._value
@value.setter
def value(self, n_value):
self._value = n_value
class Rule(object):
def __init__(self, conditions =[], decision = None):
self._conditions = conditions
self._decision = decision
@property
def conditions(self):
return self._conditions
@conditions.setter
def conditions(self, value):
self._conditions = value
@property
def decision(self):
return self._decision
@decision.setter
def decision(self, value):
self._decision = value
def to_string(self, negated=True):
"""Returns a string of the given rule"""
rule = ""
for i, condition in enumerate(self.conditions):
if i > 0:
rule += "& "
rule += "(" + condition.attribute + (", not " if negated else ", ") + condition.value + ") "
rule += "-> (" + self.decision.label + ", " + self.decision.value + ")"
return rule
def covers(self, case, negated=True):
"""Returns true if the given case is covered by the rule"""
for condition in self.conditions:
if negated:
if case.attribute_values[condition.attribute] == condition.value:
return False
else:
if case.attribute_values[condition.attribute] != condition.value:
return False
return True
def cases_covered(self, dataset, negated=True):
"""Returns a list of case numbers covered by the rule in a given dataset"""
covered = []
for i, case in enumerate(dataset.universe):
if(self.covers(case, negated)):
covered.append(i)
return covered
def is_consistent(self, dataset, negated=True):
"""Returns true if the rule is consistent for a given dataset"""
for case in self.cases_covered(dataset, negated):
if dataset.universe[case].decision != self.decision.value:
print "Rule is inconsistent"
print "\tRule:", self.to_string(negated)
print "\tCase: #", case
sys.stdout.write('\t')
for attribute in dataset.attributes:
sys.stdout.write(attribute + '\t')
sys.stdout.write(dataset.decision + '\n')
case = dataset.universe[case]
sys.stdout.write('\t')
for attribute in dataset.attributes:
sys.stdout.write(case.attribute_values[attribute] + '\t')
sys.stdout.write(case.decision + '\n')
return False
return True
class Ruleset(object):
def __init__(self, rules=[]):
self._rules = rules
@property
def rules(self):
return self._rules
@rules.setter
def rules(self, value):
self._rules = value
def display(self, negated=True):
for rule in self.rules:
print rule.to_string(negated)
def print_to_file(self, filename, negated=True):
"""Prints ruleset to a file"""
f = open(filename, 'w')
for rule in self.rules:
f.write(rule.to_string(negated) + '\n')
f.closed
def unnegate(self, dataset):
"""Creates a new ruleset of rules without negation"""
new_ruleset = []
for rule in self.rules:
new_ruleset = new_ruleset + unnegate_rule(dataset, rule)
return new_ruleset
def is_consistent(self, dataset, negated=True):
"""returns true if the ruleset is consistent for the given dataset"""
for rule in self.rules:
if not rule.is_consistent(dataset, negated):
return False
return True
def is_complete(self, dataset, negated=True):
"""returns true if the ruleset is complete for the given dataset"""
covered = []
for rule in self.rules:
covered = list(set(covered).union(set(rule.cases_covered(dataset, negated))))
return covered == range(len(dataset.universe))