1

I use pyomo and gurobi for solving a nonlinear optimization problem. but when I run my code, I get this error:

RuntimeError: Cannot write legal LP file.  Objective 'Maximum_profit' has nonlinear terms that are not quadratic.

(I don't have any problem when I use gurobi as a solver in a linear optimization problem.) My objective function is:

 Maximum_profit=pyo.Objective(doc="Profit Maximization", rule=lambda model: sum(pyo.log10(1+sum(pyo.log10(1.0 + model.t[i,k]) for i in model.N)) for k in model.J) - sum(sum(model.t[i,k] * model.p[i,k] for k in model.J) for i in model.N),sense=-1)

how can I fix it?

hamta
  • 75
  • 2
  • 10
  • We need to see the code to be of any use figuring out the error. – wallyk Apr 14 '20 at 21:43
  • Hi hamta, welcome to SO. You might find it helpful to read the how to ask section of the intro. Especially, I recommend providing an MRE for getting the most out of the site. – zabop Apr 14 '20 at 21:44
  • @wallyk I edited my question and I put the objective function code. – hamta Apr 14 '20 at 21:49

2 Answers2

2

Disclaimer: I work for Gurobi.

The problem here is pyomo, since Gurobi is able to solve non-convex mixed-integer quadratically constrained quadratic programming problems. In particular, the issue arises in the file cpxlp.py, which originally handled only the writing the CPLEX LP file, but now also handles the LP files for Gurobi. There, in line 500-650 you can see all the restrictions that apply, and that is where the exception is thrown.

I will post a github issue (if one does not already exist) so as to hopefully clarify this.

Richard
  • 256
  • 1
  • 7
  • I also have this issue. If using `gurobipy`, I would use the `GRBModel.AddGenConstrLog` method and use auxiliary variables accordingly. However, I strongly prefer modeling in Pyomo, since I sometimes use other solvers. I notice that in [the Pyomo repository](https://github.com/Pyomo/pyomo), there is no usage of `AddGenConstrLog`, so perhaps there is no interface to this functionality. Is that correct as far as you know? – Zach Siegel Aug 06 '21 at 20:31
  • This is correct. This is one of the drawbacks of working with modeling frameworks: you are dependent on the functionalities they expose rather than the API of the underlying solver (true for any solver, not only Gurobi). Your best bet might be to model all the linear stuff in `pyomo`, export the LP file, read it in with `gurobipy` and add the nonlinear terms there. It's cumbersome, but at least this way you keep part of your code solver-agnostic. – Richard Aug 09 '21 at 07:43
  • Thank you for this response @Richard. I ended up using Gurobipy to solve the problem and then writing a script that stores all the Gurobipy variables in a Pyomo model for the rest of my workflow. I hope the Pyomo folks have this on their eventual agenda. – Zach Siegel Aug 09 '21 at 15:10
0

Gurobi cannot solve nonlinear optimization models (except for some instances of quadratic models). With log terms in your objective you need a solver that can handle them, such as BARON or IPOPT.

gmavrom
  • 430
  • 4
  • 12
  • Thank you for your reply. I have also binary variable. Do you think IPOPT is useful? – hamta Apr 15 '20 at 09:20
  • I do not think IPOPT can solve MINLP problems (i.e. non linear with binary variables). BARON does though and it is supported by Pyomo. You can find other MINLP solvers discussed here: https://www.researchgate.net/post/Can_you_suggest_best_solver_for_the_mixed-integer_nonlinear_programming – gmavrom Apr 15 '20 at 12:40
  • Gurobi 9.0 can also solve nonlinear optimization problems. You just have to assign the value of your nonlinear objective to an auxiliary variable and then minimize this variable. – mattmilten Apr 17 '20 at 07:23
  • I do not think it can solve all instances of nonlinear optimization problems. I believe it’s limited to convex and non-convex quadratic optimization problems. – gmavrom Apr 17 '20 at 13:46