Friday, October 24, 2014

How to improve the Pan's FOA program

 

 

 

Question from Universiti Malaysia  below: 

Dear Dr Wei-Yuan Lin

 My name is kamal from Universiti Malaysia Pahang. I am trying to
understand your Fruit fly Optimization Algorithm  and implement it to
solve the t-way testing problem.

I would really appreciate  if you could send in sample source code
especially in Java if you have one. FYI, I have downloaded all your
papers.  But still having hard time to understand especially on how to
out in the objective function based on the smell.

I have got this simple quadratic problem and intend to find the x that
gives minimum .. still unable to solve it using FOA

f(x) = (x-10)(x-2)


Ans: you are going to find the min  instead of  the max.

The program are modified as follow:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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 Min. 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=500;  % No.of iterations
sizepop=100;  % 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;
Smell(i)= (S(i)-10)*(S(i)-2);

end

% Identify the highest concentration values of this fruit fly Drosophila groups (find the maximum value).

[bestSmell bestindex]=min(Smell); % modified

% 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)= (S(i)-10)*(S(i)-2);


end

[bestSmell bestindex]=min(Smell); % modified

% 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  % modified
X_axis=X(bestindex);
Y_axis=Y(bestindex);
Smellbest=bestSmell;
S_best=S(bestindex);

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;
end

% *** 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)

S_best

% yy
Smellbest
toc
%%******************************************

Simulated Outputs:

S_best =     5.9991

Smellbest =   -16.0000

Elapsed time is 0.441575 seconds.

%%*********************************

 More than one variable case:

Dear kamal :

You cannot find the minimum value of f(x)=(x1-10)(x2-2) by any method, including FOA. It does not exit. You can try the other function.

First, I will give you the modified Pan's Matlab FOA program below, please find the defect of it. Then Email to me. I will offer you a right program to solve it.
 
Pan's 3D-FOA Porgram:

%*****************************************************************
% Pan's  3D-FOA
% EMA Economic Department, Soochow University, Taipei,Taiwan
% Copyright by W-T Pan (2011)
% Revised by W-Y Lin (2011)
%******************************************************************
%  A Defect QP for Two Variables
 % Finding Min  Smell(i)=f(x1,x2)=(x1(i)-10)^2+(x2(i)-10)^2;
 % Optimal solution is x1*=10, x2*=10, f*(x1,x2)=0

%***
tic
clc
clear

pp=20;
X_axis=pp*rand();
Y_axis=pp*rand();
Z_axis=pp*rand();

%
maxgen=500;
sizepop=100; 

%***
for i=1:sizepop

%%***
X(i)=X_axis+2*rand()-1;
Y(i)=Y_axis+2*rand()-1;
Z(i)=Z_axis+2*rand()-1;

%***
D(i,1)=(X(i)^2+Y(i)^2+Z(i)^2)^0.5;
D(i,2)=(X(i)^2+Y(i)^2+Z(i)^2)^0.5;
x1(i)=1/D(i,1);
x2(i)=1/D(i,2);

%***
%
% f(x)=(x*x)-(12*x)+20) = (x-10)(x-2)
% unbounded solution
 % Smell(i)=(x1(i)-10)^2+(x2(i)-2)^2;

 Smell(i)=(x1(i)-10)^2+(x2(i)-10)^2;
end
%***
[bestSmell bestindex]=min(Smell);

%***
X_axis=X(bestindex);
Y_axis=Y(bestindex);
Z_axis=Z(bestindex);
Smellbest=bestSmell;

% add xValueBest,yValueBest
xValueBest=x1(bestindex);
yValueBest=x2(bestindex);

%***
for g=1:maxgen   
%***
  for i=1:sizepop
 
  X(i)=X_axis+2*rand()-1;
  Y(i)=Y_axis+2*rand()-1;
  Z(i)=Z_axis+2*rand()-1;
 
  
  D(i,1)=(X(i)^2+Y(i)^2+Z(i)^2)^0.5;
  D(i,2)=(X(i)^2+Y(i)^2+Z(i)^2)^0.5;

%***
  
   x1(i)=1/D(i,1);
   x2(i)=1/D(i,2);
  
   Smell(i)=(x1(i)-10)^2+(x2(i)-10)^2;
  
  end
 
  %***
  [bestSmell bestindex]=min(Smell);
  %***
 
if bestSmell<Smellbest
    X_axis=X(bestindex);
    Y_axis=Y(bestindex);
    Z_axis=Z(bestindex);
    Smellbest=bestSmell;

% add xValueBest,yValueBest
    xValueBest=x1(bestindex);
    yValueBest=x2(bestindex);
  
   end

%***
    optimalobj(g)=Smellbest;
    Xbest(g)=X_axis;
    Ybest(g)=Y_axis;
    Zbest(g)=Z_axis;
   
    xVBest(g)=xValueBest;
    yVBest(g)=yValueBest;
  
end

%***
figure(1)

subplot(2,2,1)
plot(optimalobj);
grid on;
title(' QP function ','fontsize',14)
xlabel('Evolution','fontsize',12);ylabel('Objective Function','fontsize',12);

subplot(2,2,2)
plot3(Xbest,Ybest,Zbest,'b.');
grid on;
title('Fruit fly flying route','fontsize',14)
xlabel('X-axis','fontsize',12);ylabel('Y-axis','fontsize',12);zlabel('Z-axis','fontsize',12);

subplot(2,2,3)
plot(xVBest,'b.')
grid on;
title('x1','fontsize',14)
xlabel('Evolution','fontsize',12);ylabel('Value','fontsize',12);

subplot(2,2,4)
plot(yVBest,'b.')
grid on;
title('x2','fontsize',14)
xlabel('Evolution','fontsize',12);ylabel('Value','fontsize',12);

yValueBest
xValueBest
optimalobj
toc

%%**********************************************************************

Reference: 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:

To give with joy is to help others with a happy mood

Soochow University EMA
 

1 comment: