IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this community and its apps will no longer be available. More details available on our FAQ.
Topic
  • 2 replies
  • Latest Post - ‏2019-05-27T11:53:01Z by CPLEX12USER
CPLEX12USER
CPLEX12USER
31 Posts
ACCEPTED ANSWER

Pinned topic Displaying constraints added to cplex model

‏2019-05-06T18:53:20Z |

Hi,

   I am having cplex object of type IloCplex and have variable array of tyoe IloNumVar[]. I am using java interface and add constraints by iterating through a huge array.

Say, I do cplex.addEq(var[i], val[i]). I add different type of constraints and have several loops through the program. When I export model, I see all the constraints added together till then and it is hard to debug where exactly problem lies if there was an issue in formulating the constraint.

Is there a way to print the constraints as interpreted by CPLEX immediately after adding these constraints using addEq, addLe etc.

 

Thank you!

 

  • PaulRubin
    PaulRubin
    889 Posts
    ACCEPTED ANSWER

    Re: Displaying constraints added to cplex model

    ‏2019-05-06T21:20:07Z   in response to CPLEX12USER

    Yes. First, if you are not already doing this, I recommend that you add names to all variables and constraints, to make them more readable. The addEq, addLe and addGe methods return a pointer to the constraints they just added. You can capture this to a variable and then just use that variable in an output statement. The .toString() method will automatically be called on the constraint.

    Here is a snippet from some working code:

        IloCplex mip = new IloCplex();
        IloIntVar[] x = new IloIntVar[n];
        IloRange[] constraints = new IloRange[m];
        for (int i = 0; i < n; i++) {
          x[i] = mip.boolVar("x" + i);
        }
        for (int i = 0; i < m; i++) {
          IloLinearNumExpr sum = mip.linearNumExpr();
          for (int j = 0; j < n; j++) {
            sum.addTerm(a[i][j], x[j]);
          }
          constraints[i] = mip.addGe(sum, 1, "C" + i);
          System.out.println("Constraint " + i + ":\n" + constraints[i]);
        }
    

    The a[][] matrix is constraint coefficients. A typical couple of lines of output is the following:

    Constraint 3:
    IloRange C3 : 1.0 <= (1.0*x2 + 1.0*x6 + 1.0*x7 + 1.0*x11 + 1.0*x13 + 1.0*x14 + 1.0*x15) <= infinity

    Paul

    • CPLEX12USER
      CPLEX12USER
      31 Posts
      ACCEPTED ANSWER

      Re: Displaying constraints added to cplex model

      ‏2019-05-27T11:53:01Z   in response to PaulRubin

      It works and makes it easy to interpret the generated model.

       

      Thank you!