Skip to content

Commit 66ee350

Browse files
authored
Miscellaneous style improvements (#225)
1 parent 55fdd9f commit 66ee350

File tree

1 file changed

+55
-76
lines changed

1 file changed

+55
-76
lines changed

src/NLopt.jl

Lines changed: 55 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,21 @@ mutable struct Opt
121121
finalizer(destroy, opt)
122122
return opt
123123
end
124+
end
124125

125-
function Opt(algorithm::Algorithm, n::Integer)
126-
if n < 0
127-
throw(ArgumentError("invalid dimension $n < 0"))
128-
end
129-
p = nlopt_create(algorithm, n)
130-
if p == C_NULL
131-
error("Error in nlopt_create")
132-
end
133-
return Opt(p)
126+
function Opt(algorithm::Algorithm, n::Integer)
127+
if n < 0
128+
throw(ArgumentError("invalid dimension $n < 0"))
134129
end
135-
136-
function Opt(algorithm::Union{Integer,Symbol}, n::Integer)
137-
return Opt(Algorithm(algorithm), n)
130+
p = nlopt_create(algorithm, n)
131+
if p == C_NULL
132+
error("Error in nlopt_create")
138133
end
134+
return Opt(p)
135+
end
136+
137+
function Opt(algorithm::Union{Integer,Symbol}, n::Integer)
138+
return Opt(Algorithm(algorithm), n)
139139
end
140140

141141
Base.unsafe_convert(::Type{Ptr{Cvoid}}, o::Opt) = getfield(o, :opt)
@@ -206,7 +206,7 @@ end
206206
struct ForcedStop <: Exception end
207207

208208
# cache current exception for forced stop
209-
nlopt_exception = nothing
209+
global nlopt_exception = nothing
210210

211211
function errmsg(o::Opt)
212212
msg = nlopt_get_errmsg(o)
@@ -347,19 +347,14 @@ end
347347
xtol_abs!(o::Opt, val::Real) = chk(o, nlopt_set_xtol_abs1(o, val))
348348

349349
function local_optimizer!(o::Opt, lo::Opt)
350-
ret = nlopt_set_local_optimizer(o, lo)
351-
return chk(o, ret)
350+
return chk(o, nlopt_set_local_optimizer(o, lo))
352351
end
353352

354-
# the initial-stepsize stuff is a bit different than GETSET_VEC,
355-
# since the heuristics depend on the position x.
356-
357353
function default_initial_step!(o::Opt, x::Vector{Cdouble})
358354
if length(x) != ndims(o)
359355
throw(BoundsError())
360356
end
361-
ret = nlopt_set_default_initial_step(o, x)
362-
return chk(o, ret)
357+
return chk(o, nlopt_set_default_initial_step(o, x))
363358
end
364359

365360
function default_initial_step!(o::Opt, x::AbstractVector{<:Real})
@@ -370,25 +365,22 @@ function initial_step!(o::Opt, dx::Vector{Cdouble})
370365
if length(dx) != ndims(o)
371366
throw(BoundsError())
372367
end
373-
ret = nlopt_set_initial_step(o, dx)
374-
return chk(o, ret)
368+
return chk(o, nlopt_set_initial_step(o, dx))
375369
end
376370

377371
function initial_step!(o::Opt, dx::AbstractVector{<:Real})
378372
return initial_step!(o, Array{Cdouble}(dx))
379373
end
380374

381375
function initial_step!(o::Opt, dx::Real)
382-
ret = nlopt_set_initial_step1(o, dx)
383-
return chk(o, ret)
376+
return chk(o, nlopt_set_initial_step1(o, dx))
384377
end
385378

386379
function initial_step(o::Opt, x::Vector{Cdouble}, dx::Vector{Cdouble})
387380
if length(x) != ndims(o) || length(dx) != ndims(o)
388381
throw(BoundsError())
389382
end
390-
ret::Result = nlopt_get_initial_step(o, x, dx)
391-
chk(o, ret)
383+
chk(o, nlopt_get_initial_step(o, x, dx))
392384
return dx
393385
end
394386

@@ -436,29 +428,25 @@ srand_time() = nlopt_srand_time()
436428
############################################################################
437429
# Objective function:
438430

439-
const empty_grad = Cdouble[] # for passing when grad == C_NULL
440-
441431
function nlopt_callback_wrapper(
442432
n::Cuint,
443-
x::Ptr{Cdouble},
444-
grad::Ptr{Cdouble},
433+
p_x::Ptr{Cdouble},
434+
p_grad::Ptr{Cdouble},
445435
d_::Ptr{Cvoid},
446-
)
436+
)::Cdouble
447437
d = unsafe_pointer_to_objref(d_)::Callback_Data
438+
x = unsafe_wrap(Array, p_x, (n,))
439+
grad = unsafe_wrap(Array, p_grad, (n,))
448440
try
449-
x_vec = unsafe_wrap(Array, x, (convert(Int, n),))
450-
grad_vec = unsafe_wrap(Array, grad, (convert(Int, n),))
451-
res =
452-
convert(Cdouble, d.f(x_vec, grad == C_NULL ? empty_grad : grad_vec))
453-
return res::Cdouble
441+
return d.f(x, p_grad == C_NULL ? Cdouble[] : grad)
454442
catch e
455443
if e isa ForcedStop
456444
global nlopt_exception = e
457445
else
458446
global nlopt_exception = CapturedException(e, catch_backtrace())
459447
end
460448
force_stop!(d.o::Opt)
461-
return 0.0 # ignored by nlopt
449+
return NaN
462450
end
463451
end
464452

@@ -470,8 +458,7 @@ function min_objective!(o::Opt, f::Function)
470458
Cdouble,
471459
(Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid})
472460
)
473-
ret = nlopt_set_min_objective(o, c_fn, cb)
474-
return chk(o, ret)
461+
return chk(o, nlopt_set_min_objective(o, c_fn, cb))
475462
end
476463

477464
function max_objective!(o::Opt, f::Function)
@@ -482,8 +469,7 @@ function max_objective!(o::Opt, f::Function)
482469
Cdouble,
483470
(Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid})
484471
)
485-
ret = nlopt_set_max_objective(o, c_fn, cb)
486-
return chk(o, ret)
472+
return chk(o, nlopt_set_max_objective(o, c_fn, cb))
487473
end
488474

489475
############################################################################
@@ -497,8 +483,7 @@ function inequality_constraint!(o::Opt, f::Function, tol::Real = 0.0)
497483
Cdouble,
498484
(Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid})
499485
)
500-
ret::Result = nlopt_add_inequality_constraint(o, c_fn, cb, tol)
501-
return chk(o, ret)
486+
return chk(o, nlopt_add_inequality_constraint(o, c_fn, cb, tol))
502487
end
503488

504489
function equality_constraint!(o::Opt, f::Function, tol::Real = 0.0)
@@ -509,40 +494,35 @@ function equality_constraint!(o::Opt, f::Function, tol::Real = 0.0)
509494
Cdouble,
510495
(Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid})
511496
)
512-
ret::Result = nlopt_add_equality_constraint(o, c_fn, cb, tol)
513-
return chk(o, ret)
497+
return chk(o, nlopt_add_equality_constraint(o, c_fn, cb, tol))
514498
end
515499

516500
function remove_constraints!(o::Opt)
517501
resize!(getfield(o, :cb), 1)
518-
ret = nlopt_remove_inequality_constraints(o)
519-
chk(o, ret)
520-
# TODO(odow): why is this called twice?
521-
ret = nlopt_remove_equality_constraints(o)
522-
return chk(o, ret)
502+
chk(o, nlopt_remove_inequality_constraints(o))
503+
chk(o, nlopt_remove_equality_constraints(o))
504+
return
523505
end
524506

525507
############################################################################
526508
# Vector-valued constraints
527509

528-
const empty_jac = Array{Cdouble}(undef, 0, 0) # for passing when grad == C_NULL
529-
530510
function nlopt_vcallback_wrapper(
531511
m::Cuint,
532-
res::Ptr{Cdouble},
512+
p_res::Ptr{Cdouble},
533513
n::Cuint,
534-
x::Ptr{Cdouble},
535-
grad::Ptr{Cdouble},
514+
p_x::Ptr{Cdouble},
515+
p_grad::Ptr{Cdouble},
536516
d_::Ptr{Cvoid},
537517
)
538518
d = unsafe_pointer_to_objref(d_)::Callback_Data
519+
res = unsafe_wrap(Array, p_res, (m,))
520+
x = unsafe_wrap(Array, p_x, (n,))
521+
grad =
522+
p_grad == C_NULL ? zeros(Cdouble, 0, 0) :
523+
unsafe_wrap(Array, p_grad, (n, m))
539524
try
540-
d.f(
541-
unsafe_wrap(Array, res, (convert(Int, m),)),
542-
unsafe_wrap(Array, x, (convert(Int, n),)),
543-
grad == C_NULL ? empty_jac :
544-
unsafe_wrap(Array, grad, (convert(Int, n), convert(Int, m))),
545-
)
525+
d.f(res, x, grad)
546526
catch e
547527
if e isa ForcedStop
548528
global nlopt_exception = e
@@ -551,7 +531,7 @@ function nlopt_vcallback_wrapper(
551531
end
552532
force_stop!(d.o::Opt)
553533
end
554-
return nothing
534+
return
555535
end
556536

557537
function inequality_constraint!(o::Opt, f::Function, tol::Vector{Cdouble})
@@ -562,8 +542,7 @@ function inequality_constraint!(o::Opt, f::Function, tol::Vector{Cdouble})
562542
Cvoid,
563543
(Cuint, Ptr{Cdouble}, Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid}),
564544
)
565-
ret::Result =
566-
nlopt_add_inequality_mconstraint(o, length(tol), c_fn, cb, tol)
545+
ret = nlopt_add_inequality_mconstraint(o, length(tol), c_fn, cb, tol)
567546
return chk(o, ret)
568547
end
569548

@@ -592,8 +571,7 @@ function equality_constraint!(o::Opt, f::Function, tol::Vector{Cdouble})
592571
Cvoid,
593572
(Cuint, Ptr{Cdouble}, Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid}),
594573
)
595-
ret::Result = nlopt_add_equality_mconstraint(o, length(tol), c_fn, cb, tol)
596-
return chk(o, ret)
574+
return chk(o, nlopt_add_equality_mconstraint(o, length(tol), c_fn, cb, tol))
597575
end
598576

599577
function equality_constraint!(o::Opt, f::Function, tol::AbstractVector{<:Real})
@@ -611,8 +589,9 @@ end
611589
OptParams <: AbstractDict{String, Float64}
612590
613591
Dictionary-like structure for accessing algorithm-specific parameters for
614-
an NLopt optimization object `opt`, returned by `opt.params`. Use this
615-
object to both set and view these string-keyed numeric parameters.
592+
an NLopt optimization object `opt`, returned by `opt.params`.
593+
594+
Use this object to both set and view these string-keyed numeric parameters.
616595
"""
617596
struct OptParams <: AbstractDict{String,Float64}
618597
o::Opt
@@ -622,13 +601,13 @@ Base.length(p::OptParams)::Int = nlopt_num_params(p.o)
622601

623602
Base.haskey(p::OptParams, s::AbstractString)::Bool = nlopt_has_param(p.o, s)
624603

625-
function Base.get(p::OptParams, s::AbstractString, defaultval::Float64)
626-
return nlopt_get_param(p.o, s, defaultval)
604+
function Base.get(p::OptParams, s::AbstractString, default::Float64)
605+
return nlopt_get_param(p.o, s, default)
627606
end
628607

629-
function Base.get(p::OptParams, s::AbstractString, defaultval)
608+
function Base.get(p::OptParams, s::AbstractString, default)
630609
if !haskey(p, s)
631-
return defaultval
610+
return default
632611
end
633612
return nlopt_get_param(p.o, s, NaN)
634613
end
@@ -643,13 +622,13 @@ function Base.setindex!(p::OptParams, v::Algorithm, s::AbstractString)
643622
end
644623

645624
function Base.iterate(p::OptParams, state = 0)
646-
if state length(p)
625+
if state >= length(p)
647626
return nothing
648627
end
649628
name_ptr = nlopt_nth_param(p.o, state)
650629
@assert name_ptr != C_NULL
651630
name = unsafe_string(name_ptr)
652-
return (name => p[name], state + 1)
631+
return name => p[name], state + 1
653632
end
654633

655634
############################################################################
@@ -780,7 +759,7 @@ function optimize!(o::Opt, x::Vector{Cdouble})
780759
if length(x) != ndims(o)
781760
throw(BoundsError())
782761
end
783-
opt_f = Array{Cdouble}(undef, 1)
762+
opt_f = Ref{Cdouble}(NaN)
784763
ret::Result = nlopt_optimize(o, x, opt_f)
785764
# We do not need to check the value of `ret`, except if it is a FORCED_STOP
786765
# with a Julia-related exception from a callback
@@ -792,7 +771,7 @@ function optimize!(o::Opt, x::Vector{Cdouble})
792771
throw(e)
793772
end
794773
end
795-
return opt_f[1], x, Symbol(ret)
774+
return opt_f[], x, Symbol(ret)
796775
end
797776

798777
function optimize(o::Opt, x::AbstractVector{<:Real})

0 commit comments

Comments
 (0)