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

Fix various JET errors #2279

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 6 additions & 3 deletions src/Bridges/Constraint/bridges/indicator_sos.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ function MOI.get(
b::IndicatorSOS1Bridge{T},
) where {T}
f = MOI.get(model, attr, b.affine_index)
terms = MOI.VectorAffineTerm{T}[
MOI.VectorAffineTerm(2, t) for t in f.terms if t.variable != b.slack
]
terms = MOI.VectorAffineTerm{T}[]
for t in f.terms
if t.variable != b.slack
push!(terms, MOI.VectorAffineTerm(2, t))
end
end
push!(terms, MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(one(T), b.z)))
return MOI.VectorAffineFunction(terms, [zero(T), f.constant])
end
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/set_dot_scaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ end
function MOI.Bridges.map_set(
::Type{<:SetDotInverseScalingBridge{T,S}},
set::MOI.Scaled{S},
) where {T,S}
) where {T,S<:MOI.AbstractVectorSet}
return set.set
end

Expand Down
20 changes: 13 additions & 7 deletions src/Bridges/Constraint/bridges/split_hyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,17 @@ function MOI.set(
bridge::SplitHyperRectangleBridge{T},
value::AbstractVector{T},
) where {T}
new_values = vcat(
T[v - l for (v, l) in zip(value, bridge.set.lower) if isfinite(l)],
T[u - v for (v, u) in zip(value, bridge.set.upper) if isfinite(u)],
)
new_values = T[]
for (v, l) in zip(value, bridge.set.lower)
if isfinite(l)
push!(new_values, v - l)
end
end
for (v, u) in zip(value, bridge.set.upper)
if isfinite(u)
push!(new_values, u - v)
end
end
MOI.set(model, attr, bridge.ci, new_values)
return
end
Expand Down Expand Up @@ -207,10 +214,9 @@ function MOI.set(
bridge::SplitHyperRectangleBridge{T},
values::AbstractVector{T},
) where {T}
set = bridge.set
new_values = vcat(
T[max(T(0), v) for (v, l) in zip(values, set.lower) if isfinite(l)],
T[min(T(0), v) for (v, u) in zip(values, set.upper) if isfinite(u)],
max.(T(0), values[isfinite.(bridge.set.lower)]),
min.(T(0), values[isfinite.(bridge.set.upper)]),
)
MOI.set(model, attr, bridge.ci, new_values)
return
Expand Down
11 changes: 7 additions & 4 deletions src/FileFormats/MPS/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1235,10 +1235,13 @@ function _add_objective(model, data, variable_map)
else
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
end
affine_terms = MOI.ScalarAffineTerm{Float64}[
MOI.ScalarAffineTerm(data.c[i], variable_map[v]) for
(i, v) in enumerate(data.col_to_name) if !iszero(data.c[i])
]
affine_terms = MOI.ScalarAffineTerm{Float64}[]
for (i, v) in enumerate(data.col_to_name)
if !iszero(data.c[i])
term = MOI.ScalarAffineTerm(data.c[i], variable_map[v])
push!(affine_terms, term)
end
end
q_terms = MOI.ScalarQuadraticTerm{Float64}[]
for (i, j, q) in data.quad_obj
x = variable_map[i]
Expand Down
26 changes: 14 additions & 12 deletions src/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,12 @@ Unfortunately, we don't have a good way of computing the updated costs for other
constraints if a variable bridge is chosen.
"""
function sorted_variable_sets_by_cost(dest::MOI.ModelLike, src::MOI.ModelLike)
constraint_types = MOI.get(src, MOI.ListOfConstraintTypesPresent())
sets = Type[S for (F, S) in constraint_types if _is_variable_function(F)]
sets = Type[]
for (F, S) in MOI.get(src, MOI.ListOfConstraintTypesPresent())
if _is_variable_function(F)
push!(sets, S)
end
end
sort!(sets; by = S::Type -> _cost_of_bridging(dest, S))
return sets
end
Expand Down Expand Up @@ -486,17 +490,15 @@ function default_copy_to(dest::MOI.ModelLike, src::MOI.ModelLike)
# Therefore, all VariableIndex and VectorOfVariable constraints are added
# seprately, and no variables constrained-on-creation are added.
has_nlp = MOI.NLPBlock() in MOI.get(src, MOI.ListOfModelAttributesSet())
constraints_not_added = if has_nlp
Any[
MOI.get(src, MOI.ListOfConstraintIndices{F,S}()) for
(F, S) in MOI.get(src, MOI.ListOfConstraintTypesPresent()) if
_is_variable_function(F)
]
else
Any[
constraints_not_added = Any[]
for S in sorted_variable_sets_by_cost(dest, src)
ret = if has_nlp
F = variable_function_type(S)
MOI.get(src, MOI.ListOfConstraintIndices{F,S}())
else
_try_constrain_variables_on_creation(dest, src, index_map, S)
for S in sorted_variable_sets_by_cost(dest, src)
]
end
push!(constraints_not_added, ret)
end
_copy_free_variables(dest, index_map, vis_src)
# Copy variable attributes
Expand Down
13 changes: 8 additions & 5 deletions src/Utilities/product_of_sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ function MOI.get(
sets::MixOfScalarSets,
::MOI.ListOfConstraintIndices{F,S},
) where {F,S}
i = set_index(sets, S)
return MOI.ConstraintIndex{F,S}[
MOI.ConstraintIndex{F,S}(ci) for
(ci, set_type) in enumerate(sets.set_ids) if set_type == i
]
index = set_index(sets, S)
ret = MOI.ConstraintIndex{F,S}[]
for (i, set_type) in enumerate(sets.set_ids)
if set_type == index
push!(ret, MOI.ConstraintIndex{F,S}(i))
end
end
return ret
end

function MOI.is_valid(
Expand Down
11 changes: 7 additions & 4 deletions src/Utilities/variables_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,13 @@ function MOI.add_variable(b::VariablesContainer{T}) where {T}
end

function MOI.get(b::VariablesContainer, ::MOI.ListOfVariableIndices)
return MOI.VariableIndex[
MOI.VariableIndex(i) for
i in 1:length(b.set_mask) if b.set_mask[i] != _DELETED_VARIABLE
]
ret = MOI.VariableIndex[]
for (i, mask) in enumerate(b.set_mask)
if mask != _DELETED_VARIABLE
push!(ret, MOI.VariableIndex(i))
end
end
return ret
end

function MOI.is_valid(b::VariablesContainer, x::MOI.VariableIndex)
Expand Down