Skip to main content

How to get value from decision matrix after optimization?

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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.
  • Jaromił Najman
    • Gurobi Staff

    You can get the optimal solution value by accessing the X attribute of a given variable.

    The following code snippet shows an example of how to construct such a numpy array. Please note that it is probably not the most Pythonic way to achieve it. Maybe someone could provide a more Pythonic way.

    import gurobipy as gp
    from gurobipy import GRB
    import numpy as np

    m = gp.Model()

    I = 3
    J = 2

    MM = m.addVars(I,J,vtype=GRB.BINARY, name="MM")

    m.addConstr(MM[1,1] == 1)
    m.setObjective(gp.quicksum(MM))
    m.optimize()

    array = np.array(0)
    if m.SolCount > 0:
        # get tupledict holding solution values
        L = []
        for i in range(I):
            K = []
            for j in range(J):
                K.append(MM[i,j].X)

            L.append(K)
        
        array = np.array(L)

    print(array)
    0

Post is closed for comments.