生活随笔
收集整理的這篇文章主要介紹了
免疫算法(二进制)算例(源码实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??????之前我們講解了免疫算法以及離散的免疫算法。見鏈接:
萬字長文了解免疫算法原理 及求解復雜約束問題(源碼實現)
離散免疫算法求解旅行商問題(源碼實現)
??????今天講下二進制的免疫算法。
我愛學習,愛玉醬。
算例
?????? 假設一個數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]不能共存。
算例實現
?????? 如果不懂算法原理,請看之前免疫算法博文。
??????由于二進制編碼不可能出現重復,所以無需計算濃度。
python實現
python3.7
import pandas
as pd
import numpy
as np
from tqdm
import tqdm
import matplotlib
.pyplot
as plt
import matplotlib
; matplotlib
.use
('TkAgg')
from pylab
import *
mpl
.rcParams
['font.sans-serif'] = ['SimHei']
mpl
.rcParams
['axes.unicode_minus'] = FalseP_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
)
def variation(Sortf
):""":param Sortf : 排序后的種群:return: 經過克隆、變異、變異抑制后的群體af .af的規模為Sortf的一半"""Ncl
= 5 af
= np
.zeros
((np
.int(NP
/ 2), state_num
)) for i
in range(np
.int(NP
/ 2)): a
= Sortf
[i
] a
= a
.reshape
(-1, state_num
) Na
= np
.tile
(a
, (Ncl
, 1)) for j
in range(Ncl
): for k
in range(state_num
):if np
.random
.random
()<0.5:Na
[j
,k
]=1else:Na
[j
,k
]=0while Na
[j
, 2] + Na
[j
, 3] == 2 :Na
[j
, 2] = np
.random
.randint
(0, 2, 1)Na
[j
, 3] = np
.random
.randint
(0, 2, 1)while Na
[j
, 5] + Na
[j
, 6] + Na
[j
, 7] == 2 or Na
[j
, 5] + Na
[j
, 6] + Na
[j
, 7] == 3:Na
[j
, 5] = np
.random
.randint
(0, 2, 1)Na
[j
, 6] = np
.random
.randint
(0, 2, 1)Na
[j
, 7] = np
.random
.randint
(0, 2, 1)while Na
[j
, 13] + Na
[j
, 14] == 2:Na
[j
, 13] = np
.random
.randint
(0, 2, 1)Na
[j
, 14] = np
.random
.randint
(0, 2, 1)while Na
[j
, 15] + Na
[j
, 16] == 2:Na
[j
, 15] = np
.random
.randint
(0, 2, 1)Na
[j
, 16] = np
.random
.randint
(0, 2, 1)Na
[0, :] = Sortf
[i
]NaMSLL
= np
.zeros
((Ncl
, 1)) for j
in range(Ncl
): NaMSLL
[j
] = calc_f
(xi
=Na
[j
]) Index
= np
.argsort
(NaMSLL
, axis
=0) Index
= Index
[:, 0]NaSortf
= Na
[Index
] af
[i
] = NaSortf
[0] return af
def refresh():""":return: 創建一半新生群體 bf"""bf
= np
.random
.randint
(0, 2, (np
.int(NP
/2), state_num
)) for j
in range(np
.int(NP
/ 2)): while bf
[j
, 2] + bf
[j
, 3] == 2:bf
[j
, 2] = np
.random
.randint
(0, 2, 1)bf
[j
, 3] = np
.random
.randint
(0, 2, 1)while bf
[j
, 5] + bf
[j
, 6] + bf
[j
, 7] == 2 or bf
[j
, 5] + bf
[j
, 6] + bf
[j
, 7] == 3:bf
[j
, 5] = np
.random
.randint
(0, 2, 1)bf
[j
, 6] = np
.random
.randint
(0, 2, 1)bf
[j
, 7] = np
.random
.randint
(0, 2, 1)while bf
[j
, 13] + bf
[j
, 14] == 2:bf
[j
, 13] = np
.random
.randint
(0, 2, 1)bf
[j
, 14] = np
.random
.randint
(0, 2, 1)while bf
[j
, 15] + bf
[j
, 16] == 2:bf
[j
, 15] = np
.random
.randint
(0, 2, 1)bf
[j
, 16] = np
.random
.randint
(0, 2, 1)return bf
NP
=100
G
=200
f
=np
.random
.randint
(0,2,(NP
,state_num
))
for j
in range(NP
):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)len=np
.zeros
((NP
,1)) for i
in range(NP
):len[i
]=calc_f
(xi
=f
[i
])
Index
=np
.argsort
(len,axis
=0)
Index
=Index
[:,0]
Sortf
=f
[Index
]
trace
=[]
for gen
in tqdm
(range(G
)):af
= variation
(Sortf
) aflen
=np
.zeros
((af
.shape
[0],1)) for j
in range(af
.shape
[0]):aflen
[j
] = calc_f
(xi
=af
[j
]) bf
= refresh
() bflen
= np
.zeros
((bf
.shape
[0], 1)) for j
in range(bf
.shape
[0]): bflen
[j
] = calc_f
(xi
=bf
[j
]) f1
= np
.concatenate
((af
, bf
), axis
=0) f1len
= np
.concatenate
((aflen
, bflen
), axis
=0) Index
= np
.argsort
(f1len
, axis
=0)Index
= Index
[:, 0]Sortf
= f1
[Index
] trace
.append
(calc_f
(xi
=f1
[0]))
Bestf
=Sortf
[0,:]
print('最優變量',Bestf
)
print('誤差',trace
[-1] )
print('最優值',np
.array
(Bestf
)*P_state
)
plt
.plot
(trace
)
plt
.title
('迭代曲線')
plt
.xlabel
('迭代次數')
plt
.ylabel
('abs(sum(xi*Pi)-Pload)')
plt
.show
()
結果圖
作者:電氣-余登武
總結
以上是生活随笔為你收集整理的免疫算法(二进制)算例(源码实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。