diff --git a/REQUIRE b/REQUIRE index b5557f1d3..666bbed0b 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,3 +1,4 @@ julia 0.3 MathProgBase 0.3.8 ECOS 0.4.1 +FactCheck 0.2.5 diff --git a/test/run_tests.jl b/test/run_tests.jl index ad565da5a..a0480f7ca 100644 --- a/test/run_tests.jl +++ b/test/run_tests.jl @@ -1,8 +1,9 @@ using Convex tests = ["test_utilities.jl", - "test.jl", - "test2.jl"] + "test_affine.jl", + "test_lp.jl", + "test_socp.jl"] tests_sdp = ["test_sdp.jl"] tests_exp = ["test_exp.jl"] tests_int = ["test_int.jl"] diff --git a/test/test.jl b/test/test.jl deleted file mode 100644 index 41ccde318..000000000 --- a/test/test.jl +++ /dev/null @@ -1,354 +0,0 @@ -using Convex - -TOLERANCE = .01 - -# Test 1 -x = Variable(1) -p = minimize(-x, [x <= 0]) -solve!(p) -@assert abs(p.optval - 0) <= TOLERANCE - -# Test 2 -x = Variable(1) -p = minimize(x, [x >= 2, x <= 4]) -solve!(p) -@assert abs(p.optval - 2) <= TOLERANCE - -# Test 3 -x = Variable(1) -p = minimize(2.0 * x, [x >= 2, x <= 4]) -solve!(p) -@assert abs(p.optval - 4) <= TOLERANCE - -# Test 4 -x = Variable(2) -constr = x >= [1.1; 1.1] -p = minimize(dot([2.0; 2.0], x), constr) -solve!(p) -@assert abs(p.optval - 4.4) <= TOLERANCE - -# Test 5 -x = Variable(2) -A = 1.5 * eye(2) -p = minimize(dot([2.0; 2.0], x), [A * x >= [1.1; 1.1]]) -solve!(p) -@assert abs(p.optval - 2.9333) <= TOLERANCE - -# Test 6 -x = Variable(1) -y = Variable(1) -p = minimize(x + y, [x >= 3, y >= 2]) -solve!(p) -@assert abs(p.optval - 5) <= TOLERANCE - -# Test 7 -x = Variable(1) -y = Variable(1) -p = minimize(2.0*x - 5.0*y, [100.0 <= x, x <= 200.0, 80.0 <= y, y <= 170.0, y >= -x]) -solve!(p) -@assert abs(p.optval + 650) <= TOLERANCE - -# Test 8 -x = Variable(2) -p = minimize(x[1] + x[2], [x >= 1]) -solve!(p) -@assert abs(p.optval - 2) <= TOLERANCE - -# Test 9 -y = Variable(1) -x = Variable(3) -z = [1.0, 2.0, 3.0] * y -k = -y * [1.0, 2.0, 3.0] -c = [y <= 3.0, y >= 0.0, x >= ones(3), k <= x, x <= z] -o = 3 * y -p = Problem(:minimize,o,c) -solve!(p) -@assert abs(p.optval - 3) < TOLERANCE - -# Test 10 -X = Variable(2, 2) -c = ones(2, 1) -p = minimize(c' * X * c, [X >= ones(2, 2)]) -solve!(p) -@assert abs(p.optval - 4) < TOLERANCE - -# Test 11 -p = maximize(c' * X * c, [X <= [1 2; 3 4]]) -solve!(p) -@assert abs(p.optval - 10) < TOLERANCE - -# Test 12 -X = Variable(2, 2) -I = Constant(eye(2)) -c = ones(2, 1) -p = maximize(c' * (eye(2) + X + I .+ 1) * c, [X + ones(2, 2) <= [1 2; 3 4]]) -solve!(p) -@assert abs(p.optval - 14) < TOLERANCE - -# Test 13 -x = Variable(1) -p = minimize(x, [x >= eye(2)]) -solve!(p) -@assert abs(p.optval - 1) < TOLERANCE - -# Test 14 -x = Variable(1) -p = minimize(x, [x .+ eye(2) >= eye(2)]) -solve!(p) -@assert abs(p.optval - 0) < TOLERANCE - -# Test 15 -x = Variable(1) -c = ones(2, 1) -p = minimize(c' * (x .+ eye(2)) * c, [x .+ eye(3) >= 2 * eye(3), -eye(4) <= x]) -solve!(p) -@assert abs(p.optval - 6) < TOLERANCE - -# Test 16 -N = 20 -x = Variable(1) -y = Variable(N, N) -c = ones(N, 1) -p = minimize(c' * (y .+ x) * (c / 100), [x >= 3, 2*y >= 0, y <= x]) -solve!(p) -@assert abs(p.optval - 12) < TOLERANCE - -# Test 17 -x = Variable(2) -c = ones(2, 1) -p = minimize(x' * c, x >= 1) -solve!(p) -@assert abs(p.optval - 2) < TOLERANCE - -# Test 18 -rows = 2 -cols = 3 -r = rand(rows, cols) -r_2 = rand(cols, rows) -x = Variable(rows, cols) -c = ones(1, cols) -d = ones(rows, 1) -p = minimize(c * x' * d + d' * x * c' + (c * x''''' * d)', - [x' >= r_2, x >= r, x''' >= r_2, x'' >= r]); -solve!(p) -s = Base.sum(Base.max(r, r_2')) * 3 -@assert abs(p.optval - s) < TOLERANCE - -# Test 19 -rows = 6 -cols = 8 -n = 2 -X = Variable(rows, cols) -A = randn(rows, cols) -c = rand(1, n) -p = minimize(c * X[1:n, 5:5+n-1]' * c', X >= A) -solve!(p); -s = c * A[1:n, 5:5+n-1]' * c' -@assert abs(p.optval - s[1]) < TOLERANCE - -# Test 20 -x = Variable(1, 10) -p = minimize(sum(x[2:5]), x >= [1 2 3 4 5 6 7 8 9 10]) -solve!(p) -@assert abs(p.optval - 14) < TOLERANCE - -# Test 21 -x = Variable(10) -a = rand(10, 1) -p = maximize(sum(x[2:6]), x <= a) -solve!(p) -@assert abs(p.optval - sum(a[2:6])) < TOLERANCE - -# Test 22 -x = Variable(10) -a = rand(10, 1) -p = minimize(maximum(x), x >= a) -solve!(p) -@assert abs(p.optval - Base.maximum(a)) < TOLERANCE - -# Test 23 -x = Variable(10, 10) -y = Variable(10, 10) -a = rand(10, 10) -b = rand(10, 10) -p = minimize(maximum(max(x, y)), [x >= a, y >= b]) -solve!(p) -max_a = Base.maximum(a) -max_b = Base.maximum(b) -@assert abs(p.optval - Base.max(max_a, max_b)) < TOLERANCE - -# Test 24 -x = Variable(1) -a = rand(10, 10) -p = maximize(minimum(x), x <= a) -solve!(p) -@assert abs(p.optval - Base.minimum(a)) < TOLERANCE - -# Test 25 -x = Variable(10, 10) -y = Variable(10, 10) -a = rand(10, 10) -b = rand(10, 10) -p = maximize(minimum(min(x, y)), [x <= a, y <= b]) -solve!(p) -min_a = Base.minimum(a) -min_b = Base.minimum(b) -@assert abs(p.optval - Base.min(min_a, min_b)) < TOLERANCE - -# Test 26 -x = Variable(3) -a = [-2; 1; 2] -p = minimize(sum(pos(x)), [x >= a, x <= 2]) -solve!(p) -@assert abs(p.optval - 3) < TOLERANCE - -# Test 27 -x = Variable(3) -p = minimize(norm_inf(x), [-2 <= x, x <= 1]) -solve!(p) -@assert abs(p.optval) < TOLERANCE - -# Test 28 -x1 = Variable(1) -x2 = Variable(1) -p = minimize(4x1 + x2, - [3x1 + x2 == 3, 4x1 + 3x2 >= 6, x1 + 2x2 <=3, x1 >=0, x2 >=0]) -solve!(p) -@assert abs(p.optval - 3.6) < TOLERANCE - -# Test 29 -x = Variable(4, 4) -y = Variable(4, 6) -p = maximize(sum(x) + sum([y 4*ones(4)]), [ [x y 2*ones(4, 2)] <= 2]) -solve!(p) -@assert abs(p.optval - 96) < TOLERANCE - -# Test 30 -x = Variable(4, 4) -y = Variable(4, 6) -p = maximize(sum(x) + sum([y 4*eye(4); x -ones(4, 6)]), [ [x, y'] <= 2]) -solve!(p) -@assert abs(p.optval - 104) < TOLERANCE - -# Test 31 -x = Variable(4, 4) -y = Variable(4, 6) -z = Variable(1) -c = ones(4, 1) -d = 2 * ones(6, 1) -constraints = [[x y] <= 2, z <= 0, z <= x, 2z >= -1] -objective = sum(x + z) + minimum(y) + c' * y * d -p = maximize(objective, constraints) -solve!(p) -@assert abs(p.optval - 130) < TOLERANCE - -# Test 32 -x = Variable(2, 1) -A = [1 2; 2 1; 3 4] -b = [2; 3; 4] -p = minimize(norm_2(A * x + b)) -solve!(p) -@assert abs(p.optval - 0.64888) < TOLERANCE - -# Test 33 -x = Variable(2, 1) -A = [1 2; 2 1; 3 4] -b = [2; 3; 4] -lambda = 1 -p = minimize(norm_2(A * x + b) + lambda * norm_2(x), x >= 1) -solve!(p) -@assert abs(p.optval - 14.9049) < TOLERANCE - -# Test 34 -x = Variable(2, 1) -A = [1 2; 2 1; 3 4] -b = [2; 3; 4] -p = minimize(sum_squares(A*x + b)) -solve!(p) -@assert abs(p.optval - 0.42105) < TOLERANCE - -# Test 35 -x = Variable(3) -p = minimize(sum(abs(x)), [-2 <= x, x <= 1]); -solve!(p) -@assert abs(p.optval) < TOLERANCE - -# Test 36 -x = Variable(2, 1) -A = [1 2; 2 1; 3 4] -b = [2; 3; 4] -lambda = 1 -p = minimize(norm_2(A * x + b) + lambda * norm_1(x), x >= 1) -solve!(p) -@assert abs(p.optval - 15.4907) < TOLERANCE - -# Test 37 -x = Variable(3, 1) -A = [0.8608 0.3131 0.5458; 0.3131 0.8584 0.5836; 0.5458 0.5836 1.5422] -p = minimize(quad_form(x, A), [x >= 1]) -solve!(p) -@assert abs(p.optval - 6.1464) < TOLERANCE - -# Test 38 -x = Variable(3, 1) -A = -1.0*[0.8608 0.3131 0.5458; 0.3131 0.8584 0.5836; 0.5458 0.5836 1.5422] -c = [3 2 4] -p = maximize(c*x , [quad_form(x, A) >= -1]) -solve!(p) -@assert abs(p.optval - 3.7713) < TOLERANCE - -# Test 39 -x = Variable(3, 1) -A = [2 -3 5; -2 9 -3; 5 -8 3] -b = [-3; 9; 5] -c = [3 2 4] -d = -3 -p = minimize(quad_over_lin(A*x + b, c*x + d)) -solve!(p) -@assert abs(p.optval - 17.7831) < TOLERANCE - -# Test 40 -x = Variable(4, 4) -c = ones(16, 1) -reshaped = reshape(x, 16, 1) -a = [1:16] -p = minimize(c' * reshaped, reshaped >= a) -solve!(p) -@assert abs(p.optval - 136) < TOLERANCE - -# Test 41 -x = Variable(4, 4) -p = minimize(sum(diag(x)), x >= 2) -solve!(p) -@assert abs(p.optval - 8) < TOLERANCE - -# Test 42 -m = Variable(4, 5) -c = [m[3, 3] == 4, m >= 1] -p = minimize(norm(m, :fro), c) -solve!(p) -@assert abs(m.value[1, 1] - 1) < TOLERANCE - -# Test 43 -x = Variable() -y = Variable(4) -p = minimize(x + sum(y), [x == 1, y == 3]) -solve!(p) -@assert abs(p.optval - 13) < TOLERANCE - -# x = Variable(1) -# p = minimize(x, [eye(2) + x >= ones(2, 2)]) -# solve!(p) -# p = minimize(y, [X >= 1, y >= X]) -# println(x.value) -# @assert x.value - y.value*[1.0,2.0,3.0] <= 0 - -# try some atoms -#y = Variable() -#p = Problem(:minimize,abs(y),[y >= 1]) -#@assert abs(solve!(p) - 1) <= TOLERANCE - -# x = Variable() -# p = Problem(:minimize,kl_div(x,y),[y >= 1 , x <= 0]) -# print -# @assert abs(solve!(p) - 1) <= TOLERANCE diff --git a/test/test2.jl b/test/test2.jl deleted file mode 100644 index 481245ceb..000000000 --- a/test/test2.jl +++ /dev/null @@ -1,98 +0,0 @@ -using Base.Test -using Convex - -TOL = 1e-3 - -# Addition, equality constraints, positive variables -x = Variable() -y = Variable(Positive()) -p = minimize(x+y,x==1) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL - -# geq inequality constraints -x = Variable() -p = minimize(x, x>=1) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL - -# maximize, leq inequality constraints -x = Variable() -p = maximize(x, x<=2) -solve!(p) -@test_approx_eq_eps p.optval 2 TOL - -# nonbinding geq inequality constraints -x = Variable() -p = maximize(x, x<=2, x>=1) -solve!(p) -@test_approx_eq_eps p.optval 2 TOL - -# scalar multiplication -x = Variable() -p = minimize(2*x, x>=1) -solve!(p) -@test_approx_eq_eps p.optval 2 TOL - -# indexing -x = Variable(2) -p = minimize(x[1], x>=1) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL - -# sums -x = Variable(2,2) -p = minimize(sum(x), x>=1) -solve!(p) -@test_approx_eq_eps p.optval 4 TOL - -# sums -x = Variable(2,2) -p = minimize(sum(x) - 2*x[1,1], x>=1, x[1,1]<=2) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL - -# abs -x = Variable() -p = minimize(abs(x), x<=-1) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL - -x = Variable(2,2) -p = minimize(sum(abs(x)), x[2,2]>=1, x[1,1]>=1, x>=0) -solve!(p) -@test_approx_eq_eps p.optval 2 TOL - -# Diag -x = Variable(2,2) -p = minimize(sum(diag(x,1)), x>=1) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL - -# .* -x = Variable(3, Positive()) -p = maximize(sum(x.*[1,2,3]), x<=1) -solve!(p) -@test_approx_eq_eps p.optval 6 TOL - -x = Variable(3, 3, Positive()) -p = maximize(sum(x.*eye(3)), x<=1) -solve!(p) -@test_approx_eq_eps p.optval 3 TOL - -x = Variable(5, 5) -p = minimize(x[1, 1], 3 .* x >= 3) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL - -# ./ -x = Variable(1, 3, Positive()) -p = maximize(sum(x./[1 2 3]), x<=1) -solve!(p) -@test_approx_eq_eps p.optval 11/6 TOL - -# huber loss -x = Variable(3) -p = minimize(sum(huber(x, 1)), x >= 2) -solve!(p) -@test_approx_eq_eps p.optval 9 TOL diff --git a/test/test_affine.jl b/test/test_affine.jl new file mode 100644 index 000000000..f37552ab4 --- /dev/null +++ b/test/test_affine.jl @@ -0,0 +1,204 @@ +using Convex +using FactCheck + +TOL = 1e-3 + +facts("Affine Atoms") do + + context("negate atom") do + x = Variable() + p = minimize(-x, [x <= 0]) + solve!(p) + @fact p.optval => roughly(0, TOL) + end + + context("multiply atom") do + x = Variable(1) + p = minimize(2.0 * x, [x >= 2, x <= 4]) + solve!(p) + @fact p.optval => roughly(4, TOL) + + x = Variable(2) + A = 1.5 * eye(2) + p = minimize([2 2] * x, [A * x >= [1.1; 1.1]]) + solve!(p) + @fact p.optval => roughly(2.93333, TOL) + + y = Variable(1) + x = Variable(3) + z = [1.0, 2.0, 3.0] * y + k = -y * [1.0, 2.0, 3.0] + c = [y <= 3.0, y >= 0.0, x >= ones(3), k <= x, x <= z] + o = 3 * y + p = Problem(:minimize, o, c) + solve!(p) + @fact p.optval => roughly(3, TOL) + end + + context("dot atom") do + x = Variable(2) + p = minimize(dot([2.0; 2.0], x), x >= [1.1; 1.1]) + solve!(p) + @fact p.optval => roughly(4.4, TOL) + end + + context("add atom") do + x = Variable(1) + y = Variable(1) + p = minimize(x + y, [x >= 3, y >= 2]) + solve!(p) + @fact p.optval => roughly(5, TOL) + + x = Variable(1) + p = minimize(x, [eye(2) + x >= eye(2)]) + solve!(p) + @fact p.optval => roughly(0, TOL) + + y = Variable() + p = minimize(y - 5, y >= -1) + solve!(p) + @fact p.optval => roughly(-6, TOL) + end + + context("transpose atom") do + x = Variable(2) + c = ones(2, 1) + p = minimize(x' * c, x >= 1) + solve!(p) + @fact p.optval => roughly(2, TOL) + + X = Variable(2, 2) + c = ones(2, 1) + p = minimize(c' * X' * c, [X >= ones(2, 2)]) + solve!(p) + @fact p.optval => roughly(4, TOL) + + rows = 2 + cols = 3 + r = rand(rows, cols) + r_2 = rand(cols, rows) + x = Variable(rows, cols) + c = ones(1, cols) + d = ones(rows, 1) + p = minimize(c * x' * d + d' * x * c' + (c * x''''' * d)', + [x' >= r_2, x >= r, x''' >= r_2, x'' >= r]); + solve!(p) + s = sum(max(r, r_2')) * 3 + @fact p.optval => roughly(s, TOL) + end + + context("index atom") do + x = Variable(2) + p = minimize(x[1] + x[2], [x >= 1]) + solve!(p) + @fact p.optval => roughly(2, TOL) + + rows = 6 + cols = 8 + n = 2 + X = Variable(rows, cols) + A = randn(rows, cols) + c = rand(1, n) + p = minimize(c * X[1:n, 5:5+n-1]' * c', X >= A) + solve!(p); + s = c * A[1:n, 5:5+n-1]' * c' + @fact p.optval => roughly(s[1], TOL) + end + + context("hcat atom") do + x = Variable(4, 4) + y = Variable(4, 6) + p = maximize(sum(x) + sum([y 4*ones(4)]), [x y 2*ones(4, 2)] <= 2) + solve!(p) + @fact p.optval => roughly(96, TOL) + end + + context("vcat atom") do + x = Variable(4, 4) + y = Variable(4, 6) + p = maximize(sum(x) + sum([y 4*eye(4); x -ones(4, 6)]), [ [x, y'] <= 2]) + solve!(p) + @fact p.optval => roughly(104, TOL) + end + + context("reshape atom") do + A = rand(2, 3) + X = Variable(3, 2) + c = rand() + p = minimize(sum(reshape(X, 2, 3) + A), X >= c) + solve!(p) + @fact p.optval => roughly(sum(A + c), TOL) + + b = rand(6) + p = minimize(sum(vec(X) + b), X >= c) + solve!(p) + @fact p.optval => roughly(sum(b + c), TOL) + + x = Variable(4, 4) + c = ones(16, 1) + reshaped = reshape(x, 16, 1) + a = [1:16] + p = minimize(c' * reshaped, reshaped >= a) + solve!(p) + @fact p.optval => roughly(136, TOL) + end + + context("sum atom") do + x = Variable(2,2) + p = minimize(sum(x), x>=1) + solve!(p) + @fact p.optval => roughly(4, TOL) + + x = Variable(2,2) + p = minimize(sum(x) - 2*x[1,1], x>=1, x[1,1]<=2) + solve!(p) + @fact p.optval => roughly(1, TOL) + + x = Variable(10) + a = rand(10, 1) + p = maximize(sum(x[2:6]), x <= a) + solve!(p) + @fact p.optval => roughly(sum(a[2:6]), TOL) + end + + context("diag atom") do + x = Variable(2,2) + p = minimize(sum(diag(x,1)), x >= 1) + solve!(p) + @fact p.optval => roughly(1, TOL) + + x = Variable(4, 4) + p = minimize(sum(diag(x)), x >= 2) + solve!(p) + @fact p.optval => roughly(8, TOL) + end + + context("trace atom") do + x = Variable(2,2) + p = minimize(trace(x), x >= 1) + solve!(p) + @fact p.optval => roughly(2, TOL) + end + + context("dot multiply atom") do + x = Variable(3) + p = maximize(sum(x.*[1,2,3]), x<=1) + solve!(p) + @fact p.optval => roughly(6, TOL) + + x = Variable(3, 3) + p = maximize(sum(x.*eye(3)), x<=1) + solve!(p) + @fact p.optval => roughly(3, TOL) + + x = Variable(5, 5) + p = minimize(x[1, 1], 3 .* x >= 3) + solve!(p) + @fact p.optval => roughly(1, TOL) + + x = Variable(1, 3, Positive()) + p = maximize(sum(x./[1 2 3]), x<=1) + solve!(p) + @fact p.optval => roughly(11/6, TOL) + end +end diff --git a/test/test_exp.jl b/test/test_exp.jl index 4dfc6d93f..6afb2beb9 100644 --- a/test/test_exp.jl +++ b/test/test_exp.jl @@ -1,56 +1,61 @@ -using Base.Test using Convex -using SCS - -TOL = 1e-2 - -# exp -y = Variable() -p = minimize(exp(y), y>=0) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL - -y = Variable() -p = minimize(exp(y), y>=1) -solve!(p) -@test_approx_eq_eps p.optval exp(1) TOL - -# log -y = Variable() -p = maximize(log(y), y<=1) -solve!(p) -@test_approx_eq_eps p.optval 0 TOL - -y = Variable() -p = maximize(log(y), y<=2) -solve!(p) -@test_approx_eq_eps p.optval log(2) TOL - -y = Variable() -p = maximize(log(y), [y<=2, exp(y)<=10]) -solve!(p) -@test_approx_eq_eps p.optval log(2) TOL - -# multidimensional exp -y = Variable(5) -p = minimize(sum(exp(y)), y>=0) -solve!(p) -@test_approx_eq_eps p.optval 5 TOL - - -y = Variable(5) -p = minimize(sum(exp(y)), y>=0) -solve!(p) -@test_approx_eq_eps p.optval 5 TOL - -# logsumexp -y = Variable(5); -p = minimize(logsumexp(y), y>=1); -solve!(p) -@test_approx_eq_eps p.optval log(exp(1)*5) TOL - -# entropy -y = Variable(5, Positive()); -p = maximize(entropy(y), sum(y)<=1); -solve!(p) -@test_approx_eq_eps p.optval -log(1/5) TOL +using FactCheck + +TOL = 1e-3 + +facts("Exp Atoms") do + + context("exp atom") do + y = Variable() + p = minimize(exp(y), y>=0) + solve!(p) + @fact p.optval => roughly(1, TOL) + + y = Variable() + p = minimize(exp(y), y>=1) + solve!(p) + @fact p.optval => roughly(exp(1), TOL) + + y = Variable(5) + p = minimize(sum(exp(y)), y>=0) + solve!(p) + @fact p.optval => roughly(5, TOL) + + y = Variable(5) + p = minimize(sum(exp(y)), y>=0) + solve!(p) + @fact p.optval => roughly(5, TOL) + end + + context("log atom") do + y = Variable() + p = maximize(log(y), y<=1) + solve!(p) + @fact p.optval => roughly(0, TOL) + + y = Variable() + p = maximize(log(y), y<=2) + solve!(p) + @fact p.optval => roughly(log(2), TOL) + + y = Variable() + p = maximize(log(y), [y<=2, exp(y)<=10]) + solve!(p) + @fact p.optval => roughly(log(2), TOL) + end + + context("log sum exp atom") do + y = Variable(5); + p = minimize(logsumexp(y), y>=1); + solve!(p) + @fact p.optval => roughly(log(exp(1)*5), TOL) + end + + context("entropy atom") do + y = Variable(5, Positive()); + p = maximize(entropy(y), sum(y)<=1); + solve!(p) + @fact p.optval => roughly(-log(1/5), TOL) + end + +end diff --git a/test/test_exp_and_sdp.jl b/test/test_exp_and_sdp.jl index 5163fcc62..1bb12a909 100644 --- a/test/test_exp_and_sdp.jl +++ b/test/test_exp_and_sdp.jl @@ -1,11 +1,15 @@ -using Base.Test using Convex -# using SCS -# set_default_solver(SCSSolver()) +using FactCheck TOL = 1e-2 -x = Variable(2, 2) -p = maximize(logdet(x), [x[1, 1] == 1, x[2, 2] == 1]) -solve!(p) -@test_approx_eq_eps p.optval 0 TOL +facts("SDP and Exp Atoms") do + + context("log det atom") do + x = Variable(2, 2) + p = maximize(logdet(x), [x[1, 1] == 1, x[2, 2] == 1]) + solve!(p) + @fact p.optval => roughly(0, TOL) + end + +end diff --git a/test/test_int.jl b/test/test_int.jl index 17bbbc4ea..f9004143d 100644 --- a/test/test_int.jl +++ b/test/test_int.jl @@ -1,5 +1,6 @@ -using Base.Test using Convex +using FactCheck + TOL = 1e-2 LPsolver() = get_default_solver() @@ -11,55 +12,63 @@ else MIPsolver() = get_default_solver() end -# LP fallback interface -x = Variable() -p = minimize(x, x>=4.3) -solve!(p, LPsolver()) -@test_approx_eq_eps p.optval 4.3 TOL +facts("Mixed Integer Programs") do + + context("lp fallback interface") do + x = Variable() + p = minimize(x, x>=4.3) + solve!(p, LPsolver()) + @fact p.optval => roughly(4.3, TOL) -x = Variable(2) -p = minimize(norm(x,1), x[1]>=4.3) -solve!(p, LPsolver()) -@test_approx_eq_eps p.optval 4.3 TOL + x = Variable(2) + p = minimize(norm(x,1), x[1]>=4.3) + solve!(p, LPsolver()) + @fact p.optval => roughly(4.3, TOL) + end -# integer variables -x = Variable(:Int) -p = minimize(x, x>=4.3) -solve!(p, MIPsolver()) -@test_approx_eq_eps p.optval 5 TOL + context("integer variables") do + x = Variable(:Int) + p = minimize(x, x>=4.3) + solve!(p, MIPsolver()) + @fact p.optval => roughly(5, TOL) -x = Variable(2, :Int) -p = minimize(sum(x), x>=4.3) -solve!(p, MIPsolver()) -@test_approx_eq_eps p.optval 10 TOL + x = Variable(2, :Int) + p = minimize(sum(x), x>=4.3) + solve!(p, MIPsolver()) + @fact p.optval => roughly(10, TOL) -x = Variable(:Int) -y = Variable() -p = minimize(sum(x + y), x>=4.3, y>=7) -solve!(p, MIPsolver()) -@test_approx_eq_eps p.optval 12 TOL + x = Variable(:Int) + y = Variable() + p = minimize(sum(x + y), x>=4.3, y>=7) + solve!(p, MIPsolver()) + @fact p.optval => roughly(12, TOL) -x = Variable(2, :Int) -p = minimize(norm(x, 1), x[1]>=4.3) -solve!(p, MIPsolver()) -@test_approx_eq_eps p.optval 5 TOL + x = Variable(2, :Int) + p = minimize(norm(x, 1), x[1]>=4.3) + solve!(p, MIPsolver()) + @fact p.optval => roughly(5, TOL) -x = Variable(2, :Int) -p = minimize(sum(x), x[1]>=4.3, x>=0) -solve!(p, MIPsolver()) -@test_approx_eq_eps p.optval 5 TOL + x = Variable(2, :Int) + p = minimize(sum(x), x[1]>=4.3, x>=0) + solve!(p, MIPsolver()) + @fact p.optval => roughly(5, TOL) -x = Variable(2, :Int) -p = minimize(sum(x), x>=.5) -solve!(p, MIPsolver()) -@test_approx_eq_eps p.optval 2 TOL + x = Variable(2, :Int) + p = minimize(sum(x), x>=.5) + solve!(p, MIPsolver()) + @fact p.optval => roughly(2, TOL) + end -x = Variable(2, :Bin) -p = minimize(sum(x), x>=.5) -solve!(p, MIPsolver()) -@test_approx_eq_eps p.optval 2 TOL + context("binary variables") do + x = Variable(2, :Bin) + p = minimize(sum(x), x>=.5) + solve!(p, MIPsolver()) + @fact p.optval => roughly(2, TOL) -x = Variable(2, :Bin) -p = minimize(sum(x), x[1]>=.5, x>=0) -solve!(p, MIPsolver()) -@test_approx_eq_eps p.optval 1 TOL + x = Variable(2, :Bin) + p = minimize(sum(x), x[1]>=.5, x>=0) + solve!(p, MIPsolver()) + @fact p.optval => roughly(1, TOL) + end + +end diff --git a/test/test_lp.jl b/test/test_lp.jl new file mode 100644 index 000000000..8a6eaa835 --- /dev/null +++ b/test/test_lp.jl @@ -0,0 +1,101 @@ +using Convex +using FactCheck + +TOL = 1e-3 + +facts("LP Atoms") do + + context("abs atom") do + x = Variable() + p = minimize(abs(x), x<=-1) + solve!(p) + @fact p.optval => roughly(1, TOL) + + x = Variable(2,2) + p = minimize(sum(abs(x)), x[2,2]>=1, x[1,1]>=1, x>=0) + solve!(p) + @fact p.optval => roughly(2, TOL) + end + + context("maximum atom") do + x = Variable(10) + a = rand(10, 1) + p = minimize(maximum(x), x >= a) + solve!(p) + @fact p.optval => roughly(maximum(a), TOL) + end + + context("minimum atom") do + x = Variable(1) + a = rand(10, 10) + p = maximize(minimum(x), x <= a) + solve!(p) + @fact p.optval => roughly(minimum(a), TOL) + + x = Variable(4, 4) + y = Variable(4, 6) + z = Variable(1) + c = ones(4, 1) + d = 2 * ones(6, 1) + constraints = [[x y] <= 2, z <= 0, z <= x, 2z >= -1] + objective = sum(x + z) + minimum(y) + c' * y * d + p = maximize(objective, constraints) + solve!(p) + @fact p.optval => roughly(130, TOL) + end + + context("max atom") do + x = Variable(10, 10) + y = Variable(10, 10) + a = rand(10, 10) + b = rand(10, 10) + p = minimize(maximum(max(x, y)), [x >= a, y >= b]) + solve!(p) + max_a = maximum(a) + max_b = maximum(b) + @fact p.optval => roughly(max(max_a, max_b), TOL) + end + + context("min atom") do + x = Variable(10, 10) + y = Variable(10, 10) + a = rand(10, 10) + b = rand(10, 10) + p = maximize(minimum(min(x, y)), [x <= a, y <= b]) + solve!(p) + min_a = minimum(a) + min_b = minimum(b) + @fact p.optval => roughly(min(min_a, min_b), TOL) + end + + context("pos atom") do + x = Variable(3) + a = [-2; 1; 2] + p = minimize(sum(pos(x)), [x >= a, x <= 2]) + solve!(p) + @fact p.optval => roughly(3, TOL) + end + + context("neg atom") do + + end + + context("hinge loss atom") do + + end + + context("norm inf atom") do + x = Variable(3) + p = minimize(norm_inf(x), [-2 <= x, x <= 1]) + solve!(p) + @fact p.optval => roughly(0, TOL) + end + + context("norm 1 atom") do + x = Variable(3) + p = minimize(norm_1(x), [-2 <= x, x <= 1]); + solve!(p) + @fact p.optval => roughly(0, TOL) + end + +end diff --git a/test/test_sdp.jl b/test/test_sdp.jl index c29742669..427b4d730 100644 --- a/test/test_sdp.jl +++ b/test/test_sdp.jl @@ -1,79 +1,86 @@ -using Base.Test using Convex +using FactCheck TOL = 1e-2 -# SDP variables -y = Variable((2,2), :Semidefinite) -p = minimize(y[1,1]) -solve!(p) -@test_approx_eq_eps p.optval 0 TOL +facts("SDP Atoms") do + context("sdp variables") do + y = Variable((2,2), :Semidefinite) + p = minimize(y[1,1]) + solve!(p) + @fact p.optval => roughly(0, TOL) -# SDP variables twice -y = Variable((3,3), :Semidefinite) -p = minimize(y[1,1], y[2,2]==1) -solve!(p) -@test_approx_eq_eps p.optval 0 TOL + y = Variable((3,3), :Semidefinite) + p = minimize(y[1,1], y[2,2]==1) + solve!(p) + @fact p.optval => roughly(0, TOL) -# Solution is obtained as y[2,2] -> infinity -# This test fails on Mosek. See -# https://github.com/JuliaOpt/Mosek.jl/issues/29 -y = Variable((2, 2), :Semidefinite) -p = minimize(y[1, 1], y[1, 2] == 1) -solve!(p) -@test_approx_eq_eps p.optval 0 TOL + # Solution is obtained as y[2,2] -> infinity + # This test fails on Mosek. See + # https://github.com/JuliaOpt/Mosek.jl/issues/29 + y = Variable((2, 2), :Semidefinite) + p = minimize(y[1, 1], y[1, 2] == 1) + solve!(p) + @fact p.optval => roughly(0, TOL) -# SDP variables -y = Semidefinite(3) -p = minimize(sum(diag(y)), y[1, 1] == 1) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL + y = Semidefinite(3) + p = minimize(sum(diag(y)), y[1, 1] == 1) + solve!(p) + @fact p.optval => roughly(1, TOL) -# SDP constraints -x = Variable(Positive()) -y = Variable((3, 3)) -p = minimize(x + y[1, 1], isposdef(y), x >= 1, y[2, 1] == 1) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL + y = Variable((3, 3), :Semidefinite) + p = minimize(trace(y), y[2,1]<=4, y[2,2]>=3) + solve!(p) + @fact p.optval => roughly(3, TOL) -x = Variable(Positive()) -y = Semidefinite(3) -p = minimize(y[1, 2], y[2, 1] == 1) -solve!(p) -@test_approx_eq_eps p.optval 1 TOL + x = Variable(Positive()) + y = Semidefinite(3) + p = minimize(y[1, 2], y[2, 1] == 1) + solve!(p) + @fact p.optval => roughly(1, TOL) + end -# trace -y = Variable((3, 3), :Semidefinite) -p = minimize(trace(y), y[2,1]<=4, y[2,2]>=3) -solve!(p) -@test_approx_eq_eps p.optval 3 TOL + context("sdp constraints") do + x = Variable(Positive()) + y = Variable((3, 3)) + p = minimize(x + y[1, 1], isposdef(y), x >= 1, y[2, 1] == 1) + solve!(p) + @fact p.optval => roughly(1, TOL) + end -# nuclear norm -y = Semidefinite(3) -p = minimize(nuclear_norm(y), y[2,1]<=4, y[2,2]>=3, y[3,3]<=2) -solve!(p) -@test_approx_eq_eps p.optval 3 TOL + context("nuclear norm atom") do + y = Semidefinite(3) + p = minimize(nuclear_norm(y), y[2,1]<=4, y[2,2]>=3, y[3,3]<=2) + solve!(p) + @fact p.optval => roughly(3, TOL) + end -# operator norm -y = Variable((3,3)) -p = minimize(operator_norm(y), y[2,1]<=4, y[2,2]>=3, sum(y)>=12) -solve!(p) -@test_approx_eq_eps p.optval 4 TOL + context("operator norm atom") do + y = Variable((3,3)) + p = minimize(operator_norm(y), y[2,1]<=4, y[2,2]>=3, sum(y)>=12) + solve!(p) + @fact p.optval => roughly(4, TOL) + end -# sigma_max -y = Variable((3,3)) -p = minimize(sigma_max(y), y[2,1]<=4, y[2,2]>=3, sum(y)>=12) -solve!(p) -@test_approx_eq_eps p.optval 4 TOL + context("sigma max atom") do + y = Variable((3,3)) + p = minimize(sigma_max(y), y[2,1]<=4, y[2,2]>=3, sum(y)>=12) + solve!(p) + @fact p.optval => roughly(4, TOL) + end -# lambda_max -y = Semidefinite(3) -p = minimize(lambda_max(y), y[1,1]>=4) -solve!(p) -@test_approx_eq_eps p.optval 4 TOL + context("lambda max atom") do + y = Semidefinite(3) + p = minimize(lambda_max(y), y[1,1]>=4) + solve!(p) + @fact p.optval => roughly(4, TOL) + end -# lambda_min -y = Semidefinite(3) -p = maximize(lambda_min(y), trace(y)<=6) -solve!(p) -@test_approx_eq_eps p.optval 2 TOL + context("lambda min atom") do + y = Semidefinite(3) + p = maximize(lambda_min(y), trace(y)<=6) + solve!(p) + @fact p.optval => roughly(2, TOL) + end + +end diff --git a/test/test_socp.jl b/test/test_socp.jl new file mode 100644 index 000000000..2bf1f081e --- /dev/null +++ b/test/test_socp.jl @@ -0,0 +1,98 @@ +using Convex +using FactCheck + +TOL = 1e-3 + +facts("SOCP Atoms") do + + context("norm 2 atom") do + x = Variable(2, 1) + A = [1 2; 2 1; 3 4] + b = [2; 3; 4] + p = minimize(norm_2(A * x + b)) + solve!(p) + @fact p.optval => roughly(0.64888, TOL) + + x = Variable(2, 1) + A = [1 2; 2 1; 3 4] + b = [2; 3; 4] + lambda = 1 + p = minimize(norm_2(A * x + b) + lambda * norm_2(x), x >= 1) + solve!(p) + @fact p.optval => roughly(14.9049, TOL) + + x = Variable(2, 1) + A = [1 2; 2 1; 3 4] + b = [2; 3; 4] + lambda = 1 + p = minimize(norm_2(A * x + b) + lambda * norm_1(x), x >= 1) + solve!(p) + @fact p.optval => roughly(15.4907, TOL) + end + + context("frobenius norm atom") do + m = Variable(4, 5) + c = [m[3, 3] == 4, m >= 1] + p = minimize(norm(m, :fro), c) + solve!(p) + @fact p.optval => roughly(sqrt(35), TOL) + end + + context("quad over lin atom") do + x = Variable(3, 1) + A = [2 -3 5; -2 9 -3; 5 -8 3] + b = [-3; 9; 5] + c = [3 2 4] + d = -3 + p = minimize(quad_over_lin(A*x + b, c*x + d)) + solve!(p) + @fact p.optval => roughly(17.7831, TOL) + end + + context("qol elementwise atom") do + end + + context("sum squares atom") do + x = Variable(2, 1) + A = [1 2; 2 1; 3 4] + b = [2; 3; 4] + p = minimize(sum_squares(A*x + b)) + solve!(p) + @fact p.optval => roughly(0.42105, TOL) + end + + context("square atom") do + end + + context("inv pos atom") do + end + + context("geo mean atom") do + end + + context("sqrt atom") do + end + + context("quad form atom") do + x = Variable(3, 1) + A = [0.8608 0.3131 0.5458; 0.3131 0.8584 0.5836; 0.5458 0.5836 1.5422] + p = minimize(quad_form(x, A), [x >= 1]) + solve!(p) + @fact p.optval => roughly(6.1464, TOL) + + x = Variable(3, 1) + A = -1.0*[0.8608 0.3131 0.5458; 0.3131 0.8584 0.5836; 0.5458 0.5836 1.5422] + c = [3 2 4] + p = maximize(c*x , [quad_form(x, A) >= -1]) + solve!(p) + @fact p.optval => roughly(3.7713, TOL) + end + + context("huber atom") do + x = Variable(3) + p = minimize(sum(huber(x, 1)), x >= 2) + solve!(p) + @fact p.optval => roughly(9, TOL) + end + +end diff --git a/test/test_utilities.jl b/test/test_utilities.jl index 9816bcdcb..6b795d96c 100644 --- a/test/test_utilities.jl +++ b/test/test_utilities.jl @@ -1,17 +1,22 @@ -using Base.Test using Convex +using FactCheck -# length and size -x = Variable(2,3) -@test length(x)==6 -@test size(x)==(2,3) -@test size(x,1)==2 -@test size(x,2)==3 +facts("Utilities") do -x = Variable(3) -@test length(x)==3 -@test size(x)==(3,1) + context("length and size") do + x = Variable(2,3) + @fact length(x) => 6 + @fact size(x) => (2,3) + @fact size(x,1) => 2 + @fact size(x,2) => 3 -x = Variable() -@test length(x)==1 -@test size(x)==(1,1) \ No newline at end of file + x = Variable(3) + @fact length(x) => 3 + @fact size(x) => (3,1) + + x = Variable() + @fact length(x) => 1 + @fact size(x) => (1,1) + end + +end