{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from cvxpy import *\n",
      "\n",
      "# Create two scalar optimization variables.\n",
      "x = Variable()\n",
      "y = Variable()\n",
      "\n",
      "# Create two constraints.\n",
      "constraints = [x + y == 1,\n",
      "               x - y >= 1]\n",
      "\n",
      "# Form objective.\n",
      "obj = Minimize(square(x - y))\n",
      "\n",
      "# Form and solve problem.\n",
      "prob = Problem(obj, constraints)\n",
      "prob.solve()\n",
      "\n",
      "# The optimal dual variable (Lagrange multiplier) for\n",
      "# a constraint is stored in constraint.dual_value.\n",
      "print \"optimal (x + y == 1) dual variable\", constraints[0].dual_value\n",
      "print \"optimal (x - y >= 1) dual variable\", constraints[1].dual_value\n",
      "print \"x - y value:\", (x - y).value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "optimal (x + y == 1) dual variable 6.47610300459e-18\n",
        "optimal (x - y >= 1) dual variable 2.00025244976\n",
        "x - y value: 0.999999986374\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Solving a problem with different solvers.\n",
      "x = Variable(2)\n",
      "obj = Minimize(norm(x, 2) + norm(x, 1))\n",
      "constraints = [x >= 2]\n",
      "prob = Problem(obj, constraints)\n",
      "\n",
      "# Solve with ECOS.\n",
      "prob.solve(solver=ECOS)\n",
      "print \"optimal value with ECOS:\", prob.value\n",
      "\n",
      "# Solve with CVXOPT.\n",
      "prob.solve(solver=CVXOPT)\n",
      "print \"optimal value with CVXOPT:\", prob.value\n",
      "\n",
      "# Solve with SCS.\n",
      "prob.solve(solver=SCS)\n",
      "print \"optimal value with SCS:\", prob.value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "optimal value with ECOS: 6.82842708233\n",
        "optimal value with CVXOPT: 6.82842708994\n",
        "optimal value with SCS: 6.82837896978\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Solve with ECOS.\n",
      "prob.solve(solver=ECOS, verbose=True)\n",
      "print \"optimal value with ECOS:\", prob.value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "ECOS 1.0.3 - (c) A. Domahidi, Automatic Control Laboratory, ETH Zurich, 2012-2014.\n",
        "\n",
        "It     pcost         dcost      gap     pres    dres     k/t     mu      step     IR\n",
        " 0   +0.000e+00   +4.000e+00   +2e+01   2e+00   1e+00   1e+00   3e+00    N/A     1 1 -\n",
        " 1   +6.451e+00   +8.125e+00   +5e+00   7e-01   5e-01   7e-01   7e-01   0.7857   1 1 1\n",
        " 2   +6.788e+00   +6.839e+00   +9e-02   1e-02   8e-03   3e-02   2e-02   0.9829   1 1 1\n",
        " 3   +6.828e+00   +6.829e+00   +1e-03   1e-04   8e-05   3e-04   2e-04   0.9899   1 1 1\n",
        " 4   +6.828e+00   +6.828e+00   +1e-05   1e-06   8e-07   3e-06   2e-06   0.9899   2 1 1\n",
        " 5   +6.828e+00   +6.828e+00   +1e-07   1e-08   8e-09   4e-08   2e-08   0.9899   2 1 1\n",
        "\n",
        "OPTIMAL (within feastol=1.3e-08, reltol=1.5e-08, abstol=1.0e-07).\n",
        "Runtime: 0.000121 seconds.\n",
        "\n",
        "optimal value with ECOS: 6.82842708233\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Solve with SCS and display output.\n",
      "opts = {\"USE_INDIRECT\": False}\n",
      "prob.solve(solver=SCS, verbose=True, solver_specific_opts=opts)\n",
      "print \"optimal value with SCS:\", prob.value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "----------------------------------------------------------------------------\n",
        "\tscs v1.0 - Splitting Conic Solver\n",
        "\t(c) Brendan O'Donoghue, Stanford University, 2012\n",
        "----------------------------------------------------------------------------\n",
        "Method: sparse-direct, nnz in A = 13\n",
        "EPS = 1.00e-03, ALPHA = 1.80, MAX_ITERS = 2500, NORMALIZE = 1, SCALE = 5.0\n",
        "Variables n = 5, constraints m = 9\n",
        "Cones:\tprimal zero / dual free vars: 0\n",
        "\tlinear vars: 6\n",
        "\tsoc vars: 3, soc blks: 1\n",
        "\tsd vars: 0, sd blks: 0\n",
        "\texp vars: 0, dual exp vars: 0\n",
        "----------------------------------------------------------------------------\n",
        " Iter | pri res | dua res | rel gap | pri obj | dua obj |  kappa  | time (s)\n",
        "============================================================================\n",
        "     0| 4.60e+00  5.78e-01       nan      -inf       inf  8.32e+00  1.54e-03 \n",
        "    60| 3.92e-05  1.12e-04  6.64e-06  6.83e+00  6.83e+00  9.31e-18  1.62e-03 \n",
        "----------------------------------------------------------------------------\n",
        "Status: Solved\n",
        "Timing: Solve time: 1.63e-03s, setup time: 1.70e-04s\n",
        "\tLin-sys: nnz in L factor: 29, avg solve time: 1.38e-07s\n",
        "\tCones: avg projection time: 5.05e-08s\n",
        "----------------------------------------------------------------------------\n",
        "Error metrics:\n",
        "|Ax + s - b|_2 / (1 + |b|_2) = 3.9223e-05\n",
        "|A'y + c|_2 / (1 + |c|_2) = 1.1168e-04\n",
        "|c'x + b'y| / (1 + |c'x| + |b'y|) = 6.6446e-06\n",
        "dist(s, K) = 0, dist(y, K*) = 0, s'y = 0\n",
        "----------------------------------------------------------------------------\n",
        "c'x = 6.8284, -b'y = 6.8285\n",
        "============================================================================\n",
        "optimal value with SCS: 6.82837896975\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Get ECOS arguments.\n",
      "c, G, h, dims, A, b = prob.get_problem_data(ECOS)\n",
      "\n",
      "# Get CVXOPT arguments.\n",
      "c, G, h, dims, A, b = prob.get_problem_data(CVXOPT)\n",
      "\n",
      "# Get CVXOPT arguments.\n",
      "data, dims = prob.get_problem_data(SCS)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}