# Is there a way to obtain coefficient matrix A and RHS b of constraints equation from cplex or guribi in python?

I have a non-linear program, minimizing sum of concave functions subject to linear constraints. Hence I'm using a different solver(in Julia). However, that solver assumes we have constraints in $$Ax \le b$$ form and requires me to pass $$A$$ and $$b$$ as input parameters. This is very inconvenient as I explicitly do not have these matrices. Since the constraints are linear, I plan to build them in cplex or gurobi and export the coefficient matrix $$A$$ and right hand side $$b$$ in some file and then import and read them in my NLP solver.

So, is there a way to extract $$A$$ and $$b$$ from the cplex model? I'm using docplex and guribipy.

Also if there is a better way to resolve this, I'm all ears.

• That looks like a difficult and error-prone approach to me. You could translate the problem into Pyomo format, which supports different (local and global) NLP and MINLP solvers. With NLP and MINLP problems, it is always a good idea to try out different solvers. Apr 19 at 18:31
• Thanks for the suggestion, but I have a particular method from a Stanford paper I want to implement. My problem is not convex NLP that solvers can solve and for non-convex problems the solver just give a feasible and local optimal solution. I want global optimum. Apr 20 at 7:34
• There are global NLP solvers. Apr 20 at 11:44
• They are either for convex programming or they use some heuristic to find a local optimal for non-convex problem. I know of itopt, Knitro, snopt and minotaur nlp solvers. The idea to check with different solvers comes in handy there because a different heuristic may lead to a different locally optimal solution and it's worth exploring different solvers to get different solutions to pick best among them. I want to solve globally and therefore can't rely on these solvers. I have an algorithm and it has convergence and complexity proofs. I would prefer that I think : ) Apr 23 at 12:57
• They are either for convex programming or they use some heuristic to find a local optimal for non-convex problem. Sorry, this is just not correct. There are quite a few global NLP solvers that give global optimal solutions for nonconvex problems. I use them myself. Apr 23 at 14:04

## 2 Answers

In gurobi, you can use model.getA() query to get the linear coeff matrix in sparse format.
You can get list of linear constraints using model.getConstrs() & also get attributes like rhs, lhs & sense.
You can output the model in python through model.write('filename.mps'). You can read in a model through model.read().

Let's assume that, when you build the model, you assign names to all constraints and variables and to the objective function. CPLEX (and I'm pretty sure Gurobi) can export the model to a text file in MPS format. You can write code (in Python or Julia) to create the required matrices, initially filled with zeros, and then read in the MPS file, parse it, and plug the entries in it into the correct slots in the matrices.