Skip to main content

Display output 2D variable

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support.
  • Maliheh Aramon
    • Gurobi Staff

    Hi Joël, 

    Assuming \(\texttt{x}\) represents the matrix variables, you can use one of the approaches below to print the values of the MVar object in the current solution.

    x = m.addMVar(shape=(6, 8), name="x")
    # Approach 1: This prints a numpy array with the (i, j) element being the value of
    # the decision variable x[i, j]
    values = m.getAttr("X", m.getVars())
    values = np.array(values).reshape((6, 8))

    # Approach 2: This prints a key-value pair mapping the variable indices to the
    # variable values in the current solution
    values = {(i, j): x[i, j].X for i in range(6) for j in range(8)}

    Best regards, 

    Maliheh

    0
  • Maliheh Aramon
    • Gurobi Staff

    Hi Joël, 

    There is also a third approach that is much neater. You can simply query the attribute \(\texttt{X}\) on the MVar object \(\texttt{x}\) and this will give you a numpy array.

    # Approach 3
    x.X

    Sorry for missing this easy and efficient approach!

    Best regards,

    Maliheh

    0

Post is closed for comments.