遗传算法
使用轮盘赌
import numpy as np
import pandas as pd
pd. set_option( 'precision' , 6 )
def Ninit ( n, len , a, b, N= [ ] ) :
for i in range ( 0 , n) :
x= np. array( [ round ( np. random. uniform( a[ 0 ] , a[ 1 ] ) , 6 ) , round ( np. random. uniform( b[ 0 ] , b[ 1 ] ) , 6 ) ] )
N. append( x)
return N
def ToCode ( input , output= [ ] ) :
N= input / 2
p= [ ]
m= input
c_x1= [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
while N> 0 :
N= int ( m / 2 )
a= m % 2
m= int ( m/ 2 )
p. append( a)
n= len ( p)
c_x1[ : n] = p
return c_x1
def toTen ( c_x) :
ten1= c_x[ 0 ] [ 0 ] * 1 + c_x[ 0 ] [ 1 ] * 2 + c_x[ 0 ] [ 2 ] * 4 + c_x[ 0 ] [ 3 ] * 8 + c_x[ 0 ] [ 4 ] * 16 + c_x[ 0 ] [ 5 ] * 32 + c_x[ 0 ] [ 6 ] * 64 + c_x[ 0 ] [ 7 ] * 128 + c_x[ 0 ] [ 8 ] * 256 + c_x[ 0 ] [ 9 ] * 512
ten2= c_x[ 1 ] [ 0 ] * 1 + c_x[ 1 ] [ 1 ] * 2 + c_x[ 1 ] [ 2 ] * 4 + c_x[ 1 ] [ 3 ] * 8 + c_x[ 1 ] [ 4 ] * 16 + c_x[ 1 ] [ 5 ] * 32 + c_x[ 1 ] [ 6 ] * 64 + c_x[ 1 ] [ 7 ] * 128 + c_x[ 1 ] [ 8 ] * 256 + c_x[ 1 ] [ 9 ] * 512
x_1= round ( - 2.9 + 14.9 * ten1/ ( ( 2 ** 10 ) - 1 ) , 6 )
x_1
x_2= round ( 4.2 + 1.5 * ten1/ ( ( 2 ** 10 ) - 1 ) , 6 )
return x_1, x_2
def file_du ( N) :
score= [ ]
for i in N:
x1= i[ 0 ]
x2= i[ 1 ]
f= round ( 21.5 + x1* np. sin( 4 * np. pi* x1) + x2* np. sin( 20 * np. pi* x2) , 6 )
score. append( f)
return score
def exchange ( N, P, e_mp, e_cmp) :
t_N= [ ]
aN= [ ]
T= [ ]
S= [ ]
t1= lunpan( P)
t2= lunpan( P)
C_N= N. copy( )
cutdot= np. random. randint( 0 , 10 )
change_dot= np. random. randint( 0 , 10 )
e_p= round ( np. random. random( ) , 6 )
x1, x2= toTen( C_N[ t1] )
t_N. append( x1)
t_N. append( x2)
aN. append( np. array( t_N) )
T. append( np. array( t_N) )
f_score1= file_du( aN)
t_N= [ ]
aN= [ ]
x1, x2= toTen( C_N[ t2] )
t_N. append( x1)
t_N. append( x2)
aN. append( np. array( t_N) )
T. append( np. array( t_N) )
f_score2= file_du( aN)
t_N= [ ]
aN= [ ]
cn1= C_N[ t1] . copy( )
cn2= C_N[ t2] . copy( )
if e_p<= e_mp:
temp= C_N[ t1] [ 0 ] [ cutdot: ]
C_N[ t1] [ 0 ] [ cutdot: ] = C_N[ t2] [ 1 ] [ cutdot: ]
C_N[ t2] [ 0 ] [ cutdot: ] = temp
temp= C_N[ t1] [ 1 ] [ cutdot: ]
C_N[ t1] [ 1 ] [ cutdot: ] = C_N[ t2] [ 1 ] [ cutdot: ]
C_N[ t2] [ 1 ] [ cutdot: ] = temp
docut_t1= np. random. randint( 0 , len ( C_N[ t1] [ 0 ] ) )
docut_t2= np. random. randint( 0 , len ( C_N[ t1] [ 0 ] ) )
tmm_t1= pd. DataFrame( C_N[ t1] [ 0 ] )
ts0= tmm_t1[ tmm_t1[ 0 ] == 0 ] . count( )
ts1= tmm_t1[ tmm_t1[ 0 ] == 1 ] . count( )
ts0p= ts0/ ( ts0+ ts1)
tmm_t2= pd. DataFrame( C_N[ t1] [ 1 ] )
ts02= tmm_t2[ tmm_t2[ 0 ] == 0 ] . count( )
ts12= tmm_t2[ tmm_t2[ 0 ] == 1 ] . count( )
ts02p= ts0/ ( ts02+ ts12)
pt= np. random. random( )
pl= np. random. random( )
for i in range ( 0 , int ( 10 * e_cmp) ) :
if 0 < pt and pt< ts0p[ 0 ] :
C_N[ t1] [ 0 ] [ docut_t1] = 0
C_N[ t2] [ 0 ] [ docut_t2] = 0
else :
C_N[ t1] [ 0 ] [ docut_t1] = 1
C_N[ t2] [ 0 ] [ docut_t2] = 1
for i in range ( 0 , 5 ) :
if 0 < pl and pl< ts02p[ 0 ] :
C_N[ t1] [ 1 ] [ docut_t1] = 0
C_N[ t2] [ 1 ] [ docut_t2] = 0
else :
C_N[ t1] [ 1 ] [ docut_t1] = 1
C_N[ t2] [ 1 ] [ docut_t2] = 1
x1, x2= toTen( C_N[ t1] )
t_N. append( x1)
t_N. append( x2)
aN. append( np. array( t_N) )
T. append( np. array( t_N) )
score1= file_du( aN)
t_N= [ ]
aN= [ ]
x1, x2= toTen( C_N[ t2] )
t_N. append( x1)
t_N. append( x2)
aN. append( np. array( t_N) )
T. append( np. array( t_N) )
score2= file_du( aN)
t_N= [ ]
aN= [ ]
S. append( f_score1)
S. append( f_score2)
S. append( score1)
S. append( score2)
T= pd. DataFrame( T)
T[ "s" ] = pd. DataFrame( np. array( S) )
T= T. sort_values( "s" )
T. index= range ( 0 , T. shape[ 0 ] )
return T
def lunpan ( P) :
R= np. random. random( )
t= 0
if len ( P) > 0 :
if R< P[ 0 ] :
t= 0
else :
for i in range ( 1 , len ( P) ) :
if P[ i- 1 ] < R and R< P[ i] :
t= i
else :
t= 0
return t
a= np. array( [ - 2.9 , 12 . ] )
b= np. array( [ 4.2 , 5.7 ] )
a[ 0 ]
N= [ ]
N= Ninit( 20 , 1 , a, b, N)
for i in range ( 0 , 100 ) :
score= file_du( N)
score= np. array( score)
M= pd. DataFrame( N)
M[ "SCORE" ] = pd. DataFrame( score)
M= M. sort_values( "SCORE" )
M. index= range ( 0 , M. shape[ 0 ] )
M_all= M. copy( )
P_ass= 0.5
L= int ( len ( N) * P_ass)
M= M. drop( M. index[ 0 : L] , axis= 0 )
M. index= range ( 0 , M. shape[ 0 ] )
M[ "s_score" ] = M. SCORE/ ( M. SCORE. sum ( ) )
M= M. sort_values( "s_score" )
M. index= range ( 0 , M. shape[ 0 ] )
P= [ ]
for i in range ( 0 , len ( M. s_score) ) :
P. append( M. s_score[ 0 : i+ 1 ] . sum ( ) )
M[ "P" ] = P
M
N= M. loc[ : , 0 : 1 ]
N= N. values
C_N= [ ]
x= [ ]
for i in N:
t_x1= int ( ( i[ 0 ] + 2.9 ) * ( ( 2 ** 10 ) - 1 ) / ( 14.9 ) )
t_x2= int ( ( i[ 1 ] - 4.2 ) * ( ( 2 ** 10 ) - 1 ) / ( 1.5 ) )
c_x1= ToCode( t_x1)
c_x2= ToCode( t_x2)
x. append( c_x1)
x. append( c_x2)
C_N. append( np. array( x) )
x= [ ]
new_N= [ ]
for i in range ( 0 , 10 ) :
son1= exchange( C_N, P, 0.7 , 0.3 )
son1= son1. sort_values( "s" )
son1. index= range ( 0 , son1. shape[ 0 ] )
son2= son1. copy( )
s1= son1. iloc[ 3 , 0 : 2 ] . values
s2= son2. iloc[ 2 , 0 : 2 ] . values
new_N. append( s1)
new_N. append( s2)
n_N= [ ]
N= [ ]
L2= int ( M. shape[ 0 ] * 0.9 )
tm= np. array( M. iloc[ L2: , 0 : 2 ] )
for i in tm:
N. append( i)
for i in new_N:
N. append( i)
n_N= [ ]
M_all