Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Trouble with the cplex Matlab Interface

  • 1.  Trouble with the cplex Matlab Interface

    Posted 11/19/12 09:57 AM

    Originally posted by: cplexforMatlabuser


    Hello,

    I solve mixed integer linear problems with cplex (Version 12.4) and am using the cplex for MATLAB API. After having solved a mip problem (i.e. cplex mip-object) succesfully I create a new cplex object which is identical to the already solved mip-cplex object except for the fact that I set the values of the binary variables of the new cplex object to the values computed for the first (mip) object and declare all variables as continuous. When I try to solve this continuous problem I get an infeasibility error. I noticed that the values computed in the mip object for the binary variables are actually not binary but deviate by e.g. 10^-5. Therefore I rounded all binary variables to 1 or zero, according to which is closer. This helps sometimes, but not always. I wonder whether other rounding errors occur and cause infeasibilities. I often noticed that when I set some variables to values computed by cplex itself and try to resolve I get infeasibility errors. To my understanding cplex should accept the solutions that were computed by itself as accurate enough. Since not many people seem to have this problem I wonder whether it is due to the MATLAB-CPLEX Interface.

    Here is the relevant prt of my code. The mixed integer object called cplex was solved succesfully then I enter:

    cplex2=Cplex('S');
    cplex2.Model.sense='minimize';
    cplex2.Model.obj=cplex.Model.obj;
    cplex2.Model.A=cplex.Model.A;
    cplex2.Model.rhs=cplex.Model.rhs;
    cplex2.Model.lhs=cplex.Model.lhs;
    cplex2.Model.ub=cplex.Model.ub;
    cplex2.Model.lb=cplex.Model.lb;
    cplex2.Model.ub(cplex.Model.ctype=='B')=cplex.Solution.x(cplex.Model.ctype=='B');
    cplex2.Model.lb(cplex.Model.ctype=='B')=cplex.Solution.x(cplex.Model.ctype=='B');
    cplex2.Model.ub(cplex.Model.ctype=='B' & cplex2.Model.ub > 0.5)=1;
    cplex2.Model.lb(cplex.Model.ctype=='B' & cplex2.Model.lb < 0.5)=0;
    cplex2.Model.ctype=ones(size(cplex.Model.ctype,1),1)*'C';
    I also attached my continuous problems as lp file.

    I tried setting the simplex tolerance parmeter to make the problem feasible: cplex2.Param.simplex.tolerances.feasibility.Cur=10^-3; This didn't help (propably partly because sometimes the LP is solved by the barrier algorithm and not with the simplex)
    I would appreciate help very much!
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Trouble with the cplex Matlab Interface

    Posted 11/20/12 03:40 AM

    Originally posted by: SystemAdmin


    I am not a matlab expert but the way you are setting the bounds for binary variables looks wrong to me. I think you want to do the following:
    % Fix all binaries with value > 0.5 to 1.
    cplex2.Model.ub(cplex.Model.ctype=='B' & cplex.Solution.x > 0.5)=1;
    cplex2.Model.lb(cplex.Model.ctype=='B' & cplex.Solution.x > 0.5)=1;
    % Fix all binaries with value < 0.5 to 0.
    cplex2.Model.ub(cplex.Model.ctype=='B' & cplex.Solution.x < 0.5)=0;
    cplex2.Model.lb(cplex.Model.ctype=='B' & cplex.Solution.x < 0.5)=0;
    

    In you implementation you first set lb=ub=x and then round only one of the bounds. If for example x(i) = 1e-5 your original code would result in lb(i)=0, ub(i) = 1e-5, probably not what you want?
    If the model with fixed bounds (cplex2) is infeasible you may want to tighten the integrality tolerance for the first model.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Trouble with the cplex Matlab Interface

    Posted 11/20/12 05:44 AM

    Originally posted by: cplexforMatlabuser


    Dear Daniel,

    thank you, your suggestion helped. Nevertheless, I think such a thing shouldn't occur. Why is my code wrong when the upper bound is higher than the lower bound? This shouldn't cause an infeasibility! cplex should accept its own solution as accurate enough, shouldn't it?
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Trouble with the cplex Matlab Interface

    Posted 11/20/12 06:49 AM

    Originally posted by: SystemAdmin


    Your code was wrong in the sense that it did not actually fix the values for binary variables.
    I agree, that CPLEX should of course accept the solution it computed before (unless there are major numerical problems). Did you investigate why CPLEX considered the second problem infeasible (for example using the conflict refiner)? Did you check that all the bounds of binary variables were consistent and that you did not end up with a variable that has lb>ub? Can you export such an infeasible problem into a SAV file and attach it here?
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Trouble with the cplex Matlab Interface

    Posted 11/29/12 03:31 AM

    Originally posted by: cplexforMatlabuser


    Yes, I did use the ConflictRefiner, the Conflict is attached as an .lp below. I hope you can work with this format, if not, please let me know. I will attach the original problem as .lp file in the next e-mail. The conflict refiner told me something was wrong with a minimum down time constraint for a certain power plant in my unit commitment problem. I have no idea why this should be the case, if the problem was solved succesfully before as a mixed integer problem and I just fix the binary variables to the computed solution.
    As I pointed out before I use

    cplex2.Model.ub=cplex.Model.ub;
    cplex2.Model.lb=cplex.Model.lb;

    where the mixed integer-problem called cplex was solved succesfully. For the mip-object called cplex I put

    cplex.Model.lb( cplex.Model.lb > cplex.Model.ub )=cplex.Model.ub( cplex.Model.lb > cplex.Model.ub );
    before using cplex.solve()

    I had problems of this type in other contexts before, i.e. fixing variables to values computed by CPLEX and trying to resolve produces infeasibility meassages.
    I would very much appreciate, if you could find out whether this has to do with numerical instabilities or wether there is a rounding error problem with the CPLEX for MATLAB API interface.
    In the latter case I would consider swithing to GAMS to put up my problem.

    Thanks a lot in advance!
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Trouble with the cplex Matlab Interface

    Posted 12/03/12 02:43 AM

    Originally posted by: SystemAdmin


    In you conflict this bounds looks suspicious:
    x2952 >= 1.00000027540659
    

    What is the type of x2952? Is this bound expected? Or should it rather be 1?
    The offending constraint basically reads
    15 x2952 - 14 x2953 + sum x(i) <= 15
    

    The 14 x(i) variables are force to be >= 1 (which probably means they are fixed to 1?) and x2953 is <= 1. But since x2952 is forced to be strictly larger than 1 the constraint can never be satisfied.
    My guess is that you still have a problem in the way you setup the bounds for the second solve. Can you try to be more explicit in this setup, like this (untested code by non-expert):
    cplex2.Model.lb = cplex.Model.lb;
    cplex2.Model.ub = cplex.Model.ub;
    % Fix all binary variables to the values in the solution.
    % Note that we round() the values so that the variables are indeed
    % fixed to 0 and 1 and not to something like 1e-6, 1+1e-6 etc. which
    % may of course result in infeasible problems.
    for i = 1:length(cplex.Model.lb)
      if (cplex.Model.ctype(i) == 'B')
        cplex2.Model.lb(i) = round(cplex.Solution.x(i));
        cplex2.Model.ub(i) = round(cplex.Solution.x(i));
      end
    end
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Trouble with the cplex Matlab Interface

    Posted 12/10/12 03:42 AM

    Originally posted by: cplexforMatlabuser


    Yes Daniel, x2952 should be binary and your more explicit code describes correctly what I do.
    The point I am trying to make is that this rounding should actually not be necessary. If cplex.Solution.x does not conflict with any constraints in the cplex object (which should be the case) there can logically be no infeasibilities/conflicts in the cplex2 object. So the conclusion is that either CPLEX produces a solution to the cplex MILP-object that violate the constraints/bounds by a small amount or that there is a rounding error problem when CPLEX passes its computed solution on to MATLAB. In case of the first problem, I do understand that there always is a certain amount of imprecision in numerical calculations, but they shouldn't be so large that when you try to resolve, you get an infeasibility message, do you agree?

    Thanks very much for checking this!

    P.S.: Today, I could apparantly attach my problem
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Trouble with the cplex Matlab Interface

    Posted 12/11/12 07:46 AM

    Originally posted by: SystemAdmin


    I think there are two issues here:
    1. Something is wrong in the way you fix the bounds for the integral variables in the cplex2 object. This problem results in contradictory bounds which render the problem infeasible. I guess this problem will go away if you do the rounding like I described.
    2. Since computers can only do finite precision arithmetic there will always be roundoff errors. The way to handle these errors is to use tolerances. CPLEX will return solutions that are correct within the specified tolerances (which in extreme cases can mean that "feasible" and "infeasible" are both correct answers). The default integrality tolerance for CPLEX is 1e-5, meaning that CPLEX considers any number r as integral for which there is an integral number i with |i-r|<1e-5. This tolerance is suitable for most problems, but it means that a binary variable may have value 1.00000056 in the solution returned by CPLEX. If you now set the lower bound of this variable to 1.00000056 and keep the upper bound at 1 then the problem becomes blatantly infeasible. That is why you either need to perform the rounding I described or need to tighten the integrality tolerance (potentially setting it to 0).
    As I said, CPLEX will deliver a solution that feasible/optimal within the specified tolerances. It is up to you to "interpret" this solution in the correct way.
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Trouble with the cplex Matlab Interface

    Posted 11/29/12 03:35 AM

    Originally posted by: cplexforMatlabuser


    Here is the cplex 2 LP-object
    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Trouble with the cplex Matlab Interface

    Posted 11/29/12 03:43 AM

    Originally posted by: cplexforMatlabuser


    uploading didn't seem to have worked, so I try again
    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: Trouble with the cplex Matlab Interface

    Posted 11/29/12 06:18 AM

    Originally posted by: cplexforMatlabuser


    another try
    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: Trouble with the cplex Matlab Interface

    Posted 11/29/12 06:34 AM

    Originally posted by: cplexforMatlabuser


    and again, this time as .sav
    #CPLEXOptimizers
    #DecisionOptimization