Retrieve a specific subset value of variables
AnsweredHello,
Is there a way to retrieve the value of a subset from a specific variable within Gurobi?
For instance, allot of X variables are created in the form of X[a,b,c,t]. After optimization, I want to retrieve for a variable with a specific a,b,c combination (lets say 'X[1,0,0'), the largest value of t of all the instances where this X[1,0,0,t] = 1.
-
Hi Sander,
There are several ways you could do this in python (or our interactive interface). Here is one approach:ind = (1,0,0) # the a,b,c values you want to match
eps = 1e-6 # tolerance to use for comparison in case values are not exactly 1largest_t = sorted([t for (a,b,c,t),v in X.items() if (a,b,c) == ind and abs(v.X - 1) < eps])[-1]
In this approach we loop through the
X
dictionary (whose values are variables) with a list comprehension, finding thoset
values for which thea,b,c
indices match and whose variable value is 1 (or close enough to 1). The list oft
values is then sorted from smallest to largest and we take the last one using the -1 index.
Note this approach will result in alist index out of range
error if there are not
values which satisfy the condition.- Riley
0 -
Thank you, works perfect!
0
Please sign in to leave a comment.
Comments
2 comments