Skip to content

Commit

Permalink
return MOI.TIME_LIMIT when time_limit_sec is set
Browse files Browse the repository at this point in the history
  • Loading branch information
zengjian-hu-rai committed Sep 27, 2023
1 parent a71268a commit 379db9a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ function MOI.get(model::Optimizer, ::MOI.TerminationStatus)
else
return MOI.OPTIMAL
end
elseif model.solver_status == "UNKNOWN" &&
model.time_limit_sec !== nothing &&
abs(model.solve_time_sec - model.time_limit_sec) < 1
return MOI.TIME_LIMIT # The solver timed out
else
return MOI.OTHER_ERROR
end
Expand Down
35 changes: 35 additions & 0 deletions test/examples/nqueens_time_limit.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2022 MiniZinc.jl contributors
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

# N-queens example
# based on MiniZinc example nqueens.mzn
# queen in column i is in row q[i]

# Test solver time out
function test_nqueens_timeout()
n = 100 # can't finish in 1s
model = MOI.instantiate(
() -> MiniZinc.Optimizer{Int}("chuffed");
with_cache_type = Int,
with_bridge_type = Int,
)
MOI.set(model, MOI.RawOptimizerAttribute("model_filename"), "test.mzn")
q = MOI.add_variables(model, n)
MOI.add_constraint.(model, q, MOI.Interval(1, n))
MOI.add_constraint(model, MOI.VectorOfVariables(q), MOI.AllDifferent(n))
for op in (+, -)
f = MOI.Utilities.vectorize([op(q[i], i) for i in eachindex(q)])
MOI.add_constraint(model, f, MOI.AllDifferent(n))
end

# test timeout in 1s
MOI.set(model, MOI.TimeLimitSec(), 1.0)
# solve
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) === MOI.TIME_LIMIT
@test MOI.get(model, MOI.ResultCount()) == 0
rm("test.mzn")
return
end

0 comments on commit 379db9a

Please sign in to comment.