From 97ecddbc273eafcbb7cae44e25c8cb0da7ca6b3e Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 13 Dec 2021 14:16:14 +1300 Subject: [PATCH 1/2] [Utilities] Fix location of check_result_index_bounds --- src/Utilities/cachingoptimizer.jl | 2 -- src/Utilities/mockoptimizer.jl | 7 +++---- src/Utilities/results.jl | 4 ++++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Utilities/cachingoptimizer.jl b/src/Utilities/cachingoptimizer.jl index 2826f56413..2542ac1da1 100644 --- a/src/Utilities/cachingoptimizer.jl +++ b/src/Utilities/cachingoptimizer.jl @@ -901,7 +901,6 @@ function MOI.get( "optimizer is attached.", ) end - MOI.check_result_index_bounds(model, attr) try return MOI.get( model.optimizer, @@ -924,7 +923,6 @@ function MOI.get( "optimizer is attached.", ) end - MOI.check_result_index_bounds(model, attr) try return MOI.get( model.optimizer, diff --git a/src/Utilities/mockoptimizer.jl b/src/Utilities/mockoptimizer.jl index 748ba207e5..e4d164d672 100644 --- a/src/Utilities/mockoptimizer.jl +++ b/src/Utilities/mockoptimizer.jl @@ -538,10 +538,10 @@ 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 @@ -549,10 +549,10 @@ 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 @@ -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 @@ -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 diff --git a/src/Utilities/results.jl b/src/Utilities/results.jl index 8098e8a262..aec74dcd90 100644 --- a/src/Utilities/results.jl +++ b/src/Utilities/results.jl @@ -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( @@ -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 @@ -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) @@ -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 From df7dc5cdec8bfc0338ce6a71f8389f137708ee07 Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 13 Dec 2021 14:19:25 +1300 Subject: [PATCH 2/2] Fix try-catch in CachingOptimizer --- src/Utilities/cachingoptimizer.jl | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Utilities/cachingoptimizer.jl b/src/Utilities/cachingoptimizer.jl index 2542ac1da1..5c9c16d3bf 100644 --- a/src/Utilities/cachingoptimizer.jl +++ b/src/Utilities/cachingoptimizer.jl @@ -907,8 +907,12 @@ function MOI.get( 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 @@ -929,8 +933,12 @@ function MOI.get( 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