-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_search.py
68 lines (51 loc) · 1.82 KB
/
local_search.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
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')
def getNeighbor(agent):
neighbor = agent.copy()
dim = agent.shape[0]
percent = 0.4
limit = int(percent*dim)
if limit <= 1 or limit > dim:
limit = dim
x = random.randint(1, limit)
pos = random.sample(range(0, dim-1), x)
for i in pos:
neighbor[i] = 1 - neighbor[i]
return neighbor
def adaptivebetaHC(agent, agentFit, agentAcc, trainX, testX, trainy, testy):
bmin = 0.001
bmax = 0.01
max_iter = 200
for itr in range(max_iter):
neighbor = agent.copy()
dim = agent.shape[0]
neighbor = getNeighbor(neighbor)
beta = bmin + (itr / max_iter) * (bmax - bmin)
for i in range(dim):
random.seed(time.time()+i)
if random.random() <= beta:
neighbor[i] = agent[i]
neighFit, neighAcc = compute_fitness(neighbor, trainX, testX, trainy, testy, weight_acc=0.99)
if neighFit > agentFit or neighAcc > agentAcc:
agent = neighbor.copy()
agentFit = neighFit
agentAcc = neighAcc
print(f'iteration {itr+1} | Fitness = {neighFit} | Accuracy = {neighAcc} | Nos of features = [{int(np.sum(agent))}/{agent.shape[0]}]')
return agent