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++)

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 14 Feb '12, 02:48

MBansal's gravatar image

accept rate: 0%

edited 14 Feb '12, 14:01

fbahr's gravatar image

fbahr ♦

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.]

(14 Feb '12, 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.

(14 Feb '12, 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.

(14 Feb '12, 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).

(14 Feb '12, 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.]

(14 Feb '12, 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!

(14 Feb '12, 19:56) MBansal
showing 5 of 6 show 1 more comments

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.


answered 14 Feb '12, 17:59

Paul%20Rubin's gravatar image

Paul Rubin ♦♦
accept rate: 19%

edited 14 Feb '12, 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.

(14 Feb '12, 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



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



Asked: 14 Feb '12, 02:48

Seen: 4,378 times

Last updated: 28 Jan '14, 12:01

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