Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams model = pyomo.ConcreteModel() model.d = pyomo.Var(initialize=1, bounds=(0,2,)) model.g = pyomo.Var(initialize=1, bounds=(0,2,)) model.s = pyomo.Var(initialize=1, bounds=(0,2,)) model.b = pyomo.Var(initialize=1, bounds=(0,2,)) objective_rule = model.d - model.b*model.g + model.s*model.b model.objective = pyomo.Objective(rule=objective_rule, sense=pyomo.minimize) # After digging in pyomo source I figured out this # should print all solvers available in my system print(IOptSolver._factory_cls) # This fails opt = SolverFactory("gurobi", solver_io="python") # opt = SolverFactory("neos") This also fails # Create a model instance and optimize instance = model results = opt.solve(instance) instance.display()

But pyomo has problems detecting gurobi:

Traceback (most recent call last): File "./__test__2.py", line 21, in <module> results = opt.solve(instance) File "/usr/local/lib/python3.5/dist-packages/pyomo/opt/base/solvers.py", line 125, in solve self._solver_error('solve') File "/usr/local/lib/python3.5/dist-packages/pyomo/opt/base/solvers.py", line 153, in _solver_error + "\n\toptions: %s" % ( self.options, ) ) RuntimeError: Attempting to use an unavailable solver. The SolverFactory was unable to create the solver "gurobi" and returned an UnknownSolver object. This error is raised at the point where the UnknownSolver object was used as if it were valid (by calling method "solve"). The original solver was created with the following parameters: executable: gurobi solver_io: python type: gurobi _args: () options: {}

I've installed pyomo using pip:

pip3 install pyomo

And I've downloaded and installed gurobi using the following commands (after decompress):

mv gurobi801 /opt/gurobi/gurobi801
cd /opt/gurobi/gurobi801/linux64/
python3 setup.py build
python3 setup.py install

And added to my .bash_profile:

export GUROBI_HOME="/opt/gurobi/gurobi801/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"

Gurobi runs fine if I call it from command line:

gurobi.sh 
Python 2.7.13 (default, Sep  4 2017, 15:40:17) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Academic license - for non-commercial use only
Gurobi Interactive Shell (linux64), Version 8.0.1
Type "help()" for help
gurobi>

But pyomo fails to print the whole help:

pyomo help -s
Pyomo Solvers and Solver Managers
---------------------------------
Pyomo uses 'solver managers' to execute 'solvers' that perform
optimization and other forms of model analysis.  A solver directly
executes an optimizer, typically using an executable found on the
user's PATH environment.  Solver managers support a flexible mechanism
for asyncronously executing solvers either locally or remotely.  The
following solver managers are available in Pyomo:
    neos       Asynchronously execute solvers on the NEOS server
    serial     Synchronously execute solvers locally
If no solver manager is specified, Pyomo uses the serial solver
manager to execute solvers locally.  The pyro and phpyro solver
managers require the installation and configuration of the pyro
software.  The neos solver manager is used to execute solvers on the
NEOS optimization server.
Serial Solver Interfaces
------------------------
The serial, pyro and phpyro solver managers support the following
solver interfaces:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pyomo/scripting/driver_help.py", line 338, in help_solvers
    logger.disable(logging.WARNING)
AttributeError: 'Logger' object has no attribute 'disable'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/bin/pyomo", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/dist-packages/pyomo/scripting/pyomo_main.py", line 82, in main
    retval = _options.func(_options)
  File "/usr/local/lib/python3.5/dist-packages/pyomo/scripting/driver_help.py", line 452, in help_exec
    help_solvers()
  File "/usr/local/lib/python3.5/dist-packages/pyomo/scripting/driver_help.py", line 351, in help_solvers
    logger.disable(logging.NOTSET)
AttributeError: 'Logger' object has no attribute 'disable'

What could I have missed in my setup? I'm running ubuntu 16.04 x64

Can you check that gurobipy is importable in the same python environment in which you are running your Pyomo script? – Qi Chen Jul 17, 2018 at 20:59 @QiChen I've tried importing gurobipy. But I'm getting an import error: ImportError: libgurobi80.so: cannot open shared object file: No such file or directory. So now I have a concrete problem to solve. – jabozzo Jul 17, 2018 at 21:35 @QiChen I've solved the import error by adding /opt/gurobi/gurobi801/linux64/lib/ to /etc/ld.so.conf, so the dynamic linker can find the library. Now I can import gurobipy without problems. I still have the original problem when trying to instance gurobi solver in pyomo. – jabozzo Jul 17, 2018 at 21:46 I think you might have an issue concerning conflicts between python versions. gurobi.sh appears to be calling python 2.7 while your 'main' environment is python 3.5. If it's not too much of a hassle, I might try reinstalling everything using python 3.6. – Qi Chen Jul 18, 2018 at 0:45 @QiChen I've looked everywhere to configure gurobi.sh python version. Then I decided to open it up only to encounter that it's hardcoded to use a python2.7 that comes in gurobi's bin folder, gurobi.sh end like this: $PYTHONHOME/bin/python2.7 "$@". I've tried manually changing it to python3 "$@", but it ImportErrors on me: ImportError: No module named 'encodings' So it's probably not compatible with python3. On the other side, gurobipy, the library is compatible with python 2.7, 3.5, 3.6 – jabozzo Jul 18, 2018 at 3:51

It turns out importing pyomo.environ is of vital importance to detect the available solvers. Updating the script to:

#! /usr/bin/env python3
import pyomo.environ # <---  HAD MISSING #
from pyomo.opt import SolverFactory, IOptSolver
import pyomo.core as pyomo
model = pyomo.ConcreteModel()
model.d = pyomo.Var(initialize=0, bounds=(0,2,))
model.g = pyomo.Var(initialize=0, bounds=(0,2,))
model.s = pyomo.Var(initialize=0, bounds=(0,2,))
model.b = pyomo.Var(initialize=0, bounds=(0,2,))
objective_rule = model.d - model.b*model.g + model.s*model.b
model.objective = pyomo.Objective(rule=objective_rule, sense=pyomo.minimize)
# Should print all available solvers
print(sorted(IOptSolver._factory_cls.keys()))
opt = SolverFactory("gurobi", solver_io="python")
# Create a model instance and optimize
instance = model
results = opt.solve(instance)
instance.display()

Yields:

['_cbc_shell', '_cplex_shell', '_gams_direct', '_gams_shell', '_glpk_direct', '_glpk_shell', '_glpk_shell_4_42', '_glpk_shell_old', '_gurobi_shell', '_mock_asl', '_mock_cbc', '_mock_cplex', '_mock_glpk', '_mock_pico', '_mock_xpress', '_neos', '_pico_shell', '_xpress_shell', 'asl', 'baron', 'bilevel_blp_global', 'bilevel_blp_local', 'bilevel_ld', 'cbc', 'conopt', 'cplex', 'cplex_direct', 'cplex_persistent', 'gams', 'gdpopt', 'glpk', 'gurobi', 'gurobi_direct', 'gurobi_persistent', 'ipopt', 'mpec_minlp', 'mpec_nlp', 'path', 'pico', 'ps', 'py', 'scip', 'trustregion', 'xpress']
Academic license - for non-commercial use only
Model unknown
  Variables:
    d : Size=1, Index=None
        Key  : Lower : Value : Upper : Fixed : Stale : Domain
        None :     0 :     0 :     2 : False :  True :  Reals
    g : Size=1, Index=None
        Key  : Lower : Value : Upper : Fixed : Stale : Domain
        None :     0 :     0 :     2 : False :  True :  Reals
    s : Size=1, Index=None
        Key  : Lower : Value : Upper : Fixed : Stale : Domain
        None :     0 :     0 :     2 : False :  True :  Reals
    b : Size=1, Index=None
        Key  : Lower : Value : Upper : Fixed : Stale : Domain
        None :     0 :     0 :     2 : False :  True :  Reals
  Objectives:
    objective : Size=1, Index=None, Active=True
        Key  : Active : Value
        None :   True :   0.0
  Constraints:

Is not doing proper optimization but at least the solver detection issue is solved.

Thanks for your work figuring this out. I'm a bit surprised that the environ import was the solution in this case here. – Qi Chen Jul 18, 2018 at 5:01

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.