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





modeling.ipynb

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# **Modeling a basic problem with FICO® Xpress Optimizer - Python Interface**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***modeling.ipynb***\n",
    "\n",
    "This example demonstrates how variables, or arrays thereof, and constraints, or arrays of constraints, are added to a problem. It prints the solution and attributes of the problem.\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 with the alias *xp*, declare data and create an Xpress problem instance using [xpress.problem()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/xpress.problem.html). A name can be assigned to a problem upon creation using the <tt>name</tt> argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xpress as xp\n",
    "\n",
    "N = 4\n",
    "S = range(N)\n",
    "\n",
    "p = xp.problem(name=\"My first problem\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add both v, an array (list) of variables, and v1 and v2, two scalar variables using the method [p.addVariable()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.addVariable.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = [p.addVariable(name=\"y{0}\".format(i), lb=0, ub=2*N) for i in S]\n",
    "\n",
    "v1 = p.addVariable(name=\"v1\", lb=0, ub=10, vartype=xp.continuous)\n",
    "v2 = p.addVariable(name=\"v2\", lb=1, ub=7, threshold=3, vartype=xp.semicontinuous)\n",
    "vb = p.addVariable(name=\"vb\", vartype=xp.binary)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add a list of constraints, which are given as arguments to [p.addConstraint()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.addConstraint.html): \n",
    "  - one explicitly created and stored in <tt>c1</tt>\n",
    "  - two constraints passed directly as arguments: \n",
    "    - one using the scalar variables <tt>v1</tt> and <tt>v2</tt>\n",
    "    - another one using selected elements of set <tt>v</tt>\n",
    "  - a set (list) of constraints indexed by all ${i \\in S: i<N-1}$ (recall that ranges in Python are numbered from 0 to N-1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "c1 = v1 + v2 >= 5\n",
    "\n",
    "p.addConstraint(c1,\n",
    "                2*v1 + 3*v2 >= 5,\n",
    "                v[0] + v[2] >= 1,\n",
    "                (v[i+1] >= v[i] + 1 for i in S if i < N-1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The method [problem.setObjective()]() sets the objective function of the problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "p.setObjective(xp.Sum([i*v[i] for i in S]), sense=xp.minimize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Optimize and print the solve and solution statuses using the objects returned by [problem.optimize()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.optimize.html) or by querying problem attributes. The method [problem.getSolution()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.getSolution.html) returns the optimal solution as a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "solvestatus, solstatus = p.optimize()\n",
    "\n",
    "if solvestatus == xp.SolveStatus.COMPLETED:\n",
    "    print(\"Solve completed with solution status: \", solstatus.name)\n",
    "else:\n",
    "    print(\"Solve status: \", solvestatus.name)\n",
    "\n",
    "# or alternatively\n",
    "# print(\"Solve status: \", p.attributes.solvestatus.name)\n",
    "# print(\"Solution status: \", p.attributes.solstatus.name)\n",
    "\n",
    "print(\"Solution:\", p.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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

Back to examples browser