{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": "# Solvers\n\n## 3rd party solver installation\n\nThrough COBRApy, StrainDesign is already shipped with the free **GLPK** linear programming solver. Alternatively, the more powerful commercial solvers [**IBM CPLEX**](https://www.ibm.com/de-de/products/ilog-cplex-optimization-studio) and [**Gurobi**](https://www.gurobi.com/) can be used by both COBRApy and StrainDesign, and the free solver [**SCIP**](https://scipopt.org/) can be used by StrainDesign. Using one of the GLPK alternatives is preferred, in particular, when using strain design algorithms like MCS, OptKnock etc. since their support of indicator contstraints renders computations significantly more stable.\n\nIn the following, you will find installation instructions for the individual solvers.\n\n
\n\n**Warning:**\n\nThe **free community versions** of CPLEX and Gurobi can be used. However, with larger problems (100+ reactions) their problem size limitations may result in uncaught errors.\n\n
\n\n\n### CPLEX\n\nTogether with Gurobi, CPLEX is the perfect choice for computing strain designs. Its stability and support of advanced features like indicator constraints and populating solution pools make it indispensible for genome-scale computations.\n\n
\n\n**Note:**\n\nYou will need an academic or commercial licence to be able to use CPLEX.\n\n
\n\n
\n\n**Warning:**\n\n**CPLEX is not available for Python 3.13 and above.** The newest CPLEX release (22.1.2) supports Python up to 3.12. If you are using Python 3.13+, please use Gurobi or SCIP instead.\n\n
\n\nDownload and install the CPLEX suite to a location with non-root access since python will need to build some things to set up the CPLEX-API, later. Make sure that your CPLEX and Python versions are compatible. Currently, CPLEX is not officially available for Python 3.13 and above (the latest release 22.1.2 supports up to Python 3.12). Once the installation is completed, you may use the installation to set up the CPLEX-API with your Python/conda environment.\n\nThis can be done either with pip\n\n```\npip install yourCPLEXhome/python/VERSION/PLATFORM\n```\n\nor with conda. For an installation with conda, make sure to activate the same Python/conda environment where `cobra` and `straindesign` are installed. Then call \n\n```\npython yourCPLEXhome/python/VERSION/PLATFORM/setup.py install\n```\n\nNow CPLEX should be available for your computations. If you face difficulties with building CPLEX, consider downgrading the setuptools package to setuptools==58.2.0.\n\nThe official instructions can be found here: https://www.ibm.com/docs/en/icos/22.1.0?topic=cplex-setting-up-python-api\n\n### Gurobi\n\nSimilar to CPLEX, Gurobi offers a fast MILP solvers with the advanced features of indicator constraints and solution pooling. The installation steps are similar to the ones of CPLEX.\n\n
\n\n**Note:**\n\nYou will need an academic or commercial license and install the Gurobi solver software.\n\n
\n\nEnsure that the versions of Gurobi and Python versions are compatible, install Gurobi on your system and activate your license following the steps from the Gurobi manual. In the next step you will link your Gurobi installation to your Python/conda environment.\n\nUsing the command line, navigate to your CPLEX installation path and into the Python folder. The path should look similar to \n\n```\nC:/gurobi950/windows64\n```\n\nMake sure to activate the same Python/conda environment where `cobra` and `straindesign` are installed. Then call \n\n```\npython setup.py install\n```\n\nIf your `gurobipy` package does not work right away, additionally install the gurobi package from conda or PyPi via\n\n```\nconda install -c gurobi gurobi\n```\n\nor\n\n```\npython -m pip install gurobipy\n```\n\nNow Gurobi is available for your computations.\n\nThe official instructions can be found here: https://support.gurobi.com/hc/en-us/articles/360044290292-How-do-I-install-Gurobi-for-Python-\n\n### SCIP\n\nLess powerful than CPLEX and Gurobi, the open source solver SCIP still offers the solution of MILPs with indicator constraints, which gives it an edge above GLPK in terms of stability. If you want to use SCIP, you may install it via conda or pip:\n\n```\nconda install -c conda-forge pyscipopt\n```\n\nor\n\n```\npython -m pip install pyscipopt\n```\n\n
\n\n**Warning:**\n\nIf you encounter program crashes with SCIP (a dependency of pyscipopt), make sure you use a SCIP version of 8.0.0 or older since newer versions are unreliable in solving MILPs and can produce errors (as of December 2023), this issue might get fixed in the future. \nYou can, manually install pyscipopt 4.2.0 and scip version 8.0.0 through ``conda install -c conda-forge pyscipopt=4.2.0 scip=8.0.0``. Keep in mind that SCIP 4.2.0 is currently (December 2023) not available for python>3.10.\n\n
\n\nOfficial website: https://github.com/scipopt/PySCIPOpt" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solver selection\n", "\n", "For any type of LP or MILP-based analysis or design method, four different sovers are supported: **GLPK** (which is built into COBRApy/optlang), **IBM CPLEX**, **Gurobi** and **SCIP**. You can query the available solvers by accessing the set ``straindesign.avail_solvers``. For the subsequent steps we also import the cobra module and load the *E. coli* core model." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Set parameter Username\n", "Academic license - for non-commercial use only - expires 2023-07-20\n" ] }, { "data": { "text/plain": [ "{'cplex', 'glpk', 'gurobi', 'scip'}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import cobra\n", "import straindesign as sd\n", "model = cobra.io.load_model('e_coli_core')\n", "\n", "sd.avail_solvers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may enforce the use of a specific solver by specifying the \"solver\"-keyword. E.g., to enforce the use of GLPK, use: " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Maximum growth: 0.873921506969.\n" ] } ], "source": [ "solution = sd.fba(model, solver='glpk')\n", "\n", "print(f\"Maximum growth: {solution.objective_value}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, the automatic solver selection uses COBRApy's selection. Therefore, StrainDesign will try to use the model's selected solver:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "When the model's solver is '', StrainDesign selects gurobi.\n" ] } ], "source": [ "print(f\"When the model\\'s solver is \\'{model.solver.configuration}', StrainDesign selects {sd.select_solver(None,model)}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Otherwise COBRApy's global configuration is used." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "COBRApy's solver is 'optlang.gurobi_interface', StrainDesign selects gurobi.\n" ] } ], "source": [ "print(f\"COBRApy\\'s solver is \\'{cobra.Configuration().solver.__name__}', StrainDesign selects {sd.select_solver()}.\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'glpk'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.solver = 'cplex'\n", "sd.select_solver('glpk',model)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('cnapy-dev')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "50a36e699ca3834a05ca1bd86fc5b7db4829f1cbbb3e27f51544575d88525899" } } }, "nbformat": 4, "nbformat_minor": 2 }