Skip to main content

Documentation of Variable Update C++ vs Python interface

Answered

Comments

5 comments

  • Jonasz Staszek
    Community Moderator Community Moderator
    Thought Leader
    Gurobi-versary
    First Question

    Hi Jonas,

    The update method for C++ is documented here.

    You can certainly add multiple new variables and only update the model every now and then. You should actually avoid calling the update method too frequently - you'll just lose time when building the model.

    Best regards
    Jonasz

    0
  • Jonas
    First Comment
    First Question
    Gurobi-versary

    Hi Jonasz,

    thank you for your reply.

    I am aware of the update function that can be called on models. I'm not interested in that function.

    I am looking for an update function that can be called on variables as in the post by Matthias Miltenberger here

    https://support.gurobi.com/hc/en-us/community/posts/4408936057873-Increase-the-number-of-variable-number-by-updating-the-gurobi-model

    it allows to add new variables without adding names for them. So it seems different from the model update function.

    Best,

    Jonas

     

    0
  • Jonasz Staszek
    Community Moderator Community Moderator
    Thought Leader
    Gurobi-versary
    First Question

    Hi Jonas,

    now I get it - Matthias was mentioning an update method on the tupledict storing variables, and not on the variable objects themselves.

    Tupledict as such is an extension of a Python object - I don't think they exist in such a form in the C++ API.

    I'm no expert in C++, but the documentation of the addVars method in C++ suggests though that this method returns an array of variable objects. AFAIK in C++ these are non-modifiable, so you could perhaps do the following:

    1. Initialize a new array of new variables to add via addVars
    2. Initialize a new array in which the contents of both the "existing variables" and "new variables" arrays are stored.

    This way, you will have a new array with all the variables you wish to have.

    Perhaps someone from Gurobi Support team can confirm this or suggest a different approach?

    Best regards
    Jonasz

     

    1
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Jonas(z) :-)

    This should be possible in C++ as well by reallocating the variables array and filling up the new slots with newly created variables. Or you set up the variable array large enough in the beginning and then fill it up iteratively. As Jonasz already explained, the update() method in my previous post is a Python built-in functionality of dictionaries: Built-in Types — Python 3.10.4 documentation

    You may want to check out the diet_c++.cpp example for how to set up these variable arrays in C++.

    Concerning the sing-in issue: This is likely to be a browser issue. Can you please try clearing the cache and then log out and in again? Typically, every user can also reply to other community posts.

    Cheers,
    Matthias

    1
  • Jonas
    First Comment
    First Question
    Gurobi-versary

    Hi Jonasz and Matthias,

    thank you for your help! I'm not sure how to reallocate the variables array (at least without having to add all the constraints again). I ended up using std::vector<GRBVar*> which works fine in my (small) test runs. I guess I was sidetracked by the .update function.

    Best,

    Jonas

    P.S. Clearing the cache did not solve the problem. I tried with Firefox and Microsoft Edge. I can answer here when I first sign in and then find this question by clicking through 'community'. But this is not too important to me, I just wanted to flag it. If the problem is on my side, then never mind :)

    0

Please sign in to leave a comment.