Skip to content

Commit f69fcad

Browse files
committed
Format using JuliaFormatter
1 parent a7ad606 commit f69fcad

15 files changed

+125
-124
lines changed

.JuliaFormatter.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
margin = 100
2+
indent = 2
3+
whitespace_typedefs = true
4+
whitespace_ops_in_indices = true
5+
remove_extra_newlines = true
6+
annotate_untyped_fields_with_any = false
7+
normalize_line_endings = "unix"

docs/make.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ makedocs(
55
doctest = true,
66
linkcheck = true,
77
strict = true,
8-
format = Documenter.HTML(assets = ["assets/style.css"], prettyurls = get(ENV, "CI", nothing) == "true"),
8+
format = Documenter.HTML(
9+
assets = ["assets/style.css"],
10+
prettyurls = get(ENV, "CI", nothing) == "true",
11+
),
912
sitename = "SolverCore.jl",
10-
pages = ["Home" => "index.md",
11-
"Reference" => "reference.md",
12-
]
13+
pages = ["Home" => "index.md", "Reference" => "reference.md"],
1314
)
1415

1516
deploydocs(
1617
repo = "github.com/JuliaSmoothOptimizers/SolverCore.jl.git",
1718
push_preview = true,
18-
devbranch = "master"
19+
devbranch = "master",
1920
)

solver-example.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ nlp = ADNLPModel(
88
[-1.2; 1.0],
99
x -> [x[1]^2 + x[2]^2 - 1],
1010
[0.0],
11-
[0.0]
11+
[0.0],
1212
)
1313
solver = DummySolver(Float16, nlp.meta)
1414
output = solve!(solver, nlp, δ = 1e-2)
@@ -26,6 +26,12 @@ output, solver = DummySolver(nlp)
2626
#%%
2727
include("test/dummy_solver.jl")
2828
problems = (
29-
ADNLPModel(x -> (x[1] - a)^2 + b^2 * (x[2] - x[1]^2)^2, [-1.2; 1.0], x -> [x[1]^2 + x[2]^2 - 1], [0.0], [0.0]) for a = 0:0.1:1, b = 2:3
29+
ADNLPModel(
30+
x -> (x[1] - a)^2 + b^2 * (x[2] - x[1]^2)^2,
31+
[-1.2; 1.0],
32+
x -> [x[1]^2 + x[2]^2 - 1],
33+
[0.0],
34+
[0.0],
35+
) for a = 0:0.1:1, b = 2:3
3036
)
31-
o = grid_search_tune(DummySolver, problems)
37+
o = grid_search_tune(DummySolver, problems)

src/SolverCore.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using OrderedCollections
77
include("solver.jl")
88
include("output.jl")
99

10+
include("logger.jl")
1011
include("parameters.jl")
1112
include("traits.jl")
1213

src/grid-search-tuning.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,14 @@ function grid_search_tune(
3434
::Type{Solver},
3535
problems;
3636
success = o -> o.status == :first_order,
37-
costs = [
38-
(o -> o.elapsed_time, 100.0),
39-
(o -> !success(o), 1),
40-
],
37+
costs = [(o -> o.elapsed_time, 100.0), (o -> !success(o), 1)],
4138
grid_length = 10,
4239
solver_kwargs = Dict(),
43-
kwargs...
44-
) where Solver <: AbstractSolver
45-
40+
kwargs...,
41+
) where {Solver <: AbstractSolver}
4642
solver_params = parameters(Solver)
4743
params = OrderedDict()
48-
for (k,v) in pairs(solver_params)
44+
for (k, v) in pairs(solver_params)
4945
if v[:type] <: AbstractFloat && (!haskey(v, :scale) || v[:scale] == :linear)
5046
params[k] = LinRange(v[:min], v[:max], grid_length)
5147
elseif v[:type] <: AbstractFloat && v[:scale] == :log
@@ -56,7 +52,7 @@ function grid_search_tune(
5652
params[k] = v[:min]:v[:max]
5753
end
5854
end
59-
for (k,v) in kwargs
55+
for (k, v) in kwargs
6056
params[k] = v
6157
end
6258

@@ -77,7 +73,7 @@ function grid_search_tune(
7773
reset!(problem)
7874
try
7975
solver = Solver(problem)
80-
P = (k => θi for (k,θi) in zip(keys(solver_params), θ))
76+
P = (k => θi for (k, θi) in zip(keys(solver_params), θ))
8177
output = with_logger(NullLogger()) do
8278
solve!(solver, problem; P...)
8379
end
@@ -101,4 +97,4 @@ function grid_search_tune(
10197
end
10298

10399
=> cost(θ) for θ in Iterators.product(values(params)...)]
104-
end
100+
end

src/logger.jl

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
export log_header, log_row
22

3-
const formats = Dict{DataType, String}(Signed => "%6d",
4-
AbstractFloat => "%8.1e",
5-
AbstractString => "%15s",
6-
Symbol => "%15s",
7-
Missing => "%15s"
8-
)
9-
10-
const default_headers = Dict{Symbol, String}(:name => "Name",
11-
:elapsed_time => "Time",
12-
:objective => "f(x)",
13-
:dual_feas => "Dual",
14-
:primal_feas => "Primal")
3+
const formats = Dict{DataType, String}(
4+
Signed => "%6d",
5+
AbstractFloat => "%8.1e",
6+
AbstractString => "%15s",
7+
Symbol => "%15s",
8+
Missing => "%15s",
9+
)
10+
11+
const default_headers = Dict{Symbol, String}(
12+
:name => "Name",
13+
:elapsed_time => "Time",
14+
:objective => "f(x)",
15+
:dual_feas => "Dual",
16+
:primal_feas => "Primal",
17+
)
1518

1619
for (typ, fmt) in formats
1720
hdr_fmt_foo = Symbol("header_formatter_$typ")
1821
len = match(r"\%([0-9]*)", fmt)[1]
1922
fmt2 = "%$(len)s"
2023

2124
@eval begin
22-
row_formatter(x :: $typ) = @sprintf($fmt, x)
23-
row_formatter(:: Type{<:$typ}) = @sprintf($fmt2, "-")
25+
row_formatter(x::$typ) = @sprintf($fmt, x)
26+
row_formatter(::Type{<:$typ}) = @sprintf($fmt2, "-")
2427

2528
$hdr_fmt_foo(x) = @sprintf($fmt2, x)
26-
header_formatter(x :: Union{Symbol,String}, :: Type{<:$typ}) = $hdr_fmt_foo(x)
29+
header_formatter(x::Union{Symbol, String}, ::Type{<:$typ}) = $hdr_fmt_foo(x)
2730
end
2831
end
2932

@@ -44,10 +47,12 @@ Keyword arguments:
4447
4548
See also [`log_row`](@ref).
4649
"""
47-
function log_header(colnames :: AbstractVector{Symbol}, coltypes :: AbstractVector{DataType};
48-
hdr_override :: Dict{Symbol,String} = Dict{Symbol,String}(),
49-
colsep :: Int = 2,
50-
)
50+
function log_header(
51+
colnames::AbstractVector{Symbol},
52+
coltypes::AbstractVector{DataType};
53+
hdr_override::Dict{Symbol, String} = Dict{Symbol, String}(),
54+
colsep::Int = 2,
55+
)
5156
out_vec = String[]
5257
for (name, typ) in zip(colnames, coltypes)
5358
x = if haskey(hdr_override, name)
@@ -59,7 +64,7 @@ function log_header(colnames :: AbstractVector{Symbol}, coltypes :: AbstractVect
5964
end
6065
push!(out_vec, header_formatter(x, typ))
6166
end
62-
return join(out_vec, " "^colsep)
67+
return join(out_vec, " "^colsep)
6368
end
6469

6570
"""
@@ -82,7 +87,7 @@ Prints
8287
Keyword arguments:
8388
- `colsep::Int`: Number of spaces between columns (Default: 2)
8489
"""
85-
function log_row(vals; colsep :: Int = 2)
90+
function log_row(vals; colsep::Int = 2)
8691
string_cols = (row_formatter(val) for val in vals)
8792
return join(string_cols, " "^colsep)
8893
end

src/output.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ abstract type AbstractSolverOutput{T} end
1313

1414
# TODO: Decision: Should STATUSES be fixed? Should it be all here?
1515
const STATUSES = Dict(
16-
:exception => "unhandled exception",
17-
:first_order => "first-order stationary",
18-
:acceptable => "solved to within acceptable tolerances",
19-
:infeasible => "problem may be infeasible",
20-
:max_eval => "maximum number of function evaluations",
21-
:max_iter => "maximum iteration",
22-
:max_time => "maximum elapsed time",
23-
:neg_pred => "negative predicted reduction",
24-
:not_desc => "not a descent direction",
16+
:exception => "unhandled exception",
17+
:first_order => "first-order stationary",
18+
:acceptable => "solved to within acceptable tolerances",
19+
:infeasible => "problem may be infeasible",
20+
:max_eval => "maximum number of function evaluations",
21+
:max_iter => "maximum iteration",
22+
:max_time => "maximum elapsed time",
23+
:neg_pred => "negative predicted reduction",
24+
:not_desc => "not a descent direction",
2525
:small_residual => "small residual",
26-
:small_step => "step too small",
27-
:stalled => "stalled",
28-
:unbounded => "objective function may be unbounded from below",
29-
:unknown => "unknown",
30-
:user => "user-requested stop",
26+
:small_step => "step too small",
27+
:stalled => "stalled",
28+
:unbounded => "objective function may be unbounded from below",
29+
:unknown => "unknown",
30+
:user => "user-requested stop",
3131
)
3232

33-
function Base.show(io :: IO, output :: AbstractSolverOutput)
33+
function Base.show(io::IO, output::AbstractSolverOutput)
3434
println(io, "Solver output of type $(typeof(output))")
3535
println(io, "Status: $(STATUSES[output.status])")
36-
end
36+
end

src/parameters.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Solvers should define
2727
"""
2828
function parameters end
2929

30-
parameters(::Type{S}) where S <: AbstractSolver = parameters(S{Float64})
31-
parameters(::S) where S <: AbstractSolver = parameters(S)
30+
parameters(::Type{S}) where {S <: AbstractSolver} = parameters(S{Float64})
31+
parameters(::S) where {S <: AbstractSolver} = parameters(S)
3232

3333
"""
3434
are_valid_parameters(solver, args...)
@@ -41,5 +41,6 @@ Solvers should define
4141
SolverCore.are_valid_parameters(::Type{Solver{T}}, arg1, arg2, ...) where T
4242
"""
4343
function are_valid_parameters end
44-
are_valid_parameters(::Type{S}, args...) where S <: AbstractSolver = are_valid_parameters(S{Float64}, args...)
45-
are_valid_parameters(::S, args...) where S <: AbstractSolver = are_valid_parameters(S, args...)
44+
are_valid_parameters(::Type{S}, args...) where {S <: AbstractSolver} =
45+
are_valid_parameters(S{Float64}, args...)
46+
are_valid_parameters(::S, args...) where {S <: AbstractSolver} = are_valid_parameters(S, args...)

src/solver.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ A solver must have three members:
1212
"""
1313
abstract type AbstractSolver{T} end
1414

15-
function Base.show(io :: IO, solver :: AbstractSolver)
15+
function Base.show(io::IO, solver::AbstractSolver)
1616
println(io, "Solver $(typeof(solver))")
1717
end
1818

@@ -23,4 +23,4 @@ Solve `problem` with `solver`.
2323
"""
2424
function solve! end
2525

26-
# TODO: Define general constructors that automatically call `solve!`, etc.?
26+
# TODO: Define general constructors that automatically call `solve!`, etc.?

src/traits.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ problem_types_handled(::Type{<:AbstractSolver}) = []
1414
Check if the `solver` can solve problems of `type`.
1515
Call `problem_types_handled` for a list of problem types that the `solver` can solve.
1616
"""
17-
function can_solve_type(::Type{S}, t :: Symbol) where S <: AbstractSolver
17+
function can_solve_type(::Type{S}, t::Symbol) where {S <: AbstractSolver}
1818
return t problem_types_handled(S)
1919
end

0 commit comments

Comments
 (0)