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

Remove using Package in favor of explicit import Package #2274

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 9 additions & 10 deletions src/Benchmarks/Benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

module Benchmarks

using BenchmarkTools

import BenchmarkTools
import MathOptInterface as MOI

const BENCHMARKS = Dict{String,Function}()
Expand Down Expand Up @@ -35,10 +34,10 @@ end
```
"""
function suite(new_model::Function; exclude::Vector{Regex} = Regex[])
group = BenchmarkGroup()
group = BenchmarkTools.BenchmarkGroup()
for (name, func) in BENCHMARKS
any(occursin.(exclude, Ref(name))) && continue
group[name] = @benchmarkable $func($new_model)
group[name] = BenchmarkTools.@benchmarkable $func($new_model)
end
return group
end
Expand All @@ -63,12 +62,12 @@ function create_baseline(
directory::String = "",
kwargs...,
)
tune!(suite)
BenchmarkTools.tune!(suite)
BenchmarkTools.save(
joinpath(directory, name * "_params.json"),
params(suite),
BenchmarkTools.params(suite),
)
results = run(suite; kwargs...)
results = BenchmarkTools.run(suite; kwargs...)
BenchmarkTools.save(joinpath(directory, name * "_baseline.json"), results)
return
end
Expand Down Expand Up @@ -108,19 +107,19 @@ function compare_against_baseline(
if !isfile(params_filename) || !isfile(baseline_filename)
error("You create a baseline with `create_baseline` first.")
end
loadparams!(
BenchmarkTools.loadparams!(
suite,
BenchmarkTools.load(params_filename)[1],
:evals,
:samples,
)
new_results = run(suite; kwargs...)
new_results = BenchmarkTools.run(suite; kwargs...)
old_results = BenchmarkTools.load(baseline_filename)[1]
open(joinpath(directory, report_filename), "w") do io
println(stdout, "\n========== Results ==========")
println(io, "\n========== Results ==========")
for key in keys(new_results)
judgement = judge(
judgement = BenchmarkTools.judge(
BenchmarkTools.median(new_results[key]),
BenchmarkTools.median(old_results[key]),
)
Expand Down
4 changes: 2 additions & 2 deletions src/FileFormats/MOF/MOF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module MOF

import ..FileFormats
import OrderedCollections
import OrderedCollections: OrderedDict
import JSON
import MathOptInterface as MOI

Expand All @@ -16,7 +16,7 @@ const VERSION = v"1.5"
const SUPPORTED_VERSIONS =
(v"1.5", v"1.4", v"1.3", v"1.2", v"1.1", v"1.0", v"0.6", v"0.5", v"0.4")

const OrderedObject = OrderedCollections.OrderedDict{String,Any}
const OrderedObject = OrderedDict{String,Any}
const UnorderedObject = Dict{String,Any}
const Object = Union{OrderedObject,UnorderedObject}

Expand Down
1 change: 0 additions & 1 deletion src/Nonlinear/Nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""
module Nonlinear

import Base.Meta: isexpr
import ForwardDiff
import ..MathOptInterface as MOI
import OrderedCollections: OrderedDict
Expand Down
18 changes: 9 additions & 9 deletions src/Nonlinear/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,28 @@ function parse_expression(
end

function _parse_expression(stack, data, expr, x, parent_index)
if isexpr(x, :call)
if length(x.args) == 2 && !isexpr(x.args[2], :...)
if Meta.isexpr(x, :call)
if length(x.args) == 2 && !Meta.isexpr(x.args[2], :...)
_parse_univariate_expression(stack, data, expr, x, parent_index)
else
# The call is either n-ary, or it is a splat, in which case we
# cannot tell just yet whether the expression is unary or nary.
# Punt to multivariate and try to recover later.
_parse_multivariate_expression(stack, data, expr, x, parent_index)
end
elseif isexpr(x, :comparison)
elseif Meta.isexpr(x, :comparison)
_parse_comparison_expression(stack, data, expr, x, parent_index)
elseif isexpr(x, :...)
elseif Meta.isexpr(x, :...)
_parse_splat_expression(stack, data, expr, x, parent_index)
elseif isexpr(x, :&&) || isexpr(x, :||)
elseif Meta.isexpr(x, :&&) || Meta.isexpr(x, :||)
_parse_logic_expression(stack, data, expr, x, parent_index)
else
error("Unsupported expression: $x")
end
end

function _parse_splat_expression(stack, data, expr, x, parent_index)
@assert isexpr(x, :...) && length(x.args) == 1
@assert Meta.isexpr(x, :...) && length(x.args) == 1
if parent_index == -1
error(
"Unsupported use of the splatting operator. This is only " *
Expand All @@ -155,7 +155,7 @@ function _parse_univariate_expression(
x::Expr,
parent_index::Int,
)
@assert isexpr(x, :call, 2)
@assert Meta.isexpr(x, :call, 2)
id = get(data.operators.univariate_operator_to_id, x.args[1], nothing)
if id === nothing
# It may also be a multivariate operator like * with one argument.
Expand All @@ -177,7 +177,7 @@ function _parse_multivariate_expression(
x::Expr,
parent_index::Int,
)
@assert isexpr(x, :call)
@assert Meta.isexpr(x, :call)
id = get(data.operators.multivariate_operator_to_id, x.args[1], nothing)
if id === nothing
if haskey(data.operators.univariate_operator_to_id, x.args[1])
Expand Down Expand Up @@ -364,7 +364,7 @@ end
_replace_comparison(expr) = expr

function _replace_comparison(expr::Expr)
if isexpr(expr, :call, 4) && expr.args[1] in (:<=, :>=, :(==), :<, :>)
if Meta.isexpr(expr, :call, 4) && expr.args[1] in (:<=, :>=, :(==), :<, :>)
return Expr(
:comparison,
expr.args[2],
Expand Down
4 changes: 2 additions & 2 deletions src/Test/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ The work-around is to wrap `Test` in a module so that `MOI.Test.Test` is
`MOI.Test`.
=#
module _BaseTest
using Test
import Test: @testset, @test, @test_throws, @inferred
end

using ._BaseTest: @testset, @test, @test_throws, @inferred
import ._BaseTest: @testset, @test, @test_throws, @inferred

# Be wary of adding new fields to this Config struct. Always think: can it be
# achieved a different way?
Expand Down
6 changes: 3 additions & 3 deletions src/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module CleverDicts
# solvers using `CleverDicts` to implement it themselves.

import MathOptInterface as MOI
import OrderedCollections
import OrderedCollections: OrderedDict

"""
index_to_key(::Type{K}, index::Int)
Expand Down Expand Up @@ -83,7 +83,7 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
inverse_hash::I
is_dense::Bool
vector::Vector{V}
dict::OrderedCollections.OrderedDict{K,V}
dict::OrderedDict{K,V}
function CleverDict{K,V}(
hash::F = key_to_index,
inverse_hash::I = index_to_key,
Expand All @@ -94,7 +94,7 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
inverse_hash,
true,
V[],
OrderedCollections.OrderedDict{K,V}(),
OrderedDict{K,V}(),
)
end
end
Expand Down
8 changes: 3 additions & 5 deletions src/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

module Utilities

using LinearAlgebra # For dot
using OrderedCollections # for OrderedDict in UniversalFallback

import LinearAlgebra
import MathOptInterface as MOI
import MutableArithmetics as MA

import MathOptInterface.Utilities as MOIU # used in macro
import MutableArithmetics as MA
import OrderedCollections: OrderedDict

const SVF = MOI.VariableIndex
const VVF = MOI.VectorOfVariables
Expand Down
10 changes: 5 additions & 5 deletions src/Utilities/operate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ end
function operate(
::typeof(*),
::Type{T},
D::Diagonal{T},
D::LinearAlgebra.Diagonal{T},
func::MOI.VectorQuadraticFunction{T},
) where {T<:Number}
return MOI.VectorQuadraticFunction{T}(
Expand All @@ -818,7 +818,7 @@ end
function operate(
::typeof(*),
::Type{T},
D::Diagonal{T},
D::LinearAlgebra.Diagonal{T},
func::MOI.VectorAffineFunction{T},
) where {T<:Number}
return MOI.VectorAffineFunction{T}(
Expand All @@ -830,7 +830,7 @@ end
function operate(
::typeof(*),
::Type{T},
D::Diagonal{T},
D::LinearAlgebra.Diagonal{T},
func::MOI.VectorOfVariables,
) where {T<:Number}
return MOI.VectorAffineFunction{T}(
Expand All @@ -847,7 +847,7 @@ end
function operate(
::typeof(*),
::Type{T},
D::Diagonal{T},
D::LinearAlgebra.Diagonal{T},
v::AbstractVector{T},
) where {T<:Number}
return T[D.diag[i] * v[i] for i in eachindex(v)]
Expand Down Expand Up @@ -1556,7 +1556,7 @@ end

### 3a: operate_term(::typeof(*), ::Diagonal, ::Vector{<:VectorTerm})

function operate_terms(::typeof(*), D::Diagonal, terms)
function operate_terms(::typeof(*), D::LinearAlgebra.Diagonal, terms)
return map(term -> operate_term(*, D, term), terms)
end

Expand Down
37 changes: 18 additions & 19 deletions src/Utilities/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

using Base.Meta: isexpr

struct _ParsedScalarAffineTerm{T}
coefficient::T
variable::Symbol
Expand Down Expand Up @@ -61,7 +59,7 @@ end
function _parse_function(ex, ::Type{T} = Float64) where {T}
if isa(ex, Symbol)
return _ParsedVariableIndex(ex)
elseif isexpr(ex, :vect)
elseif Meta.isexpr(ex, :vect)
if all(s -> isa(s, Symbol), ex.args)
return _ParsedVectorOfVariables(copy(ex.args))
else
Expand Down Expand Up @@ -113,20 +111,21 @@ function _parse_function(ex, ::Type{T} = Float64) where {T}
end
end
else
if isexpr(ex, :call, 2) && ex.args[1] == :ScalarNonlinearFunction
if Meta.isexpr(ex, :call, 2) && ex.args[1] == :ScalarNonlinearFunction
return ex
elseif isexpr(ex, :call, 2) && ex.args[1] == :VectorNonlinearFunction
elseif Meta.isexpr(ex, :call, 2) &&
ex.args[1] == :VectorNonlinearFunction
return ex
end
# For simplicity, only accept Expr(:call, :+, ...); no recursive
# expressions
if isexpr(ex, :call) && ex.args[1] == :*
if Meta.isexpr(ex, :call) && ex.args[1] == :*
ex = Expr(:call, :+, ex) # Handle 2x as (+)(2x)
end
if ex isa Number
ex = Expr(:call, :+, ex)
end
@assert isexpr(ex, :call)
@assert Meta.isexpr(ex, :call)
if ex.args[1] != :+
error(
"Unsupported operator in `loadfromstring!`: `$(ex.args[1])`. " *
Expand All @@ -139,7 +138,7 @@ function _parse_function(ex, ::Type{T} = Float64) where {T}
quadratic_terms = _ParsedScalarQuadraticTerm{T}[]
constant = zero(T)
for subex in ex.args[2:end]
if isexpr(subex, :call) && subex.args[1] == :*
if Meta.isexpr(subex, :call) && subex.args[1] == :*
if length(subex.args) == 3
# constant * variable
coef = if isa(subex.args[2], Number)
Expand Down Expand Up @@ -199,19 +198,19 @@ end

# see tests for examples
function _separate_label(ex)
if isexpr(ex, :call) && ex.args[1] == :(:)
if Meta.isexpr(ex, :call) && ex.args[1] == :(:)
# A line like `variables: x`.
return ex.args[2], ex.args[3]
elseif isexpr(ex, :tuple)
elseif Meta.isexpr(ex, :tuple)
# A line like `variables: x, y`. _Parsed as `((variables:x), y)`
ex = copy(ex)
@assert isexpr(ex.args[1], :call) && ex.args[1].args[1] == :(:)
@assert Meta.isexpr(ex.args[1], :call) && ex.args[1].args[1] == :(:)
label = ex.args[1].args[2]
ex.args[1] = ex.args[1].args[3]
return label, ex
elseif isexpr(ex, :call)
elseif Meta.isexpr(ex, :call)
ex = copy(ex)
if isexpr(ex.args[2], :call) && ex.args[2].args[1] == :(:)
if Meta.isexpr(ex.args[2], :call) && ex.args[2].args[1] == :(:)
# A line like `c: x <= 1`
label = ex.args[2].args[2]
ex.args[2] = ex.args[2].args[3]
Expand Down Expand Up @@ -241,9 +240,9 @@ _parsed_to_moi(model, s::Vector) = _parsed_to_moi.(model, s)
_parsed_to_moi(model, s::Number) = s

function _parsed_to_moi(model, s::Expr)
if isexpr(s, :call, 2) && s.args[1] == :ScalarNonlinearFunction
if Meta.isexpr(s, :call, 2) && s.args[1] == :ScalarNonlinearFunction
return _parsed_scalar_to_moi(model, s.args[2])
elseif isexpr(s, :call, 2) && s.args[1] == :VectorNonlinearFunction
elseif Meta.isexpr(s, :call, 2) && s.args[1] == :VectorNonlinearFunction
return _parsed_vector_to_moi(model, s.args[2])
end
args = Any[_parsed_to_moi(model, arg) for arg in s.args[2:end]]
Expand Down Expand Up @@ -378,7 +377,7 @@ function loadfromstring!(model, s)
label, ex = _separate_label(line)
T, label = _split_type(label)
if label == :variables
if isexpr(ex, :tuple)
if Meta.isexpr(ex, :tuple)
for v in ex.args
vindex = MOI.add_variable(model)
MOI.set(model, MOI.VariableName(), vindex, String(v))
Expand Down Expand Up @@ -416,7 +415,7 @@ function loadfromstring!(model, s)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
else
# constraint
@assert isexpr(ex, :call)
@assert Meta.isexpr(ex, :call)
f = _parsed_to_moi(model, _parse_function(ex.args[2], T))
if ex.args[1] == :in
# Could be safer here
Expand Down Expand Up @@ -446,10 +445,10 @@ end
_split_type(ex) = Float64, ex

function _split_type(ex::Expr)
if isexpr(ex, Symbol("::"), 1)
if Meta.isexpr(ex, Symbol("::"), 1)
return Core.eval(Base, ex.args[1]), Symbol("")
else
@assert isexpr(ex, Symbol("::"), 2)
@assert Meta.isexpr(ex, Symbol("::"), 2)
return Core.eval(Base, ex.args[2]), ex.args[1]
end
end
2 changes: 0 additions & 2 deletions src/Utilities/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

using Printf

_drop_moi(s) = replace(string(s), "MathOptInterface." => "")

_escape_braces(s) = replace(replace(s, "{" => "\\{"), "}" => "\\}")
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/promote_operation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ end
function promote_operation(
::typeof(*),
::Type{T},
::Type{<:Diagonal{T}},
::Type{<:LinearAlgebra.Diagonal{T}},
::Type{F},
) where {T,F}
U = promote_operation(*, T, T, scalar_type(F))
Expand Down