-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXCS.py
More file actions
442 lines (369 loc) · 11.8 KB
/
XCS.py
File metadata and controls
442 lines (369 loc) · 11.8 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# coding=utf-8
import copy
import random as rd
from Classifier import Classifier
import Interface
import Cons
import matplotlib.pyplot as plt
import time as tt
import curses
def does_match(cl, perception):
for i in range(len(cl.condition)):
if cl.condition[i] != cons.dontCare and cl.condition[i] != perception[i]:
return False
return True
def generate_covering_classifier(env, action):
newcl = Classifier()
newcl.condition = ['0'] * len(env)
for i in range(len(env)):
if rd.random() < cons.P_dontcare:
newcl.condition[i] = cons.dontCare
else:
newcl.condition[i] = env[i]
for i in range(len(action)):
if action[i] is False:
newcl.action = i
newcl.prediction = cons.predictionIni
newcl.predictionError = cons.predictionErrorIni
newcl.fitness = cons.fitnessIni
newcl.experience = 0
newcl.timeStamp = time
newcl.actionSetSize = 1
newcl.numerosity = 1
return newcl
def delete_from_population(Pop):
sum = 0
sum1 = 0
for c in Pop:
sum += c.numerosity
sum1 += c.fitness
if sum <= cons.n:
return None
avFitnessInPopulation = sum / sum1
voteSum = 0
for c in Pop:
voteSum = voteSum + c.getDelProp(avFitnessInPopulation)
choicePoint = rd.random() * voteSum
voteSum = 0
for c in Pop:
voteSum = voteSum + c.getDelProp(avFitnessInPopulation)
if voteSum > choicePoint:
if c.numerosity > 1:
c.numerosity -= 1
else:
Pop.remove(c)
return None
def gen_match_set(Pop: list, perception: list):
"""
:param Pop:
:param perception:
:return: list
"""
m = []
action = [False] * 4
while True:
m = []
for cl in Pop:
if does_match(cl, perception):
m.append(cl)
action[cl.action] = True
for i in range(cons.nbAction):
pass
if action != [True] * cons.nbAction:
newCL = generate_covering_classifier(perception, action)
Pop.append(newCL)
delete_from_population(Pop)
m = None
if m is not None:
break
return m
def gen_prediction_array(M):
"""
:type M: List
"""
pa = [0.0] * cons.nbAction
fitSum = [0.0] * cons.nbAction
for cl in M:
if pa[cl.action] is None:
pa[cl.action] = cl.prediction * cl.fitness
else:
pa[cl.action] = pa[cl.action] + cl.prediction * cl.fitness
fitSum[cl.action] = fitSum[cl.action] + cl.fitness
for i in range(cons.nbAction):
if fitSum[i] != 0.0:
pa[i] /= fitSum[i]
return pa
def select_action(pa, t):
best_action = 0.0
if rd.random() < cons.pExplor:
best_action = rd.choice(pa)
else:
for i in range(len(pa)):
if best_action < pa[i]:
best_action = pa[i]
for i in range(len(pa)):
if best_action == pa[i]:
best_action = i
return best_action
def gen_action_set(M, act):
action_set = []
for cl in M:
if cl.action == act:
action_set.append(cl)
return action_set
def select_offspring(A):
"""
:param A:
:return:
"""
fitnessSum = 0
for cl in A:
fitnessSum += cl.fitness
choicePoint = rd.random() * fitnessSum
fitnessSum = 0
for cl in A:
fitnessSum = fitnessSum + cl.fitness
if fitnessSum > choicePoint:
return cl
def apply_crossover(cl1, cl2):
# assert (cl1 is type(Classifier)), "Wrong argument for apply_crossover"
# assert (cl2 is type(Classifier)), "Wrong argument for apply_crossover"
"""
:type cl1: Classifier
:param cl1:
:param cl2:
:return:
"""
# noinspection PyTypeChecker
x = rd.random() * len(cl1.condition) + 1
# noinspection PyTypeChecker
y = rd.random() * len(cl1.condition) + 1
if x > y:
tmp = x
x = y
y = x
i = 0
while True:
if x <= i < y:
tp = cl1.condition[i]
cl1.condition[i] = cl2.condition[i]
cl2.condition[i] = tp
i += 1
if i < y:
break
def apply_mutation(cl: Classifier, perception: list):
"""
:type cl: Classifier
:param cl:
:type perception: list
:param perception:
:return:
"""
for i in range(len(cl.condition)):
if rd.random() < cons.nu:
if cl.condition[i] == cons.dontCare:
cl.condition[i] = perception[i]
else:
cl.condition[i] = cons.dontCare
if rd.random() < cons.nu:
c = rd.choice([i for i in range(0, (cons.nbAction - 1))])
cl.action = c
def insert_in_population(cl: Classifier, Pop: list):
for c in Pop:
if c.equals(c):
c.numerosity += 1
return
Pop.append(cl)
def run_ga(a, pop, percep):
"""
:type pop: list
"""
sumNum = 0
sumTSN = 0
for clo in a:
sumTSN += clo.timeStamp * clo.numerosity
sumNum += clo.numerosity
if (time - sumTSN) / sumNum > cons.theta_GA:
for clo in a:
clo.timeStamp = time
parent1 = select_offspring(a)
parent2 = select_offspring(a)
child1 = copy.copy(parent1)
child2 = copy.copy(parent2)
child1.numerosity += 1
child2.numerosity += 1
child1.experience = 0
child2.experience = 0
if rd.random() < cons.pX:
apply_crossover(child1, child2)
child1.prediction = (parent1.prediction + parent2.prediction) / 2
child1.predictionError = (parent1.predictionError + parent2.predictionError) / 2
child1.fitness = (parent1.fitness + parent2.fitness) / 2
child2.prediction = child1.prediction
child2.predictionError = child1.predictionError
child2.fitness = child1.fitness
child1.fitness *= 0.1
child2.fitness *= 0.1
apply_mutation(child1, percep)
apply_mutation(child2, percep)
if cons.doGASubsumption:
if child1.subsumes(parent1):
parent1.numerosity += 1
elif child1.subsumes(parent2):
parent2.numerosity += 1
else:
insert_in_population(child1, pop)
else:
insert_in_population(child1, pop)
if cons.doGASubsumption:
if child2.subsumes(parent1):
parent1.numerosity += 1
elif child2.subsumes(parent2):
parent2.numerosity += 1
else:
insert_in_population(child2, pop)
else:
insert_in_population(child2, pop)
delete_from_population(pop)
def update_fitness(A):
assert (type(A) == list), 'expected List got'
accurancySum = 0
k = [0] * len(A)
for i in range(len(A)):
if A[i].predictionError < cons.epsilon_0:
k[i] = 1
else:
k[i] = cons.alpha * ((A[i].predictionError / cons.epsilon_0) ** (-cons.nu))
accurancySum += k[i] * A[i].numerosity
for i in range(len(A)):
A[i].fitness += cons.beta * (k[i] * A[i].numerosity / accurancySum - A[i].fitness)
def do_action_set_subsumption(A, Pop):
subsumer = None
for cl in A:
if cl.isSubsumer():
if subsumer is None or cl.isMoreGeneral(subsumer):
subsumer = cl
# If a subsumer was found, subsume all more specific classifiers in the action set
if subsumer != None:
for cl in A:
if subsumer.isMoreGeneral(cl):
subsumer.numerosity += cl.numerosity
A.remove(cl)
Pop.remove(cl)
def update_set(A, p, Pop):
sumNum = 0
for cl in A:
sumNum += cl.numerosity
for cl in A:
cl.experience += 1
# Update prediction cl.prediction
if cl.experience < 1 / cons.beta:
cl.prediction = cl.prediction + (p - cl.prediction) / cl.experience
else:
cl.prediction = cl.prediction + cons.beta * (p - cl.prediction)
# update prediction error cl.experience
if cl.experience < 1 / cons.beta:
cl.predictionError += (abs(p - cl.prediction) - cl.predictionError) / cl.experience
else:
cl.predictionError += cons.beta * (abs(p - cl.prediction) - cl.predictionError)
# update action set size estimate
if cl.experience < 1 / cons.beta:
cl.actionSetSize = cl.actionSetSize * (sumNum - cl.actionSetSize) / cl.experience
else:
cl.actionSetSize += cons.beta * (sumNum - cl.actionSetSize)
update_fitness(A)
if cons.doActionSetSubsumption:
do_action_set_subsumption(A, Pop)
if __name__ == '__main__':
# stdscr = curses.initscr()
# curses.noecho()
# curses.cbreak()
# print("Hello")
for i in range(1):
perf = []
perfFitness = []
perfClasseur = []
rwd = 0
M = []
Pop = []
PA = []
act = None
env = Interface.Maze()
perception_ = None
A_ = None
p = None
p_ = None
cons = Cons.Cons()
time = 0
bool = True
meanA = 0
path = []
tmp = 0
d = []
while bool:
perception = env.perception
M = gen_match_set(Pop, perception)
PA = gen_prediction_array(M)
act = select_action(PA, time)
A = gen_action_set(M, act)
p = env.execute_action(act)
if A_ is not None:
# noinspection PyUnresolvedReferences
P = p_ + cons.gamma * max(PA)
update_set(A_, P, Pop)
run_ga(A_, Pop, perception_)
if env.flag:
P = p
update_set(A, P, Pop)
run_ga(A, Pop, perception)
A_ = None
if p < 0:
env.stop()
else:
A_ = A
p_ = p
perception_ = perception
env.flag = False
path.append([env.__getattr__(env.posx), env.__getattr__(env.posy), p, env.__getattr__(env.food)])
time += 1
if time > 10000:
bool = False
if time % 1 == 0:
perfClasseur.append(len(Pop))
m = 0
for cl in Pop:
m += cl.getAccuracy()
m /= len(Pop)
meanA = m
perfFitness.append(m)
if p < 0:
d.append(time - tmp)
tmp = time
if bool is False:
break
for cl in Pop:
print(cl.condition, '', cl.action, '', cl.prediction, ',', cl.predictionError, cl.fitness)
print(len(perf))
print(len(Pop))
print(perfClasseur)
print(perfFitness)
print(d)
# replay = Interface.PlayMaze()
# for i in range(time):
# string = "temps : ", i, " food :", path[i][3], " perf :", "%.2f" % perfFitness[i], " "
# string1 = " rwd : ", path[i][2], ' '
# string1 = str(string1).replace(",", " ").replace("(", " ").replace(")", " ").replace("'", " ")
# string = str(string).replace(",", " ").replace("(", " ").replace(")", " ").replace("'", " ")
# if i > 7000:
# tt.sleep(0.01)
# replay.play(path[i][0], path[i][1], string, string1)
plt.plot(d)
plt.ylabel('Numbers of Classifier')
plt.xlabel('Time')
plt.show()
plt.plot(perfFitness)
plt.ylabel('Quality q')
plt.xlabel('Time')
plt.show()
#curses.endwin()
exit(0)