Asked by Akbar Raza
on 23 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);

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

Akbar Raza
on 24 Mar 2016

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Geoff Hayes (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/274996-genetic-algorithm-taking-too-long-to-optimize#comment_352461

## Akbar Raza (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/274996-genetic-algorithm-taking-too-long-to-optimize#comment_352483

## Akbar Raza (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/274996-genetic-algorithm-taking-too-long-to-optimize#comment_352507

Sign in to comment.