Skip to main content

Retrieve a specific subset value of variables

Answered

Comments

2 comments

  • Riley Clement
    • Gurobi Staff Gurobi Staff

    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 1
    largest_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 those t values for which the a,b,c indices match and whose variable value is 1 (or close enough to 1).  The list of t values is then sorted from smallest to largest and we take the last one using the -1 index.

    Note this approach will result in a list index out of range error if there are no t values which satisfy the condition.

    - Riley

    0
  • Sander Mertens
    • Gurobi-versary
    • Curious
    • Conversationalist

    Thank you, works perfect!

    0

Please sign in to leave a comment.