-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvolutionary_algo.py
135 lines (108 loc) · 4.1 KB
/
Evolutionary_algo.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
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import TensorBoard
import numpy as np
import os
import random
#Basic keras model for doing deep learning
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=(176, 200, 3),
activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))
model.add(Conv2D(64, (3, 3), padding='same',
activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))
model.add(Conv2D(128, (3, 3), padding='same',
activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))
learning_rate = 0.0001
opt = keras.optimizers.adam(lr=learning_rate, decay=1e-6)
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
tensorboard = TensorBoard(log_dir="logs/Version_1.0.0")
directory = "train_data"
#iterates over all of the decision lists and return their lengths
def return_data_length():
choices = {"no_attacks": no_attacks,
"attack_closest_to_nexus": attack_closest_to_nexus,
"attack_enemy_structures": attack_enemy_structures,
"attack_enemy_start": attack_enemy_start}
total_data = 0
lengths = []
for choice in choices:
total_data += len(choices[choice])
lengths.append(len(choices[choice]))
return lengths
epochs = 10
for i in range(epochs):
current = 0
incrementSize = 200
not_max = True
files = os.listdir(dir)
maximum = len(all_files)
#We wanna shuffle files, so our batches are random
random.shuffle(files)
while not maximum:
#each array represents the target of an attack
idle = []
units = []
structures = []
nexus = []
for file in files[current:current + increment]:
path = os.path.join(train_data,file)
data_set = np.load(path)
data_set = list(data_set)
for data in data_set:
decision = np.argmax(data[0])
if decision == 0:
idle.append(data)
elif decision == 1:
units.append(data)
elif decision == 2:
structures.append(data)
elif decision == 3:
nexus.append(data)
lengths = return_data_length()
min_length = min(lengths)
# slice all the data to be the same size
random.shuffle(idle)
random.shuffle(units)
random.shuffle(structures)
random.shuffle(nexus)
idle = idle[:min_length]
units = units[:min_length]
structures = structures[:min_length]
nexus = nexus[:min_length]
train_data = no_attacks + attack_closest_to_nexus + attack_enemy_structures + attack_enemy_start
random.shuffle(train_data)
test_size = 100
batch_size = 128
#Setup our train data and test data, then pass it into our model
x_train = np.array([i[1] for i in train_data[:-test_size]]).reshape(-1, 176, 200, 3)
y_train = np.array([i[0] for i in train_data[:-test_size]])
x_test = np.array([i[1] for i in train_data[-test_size:]]).reshape(-1, 176, 200, 3)
y_test = np.array([i[0] for i in train_data[-test_size:]])
model.fit(x_train, y_train,
batch_size=batch_size,
validation_data=(x_test, y_test),
shuffle=True,
verbose=1, callbacks=[tensorboard])
#save the model
model.save("BasicCNN-{}-epochs-{}-LR-STAGE1".format(hm_epochs, learning_rate))
current += increment
if current > maximum:
not_maximum = False