-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbee.py
92 lines (77 loc) · 3.55 KB
/
bee.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
import random
import numpy as np
from solution import Solution
class Bee :
def __init__(self,id,problem,locIterations,state):
self.id=id
self.data=problem
self.solution = Solution(self.data,state=state)
self.fitness= 0.0
self.reward = 0.0
self.locIterations=locIterations
self.action = []
def localSearch(self):
best=self.fitness
#done=False
lista=[j for j, n in enumerate(self.solution.state) if n == 1]
indice =lista[0]
for itr in range(self.locIterations):
while(True):
pos=-1
oldFitness=self.fitness
for i in range(len(self.solution.state)):
if ((len(lista)==1) and (indice==i) and (i < self.data.nb_attribs-1)):
i+=1
self.solution.state[i]= (self.solution.state[i] + 1) % 2
quality = self.solution.get_accuracy(self.solution.get_state())
if (quality >best):
pos = i
best=quality
self.solution.state[i]= (self.solution.state[i]+1) % 2
self.fitness = oldFitness
if (pos != -1):
self.solution.state[pos]= (self.solution.state[pos]+1)%2
self.fitness = best
else:
break
for i in range(len(self.solution.state)):
oldFitness=self.fitness
if ((len(lista)==1) and (indice==i) and (i < self.data.nb_attribs-1)):
i+=1
self.solution.state[i]= (self.solution.state[i] + 1) % 2
quality = self.solution.get_accuracy(self.solution.get_state())
if (quality<best):
self.solution.state[i]= (self.solution.state[i] + 1) % 2
self.fitness = oldFitness
def ql_localSearch(self,maxIterIndex):
iterations = int(self.locIterations/maxIterIndex) if self.locIterations >= maxIterIndex else 1
for itr in range(iterations):
state = self.solution.get_state()
next_state, action = self.data.ql.step(self.data,self.solution)
next_sol = Solution(self.data,state=next_state)
acc_state = self.solution.get_accuracy(state)
acc_new_state = self.solution.get_accuracy(next_state)
if (acc_state < acc_new_state):
reward = acc_new_state
elif (acc_state > acc_new_state):
reward = acc_new_state - acc_state
else :
if (self.data.nbrUn(state) > self.data.nbrUn(next_state) ):
reward = 0.5 * acc_new_state
else :
reward = -0.5 * acc_new_state
self.fitness = self.data.ql.get_q_value(self.data,self.solution,action)
self.reward = self.solution.get_accuracy(next_state)
self.data.ql.learn(self.data,self.solution,action,reward,next_sol)
self.solution = next_sol
def setSolution(self,solution):
self.solution.set_state(solution)
if (self.data.typeOfAlgo == 0) :
self.fitness = self.solution.get_accuracy(solution)
elif (self.data.typeOfAlgo == 1):
self.reward = self.solution.get_accuracy(solution)
@classmethod
def Rand(self,num):
res = []
res = np.random.choice([0,1],size=(num,),p=[2./10,8./10]).tolist()
return res