Skip to content

Commit ee382c1

Browse files
authored
Add explanatory warning before OptimizeNotCalled error (#3156)
1 parent 8f6081b commit ee382c1

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

docs/src/manual/solutions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ If you are iteratively querying solution information and modifying a model,
305305
query all the results first, then modify the problem.
306306

307307
For example, instead of:
308-
```jldoctest
308+
```jldoctest; filter = r"\@ JuMP.+/src/JuMP.jl"
309309
julia> model = Model(HiGHS.Optimizer);
310310
311311
julia> set_silent(model)
@@ -320,6 +320,8 @@ OPTIMAL::TerminationStatusCode = 1
320320
julia> set_upper_bound(x, 1)
321321
322322
julia> x_val = value(x)
323+
┌ Warning: The model has been modified since the last call to `optimize!` (or `optimize!` has not been called yet). If you are iteratively querying solution information and modifying a model, query all the results first, then modify the model.
324+
└ @ JuMP ~/work/JuMP.jl/JuMP.jl/src/JuMP.jl:1250
323325
ERROR: OptimizeNotCalled()
324326
Stacktrace:
325327
[...]

src/JuMP.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,12 @@ function MOI.get(
12471247
if !MOI.is_set_by_optimize(attr)
12481248
return MOI.get(backend(model), attr, index(v))
12491249
elseif model.is_model_dirty && mode(model) != DIRECT
1250+
@warn(
1251+
"The model has been modified since the last call to `optimize!` (" *
1252+
"or `optimize!` has not been called yet). If you are iteratively " *
1253+
"querying solution information and modifying a model, query all " *
1254+
"the results first, then modify the model.",
1255+
)
12501256
throw(OptimizeNotCalled())
12511257
end
12521258
return _moi_get_result(backend(model), attr, index(v))
@@ -1261,6 +1267,12 @@ function MOI.get(
12611267
if !MOI.is_set_by_optimize(attr)
12621268
return MOI.get(backend(model), attr, index(cr))
12631269
elseif model.is_model_dirty && mode(model) != DIRECT
1270+
@warn(
1271+
"The model has been modified since the last call to `optimize!` (" *
1272+
"or `optimize!` has not been called yet). If you are iteratively " *
1273+
"querying solution information and modifying a model, query all " *
1274+
"the results first, then modify the model.",
1275+
)
12641276
throw(OptimizeNotCalled())
12651277
end
12661278
return _moi_get_result(backend(model), attr, index(cr))

test/model.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,22 @@ function test_optimizer_attribute_get_set()
903903
return
904904
end
905905

906+
function test_optimize_not_called_warning()
907+
model = Model() do
908+
return MOI.Utilities.MockOptimizer(
909+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
910+
)
911+
end
912+
@variable(model, x >= 0)
913+
@constraint(model, c, 2x <= 1)
914+
optimize!(model)
915+
set_start_value(x, 0.0)
916+
@test_logs (:warn,) (@test_throws OptimizeNotCalled objective_value(model))
917+
@test_logs (:warn,) (@test_throws OptimizeNotCalled value(x))
918+
@test_logs (:warn,) (@test_throws OptimizeNotCalled value(c))
919+
return
920+
end
921+
906922
function runtests()
907923
for name in names(@__MODULE__; all = true)
908924
if !startswith("$(name)", "test_")

0 commit comments

Comments
 (0)