Skip to content

Commit

Permalink
support solution_limit option targeting MOI.SolutionLimit; fails curr…
Browse files Browse the repository at this point in the history
…ently due to MiniZinc bug
  • Loading branch information
chriscoey committed Feb 7, 2024
1 parent ecbfd68 commit fed7271
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/SolverAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ function validate(json::Request)#::Vector{Error}
end

if haskey(json, :options)
for (T, k) in [(String, :print_format), (Number, :time_limit_sec)]
for (T, k) in
[(String, :print_format), (Number, :time_limit_sec), (Int, :solution_limit)]
if haskey(json.options, k) && !isa(json.options[k], T)
_err("Invalid `options.$(k)` field. Must be of type `$(T)`.")
end
Expand Down Expand Up @@ -440,14 +441,22 @@ function set_options!(model::MOI.ModelLike, options::Dict{Symbol,Any})#::Nothing
MOI.set(model, MOI.TimeLimitSec(), Float64(get(options, :time_limit_sec, 300.0)))
end

if MOI.supports(model, MOI.SolutionLimit()) && haskey(options, :solution_limit)
# Set solution limit
solution_limit = options[:solution_limit]
if solution_limit <= 0
throw(Error(NotAllowed, "Solution limit must be positive."))
end
MOI.set(model, MOI.SolutionLimit(), solution_limit)
end

if MOI.supports(model, MOI.RelativeGapTolerance()) &&
haskey(options, :relative_gap_tolerance)
# Set relative gap tolerance
rel_gap_tol = Float64(options[:relative_gap_tolerance])
if rel_gap_tol < 0 || rel_gap_tol > 1
throw(Error(NotAllowed, "Relative gap tolerance must be within [0,1]."))
end

MOI.set(model, MOI.RelativeGapTolerance(), rel_gap_tol)
end

Expand All @@ -458,7 +467,6 @@ function set_options!(model::MOI.ModelLike, options::Dict{Symbol,Any})#::Nothing
if abs_gap_tol < 0
throw(Error(NotAllowed, "Absolute gap tolerance must be non-negative."))
end

MOI.set(model, MOI.AbsoluteGapTolerance(), abs_gap_tol)
end

Expand All @@ -468,6 +476,7 @@ function set_options!(model::MOI.ModelLike, options::Dict{Symbol,Any})#::Nothing
:print_format,
:print_only,
:time_limit_sec,
:solution_limit,
:relative_gap_tolerance,
:absolute_gap_tolerance,
]
Expand Down
2 changes: 2 additions & 0 deletions test/all_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ end
"tiny_infeas",
"simple_lp",
"n_queens",
"n_queens_solution_limit_1",
"n_queens_solution_limit_3",
"min_constant",
"cons_true",
"cons_false",
Expand Down
1 change: 1 addition & 0 deletions test/inputs/n_queens_solution_limit_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"feas","variables":["q_1","q_2","q_3","q_4"],"constraints":[["range",1,4,1,"q_1"],["range",1,4,1,"q_2"],["range",1,4,1,"q_3"],["range",1,4,1,"q_4"],["and",["and",["!=","q_1","q_2"],["!=",["abs",["-","q_1","q_2"]],1]],["and",["!=","q_1","q_3"],["!=",["abs",["-","q_1","q_3"]],2]],["and",["!=","q_1","q_4"],["!=",["abs",["-","q_1","q_4"]],3]],["and",["!=","q_2","q_3"],["!=",["abs",["-","q_2","q_3"]],1]],["and",["!=","q_2","q_4"],["!=",["abs",["-","q_2","q_4"]],2]],["and",["!=","q_3","q_4"],["!=",["abs",["-","q_3","q_4"]],1]]]],"objectives":[],"options":{"solver":"MiniZinc","solution_limit":1}}
1 change: 1 addition & 0 deletions test/inputs/n_queens_solution_limit_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"feas","variables":["q_1","q_2","q_3","q_4"],"constraints":[["range",1,4,1,"q_1"],["range",1,4,1,"q_2"],["range",1,4,1,"q_3"],["range",1,4,1,"q_4"],["and",["and",["!=","q_1","q_2"],["!=",["abs",["-","q_1","q_2"]],1]],["and",["!=","q_1","q_3"],["!=",["abs",["-","q_1","q_3"]],2]],["and",["!=","q_1","q_4"],["!=",["abs",["-","q_1","q_4"]],3]],["and",["!=","q_2","q_3"],["!=",["abs",["-","q_2","q_3"]],1]],["and",["!=","q_2","q_4"],["!=",["abs",["-","q_2","q_4"]],2]],["and",["!=","q_3","q_4"],["!=",["abs",["-","q_3","q_4"]],1]]]],"objectives":[],"options":{"solver":"MiniZinc","solution_limit":3}}
1 change: 1 addition & 0 deletions test/outputs/n_queens_solution_limit_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"termination_status":"SOLUTION_LIMIT","names":["\"q_1\"","\"q_2\"","\"q_3\"","\"q_4\""],"results":[{"primal_status":"FEASIBLE_POINT","values":[2,4,1,3]}],"version":"0.1"}
1 change: 1 addition & 0 deletions test/outputs/n_queens_solution_limit_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"names":["\"q_1\"","\"q_2\"","\"q_3\"","\"q_4\""],"termination_status":"OPTIMAL","results":[{"values":[2,4,1,3],"primal_status":"FEASIBLE_POINT"},{"values":[3,1,4,2],"primal_status":"FEASIBLE_POINT"}],"version":"0.1"}

0 comments on commit fed7271

Please sign in to comment.