Skip to content

Commit 65bd1ab

Browse files
authored
error when invalid status (#212)
* error when invalid status * error on status test
1 parent aa7e827 commit 65bd1ab

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/moi_wrapper.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,6 @@ end
417417
function MOI.optimize!(model::Optimizer)
418418
model.diff = nothing
419419
MOI.optimize!(model.optimizer)
420-
421-
# do not fail. interferes with MOI.Tests.linear12test
422-
if !in(MOI.get(model.optimizer, MOI.TerminationStatus()), (MOI.LOCALLY_SOLVED, MOI.OPTIMAL))
423-
@warn "problem status: $(MOI.get(model.optimizer, MOI.TerminationStatus()))"
424-
return
425-
end
426-
427420
return
428421
end
429422

@@ -486,6 +479,10 @@ The output problem data differentials can be queried with the
486479
attributes [`BackwardOutObjective`](@ref) and [`BackwardOutConstraint`](@ref).
487480
"""
488481
function backward(model::Optimizer)
482+
st = MOI.get(model.optimizer, MOI.TerminationStatus())
483+
if !in(st, (MOI.LOCALLY_SOLVED, MOI.OPTIMAL))
484+
error("Trying to compute the forward differentiation on a model with termination status $(st)")
485+
end
489486
diff = _diff(model)
490487
for (vi, value) in model.input_cache.dx
491488
MOI.set(diff, BackwardInVariablePrimal(), model.index_map[vi], value)
@@ -510,6 +507,10 @@ The output solution differentials can be queried with the attribute
510507
[`ForwardOutVariablePrimal`](@ref).
511508
"""
512509
function forward(model::Optimizer)
510+
st = MOI.get(model.optimizer, MOI.TerminationStatus())
511+
if !in(st, (MOI.LOCALLY_SOLVED, MOI.OPTIMAL))
512+
error("Trying to compute the forward differentiation on a model with termination status $(st)")
513+
end
513514
diff = _diff(model)
514515
if model.input_cache.objective !== nothing
515516
MOI.set(diff, ForwardInObjective(), MOI.Utilities.map_indices(model.index_map, model.input_cache.objective))

test/solver_interface.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,20 @@ import HiGHS
1515
MOI.optimize!(model)
1616
@test MOI.get(model, MOI.ObjectiveValue()) == 0.0
1717
end
18+
19+
@testset "Forward or reverse without optimizing throws" begin
20+
model = DiffOpt.diff_optimizer(HiGHS.Optimizer)
21+
MOI.set(model, MOI.Silent(), true)
22+
x = MOI.add_variable(model)
23+
MOI.add_constraint(model, x, MOI.GreaterThan(1.0))
24+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
25+
MOI.set(model, MOI.ObjectiveFunction{MOI.VariableIndex}(), x)
26+
# do not optimize, just try to differentiate
27+
@test_throws ErrorException DiffOpt.forward(model)
28+
@test_throws ErrorException DiffOpt.backward(model)
29+
# impossible constraint
30+
MOI.add_constraint(model, x, MOI.LessThan(0.5))
31+
MOI.optimize!(model)
32+
@test_throws ErrorException DiffOpt.forward(model)
33+
@test_throws ErrorException DiffOpt.backward(model)
34+
end

0 commit comments

Comments
 (0)