用其他优化算法替换fmincon函数

时间:2019-10-01 15:03:01

标签: algorithm matlab optimization

此源代码是epsilon-constraint方法的实现。如何用PSO或GA优化算法替换fmincon()函数(我不想使用内置函数)。

此代码为主要功能

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);

我已替换此部分:

for i=1:n
    xopt(i,:)=fmincon('obj_eps', x0, [], [], [], [], LB, UB,'nonlcon_eps', options, eps(i));
end

与此:

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,:));
            [~,constraintViolation(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

但是,当我运行代码时,输​​出只是一种解决方案,而它应该是50(n = 50)。这是因为所有其他解决方案都不满足约束条件。如何修改代码以在每次运行时获得一个解决方案(n = 1:50)

更新: 我已经包含了PSOalgo()代码并进行了一些修改。现在输出的是50个解决方案。但是,获得的结果不正确。

fmincon()结果: fmin() result

PSO结果: PSO result

0 个答案:

没有答案