Skip to content

Commit bd90ea4

Browse files
authored
Improve performance of AbsAtom (#619)
1 parent 2a3b520 commit bd90ea4

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/atoms/AbsAtom.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@ curvature(::AbsAtom) = ConvexVexity()
2020

2121
evaluate(x::AbsAtom) = abs.(evaluate(x.children[1]))
2222

23-
function new_conic_form!(context::Context, A::AbsAtom)
23+
function new_conic_form!(context::Context{T}, A::AbsAtom) where {T}
2424
x = only(A.children)
2525
t = Variable(size(x))
2626
t_obj = conic_form!(context, t)
27-
if sign(x) == ComplexSign()
28-
for i in 1:length(vec(t))
29-
add_constraint!(
30-
context,
31-
t[i] >= LinearAlgebra.norm2([real(x[i]); imag(x[i])]),
32-
)
27+
if iscomplex(x)
28+
tape = conic_form!(context, x)
29+
re_tape = real(tape)
30+
im_tape = imag(tape)
31+
if re_tape isa SPARSE_VECTOR
32+
@assert im_tape isa SPARSE_VECTOR
33+
return abs.(re_tape + im * im_tape)
34+
end
35+
re_safs = MOI.Utilities.scalarize(to_vaf(re_tape))
36+
im_safs = MOI.Utilities.scalarize(to_vaf(im_tape))
37+
for (re, im, ti) in zip(re_safs, im_safs, t_obj.variables)
38+
f = MOI.Utilities.operate(vcat, T, ti, re, im)
39+
MOI.add_constraint(context.model, f, MOI.SecondOrderCone(3))
3340
end
3441
else
3542
add_constraint!(context, t >= x)

test/test_atoms.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,16 +1022,24 @@ function test_AbsAtom()
10221022
return abs2(Variable())
10231023
end
10241024
target = """
1025-
variables: t1, t2, x1, x2, w1, w2
1025+
variables: t1, t2, x1, x2
10261026
minobjective: [1.0 * t1, 1.0*t2]
1027-
[1.0 * t1 + -1.0 * x1] in Nonnegatives(1)
1028-
[1.0 * t2 + -1.0 * w2] in Nonnegatives(1)
1029-
[1.0 * x1, 1.0 * x2, 2.0] in SecondOrderCone(3)
1030-
[1.0 * w2, 1.0 * w1, 2.0] in SecondOrderCone(3)
1027+
[1.0 * t1, 1.0 * x1, 2.0] in SecondOrderCone(3)
1028+
[1.0 * t2, 1.0 * x2, 2.0] in SecondOrderCone(3)
10311029
"""
10321030
_test_atom(target) do context
10331031
return abs(Variable(2) + 2im)
10341032
end
1033+
target = """
1034+
variables: y1, y2
1035+
minobjective: [2 + y1, 5 + y2]
1036+
"""
1037+
_test_atom(target) do context
1038+
x = ComplexVariable(2)
1039+
y = Variable(2)
1040+
fix!(x, [2, 3 - 4im])
1041+
return y + abs(x)
1042+
end
10351043
return
10361044
end
10371045

0 commit comments

Comments
 (0)