Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
1.98k stars 507 forks source link

Gurobi solve errors when solving nonconvex problems #1884

Closed DKenefake closed 3 years ago

DKenefake commented 3 years ago

It appears that a new flag or check must be introduced for gurobi to solve nonconvex problems.

Within the gurobi python API this is done with model.setParam('NonConvex', 2) after model construction.

Here is an example of the problem. Where we are doing a variation of the example CSTR reactor problem.

from pyomo.environ import *
from pyomo.core import *
model = ConcreteModel()
# Rate constants
model.k1 = Var(initialize = 5.0/6.0, within=PositiveReals) # min^-1
model.k2 = Var(initialize = 5.0/3.0, within=PositiveReals) # min^-1
model.k3 = Var(initialize = 1.0/6000.0, within=PositiveReals) # m^3/(gmol min)
model.k1.fixed = True
model.k2.fixed = True
model.k3.fixed = True 
caf = 10000.0 # gmol/mˆ3

# create the variables
model.sv = Var(initialize = 1.0, within=PositiveReals)
model.ca = Var(initialize = 5000.0, within=PositiveReals)
model.cb = Var(initialize = 2000.0, within=PositiveReals)
model.cc = Var(initialize = 2000.0, within=PositiveReals)
model.cd = Var(initialize = 1000.0, within=PositiveReals)

# create the objective
model.obj = Objective(expr = model.cb, sense=maximize)

# create the constraints
model.ca_bal = Constraint(expr = (0 == model.sv * caf  - model.sv * model.ca - model.k1 * model.ca - 2.0 * model.k3 * model.ca ** 2.0))
model.cb_bal = Constraint(expr=(0 == -model.sv * model.cb  + model.k1 * model.ca - model.k2 * model.cb))
model.cc_bal = Constraint(expr=(0 == -model.sv * model.cc + model.k2 * model.cb))
model.cd_bal = Constraint(expr=(0 == -model.sv * model.cd + model.k3 * model.ca ** 2.0))
results = SolverFactory('gurobi').solve(model)
ERROR: Solver (gurobi) returned non-zero return code (1)
ERROR: Solver log:

    -------------------------------------------- Warning: your license will
    expire in 10 days --------------------------------------------

    Using license file C:\Users\DustinDesktop\gurobi.lic Academic license -
    for non-commercial use only - expires 2021-04-03 Read LP format model from
    file C:\Users\DUSTIN~1\AppData\Local\Temp\tmppuxw1duf.pyomo.lp Reading
    time = 0.00 seconds x9: 1 rows, 6 columns, 1 nonzeros Gurobi Optimizer
    version 9.1.0 build v9.1.0rc0 (win64) Thread count: 6 physical cores, 6
    logical processors, using up to 6 threads Optimize a model with 1 rows, 6
    columns and 1 nonzeros Model fingerprint: 0xbd480e1d Model has 4 quadratic
    constraints Coefficient statistics:
      Matrix range     [1e+00, 1e+00] QMatrix range    [2e-04, 1e+00] QLMatrix
      range   [8e-01, 1e+04] Objective range  [1e+00, 1e+00] Bounds range
      [0e+00, 0e+00] RHS range        [1e+00, 1e+00]
    Presolve removed 1 rows and 1 columns Traceback (most recent call last):
      File "<stdin>", line 5, in <module> File
      "C:\Users\DustinDesktop\Anaconda3\envs\pyomo\lib\site-
      packages\pyomo\solvers\plugins\solvers\GUROBI_RUN.py", line 114, in
      gurobi_run
        model.optimize()
      File "src\gurobipy\model.pxi", line 847, in
      gurobipy.gurobipy.Model.optimize
    gurobipy.gurobipy.GurobiError: Quadratic equality constraints are non-
    convex. Set NonConvex parameter to 2 to solve model.
michaelbynum commented 3 years ago

Our interface provides a mechanism to set this option:

opt = SolverFactory('gurobi')
opt.options['nonconvex'] = 2
results = opt.solve(model)