Skip to content
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

Closed
MirelEce opened this issue Jul 17, 2020 · 4 comments

Comments

@MirelEce
Copy link

MirelEce commented Jul 17, 2020

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:

k=3
yn=[5, 6, 8]
yp=[12, 65, 46]
epsilon=0.0001

m = Model(solver = GurobiSolver())
@variable(m, w[1:k] >= 0)
@variable(m, beta >= 0)
@variable(m, t >= 0)
@objective(m, Min, t)
@constraint(m, sum{w[i], i in 1:k}==1)
@constraint(m,sum{w[i]*yn[i], i in 1:kk} >= sum{w[i]*yp[i], i in 1:kk} + beta)
@constraint(m, t >= beta)
@constraint(m, beta >= epsilon)

wf=[0.2,0.5,0.3]
eps=0.001
i=1
while flag == 0
    @variable(m, z, Bin)  #Here I actually want to create a different variable zi (z1,z2,z3... as it iterates) 
    @constraint(m, w[1]-wf[1]<= -1*eps+(1+eps)*z) #Then there will be zi here as well
    @constraint(m, w[1]-wf[1]>= eps-(1-z)*(1+eps)) #It will be zi here too
    statusm=solve(m)

    if string(statusm)=="Optimal"
        wf = getvalue(w)
        println(wf)
    elseif string(statusfm)=="Infeasible"
        flag=1
    end
end

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!

@odow
Copy link
Member

odow commented Jul 17, 2020

  1. is the correct way to go. The names are printed as anon, but the variables are different.

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.

@MirelEce
Copy link
Author

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.

@odow
Copy link
Member

odow commented Jul 17, 2020

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.

@MirelEce
Copy link
Author

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants