【问题标题】:Implementing finding alghoritm with for loops and fmincon function使用 for 循环和 fmincon 函数实现查找算法
【发布时间】:2019-11-15 00:05:37
【问题描述】:

我正在尝试实现这个算法来寻找新的约束:

在我的例子中,我们只取3 自然数,即1,2, 3。 与这些自然数相关的集合是M1M2M3。我选择了 Matlab fmincon 提供的求解器,而不是 II(2) 中的牛顿法。 这是我的代码不起作用!

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)

我的算法是做什么的,首先它需要 S 的一些有限子集并计算这个集合上 f 的最小值,然后我试图用一些 s_new 更新 S。我试图实现的这个算法正是创建 s_new 的过程。然后,如果它工作正常,我会将 s_new 添加到我的子集并计算新集上的最小值,依此类推,直到 g(x,s)

【问题讨论】:

  • 我建议用您考虑的任何值简单地初始化s_new,因为似乎不满足创建它的任何条件。
  • 另外我建议使用return 而不是布尔变量。
  • 我认为代码一定有问题,不可能有几个不同的 x 点不符合标准。
  • fmincon 仅优化 s,将 g_new 更改为 g_new = @(s) -g(x,s)
  • 我给你重写算法,maxg的最大值,不是预定义的随机数

标签: matlab for-loop optimization


【解决方案1】:

我重写算法,通读cmets

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


【讨论】:

  • 感谢您的输入,一如既往地很有帮助,但是对于 x = [0.1,0]',它会产生错误:目标函数在初始点未定义。 Fmincon 无法继续。这很奇怪。
  • 你能解释清楚这个算法的目的吗?比如xsmeanings
  • 看一下编辑,如果不清楚,您可以要求更多或阅读此处,因为我尝试实现的算法来自这篇文章:researchgate.net/publication/…
  • 我刚刚更新了答案,再次运行。我所做的更改可以在fmincon之前的EDIT部分找到。
猜你喜欢
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多