Declaring Variable Size Array of variables in Gurobi C++ interface

671 views
Skip to first unread message

Sagnik Das

unread,
Apr 22, 2015, 2:08:48 AM4/22/15
to gur...@googlegroups.com
Dear All,

I am very familiar with C++ interface of CPLEX. I have just started using Gurobi with C++. I wanted to declare a 2 dimensional array of size I*J.

In CPLEX the syntax for such operation is:
IloEnv env;
IloArray<IloNumVarArray> x(env,I);
for(IloInt i=0;i<I;i++)
x[i] = iloNumVarArray(env,J,0,1,ILOFLOAT);

However when I try to replicate in Gurobi using:

GRBVar x[J][I];

it throws an error saying dimensions of the array should be constant.

I wish to create an array whose dimensions are not constant.
Please advise

Sagnik

Sascha S.

unread,
Apr 22, 2015, 12:40:35 PM4/22/15
to gur...@googlegroups.com
While i never used Gurobi with C++, and didn't use C++ for a long time, i think the following is the problem:

GRBVar x[J][I]; -> this is just a standard C++ 2d-array for a given type => this will never work if J,I are variables! This is basic C++ stuff. Have a look at this basic explanation of dynamic memory in C++.
This means: you will need to use the "new" operator and it might be complicated.
There are other possible approaches: and i always recommend std::vector!

"I wish to create an array whose dimensions are not constant." 
- This may describe two different things:
-> A: a constant array (you can't add new items to any dimension) based on some a-priori known dimension-sizes
-> B: a dynamic array (you may add new item to any dimension) -> std::vector is very very useful here!
So you have to be more careful how to call things. 

You might ask: why is this working in CPLEX: well... i don't know. They have to use "new" and "delete" internally if it is working. But this seems to be transparent to the user. I don't know if gurobi supports something similar.

Remember (my opinion):
The problem which you describe is not a problem of gurobi, but your usage of C++. The behaviour you described is what i might expect.

- Sascha 

Tim Chippington Derrick

unread,
Apr 23, 2015, 5:25:04 AM4/23/15
to gur...@googlegroups.com
CPLEX Concert classes like IloArray and IloNumVarArray hide the details of the implementation of the storage used for the array. For sure they use "new" inside.
But use of "new" is certainly not complicated. Just remember to delete the storage after you have finished with the data structure, and use delete[] for deleting an array that you created with new[]. See for example the link which Sascha provided as it is a good explanation.
Tim

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Dr Tim Chippington Derrick
Chippington Derrick Consultants Ltd
Tel: +44 01276 508949
Mob: +44 07971 997948
Reply all
Reply to author
Forward
0 new messages