Decision Optimization

Decision Optimization

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

 View Only
  • 1.  CPLEX MIP add cuts

    Posted Tue March 29, 2011 07:53 AM

    Originally posted by: Niki_P


    Dear Experts,

    I've just recently started using CPLEX 8.1 and basically what I'm trying to do is the following.

    Read LP file
    while (objective value < target value) {
    mipopt()
    get solution (getmipx(), getmipobjval(), etc)
    add a row using the solution
    }
    write LP file

    I have managed to read the LP file and optimize and get the objective value and solution. The problem I have is with using the solution to add row. When I use the CPXaddrows() command, the row is added (I checked this by having it write to LP file after the function). But when I use the mipopt() command again it doesn't work resulting in segmentation fault. I then tried CPXadduserscut() command and again the constraint was added but this time mipopt() just provide the same solution which resulting in the while loop looping forever. Could you please provide me with suggestions here. After I add the cut do I need to do anything with CPXsetintparam() or any other settings? what I did was add cut then opt right away.

    Thank you very much for your time,
    Regards,
    Niki
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: CPLEX MIP add cuts

    Posted Tue March 29, 2011 02:09 PM

    Originally posted by: SystemAdmin


    > Niki_P wrote:
    > Dear Experts,

    ... and others ... :-)
    >
    > I've just recently started using CPLEX 8.1

    You should seriously consider upgrading to 12.2. It's orders of magnitude faster on MIPs.

    > and basically what I'm trying to do is the following.
    >
    > Read LP file
    > while (objective value < target value) {

    Hopefully you are minimizing; otherwise this will be an infinite loop.

    > mipopt()
    > get solution (getmipx(), getmipobjval(), etc)
    > add a row using the solution
    > }
    > write LP file
    >
    > I have managed to read the LP file and optimize and get the objective value and solution. The problem I have is with using the solution to add row. When I use the CPXaddrows() command, the row is added (I checked this by having it write to LP file after the function). But when I use the mipopt() command again it doesn't work resulting in segmentation fault.

    As far as I can see, this approach should work. Try using CPXcheckaddrows to verify that the arguments to CPXaddrows are correct (so that you're not adding a null row or something) -- although the LP file printing correctly ought to eliminate most mistakes.

    > I then tried CPXadduserscut() command and again the constraint was added but this time mipopt() just provide the same solution which resulting in the while loop looping forever.

    You're certain that the cut you are adding cuts off the previous solution? And did you turn off CPX_PARAM_PRELINEAR?

    > Could you please provide me with suggestions here. After I add the cut do I need to do anything with CPXsetintparam() or any other settings? what I did was add cut then opt right away.

    No, you should be able to add a constraint and then reoptimize without any other manipulations.

    Paul
    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: CPLEX MIP add cuts

    Posted Wed March 30, 2011 07:39 AM

    Originally posted by: Niki_P


    Thank you so much sir for your response. Regarding your response could I please ask you a few more questions.

    > Read LP file
    > while (objective value < target value) {

    Hopefully you are minimizing; otherwise this will be an infinite loop.

    > mipopt()
    > get solution (getmipx(), getmipobjval(), etc)
    > add a row using the solution
    > }
    > write LP file
    >
    > I have managed to read the LP file and optimize and get the objective value and solution. The problem I have is with >using the solution to add row. When I use the CPXaddrows() command, the row is added (I checked this by having it write >to LP file after the function). But when I use the mipopt() command again it doesn't work resulting in segmentation >fault.

    >As far as I can see, this approach should work. Try using CPXcheckaddrows to verify that the arguments to CPXaddrows >are correct (so that you're not adding a null row or something) -- although the LP file printing correctly ought to >eliminate most mistakes.

    Row was definitely added and yes sir the problem is minimization. But I'm still getting the segment fault. After adding the constraint and before doing the mipopt again do I suppose to do anything else in between such as free problem, null pointers etc?

    > I then tried CPXadduserscut() command and again the constraint was added but this time mipopt() just provide the same solution which resulting in the while loop looping forever.

    >You're certain that the cut you are adding cuts off the previous solution? And did you turn off CPX_PARAM_PRELINEAR?

    Yes sir I turned off CPX_PARAM_PRELINEAR and I checked the cuts in the lp file but the problem is every loop provide the same x value and the objective value. Again do I suppose to do anything else before re-optimizing?

    Thank you very much sir for your time.
    Regards,
    Niki P
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: CPLEX MIP add cuts

    Posted Wed March 30, 2011 10:01 AM

    Originally posted by: SystemAdmin


    As Paul said, the first thing you should do is upgrade to a more recent version of CPLEX. It will be difficult to help with an old version that probably nobody has installed. Note that since version 12 CPLEX is free for academic use.

    What exactly is the cut you are adding? Are you sure that it cuts off the current solution? Did you check the return values of all functions that you call? Did you enable screen output (CPXsetintparam(env, CPX_PARAM_SCRIND, CPX_ON))? Does CPLEX print any error messages when you enable that?

    I have this file called example.lp
    Minimize
     obj: x + y
    Subject To
     c1: x + y >= 5.5
    Bounds
     0 <= x
     0 <= y
    General
     x y
    End
    

    With this file the following code works perfectly well (CPLEX 12.2):
    #include <stdio.h>
    #include <assert.h>
     
    #include <ilcplex/cplex.h>
     
    int
    main(void)
    {
       CPXENVptr env;
       CPXLPptr lp;
       int status;
       double targetvalue = 1000.0;
       double objval = 0.0;
     
       env = CPXopenCPLEX(&status);
       assert(env && !status);
     
       lp = CPXcreateprob(env, &status, "example");
       assert(lp && !status);
     
       status = CPXreadcopyprob(env, lp, "example.lp", NULL);
       assert(!status);
     
       while (objval < targetvalue) {
          int const zero = 0;
          int ind[2];
          double val[2];
     
          /* Optimize the problem. */
          status = CPXmipopt(env, lp);
          assert(!status);
     
          /* Make sure that CPLEX stopped due to finding an optimal solution. */
          status = CPXgetstat(env, lp);
          assert(status == CPXMIP_OPTIMAL);
     
          /* Get objective function value. */
          status = CPXgetbestobjval(env, lp, &objval);
          assert(!status);
          printf("objval = %f\n", objval);
     
          /* Add constraint x + y >= objval + 1 */
          ind[0] = 0; val[0] = 1.0;
          ind[1] = 1; val[1] = 1.0;
          objval += 1;
          status = CPXaddrows(env, lp, 0, 1, 2, &objval, "G", &zero,
                              ind, val, NULL, NULL);
          assert(!status);
       }
     
       CPXfreeprob(env, &lp);
       CPXcloseCPLEX(&env);
     
       return 0;
    }
    

    It keeps adding constraints and re-optimizing until the objective function value is 1000. No segfaults, no other problems.
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: CPLEX MIP add cuts

    Posted Wed March 30, 2011 05:43 PM

    Originally posted by: SystemAdmin


    > Niki_P wrote:
    >
    > Row was definitely added and yes sir the problem is minimization. But I'm still getting the segment fault. After adding the constraint and before doing the mipopt again do I suppose to do anything else in between such as free problem, null pointers etc?

    No, you should not need to do anything else, and freeing/ending the wrong thing could cause a null pointer problem. I'm not sure if null pointers can cause segment faults, but I think it's possible. I'd suggest you compare your code to Daniel's and see if you are doing anything he is not that could impact the problem (particularly by causing a pointer to point to the wrong thing).
    >
    > > I then tried CPXadduserscut() command and again the constraint was added but this time mipopt() just provide the same solution which resulting in the while loop looping forever.
    >
    > >You're certain that the cut you are adding cuts off the previous solution? And did you turn off CPX_PARAM_PRELINEAR?
    >
    > Yes sir I turned off CPX_PARAM_PRELINEAR and I checked the cuts in the lp file but the problem is every loop provide the same x value and the objective value. Again do I suppose to do anything else before re-optimizing?

    No, you should not have to do anything before reoptimizing. My question here was not whether you have confirmed that the cuts are in the .lp file; what I'm asking is whether you have confirmed (by substituting the previous solution into them) that the new cuts actually make the previous solution infeasible. CPLEX seems not to think so.

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: CPLEX MIP add cuts

    Posted Fri April 01, 2011 07:16 AM

    Originally posted by: Niki_P


    Thank you to both of you so much for all your responses. I have managed to upgrade to 12.2 and all those posted questions were solved. However, I have encountered another problem where after a certain number of loops (about 51), I have encountered a segfault before the objective value even reaches the target value. I cannot figure out why this happened since at the point where segfault occurred, the objective value and x were given and were the right answer. Could you please kindly suggest to me how could I look to solve the problem?

    Regards,
    Niki P
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: CPLEX MIP add cuts

    Posted Fri April 01, 2011 07:22 AM

    Originally posted by: Niki_P


    Just to add on the previous post. I notice something strange, which might cause the segfault. I tried 5 different LP files with this same code and all resulted in segfault after the 89th row is added.
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: CPLEX MIP add cuts

    Posted Fri April 01, 2011 07:27 AM

    Originally posted by: SystemAdmin


    Interesting. Do you happen to use an array with a static size?
    The best way to pin that down would be to run your program through a debugger to see at exactly which point the segfault occurs.
    Can you do that? Do you need help with that?
    If you need a more detailed analysis from us then you will probably need to post your code here. Or at least the relevant part of it. Otherwise it will be difficult to analyze your problem.
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: CPLEX MIP add cuts

    Posted Fri April 01, 2011 07:53 PM

    Originally posted by: SystemAdmin


    I'm no expert on segfaults, since they pretty much never happen in Java (one more reason to like Java), but my impression is that null pointers or pointers that point at the wrong thing (which might happen if an index went out of range undetected) can cause them. So, following up on Daniel's point about a fixed dimension array, you might try adding code to check pointers. It's been a long time since I crossed swords with C++, but it seems to me that either there was a compiler option or a library you could add that would do run-time testing to make sure that array indices stayed in bounds. (Might be part of valgrind.)

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization