From now on, I am going to share the FOA source codes of MATLAB to all of you.
There are two topics:
One is optimization with/without constraints and the other one is (stochastic) optimal control with/without constraints.
1. Optimization without constraints.
2. Optimization with constraints.
3. Deterministic optimal control with/without constraints.
4. Stochastic optimal control with/without constraints.
1. Optimization without constraints: QP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Pan's Original 2D-FOA
%% EMA Economic Department, Soochow University, Taipei,Taiwan
%
% Copyright by W-T Pan (2011)
% Revised by W-Y Lin (2011)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Topic: How to find the max value of a quadratic function
% Clear the operating environment.
tic
clc
clear
% Randomize the initial Drosophila population positions.
X_axis=10*rand();
Y_axis=10*rand();
% Set parameters
maxgen=100; % No.of iterations
sizepop=20; % Population size
% Start the FOA: Flies use the sense of smell to find food
for i=1:sizepop
% The Drosophila uses its olfactory to search the food
% by random direction and distance
X(i)=X_axis+2*rand()-1;
Y(i)=Y_axis+2*rand()-1;
% Due to the fly cannot find the exact location of the prey, so we first estimate the distance from the origin (Dist).
% And then calculate the flavor concentration determination value (S), it is the inverse of the distance.
D(i)=(X(i)^2+Y(i)^2)^0.5;
S(i)=1/D(i);
% Concentration determination value (S) is substituted into the fitness function, i.e. the concentration of the flavor (Smelli) of the flies.
Smell(i)=7-S(i)^2;
end
% Identify the highest concentration values of this fruit fly Drosophila groups (find the maximum value).
[bestSmell bestindex]=max(Smell);
% Retain the best Concentration values and best x, y coordinates of the flies
X_axis=X(bestindex);
Y_axis=Y(bestindex);
Smellbest=bestSmell;
% Start the Drosophila iterative optimization
for g=1:maxgen
for i=1:sizepop
X(i)=X_axis+2*rand()-1;
Y(i)=Y_axis+2*rand()-1;
D(i)=(X(i)^2+Y(i)^2)^0.5;
S(i)=1/D(i);
Smell(i)=7-S(i)^2;
end
[bestSmell bestindex]=max(Smell);
% Determine whether the concentration value is greater than the previous one.
% If so, the best value and its location of the fly is retained.
% Then all flies utilize their visual to find this best position.
if bestSmell>Smellbest
X_axis=X(bestindex);
Y_axis=Y(bestindex);
Smellbest=bestSmell;
end
% Record the optimal Smell value of each generation to yy array.
% Record the coordinates of the optimal iterations
yy(g)=Smellbest;
Xbest(g)=X_axis;
Ybest(g)=Y_axis;
% *** Draw the optimal concentration values and flight path for every iteration
figure(1)
plot(yy)
grid on;
title('Optimization process 7-X^2','fontsize',14)
xlabel('Iteration Number','fontsize',12);ylabel('Smell','fontsize',14);
figure(2)
plot(Xbest,Ybest,'b.');
grid on;
title('Fruit fly flying route 7-X^2','fontsize',14)
xlabel('X-axis','fontsize',12);ylabel('Y-axis','fontsize',12);
pause(0.5)
end
toc
Simulated results:
Here X>=0 (constraint), Optimal value (Smell) = 7.
Reference:
1. Pan, W.-T. (2011). Fruit Fly Optimization Algorithm. Taiwan: Tsang Hai Book Publishing Co., ISBN 978-986-6184-70-3. (in chinese).
2. Nien Benjamin (2011) Application of Data Mining and Fruit Fly Optimization Algorithm to Construct Financial Crisis Early Warning Model – A Case Study of Listed Companies in Taiwan, Master Thesis, Department of Economics, Soochow University, Taiwan (in chinese), Adviser: Wei-Yuan Lin.
Jing Si Aphorism:
Be honest and truthful in everything you do.
Be gentle and forgiving in your
relationships with others
Soochow University EMA