Model.optimize() time_limit using GLPK

312 views
Skip to first unread message

Will Bryant

unread,
Aug 16, 2015, 4:27:49 PM8/16/15
to cobra pie
Hi,

I am running large numbers of optimizations using the Model.optimize() method with GLPK as the solver and finding that GLPK occasionally hangs.  I have discovered in the code that there is a 'time_limit' optional argument for optimize(), but I cannot find details of what optimize() returns in the case where the optimization times out.  

Does anyone know if it raises an exception?  Or if it returns something else?

Thanks,

Will.

Ali Ebrahim

unread,
Aug 16, 2015, 4:52:49 PM8/16/15
to Will Bryant, cobra-pie

Are you using glpk or cglpk as the argument to Model.optimize? They are both interfaces to GLPK. However, I recommend that you use cglpk... It's faster, better supported, and works with the latest version of GLPK. It shouldn't ever hang on M scale LP's. If it does please send be the model so I can test it.

This time_limit option is forwarded to the solver interface. If the solver can't find a solution in the allotted time then the solution status will be something other than "optimal". I am not 100% sure if the glpk interface supports this option (I have to check) but cglpk definitely does.

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

Will Bryant

unread,
Aug 17, 2015, 4:41:56 PM8/17/15
to cobra pie, willbr...@gmail.com
Hi Ali,

Thanks for the response.  I have updated to the latest version of COBRAPy, but using solver='cglpk' returns

"
...
--> 121 return solver_dict[solver].solve(cobra_model, **kwargs)

KeyError: 'cglpk'
"

Is there an additional installation step to the cglpk up and running?  I have uninstalled and reinstalled the latest version of COBRAPy (as suggested in a previous post) and it builds cglpk.so, but I still get the same error.

I tried a more direct test, using 'solver = cobra.solvers.cglpk', but this returned

"
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-7ff36dd07f76> in <module>()
----> 1 solver = cobra.solvers.cglpk

AttributeError: 'module' object has no attribute 'cglpk'
"

Any further help would be greatly appreciated.

Cheers,

Will.

Ali Ebrahim

unread,
Aug 17, 2015, 6:12:32 PM8/17/15
to Will Bryant, cobra pie
The correct syntax is solver="cglpk"... but it's actually unnecessary because cglpk is the default solver.

Will Bryant

unread,
Aug 18, 2015, 5:27:08 AM8/18/15
to cobra pie, willbr...@gmail.com
Hi Ali,

I can't seem to get 'cglpk' to run for me.  

I added a line to the solver __init__.py file to tell me which solver is being used and it is telling me that 'glpk' is being used when I don't specify the solver.

When I try to specify solver="cglpk" it still throws the KeyError.  Is there any reason why cglpk might not have compiled properly?  By the way I am running on Mac OSX Yosemite for testing, does this make any difference?

Cheers,

Will.

Ali Ebrahim

unread,
Aug 18, 2015, 11:53:56 AM8/18/15
to Bryant, William A, cobra-pie
Ah ok. Old version of pip is definitely the culprit then. I'll add a recommendation for a newer version of pip to the installation instructions.

On Tue, Aug 18, 2015 at 8:47 AM, Bryant, William A <w.br...@imperial.ac.uk> wrote:
Ah yes, apparently I was using pip 1.5.6, which is somewhat legacy, to say the least!  I have upgraded pip and installed cobra again and it is working fine!  Thanks for all your help.

Cheers,

Will.


On 18/08/2015 15:55, Ali Ebrahim wrote:

Ok so the issue is that it is compiling against an old version of glpk.

In any case it's still weird because it should be installing from a binary wheel and not compiling anything. What version of pip do you have? Is it up to date?

Also which python are you running? The one that came with your mac, the one from homebrew, or the one from python.org

On Aug 18, 2015 7:40 AM, "Bryant, William A" <w.br...@imperial.ac.uk> wrote:
I just wanted to say it works fine on my Linux box, just not on the Mac.  There are two errors below - the first is from your suggestion and the second is the output of 'pip install cobra' after 'pip uninstall cobra', which appears to be pertinent.

The result of running 'import cobra.solvers.cglpk' is:

"
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-21-465ef8852430> in <module>()
----> 1 import cobra.solvers.cglpk

ImportError: dlopen(/Users/wbryant/.virtualenvs/cobra/lib/python2.7/site-packages/cobra/solvers/cglpk.so, 2): Symbol not found: _glp_read_prob
  Referenced from: /Users/wbryant/.virtualenvs/cobra/lib/python2.7/site-packages/cobra/solvers/cglpk.so
  Expected in: flat namespace
 in /Users/wbryant/.virtualenvs/cobra/lib/python2.7/site-packages/cobra/solvers/cglpk.so
"

Also, when I use "pip install cobra" I get the following message:

"
Installing collected packages: cobra
  Running setup.py install for cobra
    building 'cobra.solvers.cglpk' extension
    clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c cobra/solvers/cglpk.c -o build/temp.macosx-10.9-x86_64-2.7/cobra/solvers/cglpk.o -Wno-unused-function
    cobra/solvers/cglpk.c:7085:20: warning: implicit declaration of function 'glp_write_prob' is invalid in C99 [-Wimplicit-function-declaration]
      __pyx_v_result = glp_write_prob(__pyx_v_self->glp, 0, __pyx_v_name);
                       ^
    cobra/solvers/cglpk.c:7690:20: warning: implicit declaration of function 'glp_read_prob' is invalid in C99 [-Wimplicit-function-declaration]
      __pyx_v_result = glp_read_prob(__pyx_v_self->glp, 0, __pyx_v_name);
                       ^
    2 warnings generated.
    clang -bundle -undefined dynamic_lookup -L/usr/local/lib -L/usr/local/opt/sqlite/lib build/temp.macosx-10.9-x86_64-2.7/cobra/solvers/cglpk.o -L/usr/local/lib -lglpk -o build/lib.macosx-10.9-x86_64-2.7/cobra/solvers/cglpk.so

Successfully installed cobra
Cleaning up...
"

Cheers,

Will.

On 18/08/2015 15:12, Ali Ebrahim wrote:

That is interesting.

What is the output of import cobra.solvers.cgpk

Ali Ebrahim

unread,
Aug 18, 2015, 7:18:45 PM8/18/15
to Bryant, William A, cobra-pie
So, it turns out that there was a bug where the time_limit is accidentally overridden by the default (INT_MAX). I'm commiting a patch now and it will be fixed in the next beta. Also, it turns out I was mistaken earlier... it raises an exception if the time limit is exceeded in cglpk.

Generally though, it's extremely unlikely that cglpk gets stuck because it first tries to solve the problem with a 500ms time limit, and if that fails, it resets the basis and then tries again, which almost always fixes the issue. 


On Tue, Aug 18, 2015 at 1:28 PM, Bryant, William A <w.br...@imperial.ac.uk> wrote:
Hi again,

The 'time_limit' option is working great now for the 'glpk' solver, but seems not to work for 'cglpk'.  I have set the 'time_limit=0.0000000000000000001' and tried a few hundred runs with a typical sized model (about 1000 reactions), which times out for every 'glpk' optimisation, but does not time out with 'cglpk'.  Has it been tested with 'cglpk'?

I will begin testing my large numbers of optimisations using cglpk and hopefully will no longer encounter the hangs I was getting before.

Cheers,

Will.
Reply all
Reply to author
Forward
0 new messages