MATLAB Answers

## Genetic algorithm (GA) calculated values compared to measured

Asked by Mattias Larsson

### Mattias Larsson (view profile)

on 19 Mar 2018
Latest activity Commented on by Star Strider

### Star Strider (view profile)

on 22 Mar 2018
Accepted Answer by Star Strider

### Star Strider (view profile)

Hi!
I want to use a number of measured values and try to minimize the differences between the measured values and the calculated values. I calculate the values, with Cb and Lb as inputs.
Rd = 0.1; %0.1;
Rb = 164;
for i=1:num_samples
w = i*pi*2*(10^6);
part1 = (j*w*Lb*Rb);
part2 = (Rb-(w^2)*Cb*Lb*Rb+j*w*Lb);
z_calc(i) = real(Rd + part1/part2);
end
and then taking the difference between the calculated and the measured.
diff(k) = abs(z_calc_n(k)-z_mes(k));
With the differences i can apply a fitness equation
fit = 1/28*sum(diff/max(z_mes));
y = (1/fit)^(-1/3);
I want to achieve a fitness based on two variables Lb and Cb. As a total new user to GA I can't understand how to generally approach this problem in a good manner. However, when i look at the GA function and examples included i see that i can pass a number of constrains. For example non of Rb or Lb can be negative.
How should i approach the problem? Is it solvable?
B.r. Mattias

#### 0 Comments

Sign in to comment.

## 1 Answer ### Star Strider (view profile)

Answer by Star Strider

### Star Strider (view profile)

on 19 Mar 2018
Accepted Answer

It is difficult for me to understand what you are doing, and I do not understand your code.
If your fitting parameters (the parameters you want to adjust to fit your data) are ‘Cb’ and ‘Lb’, your fitness function for the ga function would be something like this:
function y = fitfcn(b, z_mes)
Cb = b(1);
Lb = b(2);
for i=1:num_samples
w = i*pi*2*(10^6);
part1 = (j*w*Lb*Rb);
part2 = (Rb-(w^2)*Cb*Lb*Rb+j*w*Lb);
z_calc(i) = real(Rd + part1/part2);
end
dif(k) = abs(z_calc_n(k)-z_mes(k));
fitcalc = 1/28*sum(dif/max(z_mes));
y = (1/fitcalc)^(-1/3);
end
You would then use ‘fitfcn’ as the fitness function argument to ga, and call it as:
[B, fval] = ga(@(b) fitfcn(b, z_mes), ...);
and will return optimised parameters: ‘B(1)=Cb’ and ‘B(2)=Lb’.
Since ‘diff’ and ‘fit’ are MATLAB function names, I changed them so as not to cause conflicts (known as ‘overshadowing’).
This is the way I would use your code to create a fitness function for the ga function. I used your code as you posted it (and as I understand it). You may need to change it to give the results you want.

#### 4 Comments

Show 1 older comment
Star Strider

### Star Strider (view profile)

on 20 Mar 2018
Thank you!
In my experience, GA works best with a large initial population, although more slowly. Your ‘individuals’ only have two parameters, so I would begin with a random population matrix defined by setting it in optimoptions, for example:
opts = optimoptions( 'ga', 'PopulationSize',500, 'InitialPopulationMatrix',randi(100, 500, 2) );
Choose the limits (first argument) for randi as appropriate to your problem. I invariably get good results with this approach, with the MATLAB ga algorithm.
Constraining the parameters may produce a less accurate outcome than doing an unconstrained optimization. I would first try it without the constraints, and see what the results are. Your data may be different than those used in the published results, so your parameters may be as well. (I do not entirely trust published results, having had problems reproducing published results a few times.)
Mattias Larsson

### Mattias Larsson (view profile)

on 22 Mar 2018
Hi.
Thank you so very much! I have a nice small set-up in Matlab now where I can plot the measured values against the calculated and see the difference. An unconstrained search in this case is probably not so useful since the parameters are for physical electric components and a, for example, negative resistance would not be so useful. However, setting the lower bound to 0 and the upper bound unconstrained gives what looks like an attempt for probably the best looking search pattern so far. Not so good results though, see below.  The parameters for that search gives a flat line, which is not so close to the somewhat exponential measured results. Searching with the constrains given by the paper doesn’t give much as search-looking pattern, see below.  It seems that the search has problems matching the almost linear part (1-10 (Mhz) - x-axis) of the measured results. Is it possible to spread the search further or should 500 in populationsize be sufficient for most standard problems? It could also be that there is no better match to so that the linear part cam be fulfilled as well.
Star Strider

### Star Strider (view profile)

on 22 Mar 2018
As always, my pleasure!
A negative resistance would be very useful, just not physically possible due to the constraints of thermodynamics.
An initial population of 500 usually works for me. You can certainly try a larger initial population.
A constrained optimization will never provide results as good as an unconstrained optimization, although it is frequently required to make physical sense.
It is difficult for me to assess your fitness function, since I have no idea what your data are. I usually use a metric analogous to norm(z_mes - z_calc) as the fitness metric, since that would minimise the difference in a least-squares sense, and always produces a non-negative result. I would assess what your fitness function is producing, to see if that could be a problem. For example, is it giving the ‘weight’ to all data that you want it to, or is it fitting one section at the expense of another?
Without knowing more about what your data and model are, my first approach would be to assess the fitness function (that may have changed since you posted it) to be certain it is doing what you want it to do.

Sign in to comment.