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

[FileFormats.NL] read ScalarAffineFunction where possible #2512

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Update
  • Loading branch information
odow committed Jun 11, 2024
commit aa6ac48076d0035d826e4fa880d30693247a9c1d
2 changes: 1 addition & 1 deletion src/FileFormats/NL/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@
MOI.set(model, MOI.NLPBlock(), block)
else
if data.objective != :()
obj = _expr_to_function(data.objective)

Check warning on line 211 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L211

Added line #L211 was not covered by tests
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
end
for (i, expr) in enumerate(data.constraints)
lb, ub = data.constraint_lower[i], data.constraint_upper[i]
f = _expr_to_function(expr)

Check warning on line 216 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L216

Added line #L216 was not covered by tests
if lb == ub
MOI.add_constraint(model, f, MOI.EqualTo(lb))
elseif -Inf == lb && ub < Inf
Expand All @@ -228,13 +228,13 @@
return model
end

_expr_to_function(expr) = expr

Check warning on line 231 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L231

Added line #L231 was not covered by tests

function _expr_to_function(expr::Expr)

Check warning on line 233 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L233

Added line #L233 was not covered by tests
@assert Meta.isexpr(expr, :call)
f = _try_scalar_affine_function(expr)
if f !== nothing
return f
return convert(MOI.ScalarAffineFunction{Float64}, f)

Check warning on line 237 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L235-L237

Added lines #L235 - L237 were not covered by tests
end
return MOI.ScalarNonlinearFunction(
expr.args[1],
Expand All @@ -242,28 +242,28 @@
)
end

_try_scalar_affine_function(x::Float64) = x

Check warning on line 245 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L245

Added line #L245 was not covered by tests

_try_scalar_affine_function(x::MOI.VariableIndex) = x

Check warning on line 247 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L247

Added line #L247 was not covered by tests

function _try_scalar_affine_function(expr::Expr)
if expr.args[1] == :+
args = _try_scalar_affine_function.(expr.args[2:end])
if !any(isnothing, args)
return MOI.Utilities.operate(+, Float64, args...)

Check warning on line 253 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L249-L253

Added lines #L249 - L253 were not covered by tests
end
elseif expr.args[1] == :*
args = _try_scalar_affine_function.(expr.args[2:end])
n_affine_terms = 0
for arg in args
n_affine_terms += arg isa MOI.VariableIndex
n_affine_terms += arg isa MOI.ScalarAffineFunction{Float64}
end
if n_affine_terms <= 1
return MOI.Utilities.operate(*, Float64, args...)

Check warning on line 263 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L255-L263

Added lines #L255 - L263 were not covered by tests
end
end
return nothing

Check warning on line 266 in src/FileFormats/NL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/FileFormats/NL/read.jl#L266

Added line #L266 was not covered by tests
end

function _parse_header(io::IO, model::_CacheModel)
Expand Down
8 changes: 4 additions & 4 deletions test/FileFormats/NL/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,10 @@ function test_hs071_free_constraint_nlexpr()
open(joinpath(@__DIR__, "data", "hs071_free_constraint.nl"), "r") do io
return read!(io, model)
end
@test MOI.get(model, MOI.ListOfConstraintTypesPresent()) == [
(MOI.ScalarNonlinearFunction, MOI.GreaterThan{Float64}),
(MOI.ScalarNonlinearFunction, MOI.Interval{Float64}),
]
types = MOI.get(model, MOI.ListOfConstraintTypesPresent())
@test length(types) == 2
@test (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) in types
@test (MOI.ScalarNonlinearFunction, MOI.Interval{Float64}) in types
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
@test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 1
end
Expand Down
Loading