Instantly share code, notes, and snippets.

# ericphanson/Convex219.jl

Last active Mar 14, 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 using Mosek using SCS import DSP: conv using Convex conv(x::AbstractVector, y::AbstractVector) = DSP.conv(x,y) conv(x::Variable, y::AbstractVector) = Convex.conv(x,y) conv(x::AbstractVector, y::Variable) = Convex.conv(x,y) using Random using SparseArrays Random.seed!(123) println(" \n \n \n solving Convex \n \n \n") n,m = 100, 10 h = randn(n) x = Vector(sprandn(m, 0.05)) y = conv(h,x)+randn(n+m-1) lambda = 0.01 x0 = Variable(m) # slv = Mosek.Optimizer() slv = SCS.Optimizer(verbose=0) problem = minimize(0.5*square(norm(conv(h,x0)-y))+lambda*norm(x0,1)) solve!(problem, slv) @show x0.value xConvex = x0.value println(" \n \n \n solving cvxpy \n \n \n") using PyCall # Conda.add("cvxpy"; channel="conda-forge") @pyimport cvxpy as cvx slv = cvx.SCS x0 = cvx.Variable(m) yy = reshape(y, (length(y), 1)) problem = cvx.Problem(cvx.Minimize(cvx.sum_squares(cvx.conv(h,x0)-yy)*0.5+cvx.norm1(x0)*lambda)) problem.solve(solver = slv, verbose = true) xcvxpy = x0.value norm(xcvxpy-xConvex)

### ericphanson commented Mar 14, 2020

Note: this was updated for Convex 0.13 in the second revision, so go to the first revision for the Convex 0.12 version. Also, uncomment the `Conda.add` command (and do `using Conda`) to install `cvxpy` if it's not already installed.