# initial package installation Pkg.add("Convex") Pkg.add("SCS") Pkg.add("Gadfly") Pkg.add("Interact") # Make the Convex.jl module available using Convex using SCS # first order splitting conic solver [O'Donoghue et al., 2014] set_default_solver(SCSSolver(verbose=0)) # could also use Gurobi, Mosek, CPLEX, ... # Generate random problem data m = 50; n = 100 A = randn(m, n) x♮ = sprand(n, 1, .5) # true (sparse nonnegative) parameter vector noise = .1*randn(m) # gaussian noise b = A*x♮ + noise # noisy linear observations # Create a (column vector) variable of size n. x = Variable(n) # nonnegative elastic net with regularization λ = 1 μ = 1 problem = minimize(norm(A * x - b)^2 + λ*norm(x)^2 + μ*norm(x, 1), x >= 0) # Solve the problem by calling solve! solve!(problem) println("problem status is ", problem.status) # :Optimal, :Infeasible, :Unbounded etc. println("optimal value is ", problem.optval) using Gadfly, Interact @manipulate for λ=0:.1:5, mu=0:.1:5 problem = minimize(norm(A * x - b)^2 + λ*norm(x)^2 + μ*norm(x, 1), x >= 0) solve!(problem) plot(x=x.value, Geom.histogram(minbincount = 20), Scale.x_continuous(minvalue=0, maxvalue=3.5))#, Scale.y_continuous(minvalue=0, maxvalue=6)) end # Scalar variable x = Variable() # (Column) vector variable y = Variable(4) # Matrix variable Z = Variable(4, 4) x + 2x e = y[1] + logdet(Z) + sqrt(x) + minimum(y) e.children[2] x <= 0 x^2 <= sum(y) M = Z for i = 1:length(y) M += rand(size(Z))*y[i] end M ⪰ 0 x = Variable() y = Variable(4) objective = 2*x + 1 - sqrt(sum(y)) constraint = x >= maximum(y) p = minimize(objective, constraint) # solve the problem solve!(p) p.status x.value # can evaluate expressions directly evaluate(objective) # Generate random problem data m = 50; n = 100 A = randn(m, n) x♮ = sprand(n, 1, .5) # true (sparse nonnegative) parameter vector noise = .1*randn(m) # gaussian noise b = A*x♮ + noise # noisy linear observations # Create a (column vector) variable of size n. x = Variable(n) # nonnegative elastic net with regularization λ = 1 μ = 1 problem = minimize(norm(A * x - b)^2 + λ*norm(x)^2 + μ*norm(x, 1), x >= 0) @time solve!(problem) λ = 1.5 @time solve!(problem, warmstart = true) # affine x = Variable(4) y = Variable (2) sum(x) + y[2] 2*maximum(x) + 4*sum(y) - sqrt(y[1] + x[1]) - 7 * minimum(x[2:4]) # not dcp compliant log(x) + x^2 # \$f\$ is convex increasing and \$g\$ is convex square(pos(x)) # \$f\$ is convex decreasing and \$g\$ is concave invpos(sqrt(x)) # \$f\$ is concave increasing and \$g\$ is concave sqrt(sqrt(x))