From 1c82406137160f2fa84913144477c98b11c6185b Mon Sep 17 00:00:00 2001 From: Chris Coey Date: Sat, 4 Nov 2023 12:38:54 -0600 Subject: [PATCH] Allow empty array (for no constraints) --- src/json_to_moi.jl | 17 ++++++++--------- test/all_tests.jl | 1 + test/inputs/empty_arr_con.json | 1 + test/outputs/empty_arr_con.json | 2 ++ 4 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 test/inputs/empty_arr_con.json create mode 100644 test/outputs/empty_arr_con.json diff --git a/src/json_to_moi.jl b/src/json_to_moi.jl index abb5fe5..aca8bfd 100644 --- a/src/json_to_moi.jl +++ b/src/json_to_moi.jl @@ -36,17 +36,11 @@ function add_cons!( vars_map::Dict, solver_info::Dict, ) where {T<:Real} + isempty(a) && return nothing head = a[1] if head == "and" - for i in eachindex(a) - i == 1 && continue - if a[i] isa Bool - if !a[i] - throw(Error(InvalidModel, "Model is infeasible.")) - end - else - add_cons!(T, model, a[i], vars_map, solver_info) - end + for i in 2:length(a) + add_cons!(T, model, a[i], vars_map, solver_info) end elseif head == "Int" v = json_to_snf(a[2], vars_map) @@ -156,6 +150,11 @@ function add_cons!( return nothing end +function add_cons!(::Type{<:Real}, ::MOI.ModelLike, a::A, ::Dict, ::Dict) where {A} + throw(Error(Unsupported, "Argument $a has type $A but should be a JSON array.")) + return nothing +end + _check_v_type(::MOI.VariableIndex) = nothing _check_v_type(_) = throw(Error(InvalidModel, "$v must be a `MOI.VariableIndex`, not $(typeof(v)).")) diff --git a/test/all_tests.jl b/test/all_tests.jl index e09a888..9f548d9 100644 --- a/test/all_tests.jl +++ b/test/all_tests.jl @@ -76,6 +76,7 @@ end "in_con_csp", "in_con_const_mip", "in_con_expr_csp", + "empty_arr_con", ] # solve and check output is expected for each input json file diff --git a/test/inputs/empty_arr_con.json b/test/inputs/empty_arr_con.json new file mode 100644 index 0000000..81c78ce --- /dev/null +++ b/test/inputs/empty_arr_con.json @@ -0,0 +1 @@ +{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[[">=","x",1.5],[],["Nonneg","x"],["Nonneg","y"]],"objectives":[["+","x","y"]],"options":{"solver":"HiGHS"}} diff --git a/test/outputs/empty_arr_con.json b/test/outputs/empty_arr_con.json new file mode 100644 index 0000000..7ca1c03 --- /dev/null +++ b/test/outputs/empty_arr_con.json @@ -0,0 +1,2 @@ +{"termination_status":"OPTIMAL","results":[{"names":["\"x\"","\"y\""],"values":[1.5,0.0], +"primal_status":"FEASIBLE_POINT","objective_value":1.5}],"version":"0.1"}