
http://www.ma-xy.com
1.2 遗传算法 第一章 智能优化
35 options = gaoptimset ( options , ’ Pop ulati onSiz e ’ ,1 0 ) ;%种 群 大 小
36 options = gaoptimset ( options , ’ Generations ’ ,150 , ’ StallGenLimit ’ , 100) ;%停 止 准 则
37 options = gaoptimset ( options ’ Selection Fcn ’ , @selectiontournament , . . .
38 ’ Fitne s s Scaling F c n ’ , @f i tsc a lin g pro p ) ;%设 置 选 择 、 交 叉 、 变 异 等 方 法
39 [ x , f v a l ] = ga ( f i t n e s s f c n , nvars , [ ] , [ ] , [ ] , [ ] , LB,UB, FunConstraint , [ ] , o p t i o ns )
40 % 4 、 让 结 果 一 致
41 t he s ta t e = rng ;% 多 次 运 行 ga 时 , 使 多 次 的 结 果 一 致 。
42 % 你 可 能 没 有 意 识 到 你 会 想 尝 试 复 制 之 前 运 行 遗 传 算 法 的 结 果 。 在 这 种 情 况 下 , 只 要 你 有 输 出 结 构
, 你 可 以 重 置 随 机 数 发 生 器 如 下
43 strm = RandStream . getGlobalStream ;
44 strm . Stat e = Output . rng s t ate . s ta te ;
45 % 5 、 混 合 遗 传 算 法
46 fminuncOptions = optimoptions ( @fminunc , ’ Display ’ , ’ i t e r ’ , ’ Algorithm ’ , ’ quasi−newton ’ ) ;
47 options = gaoptimset ( options , ’ HybridFcn ’ ,{ @fminunc , fminuncOptions }) ;%Adding a Hybrid
Function
48 [ x , f v a l ] = ga ( f i t n e s s f c n , nvars , [ ] , [ ] , [ ] , [ ] , LB,UB, FunConstraint , [ ] , o p t i o ns )
49 % 6 、 非 光 滑 目 标 函 数
50 Objfcn = @nonSmoothFcn ;
51 X0 = [ 2 −2];
52 range = [−6 6;−6 6 ] ; % Range used to p l o t the o b j e c t i v e f u n ction
53 rng (0 , ’ t w i st e r ’ ) % Reset the gl o bal random number g e n erator
54 f i g = f i g u r e ( ’ Color ’ , ’w ’ ) ;
55 [ ah , sh ] = showNonSmoothFcn( Objfcn , range ) ;
56 ah . CameraPosition = [ −36.9991 62.6267 2 07 .3 6 2 2 ] ;
57 ah . CameraTarget = [0. 1 059 −1.8145 2 2 . 3 6 6 8 ] ;
58 ah . CameraViewAngle = 6 . 09 2 4;
59 % Plot of the s t a r t i n g point ( not used by the GA s o l v e r )
60 ph = [ ] ;
61 ph(1) = plot3 (X0( 1 ) ,X0( 2 ) , Objfcn (X0) , ’ ob ’ , ’ MarkerSize ’ ,10 , ’ MarkerFaceColor ’ , ’b ’ ) ;
62 % F i l t e r out FaceColor warning
63 ws = warning ( ’ o f f ’ , ’MATLAB: legend : UnsupportedFaceColor ’ ) ;
64 oc = onCleanup (@( ) warning (ws ) ) ;
65 % Add legend to p lot
66 leg endLa bels = { ’Non−d i f f e r e n t i a b l e r egi o ns ’ , ’Smooth re g ion s ’ , ’ S t a r t poin t ’ } ;
67 lh = legend ( [ sh , ph ] , legendLabels , ’ Location ’ , ’ SouthWest ’ ) ;
68 lp = lh . P o sition ;
69 lh . Pos i t i on = [ 0 .0 05 0.005 lp (3) lp ( 4) ] ;
70 FitnessFcn = @nonSmoothFcn ;
71 numberOfVariables = 2;
72 optionsGA = gaoptimset ( ’ PlotFcns ’ , @gaplotbestfun , ’ P l ot I nt e rv a l ’ ,5 , . . .
73 ’ PopInitRange ’ ,[ −5 ; 5 ]) ;
74 [ Xga, Fga ] = ga ( FitnessFcn , numberOfVariables , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , optionsGA)
75 f i g u r e ( f i g )
76 hold on ;
77 ph(2) = plot3 (Xga( 1 ) ,Xga(2) , Fga , ’vm ’ , ’ MarkerSize ’ ,10 , ’ MarkerFaceColor ’ , ’m’ ) ;
78 leg endLa bels = [ legendLabels , ’GA s o l u ti on ’ ] ;
79 lh = legend ( [ sh , ph ] , legendLabels , ’ Location ’ , ’ SouthWest ’ ) ;
80 lp = lh . P o sition ;
81 lh . Pos i t i on = [ 0 .0 05 0.005 lp (3) lp ( 4) ] ;
82 hold o f f ;
83 % 混 合 方 案
84 optHybrid = gaoptimset ( optionsGA , ’ Generations ’ ,15 , ’ P lo t In t er v al ’ , 1 , . . .
85 ’ HybridFcn ’ ,{ @fminunc , optimoptions ( @fminunc , ’OutputFcn ’ , @fminuncOut ) }) ;
http://www.ma-xy.com 26 http://www.ma-xy.com