Skip to content

Commit cf1794d

Browse files
authored
Add files via upload
0 parents  commit cf1794d

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed

Main.m

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
clear all
2+
close all
3+
clc
4+
%% Author
5+
% Author : Federico Giai Pron
6+
% Mail : federico.giaipron@gmail.com
7+
% Avilability : support, projects / thesis development, script /
8+
% codes / controls writing, etc.
9+
% Experience : automotive, controls, modelling, finite element method,
10+
% optimization, etc.
11+
%% Input
12+
% Strategy
13+
% 1: 1D minimization test case
14+
% 2: 2D minimization test case
15+
% 3: 1D maximization test case
16+
% 4: 2D maximization test case
17+
Data.Function = 4;
18+
% Problem size
19+
switch Data.Function
20+
case 1
21+
Sett.Type = 'min';
22+
Sett.LengthX = 1;
23+
case 2
24+
Sett.Type = 'min';
25+
Sett.LengthX = 2;
26+
case 3
27+
Sett.Type = 'max';
28+
Sett.LengthX = 1;
29+
case 4
30+
Sett.Type = 'max';
31+
Sett.LengthX = 2;
32+
end
33+
% Optimization variables limits
34+
XLim(1,:) = -1000*ones(1,Sett.LengthX);
35+
XLim(2,:) = +1000*ones(1,Sett.LengthX);
36+
% Settings
37+
Sett.NumPop = 5;
38+
Sett.NumChr = 5;
39+
Sett.NumIter = 2000;
40+
Sett.FlagPlots = true;
41+
%% Run GA
42+
[Results.Xpbest,Results.ObjFunpbest,Data] = Optimization_GA_v01(XLim, Sett, Data);
43+
%% Plot
44+
run('MainPlot.m');

MainPlot.m

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
%% Author
2+
% Author : Federico Giai Pron (federico.giaipron@gmail.com)
3+
% Mail : federico.giaipron@gmail.com
4+
%% Plot
5+
% 1D
6+
if(Data.Function == 1 || Data.Function == 3)
7+
NPlot = 250;
8+
XPlot = linspace(XLim(1,1),XLim(2,1),NPlot);
9+
ObjFunPlot = zeros(1,NPlot);
10+
for IndexPlot = 1:1:NPlot
11+
ObjFunPlot(IndexPlot) = ObjFun_fun(XPlot(IndexPlot),Data);
12+
end
13+
figure;
14+
plot(XPlot,ObjFunPlot,'Linewidth',1.5);
15+
hold on;
16+
plot(Results.Xpbest,Results.ObjFunpbest,'ro','Linewidth',5,'MarkerFaceColor','r');
17+
title('Optimum point');
18+
xlabel('X');
19+
ylabel('ObjFun');
20+
grid on;
21+
end
22+
% 2D
23+
if(Data.Function == 2 || Data.Function == 4)
24+
NPlot = 50;
25+
XPlot1 = linspace(XLim(1,1),XLim(2,1),NPlot);
26+
XPlot2 = linspace(XLim(1,2),XLim(2,2),NPlot);
27+
ObjFunPlot = zeros(NPlot,NPlot);
28+
for IndexPlot1 = 1:1:NPlot
29+
for IndexPlot2 = 1:1:NPlot
30+
ObjFunPlot(IndexPlot1,IndexPlot2) = ObjFun_fun([XPlot1(IndexPlot1),XPlot2(IndexPlot2)],Data);
31+
end
32+
end
33+
figure;
34+
surf(XPlot1,XPlot2,ObjFunPlot);
35+
hold on;
36+
scatter3(Results.Xpbest(1),Results.Xpbest(2),Results.ObjFunpbest,150,'filled','r');
37+
title('Optimum point');
38+
xlabel('X_{1}');
39+
ylabel('X_{2}');
40+
zlabel('ObjFun');
41+
colorbar;
42+
grid on;
43+
view([3,3,6]);
44+
end

ObjFun_fun.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%% Author
2+
% Author : Federico Giai Pron (federico.giaipron@gmail.com)
3+
% Mail : federico.giaipron@gmail.com
4+
%% Objective function
5+
function [ObjFun] = ObjFun_fun(X, Data)
6+
switch Data.Function
7+
case 1
8+
ObjFun = +(X-27)^2+25;
9+
case 2
10+
ObjFun = +(X(1)-50)^2+(X(2)-25)^2+100;
11+
case 3
12+
ObjFun = -(X-27)^2-25;
13+
case 4
14+
ObjFun = -(X(1)-50)^2-(X(2)-25)^2-100;
15+
end
16+
end

Optimization_GA_v01.m

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)