-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDNPIO.py
278 lines (245 loc) · 10.2 KB
/
DNPIO.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import random
from .Population import Pops
from .BasePIO import BasePIO
import numpy as np
import scipy.special as sc_special
import time
import math
import copy
class DNPIO_Latin(BasePIO):
'''
消融实验-缺拉丁超立方初始化
'''
def __init__(self, params, **kwargs):
'''
:param kwargs: dict
such as:
{
"popSize" : 40
"vardim": 50
"bound" : [[l_1,l_2,...,l_n],[u_1,u_2,...,u_n]] # and n=vardim
"Maxiter" : 1000
"step" : in map factor, 60
"k" : in map factor, 4
"g" : guess step in landmark factor
"init_type" : 0 for oringin、1 for latin
"personal_type": True with Personal_Best updated
"func" : function of calculating fitness
"M" : matrix of input,E
"S" : shuffle of input,0
}
'''
self.popSize, self.vardim, self.bound = 40, 50, np.tile([[-100], [100]], 50)
self.func, self.M, self.S = None, np.eye(self.vardim),np.zeros(self.vardim)
self.init_type, self.personal_type = 0, False
self.Maxiter = 1000
self.step = 60
self.k = 16
self.c = 0.5
self.percent = 0.8
self.__set_keyword_arguments(params)
self.__set_keyword_arguments(kwargs)
self.Nc1 = int(self.percent * self.Maxiter)
self.Nc2 = self.Maxiter - self.Nc1
self.Pops = Pops(params=params,personal_type=True,init_type=1)
self.half = self.popSize
#print("xxx==",self.Pops.fitness)
def __set_keyword_arguments(self, kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
class DNPIO_Bound(BasePIO):
'''
消融实验-边界条件
'''
def __init__(self, params, **kwargs):
'''
:param kwargs: dict
such as:
{
"popSize" : 40
"vardim": 50
"bound" : [[l_1,l_2,...,l_n],[u_1,u_2,...,u_n]] # and n=vardim
"Maxiter" : 1000
"step" : in map factor, 60
"k" : in map factor, 4
"g" : guess step in landmark factor
"init_type" : 0 for oringin、1 for latin
"personal_type": True with Personal_Best updated
"func" : function of calculating fitness
"M" : matrix of input,E
"S" : shuffle of input,0
}
'''
self.popSize, self.vardim, self.bound = 40, 50, np.tile([[-100], [100]], 50)
self.func, self.M, self.S = None, np.eye(self.vardim),np.zeros(self.vardim)
self.init_type, self.personal_type = 0, False
self.Maxiter = 1000
self.step = 60
self.k = 16
self.c = 0.5
self.percent = 0.8
self.__set_keyword_arguments(params)
self.__set_keyword_arguments(kwargs)
self.Nc1 = int(self.percent * self.Maxiter)
self.Nc2 = self.Maxiter - self.Nc1
self.Pops = Pops(params=params,personal_type=True,init_type=0)
self.half = self.popSize
#print("xxx==",self.Pops.fitness)
def __set_keyword_arguments(self, kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def solve(self):
print("***{}***".format(self.__class__.__name__))
st = time.time()
#self.Pops.absorb_p = 0.5
for t in range(self.Nc1): #指南针算子
self.move_part_one(t) #更新种群位置
self.Pops.updated_2() #更新种群状态
# self.updated_worst()
print("Part.{}, fitness = {}".format(t,self.Pops.global_best_fitness))
for t in range(self.Nc2):
self.move_part_two(t)
self.Pops.updated_2()
# self.updated_worst()
class DNPIO_warpigeon1(BasePIO):
'''
消融实验-军鸽算子(非选择)
'''
def __init__(self, params, **kwargs):
'''
:param kwargs: 初始化
such as:
{
"popSize" : 40
"vardim": 50
"bound" : [[l_1,l_2,...,l_n],[u_1,u_2,...,u_n]] # and n=vardim
"Maxiter" : 1000
"step" : in map factor, 60
"k" : in map factor, 4
"g" : guess step in landmark factor
"init_type" : 0 for oringin、1 for latin
"personal_type": True with Personal_Best updated
"func" : function of calculating fitness
"M" : matrix of input,E
"S" : shuffle of input,0
}
'''
self.popSize, self.vardim, self.bound = 40, 50, np.tile([[-100], [100]], 50)
self.func, self.M, self.S = None, np.eye(self.vardim),np.zeros(self.vardim)
self.init_type, self.personal_type = 0, False
self.Maxiter = 1000
self.step = 60
self.k = 16
self.c = 0.5
self.percent = 0.8
self.__set_keyword_arguments(params)
self.__set_keyword_arguments(kwargs)
self.Nc1 = int(self.percent * self.Maxiter)
self.Nc2 = self.Maxiter - self.Nc1
self.Pops = Pops(params=params,personal_type=True,init_type=0)
self.half = self.popSize
#print("xxx==",self.Pops.fitness)
def __set_keyword_arguments(self, kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def choise(self, n=2):
# half = int(self.popSize*0.2)+2
minf = np.min(self.Pops.fitness)
idx = 1 / (self.Pops.fitness - minf + 1e-10)
p = idx / np.sum(idx)
return np.random.choice(self.popSize, size=self.popSize, p=p, replace=True)
def choise_3(self,n):
rank = np.argsort(self.Pops.fitness)
p = np.ones(self.popSize) * self.c
p = p ** rank
p /= p.sum()
return np.random.choice(self.popSize, size=n, p=p, replace=True)
def move_part_one(self, epoch):
t = 1 / (1 + np.exp(self.k * (epoch / self.Nc1)))
# choised = self.choise_2(self.popSize)#np.random.randint(0,20,size=self.popSize)
#choised = self.choise_3(self.popSize)
r = np.random.random((self.popSize, 1)) * 3 - 1
mid_point = r * self.Pops.global_best_position + (1 - r) * self.Pops.personal_best_position
self.Pops.v = 0.5 * (self.bound[1] - self.bound[0]) * np.random.normal(0, t, (self.popSize, self.vardim))
self.Pops.pop = self.Pops.v + mid_point
# r = np.random.random(self.popSize)
# idx = r<0.5
# self.Pops.pop[idx]= self.Pops.v[idx] + mid_point[idx]
# idx = r>=0.5
# self.Pops.pop[idx] = mid_point[idx]
def move_part_two(self, epoch):
t = 1 / (1 + np.exp(self.k * (epoch / self.Nc2 - 0.5)))
self.Pops.v = (self.Pops.personal_best_position - self.Pops.global_best_position) * np.random.normal(0, t, (
self.popSize, self.vardim))
self.Pops.pop = self.Pops.global_best_position + self.Pops.v
class DNPIO_warpigeon_selected(BasePIO):
'''
消融实验-军鸽算子(带选择)
'''
def __init__(self, params, **kwargs):
'''
:param kwargs: 初始化
such as:
{
"popSize" : 40
"vardim": 50
"bound" : [[l_1,l_2,...,l_n],[u_1,u_2,...,u_n]] # and n=vardim
"Maxiter" : 1000
"step" : in map factor, 60
"k" : in map factor, 4
"g" : guess step in landmark factor
"init_type" : 0 for oringin、1 for latin
"personal_type": True with Personal_Best updated
"func" : function of calculating fitness
"M" : matrix of input,E
"S" : shuffle of input,0
}
'''
self.popSize, self.vardim, self.bound = 40, 50, np.tile([[-100], [100]], 50)
self.func, self.M, self.S = None, np.eye(self.vardim),np.zeros(self.vardim)
self.init_type, self.personal_type = 0, False
self.Maxiter = 1000
self.step = 60
self.k = 16
self.c = 0.5
self.percent = 0.8
self.__set_keyword_arguments(params)
self.__set_keyword_arguments(kwargs)
self.Nc1 = int(self.percent * self.Maxiter)
self.Nc2 = self.Maxiter - self.Nc1
self.Pops = Pops(params=params,personal_type=True,init_type=0)
self.half = self.popSize
#print("xxx==",self.Pops.fitness)
def __set_keyword_arguments(self, kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def choise(self, n=2):
# half = int(self.popSize*0.2)+2
minf = np.min(self.Pops.fitness)
idx = 1 / (self.Pops.fitness - minf + 1e-10)
p = idx / np.sum(idx)
return np.random.choice(self.popSize, size=self.popSize, p=p, replace=True)
def choise_3(self,n):
rank = np.argsort(self.Pops.fitness)
p = np.ones(self.popSize) * self.c
p = p ** rank
p /= p.sum()
return np.random.choice(self.popSize, size=n, p=p, replace=True)
def move_part_one(self, epoch):
t = 1 / (1 + np.exp(self.k * (epoch / self.Nc1)))
# choised = self.choise_2(self.popSize)#np.random.randint(0,20,size=self.popSize)
choised = self.choise_3(self.popSize)
r = np.random.random((self.popSize, 1)) * 3 - 1
mid_point = r * self.Pops.personal_best_position[choised] + (1 - r) * self.Pops.personal_best_position
self.Pops.v = 0.5 * (self.bound[1] - self.bound[0]) * np.random.normal(0, t, (self.popSize, self.vardim))
self.Pops.pop = self.Pops.v + mid_point
# r = np.random.random(self.popSize)
# idx = r<0.5
# self.Pops.pop[idx]= self.Pops.v[idx] + mid_point[idx]
# idx = r>=0.5
# self.Pops.pop[idx] = mid_point[idx]
def move_part_two(self, epoch):
t = 1 / (1 + np.exp(self.k * (epoch / self.Nc2 - 0.5)))
self.Pops.v = (self.Pops.personal_best_position - self.Pops.global_best_position) * np.random.normal(0, t, (
self.popSize, self.vardim))
self.Pops.pop = self.Pops.global_best_position + self.Pops.v