examples for Reference Manual

276 views
Skip to first unread message

Jason

unread,
Apr 6, 2012, 7:59:17 PM4/6/12
to Gurobi Optimization
Hi,
I'm a new student user of Gurobi for part of my 4th year undergraduate
project. Having looked through and run some of the example c++ code, I
then tried modifying them to get a feel for inputting LPs into Gurobi.
Ran into a few problems along the way, so decided to go through the
Reference Manual for c++. Unfortunately, I can't seem to find where
the examples for the functions referred to in the Manual are stored.
If somebody could direct me to some example files for the functions,
that would be great.

Specifically, the problem I've been having is with GRBgetconstrs.
On a slightly edited mip1_c++ example model (2 variables, 4 columns in
the constraint matrix), I've called

double cval[8];
GRBgetconstrs(model, NULL, NULL, NULL, cval, NULL, NULL);

This was to try and check the constraint values that I entered.
This results in the following error: error C2664: 'GRBgetconstrs' :
cannot convert parameter 1 from 'GRBModel' to 'GRBmodel *'
I've tried passing &model instead, which results in: cannot convert
parameter 1 from 'GRBModel *' to 'GRBmodel *'

Cheers,
Jason

Christopher Maes

unread,
Apr 10, 2012, 11:46:18 AM4/10/12
to gur...@googlegroups.com
> Unfortunately, I can't seem to find where
> the examples for the functions referred to in the Manual are stored.
> If somebody could direct me to some example files for the functions,
> that would be great.
>
> Specifically, the problem I've been having is with GRBgetconstrs.
> On a slightly edited mip1_c++ example model (2 variables, 4 columns in
> the constraint matrix), I've called
>
>    double cval[8];
>    GRBgetconstrs(model, NULL, NULL, NULL, cval, NULL, NULL);

Hi Jason,

You can browse the examples online here:
http://gurobi.com/documentation/4.6/example-tour/

The example files are included in the installation under
/path/to/gurobi461/arch/examples. For example, with win64 and the
default installation path, the examples would be located in the
directory C:\gurobi461\win64\examples.

It looks like you are trying to call the GRBgetconstrs() function
associated with the C API. If you are using C++, you should call
getConstrs() from the C++ API. The documentation for that function is
here:
http://gurobi.com/documentation/4.6/reference-manual/node123

Note that this function returns a GRBConstr object. To get access to
the GRBLinExpr associated with that constraint you will need to use
getRow() described here:
http://gurobi.com/documentation/4.6/reference-manual/node126

Thanks,
Chris

Jason

unread,
Apr 15, 2012, 3:21:38 AM4/15/12
to Gurobi Optimization
Ah, thanks Chris. Somehow got the c++ and c implementation mixed up.
getRow for the GRBLinExpr does what I wanted.

On another question, in the examples the diet model outlines how to
add constraints. Is there also one for adding columns?
I've tried using model.addVars (only with one variable just to test it
out):

double lb[] = {0.0};
double ub[] = {5.0};
double obj[] = {-500};
char type[] = {'C'};
string name[] = {"x"};
GRBColumn column[1];
double coeffs1[] = {1, 1, 200, 1};
for (int i=0; i<4; i++){
column[0].addTerm( coeffs1[i], model.getConstr(i) );
}
model.addVars(lb, ub, obj, type, name, column, 1);

It compiles, but when running, an error 10003 comes up. The problem
seems to be somewhere in addTerm- it doesn't seem to like
model.getConstr(i) for some reason.
I'm not quite sure what I'm doing wrong here, so if there's any links
to an example of addVars being used in action for me to copy and
modify, that would be great.

Thanks,
Jason

Christopher Maes

unread,
Apr 16, 2012, 1:24:54 PM4/16/12
to gur...@googlegroups.com
> It compiles, but when running, an error 10003 comes up. The problem
> seems to be somewhere in addTerm- it doesn't seem to like
> model.getConstr(i) for some reason.
> I'm not quite sure what I'm doing wrong here, so if there's any links
> to an example of addVars being used in action for me to copy and
> modify, that would be great.

Hi Jason,

The error 10003 corresponds to GRB_ERROR_INVALID_ARGUMENT. You are
probably getting this error because the argument i to getConstr() is
greater than the number of rows (or constraints) in your model. You
need to add the constraints to the model before adding a term to a
column.

If you wish to create your model by defining the nonzeros in each
column, rather than defining constraints, you might want to use the C
matrix interface. The C function GRBaddvar() might be what you are
looking for:
http://gurobi.com/documentation/4.6/reference-manual/node15

Chris

Reply all
Reply to author
Forward
0 new messages