Skip to main content

How to use model.get(A) function?

Answered

Comments

8 comments

  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    You can see a simple example of the getA method in the respective documentation. Once, you have the A matrix, you can then automatically convert the sparse matrix format to a dense one and write it to a CSV file. You could use the todense method of the scipy module.

    import gurobipy as gp
    import scipy as sp
    import pandas as pd

    m = gp.Model("mip1")
    x = m.addVar(ub=2, name="x")
    y = m.addVar(ub=2, name="y")
    z = m.addVar(ub=1, name="z")

    m.setObjective(x + y - z, GRB.MINIMIZE)
    m.addConstr(x + 2 * y + 3 * z <= 4, "c0")
    m.addConstr(x + y >= 1, "c1")

    m.update()
    A = m.getA()
    denseMatrix = pd.DataFrame(data=sp.sparse.csr_matrix.todense(A))
    denseMatrix.to_csv("Amatrix.csv", index=False)

    Best regards, 
    Jaromił

    0
  • Prachi Agrawal
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you so much for your reply. 

    It really helps me a lot. 

    0
  • Prachi Agrawal
    • Gurobi-versary
    • First Comment
    • First Question

    Is it possible to use model.get A function with pyomo optimization?

    How can we use this function?

     

    Could you please give an example of this?

    0
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Pyomo doesn't support all of the features available in the native Gurobi Python API. However, if you're using the "persistent" Gurobi interface, you can access the underlying \(\texttt{gurobipy}\) Model object using the \(\texttt{_solver_model}\) attribute of your GurobiPersistent object. You can use this object to call \(\texttt{gurobipy}\) methods like Model.getA(). For example:

    model = ConcreteModel()
    model.y = Var([1, 2], domain=NonNegativeReals)
    model.my_constraint1 = Constraint(expr=3 * model.y[1] + 4 * model.y[2] <= 12)
    model.my_constraint2 = Constraint(expr=5 * model.y[1] + model.y[2] <= 11)

    solver = SolverFactory("gurobi_persistent", model=model)
    A = solver._solver_model.getA()

     

    0
  • Prachi Agrawal
    • Gurobi-versary
    • First Comment
    • First Question

    I am using GUROBI OPTIMIZATION to solve an LPP with an MPS file format. I am modifying matrix A and I can get matrix A using getA function. Can I extract the cost vector (objective function coefficients) and the RHS vector (the b vector) from an MPS file? I want to solve the LPP by modifying A, the vector b, and the cost coefficient c.

    Is it possible to get the b and c vectors from the MPS file?

    0
  • Matthias Miltenberger
    • Gurobi Staff Gurobi Staff

    Yes, it is possible to get this information using the approach that Eli described. For getting the objective coefficients, you can call the method Model.getObjective(), and to get the right-hand side values, you can call Model.getConstrs() and query the RHS attribute on the individual constraints.

    Please note that not all models follow the textbook structure of \(Ax\leq b, \;x\geq 0\).

    Cheers,
    Matthias

    0
  • Saeed Abdolmaleki
    • Gurobi-versary
    • Curious
    • Conversationalist

    Greetings Scientist

     

    I have been trying to write down the Dual of my Optimization Model. My model is huge it is a part of writing the Dual of my Sub-Problem. for large scale, it took me many times to implement the following code :

     dual.addConstrs(
          gp.quicksum(m.getCoeff(c,v)*u[c.ConstrName] for c in m.getConstrs() if m.getCoeff != 0)<=v.Obj
          for v in m.getVars() if "x" not in v.VarName
        )
     
    Therefore, I was thinking to exclude the m.getCoeff and c.ConsrtName out of my adding constraint API with a Coeff Matrix. However, I am also open to any other solution you might be prefer to. Could you instruct me
    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Saeed,

    You could use the Model.getA() method to get the constraint matrix in \(\texttt{scipy.sparse}\) format. You can then use the scipy transpose method to get the transpose of your \(A\) matrix.

    You can then get the whole right-hand side as a list by using the Model.getAttr method

    b = model.getAttr("RHS", model.getConstrs())

    You can also get all constraint senses in a single list using the same method

    senses = model.getAttr("Sense", model.getConstrs())

    Finally, you can get all objective coefficients in a list via

    c = model.getAttr("Obj", model.getVars())

    With this you should have all information required to construct the dual of your model.

    Note that you can double check your implementation by letting Gurobi write out the dual of your problem via the Model.write() method

    model.write("dual.dlp")

    Best regards, 
    Jaromił

     

    0

Please sign in to leave a comment.