-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbSCA.py
158 lines (125 loc) · 5.67 KB
/
AbSCA.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
'''
The code style has been adopted from the following repository:
https://github.com/CMATER-JUCS/Py_FS
'''
import os
import sys
import random
import math
import time
import numpy as np
import pandas as pd
import matplotlib as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score
from sklearn.metrics import classification_report
from sklearn.metrics import plot_confusion_matrix
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
from sklearn.svm import SVC as SVM
from utils import feature_selection
from utils.feature_selection import *
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
def AbSCA(num_agents, max_iter, train_data, train_label, obj_function=compute_fitness, trans_func_shape='s'):
short_name = 'SCA'
agent_name = 'Agent'
train_data, train_label = np.array(train_data), np.array(train_label)
num_features = train_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 is just compute_fitness with accuracy weight as 1
compute_accuracy = (compute_fitness, 1)
# initialize agents and Leader (the agent with the max fitness)
population = 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 data class
data = Data()
data.train_X, data.val_X, data.train_Y, data.val_Y = train_test_split(
train_data, train_label, shuffle=False, test_size=0.2)
# 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 population
population, fitness, accs = sort_agents(population, obj, data)
Leader_agent = population[0].copy()
Leader_fitness = fitness[0].copy()
# start timer
start_time = time.time()
# Eq. (3.4)
a = 3
for iter_no in range(max_iter):
print('\n================================================================================')
print(' Iteration - {}'.format(iter_no+1))
print('================================================================================\n')
# Eq. (3.4)
r1 = a-iter_no*((a)/max_iter) # r1 decreases linearly from a to 0
# update the Position of search agents
for i in range(num_agents):
for j in range(num_features):
# update r2, r3, and r4 for Eq. (3.3)
r2 = (2 * np.pi) * np.random.random()
r3 = 2 * np.random.random()
r4 = np.random.random()
# Eq. (3.3)
if r4 < 0.5:
# Eq. (3.1)
population[i][j] = population[i][j] + (r1 * np.sin(r2) * abs(r3 * Leader_agent[j] - population[i][j]))
else:
# Eq. (3.2)
population[i][j] = population[i][j] + (r1 * np.cos(r2) * abs(r3 * Leader_agent[j] - population[i][j]))
temp = population[i][j].copy()
temp = trans_function(temp)
if temp > np.random.random():
population[i][j] = 1
else:
population[i][j] = 0
# local search on every agent
print(f'\n******** Local Search on Agent {i+1} of Iteration {iter_no+1} ********\n')
agent = population[i].copy()
agentFit, agentAcc = compute_fitness(agent, data.train_X, data.val_X, data.train_Y, data.val_Y, weight_acc=0.99)
print(f'Initial fitness = {agentFit} | Initial accuracy = {agentAcc} | Nos of features = {int(np.sum(agent))}\n')
final_agent = adaptivebetaHC(agent, agentFit, agentAcc, data.train_X, data.val_X, data.train_Y, data.val_Y)
population[i] = final_agent.copy()
# update final information
population, fitness, accs = sort_agents(population, obj, data)
display(population, fitness, accs, agent_name)
if fitness[0] > Leader_fitness:
Leader_agent = population[0].copy()
Leader_fitness = fitness[0].copy()
# compute final accuracy
Leader_agent, _, Leader_accuracy = sort_agents(Leader_agent, compute_accuracy, data)
population, _, accuracy = sort_agents(population, compute_accuracy, data)
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
# update attributes of solution
solution.best_agent = Leader_agent
solution.best_fitness = Leader_fitness
solution.best_accuracy = Leader_accuracy
solution.final_particles = population
solution.final_fitness = fitness
solution.final_accuracy = accuracy
solution.execution_time = exec_time
return solution