Skip to content

Commit

Permalink
Handle constant objectives and constraints containing no variables (#13)
Browse files Browse the repository at this point in the history
* handle constant objectives and constraints containing no variables

* format
  • Loading branch information
chriscoey authored Oct 27, 2023
1 parent 7d11d83 commit fe452e4
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ 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.0]

- Improve test coverage, error messages, and others [#1](https://github.com/RelationalAI/SolverAPI.jl/pull/1), [#7](https://github.com/RelationalAI/SolverAPI.jl/pull/7),
[#8](https://github.com/RelationalAI/SolverAPI.jl/pull/8), [#10](https://github.com/RelationalAI/SolverAPI.jl/pull/10)
- Fix `StackOverflowError` and improve performance [#6](https://github.com/RelationalAI/SolverAPI.jl/pull/6)
- Return solve time and solver version [#5](https://github.com/RelationalAI/SolverAPI.jl/pull/5)
- Add default time limit [#2](https://github.com/RelationalAI/SolverAPI.jl/pull/2)
- Add default time limit [#2](https://github.com/RelationalAI/SolverAPI.jl/pull/2)

## [0.1.0]

- Initial release
11 changes: 9 additions & 2 deletions src/json_to_moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function add_obj!(
::Type{T},
model::MOI.ModelLike,
sense::String,
a::Union{String,JSON3.Array},
a::Any,
vars_map::Dict,
::Dict,
) where {T<:Real}
Expand All @@ -16,6 +16,9 @@ function add_obj!(
MOI.set(model, MOI.ObjectiveSense(), moi_sense)

g = canonicalize_SNF(T, json_to_snf(a, vars_map))
if !(g isa MOI.AbstractScalarFunction)
g = convert(MOI.ScalarAffineFunction{T}, g)
end
g_type = MOI.ObjectiveFunction{typeof(g)}()
if !MOI.supports(model, g_type)
msg = "Objective function $(trunc_str(g)) isn't supported by this solver."
Expand Down Expand Up @@ -139,7 +142,11 @@ function shift_terms(::Type{T}, args::Vector) where {T<:Real}
@assert length(args) == 2 # This should never happen.
g1 = canonicalize_SNF(T, args[1])
g2 = canonicalize_SNF(T, args[2])
return MOI.Utilities.operate(-, T, g1, g2)
g = MOI.Utilities.operate(-, T, g1, g2)
if !(g isa MOI.AbstractScalarFunction)
g = convert(MOI.ScalarAffineFunction{T}, g)
end
return g
end

# Convert object to string and truncate string length if too long.
Expand Down
3 changes: 3 additions & 0 deletions test/all_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ end
"tiny_infeas",
"simple_lp",
"n_queens",
"min_constant",
"cons_true",
"cons_false",
]

# solve and check output is expected for each input json file
Expand Down
1 change: 1 addition & 0 deletions test/inputs/cons_false.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"feas","variables":["x"],"constraints":[["==",1,0],["Nonneg","x"]],"objectives":[],"options":{"solver":"highs"}}
1 change: 1 addition & 0 deletions test/inputs/cons_true.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"feas","variables":["x_1","x_2"],"constraints":[["==",0,0],["Nonneg","x_1"],["Nonneg","x_2"]],"objectives":[],"options":{"solver":"highs"}}
1 change: 1 addition & 0 deletions test/inputs/min_constant.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.1","sense":"min","variables":["x"],"constraints":[["==","x",1],["Int","x"]],"objectives":[1.5],"options":{"solver":"HiGHS"}}
1 change: 1 addition & 0 deletions test/outputs/cons_false.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"termination_status":"INFEASIBLE","version":"0.1"}
1 change: 1 addition & 0 deletions test/outputs/cons_true.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"termination_status":"OPTIMAL","results":[{"names":["\"x_1\"","\"x_2\""],"values":[0.0,0.0],"primal_status":"FEASIBLE_POINT"}],"version":"0.1"}
1 change: 1 addition & 0 deletions test/outputs/min_constant.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"termination_status":"OPTIMAL","results":[{"names":["\"x\""],"values":[1.0],"primal_status":"FEASIBLE_POINT","objective_value":1.5}],"version":"0.1"}

0 comments on commit fe452e4

Please sign in to comment.