Is it possible to change the linear solver used within GEKKO? - python

As the title says, I'm trying to find out what linear solver is used within GEKKO, for solving problems with remote=True and remote=False. I'm also curious if it's possible to change this, as you can in Pyomo. I was unable to find any reference to this within the either the Gekko documentation (https://gekko.readthedocs.io/en/latest/) or the APMonitor documentation (https://apmonitor.com/wiki/).
Is anyone familiar with the internals of Gekko / how I could find this out?
Thank you!

All of the solvers in Gekko are Nonlinear Programming (NLP) or Mixed Integer Nonlinear Programming (MINLP). The Nonlinear Programming solvers use linear solvers to iteratively solve the line-search problem. The public server (remote=True) uses MA57 while the version distributed locally (remote=False) uses a linear solver called MUMPS that can be distributed. However, there are no linear programming solvers to directly solve LPs. The MINLP solver can solve LP, QP, QPQC, MILP, NLP, and MINLP problems. Here is example code to specify MA57 solver in IPOPT.
from gekko import GEKKO
m = GEKKO(remote=True)
m.options.SOLVER=3
m.solver_options = ['linear_solver ma57']
From the documentation:
SOLVER selects the solver to use in an attempt to find a solution. There are free solvers: 1: APOPT, 2: BPOPT, 3: IPOPT distributed with the public version of the software. There are additional solvers that are not included with the public version and require a commercial license. IPOPT is generally the best for problems with large numbers of degrees of freedom or when starting without a good initial guess. APOPT is generally the best when warm-starting from a prior solution or when the number of degrees of freedom (Number of Variables - Number of Equations) is less than 2000. APOPT is also the only solver that handles Mixed Integer problems. Use option 0 to compare all available solvers.

Related

How can I obtain random numbers in gekko?

I am trying to solve an optimization problem in Python, using gekko, where one of the variables takes on a random value at each time step, but I haven't been able to use the gekko function that returns random numbers.
Following the documentation page (http://t-t.dk/gekko/docs/user-manual/functions.htm), the function rnorm returns "a random number from a normal distribution with mean and variance provided". I used it as shown here:
x = m.Var(value=0)
m.Equation(x == 5.*m.rnorm(0, 1))
provided that
m = GEKKO()
but I get the following error message:
AttributeError: 'GEKKO' object has no attribute 'rnorm'
I would like to know if there is something that I am missing or if there is another way to get random numbers.
The documentation page you linked is associated with another package that isn't the same as the Optimization Suite in Python. I suggest looking at this page: https://gekko.readthedocs.io/en/latest/model_methods.html for the correct documentation.
As for your question about random numbers, I suggest using another package like python's random or numpy's random.normal. I'm not sure how exactly to apply it in your problem without seeing more code; what you could do is have an array of random numbers for each timestep and multiply or add it in somewhere while writing the problem in Gekko.
The documentation link that you provided is to different gekko software:
Gekko Timeseries and Modeling Software is a free and open-source software system for managing and analyzing timeseries data, and for solving and analyzing large-scale economic models. See the Gekko homepage: www.t-t.dk/gekko. Read more about the status of different Gekko versions on the Gekko versions overview page.
The Gekko Optimization Suite in Python pip install gekko is described in the Wikipedia article and in the Read the Docs documentation.
GEKKO is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations. It is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP). Modes of operation include parameter regression, data reconciliation, real-time optimization, dynamic simulation, and nonlinear predictive control. GEKKO is an object-oriented Python library to facilitate local execution of APMonitor.
Both software packages can analyze time-series data. The numpy.random.randn() function can be used with gekko.
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
p = m.Param()
x = m.Var()
m.Equation(x==5*p)
for i in range(10):
p.value = np.random.randn()
m.solve(disp=False)
print(x.value[0],p.value[0])
This solves the optimization problem 10 times with different values for p sampled from a normal, mean-zero distribution.

Non convex quadratic optimization solvers in python

Let me start saying that I am by no means expert in optimization, so any suggestion would be greatly appreciated. I have a non-convex quadratic optimization problem for which I need a solver. I myself am used to working with the modelling language CVXPY, using some of the default solvers there (ECOS, CVXOPT,...) so something similar to it would be great. I cannot use cvxpy on this problem because it is only suitable for convex optimization problems.
After googling, I saw some people recommending Pyomo, saying it could deal with non convex problems, but my problem requires matrix algebra, and as far as I know, Pyomo does not include matrix algebra capabilities. So I would require a non convex modeling language or solver for non convex quadratic problems.
The type of problem I am actually interested in solving has the following structure:
where wis the vector variable to be optimized, Xis a known data matrix and tis a prespecified parameter value.

Bi-level optimization using Python scipy.optimize

How can I code a bi-level optimization problem using scipy.optimize.minimize which consists of the following two levels, (1) being the upper-level problem, which is subject to (2) being the lower-level:
minimize linear programming problem, subject to a
minimize quadratic programming problem
I know how to write a single objective function for quadratic programming as a typical scipy.optimize.minimize routine (step 2 by itself), but how can I add an upper level minimization problem as well that has its own objective function? Can scipy handle a minimization problem subject to a lower minimization problem? A rough outline of the code using minimize and linprog would help
I don't think scipy solvers are very suited for this.
A standard approach for bilevel problems is to form the KKT conditions of the inner problem and add these as constraints to the outer problem. The complementarity conditions make the problem hard. You need a global NLP solver or a MIP solver to solve this thing to proven optimality. Here are some formulations for linear bilevel problems. If the inner problem is a (convex) QP problem, things become a little bit more difficult, but the same idea can be applied (form KKT conditions of the inner problem).
For more information see Bard, Practical Bilevel Optimization: Algorithms And Applications, Springer, 1998.

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

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?
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.
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.

multivariate optimization in python

anyone please let me know how to solve multivariate problems in python. The objective function is given in picture 1 and few constraints in picture 2
Objective function
Constraints
You are asking how to solve a linear integer programming problem in python. You could use Pyomo, PuLP or python-MIP to model your optimization problem and solve it with a suitable solver of your choice. Gurobi and Cplex are common commercial solvers (both offering a free academic-licence) while GLPK, SCIP or CBC are common used non-commercial ones.

Categories