![]() | |||||||||||
| |||||||||||
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. numpy_arrays.ipynb { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# **Using the numerical library _NumPy_**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***numpy_arrays.ipynb***\n", "\n", "An example of a problem formulation that uses multi-dimensional *NumPy* arrays and the [xpress.Dot()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/xpress.Dot.html) operator to formulate constraints simply and efficiently.\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 *NumPy* and *xpress* packages, create two random matrices and an instance of an Xpress problem." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xpress as xp\n", "import numpy as np\n", "\n", "rndseed = 10\n", "np.random.seed(rndseed)\n", "\n", "A = np.random.random(30).reshape(6, 5) # A is a 6x5 matrix\n", "Q = np.random.random(25).reshape(5, 5) # Q is a 5x5 matrix\n", "\n", "p = xp.problem()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a *NumPy* array of variables. By using [problem.addVariables()](https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/problem.addVariables.html) with an integer argument, a *NumPy* array of variables is created.\n", "\n", "A term with the identity matrix multiplied by four is then added to the matrix stored in object $Q$." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "x = p.addVariables(5) # Create Numpy array of 5 variables\n", "\n", "Q += 4 * np.eye(5) # Add 4 * the identity matrix to the previous Q matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the code cell below:\n", " - The <tt>Lin_sys</tt> object contains six linear constraints originating from the *dot* product between the matrix $A$ and vector $x$, with the right-hand side of each constraint passed as an *NumPy* array.\n", "\n", " - <tt>Conv_c</tt> defines a convex quadratic constraint obtained by the *dot* product between the $x$ vector multiplied by itself and by the matrix $Q$." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "Lin_sys = xp.Dot(A, x) <= np.array([3, 4, 1, 4, 8, 7]) # Creates 6 linear constraints\n", "\n", "Conv_c = xp.Dot(x, Q, x) <= 1 # Creates 1 quadratic constraint\n", "\n", "p.addConstraint(Lin_sys, Conv_c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a convex quadratic objective function by using the *dot* product on variables $x$ minus a randomly generated vector, and optimize the problem." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x0 = np.random.random(5) # Random vector\n", "\n", "p.setObjective(xp.Dot(x-x0, x-x0))\n", "\n", "p.optimize()\n", "\n", "p.write(\"numpy.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.9" } }, "nbformat": 4, "nbformat_minor": 2 }
| |||||||||||
© Copyright 2025 Fair Isaac Corporation. |