Pyomo / pyomo

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

Pyomo + GUROBI: I can´t get dual variable result #1702

Closed LeoHenrik closed 2 years ago

LeoHenrik commented 3 years ago

I have been using IPOPT as a solver and i´ve not problem getting dual vairable, but i´m trying to use Gurobi but i have the problem that when i try to get the result of the dual variable of some constraint the program give me a error message.

I get the dual variable doing this:

    Variable= zeros(nbr)
    for c in model.c:
        Variable[c] =  model.dual([model.constraint_a[c]])

and the program have: model.dual = pe.Suffix(direction=pe.Suffix.IMPORT) before the solver.

jsiirola commented 3 years ago

For us to be able to help diagnose the error, we will need:

LeoHenrik commented 3 years ago
  1. path
  2. 3.8.1
  3. 5.7.1 and pip
  4. 9.1.0
  5. tee=true
  6. Traceback (most recent call last): File: KeyError: 608790200 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\Desktop\file\model_12.py", line 607, in setmode variable[c] = model.dual[model.constraint_a[c]] File "c:\Users\Desktop\file.venv\lib\site-packages\pyomo\core\kern raise KeyError("Component with id '%s': %s" KeyError: "Component with id '608790200': constraint_a[0]"
mrmundt commented 3 years ago

@LeoHenrik - Is this still an issue? If so, can you please post a minimal example that recreates the issue?

LeoHenrik commented 3 years ago

@LeoHenrik - Is this still an issue? If so, can you please post a minimal example that recreates the issue?

Hi I told with the people of Gurobi and the said that reason that i can´t get the dual varibale if beacuse the problem is non-convex. that was 5 months ago maybe at this moment that fix it. Do you know somentime about this?

blnicho commented 3 years ago

@LeoHenrik Gurobi is a Mixed Integer Linear Programming solver and does not support general nonlinear or non-convex problems (although I think Gurobi does support some QCQP's). It sounds like you're trying to use a solver that is incompatible with your model.

bernalde commented 3 years ago

According to Gurobi's documentation (https://www.gurobi.com/documentation/9.1/refman/pi.html) they only return duals from continuous and convex problems. In the case that you are dealing with a quadratic program, you need to provide an extra parameter to Gurobi (QCPDual =1). It seems that you are asking for a parameter that the solver cannot provide. If you REALLY need (some) duals, what you can do is fix the integer variables (if your problem has one), and then resolve at the optimal solution provided by Gurobi your problem using a Nonlinear Programming solver, like IPOPT.

LeoHenrik commented 3 years ago

Would you recomend other solver better than IPOPT?

bernalde commented 3 years ago

From what I understand, you can solve the problem with Gurobi already, so you just need an NLP to compute the duals. IPOPT is a very good choice given its open-access nature and how easy it is to install, if you are an academic you can even use the HSL libraries to give it a performance boost. Consider warm-starting the solver, given that you already have the optimal solution from Gurobi (details here https://list.coin-or.org/pipermail/ipopt/2016-May/004221.html). If you really want to use something else, KNITRO is the best-performant NLP solver out there (according to H. Mittelmann's benchmarks http://plato.asu.edu/ftp/ampl-nlp.html) but you'll need to get a license for it. You can access through GAMS a full battery of other solvers (CONOPT, SNOPT, ...) but yet again, you'd need a license.