Skip to content

Commit

Permalink
Allow empty array (for no constraints)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscoey authored Nov 4, 2023
1 parent 7afbba5 commit 1c82406
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/json_to_moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))."))
Expand Down
1 change: 1 addition & 0 deletions test/all_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/inputs/empty_arr_con.json
Original file line number Diff line number Diff line change
@@ -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"}}
2 changes: 2 additions & 0 deletions test/outputs/empty_arr_con.json
Original file line number Diff line number Diff line change
@@ -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"}

0 comments on commit 1c82406

Please sign in to comment.