{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[1m\u001b[34mINFO: Recompiling stale cache file /Users/madeleine/.julia/lib/v0.5/Convex.ji for module Convex.\n", "\u001b[0m" ] } ], "source": [ "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);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear program\n", "$$\n", "\\begin{array}{ll}\n", " \\mbox{maximize} & c^T x \\\\\n", " \\mbox{subject to} & A x \\leq 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", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(size(coeff),size(var)) = ((4,1),(4,1))\n", "10.0\n", "[1.0; 1.0; 1.0; 1.0]\n", "[1.00006]\n" ] } ], "source": [ "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]))" ] }, { "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 \\leq 1\\\\\n", " & X' + y \\geq 1 \\\\\n", " & X \\geq 0 \\\\\n", " & y \\geq 0 \\\\\n", "\\end{array}\n", "$$" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(size(coeff),size(var)) = ((2,2),(2,2))\n", "[0.0 0.0; -0.0 0.0]\n", "0.9999994607576307\n" ] }, { "data": { "text/plain": [ "0.9999999568807811" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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(vecnorm(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" ] }, { "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", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Optimal\n" ] }, { "data": { "text/plain": [ "4×1 Array{Float64,2}:\n", " 0.0 \n", " 8.65837\n", " 14.6655 \n", " 14.6655 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Variable(4)\n", "p = satisfy(norm(x) <= 100, exp(x[1]) <= 5, x[2] >= 7, geomean(x[3], x[4]) >= x[2])\n", "solve!(p, SCSSolver(verbose=0))\n", "println(p.status)\n", "x.value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SDP cone and Eigenvalues\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(size(coeff),size(var)) = ((2,2),(2,2))\n", "(size(coeff),size(var)) = ((2,2),(2,2))\n" ] } ], "source": [ "y = Semidefinite(2)\n", "p = maximize(lambdamin(y), trace(y)<=6)\n", "solve!(p, SCSSolver(verbose=0))\n", "p.optval" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(size(coeff),size(var)) = ((2,2),(2,2))\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[1m\u001b[31mWARNING: Problem status UnknownError; solution may be inaccurate.\u001b[0m\n" ] }, { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 0.000333371 1.00002\n", " 1.00002 3048.22 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mixed integer program\n", "$$\n", "\\begin{array}{ll}\n", " \\mbox{minimize} & \\sum_{i=1}^n x_i \\\\\n", " \\mbox{subject to} & x \\in \\mathbb{Z}^n \\\\\n", " & x \\geq 0.5 \\\\\n", "\\end{array}\n", "$$" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4×1 Array{Float64,2}:\n", " 1.0\n", " 1.0\n", " 1.0\n", " 1.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using GLPKMathProgInterface\n", "x = Variable(4, :Int)\n", "p = minimize(sum(x), x >= 0.5)\n", "solve!(p, GLPKSolverMIP())\n", "x.value" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.5.0", "language": "julia", "name": "julia-0.5" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }