1+ %% Author
2+ % Author : Federico Giai Pron (federico.giaipron@gmail.com)
3+ % Mail : federico.giaipron@gmail.com
4+ %% Genetic algorithm (GA)
5+ function [Xpbest , ObjFunpbest , Data ] = Optimization_GA_v01(XLim , Sett , Data )
6+ % Initialization
7+ X = zeros(Sett .NumChr ,Sett .LengthX );
8+ Xcbest = zeros(1 ,Sett .LengthX );
9+ Xpbest = zeros(1 ,Sett .LengthX );
10+ ObjFun = zeros(Sett .NumChr ,1 );
11+ ObjFuncbestPlot = zeros(Sett .NumPop ,Sett .NumIter );
12+ switch Sett .Type
13+ case ' min'
14+ ObjFunpbest = + inf ;
15+ ObjFunpbestPlot = + inf * ones(1 ,Sett .NumIter );
16+ case ' max'
17+ ObjFunpbest = - inf ;
18+ ObjFunpbestPlot = - inf * ones(1 ,Sett .NumIter );
19+ end
20+ % Calculation
21+ for IndexPop = 1 : 1 : Sett .NumPop
22+ fprintf(' - IndexPop: %3i out of %3i\n ' ,IndexPop ,Sett .NumPop );
23+ % X initialization
24+ for IndexChr = 1 : 1 : Sett .NumChr
25+ for IndexX = 1 : 1 : Sett .LengthX
26+ X(IndexChr ,IndexX ) = XLim(1 ,IndexX ) + (XLim(2 ,IndexX ) - XLim(1 ,IndexX ))*rand(1 );
27+ end
28+ end
29+ % Iteration
30+ for IndexIter = 1 : 1 : Sett .NumIter
31+ fprintf(' -- IndexIter: %3i out of %3i\n ' ,IndexIter ,Sett .NumIter );
32+ % ObjFun calculation
33+ for IndexChr = 1 : 1 : Sett .NumChr
34+ fprintf(' --- IndexChr: %3i out of %3i\n ' ,IndexChr ,Sett .NumChr );
35+ ObjFun(IndexChr ) = ObjFun_fun(X(IndexChr ,: ),Data );
36+ end
37+ % Natural selection
38+ switch Sett .Type
39+ case ' min'
40+ [~ ,Indexc ] = sort(ObjFun ,' ascend' );
41+ case ' max'
42+ [~ ,Indexc ] = sort(ObjFun ,' descend' );
43+ end
44+ ObjFuncbest = ObjFun(Indexc(1 ));
45+ Xcbest = X(Indexc(1 ),: );
46+ % Xcbest and ObjFuncbest output
47+ fprintf(' -- ObjFuncbest: %-+1.10e,' ,ObjFuncbest );
48+ for IndexX = 1 : 1 : Sett .LengthX
49+ if (IndexX < Sett .LengthX )
50+ fprintf(' Xcbest(%i ) = %-+1.10d,' ,IndexX ,Xcbest(IndexX ));
51+ else
52+ fprintf(' Xcbest(%i ) = %-+1.10d\n ' ,IndexX ,Xcbest(IndexX ));
53+ end
54+ end
55+ % Uniform crossover
56+ for IndexChr = 1 : 1 : Sett .NumChr
57+ for IndexX = 1 : 1 : Sett .LengthX
58+ Coin = round(rand(1 ));
59+ IndexParent = randi(2 );
60+ if (IndexChr ~= Indexc(1 ) && IndexChr ~= Indexc(2 ) && Coin == true )
61+ X(IndexChr ,IndexX ) = X(Indexc(IndexParent ),IndexX );
62+ end
63+ end
64+ end
65+ % Mutation
66+ for IndexX = 1 : 1 : Sett .LengthX
67+ Coin = round(rand(1 ));
68+ if (Coin == true )
69+ X(Indexc(end ),IndexX ) = XLim(1 ,IndexX ) + (XLim(2 ,IndexX ) - XLim(1 ,IndexX ))*rand(1 );
70+ end
71+ end
72+ % Plot
73+ ObjFuncbestPlot(IndexPop ,IndexIter ) = ObjFuncbest ;
74+ switch Sett .Type
75+ case ' min'
76+ if (ObjFuncbest < ObjFunpbestPlot(IndexIter ))
77+ ObjFunpbestPlot(IndexIter ) = ObjFuncbest ;
78+ end
79+ case ' max'
80+ if (ObjFuncbest > ObjFunpbestPlot(IndexIter ))
81+ ObjFunpbestPlot(IndexIter ) = ObjFuncbest ;
82+ end
83+ end
84+ end
85+ % Xpbest and ObjFunpbest determination
86+ switch Sett .Type
87+ case ' min'
88+ if (ObjFuncbest < ObjFunpbest )
89+ Xpbest = Xcbest ;
90+ ObjFunpbest = ObjFuncbest ;
91+ end
92+ case ' max'
93+ if (ObjFuncbest > ObjFunpbest )
94+ Xpbest = Xcbest ;
95+ ObjFunpbest = ObjFuncbest ;
96+ end
97+ end
98+ % Xpbest and ObjFunpbest output
99+ fprintf(' - ObjFunpbest: %-+1.10e,' ,ObjFunpbest );
100+ for IndexX = 1 : 1 : Sett .LengthX
101+ if (IndexX < Sett .LengthX )
102+ fprintf(' Xpbest(%i ) = %-+1.10d,' ,IndexX ,Xpbest(IndexX ));
103+ else
104+ fprintf(' Xpbest(%i ) = %-+1.10d\n ' ,IndexX ,Xpbest(IndexX ));
105+ end
106+ end
107+ end
108+ %% Plot
109+ if (Sett .FlagPlots == true )
110+ figure ;
111+ semilogy(ObjFunpbestPlot ,' LineWidth' ,10 );
112+ for IndexPop = 1 : 1 : Sett .NumPop
113+ hold on ;
114+ semilogy(ObjFuncbestPlot(IndexPop ,: ),' LineWidth' ,1.5 );
115+ title(' Objective function optimization' );
116+ xlabel(' NumIter' );
117+ ylabel(' ObjFun_{c,best}(Pop)' );
118+ grid on ;
119+ end
120+ end
121+ end
0 commit comments