{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mixed-integer quadratic program\n",
    "A mixed-integer quadratic program (MIQP) is an optimization problem of the form\n",
    "$$  \n",
    "    \\begin{array}{ll}\n",
    "    \\mbox{minimize}   & x^T Q x + q^T x + r \\\\\n",
    "    \\mbox{subject to} & x \\in \\mathcal{C}\\\\\n",
    "    & x \\in \\mathbf{Z}^n,\n",
    "    \\end{array}\n",
    "$$\n",
    "where $x \\in \\mathbf{Z}^n$ is the optimization variable\n",
    "($\\mathbf Z^n$ is the set of $n$-dimensional vectors with integer-valued components),\n",
    "$Q \\in \\mathbf{S}_+^n$ (the set of $n \\times n$ symmetric positive semidefinite matrices), $q \\in \\mathbf{R}^n$, and $r \\in \\mathbf{R}$ are problem data,\n",
    "and $\\mathcal C$ is some convex set.\n",
    "\n",
    "An example of an MIQP is mixed-integer least squares, which has the form\n",
    "$$  \n",
    "    \\begin{array}{ll}\n",
    "    \\mbox{minimize}   & \\|Ax-b\\|_2^2 \\\\\n",
    "    \\mbox{subject to} & x \\in \\mathbf{Z}^n,\n",
    "    \\end{array}\n",
    "$$\n",
    "where $x \\in \\mathbf{Z}^n$ is the optimization variable,\n",
    "and $A \\in \\mathbf{R}^{m \\times n}$ and $b \\in \\mathbf{R}^{m}$ are the problem data.\n",
    "A solution $x^{\\star}$ of this problem will be a vector in $\\mathbf Z^n$ that minimizes\n",
    "$\\|Ax-b\\|_2^2$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "In the following code, we solve a mixed-integer least-squares problem with CVXPY."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cvxpy as cp\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate a random problem\n",
    "np.random.seed(0)\n",
    "m, n= 40, 25\n",
    "\n",
    "A = np.random.rand(m, n)\n",
    "b = np.random.randn(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13.66000322824753"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Construct a CVXPY problem\n",
    "x = cp.Variable(n, integer=True)\n",
    "objective = cp.Minimize(cp.sum_squares(A@x - b))\n",
    "prob = cp.Problem(objective)\n",
    "prob.solve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status:  optimal\n",
      "The optimal value is 13.66000322824753\n",
      "A solution x is\n",
      "[-1.  1.  1. -1.  0.  0. -1. -2.  0.  0.  0.  1.  1.  0.  1.  0. -1. -1.\n",
      " -1.  0.  2. -1.  2.  0. -1.]\n"
     ]
    }
   ],
   "source": [
    "print(\"Status: \", prob.status)\n",
    "print(\"The optimal value is\", prob.value)\n",
    "print(\"A solution x is\")\n",
    "print(x.value)"
   ]
  }
 ],
 "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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
