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





sos.ipynb

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# **Using Special Ordered Set (SOS) constraints**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***sos.ipynb***\n",
    "\n",
    "Construct a problem from scratch with variables of various types. Adds Special Ordered Sets (SOSs) and shows how to retrieve such modeling objects once it has been added to the problem using the API functions.\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, create an Xpress problem, and add all variables used in this example plus a linear constraint over these variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xpress as xp\n",
    "\n",
    "m = xp.problem()\n",
    "\n",
    "m.controls.miprelstop = 0\n",
    "\n",
    "# All variables used in this example\n",
    "v1 = m.addVariable(lb=0, ub=7, vartype=xp.continuous)\n",
    "v2 = m.addVariable(lb=1, ub=10, threshold=7, vartype=xp.semicontinuous)\n",
    "y = [m.addVariable(name=\"y{0}\".format(i)) for i in range(2)]\n",
    "\n",
    "m.addConstraint(v1 + v2 >= xp.Sum(y[i] for i in range(2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next cell adds an SOS type 1 constraint using the [problem.addSOS()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.addSOS.html) method.\n",
    "\n",
    "The first argument corresponds to the list of variables composing the SOS constraint, and the second argument the list of weights (one per variable) that define the order for SOS constraints (used in branching for both types)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SOS: mynewsos mynewsos\n"
     ]
    }
   ],
   "source": [
    "s = m.addSOS([v1, v2], [1, 0], name=\"mynewsos\", type=1)\n",
    "\n",
    "print(\"SOS:\", s.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following sets an objective, solves the problem, displays some output and exports the model in LP format.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.setObjective(xp.Sum([y[i] for i in range(2)]), sense=xp.maximize)\n",
    "\n",
    "m.optimize()\n",
    "\n",
    "print(\"v1: \", m.getSolution(v1),\n",
    "      \", v2: \", m.getSolution(v2),\n",
    "      \"; sol vector: \", m.getSolution(),\n",
    "      \"; obj: \", m.attributes.objval,\n",
    "      sep=\"\")  # default separator between strings is \" \"\n",
    "\n",
    "m.write(\"restriction\", \"lp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the following cell, the method [problem.delSOS()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.delSOS.html) is used to delete the previously added SOS1 constraint by passing its object as an argument.\n",
    "\n",
    "The deletion can be confirmed by comparing the exported LP files created before and after the SOS constraint is deleted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.delSOS(s)\n",
    "m.write(\"restriction-noSOS\", \"lp\")\n",
    "\n",
    "m.optimize()"
   ]
  }
 ],
 "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