Skip to content

Commit

Permalink
Support interval constraints and remove redundant range handling (#…
Browse files Browse the repository at this point in the history
…14)

* support interval, handle range correctly, change an unnecessary range test to use interval

* fixes
  • Loading branch information
chriscoey authored Oct 27, 2023
1 parent fe452e4 commit 568d53a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
24 changes: 11 additions & 13 deletions src/json_to_moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ function add_cons!(
MOI.add_constraint(model, v, MOI.Integer())
f = MOI.ScalarNonlinearFunction(:abs, Any[v])
MOI.add_constraint(model, f, MOI.EqualTo(1))
elseif head == "interval"
if length(a) != 4
throw(Error(InvalidModel, "The `interval` constraint expects 3 arguments."))
end
v = json_to_snf(a[4], vars_map)
_check_v_type(v)
if !(a[2] isa Number && a[3] isa Number)
throw(Error(InvalidModel, "The `interval` constraint expects number bounds."))
end
MOI.add_constraint(model, v, MOI.Interval{T}(T(a[2]), T(a[3])))
elseif head == "range"
if length(a) != 5
throw(Error(InvalidModel, "The `range` constraint expects 4 arguments."))
Expand Down Expand Up @@ -166,19 +176,7 @@ function json_to_snf(a::JSON3.Array, vars_map::Dict)
args = Any[json_to_snf(a[i], vars_map) for i in eachindex(a) if i != 1]

head isa String || return args
if head == "range"
# TODO handle variables in different positions, etc
# TODO handle as interval constraint?
lb, ub, step, x = args
step == 1 || throw(Error(NotAllowed, "Step size $step is not supported."))
return MOI.ScalarNonlinearFunction(
:,
Any[
MOI.ScalarNonlinearFunction(:<=, Any[lb, x]),
MOI.ScalarNonlinearFunction(:<=, Any[x, ub]),
],
)
elseif head == "and"
if head == "and"
head = "forall"
args = Any[args]
elseif head == "or"
Expand Down
2 changes: 1 addition & 1 deletion test/all_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ end
# names of JSON files in inputs/ and outputs/ folders
json_names = [
"feas_range",
"min_range",
"min_interval",
"tiny_min",
"tiny_feas",
"tiny_infeas",
Expand Down
1 change: 1 addition & 0 deletions test/inputs/min_interval.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"min","variables":["x"],"constraints":[["interval",1,3.5,"x"],["Float","x"]],"objectives":["x"],"options":{"time_limit_sec":60,"solver":"HiGHS","silent":0,"print_format":"MOI"}}
1 change: 0 additions & 1 deletion test/inputs/min_range.json

This file was deleted.

1 change: 1 addition & 0 deletions test/outputs/min_interval.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"model_string":"Minimize: x\n\nSubject to:\n x ∈ [1, 3.5]\n","termination_status":"OPTIMAL","results":[{"names":["\"x\""],"values":[1.0],"primal_status":"FEASIBLE_POINT","objective_value":1.0}],"version":"0.1"}
1 change: 0 additions & 1 deletion test/outputs/min_range.json

This file was deleted.

0 comments on commit 568d53a

Please sign in to comment.