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

Fix StackOverflowError coming from MOI.Utilities.operate #6

Merged
merged 20 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ version = "0.1.0"
[deps]
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"

[compat]
ReTestItems = "1"
julia = "1.8"
JSON3 = "1"
MathOptInterface = "1"
ReTestItems = "1"
julia = "1.8"

[extras]
Coverage = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
Expand Down
17 changes: 15 additions & 2 deletions src/SolverAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module SolverAPI

import MathOptInterface as MOI
import JSON3
import MutableArithmetics as MA

export serialize, deserialize, solve, print_model, response

Expand Down Expand Up @@ -469,8 +470,20 @@ function nl_to_aff_or_quad(::Type{T}, f::MOI.ScalarNonlinearFunction) where {T<:
return MOI.Utilities.operate(*, T, args[1], args[1])
end
else
h = get(_quad_ops, f.head, nothing)
isnothing(h) || return MOI.Utilities.operate(h, T, args...)
if f.head == :+
chriscoey marked this conversation as resolved.
Show resolved Hide resolved
# TODO (dba) this is a workaround to avoid a
# `StackOverflowError` error coming from
# `MOI.Utilities.operate`. We should avoid the `...`
# all together.
if args isa Vector{MOI.ScalarAffineFunction{Float64}}
return MA.operate!(+, args[1], args[2...])
hdavid16 marked this conversation as resolved.
Show resolved Hide resolved
else
return MOI.Utilities.operate(+, T, args...)
end
else
h = get(_quad_ops, f.head, nothing)
isnothing(h) || return MOI.Utilities.operate(h, T, args...)
end
end
end
throw(Error(Domain, "Function $f cannot be converted to linear or quadratic form."))
Expand Down
1 change: 1 addition & 0 deletions test/inputs/nl_to_aff_or_quad_overflow.json

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions test/solve_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import MathOptInterface as MOI
export run_solve, read_json

function _get_solver(solver_name::String)
if solver_name == "MiniZinc"
solver_name_lower = lowercase(solver_name)
hdavid16 marked this conversation as resolved.
Show resolved Hide resolved
if solver_name_lower == "minizinc"
return MiniZinc.Optimizer{Int}("chuffed")
elseif solver_name == "HiGHS"
elseif solver_name_lower == "highs"
return HiGHS.Optimizer()
else
error("Solver $solver_name not supported.")
Expand Down Expand Up @@ -102,3 +103,19 @@ end
@test length(errors) >= 1
end
end

@testitem "stress-test" setup = [SolverSetup] begin
using SolverAPI
import JSON3

# names of JSON files in inputs/ and outputs/ folders
json_names = [
"nl_to_aff_or_quad_overflow", # This has 110000 constraints and 100000 variables.
]

@testset "$j" for j in json_names
input = read_json("inputs", j)
output = JSON3.read(run_solve(input))
@test output isa JSON3.Object
end
end
Loading