Skip to content

Commit

Permalink
Trap non-zero process return values in _run_minizinc (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 17, 2023
1 parent 2968ff9 commit ab23f8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,25 @@ function _run_minizinc(dest::Optimizer)
end
output = joinpath(dir, "model.ozn")
_stdout = joinpath(dir, "_stdout.txt")
_minizinc_exe() do exe
cmd = `$(exe) --solver $(dest.solver) --output-objective -o $(output) $(filename)`
if dest.time_limit_sec !== nothing
limit = round(Int, 1_000 * dest.time_limit_sec::Float64)
cmd = `$cmd --time-limit $limit`
_stderr = joinpath(dir, "_stderr.txt")
try
_minizinc_exe() do exe
cmd = `$(exe) --solver $(dest.solver) --output-objective -o $(output) $(filename)`
if dest.time_limit_sec !== nothing
limit = round(Int, 1_000 * dest.time_limit_sec::Float64)
cmd = `$cmd --time-limit $limit`
end
if dest.options["num_solutions"] > 1
cmd = `$cmd --num-solutions $(dest.options["num_solutions"])`
end
return run(pipeline(cmd, stdout = _stdout, stderr = _stderr))
end
if dest.options["num_solutions"] > 1
cmd = `$cmd --num-solutions $(dest.options["num_solutions"])`
catch
status = "=====ERROR=====\n"
if isfile(_stderr)
status *= read(_stderr, String)
end
return run(pipeline(cmd, stdout = _stdout))
return status
end
if isfile(output)
return read(output, String)
Expand Down Expand Up @@ -196,7 +205,7 @@ function MOI.optimize!(dest::Optimizer{T}, src::MOI.ModelLike) where {T}
m_stat = match(r"=====(.+)=====", ret)
if m_stat !== nothing
@assert length(m_stat.captures) == 1
dest.solver_status = m_stat[1]
dest.solver_status = occursin("ERROR", ret) ? ret : m_stat[1]
else
dest.solver_status = "SATISFIABLE"
variable_map = Dict(
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,14 @@ function test_version_number()
return
end

function test_run_failure()
model = MiniZinc.Optimizer{Float64}("a")
MOI.optimize!(model, MOI.Utilities.Model{Float64}())
@test MOI.get(model, MOI.TerminationStatus()) == MOI.OTHER_ERROR
@test occursin("=ERROR=", MOI.get(model, MOI.RawStatusString()))
return
end

end

TestMiniZinc.runtests()

0 comments on commit ab23f8b

Please sign in to comment.