![]() | |||||||||||
| |||||||||||
Python notebooks Description Python notebooks available in the GitHub repository python-notebooks.
Source Files By clicking on a file name, a preview is opened at the bottom of this page. write_read.ipynb { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# **Writing and reading problem files**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***write_read.ipynb***\n", "\n", "In this notebook, we create a basic problem, save it to a file and then read that file into a new problem and solve it.\n", "\n", "© Copyright 2025 Fair Isaac Corporation\n", "\n", "Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n", " \n", "Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n", "\n", "This example uses FICO® Xpress software. By running it, you agree to the Community License terms of the [Xpress Shrinkwrap License Agreement](https://community.fico.com/s/contentdocument/06980000002h0i5AAA) with respect to the FICO® Xpress software. See the [licensing options](https://www.fico.com/en/fico-xpress-trial-and-licensing-options) overview for additional details and information about obtaining a paid license." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Install the xpress package\n", "%pip install -q xpress" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a basic Xpress problem with five variables of various types, set an objective and add a set of three constraints:\n", "\n", "$$\n", "\\min c_{1} + c_{2}\n", "$$\n", "\n", "subject to:\n", "\n", "$$\n", "c_{1}^2 + c_{2}^2 \\leq 6 \\\\\n", "2c_{1} + 3c_{2} + c_{3} = 2 \\\\\n", "-c_{3}^2 + c_{4}^2 + c_{5}^2 \\leq 0 \\\\\n", "$$\n", "\n", "with the following bounds:\n", "\n", "$$\n", "-\\inf \\leq c_{1} \\leq \\inf \\\\\n", "-\\inf \\leq c_{2} \\leq 200 \\\\\n", "0 \\leq c_{3} \\leq \\inf \\\\\n", "0 \\leq c_{4} \\leq 6 \\\\\n", "0 \\leq c_{5} \\leq \\inf \\\\\n", "\n", "$$\n", "\n", "with $c_{3}$ being *partially integer* with a threshold of 10, $c_{4}$ a *semi-continuous* variable with a <tt>threshold</tt> of 3 and upper bound equal to 6, and $c_{5}$ being *integer*." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import xpress as xp\n", "\n", "p = xp.problem()\n", "\n", "c1 = p.addVariable(name=\"C1\", lb=-xp.infinity, ub=xp.infinity)\n", "c2 = p.addVariable(name=\"C2\", lb=-xp.infinity, ub=200)\n", "c3 = p.addVariable(name=\"C3\", vartype=xp.partiallyinteger, threshold=10) # Integer up to 10, then continuous\n", "c4 = p.addVariable(name=\"C4\", vartype=xp.semicontinuous, threshold=3, ub=6) # A 'hole' between 0 and 3, then continuous up to 6\n", "c5 = p.addVariable(name=\"C5\", vartype=xp.integer)\n", "\n", "p.setObjective(c1 + c2)\n", "\n", "p.addConstraint(c1**2 + c2**2 <= 6,\n", " 2 * c1 + 3 * c2 + c3 == 2,\n", " -c3**2 + c4**2 + c5**2 <= 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After a problem is built by the user, the [problem.write()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.write.html) method generates and exports the problem as an LP file named \"example0\" into the active working directory. The method can be used to write in either LP or MPS format, where LP is more human readable and best fit for debugging and model validation, while the MPS format is recommended for numerical experiments and reproduceability.\n", "\n", "This file can then be opened for debugging purposes or for a visual validation of the problem formulation." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "p.write(\"example0\", \"lp\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A second problem is created and loaded by reading the generated LP file using the [problem.read()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.read.html) method, with the **filename with extension** being passed as an argument. Then, the problem is optimized, and the objective and solutions values of the optimal solution are printed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p2 = xp.problem()\n", "\n", "p2.read(\"example0.lp\")\n", "\n", "p2.optimize()\n", "\n", "print(\"Objective value:\", p2.attributes.objval)\n", "print(\"Solution:\", p2.getSolution())" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.9" } }, "nbformat": 4, "nbformat_minor": 2 }
| |||||||||||
© Copyright 2025 Fair Isaac Corporation. |