Get variables by name
OngoingI have variable gamma indexed by a twodimension list base_M (e.g. [1, 1], [1, 2], ..., [20, 2])
gamma = m.addVars(base_M, lb = gamma_lb, ub = gamma_ub, vtype = GRB.BINARY, name = 'gamma')
I am wondering is it possible to get all variables once by calling something like getVarByName('gamma') . I understand that we can get each variable one by one by calling getVarByName('gamma[1, 1]'), getVarByName('gamma[1, 2]'), ..., but this is cumbersome

Hi Wenbo,
We can use list comprehension as below:
[var for var in model.getVars() if "gamma" in var.VarName]
The above will iterate over all variables. To do it more efficiently such that only the variables \(\texttt{gamma}\) are retrieved, we can do it like below:names_to_retrieve = (f"gamma[{i},{j}]" for i in range(20) for j in range(2))
[model.getVarByName(name) for name in names_to_retrieve]Best regards,
Maliheh
0 
Dear Maliheh,
I'm using Gurobi in Python and I'm trying to solve a model that have multiple optimal solution, with one binary and one continuous variable.
y=m.addVars(SC_id,vtype=GRB.BINARY,name='facility')
x=m.addVars(PP_id,SC_id,lb=0,ub=1,vtype=GRB.CONTINUOUS,name='assignment')Now, I want to retrieve decision variables for every optimal solution. I wrote a piece of code as following:
count=0
for aa in range(m.SolCount):
count+=1
m.Params.SolutionNumber = aa
print("solution: ", count)
for var in m.getVars():
if var.xn > 0:
if "assignment" in var.VarName:
print ('%s %g' %(var.varName, var.xn))and this is the results of my code:
solution: 1
assignment[11,1] 1
assignment[12,1] 1
assignment[23,4] 0.6
.
.Now I want to save the [i,j] inside the brackets along with the value of assignment. Because right now I have them in terms of string. Since I want to compute Risk of each solution, I need to have value of decision variables for each solution. If I had one optimal solution, below code would work (R[i,j] was computed before). But now that I have multiple optimal solution, I do not know how to access the decision variables for each solution number.
A=0
for i in PP_id:
for j in SC_id:
if x[i,j].x>0:
A+=x[i,j].x*R[i,j]Thanks for your time,
I really appreciate your help
0 
Hi Maedeh,
The simplest approach would be to manipulate the variable string names to retrieve the indices \(i\) and \(j\). You can modify the assignment variable names, \(\texttt{assignment[i,j]}\) like below:
i, _, j = (
var.VarName.replace("assignment", "")
.replace("[", "")
.replace("]", "")
.partition(",")
)A cleaner approach would be to use regular expressions like below, but the first approach is easier to understand.
import re
pattern = re.compile("assignment\[((?P<i>\d+),(?P<j>\d+))\]")
i, j = pattern.match(var.VarName).groupdict().values()I hope this answers your question.Best regards,Maliheh0 
Hi Maliheh,
Quick question, I wanted to know the value of some of my variables during the optimization using a callback function (where=MIPSOL). However, I get an error during the callback when I want to access the .X attribute of my variables. Is there any reason for that ? Any workaround ? Thank you.
0 
Hi Sophie,
You need to use the method Model.cbGetSolution(vars) to retrieve values from a new MIP solution. There is a clear example of how to do this at the bottom of the documentation page linked above that you can check.
Best regards,
Maliheh
0 
Hi Maliheh,
I saw this solution but the problem is I want to only access specific variables by name and not by the variable object.I tried the following but I still get an error:
names_to_retrieve = (f"{name}[{i}]" for i in range(M))
vars_obj = [model.getVarByName(name) for name in names_to_retrieve]
vars_value = [model.cbGeSolution(var) for var in vars_ob]
print("Name = {}, Objective value = {}".format(name, sum(vars_value))Thank you in advance for your help.
Sophie
0
Please sign in to leave a comment.
Comments
6 comments