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
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
Next Next commit
Update
  • Loading branch information
odow committed Jun 11, 2024
commit 320ea301817972bd890f2f8a6e72b7bdc2180d51
17 changes: 11 additions & 6 deletions 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 = _to_scalar_nonlinear_function(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 = _to_scalar_nonlinear_function(expr)
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,37 +228,42 @@
return model
end

_to_scalar_nonlinear_function(expr) = expr
_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 _to_scalar_nonlinear_function(expr::Expr)
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

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],
Any[_to_scalar_nonlinear_function(arg) for arg in expr.args[2:end]],
Any[_expr_to_function(arg) for arg in expr.args[2:end]],
)
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 +(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])
if count(arg isa MOI.VariableIndex for arg in args) <= 1
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 *(expr.args[2:end]...)

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
Loading