Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Possible Matlab bug: "CPLEX encountered arrays with inconsistent lengths."

    Posted 12/18/12 05:01 PM

    Originally posted by: cgaray


    I am using CPLEX 12.5 with the Matlab API. I am adding sets of constraints and variables to a problem, solving the problem, and then adding another set of variables and constraints. I'd like to re-use the same instance of the Cplex class for each set of constraints. After each solution, I use delCols and rowCols on the object using the correct indices. After adding the second set of constraints I see the following error:

    Error using cplexlink125
    CPLEX encountered arrays with inconsistent lengths.

    Error in C:\Program
    Files\IBM\ILOG\CPLEX_Studio125\cplex\matlab\x64_win64\@Cplex\Cplex.p>Cplex.solve
    (line 1592)
    Error in C:\Program
    Files\IBM\ILOG\CPLEX_Studio125\cplex\matlab\x64_win64\@Cplex\Cplex.p>Cplex.subsref
    (line 3840)

    The object looks like this:

    K>> cplex.Model

    ans =

    sense: 'maximize'
    obj: 2029x1 double
    lb: 2029x1 double
    ub: 2029x1 double
    A: 2747x2029 double
    lhs: 2747x1 double
    rhs: 2747x1 double
    name: 'CPLEX'
    rowname: 2747x49 char
    colname: 2029x73 char
    ctype: 1x2029 char

    As you can see, the vector lengths are consistent. When I create a new object and copy each array from the old object to the new object, I am able to generate an optimal solution. It looks like the underlying model may not reflect the values that are presented when I look at cplex.Model. Is there any solution to this problem?

    Thanks,

    Chris
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Possible Matlab bug: "CPLEX encountered arrays with inconsistent lengths."

    Posted 01/02/13 03:22 AM

    Originally posted by: SystemAdmin


    Hm, I could not reproduce the problem here. Could you provide a small example code that triggers the issue?
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Possible Matlab bug: "CPLEX encountered arrays with inconsistent lengths."

    Posted 01/09/13 09:58 AM

    Originally posted by: cgaray


    In trying to put together an example, I have run into another problem. I think the way I've written this is correct but maybe I'm missing something may be contributing to the previous error.

    % Problem variables
    A = [1 -1 -1 0 0 0 0 0
         0 0 1 -1 0 0 0 0
         0 0 0 1 -1 0 0 0
         -1 -1 0 -1 0 1 -1 -1
         1 1 0 1 0 -1 1 2
         0 0 0 0 0 0 0 -1];
    lb = -1000 * ones(size(A, 2), 1);
    ub = 1000 * ones(size(A, 2), 1);
    obj = [0 0 0 -1 0 0 0 0];
    lhs = zeros(size(A, 1), 1);
    rhs = lhs;
     
    % Instantiate instance of cplex
    cpx = Cplex();
     
    % Set model variables
    cpx.Model.obj = obj;
    cpx.Model.lb = lb;
    cpx.Model.ub = ub;
    cpx.Model.A = A;
    cpx.Model.lhs = lhs;
    cpx.Model.rhs = rhs;
     
    % This works
    sol = cpx.solve();
     
    % Adding a new constraint fails
    cpx.addRows(0, [0 1 -1 0 0 0 0 0], 0);
    


    The cpx.Model.A is 6x8. The size of the new row is 1x8, so the number of columns is the same. However, I get the following error:

    Error using Cplex.addRows (line 2316)
    Cplex.addRows Error: number of column of Model.A does not match model.
     
    Error in C:\Program
    Files\IBM\ILOG\CPLEX_Studio125\cplex\matlab\x64_win64\@Cplex\Cplex.p>Cplex.subsref
    (line 3846)
    


    I am using CPLEX 12.5 and MATLAB R2012a.

    Thanks for your help.

    Chris
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Possible Matlab bug: "CPLEX encountered arrays with inconsistent lengths."

    Posted 01/10/13 02:06 AM

    Originally posted by: SystemAdmin


    I reproduced the problem here. Interestingly enough, the problem does not occur if I transpose the objective function:
    obj = [0 0 0 -1 0 0 0 0]';
    

    With this objective function everything works well.
    I am investigating.
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Possible Matlab bug: "CPLEX encountered arrays with inconsistent lengths."

    Posted 01/10/13 02:43 AM

    Originally posted by: SystemAdmin


    OK, the issue is that CPLEX really expects the objective function to be a column vector (see the reference documentation).
    In some places (like the solve() function) it gracefully accepts row vectors as well and transposes them before processing. In other places (like the addRows() function) it produces a "dimension mismatch" error if the objective function is a row vector.
    To avoid any trouble you should specify the objective function as column vector and consider objective row vectors as unsupported.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Possible Matlab bug: "CPLEX encountered arrays with inconsistent lengths."

    Posted 04/28/14 10:51 PM

    Originally posted by: ssly


    This has nothing to do with whether a row vector or column vector.

    I have encountered the same problem too. Even the vector length is totally consistent, Cplex will still report the error "CPLEX encountered arrays with inconsistent lengths."

    As long as modifications are applied to an old model, solving the modified model will cause this error. 

    What's more are that if you create a new cplex object and replicate all the parameters, this issue will disapper and the model become solvable again.  There are something wrong with the underlying model at cplex.Model. 

    After experiments, I generate the following simple example to demonstrate this issue.

    %% We build the simple original problem : min f=x1+x2+x3+x4+x5 subject to:-1<=x1,x2,x3,x4,x5<=1 and -2<=x1+x2+x3+x4+x5<=4
    cplex_master=Cplex('Lsh_master');
    cplex_master.Model.sense='minimize';
    cplex_master.Model.obj=ones(5,1);
    cplex_master.Model.lb=-ones(5,1);
    cplex_master.Model.ub=ones(5,1);
    cplex_master.Model.A = ones(1,5);
    cplex_master.Model.lhs = -2;
    cplex_master.Model.rhs = 4;
    cplex_master.DisplayFunc = [];
     
     
    try
        cplex_master.solve();
    catch m
        disp (m.message);
    end
    fprintf('\nSolution status = %s\n',cplex_master.Solution.statusstring);

     

    % the original problem can be solved successfully
    %% We now add one extra decision variable x6 to the original problem

     

    cplex_master.Model.obj=ones(6,1);
    cplex_master.Model.lb=-ones(6,1);
    cplex_master.Model.ub=ones(6,1);
    cplex_master.Model.A = ones(1,6);
     
    try
        cplex_master.solve();
    catch m
        disp (m.message);
    end

    % Now we can not solve this problem, it shows the error "CPLEX encountered arrays with inconsistent lengths." However, the vectors and matrix size are totally correct and consistent with each other.

     

    %% However, if we create a new cplex model and replicate the same new problem, it gets solvable again.
    cplex_sub=Cplex('Lsh_sub');
    cplex_sub.Model.obj=cplex_master.Model.obj;
    cplex_sub.Model.lb=cplex_master.Model.lb;
    cplex_sub.Model.ub=cplex_master.Model.ub;
    cplex_sub.Model.A = cplex_master.Model.A;
    cplex_sub.Model.lhs=cplex_master.Model.lhs;
    cplex_sub.Model.rhs = cplex_master.Model.rhs;
     
    try
        cplex_sub.solve();
    catch m
        disp (m.message);
    end
     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Possible Matlab bug: "CPLEX encountered arrays with inconsistent lengths."

    Posted 04/29/14 04:08 AM

    Thank you for reporting this issue. I have filed a bug report for this.

    A workaround is to delete starting information before the solve:

    delete(cplex_master.findprop('Start'));
    cplex_master.solve();

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Possible Matlab bug: "CPLEX encountered arrays with inconsistent lengths."

    Posted 05/05/14 03:09 AM

    Actually, this is not a bug but expected behavior. The first solve sets up the cplex_master.Start property with starting information (such as optimal basis) for a potential next call to cplex_master.solve().

    If you change the model directly my modifying cplex_master.Model.A and friends directly, then it is your responsibility to also update the start vectors or delete the 'Start' property. Otherwise the starting information will not match the model and you will get the error about arrays not being of equal length.

    Instead of modifying the vectors directly you should use the modification functions in the class API, such as cplex_master.addCols (see here). These functions take care of updating the 'Start' property as well.


    #CPLEXOptimizers
    #DecisionOptimization