-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneticAlgorithm.py
189 lines (147 loc) · 5.96 KB
/
geneticAlgorithm.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
import pandas as pd
import numpy as np
import random
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import random
from matplotlib import pyplot as plt
########## Functions ##########
def initializePopulation(size, genes):
population = []
for i in range(size):
chromosome = np.random.randint(2, size=genes)
population.append(chromosome)
return population
########## ANN ##########
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(784, 397) #input
self.fc2 = nn.Linear(397, 204)
self.fc3 = nn.Linear(204, 10)
self.relu = nn.LeakyReLU(0.2) #hidden layer activation function
self.softmax = nn.Softmax(dim=1) #output layer activation function
def forward(self, input):
x = self.relu(self.fc1(input)) # hidden layer
x = self.relu(self.fc2(x)) # hidden layer
return self.softmax(self.fc3(x)) # output layer
# Custom Dataset function
class myDataset(torch.utils.data.Dataset):
def __init__(self, data, labels):
self.data = data
self.labels = labels
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
data = self.data[idx]
labels = self.labels[idx]
sample = {'data' : data, 'labels' : labels}
return sample
#read testdata
testData = pd.read_csv('./mnist_test.csv')
x = testData.to_numpy()
labels = x[:,0]
data = x[:,1:]
data = data/255
test_dataset = myDataset(data, labels)
test_dataloader = DataLoader(test_dataset, batch_size=64, num_workers= 0)
#initialize network with saved weights from bestANN.pth
net = Net().double()
net.load_state_dict(torch.load('./bestANN.pth'))
net.eval()
chromosomes = 200
genes = 784
num_inputs = 450
mean_best_fit = 0
mean_epoch = 0
mean_plot = []
for loop in range(10):
#buffer to terminate if there isn't progress for 10 generations - save time
buffer = [0 for i in range(10)]
temp_plot = []
population = initializePopulation(chromosomes, genes)
for epoch in range(100):
test_total = np.zeros(chromosomes)
test_correct = np.zeros(chromosomes)
avg_test_accuracy = np.zeros(chromosomes)
for j, data1 in enumerate(test_dataloader):
dedomena = data1['data'].double()
etiketes = data1['labels'].double()
for k, chromosome in enumerate(population):
chromosome_output = net(dedomena * chromosome)
for idx, i in enumerate(chromosome_output):
if torch.argmax(i) == etiketes[idx]:
test_correct[k]+= 1
test_total[k] += 1
#punish accuracy when having large number of inputs
if np.count_nonzero(chromosome == 1) > num_inputs:
avg_test_accuracy=90*(test_correct/test_total)
else:
avg_test_accuracy=100*(test_correct/test_total)
temp_plot.append(avg_test_accuracy.max())
if avg_test_accuracy.max() == buffer[0]:
break
buffer.append(avg_test_accuracy.max())
buffer.pop(0)
population = list(np.array(population)[np.argsort(avg_test_accuracy)])
print('Loop: '+str(loop)+' Epoch: '+str(epoch)+' Best accuracy: '+str(avg_test_accuracy.max()))
#selection
p_chromosome = avg_test_accuracy / avg_test_accuracy.sum()
q_chromosome = [p_chromosome[0:i].sum() for i in range(1,chromosomes+1)]
r = [random.uniform(0, 1) for i in range(chromosomes)]
temp_pop = []
for i in range(chromosomes):
if r[i] <= q_chromosome[i]:
temp_pop.append(population[i])
population = []
_ = [population.append(i) for i in temp_pop]
while len(population) < chromosomes:
population.extend(population)
population = population[0:chromosomes]
#crossover
pc = 0.9
mating_pool = []
rest_pop = []
r = [random.uniform(0, 1) for i in range(chromosomes)]
for i in range(chromosomes):
if r[i] < pc:
mating_pool.append(population[i])
else:
rest_pop.append(population[i])
for i in range(0,len(mating_pool),2):
if i+1 == len(mating_pool): continue
k = random.randrange(1,genes)
a = np.concatenate((mating_pool[i][0:k],mating_pool[i+1][k:]),axis = 0)
b = np.concatenate((mating_pool[i+1][0:k],mating_pool[i][k:]),axis = 0)
mating_pool[i] = a
mating_pool[i+1] = b
population = []
_ = [population.append(i) for i in mating_pool]
population.extend(rest_pop)
#mutation
pm = 0.01
for i in range(chromosomes):
for j in range(genes):
if np.random.uniform(0,1)< pm:
population[i][j] = population[i][j]^1
mean_plot.append(temp_plot)
mean_epoch += epoch
mean_best_fit += avg_test_accuracy.max()
mean_best_fit /= 10
mean_epoch /= 10
#plots
plt.figure()
plt.title('Mean Best Accuracy Plot for case 9')
plt.ylabel('Mean Accuracy')
plt.xlabel('Generations')
#for plot - number of generations is different in each loop
max_len = 0
for i in range(10):
max_len = max([max_len, len(mean_plot[i])])
plot_array = np.zeros((10,max_len))
for i in range(10):
plot_array[i,0:len(mean_plot[i])] = np.array(mean_plot[i])
plt.plot(plot_array.sum(0)/np.count_nonzero(plot_array,axis=0))