login about faq
1
1

Hi, how can we modify the coefficients of a constraint (extractable) in a model (IloModel). The IloModel::Iterator returns an object of class IloExtractable, which doesn't have any routine to change the coefficients of the constraint (or may be there is one and I am not able to find it!).

Code to give details:

Step 1)

Adding a constraint:

    IloRangeArray c1(env);
    c1.add(IloRange(env, -IloInfinity, -1.0));
    for(int I=0;I < NumVar; I++)
        c1[0].setLinearCoef(Var[I],-1);
    model.add(c1);
    c1.end();


Step 2)

Now how to modify the coefficients of the constraint, just added, by using the object "model"?


[Edit: X-Post @ IBM ILOG Optimization Forums: How to modify the coefficients of constraints already added in Ilomodel]

asked Feb 14 at 02:48

MBansal's gravatar image

MBansal
211

edited Feb 14 at 14:01

fbahr's gravatar image

fbahr ♦
1.6k37

Hi, @MBansal, I'm not actually sure whether we have a (strict anti-)cross-posting policy here on OR-X; let's assume for a second: we don't have such thing. So I'd recommend to repost your question here, too. [It's good style to paste a link to that repost in your question here, though.]

(Feb 14 at 04:49) fbahr ♦

Obviously I don't make the rules here, but I am reasonable sure we recommend using software specific forums for such related questions, though you will possibly get a qualified answer here too.

(Feb 14 at 04:58) Bo Jensen ♦

I pasted the question literally into a google search, and the first hit looks good to me, but see @Florian's comment below.

(Feb 14 at 05:29) Marco Luebbecke

@Marco: If I'm getting this right, the question here is: Can you extract an IloRange object from an IloModel instance [to call IloRange::setLinearCoef]? @Paul Rubin has authored two blog posts (here and here) that address this problem (accessing model components using an iterator), but [these] require you to have access to the IloCplex instance - Warning: Java!

(Feb 14 at 05:58) fbahr ♦

@MBansal: This said, & IANAE - in anything, IloModel::Iterator grants access to IloExtractable objects; if those represent constraints, IloExtractable::asConstraint returns the corresponding IloConstraint object, let's call it "cons". From here, IloRangeI* impl = dynamic_cast<IloRangeI *>(cons.getImpl()); IloRange rng(impl); should take you where you want to get. [You want to read this and this for further details.]

(Feb 14 at 13:38) fbahr ♦

Thank you to all of you for your time and the replies. @fbahr, using dynamic_cast I can now extract an IloRange object from an IloModel instance. Also, the links, you shared, were very helpful. Thanks a lot!

(Feb 14 at 19:56) MBansal
showing 5 of 6 show all

I don't know why you're using an IloRangeArray for a single range, but that really does not matter.

IloModel::add returns the handle of the added object; store it somewhere, after casting it back to IloRange or IloRangeArray as appropriate (I think the return value is IloExtractable). Do not invoke end() on the stored value. (I'm not sure it's safe to end() c1, either. It is safe to reinitialize c1 later.)

If you know what modifications you want to do, just use IloRange::setLinearCoefficient on each constraint to be modified.

Iterating is only necessary if you do not know which variables appear in the constraint, and need to scan the constraint to find them.

link

answered Feb 14 at 17:59

Paul%20Rubin's gravatar image

Paul Rubin ♦
6.2k210

edited Feb 14 at 18:25

Sir, Thanks for the answer and editing the post. This method also works i.e. do not invoke end() and use IloRange::setLinearCoefficient to modify a constraint. Note: we should not end() c1 if any modification has to be done.

(Feb 14 at 20:20) MBansal
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×24
×3

Asked: Feb 14 at 02:48

Seen: 274 times

Last updated: Feb 14 at 20:20

OR-Exchange! Your site for questions, answers, and announcements about operations research.