-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
281 lines (229 loc) · 9 KB
/
main.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
273
274
275
276
277
278
279
280
281
import os
import random
import cPickle
import numpy as np
import sys
from sklearn.decomposition import PCA
from sklearn.svm import SVC, NuSVC
from sklearn.cross_validation import train_test_split, KFold
from sklearn.naive_bayes import GaussianNB, MultinomialNB, BernoulliNB
from metric_learn import LMNN
from sklearn.neighbors import KNeighborsClassifier, DistanceMetric
def parse(filename, outputfilename, keywords, **kwargs):
# parse('./data/blocks-lib', './parsed.arff',
# keywords=['(ON', '(STACK', '(UNSTACK', '(PICK-UP'],
# RELATION = 'blocks-world')
states_set = set([])
with open(filename, 'r') as f:
stage = None
for line in f:
if line.strip().startswith('(:'):
stage = line.strip().split('(:')[-1]
for keyword in keywords:
if line.strip().startswith(keyword):
state_name = '{}:{}:{}'.format(stage, keyword, line.strip()).replace(' ', '-')
states_set.update([state_name])
states_list = list(states_set)
output = open(outputfilename, 'w')
output.write('@RELATION {}\n'.format(kwargs.get('RELATION', 'UNKNOWN')))
for state in states_set:
output.write('@ATTRIBUTE {} {{1, 0}}\n'.format(state))
output.write('@ATTRIBUTE SUCCESS {{1, 0}}\n'.format(state))
output.write('@DATA\n'.format(state))
features = []
feature = []
with open(filename, 'r') as f:
stage = None
for line in f:
if line.strip().startswith('(:'):
stage = line.strip().split('(:')[-1]
if stage == 'trace':
if feature and max(feature) != 0:
# positive example
features.append(feature+[1])
# error example
for _ in xrange(3):
r = random.randint(0, len(states_set))
feature[r] = 1-feature[r]
features.append(feature+[0])
feature = [0]* len(states_set)
# print line
for keyword in keywords:
if line.strip().startswith(keyword):
state_name = '{}:{}:{}'.format(stage, keyword, line.strip()).replace(' ', '-')
feature[states_list.index(state_name)] = 1
print states_list.index(state_name)
for feature in features:
output.write(','.join(map(str, feature)))
output.write(',1\n')
def change(filename, output, p):
ifile = open(filename, 'r')
ofile = open(output, 'w')
for line in ifile:
if any(map(lambda x: x in line, ['define', 'trace', 'init', 'plan', 'goal'])):
ofile.write(line)
elif random.random() >= p:
ofile.write(line)
else:
continue
# delete pass to change "deletion" to "shuffling"
components = line.replace('(', '').replace(')', '').strip().split(' ')
# print components
action = components.pop(0)
np.random.shuffle(components)
components = [action] + components
# print components
ofile.write(' '.join(components))
ifile.close()
ofile.close()
print 'changed'
return
def parser2(filename):
articles = []
words = []
with open(filename, 'r') as f:
for line in f:
line = line.strip()
if 'trace' in line:
if words: articles.append(words)
words = []
elif 'define' in line or line.startswith(')'):
continue
else:
line = line.replace('(', '').replace(')', '').strip()
if not line: continue
line = line.split(' ')
words.extend(line)
if words: articles.append(words)
return articles
def NLP_Code(X, K=1):
d = set([])
newx = []
for x in X:
neww = []
for ind in range(len(x)):
if ind+K-1 < len(x):
concatenated = '+'.join(map(str, x[ind:ind+K]))
neww.append(concatenated)
newx.append(neww)
d.update(neww)
d = list(d)
arr = np.zeros(( len(newx), len(d) ))
for ind, article in enumerate(newx):
for word in article:
arr[ind][d.index(word)] += 1
return arr, d
def store2arff(X, Y, filename, dictionary, **kwargs):
output = open(filename, 'w')
output.write('@RELATION {}\n'.format(kwargs.get('RELATION', 'UNKNOWN')))
for word in dictionary:
output.write('@ATTRIBUTE {} NUMERIC\n'.format(word))
output.write('@ATTRIBUTE SUCCESS {1, 0}\n')
output.write('@DATA\n')
for x, y in zip(X, Y):
# output.write('{},{}\n'.format(','.join(map(lambda _: '1' if _>0 else '0', x)), y))
output.write('{},{}\n'.format(','.join(map(str, x)), y))
output.close()
print '{} is ready ...'.format(filename)
def pca(x, variance_ratio=0.90):
pca = PCA()
pca.fit(x)
acc = 0.0
for ind, ele in enumerate(pca.explained_variance_ratio_):
if acc > variance_ratio: break
acc += ele
ind += 1
pca.set_params(n_components=ind)
x = pca.fit_transform(x)
return x, pca
def knn(train_x, train_y, test_x, test_y, K=5):
neigh = KNeighborsClassifier(n_neighbors=K)
neigh.fit(train_x, train_y)
acc = (neigh.predict(test_x) == test_y).sum()
return float(acc)/test_y.shape[0]
if __name__ == '__main__':
try:
ind = sys.argv.index('-max')
MAX = int(sys.argv[ind+1])
except:
print 'cannot find max argument'
print 'set MAX as 400'
MAX = 400
def lab(pfile, nfile, p):
change(pfile, nfile, p)
return parser2(pfile), parser2(nfile)
positive_x, negative_x = lab('./data/blocks-lib', './data/blocks-negative', p=0.40)
# positive_x, negative_x = lab('./data/depots-lib', './data/depots-negative', p=0.20)
# positive_x, negative_x = lab('./data/driverlog-lib', './data/driverlog-negative', p=0.20)
# features
# L1_x, dictionary = NLP_Code(positive_x+negative_x, K=1)
# L2_x, dictionary = NLP_Code(positive_x+negative_x, K=2)
# L4_x, dictionary = NLP_Code(positive_x+negative_x, K=4)
# L2_x, _ = pca(L2_x, 0.99)
# L4_x, _ = pca(L4_x, 0.99)
xs = []
for l in ['-1', '-2','-3','-4']:
if l in sys.argv:
l = int(l[1])
_x, dictionary = NLP_Code(positive_x+negative_x, K=l)
_x, _ = pca(_x, 0.99)
xs.append(_x)
print 'Adding level-{} data'.format(l)
x = np.concatenate(xs, 1)
y = np.array(len(positive_x)*[1] + len(negative_x)*[0] )
print 'x.shape={} y.shape={}'.format(x.shape, y.shape)
index = np.random.permutation(len(x))
x = x[index]
y = y[index]
# x -= x.min(1).reshape(-1, 1)
# x /= x.max(1).reshape(-1, 1)
# truncate
x = x[:MAX, :]
y = y[:MAX]
print 'MAX={}'.format(MAX)
sys.stdout.flush()
# training
svm = NuSVC(kernel='linear') # linear, poly, rbf, NuSVC
lmnn = LMNN(k=5, learn_rate=1e-7, max_iter=400)
gnb = GaussianNB()
mnb = MultinomialNB(alpha=0.0)
bnb = BernoulliNB(alpha=0.0)
svmrec = []
lmnnrec = []
gnbrec = []
mnbrec = []
bnbrec = []
for train_index, test_index in KFold(len(x), n_folds=10, shuffle=True):
train_x, test_x = x[train_index], x[test_index]
train_y, test_y = y[train_index], y[test_index]
gnb.fit(train_x, train_y)
gnbrec.append( float((gnb.predict(test_x) == test_y).sum())/ len(test_y) )
nonneg_train_x = train_x - train_x.min()
nonneg_test_x = test_x - test_x.min()
mnb.fit(nonneg_train_x, train_y)
mnbrec.append( float((mnb.predict(nonneg_test_x) == test_y).sum())/ len(test_y) )
bnb.fit(train_x, train_y)
bnbrec.append( float((bnb.predict(test_x) == test_y).sum())/ len(test_y) )
svm.fit(train_x, train_y)
svmrec.append( float((svm.predict(test_x) == test_y).sum())/ len(test_y) )
_ = PCA(n_components=20).fit(train_x)
train_x = _.transform(train_x)
test_x = _.transform(test_x)
print train_x.shape
L = lmnn.fit(train_x, train_y, verbose=True).L
lmnnrec.append( knn(np.dot(train_x, L), train_y, np.dot(test_x, L), test_y, K=5) )
print '\tSVM accuracy: {} = {}'.format(svmrec, np.mean(svmrec))
print '\tLMNN accuracy: {} = {}'.format(lmnnrec, np.mean(lmnnrec))
print '\tGaussianNB accuracy: {} = {}'.format(gnbrec, np.mean(gnbrec))
print '\tMultinomiaNB accuracy: {} = {}'.format(mnbrec, np.mean(mnbrec))
print '\tBernoulliNB accuracy: {} = {}'.format(bnbrec, np.mean(bnbrec))
# lmnnavr.append(np.mean(lmnnrec))
# gnbavr.append(np.mean(gnbrec))
# svmavr.append(np.mean(svmrec))
# svmavr = []
# lmnnavr = []
# gnbavr = []
# print 'GuassianNB final accuracy: {}'.format(np.mean(gnbavr))
# print 'SVM final accuracy: {}'.format(np.mean(svmavr))
# print 'LMNN final accuracy: {}'.format(np.mean(lmnnavr))
# store2arff(x, y, 'parsed{}.arff'.format(MAX), range(x.shape[1]))