Replace the fmincon function with another optimization algorithm
Mostrar comentarios más antiguos
In this source code, how can I replace the fmincon function with PSO or GA optimization algorithm (I do not want to use a build-in function).
x0 = [1 1]; % Starting point
UB = [1 1]; % Upper bound
LB = [0 0]; % Lower bound
options = optimset('LargeScale', 'off', 'MaxFunEvals', 1000, ...
'TolFun', 1e-6, 'TolCon', 1e-6, 'disp', 'off');
% Create constraint bound vector:
n = 50; % Number of Pareto points
eps_min = -1;
eps_max = 0;
eps = eps_min:(eps_max - eps_min)/(n-1):eps_max;
% Solve scalarized problem for each epsilon value:
xopt = zeros(n,length(x0));
for i=1:n
xopt(i,:)=fmincon('obj_eps', x0, [], [], [], [], LB, UB,...
'nonlcon_eps', options, eps(i));
end
function [C,constraintViolation] = nonlcon_eps(x, eps)
constraintViolation= 0;
Ceq = [];
C(1) =x(2)+(x(1)-1)^3;
if C(1) > 0
constraintViolation= constraintViolation+ 1;
end
C(2) = -x(1) - eps;
if C(2) > 0
constraintViolation= constraintViolation+ 1;
end
function f = obj_eps(x, ~)
f = 2*x(1)-x(2);
This part:
for i=1:n
xopt(i,:)=fmincon('obj_eps', x0, [], [], [], [], LB, UB,'nonlcon_eps', options, eps(i));
end
Becomes:
maxIteration = 1000;
dim = 2;
n = 50; % Number of Pareto points
eps_min = -1;
eps_max = 0;
EpsVal = eps_min:(eps_max - eps_min)/(n-1):eps_max;
for i=1:n
[gbest]= PSOalgo(N,T,lb,ub,dim,fobj,fcon,EpsVal(i));
end
function [gbest]= PSOalgo(N,maxite,lb,ub,dim,fobj,fcon,EpsVal)
% initialization
wmax=0.9; % inertia weight
wmin=0.4; % inertia weight
c1=2; % acceleration factor
c2=2; % acceleration factor
% pso initialization
X=initialization(N,dim,ub,lb);
v = 0.1*X; % initial velocity
for i=1:N
fitnessX(i,1)= fobj(X(i,:));
end
[fmin0,index0]= min(fitnessX);
pbest= X; % initial pbest
pbestfitness = fitnessX;
gbest= X(index0,:); % initial gbest
gbestfitness = fmin0;
ite=0; % Loop counter
while ite<maxite
w=wmax-(wmax-wmin)*ite/maxite; % update inertial weight
% pso velocity updates
for i=1:N
for j=1:dim
v(i,j)=w*v(i,j)+c1*rand()*(pbest(i,j)- X(i,j)) + c2*rand()*(gbest(1,j)- X(i,j));
end
end
% pso position update
for i=1:N
for j=1:dim
X(i,j)= X(i,j)+v(i,j);
end
% Check boundries
FU=X(i,:)>ub;
FL=X(i,:)<lb;
X(i,:)=(X(i,:).*(~(FU+FL)))+ub.*FU+lb.*FL;
% evaluating fitness
fitnessX(i,1) = fobj(X(i,:));
[~,consentViolation(i,1)] = fcon(X(i,:), EpsVal);
end
% updating pbest and fitness
for i=1:N
if fitnessX(i,1) < pbestfitness(i,1) && constraintViolation(i,1) == 0
pbest(i,:)= X(i,:);
pbestfitness(i,1)= fitnessX(i,1);
end
[~,constraintViolation(i,1)] = fcon(pbest(i,:), EpsVal);
end
% updating gbest and best fitness
for i=1:N
if pbestfitness(i,1)<gbestfitness && constraintViolation(i,1) == 0
gbest=pbest(i,:);
gbestfitness= pbestfitness(i,1);
end
end
ite = ite+1;
end
end
The obtained result by using PSO algorithm is not correct.
fmincon () result:

PSO algorithm result:

7 comentarios
Walter Roberson
el 11 de Oct. de 2019
What leads you to say that the PSO output is not correct? PSO searches semi-randomly, and will usually require more iterations to find a minima even when there is only one minima.
Maroco Sc
el 11 de Oct. de 2019
Walter Roberson
el 12 de Oct. de 2019
So? That does not prove that the PSO output is not correct.
PSO does not guarantee it will find a global minima, and for most problems does not guarantee that it will find a local minima either. There is a class of equations for which a properly implemented PSO is known to converge to a local minima eventually for some particular kinds of constraints.
Maroco Sc
el 13 de Oct. de 2019
Walter Roberson
el 13 de Oct. de 2019
Editada: Walter Roberson
el 20 de Oct. de 2019
I am not sure what your question is at this point? It sounds like you are asking for MATLAB code for epsilon constraint multi objective pso. https://www.mathworks.com/matlabcentral/fileexchange/64787-demonstration-of-two-multi-objective-optimization-strategies?s_tid=prof_contriblnk
Maroco Sc
el 19 de Oct. de 2019
Walter Roberson
el 20 de Oct. de 2019
https://www.mathworks.com/matlabcentral/fileexchange/25986-constrained-particle-swarm-optimization appears to be pso with nonlinear constraint capability.
Respuestas (0)
Categorías
Más información sobre Particle Swarm en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!