Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions test/test_lp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@
solve!(p, solver)
@test p.optval ≈ 1 atol=TOL
@test evaluate(abs(x)) ≈ 1 atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual correctness.")
@test p.constraints[1].dual ≈ 1 atol=TOL
end

x = Variable(2,2)
p = minimize(sum(abs(x)), x[2,2]>=1, x[1,1]>=1, x>=0)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 2 atol=TOL
@test evaluate(sum(abs(x))) ≈ 2 atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual correctness.")
@test p.constraints[1].dual ≈ 1 atol=TOL
@test p.constraints[2].dual ≈ 1 atol=TOL
@test p.constraints[3].dual[1,1] ≈ 0 atol=TOL
@test p.constraints[3].dual[2,2] ≈ 0 atol=TOL
@test p.constraints[3].dual[1,2] ≈ p.constraints[3].dual[2,1] atol=TOL
end
end

@testset "maximum atom" begin
Expand Down Expand Up @@ -155,6 +167,11 @@
solve!(p, solver)
@test p.optval ≈ 0 atol=TOL
@test evaluate(norm_inf(x)) ≈ 0 atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual correctness.")
@test norm(p.constraints[1].dual) ≈ 0 atol=TOL
@test norm(p.constraints[2].dual) ≈ 0 atol=TOL
end
end

@testset "norm 1 atom" begin
Expand All @@ -164,5 +181,10 @@
solve!(p, solver)
@test p.optval ≈ 0 atol=TOL
@test evaluate(norm_1(x)) ≈ 0 atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual correctness.")
@test norm(p.constraints[1].dual) ≈ 0 atol=TOL
@test norm(p.constraints[2].dual) ≈ 0 atol=TOL
end
end
end
67 changes: 65 additions & 2 deletions test/test_socp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@
solve!(p, solver)
@test p.optval ≈ 14.9049 atol=TOL
@test evaluate(norm2(A * x + b) + lambda * norm2(x)) ≈ 14.9049 atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual correctness.")
dual = [4.4134, 5.1546]
@test p.constraints[1].dual ≈ dual atol=TOL
end

x = Variable(2)

p = minimize(norm2([x[1] + 2x[2] + 2; 2x[1] + x[2] + 3; 3x[1]+4x[2] + 4]) + lambda * norm2(x), x >= 1)
@test vexity(p) == ConvexVexity()

solve!(p, solver)
@test p.optval ≈ 14.9049 atol=TOL
@test evaluate(norm2(A * x + b) + lambda * norm2(x)) ≈ 14.9049 atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual correctness.")
dual = [4.4134, 5.1546]
@test p.constraints[1].dual ≈ dual atol=TOL
end

x = Variable(2, 1)
A = [1 2; 2 1; 3 4]
Expand All @@ -38,6 +46,11 @@
solve!(p, solver)
@test p.optval ≈ 15.4907 atol=TOL
@test evaluate(norm2(A * x + b) + lambda * norm_1(x)) ≈ 15.4907 atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual correctness.")
dual = [4.7062, 5.4475]
@test p.constraints[1].dual ≈ dual atol=TOL
end
end

@testset "frobenius norm atom" begin
Expand All @@ -48,6 +61,12 @@
solve!(p, solver)
@test p.optval ≈ sqrt(35) atol=TOL
@test evaluate(norm(vec(m), 2)) ≈ sqrt(35) atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual correctness.")
@test p.constraints[1].dual ≈ 0.6761 atol=TOL
dual = 0.1690 .* ones(4, 5); dual[3, 3] = 0
@test p.constraints[2].dual ≈ dual atol=TOL
end
end

@testset "quad over lin atom" begin
Expand Down Expand Up @@ -277,5 +296,49 @@
@test o1 <= o2
end
end

@testset "minimal norm solutions" begin
x = Variable(2)
A = [1 2; 2 4];
b = [3, 6];
p = minimize(norm(x, 1), A*x==b)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 1.5 atol=TOL
@test evaluate(x) ≈ [0, 1.5] atol=TOL
@test evaluate(norm(x, 1)) ≈ p.optval atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual problem correctness.")
@test dot(b, p.constraints[1].dual) ≈ p.optval atol=TOL
end

x = Variable(2)
A = [1 2; 2 4];
b = [3, 6];
p = minimize(norm(x, 2), A*x==b)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 3/sqrt(5) atol=TOL
@test evaluate(x) ≈ [3/5, 6/5] atol=TOL
@test evaluate(norm(x, 2)) ≈ p.optval atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual problem correctness.")
@test dot(b, p.constraints[1].dual) ≈ p.optval atol=TOL
end

x = Variable(2)
A = [1 2; 2 4];
b = [3, 6];
p = minimize(norm(x, Inf), A*x==b)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 1.0 atol=TOL
@test evaluate(x) ≈ [1, 1] atol=TOL
@test evaluate(norm(x, Inf)) ≈ p.optval atol=TOL
if p.solution.has_dual
println("Solution object has dual value, checking for dual problem correctness.")
@test dot(b, p.constraints[1].dual) ≈ p.optval atol=TOL
end
end
end
end