# How to modify the coefficients of constraints already added in a model (Ilomodel)

 1 2 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 14 Feb '12, 02:48 MBansal 21●1●3 accept rate: 0% fbahr ♦ 4.6k●7●16 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 ♦ 1 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(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 ♦ 1 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

 2 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 Rubin ♦♦ 14.6k●4●12 accept rate: 19% 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
 toggle preview community wiki

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×191
×21