Skip to content

Commit

Permalink
remove interval and NonNegative, as these are better expressed wi…
Browse files Browse the repository at this point in the history
…th inequalities
  • Loading branch information
chriscoey committed Dec 16, 2023
1 parent 59258a2 commit c4cba7c
Show file tree
Hide file tree
Showing 16 changed files with 13 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Make `options` field optional [#22](https://github.com/RelationalAI/SolverAPI.jl/pull/22)
- Remove `names` field in results [#20](https://github.com/RelationalAI/SolverAPI.jl/pull/20)
- Update format to use JSON vector for relational appl constraint
args [#21](https://github.com/RelationalAI/SolverAPI.jl/pull/21)
args [#21](https://github.com/RelationalAI/SolverAPI.jl/pull/21)

## [0.2.2]

Expand Down
17 changes: 2 additions & 15 deletions src/json_to_moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,14 @@ function add_cons!(
_check_v_type(v)
MOI.add_constraint(model, v, MOI.ZeroOne())
elseif head == "Float"
elseif head == "Nonneg"
v = json_to_snf(a[2], vars_map)
_check_v_type(v)
MOI.add_constraint(model, v, MOI.GreaterThan(zero(T)))
# no-op
elseif head == "PosNegOne"
v = json_to_snf(a[2], vars_map)
_check_v_type(v)
# TODO only for MiniZinc
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 three 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 four arguments."))
Expand Down Expand Up @@ -159,7 +146,7 @@ function add_cons!(::Type{<:Real}, ::MOI.ModelLike, a::A, ::Dict, ::Dict) where
end

_check_v_type(::MOI.VariableIndex) = nothing
_check_v_type(_) =
_check_v_type(v::Any) =
throw(Error(InvalidModel, "$v must be a `MOI.VariableIndex`, not $(typeof(v))."))

ineq_to_moi = Dict(:<= => MOI.LessThan, :>= => MOI.GreaterThan, :(==) => MOI.EqualTo)
Expand Down
3 changes: 0 additions & 3 deletions test/all_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ end
# names of JSON files in inputs/ and outputs/ folders
json_names = [
"feas_range",
"min_interval",
"tiny_min",
"tiny_feas",
"tiny_infeas",
Expand Down Expand Up @@ -132,8 +131,6 @@ end
("incorrect_range_num_params", "InvalidModel"),
# range: step not one
("incorrect_range_step_not_1", "InvalidModel"),
# interval: wrong number of args
("incorrect_interval_num_params", "InvalidModel"),
# relational application constraint malformed
("in_con_malformed", "InvalidModel"),
# unsupported objective function type
Expand Down
2 changes: 1 addition & 1 deletion test/inputs/abs_gap_out_of_range.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],["Nonneg","x"]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HIGHS", "absolute_gap_tolerance":-0.1}}
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],[">=","x",0]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HIGHS", "absolute_gap_tolerance":-0.1}}
2 changes: 1 addition & 1 deletion test/inputs/cons_false.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"feas","variables":["x"],"constraints":[["==",1,0],["Nonneg","x"]],"objectives":[],"options":{"solver":"highs"}}
{"version":"0.1","sense":"feas","variables":["x"],"constraints":[["==",1,0],[">=","x",0]],"objectives":[],"options":{"solver":"highs"}}
2 changes: 1 addition & 1 deletion test/inputs/cons_true.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"feas","variables":["x_1","x_2"],"constraints":[["==",0,0],["Nonneg","x_1"],["Nonneg","x_2"]],"objectives":[],"options":{"solver":"highs"}}
{"version":"0.1","sense":"feas","variables":["x_1","x_2"],"constraints":[["==",0,0],[">=","x_1",0],[">=","x_2",0]],"objectives":[],"options":{"solver":"highs"}}
2 changes: 1 addition & 1 deletion test/inputs/empty_arr_con.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[[">=","x",1.5],[],["Nonneg","x"],["Nonneg","y"]],"objectives":[["+","x","y"]],"options":{"solver":"HiGHS"}}
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[[">=","x",1.5],[],[">=","x",0],[">=","y",0]],"objectives":[["+","x","y"]],"options":{"solver":"HiGHS"}}
1 change: 0 additions & 1 deletion test/inputs/incorrect_interval_num_params.json

This file was deleted.

2 changes: 1 addition & 1 deletion test/inputs/min_constant.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"min","variables":["x"],"constraints":[["==","x",1],["Int","x"]],"objectives":[1.5],"options":{"solver":"HiGHS"}}
{"version":"0.1","sense":"min","variables":["x"],"constraints":[["==","x",1],["Bin","x"]],"objectives":[1.5],"options":{"solver":"HiGHS"}}
1 change: 0 additions & 1 deletion test/inputs/min_interval.json

This file was deleted.

2 changes: 1 addition & 1 deletion test/inputs/rel_gap_out_of_range.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],["Nonneg","x"]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HIGHS", "relative_gap_tolerance":1.1}}
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],[">=","x",0]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HIGHS", "relative_gap_tolerance":1.1}}
2 changes: 1 addition & 1 deletion test/inputs/simple_lp.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],["Nonneg","x"]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HiGHS"}}
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],[">=","x",0]],["Bin","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HiGHS"}}
2 changes: 1 addition & 1 deletion test/inputs/unsupported_con_sign.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["!=",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],["Nonneg","x"]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HiGHS"}}
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["!=",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],[">=","x",0]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HiGHS"}}
2 changes: 1 addition & 1 deletion test/inputs/unsupported_con_type.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["*","x",["*","x","y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],["Nonneg","x"]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HiGHS"}}
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["*","x",["*","x","y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],[">=","x",0]],["Int","y"]],"objectives":[["+",["*",2,"x"],"y"]],"options":{"solver":"HiGHS"}}
2 changes: 1 addition & 1 deletion test/inputs/unsupported_obj_type.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],["Nonneg","x"]],["Int","y"]],"objectives":[["/",["*",2,"x"],"y"]],"options":{"solver":"HiGHS"}}
{"version":"0.1","sense":"min","variables":["x","y"],"constraints":[["and",["==",["+","x",["*",3,"y"]],1],[">=",["+","x","y"],1]],["and",["Int","x"],[">=","x",0]],["Int","y"]],"objectives":[["/",["*",2,"x"],"y"]],"options":{"solver":"HiGHS"}}
1 change: 0 additions & 1 deletion test/outputs/min_interval.json

This file was deleted.

0 comments on commit c4cba7c

Please sign in to comment.