Skip to main content

gurobi enviroment,model and C++ header file issue



  • Tobias Achterberg
    Gurobi Staff Gurobi Staff

    What your code is implicitly doing is to create a "model" object with the default constructor. Then, you are creating another GRBModel object passing the "env" to it and assign this using the assignment operator "=" to the existing "model". But the assignment operator for GRBModel objects is not implemented (Gurobi does not support this operation for good reason).

    What you need to do instead is to use pointers. Declare your "model" variable to be a pointer to a GRBModel object, i.e.,

    GRBModel *model;

    Then, in your "inti" function, use the "new" operator:

    model = new GRBModel(env);

    Finally, you need to make sure that you destruct the "model" pointer again using the "delete" operator when you don't use it anymore.


    But there might be a simpler solution. If you need to solve a Gurobi model inside your cut separation procedure, you can just use local variables to do this. Like this:

    void separatecuts(GRBEnv env)
       GRBModel model = GRBModel(env);

       ... use model ...

    The last time I wrote C++ is already quite a while ago for me, but I still hope that what I said is correct.




  • Quanmeng Wang
    First Comment
    First Question

    Thanks a lot! Now on some small instances my code works.


    Just one quick question.. When the following code is excuted,

    model = new GRBModel(env);

    Every time the system allocate some new location to pointer model, but we have no idea what that location previously stored( as we are dynamically allocate and delete model ).

    Is it safe to just use the new location or should we clear everything that originally stored at new location?

    I did not find any gurobi function that clears variables and constraints in some GRBModel... what should I do?


Please sign in to leave a comment.