2

I am facing problem in writing constraints using IloRange. There are no compilation error but the code is not running.

In the code, ad_sales.u_j[j] is a variable, while demand[j] and lambda[j] are parameters. The syntax I have used for ILoRange is ilorange(env, lower bound, variable, upper bound).

I need to find duals of a set of constraint, which is why i switched to IloRange. If I use the following command instead of Ilorange the code is running fine.

for(IloInt j=0; j<data.n; j++)
model_ad.add(ad_sales.u_j[j] <= demand[j]*lambda[j]);

But following is not working:

IloRangeArray cons(env, data.n); 
for(IloInt j=0; j<data.n; j++)
{
    cons.add(IloRange(env, 0, ad_sales.u_j[j],demand[j]*lambda[j]));
    //model_ad.add(cons[j]);
}
model_ad.add(cons);

Really appreciate the help.

Thank You

S_R
  • 21
  • 2

2 Answers2

1

Try to call IloExpr like you did before, i.e.,

IloRangeArray cons(env, data.n); 
for(IloInt j=0; j<data.n; j++) {
    cons.add(d_sales.u_j[j] <= demand[j]*lambda[j]);
}
model_ad.add(cons);

The problem with your way is that IloRange expects IloNumExprArg, not just IloNumVar.

Edit:

add() method of IloModel expects IloExtractableArray. I tried to add a constraint object to IloRangeArray and got no matching function for call to ‘IloRangeArray::add(IloConstraint&)’ error message. I would suggest to use IloConstraintArray, e.g.

IloConstraintArray cons(env); 
for(IloInt j=0; j<data.n; j++) {
    cons.add(d_sales.u_j[j] <= demand[j]*lambda[j]);
}
model_ad.add(cons);

It worked with my example.

serge_k
  • 1,772
  • 2
  • 15
  • 21
  • I tried your suggested method, the constraints are getting added in the IloRange array, but the code is unable to add them in the model_ad. The code stops working at `model_ad.add(cons)` – S_R Jun 13 '16 at 09:57
  • 1
    I really appreciate the help......I tried `IloConstraintArray cons(env)` method.... It is working fine.. But the problem is I want to get duals of those constraint set. The getDual expects an IloRangeArray and is not working with an IloConstraintArray .... The code stops at `for(IloInt j=0; j – S_R Jun 13 '16 at 11:59
  • Well, in that case populate your `cons` array first, then add constraints one by one in a cycle `for(IloInt j=0; j – serge_k Jun 13 '16 at 12:25
  • I tried doing that, but again while running the code, it is stopping at addition of constraints to the model..... – S_R Jun 14 '16 at 12:41
0

After facing this issue and quite some debugging, I found the issue (at least for my case).

The thing is, IloRangeArray cons(env, data.n); seems to populate cons with data.n "empty" IloRange objects. (By "empty", I mean that the object is there according to the debugger, but its _impl field is set to null).

So, when calling cons.add(...), it is appending to these existing empty constraints. Which means you end up with a bunch of empty IloRange objects, followed by the actual constraints you are adding.

This messes up the call to model_ad.add(cons); and causes a segmentation fault (at least in my case).


Solution

What worked for me: Declaring the IloRangeArray without setting the size parameter, i.e. using:

IloRangeArray cons(env); 

Instead of

IloRangeArray cons(env, data.n); 

Hope this helps any future reader.

Anis R.
  • 6,656
  • 2
  • 15
  • 37