Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 22, 2023
1 parent af41d7f commit 7224e52
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 41 deletions.
91 changes: 59 additions & 32 deletions src/Utilities/distance_to_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,37 @@ function distance_to_set(
return LinearAlgebra.norm(element_distance, 2)

Check warning on line 229 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L229

Added line #L229 was not covered by tests
end

# function distance_to_set(
# ::ProjectionUpperBoundDistance,
# x::AbstractVector{T},
# set::MOI.ExponentialConne,
# ) where {T<:Real}
# _check_dimension(x, set)
# return
# end

# function distance_to_set(
# ::ProjectionUpperBoundDistance,
# x::AbstractVector{T},
# set::MOI.DualExponentialConne,
# ) where {T<:Real}
# _check_dimension(x, set)
# return
# end
function distance_to_set(

Check warning on line 232 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L232

Added line #L232 was not covered by tests
::ProjectionUpperBoundDistance,
x::AbstractVector{T},
set::MOI.ExponentialCone,
) where {T<:Real}
_check_dimension(x, set)
if x[2] <= 0 # Project to x[2] = 1
element_distance = (

Check warning on line 239 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L237-L239

Added lines #L237 - L239 were not covered by tests
x[2] - one(T),
max(exp(x[1]) - x[3], zero(T))
)
return LinearAlgebra.norm(element_distance, 2)

Check warning on line 243 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L243

Added line #L243 was not covered by tests
end
return max(x[2] * exp(x[1] / x[2]) - x[3], zero(T))

Check warning on line 245 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L245

Added line #L245 was not covered by tests
end

function distance_to_set(

Check warning on line 248 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L248

Added line #L248 was not covered by tests
::ProjectionUpperBoundDistance,
x::AbstractVector{T},
set::MOI.DualExponentialCone,
) where {T<:Real}
_check_dimension(x, set)
if x[1] >= 0 # Project to x[1] = -1
element_distance = (

Check warning on line 255 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L253-L255

Added lines #L253 - L255 were not covered by tests
x[1] - -one(T),
max(exp(-x[2]) - exp(1) * x[3], zero(T)),
)
return LinearAlgebra.norm(element_distance, 2)

Check warning on line 259 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L259

Added line #L259 was not covered by tests
end
return max(-x[1] * exp(x[2] / x[1]) - exp(1) * x[3], zero(T))

Check warning on line 261 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L261

Added line #L261 was not covered by tests
end

function distance_to_set(

Check warning on line 264 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L264

Added line #L264 was not covered by tests
::ProjectionUpperBoundDistance,
Expand All @@ -254,7 +268,7 @@ function distance_to_set(
) where {T<:Real}
_check_dimension(x, set)
t, xs = x[1], @view(x[2:end])
if any(<(zero(T)), xs) # There exists x[i] < 0
if any(<(zero(T)), xs) # Project to x = 0
return LinearAlgebra.norm((min.(xs, zero(T)), max(t, zero(T))), 2)

Check warning on line 272 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L269-L272

Added lines #L269 - L272 were not covered by tests
end
return max(t - prod(xs)^inv(MOI.dimension(set) - 1), zero(T))

Check warning on line 274 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L274

Added line #L274 was not covered by tests
Expand All @@ -267,7 +281,7 @@ function distance_to_set(
) where {T<:Real}
_check_dimension(x, set)
α = set.exponent
if x[1] < 0 || x[2] < 0
if x[1] < 0 || x[2] < 0 # Project to x = 0
return LinearAlgebra.norm(

Check warning on line 285 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L282-L285

Added lines #L282 - L285 were not covered by tests
(min(x[1], zero(T)), min(x[2], zero(T)), x[3]),
2,
Expand All @@ -283,7 +297,7 @@ function distance_to_set(
) where {T<:Real}
_check_dimension(x, set)
α = set.exponent
if x[1] < 0 || x[2] < 0
if x[1] < 0 || x[2] < 0 # Project to x = 0
return LinearAlgebra.norm(

Check warning on line 301 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L298-L301

Added lines #L298 - L301 were not covered by tests
(min(x[1], zero(T)), min(x[2], zero(T)), x[3]),
2,
Expand All @@ -310,25 +324,38 @@ function distance_to_set(
return max(maximum(abs, @view(x[2:end])) - x[1], zero(T))

Check warning on line 324 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L323-L324

Added lines #L323 - L324 were not covered by tests
end

# function distance_to_set(
# ::ProjectionUpperBoundDistance,
# x::AbstractVector{T},
# set::MOI.RelativeEntropyCone,
# ) where {T<:Real}
# _check_dimension(x, set)
# return
# end
function distance_to_set(

Check warning on line 327 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L327

Added line #L327 was not covered by tests
::ProjectionUpperBoundDistance,
x::AbstractVector{T},
set::MOI.RelativeEntropyCone,
) where {T<:Real}
_check_dimension(x, set)
n = div(MOI.dimension(set) - 1, 2)
u, v, w = x[1], @view(x[2:(n+1)]), @view(x[(n+2):end])
_to_one(x) = x <= zero(T) ? one(T) : x
if any(<=(zero(T)), v) || any(<=(zero(T)), w) # Project to v = w = 1
v_p, w_p = _to_one.(v), _to_one.(w)
element_distance = (

Check warning on line 338 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L332-L338

Added lines #L332 - L338 were not covered by tests
v_p .- v,
w_p .- w,
max(
sum(w_p[i] * log(w_p[i] / v_p[i]) for i in eachindex(w)) - u,
zero(T),
),
)
return LinearAlgebra.norm(element_distance, 2)

Check warning on line 346 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L346

Added line #L346 was not covered by tests
end
return max(sum(w[i] * log(w[i] / v[i]) for i in eachindex(w)) - u, zero(T))

Check warning on line 348 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L348

Added line #L348 was not covered by tests
end

function distance_to_set(

Check warning on line 351 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L351

Added line #L351 was not covered by tests
::ProjectionUpperBoundDistance,
x::AbstractVector{T},
set::MOI.HyperRectangle,
) where {T<:Real}
_check_dimension(x, set)
element_distance = (
max.(set.lower .- x, zero(T)),
max.(x .- set.upper, zero(T)),
)
element_distance =

Check warning on line 357 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L356-L357

Added lines #L356 - L357 were not covered by tests
(max.(set.lower .- x, zero(T)), max.(x .- set.upper, zero(T)))
return LinearAlgebra.norm(element_distance, 2)

Check warning on line 359 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L359

Added line #L359 was not covered by tests
end

Expand Down
44 changes: 35 additions & 9 deletions test/Utilities/distance_to_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,27 @@ function test_rotatedsecondordercone()
return
end

# function test_exponential()
# return
# end
function test_exponential()
_test_set(
MOI.ExponentialCone(),
[1.0, 1.0, 1.0] => exp(1) - 1,
[1.0, 1.0, 3.0] => 0.0,
[1.0, -1.0, 1.0] => sqrt(2^2 + (exp(1) - 1)^2);
mismatch = [1.0],
)
return
end

# function test_dualexponential()
# return
# end
function test_dualexponential()
_test_set(
MOI.DualExponentialCone(),
[1.0, 1.0, 1.0] => 2.0,
[-1.0, 1.0, 3.0] => 0.0,
[-2.0, 3.0, 0.1] => 2 * exp(3 / -2) - 0.1 * exp(1);
mismatch = [1.0],
)
return
end

function test_geometricmeancone()
_test_set(
Expand Down Expand Up @@ -233,9 +247,21 @@ function test_norminfinitycone()
return
end

# function test_relativeentropycone()
# return
# end
function test_relativeentropycone()
_test_set(
MOI.RelativeEntropyCone(5),
[1.0, 1.0, 1.0, 1.0, 1.0] => 0.0,
[-2.0, 1.0, 1.0, 1.0, 1.0] => 2.0,
[1.0, 1.0, 2.0, 3.0, 1.0] => 3 * log(3 / 1) + 1 * log(1 / 2) - 1,
[4.0, 1.0, 2.0, 3.0, 1.0] => 0.0,
[4.0, -1.0, 2.0, 3.0, 1.0] => 2.0,
[4.0, 1.0, -2.0, 3.0, 1.0] => 3.0,
[4.0, 1.0, -2.0, 3.0, -1.0] => sqrt(3^2 + 2^2),
[0.0, 1.0, -2.0, 3.0, -1.0] => sqrt(3^2 + 2^2 + (3 * log(3))^2);
mismatch = [1.0],
)
return
end

function test_hyperrectangle()
_test_set(
Expand Down

0 comments on commit 7224e52

Please sign in to comment.