-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.py~
More file actions
341 lines (250 loc) · 9.25 KB
/
lcs.py~
File metadata and controls
341 lines (250 loc) · 9.25 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- coding: utf-8 -*-
#import ea.py
import random_rules
import time, sys, getopt, ast
''' Notes '''
# After a while, we need to choose a number where the classifier is sufficiently experienced enough to be deleted if it's fitness is too low
''' (Not used)
HEADINGS_DICT = {'age': 'continuous',
'workclass': 'discrete',
'fnlwgt': 'continuous',
'education':'discrete',
'education-num': 'continuous',
'marital-status': 'discrete',
'occupation':'discrete',
'relationship':'discrete',
'race':'discrete',
'sex':'discrete',
'capital-gain':'continuous',
'capital-loss':'continuous',
'hours-per-week':'continuous',
'native-country':'discrete',
'salary':'class-label'}
'''
FILENAME = 'adult.data'
HEADINGS = ['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'capital-gain', 'capital-loss', 'hours-per-week', 'native-country', 'salary']
CLASS_LABELS = ['<=50K', '>50K']
NUM_CLASSIFIERS = 500
LEARN_MODE = 0
CLASSIFY_MODE = 1
CLASSIFIERS_FILE = "classifiers"
verbose = False
def make_verbose():
global verbose
verbose = True
# A classifier.
# Contains a condition, an action, a fitness value
class Classifier:
__all__ = set()
def __init__(self, condition = None, action = None, duplicate = True, from_dict = False):
if from_dict:
self.read_from_dictionary(from_dict)
else:
if condition:
self.condition = condition
else:
self.condition = random_rules.generate_condition()
if action:
self.action = action
else:
self.action = random_rules.generate_action(CLASS_LABELS)
self.prediction = 0.0
self.fitness = 0.0
self.error = 0.0
self.experience = 0
self.times_correct = 0
self.times_wrong = 0
self.accuracy = 0.0
# Duplicate this classifier, creating the same classifier but for the inverse rule
if duplicate:
def flipped_action(action):
if action == '<=50K':
return '>50K'
else:
return '<=50K'
dc = Classifier(self.condition, flipped_action(self.action), False)
self.__class__.__all__.add(self)
#self.test = random_rules.generate_condition()
# Checks if condition is met in environment
def check_condition(self, environment):
for k, v in self.condition.items():
if environment.dictionary[k] != v:
return False # One condition of this classifier was not met
return True
# Learns from the environment. Checks whether the rule held by the classifier is correct or not, depending on whether its conditions are met in the environment
def learn(self, environment):
# Updates the classifier's parameters based on whether it was correct or incorrect.
def update_classifier(was_correct):
self.experience += 1
if was_correct:
self.times_correct += 1
self.accuracy = self.times_correct * 1.0 / self.experience * 1.0
else:
self.times_wrong += 1
#self.accuracy =
# If this classifier has met all its conditions on the environment, add +1 experience points and return the classifier's action
# (which in this case is either <= 50K or > 50K)
if self.check_condition(environment):
was_correct = (self.action == environment.correct_class)
update_classifier(was_correct)
#self.print_details()
# return self.action
# else:
# return None
def classify(self, environment):
if self.check_condition(environment):
return (self.accuracy, self.action)
# Prints the details of the classifier in a nice, easy-to-read manner.
def print_details(self):
if verbose:
# print("{0:<15s} : {1}".format("Classifier #", self.id))
print("{0:<15s} : {1}".format("Condition:", self.condition))
print("{0:<15s} : {1}".format("Action:", self.action))
# print("{0:<15s} : {1}".format("Prediction:", self.prediction))
print("{0:<15s} : {1}".format("Fitness:", self.fitness))
print("{0:<15s} : {1}".format("Experience:", self.experience))
print("{0:<15s} : {1}".format("Times Correct:", self.times_correct))
print("{0:<15s} : {1}".format("Times Wrong:", self.times_wrong))
print("{0:<15s1} : {1}".format("Accuracy:", self.accuracy * 100))
# print("{0:<15s} : {1}".format("Error:", self.error))
print()
# Outputs the classifier's info as a dictionary
def to_dictionary(self):
to_dict = {}
# to_dict["id"] = self.id
to_dict["condition"] = self.condition
to_dict["action"] = self.action
to_dict["accuracy"] = self.accuracy
to_dict["experience"] = self.experience
return to_dict
# Inputs the classifiers info from a dictionary
def read_from_dictionary(self, from_dict):
# self.id = from_dict["id"]
self.condition = from_dict["condition"]
self.action = from_dict["action"]
self.accuracy = from_dict["accuracy"]
self.experience = from_dict["experience"]
# One environment (a dictionary that maps field names to their values).
# For example,
# {'education': 'HS-grad', 'workclass': 'Local-gov', 'age': 67 ...}
# self.correct_class = The correct class label for this particular environment.
class Environment:
def __init__(self, data):
self.dictionary = {}
for h in range(len(HEADINGS) - 1):
# Convert field to integer if it is numeric, otherwise keep it as a string (so it may handle discrete/continuous variables).
self.dictionary[HEADINGS[h]] = int(data[h]) if unicode(data[h]).isnumeric() else data[h]
self.correct_class = data[len(HEADINGS) - 1]
# Prints the details of the environment in a nice, easy-to-read manner.
def print_details(self):
if verbose: #match_set = []
#action_set = []
for k, v in self.dictionary.items():
print("{0:<20s} : {1}".format(k, v))
print("----------------------------")
print("{0:<20s} : {1}".format("Correct class", self.correct_class))
def main(argv):
def print_usage():
print('--------------------------')
print('lcs.py usage')
print('--------------------------')
print('Options:')
print('-v verbose (print output)')
print('-l Learn mode (default)')
print('-c Classify mode')
mode = LEARN_MODE
try:
opts, args = getopt.getopt(argv, "hvlc")
except getopt.GetoptError: #match_set = []
#action_set = []
print_usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print_usage()
sys.exit()
elif opt == '-v':
make_verbose()
elif opt == '-l':
mode = LEARN_MODE
elif opt == '-c':
mode = CLASSIFY_MODE
# Creates a list of environments.
# Environments are stored as objects, which contain a dictionary, and a correct_class.
def create_environments():
with open(FILENAME, "r") as file:
data = [f.replace(' ', '').rstrip().split(',') for f in file.readlines()]
return [Environment(d) for d in data]
environments = create_environments()
def do_learn_mode():
# Creates the initial population of classifiers, in the form of a list.
def create_classifiers():
for x in range(NUM_CLASSIFIERS):
Classifier()
return Classifier.__all__
# Writes all classifiers to a file, in dictionary format
def write_classifiers(classifiers, output_file):
for c in classifiers:
output_file.write(str(c.to_dictionary()))
output_file.write('\n')
# One timestep.
def step(environment, classifiers):
for c in classifiers:
c.learn(environment)
time_start = time.time()
classifiers = create_classifiers()
if verbose:
for c in classifiers:
c.print_details()
print('------------------------------------')
for x in range(len(environments)):
step(environments[x], classifiers);
time_end = time.time()
total_time = time_end - time_start
print(total_time, "seconds")
output_file = open(CLASSIFIERS_FILE, "w")
write_classifiers(classifiers, output_file)
def do_classify_mode():
def read_classifiers():
print("Reading classifiers...")
with open(CLASSIFIERS_FILE, "r") as file:
classifier_dict = [ast.literal_eval(l) for l in file.readlines()]
return [Classifier(from_dict = k) for k in classifier_dict]
# One timestep.
def step(environment, classifiers, total_correct, total_seen):
match_set = []
for c in classifiers:
cl = c.classify(environment) # [classifier, action, accuracy]
if cl:
match_set.append(cl)
sorted_set = sorted(match_set, key=lambda tup: tup[1])
if len(sorted_set) > 0:
action = sorted_set[0][1]
if action == environment.correct_class:
correct = "Correct!"
total_correct += 1
else:
correct = "Fail "
print("Action:", sorted_set[0][1], " | Accuracy:", "%.2f" % (sorted_set[0][0] * 100), "% |", correct, "{", total_correct, total_seen, "(", "%.5f" % (total_correct * 1.0 / total_seen * 1.0 * 100), ") }")
else:
"No classification can be made."
return total_correct
classifiers = read_classifiers()
total_correct = 0
total_seen = 0
for x in range(len(environments)):
total_seen += 1
total_correct = step(environments[x], classifiers, total_correct, total_seen);
if mode == LEARN_MODE:
do_learn_mode()
else:
do_classify_mode()
if __name__ == "__main__":
main(sys.argv[1:])
''' Notes '''
# Look at all classifiers in the match set
# Look at the classifier in M with highest accuracy
# Action set = all classifiers with that same class label as action
# Reward = given when it is part of the action set
# Prediction p , estimate payoff when rule is seen
# Accuracy relative to others