-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
442 lines (314 loc) · 12.3 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
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
import tensorflow as tf
import pandas as pd
import numpy as np
from sklearn import preprocessing
from matplotlib import pyplot
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from pandas import DataFrame
from pandas import concat
from tensorflow.keras.models import model_from_json
from tensorflow.keras.models import load_model
import glob
import os
from tensorflow.keras.utils import to_categorical
def load_data(path):
# data = pd.DataFrame()
# files = glob.glob(path+'/*.csv')
# dflist = []
# for file in files:
# df = pd.read_csv(file, header=None)
# dflist.append(df)
# data = pd.concat([dflist[0],dflist[1]], axis = 0)
data = pd.read_csv("./DataSet-1/dataset536.csv")
info = data.iloc[:,0:8]
states = data.iloc[:,8:9]
phases = data.iloc[:,9:13]
return info, states, phases
def convert_actions(phases):
actions = pd.DataFrame(columns=['A'])
for index, row in phases.iterrows():
if(np.array_equal([33, 33, 13, 13],row.values)):
actions.loc[index] = [0]
if(np.array_equal([33, 13, 33, 13],row.values)):
actions.loc[index] = [1]
if(np.array_equal([33, 13, 13, 33],row.values)):
actions.loc[index] = [2]
if(np.array_equal([13, 33, 33, 13],row.values)):
actions.loc[index] = [3]
if(np.array_equal([13, 33, 13, 33],row.values)):
actions.loc[index] = [4]
if(np.array_equal([13, 13, 33, 33],row.values)):
actions.loc[index] = [5]
if(np.array_equal([33, 23, 23, 13],row.values)):
actions.loc[index] = [6]
if(np.array_equal([33, 23, 13, 23],row.values)):
actions.loc[index] = [7]
if(np.array_equal([33, 13, 23, 23],row.values)):
actions.loc[index] = [8]
if(np.array_equal([23, 33, 23, 13],row.values)):
actions.loc[index] = [9]
if(np.array_equal([23, 33, 13, 23],row.values)):
actions.loc[index] = [10]
if(np.array_equal([13, 33, 23, 23],row.values)):
actions.loc[index] = [11]
if(np.array_equal([23, 23, 33, 13],row.values)):
actions.loc[index] = [12]
if(np.array_equal([23, 13, 33, 23],row.values)):
actions.loc[index] = [13]
if(np.array_equal([13, 23, 33, 23],row.values)):
actions.loc[index] = [14]
if(np.array_equal([23, 23, 13, 33],row.values)):
actions.loc[index] = [15]
if(np.array_equal([23, 13, 23, 33],row.values)):
actions.loc[index] = [16]
if(np.array_equal([13, 23, 23, 33],row.values)):
actions.loc[index] = [17]
if(np.array_equal([23, 23, 23, 23],row.values)):
actions.loc[index] = [18]
if(np.array_equal([53, 13, 13, 13],row.values)):
actions.loc[index] = [19]
if(np.array_equal([13, 53, 13, 13],row.values)):
actions.loc[index] = [20]
if(np.array_equal([13, 13, 53, 13],row.values)):
actions.loc[index] = [21]
if(np.array_equal([13, 13, 13, 53],row.values)):
actions.loc[index] = [22]
return actions
def normalize(raw_info):
min_max_scaler = preprocessing.MinMaxScaler()
info = min_max_scaler.fit_transform(raw_info)
return info
def split_data(data):
train_percent = 0.9
m = len(data)
train = data[:int(train_percent * m)]
test = data[int(train_percent * m):]
return train, test
def state_state_1_timestep():
train_X = info_train[:, :-1]
test_X = info_test[:, :-1]
train_y = states_train
test_y = states_test
train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
# design network
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# fit network
history = model.fit(train_X, train_y, epochs=50, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)
# plot history
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()
def state_state_n_timestep(info, states, timesteps):
n_features = 8
n_obs = timesteps * n_features
data = DataFrame(info)
label = DataFrame(states)
cols = []
for i in range(timesteps, 0, -1):
cols.append(data.shift(i))
for i in range(0,1):
cols.append(label.shift(-i))
data = concat(cols, axis=1)
data.dropna(inplace=True)
data = data.values
# split to train & test
train_percent = 0.9
m = len(data)
point = int(train_percent * m)
train = data[:point, :]
test = data[point:, :]
train_X = train[:, :n_obs]
test_X = test[:, :n_obs]
train_y = train[:, -1]
test_y = test[:, -1]
train_X = train_X.reshape((train_X.shape[0], timesteps, n_features))
test_X = test_X.reshape((test_X.shape[0], timesteps, n_features))
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_X, train_y, epochs=100, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()
# model = Sequential()
# model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
# train_y = to_categorical(train_y)
# test_y = to_categorical(test_y)
# model.add(Dense(50, activation='relu'))
# model.add(Dense(24, activation='softmax'))
# model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# # fit network
# model.fit(train_X, train_y, epochs=100, batch_size=72, verbose=0)
# _, accuracy = model.evaluate(test_X, test_y, batch_size=72, verbose=0)
# print(accuracy)
return model
def state_action_state_n_timestep(info, actions, states, timesteps):
n_features = 9
n_obs = timesteps * n_features
data = pd.concat([DataFrame(info), DataFrame(actions)], axis=1)
label = DataFrame(states)
cols = []
for i in range(timesteps, 0, -1):
cols.append(data.shift(i))
for i in range(0,1):
cols.append(label.shift(-i))
data = concat(cols, axis=1)
data.dropna(inplace=True)
data = data.values
# split to train & test
train_percent = 0.9
m = len(data)
point = int(train_percent * m)
train = data[:point, :]
test = data[point:, :]
train_X = train[:, :n_obs]
test_X = test[:, :n_obs]
train_y = train[:, -1]
test_y = test[:, -1]
train_X = train_X.reshape((train_X.shape[0], timesteps, n_features))
test_X = test_X.reshape((test_X.shape[0], timesteps, n_features))
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_X, train_y, epochs=100, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()
# model = Sequential()
# model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
# train_y = to_categorical(train_y)
# test_y = to_categorical(test_y)
# model.add(Dense(50, activation='relu'))
# model.add(Dense(24, activation='softmax'))
# model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# # fit network
# history =model.fit(train_X, train_y, epochs=70, batch_size=72, verbose=0)
# _, accuracy = model.evaluate(test_X, test_y, batch_size=72, verbose=0)
# print(accuracy)
return model
def state_action_state_action_n_timestep(info, actions, states, timesteps):
n_features = 9
n_obs = timesteps * n_features
data = pd.concat([DataFrame(info), DataFrame(actions)], axis=1)
label = pd.concat([DataFrame(states), DataFrame(actions)], axis=1)
cols = []
for i in range(timesteps, 0, -1):
cols.append(data.shift(i))
for i in range(0,1):
cols.append(label.shift(-i))
data = concat(cols, axis=1)
data.dropna(inplace=True)
data = data.values
# split to train & test
train_percent = 0.9
m = len(data)
point = int(train_percent * m)
train = data[:point, :]
test = data[point:, :]
train_X = train[:, :n_obs]
test_X = test[:, :n_obs]
train_y = train[:, -2:]
test_y = test[:, -2:]
train_X = train_X.reshape((train_X.shape[0], timesteps, n_features))
test_X = test_X.reshape((test_X.shape[0], timesteps, n_features))
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(2))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_X, train_y, epochs=100, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()
return model
def save_model(model, name):
model_json = model.to_json()
with open(name + ".json", "w") as json_file:
json_file.write(model_json)
model.save_weights(name + ".h5")
return
def load_saved_model(name):
# load json and create model
json_file = open(name+'.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(name + ".h5")
return load_model
raw_info, states, phases = load_data(path='./DataSet-1')
actions = convert_actions(phases)
info = normalize(raw_info)
model_num = 3
timesteps = 5
# save models
if(model_num == 1):
model = state_state_n_timestep(info, states, timesteps=timesteps)
save_model(model, name='state_state')
if(model_num == 2):
model = state_action_state_n_timestep(info, actions, states, timesteps=timesteps)
save_model(model, name='state_action_state')
if(model_num == 3):
model = state_action_state_action_n_timestep(info, actions, states, timesteps=timesteps)
save_model(model, name='state_action_state_action')
# run models on test data
raw_info, states, phases = load_data(path='./DataSet-2')
actions = convert_actions(phases)
info = normalize(raw_info)
if(model_num == 1):
n_features = 8
n_obs = timesteps * n_features
data = DataFrame(info)
cols = []
for i in range(timesteps, 0, -1):
cols.append(data.shift(i))
data = concat(cols, axis=1)
data.dropna(inplace=True)
data = data.values
test_X = data[:, :n_obs]
test_X = test_X.reshape((test_X.shape[0], timesteps, n_features))
loaded_model = load_saved_model(name='state_state')
yhat = model.predict(test_X)
print(yhat)
np.savetxt("state_state.csv", yhat, delimiter=",")
if(model_num == 2):
n_features = 9
n_obs = timesteps * n_features
data = pd.concat([DataFrame(info), DataFrame(actions)], axis=1)
cols = []
for i in range(timesteps, 0, -1):
cols.append(data.shift(i))
data = concat(cols, axis=1)
data.dropna(inplace=True)
data = data.values
test_X = data[:, :n_obs]
test_X = test_X.reshape((test_X.shape[0], timesteps, n_features))
loaded_model = load_saved_model(name='state_action_state')
yhat = model.predict(test_X)
np.savetxt("state_action_state.csv", yhat, delimiter=",")
if(model_num == 3):
n_features = 9
n_obs = timesteps * n_features
data = pd.concat([DataFrame(info), DataFrame(actions)], axis=1)
cols = []
for i in range(timesteps, 0, -1):
cols.append(data.shift(i))
data = concat(cols, axis=1)
data.dropna(inplace=True)
data = data.values
test_X = data[:, :n_obs]
test_X = test_X.reshape((test_X.shape[0], timesteps, n_features))
loaded_model = load_saved_model(name='state_action_state_action')
yhat = model.predict(test_X)
np.savetxt("state_action_state_action.csv", yhat, delimiter=",")