生活随笔
收集整理的這篇文章主要介紹了
二进制蚁群算法【源码实现】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
蟻群算法
?????之前講解了蟻群算法,見鏈接。萬字長文帶你了解蟻群算法及求解復雜約束問題【源碼實現】
?????今天講解下二進制的蟻群算法。
直接上算例。
算例
愛小玉,愛學習。
? ? ? ?? 假設一個數PD=210,它可以看成下列中的數,某幾個組合而成來的P_state=np.array([1700,2600,180,2200,1400,60,85,290,4100,3000,1590,200,1300,50,100,30,90]),即PD=sum(xiP_statei)其中的xi表示狀態,只能取0或1。
? ? ? ?? 限制條件:【180,2200】,序號[2,3]不能共存;【60,85,300】,序號[5,6,7]不能共存;【50,100】 序號[13,14]不能共存;【25,90】序號[15,16]不能共存。
這題的意思假設某刻時刻。家用總負荷為210W
家用總負荷是由各個電器的功率疊加的。其中【180,2200】,序號[2,3]不能共存,表示【180,2200】對應的是同一個電器,這個電器的功率可以是180或者2200.當一個時刻只能是一個值。其它同理。
算例實現
python 版代碼。
import numpy
as np
from tqdm
import tqdm
import matplotlib
.pyplot
as plt
import matplotlib
as mpl
import matplotlib
; matplotlib
.use
('TkAgg')
mpl
.rcParams
['font.sans-serif'] = ['SimHei']
mpl
.rcParams
['axes.unicode_minus'] = False
import mathP_state
=np
.array
([1700,2600,180,2200,1400,60,85,290,4100,3000,1590,200,1300,50,100,30,90])state_num
=17
Pload
=446.0
def calc_f(xi
):""":param xi 為個體 即電器的工作狀態,01二二進制,0表示當前狀態關,1表示開:xi 為np.array(數據):return 返回 np.abs(XiP_state-pload) P_state為電器"額定功率":"""xi
=np
.array
(xi
)xi
=xi
.reshape
(state_num
,)return np
.abs(np
.sum(xi
*P_state
)-Pload
)
m
=20
G_max
=300
Rho
=0.9
P0
=0.2
Tau
=np
.zeros
(shape
=(m
,))
P
=np
.zeros
(shape
=(G_max
,m
))
fitneess_value_list
=[]
f
=np
.random
.randint
(0,2,(m
,state_num
))
for j
in tqdm
(range(m
)):while f
[j
, 2] + f
[j
, 3] == 2:f
[j
, 2] = np
.random
.randint
(0, 2, 1)f
[j
, 3] = np
.random
.randint
(0, 2, 1)while f
[j
, 5] + f
[j
, 6] + f
[j
, 7] == 2 or f
[j
, 5] + f
[j
, 6] + f
[j
, 7] == 3:f
[j
, 5] = np
.random
.randint
(0, 2, 1)f
[j
, 6] = np
.random
.randint
(0, 2, 1)f
[j
, 7] = np
.random
.randint
(0, 2, 1)while f
[j
, 13] + f
[j
, 14] == 2:f
[j
, 13] = np
.random
.randint
(0, 2, 1)f
[j
, 14] = np
.random
.randint
(0, 2, 1)while f
[j
, 15] + f
[j
, 16] == 2:f
[j
, 15] = np
.random
.randint
(0, 2, 1)f
[j
, 16] = np
.random
.randint
(0, 2, 1)for i
in range(m
):Tau
[i
] = calc_f
(f
[i
])step
=0.3;
for NC
in range(G_max
):lamda
=1/(NC
+1)BestIndex
=np
.argmin
(Tau
) Tau_best
=Tau
[BestIndex
] for i
in range(m
):P
[NC
,i
]=np
.abs((Tau_best
-Tau
[i
]))/np
.abs(Tau_best
)+0.01 for i
in range(m
): bf
=f
[i
].copy
()if P
[NC
, i
] < P0
:sizeaca
=np
.int(state_num
*(step
*lamda
))index
=np
.random
.choice
(np
.arange
(0,state_num
,1),size
=sizeaca
,replace
=False)bf
[index
]=1-bf
[index
] else:index
= np
.random
.choice
(np
.arange
(0, state_num
, 1), size
=np
.int(state_num
/4), replace
=False) bf
[index
] = 1 - bf
[index
] while bf
[2] + bf
[3] == 2:bf
[2] = np
.random
.randint
(0, 2, 1)bf
[3]= np
.random
.randint
(0, 2, 1)while bf
[5] + bf
[6] + bf
[7] == 2 or bf
[5] + bf
[6] + bf
[7] == 3:bf
[5] = np
.random
.randint
(0, 2, 1)bf
[6] = np
.random
.randint
(0, 2, 1)bf
[7] = np
.random
.randint
(0, 2, 1)while bf
[13] + bf
[14] == 2:bf
[13] = np
.random
.randint
(0, 2, 1)bf
[14] = np
.random
.randint
(0, 2, 1)while bf
[15] + bf
[16] == 2:bf
[15] = np
.random
.randint
(0, 2, 1)bf
[16] = np
.random
.randint
(0, 2, 1)if calc_f
(bf
) < calc_f
(f
[i
]):f
[i
]=bf
for i
in range(m
): Tau
[i
] = (1 - Rho
) * Tau
[i
] + calc_f
(f
[i
]) index
= np
.argmin
(Tau
) value
= Tau
[index
] fitneess_value_list
.append
(calc_f
(f
[i
]))
min_index
=np
.argmin
(Tau
)
minf
=f
[min_index
] minValue
=calc_f
(minf
) print('最優變量',minf
)
print('最優變量對應的功率',minf
*P_state
)
print('最優誤差',minValue
)plt
.plot
(fitneess_value_list
,label
='迭代曲線')
plt
.legend
()
plt
.show
()
作者:電氣-余登武。 一個電氣專業的計算機選手
原創不易,禁止轉載。
總結
以上是生活随笔為你收集整理的二进制蚁群算法【源码实现】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。