![]() | |||||||||||
| |||||||||||
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. logic_cons.ipynb { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# **Using general (logic) constraints**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***logic_cons.ipynb***\n", "\n", "In this example we use the and/or operators of the FICO® Xpress Python interface to state a MIP optimization problem with logic constraints.\n", "\n", "Solve a simple SAT problem by finding the solution with the fewest <i>True</i> variables that satisfy all clauses.\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 two scalar objects and a list of variables used in this example." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import xpress as xp\n", "\n", "p = xp.problem()\n", "\n", "N = 10\n", "k = 5\n", "\n", "x = [p.addVariable(vartype=xp.binary) for _ in range(N)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following constraint ensures that at most one of each pair of variables $x$ can be *True*, by using the Python native '**&**' operator. \n", "\n", "The same effect could be obtained by using the [xpress.And()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/xpress.And.html) operator, which returns a logical *AND* of two or more binary variables or expressions passed as arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cons0 = [(x[i] & x[i+1]) == 0 for i in range(0, N-1, 2)] \n", "\n", "# or equivalently\n", "#cons0 = [xp.And(x[i],x[i+1]) == 0 for i in range(0, N-1, 2)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next constraint assumes that at least a quarter of all OR clauses on contiguous groups of k clauses must be *True*, by using the [xpress.Or()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/xpress.Or.html) operator, which returns a logical *OR* of two or more binary variables or expressions passed as arguments." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "cons1 = xp.Sum(xp.Or(*(x[i:i+k])) for i in range(N-k)) >= N/4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both constraints are added to the problem, and the problem is solved with a time limit of 5 seconds." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p.addConstraint(cons0, cons1)\n", "\n", "p.controls.timelimit = 5\n", "p.optimize()\n", "\n", "print(\"solution: x = \", 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.9" } }, "nbformat": 4, "nbformat_minor": 2 }
| |||||||||||
© Copyright 2025 Fair Isaac Corporation. |