Skip to main content

Is there a limit to the declaration of Gurobi variables?

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Eli Towle
    Gurobi Staff Gurobi Staff

    Have you looked at the Declaration of 3D variables with C++ forum post? This sounds like the same issue.

    0
  • Murilo Machado
    Gurobi-versary
    First Question
    Conversationalist

    I'm not very experienced in C++, but I've implemented 3D matrix in C++ and I didn't have any problems.

    What intrigues me in this case is that Ex1 works and Ex2 doesn't.

    Ex1: GRBVar vv[6][50][45][45];
    Ex2: GRBVar vv[6][133][45][45];

    And there is a relationship with the value of each dimension of the variable.

    For example, Ex3 doesn't work and Ex4 works:
    Ex3: GRBVar v[300][300][10][10];
    Ex4: GRBVar v[30][30][10][10];

    I know it's possible to boot like this:

    GRBVar *vv = new GRBVar **[6];

    for (int i = 0; i < 6; i++)
    {
    vv[i] = new GRBVar **[133];

    for (int j = 0; j < 133; j++)
    {
    vv[i][j] = new GRBVar *[45];

    for (int k = 0; k < 45; k++)
    {
    vv[i][j][k] = new GRBVar[45];
    }
    }
    }


    It works !

    But it doesn't answer my question above about variable limits.


    But anyway Eli Towle , thank you very much for your attention.


    Murilo oliveira MACHADO

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    There is no limit to the number of variables you can add to your Gurobi model. When you declare your array like this:

    GRBVar vv[6][50][45][45];

    you allocate the array on the stack. There is a limit to the size of your stack, which depends on your operating system. Each GRBVar element consumes 8 bytes of memory.

    For example: on my MacBook, I have a stack size of 8 megabytes. I can allocate the following array on the stack without issue:

    GRBVar vv[6][86][45][45];

    The amount of memory required to store this array is \(8(6\cdot 86 \cdot 45 \cdot 45)\)=8,359,200 bytes, or ~7.97 megabytes. However, when I try to allocate the following (slightly larger) array, I encounter a segmentation fault like you did:

    GRBVar vv[6][87][45][45];

    This is not unexpected, because the array requires \(8(6\cdot 87 \cdot 45 \cdot 45)\)=8,456,400 bytes (~8.06 megabytes) of memory to store, which is larger than my stack size.

    1
  • Murilo Machado
    Gurobi-versary
    First Question
    Conversationalist

    OI Eli Towle ,

     

    Thank you very much for the information

     

    att. Murilo O. MACHADO

     

     

    0

Post is closed for comments.