Skip to content
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
18 changes: 12 additions & 6 deletions src/Utilities/cachingoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -901,15 +901,18 @@ function MOI.get(
"optimizer is attached.",
)
end
MOI.check_result_index_bounds(model, attr)
try
return MOI.get(
model.optimizer,
attr,
model.model_to_optimizer_map[index],
)
catch
return get_fallback(model, attr, index)
catch err
if err isa ArgumentError # Thrown if .optimizer doesn't support attr
return get_fallback(model, attr, index)
else
rethrow(err)
end
end
end

Expand All @@ -924,15 +927,18 @@ function MOI.get(
"optimizer is attached.",
)
end
MOI.check_result_index_bounds(model, attr)
try
return MOI.get(
model.optimizer,
attr,
[model.model_to_optimizer_map[i] for i in indices],
)
catch
return [get_fallback(model, attr, i) for i in indices]
catch err
if err isa ArgumentError # Thrown if .optimizer doesn't support attr
return [get_fallback(model, attr, i) for i in indices]
else
rethrow(err)
end
end
end

Expand Down
7 changes: 3 additions & 4 deletions src/Utilities/mockoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -538,21 +538,21 @@ end
MOI.get(mock::MockOptimizer, ::MOI.TerminationStatus) = mock.termination_status

function MOI.get(mock::MockOptimizer, attr::MOI.ObjectiveValue)
MOI.check_result_index_bounds(mock, attr)
if mock.eval_objective_value
return get_fallback(mock, attr)
end
MOI.check_result_index_bounds(mock, attr)
return get(mock.objective_value, attr.result_index, NaN)
end

function MOI.get(
mock::MockOptimizer{<:MOI.ModelLike,T},
attr::MOI.DualObjectiveValue,
) where {T}
MOI.check_result_index_bounds(mock, attr)
if mock.eval_dual_objective_value
return get_fallback(mock, attr, T)
end
MOI.check_result_index_bounds(mock, attr)
return get(mock.dual_objective_value, attr.result_index, NaN)
end

Expand Down Expand Up @@ -618,7 +618,6 @@ function MOI.get(
attr::MOI.ConstraintPrimal,
idx::MOI.ConstraintIndex,
)
MOI.check_result_index_bounds(mock, attr)
return get_fallback(mock, attr, idx)
end

Expand Down Expand Up @@ -660,12 +659,12 @@ function MOI.get(
attr::MOI.ConstraintDual,
idx::MOI.ConstraintIndex{F},
) where {F}
MOI.check_result_index_bounds(mock, attr)
MOI.throw_if_not_valid(mock, idx)
if mock.eval_variable_constraint_dual &&
(F == MOI.VariableIndex || F == MOI.VectorOfVariables)
return get_fallback(mock, attr, idx)
else
MOI.check_result_index_bounds(mock, attr)
return _safe_get_result(mock.constraint_dual, attr, idx, "dual")
end
end
Expand Down
4 changes: 4 additions & 0 deletions src/Utilities/results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Compute the objective function value using the `VariablePrimal` results and
the `ObjectiveFunction` value.
"""
function get_fallback(model::MOI.ModelLike, attr::MOI.ObjectiveValue)
MOI.check_result_index_bounds(model, attr)
F = MOI.get(model, MOI.ObjectiveFunctionType())
f = MOI.get(model, MOI.ObjectiveFunction{F}())
obj = eval_variables(
Expand Down Expand Up @@ -149,6 +150,7 @@ function get_fallback(
attr::MOI.DualObjectiveValue,
T::Type,
)
MOI.check_result_index_bounds(model, attr)
value = zero(T) # sum will not work if there are zero constraints
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
value += dual_objective_value(model, F, S, T, attr.result_index)::T
Expand Down Expand Up @@ -177,6 +179,7 @@ function get_fallback(
attr::MOI.ConstraintPrimal,
idx::MOI.ConstraintIndex,
)
MOI.check_result_index_bounds(model, attr)
f = MOI.get(model, MOI.ConstraintFunction(), idx)
c = eval_variables(f) do vi
return MOI.get(model, MOI.VariablePrimal(attr.result_index), vi)
Expand Down Expand Up @@ -476,6 +479,7 @@ function get_fallback(
attr::MOI.ConstraintDual,
ci::MOI.ConstraintIndex{<:Union{MOI.VariableIndex,MOI.VectorOfVariables}},
)
MOI.check_result_index_bounds(model, attr)
func = MOI.get(model, MOI.ConstraintFunction(), ci)
return variable_dual(model, attr, ci, func)
end
Expand Down