MATLAB Yalmip:无法运行优化器;消除变量中的错误

时间:2017-01-23 10:47:33

标签: matlab mathematical-optimization

底线: Matlab抛出下面的错误,对我来说不明显的根本原因是什么。问题似乎存在于输入参数中,但我无法确切地知道它是什么。我非常感谢你找到它的任何帮助。

Index exceeds matrix dimensions.

Error in eliminatevariables (line 42)
    aux(model.precalc.index2) = value(model.precalc.jj2);

Error in optimizer/subsref (line 276)
            [self.model,keptvariablesIndex] =
            eliminatevariables(self.model,self.model.parameterIndex,thisData(:),self.model.parameterIndex);

Error in SCMv0_justrun (line 68)
    [solutions,diagnostics] = controller{inputs};

背景 我正在尝试编写模型预测控件,但我对Yalmip或数学优化算法还不是很熟悉。我确保定义的输入和实际输入具有相同的尺寸,因此我很惊讶错误与矩阵尺寸有关。

错误源自我的代码调用优化器时。 我的代码基于:https://yalmip.github.io/example/standardmpc/

这是我的代码(代码的第一部分只需要定义优化问题,并在" %%%%%"之间标记;错误发生在最后):

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yalmip('clear')
clear all

% Model data
A = eye(3);
B = [1 0 -1 0 0 0 0; 0 1 0 -1 0 0 0; 0 0 0 0 1 -1 -1];
nx = 3; % Number of states
nu = 7; % Number of inputs

% MPC data
Q = [10 10 20]';
R = [10 10 1 1 5 3 3]';
C = [50 0; 0 30];
N = 90;


ny = 2;
E = [0 0 0 0 0 1 0; 0 0 0 0 0 0 1];

u = sdpvar(repmat(nu,1,N),repmat(1,1,N));
x = sdpvar(repmat(nx,1,N+1),repmat(1,1,N+1));
r = sdpvar(repmat(ny,1,N+1),repmat(1,1,N+1));
d = sdpvar(ny,1);
pastu = sdpvar(nu,1);

dx = 0.05;
Gx=[-1*eye(3);eye(3)];
gx = [0 0 0 500 500 1000]';
COVd = [zeros(5,7);0 0 0 0 0 10 0; 0 0 0 0 0 0 10];
COVx = zeros(nx,nx);

auxa = eye(5);
auxb = zeros(5,2);
Gu = [-1*eye(7,7); auxa auxb;0 0 0 0 0 1 1];
gu = [zeros(7,1); 200; 200; 50; 50; 100; 500];

Ga = [0 0 0.5 0.5 -1 0 0];

constraints = [];
objective = 0;
for k = 1:N
    r{k} = r{k} + d;
    objective = objective + Q'*x{k} + R'*u{k} + (r{k}-E*u{k})'*C*(r{k}-E*u{k});
    constraints = [constraints, x{k+1} == A*x{k}+B*u{k}];

    COVx = A*COVx*A' + B*COVd*B';
    COVGx = Gx*COVx*Gx';
    StDevGx = sqrt(diag(COVGx));
    chance = gx - norminv(1-dx/(length (gx)*N))*StDevGx;

    constraints = [constraints, Ga*u{k}==0, Gu*u{k}<=gu, Gx*x{k}<=gx-chance]; 
end
objective = objective + Q'*x{N+1};

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

parameters_in = {x{1},[r{:}],d,pastu};
solutions_out = {[u{:}], [x{:}]};

controller = optimizer(constraints, objective,[],parameters_in,solutions_out);
x = 100*ones(nx,1);
clf;
disturbance = randn(ny,1)*10;
oldu = zeros(nu,1);
hold on
for i = 1:150
    future_r = [4*sin((i:i+N)/40);3*sin((i:i+N)/20)];% match dimensions of r  
    inputs = {x,future_r,disturbance,oldu};
    [solutions,diagnostics] = controller{inputs};    
    U = solutions{1};oldu = U(1);
    X = solutions{2};
    if diagnostics == 1
        error('The problem is infeasible');
    end
    x = A*x+B*u;
end

1 个答案:

答案 0 :(得分:0)

这是最新版YALMIP中的一个错误。