1

我有一个 Matlab 代码,它使用fmincon一些约束。这样我就可以修改我考虑过的代码,条件矩阵 A 中的行位置是否有所不同

我设置了一个测试文件,这样我就可以更改一些变量。事实证明,条件的位置与结果无关,但 A 和 b 中的行数起了作用。我对此感到惊讶,因为我希望 A 和 b 中只有零的行会被抵消。

fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
options1 = optimoptions('fmincon','Display','off');

A=zeros(2,2); %setup A
A(2,2)=1; %x2<0
b=[0 0]'; %setup b
x = fmincon(fun,[-1,2],A,b,[],[],[],[],[],options1);x

%change condition position inside A
A=zeros(2,2);    
A(1,2)=1; %x2<0
b=[0 0]';
x = fmincon(fun,[-1,2],A,b,[],[],[],[],[],options1);x 
% no change; the position doesn´t influence fmincon

%change row size of A
A=zeros(1,2);
A(1,2)=1; %x2<0
b=[0]';
x = fmincon(fun,[-1,2],A,b,[],[],[],[],[],options1);x 
%change in x2

%increase size of A
A=zeros(10,2);
A(1,2)=1; %x2<0
b=[0 0 0 0 0 0 0 0 0 0]';
x = fmincon(fun,[-1,2],A,b,[],[],[],[],[],options1);x 
%change in x2

有人可以向我解释为什么 fmincon 会受到行号的影响吗?A 和 b 中的“正确”行号是多少?变量的数量还是条件的数量?

编辑 出于完整性的原因:

我同意由于迭代过程,不同的值是可能的。不过,我可以找到差异大于公差的情况:

添加+log(x(2)到函数中:

fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2+log(x(3));
options1 = optimoptions('fmincon','Display','off');

options = optimoptions('fmincon')

A=zeros(2,3); %setup A
A(2,3)=1; %x2<0
b=[0 0]'; %setup b
x = fmincon(fun,[-1,2,1],A,b,[],[],[],[],[],options1);x

%change row size of A
A=zeros(1,3);
A(1,3)=1; %x2<0
b=[0]';
x = fmincon(fun,[-1,2,1],A,b,[],[],[],[],[],options1);x 
%change in x2

%increase size of A
A=zeros(10,3);
A(1,3)=1; %x2<0
b=[0 0 0 0 0 0 0 0 0 0]';
x = fmincon(fun,[-1,2,1],A,b,[],[],[],[],[],options1);x 
%change in x2

x =
     -0.79876      **0.49156**   2.3103e-11
x =
     -0.79921      0.49143   1.1341e-11
x =
     -0.80253      **0.50099**   5.8733e-12

Matlab 支持告诉我,A 矩阵的行数不应多于条件。每个条件都使算法变得更加困难。

4

1 回答 1

3

请注意,fmincom这不一定给出精确的解决方案,而是根据特定标准对解决方案的良好近似。

结果的差异是合理的,因为fmincon它是一种迭代算法,并且这些矩阵乘法(即使主要有零)最终将以不同的结果结束。Matlab 实际上会做这些矩阵乘法,直到他找到最好的结果。所以这些结果都是正确的,因为它们都接近解决方案。

x =
   0.161261791015350  -0.000000117317860

x =
   0.161261791015350  -0.000000117317860

x =
   0.161261838607809  -0.000000077614999

x =
   0.161261877075196  -0.000000096088746

1.0e-07考虑到您没有指定停止标准,结果的差异在于哪个是体面的结果。您可以使用命令查看默认情况下拥有的内容

options = optimoptions('fmincon')

我的结果是

Default properties:
                Algorithm: 'interior-point'
           CheckGradients: 0
      ConstraintTolerance: 1.0000e-06
                  Display: 'final'
 FiniteDifferenceStepSize: 'sqrt(eps)'
     FiniteDifferenceType: 'forward'
     HessianApproximation: 'bfgs'
               HessianFcn: []
       HessianMultiplyFcn: []
              HonorBounds: 1
   MaxFunctionEvaluations: 3000
            MaxIterations: 1000
           ObjectiveLimit: -1.0000e+20
      OptimalityTolerance: 1.0000e-06
                OutputFcn: []
                  PlotFcn: []
             ScaleProblem: 0
SpecifyConstraintGradient: 0
 SpecifyObjectiveGradient: 0
            StepTolerance: 1.0000e-10
      SubproblemAlgorithm: 'factorization'
                 TypicalX: 'ones(numberOfVariables,1)'
              UseParallel: 0

例如,我可以使用以下选项获得更接近的结果:

options1 = optimoptions('fmincon','Display','off', 'OptimalityTolerance', 1.0e-09);

结果是

x =
   0.161262015455003  -0.000000000243997

x =
   0.161262015455003  -0.000000000243997

x =
   0.161262015706777  -0.000000000007691

x =
   0.161262015313928  -0.000000000234186

您也可以尝试使用其他标准MaxFunctionEvaluationsMaxFunctionEvaluations,看看您是否可以获得更接近的结果......

于 2019-01-22T14:56:12.480 回答