MATLAB Answers

0

Genetic Algorithm taking too long to optimize

Asked by Akbar Raza on 23 Mar 2016
Latest activity Edited by Akbar Raza on 24 Mar 2016
Hi,
I have been trying to build a code to optimize a curve towards an ideal curve using genetic algorithm but the iterations are not going past the 1st generation. However the optimization app says that it is still optimizing. Can someone guide me and check if there is something wrong with my code and suggest ways to speed it up for faster results. As you are aware the stochastic process might not give the best results in the first attempt but might take two or three attempts, the time consumed till then would be too long. Please note that the variables are changing in each for loop iteration therefore the number of variables are 14. I request for some urgent help on this code:
function [Error] = opskinmodel(x)
Einf=x(1);
Es_cole(1)=x(2);
Es_cole(2)=x(3);
Es_cole(3)=x(4);
Es_cole(4)=x(5);
Tau(1)=x(6);
Tau(2)=x(7);
Tau(3)=x(8);
Tau(4)=x(9);
Cond=x(10);
alpha(1)=x(11);
alpha(2)=x(12);
alpha(3)=x(13);
alpha(4)=x(14);
freq=90e9; % Frequency Limit
Eo = 8.854e-12; % Permittivity of free space
g =1:1:(freq/1e9);
CondGabData=xlsread('tissueproperties(final).csv','C:C');% Conductivity from Gabriel's data
PermGabData=xlsread('tissueproperties(final).csv','D:D'); % Permitivity from Gabriel's data
% Cole Cole ----------------------------------------------------------
i=1;
j=sqrt(-1);%imaginary iota
m=1;
Error=0; % Cost Function
PDiff=0;
CDiff=0;
for f =1e9:1e9:(freq)
tmp = 0; % temporary summation variable for higher order value addition
for k=1:4
tmp = tmp + Es_cole(k) / (1 + (j * 2 * pi * (f) * Tau(k)))^(1-alpha(k)); % higher order summation
end
Er_relative(i) = Einf + tmp + Cond/(j * 2 * pi * (f) * Eo);% Complex relative permitivity
Er(i)=real(Er_relative(i));% real part (e') relative permitivity
Ei(i)=-imag(Er_relative(i));% imaginary part (e'') loss factor
Conductivity(i)=Ei(i)*Eo*2*pi*f; % conductivity
i=i+1;
end
%EVALUATION OF COST FUNCTION
for f=1e9:1e9:freq;
lossfactor(m)=CondGabData(m)/(Eo*2*pi*f); % (e'' or lossfactor of Gabriel's data)
PDiff=PDiff+((PermGabData(m)-Er(m))/median(Er))^2;
CDiff=CDiff+((CondGabData(m)-Conductivity(m))/median(Conductivity))^2;
m=m+1;
end
Error=(PDiff+CDiff)/length(g);

  3 Comments

Akbar - are you using the Genetic Algorithm from the Global Optimization Toolbox, or are you using a custom GA? What is your initial population size? How are you invoking the GA with the above optimization function?
Looking at your code, there are several optimizations that come to mind:
  • Pre-size your arrays (i.e. Er_relative, Er, Ei, Conductivity)
  • Remove unneeded calculations (i.e. lossfactor)
  • Don't read from the Excel files on every call to this function. Since (presumably) these files are static (and so are not updated during the run of the GA) then persist the data from these files so that you only read once. See defining persistent variables for details.
Try implementing the above suggestions and see what happens.
Hi Geoff. Thanks for your reply. Well I am using the function handle in the optimization application of global optimization toolbox to initiate the optimization. I am simply using @opskinmodel and mention that I have 14 variables. The lower bounds are set to 0 while I am not setting the upper bounds. # of generations are set to 150 and function tolerance is 1e-8. These settings are giving me very slow results and since the posting of my previous message only 6 iterations have been possible. Please help in resolving this issue.
  • Can you please elaborate on how to presize my arrays?
  • Well I need lossfactor calculation for plotting purpose
Wow it actually worked!!! Now the problem is that my minimum isnt going below 0.445 using GA. No matter which value I change for optimization app, there is no change in error function minimized value. Its like the minimum value it can give is 0.44 and is the equivalent of zero. I have changed every parameter but the result just wont go below 0.44 even in the case of bounded or unbounded constraints. Please help as I am almost there. Do you think is there something wrong with my error function?

Sign in to comment.

1 Answer

Answer by Alan Weiss
on 23 Mar 2016
 Accepted Answer

I think that the best thing to do is to not use ga, but instead use patternsearch, possibly starting from a variety of initial points. You will probably be surprised at the speed and robustness of patternsearch compared to ga.
The other thing to do to speed your code for either ga or patternsearch is NOT have a call to xlsread inside your objective function. Instead, call xlsread once OUTSIDE your objective function, and pass in any data to the objective function using MATLAB syntax.
Alan Weiss
MATLAB mathematical toolbox documentation

  1 Comment

hi Alan. Thanks for replying. But dont you think that there might be some other reason for not going beyond 0.44. I used fmincon as well and several other algorithms in Optimization toolbox but in their case it wasnt going below value of 2. Please guys really need your help in finding the solution. The puzzling thing is that it is a stochastic algorithm but it always give the same minimization value. It is like 0.44 is a constant added to the final minimized solution.

Sign in to comment.