-
Notifications
You must be signed in to change notification settings - Fork 2
/
PSO.py
183 lines (148 loc) · 7.39 KB
/
PSO.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
import os
import sys
import random
import math
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import auc, precision_score, classification_report, plot_confusion_matrix, f1_score, confusion_matrix
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.svm import SVC as SVM
import utils
from utils.feature_selection import *
from utils.solution import *
import warnings
warnings.filterwarnings('ignore')
# function for PSO Algorithm to perfom FS on a given dataset
def PSO(num_agents, max_iter, data, label, obj_function=compute_fitness, trans_func_shape='s', save_conv_graph=False):
# Particle Swarm Optimizer
############################### Parameters ####################################
# #
# num_agents: number of particles #
# max_iter: maximum number of generations #
# data: feature set of data #
# label: class labels for the samples #
# obj_function: the function to maximize while doing feature selection #
# trans_function_shape: shape of the transfer function used #
# save_conv_graph: boolean value for saving convergence graph #
# #
###############################################################################
short_name = 'PSO'
agent_name = 'Particle'
data, label = np.array(data), np.array(label)
num_features = data.shape[1]
trans_function = get_trans_function('trans_func_shape')
# setting up the objectives
weight_acc = None
if(obj_function==compute_fitness):
weight_acc = 0.99
obj = (obj_function, weight_acc)
compute_accuracy = (compute_fitness, 1) # compute_accuracy is just compute_fitness with accuracy weight as 1
# initialize particles and Leader (the agent with the max fitness)
particles = initialize(num_agents, num_features)
fitness = np.zeros(num_agents)
accuracy = np.zeros(num_agents)
Leader_agent = np.zeros((1, num_features))
Leader_fitness = float("-inf")
Leader_accuracy = float("-inf")
# initialize convergence curves
convergence_curve = {}
convergence_curve['fitness'] = np.zeros(max_iter)
convergence_curve['feature_count'] = np.zeros(max_iter)
# initialize Data class
item = Data()
item.train_X, item.val_X, item.train_Y, item.val_Y = train_test_split(data, label, test_size=0.2, shuffle=False)
# create a solution object
solution = Solution()
solution.num_agents = num_agents
solution.max_iter = max_iter
solution.num_features = num_features
solution.obj_function = obj_function
# rank initial particles
particles, fitness, accs = sort_agents(particles, obj, item)
# start timer
start_time = time.time()
# initialize global and local best particles
globalBestParticle = [0 for i in range(num_features)]
globalBestFitness = float("-inf")
localBestParticle = [ [ 0 for i in range(num_features) ] for j in range(num_agents) ]
localBestFitness = [float("-inf") for i in range(num_agents) ]
weight = 1.0
velocity = [ [ 0.0 for i in range(num_features) ] for j in range(num_agents) ]
for iter_no in range(max_iter):
print('\n================================================================================')
print(' Iteration - {}'.format(iter_no+1))
print('================================================================================\n')
# update weight
weight = 1.0 - (iter_no / max_iter)
# update the velocity
for i in range(num_agents):
for j in range(num_features):
velocity[i][j] = (weight*velocity[i][j])
r1, r2 = np.random.random(2)
velocity[i][j] = velocity[i][j] + (r1 * (localBestParticle[i][j] - particles[i][j]))
velocity[i][j] = velocity[i][j] + (r2 * (globalBestParticle[j] - particles[i][j]))
# updating position of particles
for i in range(num_agents):
for j in range(num_features):
trans_value = trans_function(velocity[i][j])
if (np.random.random() < trans_value):
particles[i][j] = 1
else:
particles[i][j] = 0
# updating fitness of particles
particles, fitness, accs = sort_agents(particles, obj, item)
display(particles, fitness, accs, agent_name)
# updating the global best and local best particles
for i in range(num_agents):
if fitness[i]>localBestFitness[i]:
localBestFitness[i]=fitness[i]
localBestParticle[i]=particles[i][:]
if fitness[i]>globalBestFitness:
globalBestFitness=fitness[i]
globalBestParticle=particles[i][:]
# update Leader (best agent)
if globalBestFitness > Leader_fitness:
Leader_agent = globalBestParticle.copy()
Leader_fitness = globalBestFitness.copy()
convergence_curve['fitness'][iter_no] = Leader_fitness
convergence_curve['feature_count'][iter_no] = int(np.sum(Leader_agent))
# compute final accuracy
Leader_agent, _, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, item)
particles, _, accuracy = sort_agents(particles, compute_accuracy, item)
print('\n================================================================================')
print(' Final Result ')
print('================================================================================\n')
print('Leader ' + agent_name + ' Dimension : {}'.format(int(np.sum(Leader_agent))))
print('Leader ' + agent_name + ' Fitness : {}'.format(Leader_fitness))
print('Leader ' + agent_name + ' Classification Accuracy : {}'.format(Leader_accuracy))
print('\n================================================================================\n')
# stop timer
end_time = time.time()
exec_time = end_time - start_time
# plot convergence curves
iters = np.arange(max_iter)+1
fig, axes = plt.subplots(2, 1)
fig.tight_layout(pad=5)
fig.suptitle('Convergence Curves')
axes[0].set_title('Convergence of Fitness over Iterations')
axes[0].set_xlabel('Iteration')
axes[0].set_ylabel('Fitness')
axes[0].plot(iters, convergence_curve['fitness'])
axes[1].set_title('Convergence of Feature Count over Iterations')
axes[1].set_xlabel('Iteration')
axes[1].set_ylabel('Number of Selected Features')
axes[1].plot(iters, convergence_curve['feature_count'])
plt.show()
# update attributes of solution
solution.best_agent = Leader_agent
solution.best_fitness = Leader_fitness
solution.best_accuracy = Leader_accuracy
solution.convergence_curve = convergence_curve
solution.final_particles = particles
solution.final_fitness = fitness
solution.final_accuracy = accuracy
solution.execution_time = exec_time
return solution, convergence_curve