From 1eecb32508d5befc25fbd5a2db25921f69697311 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Sep 2023 16:40:17 +1200 Subject: [PATCH 1/2] Fix various JET errors --- src/FileFormats/LP/LP.jl | 9 +++++---- src/FileFormats/MPS/MPS.jl | 4 ++-- src/Nonlinear/ReverseAD/mathoptinterface_api.jl | 12 ++++++------ src/Utilities/CleverDicts.jl | 2 +- src/Utilities/struct_of_constraints.jl | 4 ++-- src/Utilities/universalfallback.jl | 2 +- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/FileFormats/LP/LP.jl b/src/FileFormats/LP/LP.jl index 603de5a4fe..4ca4086017 100644 --- a/src/FileFormats/LP/LP.jl +++ b/src/FileFormats/LP/LP.jl @@ -639,7 +639,8 @@ function _parse_section( line::AbstractString, ) if occursin(":", line) # Strip name of the objective - line = String(match(r"(.*?)\:(.*)", line)[2]) + m = match(r"(.*?)\:(.*)", line)::RegexMatch + line = String(m[2]) end if occursin("^", line) line = replace(line, "^" => " ^ ") @@ -675,7 +676,7 @@ function _parse_section( end if isempty(cache.constraint_name) if occursin(":", line) - m = match(r"(.*?)\:(.*)", line) + m = match(r"(.*?)\:(.*)", line)::RegexMatch cache.constraint_name = String(m[1]) line = String(m[2]) else @@ -930,7 +931,7 @@ end function _strip_comment(line::String) if occursin("\\", line) - m = match(r"(.*?)\\(.*)", line) + m = match(r"(.*?)\\(.*)", line)::RegexMatch return strip(String(m[1])) else return strip(line) @@ -1014,7 +1015,7 @@ function _readline(io::IO, line::AbstractString) elseif any(Base.Fix1(startswith, peeked_line), (']', '/')) # Always read in the next line if it starts with ] or /, which are used # in quadratic functions. - return _readline(io, string(line, ' ', peeked_line)) + return _readline(io, string(line, " ", peeked_line)) end return line, peeked_line end diff --git a/src/FileFormats/MPS/MPS.jl b/src/FileFormats/MPS/MPS.jl index 5bea825d7e..9fc47035f0 100644 --- a/src/FileFormats/MPS/MPS.jl +++ b/src/FileFormats/MPS/MPS.jl @@ -994,8 +994,8 @@ function TempMPSModel() Float64[], # row_lower Float64[], # row_upper Sense[], # sense - Vector{Vector{Tuple{Int,Float64}}}[], # A - Bool[], + Vector{Tuple{Int,Float64}}[], # A + VType[], Dict{String,Int}(), String[], Dict{String,Int}(), diff --git a/src/Nonlinear/ReverseAD/mathoptinterface_api.jl b/src/Nonlinear/ReverseAD/mathoptinterface_api.jl index 0cf59ea26f..e75c07ddec 100644 --- a/src/Nonlinear/ReverseAD/mathoptinterface_api.jl +++ b/src/Nonlinear/ReverseAD/mathoptinterface_api.jl @@ -176,7 +176,7 @@ function MOI.eval_objective(d::NLPEvaluator, x) error("No nonlinear objective.") end _reverse_mode(d, x) - return d.objective.forward_storage[1] + return something(d.objective).forward_storage[1] end function MOI.eval_objective_gradient(d::NLPEvaluator, g, x) @@ -185,7 +185,7 @@ function MOI.eval_objective_gradient(d::NLPEvaluator, g, x) end _reverse_mode(d, x) fill!(g, 0.0) - _extract_reverse_pass(g, d, d.objective) + _extract_reverse_pass(g, d, something(d.objective)) return end @@ -297,7 +297,7 @@ function MOI.eval_hessian_objective(d::NLPEvaluator, H, x) _reverse_mode(d, x) fill!(d.input_ϵ, 0.0) if d.objective !== nothing - _eval_hessian(d, d.objective, H, 1.0, 0) + _eval_hessian(d, something(d.objective), H, 1.0, 0) end return end @@ -316,7 +316,7 @@ function MOI.eval_hessian_lagrangian(d::NLPEvaluator, H, x, σ, μ) fill!(d.input_ϵ, 0.0) offset = 0 if d.objective !== nothing - offset += _eval_hessian(d, d.objective, H, σ, offset)::Int + offset += _eval_hessian(d, something(d.objective), H, σ, offset)::Int end for (i, ex) in enumerate(d.constraints) offset += _eval_hessian(d, ex, H, μ[i], offset)::Int @@ -356,7 +356,7 @@ function MOI.eval_hessian_lagrangian_product(d::NLPEvaluator, h, x, v, σ, μ) if d.objective !== nothing _forward_eval_ϵ( d, - d.objective, + something(d.objective), reinterpret(T, d.forward_storage_ϵ), reinterpret(T, d.partials_storage_ϵ), input_ϵ, @@ -365,7 +365,7 @@ function MOI.eval_hessian_lagrangian_product(d::NLPEvaluator, h, x, v, σ, μ) ) _reverse_eval_ϵ( output_ϵ, - d.objective, + something(d.objective), reinterpret(T, d.reverse_storage_ϵ), reinterpret(T, d.partials_storage_ϵ), d.subexpression_reverse_values, diff --git a/src/Utilities/CleverDicts.jl b/src/Utilities/CleverDicts.jl index 0cee2a1e07..737b1a9dc4 100644 --- a/src/Utilities/CleverDicts.jl +++ b/src/Utilities/CleverDicts.jl @@ -93,7 +93,7 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V} hash, inverse_hash, true, - K[], + V[], OrderedCollections.OrderedDict{K,V}(), ) end diff --git a/src/Utilities/struct_of_constraints.jl b/src/Utilities/struct_of_constraints.jl index 4d10126433..41d6f83cda 100644 --- a/src/Utilities/struct_of_constraints.jl +++ b/src/Utilities/struct_of_constraints.jl @@ -320,7 +320,7 @@ function struct_of_constraint_code(struct_name, types, field_types = nothing) model::$typed_struct, ::Type{<:$fun}, ::Type{<:$set}, - )::$(field_type) where {$T} + ) where {$T} if model.$field === nothing model.$field = $(field_type)() $MOI.Utilities._add_variables( @@ -328,7 +328,7 @@ function struct_of_constraint_code(struct_name, types, field_types = nothing) model.num_variables, ) end - return model.$field + return something(model.$field) end ) if type_parametrized diff --git a/src/Utilities/universalfallback.jl b/src/Utilities/universalfallback.jl index c57df71d7b..23cdad1a81 100644 --- a/src/Utilities/universalfallback.jl +++ b/src/Utilities/universalfallback.jl @@ -549,7 +549,7 @@ function MOI.get( if uf.objective === nothing return MOI.get(uf.model, attr) end - return uf.objective + return something(uf.objective) end function MOI.set( From 32e934fa48c96b2421491c72003282a0ecc8bff6 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Sep 2023 16:57:19 +1200 Subject: [PATCH 2/2] More updates --- src/Benchmarks/Benchmarks.jl | 2 +- src/Bridges/Constraint/bridges/all_different.jl | 11 ++++++----- src/FileFormats/LP/LP.jl | 10 +++++----- src/Nonlinear/ReverseAD/reverse_mode.jl | 4 ++-- src/Utilities/parser.jl | 7 +++++-- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Benchmarks/Benchmarks.jl b/src/Benchmarks/Benchmarks.jl index 1099a3e16b..0fa2ff2a92 100644 --- a/src/Benchmarks/Benchmarks.jl +++ b/src/Benchmarks/Benchmarks.jl @@ -222,7 +222,7 @@ end x = MOI.add_variables(model, 10_000) MOI.add_constraints( model, - [ + MOI.ScalarAffineFunction{Float64}[ MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, xi)], 0.0) for xi in x ], diff --git a/src/Bridges/Constraint/bridges/all_different.jl b/src/Bridges/Constraint/bridges/all_different.jl index 33a0204337..654bd14278 100644 --- a/src/Bridges/Constraint/bridges/all_different.jl +++ b/src/Bridges/Constraint/bridges/all_different.jl @@ -165,7 +165,7 @@ function MOI.get( if bridge.y === nothing return MOI.VariableIndex[] end - return [bridge.y] + return MOI.VariableIndex[something(bridge.y)] end function MOI.get( @@ -182,11 +182,12 @@ function MOI.get( bridge::AllDifferentToCountDistinctBridge{T}, ::MOI.ListOfConstraintIndices{MOI.VariableIndex,MOI.EqualTo{T}}, ) where {T} - if bridge.y === nothing - return MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{T}}[] + F, S = MOI.VariableIndex, MOI.EqualTo{T} + ret = MOI.ConstraintIndex{F,S}[] + if bridge.y !== nothing + push!(ret, MOI.ConstraintIndex{F,S}(something(bridge.y).value)) end - ci = MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{T}}(bridge.y.value) - return [ci] + return ret end function MOI.get( diff --git a/src/FileFormats/LP/LP.jl b/src/FileFormats/LP/LP.jl index 4ca4086017..bb9e7c4603 100644 --- a/src/FileFormats/LP/LP.jl +++ b/src/FileFormats/LP/LP.jl @@ -640,7 +640,7 @@ function _parse_section( ) if occursin(":", line) # Strip name of the objective m = match(r"(.*?)\:(.*)", line)::RegexMatch - line = String(m[2]) + line = String(m[2]::AbstractString) end if occursin("^", line) line = replace(line, "^" => " ^ ") @@ -677,8 +677,8 @@ function _parse_section( if isempty(cache.constraint_name) if occursin(":", line) m = match(r"(.*?)\:(.*)", line)::RegexMatch - cache.constraint_name = String(m[1]) - line = String(m[2]) + cache.constraint_name = String(m[1]::AbstractString) + line = String(m[2]::AbstractString) else # Give it a temporary name for now cache.constraint_name = "R$(cache.num_constraints)" @@ -932,7 +932,7 @@ end function _strip_comment(line::String) if occursin("\\", line) m = match(r"(.*?)\\(.*)", line)::RegexMatch - return strip(String(m[1])) + return strip(String(m[1]::AbstractString)) else return strip(line) end @@ -1011,7 +1011,7 @@ function _readline(io::IO, line::AbstractString) return _readline(io, line) elseif any(Base.Fix1(endswith, line), ('+', '-', '[', '=')) # If the line ends with a continuation character, read in the next line. - return _readline(io, string(line, ' ', peeked_line)) + return _readline(io, string(line, " ", peeked_line)) elseif any(Base.Fix1(startswith, peeked_line), (']', '/')) # Always read in the next line if it starts with ] or /, which are used # in quadratic functions. diff --git a/src/Nonlinear/ReverseAD/reverse_mode.jl b/src/Nonlinear/ReverseAD/reverse_mode.jl index 2333fd4cef..ba5b73a92a 100644 --- a/src/Nonlinear/ReverseAD/reverse_mode.jl +++ b/src/Nonlinear/ReverseAD/reverse_mode.jl @@ -314,7 +314,7 @@ end _extract_reverse_pass( g::AbstractVector{T}, d::NLPEvaluator, - f::Union{_FunctionStorage,_SubexpressionStorage}, + f::_FunctionStorage, ) where {T} Fill the gradient vector `g` with the values from the reverse pass. Assumes you @@ -323,7 +323,7 @@ have already called `_reverse_eval_all(d, x)`. function _extract_reverse_pass( g::AbstractVector{T}, d::NLPEvaluator, - f::Union{_FunctionStorage,_SubexpressionStorage}, + f::_FunctionStorage, ) where {T} for i in f.dependent_subexpressions d.subexpression_reverse_values[i] = 0.0 diff --git a/src/Utilities/parser.jl b/src/Utilities/parser.jl index 8b1884a840..610d145906 100644 --- a/src/Utilities/parser.jl +++ b/src/Utilities/parser.jl @@ -370,8 +370,11 @@ Do not name `VariableIndex` constraints. * `x^2` does NOT currently parse. Instead, write `x * x`. """ function loadfromstring!(model, s) - parsedlines = filter(ex -> ex !== nothing, Meta.parse.(split(s, "\n"))) - for line in parsedlines + for string_line in split(s, "\n") + line = Meta.parse(string_line) + if line === nothing + continue + end label, ex = _separate_label(line) T, label = _split_type(label) if label == :variables