遺傳算法Python實(shí)現(xiàn)
- 瞎BB
- 代碼
- 導(dǎo)入庫以及參數(shù)設(shè)置
- 目標(biāo)函數(shù)
- 生成C行R列的值在0-1的數(shù)組
- 混沌函數(shù)
- 二進(jìn)制轉(zhuǎn)十進(jìn)制
- 個(gè)體按值從大到小排序
- 交叉變異
- 適應(yīng)度函數(shù)
- 主函數(shù)
瞎BB
代碼
導(dǎo)入庫以及參數(shù)設(shè)置
import
pandas
as
pd
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
import
math
import
random
#range of variable
bounds
=
np
.
array
(
[
-
2
,
2
]
)
#begin of variable
boundsbegin
=
bounds
[
0
]
#end of variable
boundsend
=
bounds
[
1
]
precision
=
0.0001
#calc the BitLength
BitLength
=
math
.
ceil
(
np
.
log2
(
(
boundsend
-
boundsbegin
)
/
precision
)
)
#init
popsize
=
100
Generationmax
=
100
pmut
=
0.09
目標(biāo)函數(shù)
def
targetfun
(
x
)
:
value
=
200
*
math
.
exp
(
-
0.05
*
x
)
*
math
.
sin
(
x
)
return
value
生成C行R列的值在0-1的數(shù)組
def
random_random
(
C
,
R
)
:
rand
=
[
]
for
i
in
range
(
C
*
R
)
:
rand
.
append
(
random
.
random
(
)
)
rand
=
np
.
array
(
rand
)
return
rand
.
reshape
(
C
,
R
)
混沌函數(shù)
def
chaos
(
size
)
:
chaos_cro
=
np
.
zeros
(
size
)
chaos_cro
[
0
]
=
random
.
random
(
)
for
j
in
range
(
1
,
size
)
:
chaos_cro
[
j
]
=
4
*
chaos_cro
[
j
-
1
]
*
(
1
-
chaos_cro
[
j
-
1
]
)
return
chaos_cro
[
-
1
]
二進(jìn)制轉(zhuǎn)十進(jìn)制
def
transform2to10
(
sample
)
:
BitLength
=
len
(
sample
)
x
=
sample
[
-
1
]
for
i
in
range
(
1
,
BitLength
)
:
x
=
x
+
sample
[
-
i
-
1
]
*
np
.
power
(
2
,
i
)
return
x
個(gè)體按值從大到小排序
def
rank
(
population
)
:
popsize
=
population
.
shape
[
0
]
BitLength
=
population
.
shape
[
1
]
fitvalue
=
np
.
zeros
(
popsize
)
.
reshape
(
popsize
,
1
)
for
i
in
range
(
popsize
)
:
x
=
transform2to10
(
population
[
i
]
)
#tansform to range of variable
xx
=
boundsbegin
+
x
*
(
boundsend
-
boundsbegin
)
/
(
np
.
power
(
boundsend
,
BitLength
)
-
1
)
fitvalue
[
i
,
0
]
=
targetfun
(
xx
)
#make fitvalue(j)
res
=
np
.
concatenate
(
(
population
,
fitvalue
)
,
axis
=
1
)
res
=
pd
.
DataFrame
(
res
)
population
=
res
.
sort_values
(
by
=
BitLength
)
population
=
np
.
array
(
population
)
population
=
population
[
:
,
0
:
BitLength
]
return
population
交叉變異
def
cro_mut_improve
(
population
,
rate_mut
)
:
#seln:two individuals
popsize
=
population
.
shape
[
0
]
BitLength
=
population
.
shape
[
1
]
population
=
rank
(
population
)
#crossover
pop
=
popsize
chaos_cro
=
np
.
zeros
(
BitLength
)
if
popsize
%
2
==
1
:
pop
=
popsize
-
1
for
i
in
range
(
0
,
pop
,
2
)
:
chaos_cro
=
chaos
(
BitLength
)
chao
=
math
.
floor
(
chaos_cro
*
BitLength
)
temp
=
population
[
i
,
chao
]
population
[
i
,
chao
]
=
population
[
i
+
1
,
chao
]
population
[
i
+
1
,
chao
]
=
temp
#mutation
by
=
np
.
array
(
[
]
)
n
=
len
(
by
)
#generate random individuals to mutate
while
n
==
0
:
by
=
random_random
(
1
,
popsize
)
<
rate_mut
for
i
in
by
[
0
]
:
if
i
:
n
+=
1
num_mut
=
n
for
k
in
range
(
popsize
)
:
if
by
[
0
,
k
]
==
True
:
chaos_mut
=
np
.
zeros
(
BitLength
)
chaos_mut
[
0
]
=
random
.
random
(
)
for
t
in
range
(
1
,
BitLength
)
:
chaos_mut
[
t
]
=
4
*
chaos_mut
[
t
-
1
]
*
(
1
-
chaos_mut
[
t
-
1
]
)
position
=
np
.
floor
(
BitLength
*
random_random
(
1
,
2
)
)
position
.
astype
(
np
.
int16
)
population
[
k
,
int
(
position
[
0
,
0
]
)
]
=
np
.
round
(
chaos
(
BitLength
)
)
population
[
k
,
int
(
position
[
0
,
1
]
)
]
=
np
.
round
(
chaos
(
BitLength
)
)
return
population
適應(yīng)度函數(shù)
def
fitness
(
population
)
:
#population=pd.DataFrame(population)
popsize
=
population
.
shape
[
0
]
BitLength
=
population
.
shape
[
1
]
cumsump
=
np
.
zeros
(
popsize
)
.
reshape
(
1
,
popsize
)
fitvalue
=
np
.
zeros
(
popsize
)
.
reshape
(
1
,
popsize
)
for
i
in
range
(
popsize
)
:
x
=
transform2to10
(
population
[
i
]
)
#tansform to range of variable
xx
=
boundsbegin
+
x
*
(
boundsend
-
boundsbegin
)
/
(
np
.
power
(
boundsend
,
BitLength
)
-
1
)
fitvalue
[
0
,
i
]
=
targetfun
(
xx
)
#ensure fitvalue>0
fitvalue
=
fitvalue
+
230
fsum
=
fitvalue
.
sum
(
1
)
[
0
]
Pperpopulation
=
fitvalue
/
fsum
cumsump
[
0
]
=
Pperpopulation
[
0
]
for
i
in
range
(
1
,
popsize
)
:
cumsump
[
0
,
i
]
=
cumsump
[
0
,
i
-
1
]
+
Pperpopulation
[
0
,
i
]
res
=
np
.
concatenate
(
(
fitvalue
,
cumsump
)
,
axis
=
0
)
return
res
主函數(shù)
ymax
=
np
.
zeros
(
Generationmax
+
1
)
ymean
=
np
.
zeros
(
Generationmax
+
1
)
xmax
=
np
.
zeros
(
Generationmax
+
1
)
#generate random population
population
=
np
.
round
(
random_random
(
popsize
,
BitLength
)
)
#calc fitness return fitvalue and sum of probability
res
=
fitness
(
population
)
fitvalue
=
res
[
0
]
cumsump
=
res
[
1
]
#main code
Generation
=
1
while
Generation
<
Generationmax
+
1
:
population
=
cro_mut_improve
(
population
,
pmut
)
res
=
fitness
(
population
)
fitvalue
=
res
[
0
]
cumsump
=
res
[
1
]
fmax
=
np
.
max
(
fitvalue
)
for
i
in
range
(
popsize
)
:
if
fitvalue
[
i
]
==
fmax
:
nmax
=
i
break
fmean
=
np
.
mean
(
fitvalue
,
axis
=
0
)
ymax
[
Generation
]
=
fmax
ymean
[
Generation
]
=
fmean
x
=
transform2to10
(
population
[
nmax
]
)
xx
=
boundsbegin
+
x
*
(
boundsend
-
boundsbegin
)
/
(
pow
(
boundsend
,
BitLength
)
-
1
)
xmax
[
Generation
]
=
xx
Generation
+=
1
#Generation=Generation-1;
Bestpopulation
=
np
.
max
(
xmax
)
BestValue
=
targetfun
(
Bestpopulation
)
print
(
Bestpopulation
)
print
(
BestValue
)
x
=
[
]
y
=
[
]
for
i
in
range
(
400
)
:
t
=
-
2
+
0.01
*
i
x
.
append
(
t
)
y
.
append
(
targetfun
(
t
)
)
plt
.
scatter
(
x
,
y
)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
