/
XCSProgram.py
272 lines (262 loc) · 9.83 KB
/
XCSProgram.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
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
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.1'
# jupytext_version: 0.8.5
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# language_info:
# codemirror_mode:
# name: ipython
# version: 3
# file_extension: .py
# mimetype: text/x-python
# name: python
# nbconvert_exporter: python
# pygments_lexer: ipython3
# version: 3.6.7rc2
# ---
# %%
# coding: utf-8
# %%
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import random
import csv
import os.path
from XCSConfig import *
from XCSEnvironment import *
from XCSClassifier import *
from XCSClassifierSet import *
from XCSMatchSet import *
from XCSActionSet import *
# %%
class XCSProgram:
def __init__(self):
self.env = XCSEnvironment()
self.perf = []
def init(self):
self.env = XCSEnvironment()
self.perf = []
def run_experiments(self):
for exp in range(conf.max_experiments):
random.seed(exp)
self.actual_time = 0.0
self.pop = XCSClassifierSet(self.env, self.actual_time)
self.init()
for iteration in range(conf.max_iterations):
self.run_explore()
self.run_exploit(iteration)
print("exp iteration : " + str(iteration))
print("now"+str(exp))
self.file_writer(exp)
self.performance_writer(exp)
self.make_graph()
def run_explore(self):
"""環境の状態をセット"""
self.env.set_state()
"""MatchSet[M]を生成"""
self.match_set = XCSMatchSet(self.pop, self.env, self.actual_time)
"""MatchSet[M]に基づいて,prediction array[PA]を生成"""
self.generate_prediction_array()
"""prediction array[PA]に基づいて行動選択"""
self.select_action()
"""選択した行動に基づいて,ActionSet[A]を生成する"""
self.action_set = XCSActionSet(self.match_set, self.action, self.env, self.actual_time)
"""行動を取る.強化学習が働く"""
self.action_set.do_action()
"""ActionSet[A]内に対してパラメータ更新"""
self.action_set.update_action_set()
"""ActionSet[A]内でルールの包摂をする"""
self.action_set.do_action_set_subsumption(self.pop)
"""ActionSet[A]に対してGAを回す"""
self.run_GA()
if len(self.pop.cls) > conf.N:
self.pop.delete_from_population()
self.actual_time += 1.0
def run_exploit(self, iteration): #正答率計算 確認用
if iteration%100 == 0:
p = 0
for i in range(100):
self.env.set_state()
self.match_set = XCSMatchSet(self.pop, self.env, self.actual_time)
self.generate_prediction_array()
self.action = self.best_action()
if self.env.is_true(self.action):
p += 1
self.perf.append(p)
def select_action(self):
if random.random() > conf.p_explr:
self.action = self.best_action()
else:
self.action = random.randrange(2)
def best_action(self):
big = self.p_array[0]
best = 0
for i in range(2):
if big < self.p_array[i]:
big = self.p_array[i]
best = i
return best
def generate_prediction_array(self):
self.p_array = [0,0]
self.f_array = [0,0]
for cl in self.match_set.cls:
self.p_array[cl.action] += cl.prediction * cl.fitness
self.f_array[cl.action] += cl.fitness
for i in range(2):
if self.f_array[i] != 0:
self.p_array[i] /= self.f_array[i]
def select_offspring(self):
"""fitnessを基に親をルーレット選択"""
fit_sum = self.action_set.fitness_sum()
choice_point = fit_sum * random.random()
fit_sum = 0.0
for cl in self.action_set.cls:
fit_sum += cl.fitness
if fit_sum > choice_point:
return cl
return None
def apply_crossover(self, cl1, cl2):
"""2点交叉適用"""
length = len(cl1.condition)
sep1 = int(random.random() * (length))
sep2 = int(random.random() * (length))
if sep1 > sep2:
sep1,sep2 = sep2,sep1
elif sep1 == sep2:
sep2 = sep2+1
cond1 = cl1.condition
cond2 = cl2.condition
for i in range(sep1, sep2):
if cond1[i] != cond2[i]:
cond1[i],cond2[i] = cond2[i],cond1[i]
cl1.condition = cond1
cl2.condition = cond2
def apply_mutation(self,cl):
"""突然変異"""
i = 0
for i in range(len(cl.condition)):
if random.random() < conf.myu:
if cl.condition[i] == '#':
cl.condition[i] = self.env.state[i]
else:
cl.condition[i] = '#'
if random.random() < conf.myu:
cl.action = random.randrange(2)
def run_GA(self):
if self.actual_time - self.action_set.ts_num_sum() / self.action_set.numerosity_sum() > conf.theta_ga:
for cl in self.action_set.cls:
cl.time_stamp = self.actual_time
parent1 = self.select_offspring()
parent2 = self.select_offspring()
child1 = parent1.deep_copy(self.actual_time)
child2 = parent2.deep_copy(self.actual_time)
child1.numerosity = 1
child2.numerosity = 1
child1.experience = 0
child2.experience = 0
if random.random() < conf.chi:
self.apply_crossover(child1, child2)
child1.prediction = (parent1.prediction+parent2.prediction)/2.0
child1.error = 0.25*(parent1.error+parent2.error)/2.0
child1.fitnes = 0.1*(parent1.fitness+parent2.fitness)/2.0
child2.prediction = child1.prediction
child2.error = child1.error
child2.fitness = child1.fitness
self.apply_mutation(child1)
self.apply_mutation(child2)
if conf.doGASubsumption:
if parent1.does_subsume(child1):
parent1.numerosity += 1
elif parent2.does_subsume(child1):
parent2.numerosity += 1
else:
self.pop.insert_in_population(child1)
if parent1.does_subsume(child2):
parent1.numerosity += 1
elif parent2.does_subsume(child2):
parent2.numerosity += 1
else:
self.pop.insert_in_population(child2)
else:
self.pop.insert_in_population(child1)
self.pop.insert_in_population(child2)
while self.pop.numerosity_sum() > conf.N:
self.pop.delete_from_population()
def file_writer(self,num):
file_name = "population"+str(num)+".csv"
write_csv = csv.writer(open(file_name,'w'),lineterminator='\n')
write_csv.writerow(["condition","action","fitness","prediction","error","numerosity","experience","time_stamp","action_set_size"])
for cl in self.pop.cls:
cond = ""
for c in cl.condition:
cond += str(c)
write_csv.writerow([cond,cl.action,cl.fitness,cl.prediction,cl.error,cl.numerosity,cl.experience,cl.time_stamp,cl.action_set_size])
def performance_writer(self,num):
file_name = "performance" + str(num) + ".csv"
np.savetxt(file_name, np.array(self.perf), fmt="%d", delimiter=",")
def make_graph(self):
performance = []
"""操作するファイルはperformance0.csvスタート"""
i = 0
file_path = "performance" + str(i) + ".csv"
while os.path.exists(file_path):
pf = np.loadtxt(file_path, delimiter=",")
performance.append(pf)
i += 1
file_path = "performance" + str(i) + ".csv"
"""データの数 = whileでインクリメントした分"""
data_num = i
"""データの中身の長さ = np.loadtxtしたデータのlen"""
data_length = len(sp.loadtxt("performance0.csv", delimiter=","))
pf = []
"""0, 100, 200, 300, ..., data_length*100"""
x = np.arange(0,data_length*100,100)
for i in range(data_length):
sum = 0.0
for j in range(data_num):
sum += performance[j][i]
pf.append(sum/float(data_num))
pf = np.array(pf)
np.savetxt("ave_performance.csv", pf, delimiter=",")
fig = plt.figure(figsize=(16, 10))
ax = fig.add_subplot(1,1,1)
ax.plot(x, pf, linewidth=2, label='performance')
ax.set_ylim(40, 110)
ax.set_xlim(0, data_length*100)
ax.set_title('Performance')
ax.set_yticklabels(['40%','50%','60%','70%','80%','90%','100%',''])
ax.grid()
filenamepng = "performance.png"
plt.savefig(filenamepng, dpi = 150)
filenameeps = "performance.eps"
plt.savefig(filenameeps)
plt.show()
if __name__ == '__main__':
"""
print("main start")
xcs = XCSProgram()
print("initialized XCSProgram")
xcs.run_experiments()
"""
xcs = XCSProgram()
xcs.pop = XCSClassifierSet(xcs.env, 0.0)
xcs.init()
xcs.env.set_state()
xcs.match_set=XCSMatchSet(pop, xcs.env, 0.0)
xcs.generate_prediction_array()
xcs.match_set = XCSMatchSet(xcs.pop, xcs.env, 0.0)
xcs.generate_prediction_array()
xcs.select_action()
xcs.action_set = XCSActionSet(xcs.match_set, xcs.action, xcs.env, 0.0)
actset = xcs.action_set.get_cls()
actset = xcs.action_set.get_cls()
actset[0].get_cond()