Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support in / table / relational application constraints #16

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.1]
- Support `interval` constraint [#14](https://github.com/RelationalAI/SolverAPI.jl/pull/14)
- Handle constant objectives and constraints containing no
variables
[#13](https://github.com/RelationalAI/SolverAPI.jl/pull/13)
- Improve error messages for empty objective [#12](https://github.com/RelationalAI/SolverAPI.jl/pull/12)

- Support `interval` constraint [#14](https://github.com/RelationalAI/SolverAPI.jl/pull/14)
- Handle constant objectives and constraints containing no
variables
[#13](https://github.com/RelationalAI/SolverAPI.jl/pull/13)
- Improve error messages for empty objective [#12](https://github.com/RelationalAI/SolverAPI.jl/pull/12)

## [0.2.0]

Expand Down
14 changes: 14 additions & 0 deletions src/json_to_moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ function add_cons!(
end
MOI.add_constraint(model, v, MOI.Integer())
MOI.add_constraint(model, v, MOI.Interval{T}(a[2], a[3]))
elseif head == "in"
if length(a) < 3
msg = "The relational application constraint expects at least two arguments."
throw(Error(InvalidModel, msg))
end
exprs = [canonicalize_SNF(T, json_to_snf(a[i], vars_map)) for i in 2:length(a)-1]
vecs = a[end]
if !(vecs isa JSON3.Array) || !all(length(row) == length(exprs) for row in vecs)
msg = "The relational application constraint is malformed."
throw(Error(InvalidModel, msg))
end
mat = convert(Matrix{T}, stack(vecs, dims = 1))
vaf = MOI.Utilities.operate(vcat, T, exprs...)
MOI.add_constraint(model, vaf, MOI.Table(mat))
elseif head == "implies" && solver_info[:use_indicator]
# TODO maybe just check if model supports indicator constraints
# use an MOI indicator constraint
Expand Down
8 changes: 8 additions & 0 deletions test/all_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ end
"min_constant",
"cons_true",
"cons_false",
"in_con_mip",
"in_con_csp",
"in_con_const_mip",
"in_con_expr_csp",
]

# solve and check output is expected for each input json file
Expand Down Expand Up @@ -124,6 +128,10 @@ 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
("unsupported_obj_type", "Unsupported"),
# unsupported constraint function type
Expand Down
1 change: 1 addition & 0 deletions test/inputs/in_con_const_mip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"min","variables":["x_1","x_2"],"constraints":[["in","x_1",2,"x_2",[[1,2,1],[-2.5,2,3],[-1,0.5,-2]]],["Float","x_1"],["Float","x_2"]],"objectives":[["+","x_1","x_2"]],"options":{"solver":"highs"}}
1 change: 1 addition & 0 deletions test/inputs/in_con_csp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"min","variables":["x_1","x_2"],"constraints":[["in","x_1","x_2",[[1,2],[-2,3]]],["Int","x_1"],["Int","x_2"]],"objectives":[["+","x_1","x_2"]],"options":{"solver":"minizinc"}}
1 change: 1 addition & 0 deletions test/inputs/in_con_expr_csp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"min","variables":["x_1","x_2"],"constraints":[["in",["+","x_2",1],"x_1",3,[[1,5,2],[-2,1,3]]],["Int","x_1"],["Int","x_2"]],"objectives":[["+","x_1","x_2"]],"options":{"solver":"minizinc"}}
1 change: 1 addition & 0 deletions test/inputs/in_con_malformed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"min","variables":["x_1","x_2"],"constraints":[["in","x_1","x_2",[1,2]],["Float","x_1"],["Float","x_2"]],"objectives":[["+","x_1","x_2"]],"options":{"solver":"minizinc"}}
1 change: 1 addition & 0 deletions test/inputs/in_con_mip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"min","variables":["x_1","x_2"],"constraints":[["in","x_1","x_2",[[1,2],[-2.5,3]]],["Float","x_1"],["Float","x_2"]],"objectives":[["+","x_1","x_2"]],"options":{"solver":"highs"}}
1 change: 1 addition & 0 deletions test/inputs/incorrect_interval_num_params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"feas","variables":["x"],"constraints":[["interval",0,4,1,"x"],["Int","x"]],"objectives":[],"options":{"solver":"MiniZinc"}}
1 change: 1 addition & 0 deletions test/outputs/in_con_const_mip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"termination_status":"OPTIMAL","results":[{"names":["\"x_1\"","\"x_2\""],"values":[-2.5,3],"primal_status":"FEASIBLE_POINT","objective_value":0.5}],"version":"0.1"}
1 change: 1 addition & 0 deletions test/outputs/in_con_csp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"termination_status":"OPTIMAL","results":[{"names":["\"x_1\"","\"x_2\""],"values":[-2,3],"primal_status":"FEASIBLE_POINT","objective_value":1}],"version":"0.1"}
1 change: 1 addition & 0 deletions test/outputs/in_con_expr_csp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"termination_status":"OPTIMAL","results":[{"names":["\"x_1\"","\"x_2\""],"values":[1,-3],"primal_status":"FEASIBLE_POINT","objective_value":-2}],"version":"0.1"}
1 change: 1 addition & 0 deletions test/outputs/in_con_mip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"termination_status":"OPTIMAL","results":[{"names":["\"x_1\"","\"x_2\""],"values":[-2.5,3],"primal_status":"FEASIBLE_POINT","objective_value":0.5}],"version":"0.1"}
Loading