Skip to content
Open
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
14 changes: 13 additions & 1 deletion src/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ function pass_attributes(dest::MOI.ModelLike, src::MOI.ModelLike, index_map)
return
end

function _pass_attribute(
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map,
::MOI.ConstraintFunction,
)
return _pass_attribute(dest, src, index_map, UnsafeConstraintFunction())
end

function _pass_attribute(
dest::MOI.ModelLike,
src::MOI.ModelLike,
Expand Down Expand Up @@ -175,7 +184,10 @@ function _copy_constraints(
cis_src::Vector{<:MOI.ConstraintIndex},
)
for ci in cis_src
f = MOI.get(src, MOI.ConstraintFunction(), ci)
# MOI does not allows the implementation of `MOI.constraint`
# to modify it or hold a copy of it.
# `map_indices` is also often going to copy it anyway
f = MOI.get(src, UnsafeConstraintFunction(), ci)
s = MOI.get(src, MOI.ConstraintSet(), ci)
index_map_FS[ci] =
MOI.add_constraint(dest, map_indices(index_map, f), s)
Expand Down
11 changes: 9 additions & 2 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ end

function MOI.get(
model::AbstractModel,
attr::Union{MOI.ConstraintFunction,MOI.ConstraintSet},
attr::Union{
UnsafeConstraintFunction,
MOI.ConstraintFunction,
MOI.ConstraintSet,
},
ci::MOI.ConstraintIndex,
)
return MOI.get(constraints(model, ci), attr, ci)
Expand Down Expand Up @@ -530,7 +534,10 @@ function MOI.supports(model::AbstractModel, attr::MOI.ObjectiveFunction)
return MOI.supports(model.objective, attr)
end

function MOI.get(model::AbstractModel, attr::MOI.ObjectiveFunction)
function MOI.get(
model::AbstractModel,
attr::Union{UnsafeObjectiveFunction,MOI.ObjectiveFunction},
)
return MOI.get(model.objective, attr)
end

Expand Down
10 changes: 10 additions & 0 deletions src/Utilities/objective_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ function MOI.supports(
return true
end

struct UnsafeObjectiveFunction{F<:MOI.AbstractFunction} <: MOI.AbstractModelAttribute end

function MOI.get(model::MOI.ModelLike, ::UnsafeObjectiveFunction{F}) where {F}
return MOI.get(model, MOI.ObjectiveFunction{F}())
end

function MOI.get(
o::ObjectiveContainer{T},
attr::MOI.ObjectiveFunction{F},
Expand Down Expand Up @@ -153,6 +159,10 @@ function MOI.get(
return convert(F, zero(MOI.ScalarAffineFunction{T}))
end

function MOI.get(o::ObjectiveContainer, ::MOI.ObjectiveFunction{F}) where {F}
return copy(MOI.get(o, UnsafeObjectiveFunction{F}()))
end

function _empty_keeping_sense(o::ObjectiveContainer)
sense, is_sense_set = o.sense, o.is_sense_set
MOI.empty!(o)
Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ end

function MOI.get(
uf::UniversalFallback,
attr::MOI.ObjectiveFunction{F},
attr::Union{MOI.UnsafeObjectiveFunction{F},MOI.ObjectiveFunction{F}},
)::F where {F}
if uf.objective === nothing
return MOI.get(uf.model, attr)
Expand Down Expand Up @@ -866,7 +866,7 @@ end

function MOI.get(
uf::UniversalFallback,
attr::Union{MOI.ConstraintFunction,MOI.ConstraintSet},
attr::Union{MOI.UnsafeConstraintFunction,MOI.ConstraintFunction,MOI.ConstraintSet},
ci::MOI.ConstraintIndex,
)
return MOI.get(constraints(uf, ci), attr, ci)
Expand Down
22 changes: 20 additions & 2 deletions src/Utilities/vector_of_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,32 @@ function MOI.delete(
return
end

struct UnsafeConstraintFunction <: MOI.AbstractConstraintAttribute end

function MOI.get_fallback(
model::MOI.ModelLike,
::UnsafeConstraintFunction,
ci::MOI.ConstraintIndex,
)
return MOI.get(model, MOI.ConstraintFunction(), ci)
end

function MOI.get(
v::VectorOfConstraints{F,S},
::MOI.ConstraintFunction,
::UnsafeConstraintFunction,
ci::MOI.ConstraintIndex{F,S},
) where {F,S}
MOI.throw_if_not_valid(v, ci)
f, _ = v.constraints[ci]::Tuple{F,S}
return copy(f)
return f
end

function MOI.get(
v::VectorOfConstraints{F,S},
::MOI.ConstraintFunction,
ci::MOI.ConstraintIndex{F,S},
) where {F,S}
return copy(MOI.get(v, UnsafeConstraintFunction(), ci))
end

function MOI.get(
Expand Down
Loading