-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to define new variables into the model with a while loop using JuMP and Gurobi #341
Comments
Here is the documentation: https://jump.dev/JuMP.jl/v0.21.1/variables/#Anonymous-JuMP-variables-1 p.s., in the future, please post questions like this on the forum: https://discourse.julialang.org/c/domain/opt/13. We try to keep Github issues for bug reports specific to each package. |
Thank you so much for your response. If I use option 2, is there also a way that I can get the optimal values of the anonymous variables somehow? If I use getvalue(z), it only gives me the optimal value of the latest _anon, but not the others. About the forum- I didn't know, my bad! Will do next time. Feel free to delete this post afterwards to avoid any confusion. |
Do something like: z_vec = []
while true
z = @variable(model)
push!(z_vec, z)
# optimize
value(z_vec[end])
end Note that you appear to be using an old version of JuMP. You should upgrade to the most recent version. |
Thank you so much! |
I am new to Julia and Gurobi and trying to add a new variable and two constraints associated with the newly added variable to my model in each iteration in the while loop. I am not sure how to parametrically denote new variables in this case. Consider the following simple example:
Here are the options I tried:
1- leaving it as such -> I am afraid that as the variables' names are not unique, the model would just treat all variables as the same, i.e., all variables denoted as z would be the same.
2- trying to define the variables anonymously by doing: z= @variable(m, binary=true) -> after writing down the model by print(m), I still see that all of the variables are anon, so it does not differentiate between them.
3- Defining a variable as an array z, but I do not know the size of the array in advance, as it will continue to grow until the while loop condition is no longer satisfied (which is when the model I am trying to solve becomes infeasible) and defining a variable of an array whose size is 10^30 is highly inefficient- may not even work as the setup could not be completed..
After searching online, maybe I can use dictionary to solve my problem; however, I am not sure how to do so in my case. I would truly appreciate it if someone could help me with this. Thanks!
The text was updated successfully, but these errors were encountered: