Skip to content

Commit

Permalink
Remove interval and NonNegative operators (#28)
Browse files Browse the repository at this point in the history
* remove `interval` and `NonNegative`, as these are better expressed with inequalities

* picky formatter lulz
  • Loading branch information
chriscoey authored Jan 10, 2024
1 parent 3161fb0 commit ecbfd68
Show file tree
Hide file tree
Showing 15 changed files with 13 additions and 39 deletions.
15 changes: 1 addition & 14 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
14 changes: 2 additions & 12 deletions test/all_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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 @@ -194,10 +193,7 @@ end
),
)

result = solve(
tiny_min,
HiGHS.Optimizer()
)
result = solve(tiny_min, HiGHS.Optimizer())

@test result["termination_status"] == "OPTIMIZE_NOT_CALLED"
@test result["model_string"] isa String
Expand All @@ -222,11 +218,7 @@ end
),
)

result = solve(
tiny_min,
HiGHS.Optimizer(),
Dict{Symbol,Any}(:print_only => true)
)
result = solve(tiny_min, HiGHS.Optimizer(), Dict{Symbol,Any}(:print_only => true))

@test result["termination_status"] == "OPTIMIZE_NOT_CALLED"
@test result["model_string"] isa String
Expand Down Expand Up @@ -272,8 +264,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 ecbfd68

Please sign in to comment.