Decision Optimization

Decision Optimization

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


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

How to add a constraint to a problem (C++ API)

  • 1.  How to add a constraint to a problem (C++ API)

    Posted 08/21/15 08:44 AM

    Originally posted by: Namal


    Hello,

     

    i want to add a constraint to my problem, that looks like this.

     

    x[1] + x[2] < 1

     

    now IloRange only allows <=, >= and ==

    and I get an Error if I try to add it like that:  no known conversion for argument 1 from 'IloConstraint' to 'IloRangeArray'

    Documentation says I should use a constructor from IloRange to create a constraint, but this again doest allow lesser or greater. So how can I crate a constraint and add it to my problem, thx.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How to add a constraint to a problem (C++ API)

    Posted 08/21/15 08:53 AM

    Originally posted by: sguazt


    Why not just simply:

    IloConstraint cons(x[1]+x[2] < 1);

    model.add(cons);

    ?


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How to add a constraint to a problem (C++ API)

    Posted 08/21/15 09:15 AM

    Originally posted by: Namal


    yes, this is what I was looking for. thx
     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How to add a constraint to a problem (C++ API)

    Posted 11/05/15 10:03 AM

    Originally posted by: Namal


    Hello again,

    how can I add a name for the constrain. If I add it like that it is always on top with a default name 'c1' but I want it put in the end. Also, is it possible to extract the index of the failed constraint? For instance, if I have

     

    Default row names c1, c2 ... being created.
    Infeasibility row 'c3':  0 <= -1.
    Presolve time = 0.03 sec. (0.00 ticks)
    Failed to optimize LP


    How can I get the index of row "c3"?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: How to add a constraint to a problem (C++ API)

    Posted 11/05/15 10:25 AM

    For questions like this it helps to consult the reference documentation. IloConstraint is a subclass of IloExtractable which has a function setName to set the name of the object.

    To find an object by name you will have to iterate over all objects, see the documentation of IloIterator. There are also several threads in this Forum that describe how to do that. You may want to search for them.

    In order to analyze infeasibility of your model, it may be easier to use the conflict refiner.


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: How to add a constraint to a problem (C++ API)

    Posted 06/25/19 11:43 AM

    Originally posted by: SOPHIA__


    hi, i want to use IloRangeArray to add constraints to the current model in an iteration way.  the codes are as follows:

    IloRangeArray c (env);

    for(t=0;t<nreq;t++)

    {

         if (p[t]>0)

       {

              IloExpr cons(env)

              for(i=0;i<nreq;i++)

                  cons+=x[t];

             c.add(cons<=1);

             cons.end();

       }

        model.add(c);

    }

    I can not use c.add() to add constraints to the current model. Can you help me find out the errors?


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: How to add a constraint to a problem (C++ API)

    Posted 06/25/19 12:08 PM

    To make it easier for us to help you, can you explain why you can not use c.add() to add constraints to the current model? What is the entire error message that you get when you try this?

     

    Also, while your question is somewhat related to the original, it is also different. In the future please consider creating a new question when this is the case, rather than adding to an old one.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: How to add a constraint to a problem (C++ API)

    Posted 06/25/19 12:11 PM

    Originally posted by: SOPHIA__


    Thank you for your advice. I will open a new question next time. I use cplex.export model to output the model, but i find that the new constraints have not been added to the model. 


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: How to add a constraint to a problem (C++ API)

    Posted 06/25/19 12:24 PM

    It sounds like you're trying to do something similar to what is done in the cutstock.cpp example that is installed with CPLEX. Have you taken a look at that?


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: How to add a constraint to a problem (C++ API)

    Posted 06/25/19 12:26 PM

    Originally posted by: SOPHIA__


    yes, i know the cutstock.cpp. I want to add columns to the model, but also i also need to add the rows in the model. 


    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: How to add a constraint to a problem (C++ API)

    Posted 06/25/19 01:49 PM

    I've attached a trivial modification of the ilomipex1.cpp that adds a batch of constraints and solves the model over and over in a loop. If you look at the LP files that are exported you can see that the constraints are being added as expected. If this doesn't help, can you provide a complete small example to show the behavior you are getting?


    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: How to add a constraint to a problem (C++ API)

    Posted 06/26/19 02:45 AM

    The code you posted looks correct to me. In case you still have trouble after following Ryan's advice, can you please

    • Create a new question in this forum
    • in that question post a minimal code example that illustrates your problem. It seems that something may be wrong in the part of the code that you did not post.

    #CPLEXOptimizers
    #DecisionOptimization