GRBaddconstr produces error 10006 (C API)

531 views
Skip to first unread message

Tayo Ajayi

unread,
Apr 25, 2016, 6:07:04 PM4/25/16
to Gurobi Optimization
Hello,

This is my first time posting, so feel free to include comments about what to include to facilitate assistance in getting an answer.

I am using the C API for Gurobi. Essentially, I have created a function in C called lpFunc:


struct lpResult * lpFunc(double *b, double **A, double *obj, char *vtype, int m, int n, char *conType, int reportBasis, struct lpResult *output)


Basically, I needed a function in which if I passed a constraint matrix, objective function, RHS, variable types and constraint types, I could retrieve the solution and objective value (in the struct). 


I believe that the lines important to this question are:


    GRBenv *env = malloc(sizeof(double)*10000);//NULL; //create gurobi environment

    GRBmodel *model = NULL; //create model variable

    int error = 0;    

    

    error = GRBloadenv(&env, "");

    if (error || env == NULL) {

        fprintf(stderr, "Error: could not create environment\n");

        exit(1);

    }

    

    /* Create an empty model */

    error = GRBnewmodel(env, &model, "CG_Proc", 0, NULL, NULL, NULL, NULL, NULL);

    if (error) goto QUIT; 

    

    error = GRBsetintparam(GRBgetenv(model), GRB_INT_PAR_OUTPUTFLAG, 0);

    if (error) goto QUIT;

    

    /* Add variables */

    

    error = GRBaddvars(model, n, 0, NULL, NULL, NULL, obj, NULL, NULL, vtype, NULL);

    //

    /* Change objective sense to maximization */

    

    error = GRBsetintattr(model, GRB_INT_ATTR_MODELSENSE, GRB_MAXIMIZE);

    if (error) goto QUIT;

    

    

    /* Integrate new variables */

    error = GRBupdatemodel(model);

    if (error) goto QUIT; /////WILL NEED TO UNCOMMENT THIS LATER

    

    error = GRBsetintparam(env, "Method", -1);

    

    error = GRBsetintparam(env, "InfUnbdInfo", 1); ///collect ray if unbounded

    if (error) goto QUIT;


    int row = 0;

    

    ////add constraints

    for(row = 0; row < m; row++)

    {

        int *ind = malloc(sizeof(int)*n*2);

        double *val = malloc(sizeof(double)*2*n);

        int col = 0;

        for(col = 0; col < n; col++)

        {

            ind[col] = col;

            val[col] = A[row][col];

        } 

        error = GRBaddconstr(model, n, ind, val, conType[row], b[row], NULL);


        free(val); free(ind);

        printf("");

        if (error) goto QUIT; 

    }


I know that I could have declared ind and val outside of the loop. However, that does not seem to be the issue. In most cases, this adds the right constraints and the rest of the function solves and returns the solution. In a particular case, I get error code 10006, which says, "Tried to query or set an attribute, but one or more of the provided indices (e.g., constraint index, variable index) was outside the range of valid values."

The only thing that changes are the parameters that I send into lpFunc.

Can someone tell me why I would receive this error only some of the time? Additionally, if someone could help me find how I could check what the range of valid values are, that might also help me find the answer.

Thanks,
Tayo

Tobias Achterberg

unread,
Apr 25, 2016, 7:23:26 PM4/25/16
to gur...@googlegroups.com
Hi Tayo,

first of all, the malloc for GRBenv is not needed (it will produce a memory leak
in your code). The GRBloadenv() method creates the environment for you. So, you
should initialize env = NULL.

Second, you won't be able to deal with medium or large sized models if you pass
in the constraint matrix A as a dense two-dimensional array. For large models
you need to use a sparse representation where the zero entries do not exist. But
if you continue to use a dense representation, then you can significantly
improve your code: you only need to allocate and initialize your ind[] array
once to contain values 0..n-1, and the val[] array is not needed at all, because
you can just pass A[row] into GRBaddconstr().

Third, your code is missing the error code check for the GRBaddvars() call. This
would be rather important for your question.

You did not include the code where you solve the model and then query the
result. Are you sure that you get the error within the code that you listed? And
if so, which GRB method returns this error?


Regards,

Tobias

Renan Garcia

unread,
Apr 25, 2016, 7:23:49 PM4/25/16
to gur...@googlegroups.com
It appears you aren't checking the return value for GRBaddvars(), which can fail if you pass invalid values in the vtype array, for example. If that fails, then the indices wouldn't exist. Can you check if that's the problem?

--

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

Tayo Ajayi

unread,
Apr 26, 2016, 8:04:14 AM4/26/16
to Gurobi Optimization
Hi thanks for the quick response. Yes, I found I was missing the error check for GRBaddvars. Apparently, I was missing a variable type and thus it could not add the variables. 

I will also switch the env initialization back to NULL.
Reply all
Reply to author
Forward
0 new messages