Unable to solve using partical swarm

3 次查看(过去 30 天)
Salah
Salah 2023-1-27
回答: Kartik 2023-5-15
%% I was using G.A. to optimize this problem, and i wanted to see which is better algorithm compared with PSO
%% I used the same variable initial point etc.
%% i changed the (Set nondefault solver options) and (Solve problem) to change it to particle swarm optimization
%% i had this error
% Create optimization variables
Lc3 = optimvar("Lc","LowerBound",1e-9,"UpperBound",1e-2);
Cc3 = optimvar("Cc","LowerBound",1e-9,"UpperBound",1e-2);
Lf3 = optimvar("Lf","LowerBound",1e-9,"UpperBound",1e-2);
Cf3 = optimvar("Cf","LowerBound",1e-9,"UpperBound",1e-2);
f3 = optimvar("f","LowerBound",1e3,"UpperBound",15e4);
% Set initial starting point for the solver
initialPoint2.Lc = repmat(1e-9,size(Lc3));
initialPoint2.Cc = repmat(1e-9,size(Cc3));
initialPoint2.Lf = repmat(1e-9,size(Lf3));
initialPoint2.Cf = repmat(1e-9,size(Cf3));
initialPoint2.f = repmat(1e3,size(f3));
% Create problem
problem = optimproblem;
% Define problem objective
problem.Objective = fcn2optimexpr(@objectiveFcn,Lc3,Cc3,Lf3,Cf3,f3);
% Define problem constraints
problem.Constraints = constraintFcn(Cf3,f3,Lf3);
% Set nondefault solver options
options2 = optimoptions("particleswarm","Display","iter","PlotFcn","pswplotbestf");
% Display problem information
show(problem);
OptimizationProblem : Solve for: Cc, Cf, Lc, Lf, f minimize : objectiveFcn(Lc, Cc, Lf, Cf, f) subject to : (Cf - (380 ./ (((2 .* f) .* 6) .* 14))) == 0 (Lf - (91.2 ./ ((2 .* f) .* 12))) == 0 variable bounds: 1e-09 <= Cc <= 0.01 1e-09 <= Cf <= 0.01 1e-09 <= Lc <= 0.01 1e-09 <= Lf <= 0.01 1000 <= f <= 150000
% Solve problem
rng('default')
[solution,objectiveValue,reasonSolverStopped] = solve(problem,initialPoint2,...
"Solver","particleswarm","Options",options2);
Error using optim.internal.problemdef.ProblemImpl/prob2structImpl
Unable to solve using 'particleswarm'. Do not specify a solver or specify a compatible solver such as 'fmincon'.

Error in optim.internal.problemdef.ProblemImpl/solveImpl

Error in optim.problemdef.OptimizationProblem/solve
% Display results
solution
reasonSolverStopped
objectiveValue
% Clear variables
clearvars Lc3 Cc3 Lf3 Cf3 f3 initialPoint2 options2 reasonSolverStopped...
objectiveValue
function objective = objectiveFcn(Lc,Cc,Lf,Cf,f)
Vo=500;
R=6;
DutyC=0.76;
s=tf('s');
dp = 1-DutyC;
w1 = -Lc;
w2 = dp*R;
u1 = Lc*Cc*R;
u2 = Lc;
u3 = (dp^2)*R;
x1 = Lf*Cf*(Lc^2)*dp*R*Cc;
x2 = ((Lf+(Lc*dp*R)-((dp^2)*R*Lf*Cf))*Lc*Cc)+((Lc^2)*Lf*Cf*dp*R);
x3 = ((Lf+(Lc*dp*R)-((dp^2)*R*Lf*Cf))*Lc)+(Lf*Cf*Lc*(dp^3)*(R^2));
x4 = ((Lf+(Lc*dp*R)-((dp^2)*R*Lf*Cf))*(dp^2)*R)-((dp^2)*R*Lc);
x5 = -((dp^4)*(R^2));
y1 = Lc*Cc*Lf*Cf;
y2 = (Lc*Cc) + (Lc*Lf*Cf);
y3 = Lc+Lf+(Lf*Cf*(dp^2)*R);
y4 = R*(dp^2);
Gvd = (Vo/dp)*((s*w1+w2)/((s^2)*u1+(s*u2)+u3))*((((s^4)*x1)+((s^3)*x2)+((s^2)*x3)+(s*x4)+x5)/(((s^3)*y1)+((s^2)*y2)+(s*y3)+y4));
w = 1.542362224647084e+04;
H = freqresp(Gvd,w);
objective = -20*log(abs(H));
end
function constraints = constraintFcn(Cf,f,Lf)
V_Ripple = 14;
C_Ripple = 12;
d = 0.76;
Vo=500;
Vin=120;
R=6;
constraints(1) = (Cf - (d*Vo)/(2*f*R*V_Ripple))==0;
constraints(2) = (Lf - (d*Vin)/(2*f*C_Ripple))==0;
end

回答(1 个)

Kartik
Kartik 2023-5-15
Hi,
The error message indicates that the "particleswarm" solver is not compatible with the optimproblem object created. The error suggests that you use a different solver that is compatible with the "optimproblem" object, such as the "fmincon" solver. Here is how you can modify your code to use the fmincon solver:
  1. Remove the following line that sets the non-default solver options for "particleswarm": "options2 = optimoptions("particleswarm","Display","iter","PlotFcn","pswplotbestf");"
  2. Update the code that solves the optimization problem by specifying the fmincon solver:
options = optimoptions('fmincon', 'Algorithm', 'active-set', 'Display', 'iter');
[solution,objectiveValue,exitflag] = solve(problem,initialPoint2,'solver','fmincon','options',options);
This code sets options for the fmincon solver and specifies it as the solver to use when calling the "solve" function.
3. Run the modified code and compare the results obtained using the fmincon solver with those obtained using the "particleswarm" solver.
Here is the link to the MathWorks documentation on the "optimoptions" function, which can be used to set solver options in MATLAB:
And here is the documentation on the "fmincon" solver:

类别

Help CenterFile Exchange 中查找有关 Particle Swarm 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by