Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bridges] improve function getter of SplitHyperRectangleBridge #2681

Merged
merged 2 commits into from
Mar 6, 2025
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
2 changes: 2 additions & 0 deletions src/Bridges/Bridges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@

_fake_start(value, set::MOI.AbstractVectorSet) = fill(value, MOI.dimension(set))

_fake_start(value::AbstractVector, set::MOI.AbstractVectorSet) = value

Check warning on line 419 in src/Bridges/Bridges.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L419

Added line #L419 was not covered by tests

function _bridged_model(Bridge::Type{<:Constraint.AbstractBridge}, inner)
return Constraint.SingleBridgeOptimizer{Bridge}(inner)
end
Expand Down
97 changes: 58 additions & 39 deletions src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,37 @@
f::F,
s::MOI.HyperRectangle,
) where {T,G,F}
lower = MOI.Utilities.operate(-, T, f, s.lower)
upper = MOI.Utilities.operate(-, T, s.upper, f)
if any(!isfinite, s.lower)
indices = [i for (i, l) in enumerate(s.lower) if isfinite(l)]
lower = MOI.Utilities.eachscalar(lower)[indices]
end
if any(!isfinite, s.upper)
indices = [i for (i, u) in enumerate(s.upper) if isfinite(u)]
upper = MOI.Utilities.eachscalar(upper)[indices]
end
free_indices = Int[]
for (i, (l, u)) in enumerate(zip(s.lower, s.upper))
if !isfinite(l) && !isfinite(u)
push!(free_indices, i)
N = MOI.dimension(s)
g_vec = Vector{MOI.Utilities.scalar_type(G)}(undef, 2 * MOI.dimension(s))
rows_to_keep = fill(true, length(g_vec))
free_rows = Int[]
scalars = MOI.Utilities.eachscalar(f)
for (i, fi) in enumerate(scalars)
if !isfinite(s.lower[i])
rows_to_keep[i] = false

Check warning on line 49 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L42-L49

Added lines #L42 - L49 were not covered by tests
# It doesn't really matter what goes here. We're going to drop it
# when we vectorize the function
g_vec[i] = fi
elseif iszero(s.lower[i])
g_vec[i] = fi

Check warning on line 54 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L52-L54

Added lines #L52 - L54 were not covered by tests
else
g_vec[i] = MOI.Utilities.operate(-, T, fi, s.lower[i])

Check warning on line 56 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L56

Added line #L56 was not covered by tests
end
if !isfinite(s.upper[i])
rows_to_keep[N+i] = false
g_vec[N+i] = fi
elseif iszero(s.upper[i])
g_vec[N+i] = MOI.Utilities.operate(-, T, fi)

Check warning on line 62 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L58-L62

Added lines #L58 - L62 were not covered by tests
else
g_vec[N+i] = MOI.Utilities.operate(-, T, s.upper[i], fi)

Check warning on line 64 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L64

Added line #L64 was not covered by tests
end
if !isfinite(s.lower[i]) && !isfinite(s.upper[i])
push!(free_rows, i)

Check warning on line 67 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L66-L67

Added lines #L66 - L67 were not covered by tests
end
end
free_rows = MOI.Utilities.eachscalar(f)[free_indices]
g = MOI.Utilities.operate(vcat, T, lower, upper)
g = MOI.Utilities.vectorize(g_vec[rows_to_keep])

Check warning on line 70 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L70

Added line #L70 was not covered by tests
ci = MOI.add_constraint(model, g, MOI.Nonnegatives(MOI.output_dimension(g)))
return SplitHyperRectangleBridge{T,G,F}(ci, s, free_rows)
return SplitHyperRectangleBridge{T,G,F}(ci, s, scalars[free_rows])

Check warning on line 72 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L72

Added line #L72 was not covered by tests
end

function MOI.supports_constraint(
Expand Down Expand Up @@ -97,33 +108,41 @@
) where {T,G,F}
f = MOI.get(model, MOI.ConstraintFunction(), bridge.ci)
f_s = MOI.Utilities.eachscalar(f)
s = bridge.set
func = Vector{eltype(f_s)}(undef, MOI.dimension(s))

lower_indices = [i for (i, l) in enumerate(s.lower) if isfinite(l)]
for (i, index) in enumerate(lower_indices)
func[index] = MOI.Utilities.operate(+, T, f_s[i], s.lower[index])
end

upper_indices = [i for (i, u) in enumerate(s.upper) if isfinite(u)]
for (j, index) in enumerate(upper_indices)
i = length(lower_indices) + j
if !(index in lower_indices)
func[index] = MOI.Utilities.operate(-, T, s.upper[index], f_s[i])
end
end
func = Vector{eltype(f_s)}(undef, MOI.dimension(bridge.set))

Check warning on line 111 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L111

Added line #L111 was not covered by tests
free_s = MOI.Utilities.eachscalar(bridge.free_rows)
free_indices = Int[]
for (i, (l, u)) in enumerate(zip(s.lower, s.upper))
n_free_rows, n_f_rows, upper_bound_rows = 0, 0, Int[]
for (row, (l, u)) in enumerate(zip(bridge.set.lower, bridge.set.upper))

Check warning on line 114 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L113-L114

Added lines #L113 - L114 were not covered by tests
if !isfinite(l) && !isfinite(u)
push!(free_indices, i)
n_free_rows += 1
func[row] = free_s[n_free_rows]
elseif iszero(l)
n_f_rows += 1
func[row] = f_s[n_f_rows]
elseif isfinite(l)
n_f_rows += 1
func[row] = MOI.Utilities.operate(+, T, f_s[n_f_rows], l)

Check warning on line 123 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L116-L123

Added lines #L116 - L123 were not covered by tests
else
@assert isfinite(u)

Check warning on line 125 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L125

Added line #L125 was not covered by tests
# This row exists only as u - f, but we don't know where it starts
# yet because we need to count all the `f - l` rows first.
push!(upper_bound_rows, row)

Check warning on line 128 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L128

Added line #L128 was not covered by tests
end
end
for (i, index) in enumerate(free_indices)
func[index] = free_s[i]
for (row, (l, u)) in enumerate(zip(bridge.set.lower, bridge.set.upper))
if !isfinite(u)
continue

Check warning on line 133 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L131-L133

Added lines #L131 - L133 were not covered by tests
end
n_f_rows += 1
if !(row in upper_bound_rows)
continue

Check warning on line 137 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L135-L137

Added lines #L135 - L137 were not covered by tests
end
func[row] = if iszero(bridge.set.upper[row])
MOI.Utilities.operate(-, T, f_s[n_f_rows])

Check warning on line 140 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L139-L140

Added lines #L139 - L140 were not covered by tests
else
MOI.Utilities.operate(-, T, bridge.set.upper[row], f_s[n_f_rows])

Check warning on line 142 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L142

Added line #L142 was not covered by tests
end
end
g = MOI.Utilities.operate(vcat, T, func...)
return MOI.Utilities.convert_approx(F, g)
return MOI.Utilities.convert_approx(F, MOI.Utilities.vectorize(func))

Check warning on line 145 in src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SplitHyperRectangleBridge.jl#L145

Added line #L145 was not covered by tests
end

function MOI.get(
Expand Down
39 changes: 39 additions & 0 deletions test/Bridges/Constraint/SplitHyperRectangleBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,45 @@ function test_runtests_free_row()
return
end

function test_basic_HyperRectangle()
model = MOI.Bridges.Constraint.SplitHyperRectangle{Float64}(
MOI.Utilities.Model{Float64}(),
)
config = MOI.Test.Config()
MOI.empty!(model)
MOI.Test.test_basic_VectorOfVariables_HyperRectangle(model, config)
MOI.empty!(model)
MOI.Test.test_basic_VectorAffineFunction_HyperRectangle(model, config)
MOI.empty!(model)
MOI.Test.test_basic_VectorQuadraticFunction_HyperRectangle(model, config)
MOI.empty!(model)
MOI.Test.test_basic_VectorNonlinearFunction_HyperRectangle(model, config)
return
end

function test_runtests_VectorOfVariables_mix_of_signs()
# 0 <= a <= 1 | a >= 0
# -Inf <= b <= Inf | c >= 0
# 0 <= c <= Inf | -1 + d >= 0
# 1 <= d <= Inf | 1 - a >= 0
# -Inf <= e <= 0 | - e >= 0
# -Inf <= f <= Inf | 1 - g >= 0
# -Inf <= g <= 1 |
MOI.Bridges.runtests(
MOI.Bridges.Constraint.SplitHyperRectangleBridge,
"""
variables: a, b, c, d, e, f, g
[a, b, c, d, e, f, g] in HyperRectangle([0.0, -Inf, 0.0, 1.0, -Inf, -Inf, -Inf], [1.0, Inf, Inf, Inf, 0.0, Inf, 1.0])
""",
"""
variables: a, b, c, d, e, f, g
[1.0 * a, 1.0 * c, 1.0 * d + -1.0, 1.0 + -1.0 * a, -1.0 * e, 1.0 + -1.0 * g] in Nonnegatives(6)
""";
constraint_start = [1.1, 0.0, 1.2, 1.3, -1.1, 0.0, -1.2],
)
return
end

end # module

TestConstraintHyperRectangle.runtests()
Loading