FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browser

Python notebooks

Description

Python notebooks available in the GitHub repository python-notebooks.


pythonnotebooks.zip[download all files]

Source Files





loadlp.ipynb

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# **Using the low-level API function _loadproblem()_**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***loadlp.ipynb***\n",
    "\n",
    "This example shows how to load a problem using the [loadproblem()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.loadproblem.html) functionality and solve it via the FICO® Xpress Python interface.\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": [
    "Start by importing the xpress Python package and create an Xpress problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xpress as xp\n",
    "\n",
    "p = xp.problem()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Construct the following optimization problem with one call to [loadproblem()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.loadproblem.html) containing three variables with different bounds, and four constraints:\n",
    "\n",
    "$$\n",
    "\\min 3x_{1} + 4x_{2} + 5x_{3} \n",
    "$$\n",
    "\n",
    "subject to:\n",
    "\n",
    "$$\n",
    "x_{1} + x_{2} \\geq -2.4 \\\\\n",
    "x_{1} + x_{3} \\geq -3 \\\\\n",
    "x_{2} + x_{3} = 4 \\\\\n",
    "x_{1} + x_{2} + x_{3} \\leq 5 \\\\\n",
    "$$\n",
    "\n",
    "with the following bounds:\n",
    "\n",
    "$$\n",
    "-1 \\leq x_{1} \\leq 3 \\\\\n",
    "-1 \\leq x_{2} \\leq 5 \\\\\n",
    "-1 \\leq x_{3} \\leq 8\\\\\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As a first step in the transformation of the mathematical problem into the form required by the function we write the problem in the form of a table where the columns represent the decision variables and the rows are the constraints. All non-zero coefficients are then entered into this table, resulting in the problem matrix, completed by the operators and the constant terms (the latter are usually refered to as the right hand side, RHS, values).\n",
    "\n",
    "<table tex=\"l@{\\quad}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c@{\\hspace{-0.1em}}c\">\n",
    "<caption>LP matrix</caption>\n",
    "<tr>\n",
    "<tr>\n",
    "<td></td> <td tex=\"@{\\quad}c\"></td> <th tex=\"@{\\hspace{-0.1em}}c\">x1</th> <th tex=\"@{\\hspace{-0.1em}}c\">x2</th> <th tex=\"@{\\hspace{-0.1em}}c\">x3</th> <th tex=\"@{\\hspace{-0.1em}}c\"><i>rowtype</i></th> <th tex=\"@{\\hspace{-0.1em}}c\"><i>rhs</i></th> </tr>\n",
    "<texline/>\n",
    "<tr>\n",
    "<th align=\"left\">Constraint</th> <th tex=\"@{\\quad}c\"><i>0</i></th>  <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>0</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>3</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\"></td>  <td tex=\"@{\\hspace{-0.1em}}c\">'G'</td> <td tex=\"@{\\hspace{-0.1em}}c\">-2.4</td> </tr>\n",
    "<tr>\n",
    "<th align=\"left\"></th> <th tex=\"@{\\quad}c\"><i>1</i></th> <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>1</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\"></td>  <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>6</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\">'G'</td> <td tex=\"@{\\hspace{-0.1em}}c\">-3</td> </tr>\n",
    "<tr>\n",
    "<th align=\"left\"></th> <th tex=\"@{\\quad}c\"><i>2</i></th> <td tex=\"@{\\hspace{-0.1em}}c\"></td> <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>4</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>7</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\">'E'</td> <td tex=\"@{\\hspace{-0.1em}}c\">4</td> </tr>\n",
    "<th align=\"left\"></th> <th tex=\"@{\\quad}c\"><i>3</i></th>   <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>2</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>5</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\">1<math><sup><sup>8</sup></sup></math></td> <td tex=\"@{\\hspace{-0.1em}}c\">'L'</td> <td tex=\"@{\\hspace{-0.1em}}c\">5</td> </tr>\n",
    "\n",
    "<notex><tr></tr></notex>\n",
    "<texline/>\n",
    "<tr>\n",
    "<th tex=\"@{\\hspace{-0.1em}}c\"><i>start</i></th> <th tex=\"@{\\quad}c\"></th>   <td tex=\"@{\\hspace{-0.1em}}c\">0</td> <td tex=\"@{\\hspace{-0.1em}}c\">3</td> <td tex=\"@{\\hspace{-0.1em}}c\">6</td> <td tex=\"@{\\hspace{-0.1em}}c\">9</td></tr>\n",
    "<tr>\n",
    "</table>\n",
    "\n",
    "*numbers in exponents denote the sequence of array entries\n",
    "\n",
    "The table contains some of the arrays that are passed as arguments to the *loadproblem()* function, such as the *rowtype* and *rhs* columns, and the *start* row. The comments below contain a brief description of each argument. The <tt>start</tt> argument indicates the index of\n",
    "the <tt>rowind</tt> (row indices) and <tt>rowcoef</tt> (row coefficients) at which the values refer to the next column, starting from zero. \n",
    "\n",
    "More information about the function can be found in [loadproblem()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.loadproblem.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p.loadproblem(probname=\"myprob\",                          # Problem name\n",
    "              rowtype=['G', 'G', 'E', 'L'],               # Character array containing row types\n",
    "              rhs=[-2.4, -3, 4, 5],                       # Right hand side coefficients of the rows\n",
    "              rng=None,                                   # Range values for range rows\n",
    "              objcoef=[3, 4, 5],                          # Objective function coefficients\n",
    "              start=[0, 3, 6, 9],                         # Offsets in the rowind and rowcoef arrays of the start of the elements for each column\n",
    "              collen=None,                                # Number of nonzero elements in each column\n",
    "              rowind=[0, 1, 3, 0, 2, 3, 1, 2, 3,],        # Row indices for the nonzero elements\n",
    "              rowcoef=[1, 1, 1, 1, 1, 1, 1, 1, 1],        # Nonzero coefficients; length as for rowind\n",
    "              lb=[-1, -1, -1],                            # Lower bounds\n",
    "              ub=[3, 5, 8],                               # Upper bounds\n",
    "              colnames=['x1', 'x2', 'x3'],                # Column names\n",
    "              rownames=['row1', 'r2', 'R3', 'constr04'])  # Row names - various types\n",
    "\n",
    "p.write(\"loadlp\", \"lp\")\n",
    "p.optimize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Retrive a variable by its index and modify the objective function using the variable object. Note that by calling *p.setObjective* again, the objective function is **replaced by**, not amended with, the new objective."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = p.getVariable(0)  # Retrieve a variable via its index\n",
    "\n",
    "p.setObjective(x1**2 + 2*x1 + 444)\n",
    "p.optimize()\n",
    "p.write(\"updated\", \"lp\")"
   ]
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

Back to examples browser