使用for循环和fmincon函数实现查找算法

时间:2019-07-04 09:22:33

标签: matlab for-loop optimization

我正在尝试实施该算法以查找新的约束:enter image description here

就我而言,我们仅采用3个自然数,即1,2, 3。 与这些自然数关联的集合为M1M2M3。我选择了II(2)所提供的求解器,而不是fmincon中的牛顿法。 这是我的代码不起作用!

function[s_new]= checking2(M1,M2,M3,x)
M1=linspace(0,1,10)';
M2=linspace(0,1,100)';
M3=linspace(0,1,1000)'; 
bool1=0;
eta = 10^-8;
pocz=[];
max=-100;
x = [0.1,0.1]'; % warunek początkowy
A = [];
b = [];
Aeq = [];
beq = [];
Set=[0,1];
g = @(x,s) 5*x(1).^2.*sin(pi.*sqrt(s))./(1+s.^2) - x(2);
g_new = @(s) -g(x,s);

for i=1:length(M1)
    if g(x,M1(i,:))>eta
       s_new=M1(i,:);
       bool1=1;
    end
end
if ~bool1
    for i=1:length(M1)
        if g(x,M1(i,:))>max
           pocz=M1(i,:);
           max=g(x,M1(i,:));
        end
    end
    if max<-eta
        bool1=1;
    end
end
if ~bool1
    s_maybe = fmincon(g_new,pocz,A,b,Aeq,beq,min(Set),max(Set));
    if g(x,s_maybe)>eta
       s_new=s_maybe;
       bool1=1;
    end
end
if ~bool1
    for i=1:length(M2)
        if g(x,M2(i,:))>eta
           s_new=M2(i,:);
           bool1=1;
        end
     end
end
if ~bool1
    for i=1:length(M2)
        if g(x,M2(i,:))>max
           pocz=M2(i,:);
           max=g(x,M2(i,:));
        end
    end
    if max<-eta
    bool1=1;
    end
end
if ~bool1        
    s_maybe = fmincon(g_new,pocz,A,b,Aeq,beq,min(Set),max(Set));
    if g(x,s_maybe)>eta
       s_new=s_maybe;
       bool1=1;
    end
end
if ~bool1
    for i=1:length(M3)
        if g(x,M3(i,:))>eta
           s_new=M3(i,:);
           bool1=1;
        end
    end
end
if ~bool1
    s_new = 1;
end
disp(s_new);

问题是:

Undefined function or variable 's_new'.

Error in checking2 (line 70)
disp(s_new);

所以基本上一切都可能是错误的,但我想这与fmincon有关。

编辑:

算法的目的是找到目标函数f(x)的最小值,满足S中所有s的所有约束g(x,s)<= 0,其中S是一个无限集(某个区间就我们而言)。

我的算法所做的事情,首先,它需要S的一个有限子集,并计算该集合上f的最小值,然后我尝试使用一些s_new更新S。我正在尝试实现的算法正是创建s_new的过程。然后,如果工作正常,我将s_new添加到我的子集中并计算新集合的最小值,依此类推,直到g(x,s)<= eta,其中eta是一个小数。

1 个答案:

答案 0 :(得分:1)

我重写算法,通读注释

clc
clear

lb = 0;
ub = 1;

% Given 
l = 3;
M1=linspace(lb,ub,10)';
M2=linspace(lb,ub,100)';
M3=linspace(lb,ub,1000)'; 

% one boolean value for each Matrix
bool = zeros(1,3);

eta = 10^-8;
% Used as  fmincon  initial starting guess
pocz = nan;

% Used to store the new finding s that fits all the conditions
s_new = nan;

% Fixed x
x = [0.1,0]';

% fmincon linear constraints 
A = [];
b = [];
Aeq = [];
beq = [];

% Main function 
g = @(x,s) 5*x(1).^2*sin(pi*sqrt(s))/(1+s.^2) - x(2);

% Optimization concerns s only, don't include x as x is fixed 
g_new = @(s) -g(x,s);

% Assuming the maximum is reached at the upper bound, used in(II)(2)
max_s = ub;
maxfun = g(x, max_s);

% Use a cell, for each iteration use a specific matrix M
M = {M1, M2, M3};

for j = 1: length(M)
    % used in (II)(1)
    check = 0;
    step = 1;
    % (I) step 1
    for i = 1:length(M{j})

        % Stopping criteria

        if g(x, M{j}(i)) > eta
            s_new = M{j}(i);
            bool(j) = 1;
            break;
        else


          % Function maximum value for next step (II)
            if maxfun < g(x, M{j}(i))
                maxfun = g(x, M{j}(i));

                % To be used in fmincon as pocz
                max_s = M{j}(i);
            end


        end 
    % To be used in (II)(1)
        if maxfun < -eta
              check = 1;
        end
    end
    % End of (I)

    % Put (II)(1) here  step 2

     if ~bool(j) && check
            step = step + 1;
            % Stopping criteria 
            if step >= l
                disp('S_new not defined');
                break;
            end

            % otherwise go to the next M

      end

    % (II)(2) step 3
    if ~bool(j)
        step = step + 1;
        if maxfun >= -eta && maxfun <= eta 
            pocz = max_s;        
            bool(j) = 1;
        end

    end

    %% EDIT: if bool(j) changed to if ~bool(j)
    %  (II)(2) Continue
    if ~bool(j)
        s_maybe = fmincon(g_new,pocz,A,b,Aeq,beq,lb,ub);

        % End of (II)(2)


        % (II)(2)-1 step 4
        step = step + 1;  
        if g(x, s_maybe) > eta

            s_new = s_maybe;

            bool(j) = 1;
        end
        % End of (II)(2)-1
    end

        % Put (II)(2) here  step 5
        if ~bool(j)
            step = step + 1; 
            % Stopping criteria 
            if step >= l
                disp('S_new not defined');
                break;
            end

            % otherwise go to the next M

        end


end