From 38e7adfa652edcff0bf44ba32b65a675eb2472c5 Mon Sep 17 00:00:00 2001 From: Chris Coey Date: Mon, 30 Oct 2023 13:22:37 -0700 Subject: [PATCH] Support in / table / relational application constraints (#16) --- CHANGELOG.md | 11 ++++++----- src/json_to_moi.jl | 14 ++++++++++++++ test/all_tests.jl | 8 ++++++++ test/inputs/in_con_const_mip.json | 1 + test/inputs/in_con_csp.json | 1 + test/inputs/in_con_expr_csp.json | 1 + test/inputs/in_con_malformed.json | 1 + test/inputs/in_con_mip.json | 1 + test/inputs/incorrect_interval_num_params.json | 1 + test/outputs/in_con_const_mip.json | 1 + test/outputs/in_con_csp.json | 1 + test/outputs/in_con_expr_csp.json | 1 + test/outputs/in_con_mip.json | 1 + 13 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 test/inputs/in_con_const_mip.json create mode 100644 test/inputs/in_con_csp.json create mode 100644 test/inputs/in_con_expr_csp.json create mode 100644 test/inputs/in_con_malformed.json create mode 100644 test/inputs/in_con_mip.json create mode 100644 test/inputs/incorrect_interval_num_params.json create mode 100644 test/outputs/in_con_const_mip.json create mode 100644 test/outputs/in_con_csp.json create mode 100644 test/outputs/in_con_expr_csp.json create mode 100644 test/outputs/in_con_mip.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 902b88b..58fc5d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/src/json_to_moi.jl b/src/json_to_moi.jl index 0fd01fd..abb5fe5 100644 --- a/src/json_to_moi.jl +++ b/src/json_to_moi.jl @@ -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 diff --git a/test/all_tests.jl b/test/all_tests.jl index 98c9d69..64c0510 100644 --- a/test/all_tests.jl +++ b/test/all_tests.jl @@ -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 @@ -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 diff --git a/test/inputs/in_con_const_mip.json b/test/inputs/in_con_const_mip.json new file mode 100644 index 0000000..7678564 --- /dev/null +++ b/test/inputs/in_con_const_mip.json @@ -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"}} diff --git a/test/inputs/in_con_csp.json b/test/inputs/in_con_csp.json new file mode 100644 index 0000000..2b0c8f7 --- /dev/null +++ b/test/inputs/in_con_csp.json @@ -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"}} diff --git a/test/inputs/in_con_expr_csp.json b/test/inputs/in_con_expr_csp.json new file mode 100644 index 0000000..f9ca127 --- /dev/null +++ b/test/inputs/in_con_expr_csp.json @@ -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"}} diff --git a/test/inputs/in_con_malformed.json b/test/inputs/in_con_malformed.json new file mode 100644 index 0000000..d8a6f56 --- /dev/null +++ b/test/inputs/in_con_malformed.json @@ -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"}} diff --git a/test/inputs/in_con_mip.json b/test/inputs/in_con_mip.json new file mode 100644 index 0000000..dcc5938 --- /dev/null +++ b/test/inputs/in_con_mip.json @@ -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"}} diff --git a/test/inputs/incorrect_interval_num_params.json b/test/inputs/incorrect_interval_num_params.json new file mode 100644 index 0000000..d553b45 --- /dev/null +++ b/test/inputs/incorrect_interval_num_params.json @@ -0,0 +1 @@ +{"version":"0.1","sense":"feas","variables":["x"],"constraints":[["interval",0,4,1,"x"],["Int","x"]],"objectives":[],"options":{"solver":"MiniZinc"}} diff --git a/test/outputs/in_con_const_mip.json b/test/outputs/in_con_const_mip.json new file mode 100644 index 0000000..3b9cf4f --- /dev/null +++ b/test/outputs/in_con_const_mip.json @@ -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"} diff --git a/test/outputs/in_con_csp.json b/test/outputs/in_con_csp.json new file mode 100644 index 0000000..7eb0307 --- /dev/null +++ b/test/outputs/in_con_csp.json @@ -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"} diff --git a/test/outputs/in_con_expr_csp.json b/test/outputs/in_con_expr_csp.json new file mode 100644 index 0000000..b7dd59f --- /dev/null +++ b/test/outputs/in_con_expr_csp.json @@ -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"} diff --git a/test/outputs/in_con_mip.json b/test/outputs/in_con_mip.json new file mode 100644 index 0000000..3b9cf4f --- /dev/null +++ b/test/outputs/in_con_mip.json @@ -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"}