Documentation of Variable Update C++ vs Python interface
AnsweredHi there,
I'm exactly in the situation described by Taejoon Park in
In that post, Matthias Miltenberger offers a nice solution using an update function that is called on a Gurobi variable. In the python section of the reference manual, I cannot find the .update function for variables (I can find it only for models). Is the function documented elsewhere? Is it specific to python?
I'm asking because I would like to do the same from the C++ interface (and add multiple new variables rather than one at a time if possible).
Best,
Jonas
P.S. I'd have liked to add this as a comment on the other post, but there seems to be an issue with "Please sign in to leave a comment.": While I can sign in, I still cannot comment. (The same message appears.)
-
Official comment
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
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
Jonasz0 -
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
it allows to add new variables without adding names for them. So it seems different from the model update function.
Best,
Jonas
0 -
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:
- Initialize a new array of new variables to add via addVars
- 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
Jonasz1 -
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,
Matthias1 -
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
Post is closed for comments.
Comments
6 comments