Skip to main content

How to get value from decision matrix after optimization?

Answered

Comments

1 comment

  • 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

Please sign in to leave a comment.