, d) [$ I6 }8 m
种群:多个个体即组成一个种群。GA算法中,一个问题的多组解即构成了问题的解的种群。6 a1 Y* x b# H+ M
7 ^% Y. g2 L+ G y' ]% e
2、主要步骤 ( h# D5 N. o3 ?6 i7 E# eGA算法的基本步骤如下:) h% l. Z$ t ^4 s
X* }8 l4 v* X2 ]" Q/ l
Step 1. 种群初始化。选择一种编码方案然后在解空间内通过随机生成的方式初始化一定数量的个体构成GA的种群。8 s+ R8 v8 G% g, U2 r6 s% s: v% G3 p4 m, S- [
! |9 l% r; B9 K+ e. vStep 2. 评估种群。利用启发式算法对种群中的个体(矩形件的排入顺序)生成排样图并依此计算个体的适应函数值(利用率),然后保存当前种群中的最优个体作为搜索到的最优解。/ W) T+ z/ L; k7 p% N: H1 m! S% I
D# p5 v# M# I) {6 l% m
Step 3. 选择操作。根据种群中个体的适应度的大小,通过轮盘赌或者期望值方法,将适应度高的个体从当前种群中选择出来。 ; R2 D# S1 o- W: J" t * }) O7 u( c. J7 O3 {/ S) }' b* hStep 4. 交叉操作。将上一步骤选择的个体,用一定的概率阀值Pc控制是否利用单点交叉、多点交叉或者其他交叉方式生成新的交叉个体。 + p2 u& U0 C) @1 u4 v7 e 8 v7 E. |/ I6 [9 q. JStep 5. 变异操作。用一定的概率阀值Pm控制是否对个体的部分基因执行单点变异或多点变异。 5 z. w9 y; w. B 1 y' a: E& b0 m" iStep 6. 终止判断。若满足终止条件,则终止算法,否则返回Step 2。 9 w% G) A3 g% K9 Z" `/ ]4 D" g! T+ D$ U
流程图如下所示: % m2 \: Y& W0 N
: J8 C* v; n' E* K& l4 Z$ A5 _7 m |
4、Python代码 . \ v' z1 w; n- k# B; t3 d#-*- coding:utf-8 -*- 2 K1 {. C0 @7 S/ B) ?! X& v# A R4 M% C/ @
import random& I- H$ j2 j/ i i O; D, u
import math! t& I% n& i v3 w% M3 ]2 l8 F! {1 ^# G
from operator import itemgetter : [2 {& X( L. e% X9 x( X9 S3 P: \! p0 ^* f& I$ L
class Gene: 5 u1 @9 v4 Y/ {: [( c g3 V( V ''' , t S* @. B$ Z7 e( X( A" C, [ This is a class to represent individual(Gene) in GA algorithom / q$ S# l2 ~7 { Y. O each object of this class have two attribute: data, size' Q3 T$ d: f7 [# h+ r8 \, ?
''' ( v7 v( ?7 h8 q% y0 P( L5 q, m& r+ h def __init__(self,**data): ; }% V% S( `" x/ h: R self.__dict__.update(data) 7 f! s7 e' Z8 A6 T$ p
self.size = len(data['data'])#length of gene ( y. h& F" s% [4 y; \, {' Q& [; u5 m: T# q* d
$ d; h) ~& z4 W4 Q2 `. x3 Wclass GA:6 Q. I0 ^& h+ P1 x/ k& I
''' / S: ^2 i1 T' A, s- b This is a class of GA algorithm. 2 [+ A6 t# j, l( `) Q- v ''' $ K9 D# X* b! H" @ def __init__(self,parameter): 7 O6 u2 d/ A! J# z1 q3 X ''' # Q; F& y" \, @% W: R* _) Z' ] Initialize the pop of GA algorithom and evaluate the pop by computing its' fitness value . 4 W/ V* J' a4 g- ?1 v The data structure of pop is composed of several individuals which has the form like that:, M* @% a1 k$ E. n3 n
% @9 W; N8 }4 }( |4 |8 v; n {'Gene':a object of class Gene, 'fitness': 1.02(for example)}$ M; `4 ?% m. w a; N
Representation of Gene is a list: [b s0 u0 sita0 s1 u1 sita1 s2 u2 sita2]; C8 k( h7 d$ s: ~) S8 `; @8 |/ z. I L
& q6 p4 T! c S4 K7 E
''' 3 ?. d0 V" l$ z" L! q' _) L #parameter = [CXPB, MUTPB, NGEN, popsize, low, up] S5 s+ |! ?/ c: O1 d6 O3 @" ^; {
self.parameter = parameter 6 W0 _5 p: x. O; }5 _# S7 { $ m& }) b1 X- a8 `2 _ low = self.parameter[4] 8 c0 ^% \" l i8 `2 h$ R' j up = self.parameter[5]+ ^; O- V4 j2 y- S
! z% v; v# F$ k0 i
self.bound = []! v4 P6 r9 ^1 v6 B: [
self.bound.append(low)& M& A# @: L$ q6 P" N5 |1 S
self.bound.append(up) |5 ^8 I! a C2 d# z" T" k
- l* d% p# Z9 \' X+ y3 `" G- r6 G pop = []$ n$ L; N* {% X) w$ ~
for i in range(self.parameter[3]): ' ~( R& Y" H i* C* U2 c& c- v geneinfo = [] + y) y5 r, {# p- }+ F9 f, u: ]# q* T for pos in range(len(low)): 5 g4 k9 ?" H; R4 [4 U9 U geneinfo.append(random.uniform(self.bound[0][pos], self.bound[1][pos]))#initialise popluation6 i, u. g8 z9 F
8 j' O7 |. m( O9 r& c1 d
fitness = evaluate(geneinfo)#evaluate each chromosome) E5 E0 x) @) m% D- i& e8 B
pop.append({'Gene':Gene(data = geneinfo), 'fitness':fitness})#store the chromosome and its fitness 5 s6 I q7 |, @4 v f : c" x. M/ E" j0 I1 ] self.pop = pop / B, `$ S, E: ^% m! R6 F8 P self.bestindividual = self.selectBest(self.pop)#store the best chromosome in the population ) S2 u$ a# h7 I # P# x5 P, G+ ?& I5 ? def selectBest(self, pop):/ R- R e( b4 b g) u+ j4 s
''' 1 Y/ o+ G) z% B6 ? select the best individual from pop 4 g3 n+ }& @- n2 j ''' ) H$ E! p" w) m* i* U s_inds = sorted(pop, key = itemgetter("fitness"), reverse = False) ( y* [% z% O5 M) ^' F return s_inds[0] , M) g3 E+ P% ^+ a9 j4 o) l* ^" Y/ p# g' P5 c9 \
def selection(self, individuals, k):' O+ U# o6 t' w- H' c0 ?
''' $ V, x3 Q5 U |& b9 i, L2 w0 I select two individuals from pop7 q1 n" h8 H7 x3 q! z
''' / K# G( c' r8 S8 ? s_inds = sorted(individuals, key = itemgetter("fitness"), reverse=True)#sort the pop by the reference of 1/fitness # V* C1 J( k( q ? sum_fits = sum(1/ind['fitness'] for ind in individuals) #sum up the 1/fitness of the whole pop$ [0 G( d( W0 M/ C) A
& m/ [; S h" m( u+ a) I- Z chosen = []! z \# ^# U1 y& `7 _* x
for i in xrange(k):7 q% `) @+ o# T5 y3 K% S
u = random.random() * sum_fits#randomly produce a num in the range of [0, sum_fits]2 |- f& N1 [7 L0 c, W( k
sum_ = 0 3 f' _! p, b# I' X6 r3 ~ for ind in s_inds: , B6 u0 |- N: H( Q7 @ sum_ += 1/ind['fitness']#sum up the 1/fitness9 A: B% b# j1 N8 C
if sum_ > u: 4 |( Q/ S+ S4 _* g$ A1 | #when the sum of 1/fitness is bigger than u, choose the one, which means u is in the range of [sum(1,2,...,n-1),sum(1,2,...,n)] and is time to choose the one ,namely n-th individual in the pop% e% y1 c8 x! F$ x
chosen.append(ind) 2 ` E% |4 ^/ H1 _- w# _. M% e break6 O$ ]0 L. a0 h4 t5 n
. ~0 t- R6 N2 S# }- r4 N1 Z- E( S ; _6 o0 w F6 P2 _7 t3 x def crossoperate(self, offspring): " [0 V9 K. K$ Q: ~& c- Y* B '''* k7 ^4 v2 X8 R
cross operation 9 `9 a% ?+ X0 k& o7 N$ r) T '''- I) u% J( t* v- q* J
dim = len(offspring[0]['Gene'].data)7 f' F* x6 d# L6 n; z' ^
! z$ b" G; z- g! M- l8 c# P$ W1 c
geninfo1 = offspring[0]['Gene'].data#Gene's data of first offspring chosen from the selected pop ; B4 ]1 m! L1 i geninfo2 = offspring[1]['Gene'].data#Gene's data of second offspring chosen from the selected pop " E% I+ _0 M" K* i' f& A8 a : U+ b# R1 [$ p; ]( |3 S pos1 = random.randrange(1,dim)#select a position in the range from 0 to dim-1, : |, S6 J& Z4 r3 ] pos2 = random.randrange(1,dim)7 d& Q. `% L( H* H" c
* @/ W9 A! Z, ^* Y. g/ L+ S newoff = Gene(data = [])#offspring produced by cross operation , `8 H1 _" N! T temp = [] E, n1 P- M! G5 J: l for i in range(dim):3 M7 I5 j' m- e t$ q- k: C
if (i >= min(pos1,pos2) and i <= max(pos1,pos2)): \! }' r* e; I4 F; F
temp.append(geninfo2)& D4 }# v. W: D& \' G |
#the gene data of offspring produced by cross operation is from the second offspring in the range [min(pos1,pos2),max(pos1,pos2)] . R! {5 }% M4 W" C else:% x, d4 Q8 o# w
temp.append(geninfo1)$ }* P. K3 n8 N" ^$ f: R* X4 c0 c; f
#the gene data of offspring produced by cross operation is from the frist offspring in the range [min(pos1,pos2),max(pos1,pos2)]1 L! Z- h0 O" D! v5 }- w
newoff.data = temp3 S- {+ ]: E( }6 q2 c9 T. e6 n; O
1 h( R4 {3 d! s. A1 @ return newoff V) M C; T$ Z7 x) H0 }* \& N5 f, M" K+ f0 T5 N
4 s$ G, A9 ^4 `) [+ E; o3 K( W1 S, n
def mutation(self, crossoff, bound): , w% H. o: s- P* @6 W; J- w '''( @) }- j4 @, v8 d. _# v" P+ Y
mutation operation $ @7 _1 ?" `: f# ~$ w/ {6 s1 P ''' " w- i0 V1 t4 X! `# k; s0 `6 s. X9 L; E/ {2 F h
dim = len(crossoff.data)' L2 G7 o# S) x3 |0 y$ v
, }9 H* V( G+ m( F( A0 E: o( ~0 f0 g pos = random.randrange(1,dim)#chose a position in crossoff to perform mutation. 0 J2 p6 {- U. I* Z7 I ! G4 j9 A4 Z. V3 H/ J crossoff.data[pos] = random.uniform(bound[0][pos],bound[1][pos]) 0 @% Y; I+ q- q, K7 c return crossoff/ @# a. h% N y
& | N* D" E5 j n% d+ y def GA_main(self): ; d( p& Z; \* n9 f7 W3 N; z. m- ? '''& |) ]( g7 h# n C/ l ~8 m
main frame work of GA : k, ]; L+ r8 A, \; m; K ''' ! a A! @- b+ f, |, l+ W , S+ \8 k* x% F# l popsize = self.parameter[3] 6 ^% _' b$ l5 z" P $ \4 d6 Q0 p: ^' m print("Start of evolution")+ e/ G$ `: m7 m
0 M' O/ [5 _5 R9 i7 | # Begin the evolution ; F9 j/ t! j# [ for g in range(NGEN):$ p' e1 v# f/ A% N5 k
t) Y- q$ O' w$ _6 @ print("-- Generation %i --" % g) 8 U$ [ L8 v7 q7 C) W: J" i
& p8 J4 ?0 c2 z& P/ ^( d #Apply selection based on their converted fitness J% r9 b0 z: K selectpop = self.selection(self.pop, popsize) : f7 M$ `8 ^3 n8 T
% {! f3 K4 A9 t% F" u( Y- S
nextoff = [] * }) {+ N T, ^8 [. ~' R while len(nextoff) != popsize: 3 f9 ~, K9 C9 T0 W; e! B* f* K! z # Apply crossover and mutation on the offspring ( C' b( g5 z% x" {( ]: C$ K, o6 K' q/ Y- c6 G
# Select two individuals- x# S0 s' w1 Y- u& r6 k) E* p
offspring = [random.choice(selectpop) for i in xrange(2)]$ e3 i# U; F' n, s2 v* g
1 q3 h' t: Z! I0 Z" }# n# j
if random.random() < CXPB: # cross two individuals with probability CXPB9 i) m% E% _) y: x" A
crossoff = self.crossoperate(offspring)! K9 F& d5 V) B g7 ?! A& n
fit_crossoff = evaluate(self.xydata, crossoff.data)# Evaluate the individuals 0 o: p* M8 P. C5 s / u" Q! K" \& o b! U if random.random() < MUTPB: # mutate an individual with probability MUTPB' @) Z6 E/ `$ n! H
muteoff = self.mutation(crossoff,self.bound) + y3 x+ T; c! ~ fit_muteoff = evaluate(self.xydata, muteoff.data)# Evaluate the individuals4 K# d% y9 x% m; ]
nextoff.append({'Gene':muteoff,'fitness':fit_muteoff}); P) g n Z- L2 V
$ T" A+ m4 m% M! U( ]; ` # The population is entirely replaced by the offspring+ |! W; ~2 {! ^# D7 Z x$ Y' W) I
self.pop = nextoff; F/ r. R& o% T4 |3 e
7 a. n: W2 x K4 ~5 s! [, u! i
# Gather all the fitnesses in one list and print the stats' `7 g; I3 N- g! U |# u
fits = [ind['fitness'] for ind in self.pop]6 i8 d; F& x% @
) `; V& C6 R: ]# N$ ]& ]
length = len(self.pop) " I4 j- j- \- A+ N0 @) Q mean = sum(fits) / length ; u8 A' Z; Z' [& z sum2 = sum(x*x for x in fits); i3 Q5 q' a t6 h" C* s) L3 g1 r
std = abs(sum2 / length - mean**2)**0.5 9 W( O- Q" ?1 @ best_ind = self.selectBest(self.pop) 2 M$ Z8 O! J. c& S, y6 T6 B3 x1 ?8 E' z7 L/ j# f1 S; k
if best_ind['fitness'] < self.bestindividual['fitness']: 1 \6 A$ L- d6 a7 O: J/ S self.bestindividual = best_ind9 \1 v$ N1 x9 L. U
1 R3 }. c$ |2 _, }
print("Best individual found is %s, %s" % (self.bestindividual['Gene'].data,self.bestindividual['fitness'])) 4 j7 Y1 w- |( U& n print(" Min fitness of current pop: %s" % min(fits))2 X$ f5 O& f* ]7 b8 p4 w1 D
print(" Max fitness of current pop: %s" % max(fits))5 ~! U$ u* G q1 \) K
print(" Avg fitness of current pop: %s" % mean)5 R: j$ [: V5 `7 s
print(" Std of currrent pop: %s" % std) & c" }9 h' F6 H, X3 `' ]* l0 u! T4 r% f: G- \ n
print("-- End of (successful) evolution --") ( `3 D" C! J1 w9 @" O# _9 a. a/ y ! X/ y- C, i0 N5 z/ Xif __name__ == "__main__":8 o# I1 d4 ]* ]
* G/ V: `( d7 t, u7 J
CXPB, MUTPB, NGEN, popsize = 0.8, 0.3, 50, 100#control parameters ' a8 j; K% z! V4 C8 ]3 v: | % V" L" n& K6 t! m up = [64, 64, 64, 64, 64, 64, 64, 64, 64, 64]#upper range for variables5 X% t" B0 f# _) K- G+ d- Q: d
low = [-64, -64, -64, -64, -64, -64, -64, -64, -64, -64]#lower range for variables* z9 s0 R6 F( Z- z% d$ B4 j! @: G
parameter = [CXPB, MUTPB, NGEN, popsize, low, up]6 l4 C& b9 i1 o( G: e
' s+ n0 B/ z, e1 t run = GA(parameter)4 l. _& O: J7 S
run.GA_main()' b; C0 i6 k7 T; ]& I3 z
———————————————— 9 C. \& |8 S4 D5 y }% g+ T版权声明:本文为CSDN博主「bible_reader」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 $ H8 C" y2 ~& w2 i0 I/ C原文链接:https://blog.csdn.net/bible_reader/article/details/72782675 ?; x! l y1 A% n* V `1 d
9 S" h/ S' U, V5 S