@@ -864,15 +864,34 @@ function _get_model_attribute(
864
864
)
865
865
end
866
866
867
+ function _throw_if_get_attribute_not_allowed (
868
+ model:: CachingOptimizer ,
869
+ attr;
870
+ needs_optimizer_map:: Bool ,
871
+ )
872
+ # If the state(model) == EMPTY_OPTIMIZER, then
873
+ # `model.model_to_optimizer_map[index]` might be empty (because copy_to
874
+ # has not been called yet), or it might be full, if optimize!(dest, src)
875
+ # did not leave a copy in `dest`.
876
+ missing_map = needs_optimizer_map && isempty (model. model_to_optimizer_map)
877
+ if state (model) == NO_OPTIMIZER || missing_map
878
+ msg =
879
+ " Cannot query $(attr) from `Utilities.CachingOptimizer` " *
880
+ " because no optimizer is attached (the state is `$(state (model)) `)."
881
+ throw (MOI. GetAttributeNotAllowed (attr, msg))
882
+ end
883
+ return
884
+ end
885
+
867
886
function MOI. get (model:: CachingOptimizer , attr:: MOI.AbstractModelAttribute )
868
887
if ! MOI. is_set_by_optimize (attr)
869
888
return MOI. get (model. model_cache, attr)
870
- elseif state (model) == NO_OPTIMIZER
871
- error (
872
- " Cannot query $(attr) from caching optimizer because no " *
873
- " optimizer is attached." ,
874
- )
875
889
end
890
+ _throw_if_get_attribute_not_allowed (
891
+ model,
892
+ attr;
893
+ needs_optimizer_map = false ,
894
+ )
876
895
return _get_model_attribute (model, attr)
877
896
end
878
897
@@ -901,51 +920,33 @@ function MOI.get(
901
920
attr:: Union{MOI.AbstractVariableAttribute,MOI.AbstractConstraintAttribute} ,
902
921
index:: MOI.Index ,
903
922
)
904
- if MOI. is_set_by_optimize (attr)
905
- if state (model) == NO_OPTIMIZER
906
- error (
907
- " Cannot query $(attr) from caching optimizer because no " *
908
- " optimizer is attached." ,
909
- )
910
- end
911
- return map_indices (
912
- model. optimizer_to_model_map,
913
- attr,
914
- MOI. get (
915
- model. optimizer,
916
- attr,
917
- model. model_to_optimizer_map[index],
918
- ):: MOI.attribute_value_type (attr),
919
- )
920
- else
923
+ if ! MOI. is_set_by_optimize (attr)
921
924
return MOI. get (model. model_cache, attr, index)
922
925
end
926
+ _throw_if_get_attribute_not_allowed (model, attr; needs_optimizer_map = true )
927
+ value = MOI. get (
928
+ model. optimizer,
929
+ attr,
930
+ model. model_to_optimizer_map[index],
931
+ ):: MOI.attribute_value_type (attr)
932
+ return map_indices (model. optimizer_to_model_map, attr, value)
923
933
end
924
934
925
935
function MOI. get (
926
936
model:: CachingOptimizer ,
927
937
attr:: Union{MOI.AbstractVariableAttribute,MOI.AbstractConstraintAttribute} ,
928
938
indices:: Vector{<:MOI.Index} ,
929
939
)
930
- if MOI. is_set_by_optimize (attr)
931
- if state (model) == NO_OPTIMIZER
932
- error (
933
- " Cannot query $(attr) from caching optimizer because no " *
934
- " optimizer is attached." ,
935
- )
936
- end
937
- return map_indices (
938
- model. optimizer_to_model_map,
939
- attr,
940
- MOI. get (
941
- model. optimizer,
942
- attr,
943
- map (index -> model. model_to_optimizer_map[index], indices),
944
- ):: Vector{<:MOI.attribute_value_type(attr)} ,
945
- )
946
- else
940
+ if ! MOI. is_set_by_optimize (attr)
947
941
return MOI. get (model. model_cache, attr, indices)
948
942
end
943
+ _throw_if_get_attribute_not_allowed (model, attr; needs_optimizer_map = true )
944
+ value = MOI. get (
945
+ model. optimizer,
946
+ attr,
947
+ map (Base. Fix1 (getindex, model. model_to_optimizer_map), indices),
948
+ ):: Vector{<:MOI.attribute_value_type(attr)}
949
+ return map_indices (model. optimizer_to_model_map, attr, value)
949
950
end
950
951
951
952
# ##
@@ -961,12 +962,7 @@ function MOI.get(
961
962
attr:: MOI.ConstraintPrimal ,
962
963
index:: MOI.ConstraintIndex ,
963
964
)
964
- if state (model) == NO_OPTIMIZER
965
- error (
966
- " Cannot query $(attr) from caching optimizer because no " *
967
- " optimizer is attached." ,
968
- )
969
- end
965
+ _throw_if_get_attribute_not_allowed (model, attr; needs_optimizer_map = true )
970
966
try
971
967
return MOI. get (
972
968
model. optimizer,
@@ -987,12 +983,7 @@ function MOI.get(
987
983
attr:: MOI.ConstraintPrimal ,
988
984
indices:: Vector{<:MOI.ConstraintIndex} ,
989
985
)
990
- if state (model) == NO_OPTIMIZER
991
- error (
992
- " Cannot query $(attr) from caching optimizer because no " *
993
- " optimizer is attached." ,
994
- )
995
- end
986
+ _throw_if_get_attribute_not_allowed (model, attr; needs_optimizer_map = true )
996
987
try
997
988
return MOI. get (
998
989
model. optimizer,
@@ -1055,16 +1046,15 @@ function MOI.set(
1055
1046
end
1056
1047
1057
1048
function MOI. get (model:: CachingOptimizer , attr:: MOI.AbstractOptimizerAttribute )
1058
- if state (model) == NO_OPTIMIZER
1059
- # TODO : Copyable attributes (e.g., `Silent`, `TimeLimitSec`,
1060
- # `NumberOfThreads`) should also be stored in the cache so we could
1061
- # return the value stored in the cache instead. However, for
1062
- # non-copyable attributes( e.g. `SolverName`) the error is appropriate.
1063
- error (
1064
- " Cannot query $(attr) from caching optimizer because no " *
1065
- " optimizer is attached." ,
1066
- )
1067
- end
1049
+ # TODO : Copyable attributes (e.g., `Silent`, `TimeLimitSec`,
1050
+ # `NumberOfThreads`) should also be stored in the cache so we could
1051
+ # return the value stored in the cache instead. However, for
1052
+ # non-copyable attributes( e.g. `SolverName`) the error is appropriate.
1053
+ _throw_if_get_attribute_not_allowed (
1054
+ model,
1055
+ attr;
1056
+ needs_optimizer_map = false ,
1057
+ )
1068
1058
return map_indices (
1069
1059
model. optimizer_to_model_map,
1070
1060
attr,
0 commit comments