{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Translate between `OptimizationProblem` and Docplex\n", "\n", "If you are used to the Docplex workflow, you can seamlessly translate between a Docplex model and an `OptimizationProblem` with the `from_docplex_mp` and `to_docplex_mp` translators. This translation allows to leverage Docplex capabilities such as **LP file loading** without too many additional steps.\n", "\n", "\n", "
\n", "Note: Docplex does not support higher-order terms in its formulation. This conversion is limited to constant, linear, and quadratic terms.\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\ This file has been generated by DOcplex\n", "\\ ENCODING=ISO-8859-1\n", "\\Problem name: docplex model\n", "\n", "Minimize\n", " obj: x + [ 4 x*y - 2 z^2 ]/2 + 3\n", "Subject To\n", " lin_eq: x + 2 y = 3\n", " lin_leq: x + 2 y <= 3\n", " lin_geq: x + 2 y >= 3\n", "\n", "Bounds\n", " 0 <= x <= 1\n", " -1 <= y <= 5\n", " -1 <= z <= 5\n", "\n", "Binaries\n", " x\n", "\n", "Generals\n", " y\n", "End\n", "\n" ] } ], "source": [ "from docplex.mp.model import Model\n", "\n", "docplex_mod = Model(name=\"docplex model\")\n", "x = docplex_mod.binary_var(name=\"x\")\n", "y = docplex_mod.integer_var(name=\"y\", lb=-1, ub=5)\n", "z = docplex_mod.continuous_var(name=\"z\", lb=-1, ub=5)\n", "\n", "docplex_mod.minimize(3 + x + 2 * x * y - z * z)\n", "docplex_mod.add_constraint(x + 2 * y == 3, \"lin_eq\")\n", "docplex_mod.add_constraint(x + 2 * y <= 3, \"lin_leq\")\n", "docplex_mod.add_constraint(x + 2 * y >= 3, \"lin_geq\")\n", "\n", "print(docplex_mod.export_as_lp_string())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you display your problem as LP format using `export_as_lp_string`,\n", "`Binaries` denotes binary variables and `Generals` denotes integer variables.\n", "\n", "If variables are not included in either `Binaries` or `Generals`, such variables are continuous ones with default lower bound == 0 and upper bound == infinity.\n", "\n", "Note that you cannot use `'e`' or `'E'` as the first character of names due to the [specification of LP format](https://www.ibm.com/docs/en/icos/22.1.0?topic=representation-variable-names-in-lp-file-format).\n", "\n", "The following snippet shows the `from_docplex_mp` translator:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem name: docplex model\n", "\n", "Minimize\n", " 2*x*y - z^2 + x + 3\n", "\n", "Subject to\n", " Linear constraints (3)\n", " x + 2*y == 3 'lin_eq'\n", " x + 2*y <= 3 'lin_leq'\n", " x + 2*y >= 3 'lin_geq'\n", "\n", " Integer variables (1)\n", " -1 <= y <= 5\n", "\n", " Continuous variables (1)\n", " -1 <= z <= 5\n", "\n", " Binary variables (1)\n", " x\n", "\n" ] } ], "source": [ "from qiskit_addon_opt_mapper.translators import from_docplex_mp\n", "\n", "mod2 = from_docplex_mp(docplex_mod)\n", "print(mod2.prettyprint())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As stated before, you can also convert a `OptimizationProblem` to a Docplex model using the `to_docplex` function." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\ This file has been generated by DOcplex\n", "\\ ENCODING=ISO-8859-1\n", "\\Problem name: docplex model\n", "\n", "Minimize\n", " obj: x + [ 4 x*y - 2 z^2 ]/2 + 3\n", "Subject To\n", " lin_eq: x + 2 y = 3\n", " lin_leq: x + 2 y <= 3\n", " lin_geq: x + 2 y >= 3\n", "\n", "Bounds\n", " 0 <= x <= 1\n", " -1 <= y <= 5\n", " -1 <= z <= 5\n", "\n", "Binaries\n", " x\n", "\n", "Generals\n", " y\n", "End\n", "\n" ] } ], "source": [ "from qiskit_addon_opt_mapper.translators import to_docplex_mp\n", "\n", "docplex_mod2 = to_docplex_mp(mod2)\n", "print(docplex_mod2.export_as_lp_string())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load LP files using Docplex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once you are used to the Docplex translators, loading from LP files becomes very simple. You can use the `docplex.mp.ModelReader` class followed by a translation step:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem name: my_lp_file\n", "\n", "Minimize\n", " 2*x0^2 + 0.5*x0*x1 + 0.5*x1^2 + 2*x0 + 2*x1\n", "\n", "Subject to\n", " Linear constraints (1)\n", " x0 + x1 == 2 'constraint'\n", "\n", " Integer variables (2)\n", " 0 <= x0 <= 4\n", " 0 <= x1 <= 4\n", "\n" ] } ], "source": [ "from docplex.mp.model_reader import ModelReader\n", "\n", "model = ModelReader.read(\"data/my_lp_file.lp\")\n", "\n", "op3 = from_docplex_mp(model)\n", "print(op3.prettyprint())" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.13.4" } }, "nbformat": 4, "nbformat_minor": 4 }