{ "metadata": { "language": "Julia", "name": "", "signature": "sha256:45171162088efc3437fbfcc626939b6f52664d7d7b5c6d07b04db4a3931b2156" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "using Convex\n", "using SCS\n", "\n", "# passing in verbose=0 to hide output from SCS\n", "solver = SCSSolver(verbose=0)\n", "set_default_solver(solver);" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear program\n", "$$\n", "\\begin{array}{ll}\n", " \\mbox{maximize} & c^T x \\\\\n", " \\mbox{subject to} & A x <= b\\\\\n", " & x \\geq 1 \\\\\n", " & x \\leq 10 \\\\\n", " & x_2 \\leq 5 \\\\\n", " & x_1 + x_4 - x_2 \\leq 10 \\\\\n", "\\end{array}\n", "$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = Variable(4)\n", "c = [1; 2; 3; 4]\n", "A = eye(4)\n", "b = [10; 10; 10; 10]\n", "p = minimize(dot(c, x)) # or c' * x\n", "p.constraints += A * x <= b\n", "p.constraints += [x >= 1; x <= 10; x[2] <= 5; x[1] + x[4] - x[2] <= 10]\n", "solve!(p)\n", "\n", "println(round(p.optval, 2))\n", "println(round(x.value, 2))\n", "println(evaluate(x[1] + x[4] - x[2]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10" ] }, { "output_type": "stream", "stream": "stdout", "text": [ ".0\n", "[" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1.0\n", " 1.0\n", " 1.0\n", " 1.0]\n", "[0.9999794207077799]\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matrix Variables and promotions\n", "$$\n", "\\begin{array}{ll}\n", " \\mbox{minimize} & \\| X \\|_F + y \\\\\n", " \\mbox{subject to} & 2 X <= 1\\\\\n", " & X' + y >= 1 \\\\\n", " & X >= 0 \\\\\n", " & y >= 0 \\\\\n", "\\end{array}\n", "$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "X = Variable(2, 2)\n", "y = Variable()\n", "# X is a 2 x 2 variable, and y is scalar. X' + y promotes y to a 2 x 2 variable before adding them\n", "p = minimize(sum(X) + y, 2 * X <= 1, X' + y >= 1, X >= 0, y >= 0)\n", "solve!(p)\n", "println(round(X.value, 2))\n", "println(y.value)\n", "p.optval" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0" ] }, { "output_type": "stream", "stream": "stdout", "text": [ ".0 0.0\n", " 0.0 0.0]\n", "1.0000000807021356\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "1.000001111507024" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Norm, exponential and geometric mean\n", "$$\n", "\\begin{array}{ll}\n", " \\mbox{satisfy} & \\| x \\|_2 \\leq 100 \\\\\n", " & e^{x_1} \\leq 5 \\\\\n", " & x_2 \\geq 7 \\\\\n", " & \\sqrt{x_3 x_4} \\geq x_2\n", "\\end{array}\n", "$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = Variable(4)\n", "p = satisfy(norm(x) <= 100, exp(x[1]) <= 5, x[2] >= 7, geo_mean(x[3], x[4]) >= x[2])\n", "solve!(p, SCSSolver(verbose=0))\n", "println(p.status)\n", "x.value" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Optimal\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "4x1 Array{Float64,2}:\n", " 0.0 \n", " 8.65837\n", " 14.6655 \n", " 14.6655 " ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SDP cone and Eigenvalues\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "y = Semidefinite(2)\n", "p = maximize(lambda_min(y), trace(y)<=6)\n", "solve!(p, SCSSolver(verbose=0))\n", "p.optval" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "3.0000029948079856" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "x = Variable()\n", "y = Variable((2, 2))\n", "# SDP constraints\n", "p = minimize(x + y[1, 1], isposdef(y), x >= 1, y[2, 1] == 1)\n", "solve!(p)\n", "y.value" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "2x2 Array{Float64,2}:\n", " 0.00352332 1.00018\n", " 1.00018 286.415 " ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mixed integer program\n", "$$\n", "\\begin{array}{ll}\n", " \\mbox{minimize} & sum(x) \\\\\n", " \\mbox{subject to} & x \\in \\mathbb{Z} \\\\\n", " & x >= 0.5 \\\\\n", "\\end{array}\n", "$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "using GLPKMathProgInterface\n", "x = Variable(4, :Int)\n", "p = minimize(sum(x), x >= 0.5)\n", "solve!(p, GLPKSolverMIP())\n", "x.value" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "4x1 Array{Float64,2}:\n", " 1.0\n", " 1.0\n", " 1.0\n", " 1.0" ] } ], "prompt_number": 7 } ], "metadata": {} } ] }