install and use Gurobi solver

529 views
Skip to first unread message

Stephen Chapman

unread,
Mar 21, 2018, 8:37:46 AM3/21/18
to cobra pie
Hi all,

I'm wanting to replicate flux predictions I made 3 years ago, in which I used Matlab, COBRA and the Gurobi solver. Interesting, as I'm now using Python and COBRApy and CPLEX solver but getting completely different flux distributions. Is this normal? I want to use the Gurobi solver to see if the differences are made between COBRApy or COBRA and not MatLab and Python.

So I installed the latest release of Gurobi Solver (gurobi7.5.2_mac64.pkg) and followed their installation wizard which installed the solver to the hard drive. I obtained the license and installed it here:

'/Users/stephenchapman/gurobi.lic', (The solver is under '/Users/stephenchapman/Applications/'.


After loading the model


import cobra

import os

from os.path import join

data_dir = '/Users/stephenchapman/Documents/FBA_algae_digesate/COBRApy/models/iRC1080/'

model = cobra.io.read_sbml_model(join(data_dir, "IRC1080_SC_correctedPSII.xml"))


model

#<Model iRC1080 at 0x11ffc6048>


I then go to change solver:


model.solver ='gurobi'


and receive this error:


Traceback (most recent call last):

  File "/Users/stephenchapman/anaconda/lib/python3.6/site-packages/cobra/core/model.py", line 138, in solver

    interface = solvers[interface_to_str(value)]

KeyError: 'gurobi'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Users/stephenchapman/anaconda/lib/python3.6/site-packages/cobra/util/context.py", line 69, in wrapper

    f(self, new_value)

  File "/Users/stephenchapman/anaconda/lib/python3.6/site-packages/cobra/core/model.py", line 140, in solver

    raise not_valid_interface

cobra.util.solver.SolverNotFound: gurobi is not a valid solver interface. Pick from ['glpk', 'scipy'], or specify an optlang interface (e.g. optlang.glpk_interface).



Can anyone help me here?


Kind regards



S.








Moritz Beber

unread,
Mar 21, 2018, 9:22:04 AM3/21/18
to cobr...@googlegroups.com

Hello Stephen,

Can you ensure that optlang recognizes Gurobi, please? I feel this may be easier discussed in an issue (easier to handle code examples there). Could you open one at https://github.com/opencobra/cobrapy/issues, please? I'm especially interested in the output of cobra.show_versions(). And please try to import optlang.gurobi_interface as gurobi;gurobi.Model() in your environment and put that into the new issue, too.

Thank you,

Moritz

--
You received this message because you are subscribed to the Google Groups "cobra pie" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cobra-pie+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen Chapman

unread,
Mar 22, 2018, 5:33:13 AM3/22/18
to cobra pie
Thank you again, Moritz


#### Output of `cobra.show_versions()`
<details>
>>> cobra.show_versions()
System Information
==================
OS              Darwin
OS-release      15.6.0
Python           3.6.1

Package Versions
================
pip              9.0.2
setuptools      27.2.0
cobra            0.8.2
future          0.16.0
swiglpk          1.4.4
optlang          1.2.3
ruamel.yaml    0.14.12
pandas          0.20.1
numpy           1.12.1
tabulate         0.8.1
python-libsbml  5.15.0
lxml             3.7.3
scipy           0.19.0
matplotlib       2.0.2
<details>

Amarjeet Kumar

unread,
Mar 29, 2018, 7:42:58 AM3/29/18
to cobra pie
Hello Moritz Beber,
I am facing a problem where the read model has no attribute solver in it.
I have install  cobra using pip install cobra[all] and it successfully ran.
I have installed optlang and am able to import gurobi_interface form optlang

Note: the cobra.show_versions() does not exist in my installation.

Please suggest how to fix the bug.

Thank you

Isabella Casini

unread,
May 25, 2018, 5:32:35 AM5/25/18
to cobra pie
Hello! 

I am also having trouble switching to the gurobi solver (with an academic license version 8) with COBRApy (version 0.11.3). I have no trouble running an FBA with glpk and CPLEX and I don't have trouble using gurobi in Python outside of COBRApy. 
I attempted to set the model solver to gurobi (just as I did switching between glpk and CPLEX) and I get this error: "AttributeError: 'Variable' object has no attribute 'problem'."
I've attached a sample code in Jupyter Notebook illustrating my trouble. The kernel (an Anaconda Environment) I'm using has both gurobipy and cobra installed. My code file includes an example of successfully using the gurobipy package for an example given by Gurobi and then unsuccessfully in COBRApy. 

Any help would be appreciated! 

Thanks!
Isabella
gurobi trial.ipynb

Christian Diener

unread,
May 28, 2018, 2:01:37 PM5/28/18
to cobra pie
Hi Isabella,

Sorry for the inconvenience. This was a known bug in optlang (https://github.com/biosustain/optlang/issues/158) which has been fixed in version 1.4.2. Updating optlang to that version (or any newer one) should fix that problem.

Cheers
Christian
Reply all
Reply to author
Forward
0 new messages