Skip to content

Instantly share code, notes, and snippets.

@ericphanson
Last active Mar 14, 2020
Embed
What would you like to do?
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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment