Development of an optimization function
Show older comments
Hello everyone,
I am working on the minimization of losses in the flow of the electrical network, In this context I will use the Firefly algorithm and I took the example below to understand its operating principle, but I get stuck on the line who asks me the fitness function (I tried with the optimization function Matlab ackleyfcn).
function FFA
clear all
clc
N=input('Please Enter the Number of Firefly: ');
clc
maxGen=input('Please Provide the Stopping Criteria: ');
clc
alpha=input('Please Enter the value of alpha: ');
clc
Beta0=input('Please enter the Initial attractiveness: ');
clc
gamma=input('Please Provide the Absorption Coefficient (gamma): ');
clc
fitness=input('Select the test function: ');
clc
FIREFLY=input('Please Enter the Population of the FIREFLY: or\nPress zero(0) for the default population: ');
clc
if FIREFLY==0
pop=rand(N);
else
pop=FIREFLY;
end
popi=pop;
popj=rand(N);
Ii=objfunc(fitness,popi);
%this reprsent the light intesity of(pop)
%this reprsent the light intesity of(pop)
Ij=objfunc(fitness,popj);
%Ij=fitness+popj;
itr=0;
while itr<maxGen
itr=itr+1;
for i=1:N
for j=1:N
r=sqrt((popi(i)-popi(j))^2+(popj(i)-pop(j))^2);
if Ii<Ij
beta=Beta0*exp(-gamma*r.^2);
Ii=popj(i).*(1-beta)+popj(j).*beta+alpha.*(rand-0.5);
end
beta=Beta0*exp(-gamma*r.^2);
popi(i)=popi(i).*(1-beta)+popi(j).*beta+alpha.*(rand-0.5);
popj(i)=popj(i).*(1-beta)+popj(j).*beta+alpha.*(rand-0.5);
Iinew=objfunc(fitness,popi(i)); Ijnew=objfunc(fitness,popj(i));
end
end
if Iinew<Ijnew
Best_Soln=Iinew;
else Best_Soln=Ijnew;
end
end
disp(Best_Soln)
thank you in advance
3 Comments
Walter Roberson
on 13 Apr 2018
Having "clear all" in code is rather like Wile E. Coyote exploding the bridge underneath himself and expecting to stay safe in mid-air as long as he does not look down.
jelol sadf
on 13 Apr 2018
Walter Roberson
on 13 Apr 2018
If you were to create a chip-level clone of your computer and a byte-level copy of your hard drive, then someone might be able to figure out exactly how your system would react to exploding the currently executing function. But short of that... Nope, not gonna happen. The byte fragments are going to land where-ever they land, and if they crash your MATLAB or mess up your program in some really strange way then it's nothing we can predict.
Answers (1)
Walter Roberson
on 14 Apr 2018
We do not know what objfunc() is, and what data types it expects so we cannot tell you what to put in for fitness . It might have to be a quoted string; or perhaps it should be an anonymous function. But we do not know how many arguments it should take.
It appears from the code that objfunc() expect to be able to be passed either individual scalar doubles or else square matrices.
rand(N) is an N x N matrix, so both popi and popj can be N x N matrices. But your nested for loop accesses popi(i) and popi(j) and popj(i) and popj(j) which is at most the first column of the N x N matrix. Either your for loop is incorrect or else your random initialization is wrong.
It would help if the input prompts gave some hints as to expected sizes.
Also,
Iinew=objfunc(fitness,popi(i)); Ijnew=objfunc(fitness,popj(i));
Those overwrite all of Iinew and Ijnew in each iteration of the double nested for loops, and those variables are not otherwise used in the loop so they are not being constructed iterative. The end result is going to be whatever was set in the last iteration of the loops, when both i and j equal n. If that is what is desired then there is no point in calculating them inside the loops.
2 Comments
jelol sadf
on 14 Apr 2018
Walter Roberson
on 14 Apr 2018
objfunc = @(fun, x) fun(x);
fitness = @(x) (max(x) + min(x)) * coth(mean(x)); %a proposed fitness function
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!