Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into dba-fix-stackoverflow
Browse files Browse the repository at this point in the history
  • Loading branch information
bachdavi committed Sep 21, 2023
2 parents aa3b33b + ec5d329 commit b7cbd98
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
fail-fast: false
matrix:
version:
- '1.8'
- '1.9'
os:
- ubuntu-latest
Expand All @@ -34,4 +33,4 @@ jobs:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-runtest@v1
5 changes: 2 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
JSON3 = "1"
MathOptInterface = "1"
ReTestItems = "1"
julia = "1.8"
julia = "1.9"

[extras]
Coverage = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
MiniZinc = "a7f392d2-6c35-496e-b8cc-0974fbfcbf91"
ReTestItems = "817f1d60-ba6b-4fd5-9520-3cf149f6a823"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["HiGHS", "MiniZinc", "ReTestItems", "Test", "Coverage"]
test = ["HiGHS", "MiniZinc", "ReTestItems", "Test"]
32 changes: 23 additions & 9 deletions src/SolverAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ struct Error <: Exception
end

"""
response([request], [model]; kw...)::Dict{String,Any}
response([request], [model], [solver]; kw...)::Dict{String,Any}
Return a response
## Args
- `request::SolverAPI.Request`: [optionally] The request that was received.
- `model::MOI.ModelLike`: [optionally] The model that was solved.
- `solver::MOI.AbstractOptimizer`: [optionally] The solver that was used.
## Keyword Args
Expand All @@ -75,25 +76,39 @@ function response(;
return Dict(pairs)
end
response(error::Error; kw...) = response(; errors = [error], kw...)
function response(json::Request, model::MOI.ModelLike; version = "0.1", kw...)
function response(
json::Request,
model::MOI.ModelLike,
solver::MOI.AbstractOptimizer;
version = "0.1",
kw...,
)
res = Dict{String,Any}()

res["version"] = version

status = MOI.get(model, MOI.TerminationStatus())
res["termination_status"] = status
solver_name = MOI.get(solver, MOI.SolverName())
solver_ver = try
VersionNumber(MOI.get(solver, MOI.SolverVersion()))
catch
pkgversion(parentmodule(typeof(solver)))
end
res["solver_version"] = string(solver_name, '_', solver_ver)

res["termination_status"] = MOI.get(model, MOI.TerminationStatus())

options = get(() -> Dict{String,Any}(), json, :options)
format = get(options, :print_format, nothing)
if !isnothing(format)
# print model
res["model_string"] = print_model(model, format)
end

if Bool(get(options, :print_only, false))
return res
end

res["solve_time_sec"] = Float64(MOI.get(model, MOI.SolveTimeSec()))

result_count = MOI.get(model, MOI.ResultCount())
results = [Dict{String,Any}() for _ in 1:result_count]
var_names = [string('\"', v, '\"') for v in json.variables]
Expand Down Expand Up @@ -170,11 +185,10 @@ function solve(fn, json::Request, solver::MOI.AbstractOptimizer)
load!(json, T, solver_info, model)
fn(model)
options = get(() -> Dict{String,Any}(), json, :options)
if Bool(get(options, :print_only, false))
return response(json, model)
if !Bool(get(options, :print_only, false))
MOI.optimize!(model)
end
MOI.optimize!(model)
return response(json, model)
return response(json, model, solver)
catch e
if e isa MOI.UnsupportedError
throw(Error(Unsupported, sprint(Base.showerror, e)))
Expand Down
11 changes: 8 additions & 3 deletions test/solve_tests.jl → test/all_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ end # end of setup module.
]

@testset "$j" for j in json_names
input = read_json("inputs", j)
output = run_solve(input)
@test JSON3.read(output) == JSON3.read(read_json("outputs", j))
result = JSON3.read(run_solve(read_json("inputs", j)))
@test result.solver_version isa String
@test result.solve_time_sec isa Float64

expect = JSON3.read(read_json("outputs", j))
for (key, expect_value) in pairs(expect)
@test result[key] == expect_value
end
end
end

Expand Down

0 comments on commit b7cbd98

Please sign in to comment.