计算KKT系数

时间:2019-07-13 09:28:13

标签: matlab optimization

我想知道我的代码是否100%正确,因为在我的算法中,有人指出,当删除KKT系数等于零的约束时,它不会改变优化结果,不幸的是它会改变。任务是计算满足KKT条件的KKT系数:

enter image description here

function [v]= KKTpz(x)
        global Sk g f
        n = length(x);
        m = size(Sk,1);
        F_S = @(k) sum(k.^2);
        % Create A and B %% 
        Aeq_v = [];
        h =0.00001;
        for j = 1:m
            Aeq_v(1,j) = 0;
            for m1 = 1:n
                vec = zeros(1,n);
                vec(m1) = h;
                Aeq_v(1,j) = Aeq_v(1,j) + (g(x + vec, Sk(j,:)) - g(x, Sk(j,:)))/(h);
            end
        end
        %Aeq is a gradient g with respect to x in points s_i 
        beq_v = zeros(1);
        for m1 = 1:n
                vec = zeros(1,n);
                vec(m1) = h;
                beq_v(1) = beq_v(1) -(f(x + vec) - f(x))/(h);
        end
        %beq is minus gradient f(x)
        k = 2;
        for i = 1:m
            if abs(g(x, Sk(i,:)))>10^-4 % Check if g(x, s) == 0
                Aeq_v(k,:) = 0;
                Aeq_v(k,i) = 1;
                beq_v = [beq_v,0];
                k = k+1;
            end
        end
        % if g(x,s_i) is not equal to zero
        % then v(s_i) must be equal zero 
        v = fmincon(F_S,ones(size(Sk,1),1),[],[], Aeq_v, beq_v,zeros(m,1),[]); % Define v
end   

0 个答案:

没有答案