Decision Optimization

Decision Optimization

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


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

Need Help in Debugging cplex code

  • 1.  Need Help in Debugging cplex code

    Posted 04/18/12 01:29 AM

    Originally posted by: cplex-newbie


    Hi All,

    I am new to cplex and would like some help in debugging a cplex code. I would appreciate if somebody could reply to me, so that I can share the code and explain what I am trying to do.

    Thanks.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Need Help in Debugging cplex code

    Posted 04/18/12 03:08 AM

    Originally posted by: SystemAdmin


    What exactly are you trying to debug? What API do you use (C, C++, Java, ...).
    Could you describe your problems in more detail?
    Maybe also take a look at the user manual, in particular at this section.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Need Help in Debugging cplex code

    Posted 04/18/12 08:04 AM

    Originally posted by: cplex-newbie


    Hi Daniel,

    Thanks for the reply. I will go over the link which you sent.
    I am using c++ for programming.

    Problem I am trying to formulate.

    Equation 1:
    \sum_{k=2} \sum_{w} C_ij,k,w = 3 ∀(i, j) ∈ Z

    Equation 2:
    \sum_{(i,j)∈ Z } \sum_{k=2} C_ij,k,w * X_ij,k,l <= 1 ∀l ∈ L ∀w

    Equation 3:
    \sum_{(i,j)∈ Z } \sum_{k=2} C_ij,k,w <= 2*U^w ∀w

    Equation 4:
    V >= w*U^w ∀w

    Objective : I want to minimize V.

    Notation.
    1) where C_ij,k,w is the binary decision variable indicating whether
    there exists a lightpath on path P_ij,k which uses wavelength
    w. Here I am trying to reach from node i to node j along the path
    denoted by k.
    2) Z is the set of all node pairs in the network.
    3) The variable X_ij,k,l = 1 if P_ij,k uses link l and is 0 otherwise.
    4) U^w is a binary variable which indicates whether wavelength w is used.
    5) V is the number of wavelengths used.

    Program :
    For equation 1, I am using
    for (i = 0; i < nbElements; i++) {
    for(j = 0; j < nbElements; j++) {
    con.add(IloRange(env, 3, 3);
    varCount = 0;
    for (k = 0; k < 2; k++) {
    for(w = 0; w < 50; w++) {
    var.add(IloNumVar(env));
    conconCount.setLinearCoef(varvarCount++, 1.0);
    }
    }
    conCount++;
    }
    }

    For equation 2, I am using
    for (i = 0; i < nbElements; i++) {
    for(j = 0; j < nbElements; j++) {
    varCount = 0;
    for (k = 0; k < K; k++) {
    for(w = 0; w < W; w++) {
    /* some more core over here */
    conconCount.setLinearCoef(varvarCount++, 1.0);
    }
    }
    conCount++;
    }
    }

    I am reusing the constriant from equation 1 in 2 and I am not adding this constriant again to the model.
    Is this correct ?
    For equation 3, I am using
    for (i = 0; i < nbElements; i++) {
    for(j = 0; j < nbElements; j++) {
    for (k = 0; k < K; k++) {
    for(w = 0; w < W; w++) {
    //because P=|p| and there are two paths.
    model.add(conconCount <= 2*u_w[w]);
    }
    }
    conCount++;
    }
    }

    Is the model add function correct ?

    For equation 4, I am using
    for(w = 0; w < W; w++) {
    model.add(w*u_w[w] <= V);
    }

    Is the model add function correct ?

    Finally i am adding my objective function.
    model.add(IloMinimize(env, V));

    Is the model add function correct ?

    Thanks,
    Shireesh
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Need Help in Debugging cplex code

    Posted 04/18/12 08:07 AM

    Originally posted by: cplex-newbie


    Looks like the operator "[]" is being shown as an hyperlink.

    These are the variables I have declared.

    IloNumVarArray var(env);
    IloRangeArray con(env);
    /*
    * Declare an array to hold the wavelengths used.
    */
    IloNumVarArray u_w(env,W,0,1);

    Thanks,
    Shireesh
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Need Help in Debugging cplex code

    Posted 04/18/12 11:36 AM

    Originally posted by: SystemAdmin


    I see several problems with your code:
    1. The way in which you reuse con to build equation 2 is wrong. Actually, equation 2 is a quadratic expression, so it is not enough to set only linear coefficients.
    2. You need to explicitly add() each constraint that you want to appear in the model.
    3. For equation 3 you use
    
    con[conCount] <= 2*u_w[w]
    

    that will not do what you intended. Note that you are writing "constraint less than or equal to expression" here. What you want is "expression less than or equal to expression".
    There may be more problems in your code :-(

    Here are a few tips to make things easier:
    1. Name all your variables, any maybe all your constraints as well.
    2. In the beginning don't try to be too clever by re-using constraints, expressions or whatever. Instead use a new constraint etc. every time you create something new.
    3. Write out your model to an LP file (cplex.exportModel("model.lp")) and look at that file with a text editor. Double check that the model is what you expected.

    Here is how I would go about defining the C variables and the first equation:
    
    #include <sstream> #include <ilcplex/ilocplex.h>   ...   
    // Create the C variables. We put all the variables into one flat 
    // array and use macro C to access them by index. 
    // Another option would be to store the variables in a 4-dimensional 
    // array. #define C(i,j,k,w)                              \ varsC[(i) * nbElements * 2 * 50 + (j) * 2 * 50 + (k) * 50 + (w)] IloNumVarArray varsC(env); 
    
    for (i = 0; i < nbElements; i++) 
    { 
    
    for(j = 0; j < nbElements; j++) 
    { 
    
    for (k = 0; k < 2; k++) 
    { 
    
    for(w = 0; w < 50; w++) 
    { std::ostringstream s; s << 
    "C_" << i << 
    "_" << j << 
    "_" << k << 
    "_" << w; varsC.add(IloBoolVar(env, s.str().c_str())); 
    } 
    } 
    } 
    }   
    // Create equation 1. 
    // \sum_{k=2} \sum_{w} C_ij,k,w = 3 ∀(i, j) ∈ Z 
    
    for (i = 0; i < nbElements; ++i) 
    { 
    
    for (j = 0; j < nbElements; ++j) 
    { IloExpr sum(env); 
    
    for (k = 0; k < 2; ++k) 
    
    for (w = 0; w < 50; ++w) sum += C(i, j, k, w); std::ostringstream s; s << 
    "equation1_" << i << 
    "_" << j; model.add(IloRange(env, 3, sum, 3, s.str().c_str())); sum.end(); 
    } 
    }   IloCplex cplex(model); cplex.exportModel(
    "model.lp");
    

    After this, check that the constraints in "model.lp" look as expected.
    If that is correct then define variables X in a similar way and create equation 2.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Need Help in Debugging cplex code

    Posted 04/19/12 09:20 PM

    Originally posted by: cplex-newbie


    Hi Daniel,

    Thanks for identifying the problems and also for the tips.
    It was indeed really helpful in understanding cplex.

    I have tried to address some of your comments but I would need some help on the rest. Please see inline at <shireesh>

    <Comment-1> 1. The way in which you reuse con to build equation 2 is wrong. Actually, equation 2 is a quadratic expression, so it is not enough to set only linear coefficients.
    <shireesh> There was a problem with the code for equation 2. I have changed the code so that the constraint code will be hit for just the outermost for loop. So this will be a linear equation.

    <Comment-2> 2. You need to explicitly add() each constraint that you want to appear in the model.
    <shireesh> I now add all the constraints I have created so far to the model before i solve the cplex.

    <Comment-3> 3. For equation 3 you use

    conconCount <= 2*u_w[w]

    that will not do what you intended. Note that you are writing "constraint less than or equal to expression" here. What you want is "expression less than or equal to expression".
    There may be more problems in your code :-(
    <shireesh> I have fixed this problem.

    Here are a few tips to make things easier:
    <Comment-4> 1. Name all your variables, any maybe all your constraints as well.
    <shireesh> I haven't done this, but I understand that this would help in debugging.

    <Comment-5> 2. In the beginning don't try to be too clever by re-using constraints, expressions or whatever. Instead use a new constraint etc. every time you create something new.
    <shireesh> I use a new constraint everytime and also the variables.

    <Comment-6> 3. Write out your model to an LP file (cplex.exportModel("model.lp")) and look at that file with a text editor. Double check that the model is what you expected.
    <shireesh> I did this and I was able to look at the model.lp file and relate better.

    <Comment-7> /* Create the C variables. We put all the variables into one flat array and use macro C to access them by index.*/
    <shireesh> I have done this and created one single variable array and I index them for the equations 1,2 and 3.

    I need some help in representing equation 4 in cplex.
    Equation 4:
    V >= w*U^w ∀w

    Notation :
    U^w is a binary variable which indicates whether wavelength w is used.
    V is the number of wavelengths used.

    I have something equivalent to
    IloExpr obj(env);
    for(w = 0; w < W; w++) {
    obj += var[w];
    }
    model.add(IloMinimize(env, obj)); //minimize the Used Wavelength

    Is this correct ? If not, how do I write this objective function ?

    Once again, thanks for replying to my previous post.

    Regards,
    Shireesh
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Need Help in Debugging cplex code

    Posted 04/19/12 11:26 PM

    Originally posted by: cplex-newbie


    Hi Daniel,

    I have also attached my model.lp file which I exported from my progam.

    Thanks,
    Shireesh
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Need Help in Debugging cplex code

    Posted 04/20/12 01:58 AM

    Originally posted by: SystemAdmin


    This file is not too useful unless you name your variables.
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Need Help in Debugging cplex code

    Posted 04/20/12 02:02 AM

    Originally posted by: SystemAdmin


    > I need some help in representing equation 4 in cplex.
    > Equation 4:
    > V >= w*U^w ∀w
    >
    That would be something like this
    for (w = 0; w < ...; ++w)
       model.add(V >= w * U[w]);
    

    Note that Concert-C++ overloads most of the relational operators and allows you to write constraints like you would write them on paper.

    > Notation :
    > U^w is a binary variable which indicates whether wavelength w is used.
    > V is the number of wavelengths used.
    >
    > I have something equivalent to
    > IloExpr obj(env);
    > for(w = 0; w < W; w++) {
    > obj += var[w];
    > }
    > model.add(IloMinimize(env, obj)); //minimize the Used Wavelength
    >
    What is it that you want to minimize? The above will minimize the sum of the variables over which you loop. If you intend to do that then your code is correct.

    Maybe also have a look at the numerous examples that come with CPLEX, in particular the ilolpex?.cpp and ilomipex?.cpp examples. They should illustrate how to build models.
    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Need Help in Debugging cplex code

    Posted 04/20/12 08:10 AM

    Originally posted by: cplex-newbie


    Hi Daneil,

    Thanks, for the prompt reply.
    I am attaching the model.lp file with the variable names.

    When I run this model, it says
    Warning: Output names have been modified to conform to LP format.
    No Solution
    Error: CPLEX Error 1217: No solution exists.

    Any tips on how I can narrow down the issue ?

    Thanks,
    Shireesh
    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: Need Help in Debugging cplex code

    Posted 04/20/12 08:50 AM

    Originally posted by: SystemAdmin


    Please take a look at this section of the user's manual. It describes in detail how you can analyze infeasibility of models.
    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: Need Help in Debugging cplex code

    Posted 04/20/12 09:09 AM

    Originally posted by: cplex-newbie


    Thanks Daniel. I will go through the link.
    #CPLEXOptimizers
    #DecisionOptimization