Issues in Patternsolve and ParticleSwarm Optimzation

18 次查看(过去 30 天)
So a few months ago I asked some help here for fitting a really complicated function and some really helpful people in this community helped me solve the problem using genetic algorithm. Now I just want to try to fit these use other algorithms (like pattern search, particle swarm etc.) and see how they perform too.
So for starters, here is the function with 9 parameters where V represents the voltage and a’s represent the fit parameters.
Theta= @(a,V) a(5).*V+a(6).*atan(a(7)+a(8).*V+a(9).*V.^2)
F= @(a,V) a(1).*(a(2) - a(3) - (a(2).*a(4))./(a(2).^2.*cos(Theta(a,V)).^2 + a(4).^2.*sin(Theta(a,V)).^2).^(1/2));
The code that used genetic algorithm to fit the data well was (Fitness value of 0.3):
inistart=10;
V=volt(inistart:length(volt));
volt=V;
Phase=Phase(inistart:length(Phase))
ftns= @(a) norm(Phase-F(a,V))
PopSz = 100;
Parms =9;
optsAns = optimoptions('ga', 'PopulationSize',PopSz, 'MaxGenerations',5E3, 'FunctionTolerance',1E-10);
t0 = clock;
fprintf('\nStart Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t0)
for i=1:20
[a_fit{i},fval,exitflag,output,population,scores]=ga(ftns,Parms, [],[],[],[],[0; 1.4;-Inf;1.40;-Inf;-Inf;-Inf;-Inf;-Inf],[8e+04; 1.7;Inf;1.7;Inf;Inf; Inf;Inf;Inf],[],[],optsAns);
t1 = clock;
fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
GA_Time = etime(t1,t0)
DT_GA_Time = datetime([0 0 0 0 0 GA_Time], 'Format','HH:mm:ss.SSS');
fprintf('\nElapsed Time: %23.15E\t\t%s\n\n', GA_Time, DT_GA_Time)
fprintf('Fitness value at convergence = %.4f\nGenerations \t\t\t\t = %d\n\n',fval,output.generations)
fprintf(1,'\tRate Constants %d:\n',i)
for k1 = 1:numel(a_fit{i})
fprintf(1, '\t\ta(%2d) = %8.5f\n', k1, a_fit{i}(k1))
end
figure
plot(V, Phase, '.b')
hold on
plot(V, F(a_fit{i},V), '-r')
hold off
grid
xlabel('V')
ylabel('Phase')
title('GA Parameters')
end
What does not work?
But just to see how the other algorithms do, I tried the following (For example the patternsolve algorithm):
optsAns = optimoptions('patternsearch','FunctionTolerance',1e-10,'MaxIterations',5e3);
t0 = clock;
fprintf('\nStart Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t0)
for i=1:20
[a_fit{i},fval,exitflag,output]=patternsearch(ftns,[8e+04; 1.7;10000;1.7;10000;10000;10000;10000;10000],[],[],[],[],[0; 1.4;-Inf;1.40;-Inf;-Inf;-Inf;-Inf;-Inf],[8e+04; 1.7;Inf;1.7;Inf;Inf; Inf;Inf;Inf],[],optsAns);
t1 = clock;
fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
GA_Time = etime(t1,t0)
DT_GA_Time = datetime([0 0 0 0 0 GA_Time], 'Format','HH:mm:ss.SSS');
fprintf('\nElapsed Time: %23.15E\t\t%s\n\n', GA_Time, DT_GA_Time)
fprintf('Fitness value at convergence = %.4f\nGenerations \t\t\t\t = %d\n\n',fval)
fprintf(1,'\tRate Constants %d:\n',i)
for k1 = 1:numel(a_fit{i})
fprintf(1, '\t\ta(%2d) = %8.5f\n', k1, a_fit{i}(k1))
end
figure
plot(V, Phase, '.b')
hold on
plot(V, F(a_fit{i},V), '-r')
hold off
grid
xlabel('V')
ylabel('Phase')
title('GA Parameters')
temp(i)=fval
end
I always get a fitness value as 25.7862 all the 20 times I loop this code. And the fit looks like this:
Particle swarm also gives bad results: 29.7108 30.8890 49.0237 30.8896 49.0237 49.0237 49.0237 49.0237 30.8896 49.0237 49.0237 30.8895 30.8894 49.0237 49.0237 30.4015 25.3389 30.8666 49.0237 30.8896 as the fitness values.
PopSz = 100;
Parms =9;
optsAns = optimoptions('particleswarm','FunctionTolerance',1e-10,'MaxIterations',5e3,'HybridFcn','fmincon');
t0 = clock;
fprintf('\nStart Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t0)
for i=1:20
[a_fit{i},fval,exitflag,output]=particleswarm(ftns,Parms,[0; 1.4;-Inf;1.40;-Inf;-Inf;-Inf;-Inf;-Inf],[8e+04; 1.7;Inf;1.7;Inf;Inf; Inf;Inf;Inf],optsAns);
t1 = clock;
fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
GA_Time = etime(t1,t0)
DT_GA_Time = datetime([0 0 0 0 0 GA_Time], 'Format','HH:mm:ss.SSS');
fprintf('\nElapsed Time: %23.15E\t\t%s\n\n', GA_Time, DT_GA_Time)
fprintf('Fitness value at convergence = %.4f\nGenerations \t\t\t\t = %d\n\n',fval)
fprintf(1,'\tRate Constants %d:\n',i)
for k1 = 1:numel(a_fit{i})
fprintf(1, '\t\ta(%2d) = %8.5f\n', k1, a_fit{i}(k1))
end
figure
plot(V, Phase, '.b')
hold on
plot(V, F(a_fit{i},V), '-r')
hold off
grid
xlabel('V')
ylabel('Phase')
title('GA Parameters')
temp(i)=fval
end
Can you help me understand why I get this?
Thanks in Advance!
  1 个评论
Alex Sha
Alex Sha 2024-1-9
Hi, refer to the results below, multi-solutions. Matlab GA or PSO is not easy to obtain such result
Sum Squared Error (SSE): 0.0372804765881514
Root of Mean Square Error (RMSE): 0.0166797035900485
Correlation Coef. (R): 0.999982445829172
R-Square: 0.999964891966492
Parameter Best Estimate
--------- -------------
a1 -0.0412514328360699
a2 -271.691129419169
a3 -361.400490920017
a4 -20.7789698301179
a5 0.00201615721161083
a6 0.969640435459053
a7 2.14250046959902
a8 -10.1074996911561
a9 11.4388408744042
or:
Parameter Best Estimate
--------- -------------
a1 -0.0127221786756855
a2 880.956285735209
a3 590.079842553096
a4 67.3695361874826
a5 0.00201595859852817
a6 -0.969641700941842
a7 -2.14281229346878
a8 10.1085639749314
a9 -11.4397708378142

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by