GA fitness function with variables in a summation

What is the best way to define the following fitness function for GA optimization?
where are the variables to be found with GA-toolbox. N can be a positive integer.

댓글 수: 2

What is y?
How large is N and how is it defined?
Nima
Nima 2022년 11월 8일
I just made a change in the formula! So ist frequency dependent impedance and as an example.

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2022년 11월 8일
편집: Star Strider 2022년 11월 8일
I still do not completely understand what you want to do.
Perhaps this —
Z = randn(16,1) + 1j*randn(16,1); % Creeate Data
omega = (0:15).'; % Creeate Data
objfcn = @(b,x) b(1) + 1i.*x.*b(2) + 1./(b(3) + 1j.*x.*b(4)); % Objective Function
Parms = 4; % Number Of Parameters To Be Estimated
ftnsfcn = @(b) norm(Z - objfcn(b,omega)); % Fitness Function
[B,fval,exitflag,output,population,scores] = ga(ftnsfcn, Parms, [],[],[],[], zeros(1,Parms)); % Genetic Algorithm Call
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf('\nR\t= %15.3f\nL\t= %15.3f\nG\t= %15.3f\nC\t= %15.3f\n',B)
R = 0.000 L = 0.007 G = 1.275 C = 0.760
fprintf('\nFinal Fitness Value = %15.6f\n',fval)
Final Fitness Value = 6.843630
fprintf('\nGenerations = %6d\n', output.generations)
Generations = 157
fprintf('\nMessage: %s\n',output.message)
Message: Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
figure
plot(omega, real(Z), 'pm', 'DisplayName','Re(Z)')
hold on
plot(omega, imag(Z), 'pc', 'DisplayName','Im(Z)')
plot(omega, real(objfcn(B,omega)), '-m', 'DisplayName','Re(Z_{est})')
plot(omega, imag(objfcn(B,omega)), '-c', 'DisplayName','Im(Z_{est})')
hold off
grid
legend('Location','best')
I do not understand the reason for the summation in the objective funciton, so I do not use it here.
It may be necessary to run ga a few times to get the best fit (save the results each time), however it should be possible to get a decent fit to your data.
EDIT — Corrected typographical errors.
.

댓글 수: 4

Nima
Nima 2022년 11월 8일
thanks for your effort! Maybe you need more details about the problem:
each set of are the components of a circuit. means that there are 16 parallel circuits and these values must be calculated (64 parameters).
In fact, the objective function to be minimized is the following: , where K is the length of ω and is known and has the same dimension as .
The question is how to define the fitness function that can calculate these 64 parameters (16*4)?
This runs without error, however it has become something of a moving target, so I leave it to you to correct any differences between the objective function I wrote and the one you want. See the documentation on the norm function for details.
The ‘objfcn’ function reshapes the matrix so that the columns are the values of R, L, G and C in order, and the rows are each circuit. It then iterates through all 16 rows and sums the results at the end, then transposes that to fit the data.
It may be necessary for you to use an 'InitialPopulationMatrix' to create the correct ranges for the variables. For example is R is in , L in , and C in (I have no idea what G woiuld be), it could be something like this:
format shortE
IPM = randi(1E+4, 5, 4) .* [1 1E-9 1E-3 1E-15]
IPM = 5×4
1.0e+00 * 5.1210e+03 6.5960e-06 9.4080e+00 8.4710e-12 5.9620e+03 6.8550e-06 4.2310e+00 5.7960e-12 9.3860e+03 4.1560e-06 9.0080e+00 5.3650e-12 4.1920e+03 9.8970e-06 3.9130e+00 4.1800e-12 2.0940e+03 7.5560e-06 5.1570e+00 4.6060e-12
format shortG
This scales the matrix appropriately, making convergence more likely.
I limited the number of generations to 5 so it would run here. Change that (and any other options you want to use) to do what you want.
Try this —
Ndata = 11;
Z = randn(Ndata,1) + 1j*randn(Ndata,1); % Creeate Data
omega = (0:Ndata-1).'; % Creeate Data
% objfcn = @(b,x) b(1) + 1i.*x.*b(2) + 1./(b(3) + 1j.*x.*b(4)); % Objective Function
opts = optimoptions('ga', 'MaxGenerations',5); % Change This
Parms = 64; % Number Of Parameters To Be Estimated
ftnsfcn = @(b) norm(Z - objfcn(b,omega)/Ndata); % Fitness Function
[B,fval,exitflag,output,population,scores] = ga(ftnsfcn, Parms, [],[],[],[], zeros(1,Parms),[],[],[],opts); % Genetic Algorithm Call
Optimization terminated: maximum number of generations exceeded.
fprintf('\nR\t= %15.3f\nL\t= %15.3f\nG\t= %15.3f\nC\t= %15.3f\n',B)
R = 18.198 L = 4.352 G = 16.301 C = 19.328 R = 7.640 L = 0.963 G = 13.551 C = 12.728 R = 3.636 L = 9.410 G = 3.550 C = 14.360 R = 4.090 L = 0.113 G = 1.689 C = 0.301 R = 19.972 L = 6.039 G = 11.053 C = 6.713 R = 16.999 L = 0.693 G = 11.361 C = 1.642 R = 3.259 L = 2.534 G = 3.123 C = 8.141 R = 17.178 L = 0.965 G = 19.393 C = 10.615 R = 4.370 L = 5.042 G = 8.982 C = 17.368 R = 18.659 L = 1.798 G = 1.602 C = 0.663 R = 16.729 L = 2.769 G = 11.482 C = 16.732 R = 4.066 L = 0.756 G = 19.334 C = 6.838 R = 0.998 L = 2.161 G = 1.286 C = 17.267 R = 11.470 L = 6.461 G = 4.122 C = 15.188 R = 15.062 L = 0.271 G = 15.560 C = 2.741 R = 5.511 L = 4.545 G = 12.820 C = 17.735
fprintf('\nFinal Fitness Value = %15.6f\n',fval)
Final Fitness Value = 101.409210
fprintf('\nGenerations = %6d\n', output.generations)
Generations = 5
fprintf('\nMessage: %s\n',output.message)
Message: Optimization terminated: maximum number of generations exceeded.
figure
plot(omega, real(Z), 'pm', 'DisplayName','Re(Z)')
hold on
plot(omega, imag(Z), 'pc', 'DisplayName','Im(Z)')
plot(omega, real(objfcn(B,omega)), '-m', 'DisplayName','Re(Z_{est})')
plot(omega, imag(objfcn(B,omega)), '-c', 'DisplayName','Im(Z_{est})')
hold off
grid
legend('Location','best')
function zv = objfcn(b,x)
mb = reshape(b, 4, []).';
for k = 1:16
z(k,:) = mb(k,1) + 1i.*x.*mb(k,2) + 1./(mb(k,3) + 1j.*x.*mb(k,4));
end
zv = sum(z).';
% Qzzv = size(zv)
end
.
Nima
Nima 2022년 11월 10일
Thank you so much!
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2022년 11월 8일

댓글:

2022년 11월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by