メインコンテンツへスキップ

Error when assigning a GRBVar into a 3d dynamically array in c++

回答済み

コメント

7件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Marika Karbstein
    • Gurobi Staff

    Hi Giorgos,

    Could you please share the error you see?
    Your code is not directly reproducible. But if I initialize customers, vehicles, and the Gurobi model 

    int customers = 4;
    int vehicles = 5;
    GRBEnv* env = new GRBEnv();
    GRBModel model = GRBModel(*env);

    I can compile without errors.

    Best regards,
    Marika

    0
  • Giorgos Tsoumos
    • Gurobi-versary
    • Curious
    • Conversationalist

    Yes the error is directing me in this line of gurobi_c++.h which is

    GRBVar& operator=(const GRBVar& xvar) { varRep = xvar.varRep; return *this; }

    and the error is 

    "Exception thrown: read access violation.
    this was 0xFFFFFFFFFFFFFFFF."

     

     

    0
  • Marika Karbstein
    • Gurobi Staff

    Could you post the full example?
    What do you mean by "the debugging shows" the error?

    0
  • Giorgos Tsoumos
    • Gurobi-versary
    • Curious
    • Conversationalist

    Yes sure, 

    I am running this code in VS 2019 and when i start debugging

    then an exception is thrown that points me to this line of "gurobi_c++.h" file 

    "GRBVar& operator=(const GRBVar& xvar) { varRep = xvar.varRep; return *this; }"

    I made some moving from main.cpp to this function in test.cpp 

    namespace PDPTW_Init 
    {

    int vehicles = 10;

    int customers = 101;

        GRBVar***  init_x(GRBModel& model)
        {
            

            GRBVar ***x = new GRBVar * *[customers];

            for (int i = 0; i < customers; i++)
            {
                x[i] = new GRBVar * [customers];
                for (int k = 0; k < vehicles; k++)
                  {
                        x[i][k] = new GRBVar[vehicles];
                   }
            }

            // Initialize x if vehicle goes from node i to node j
            for (int i = 0; i < customers; i++)
            {
                for (int j = 0; j < customers; j++)
                {
                    for (int k = 0; k < vehicles; k++)
                    {
                        std::string msg = "from x_ " + std::to_string(i) + " goes to " + std::to_string(j) + " with vehicle " + std::to_string(k);
                        x[i][j][k] = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "x");
                    }
                }
            }

     

            return x;

        }

    };

    // main.cpp

    #include "PDPTW_Init.h"
    #include "gurobi_c++.h"
    #include<string>


    int main(int argc, char* argv[])
    {


        GRBVar*** x = nullptr;

    try
        {
            GRBEnv env = GRBEnv();
            GRBModel model = GRBModel(env);

            // Set variables
            
            x = PDPTW_Init::init_x(model);

    }

    catch (GRBException e)
        {
            std::cout << "Error code = "
                << e.getErrorCode()
                << std::endl;
            std::cout << e.getMessage() << std::endl;
        }
        catch (...)
        {
            std::cout << "Exception during optimization"
                << std::endl;
        }
      
        return 0;
    }

     

    0
  • Marika Karbstein
    • Gurobi Staff

    I think your error is in init_x() when declaring the variables. The second for-loop should be

    for (int k = 0; k < customers; k++)
    instead of
    for (int k = 0; k < vehicles; k++)
     
    0
  • Giorgos Tsoumos
    • Gurobi-versary
    • Curious
    • Conversationalist

    God bless you! Thank you so much i am struggling with this for days... Solved <3 

    0

投稿コメントは受け付けていません。