@@ -121,21 +121,21 @@ mutable struct Opt
121
121
finalizer (destroy, opt)
122
122
return opt
123
123
end
124
+ end
124
125
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" ))
134
129
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 " )
138
133
end
134
+ return Opt (p)
135
+ end
136
+
137
+ function Opt (algorithm:: Union{Integer,Symbol} , n:: Integer )
138
+ return Opt (Algorithm (algorithm), n)
139
139
end
140
140
141
141
Base. unsafe_convert (:: Type{Ptr{Cvoid}} , o:: Opt ) = getfield (o, :opt )
206
206
struct ForcedStop <: Exception end
207
207
208
208
# cache current exception for forced stop
209
- nlopt_exception = nothing
209
+ global nlopt_exception = nothing
210
210
211
211
function errmsg (o:: Opt )
212
212
msg = nlopt_get_errmsg (o)
@@ -347,19 +347,14 @@ end
347
347
xtol_abs! (o:: Opt , val:: Real ) = chk (o, nlopt_set_xtol_abs1 (o, val))
348
348
349
349
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))
352
351
end
353
352
354
- # the initial-stepsize stuff is a bit different than GETSET_VEC,
355
- # since the heuristics depend on the position x.
356
-
357
353
function default_initial_step! (o:: Opt , x:: Vector{Cdouble} )
358
354
if length (x) != ndims (o)
359
355
throw (BoundsError ())
360
356
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))
363
358
end
364
359
365
360
function default_initial_step! (o:: Opt , x:: AbstractVector{<:Real} )
@@ -370,25 +365,22 @@ function initial_step!(o::Opt, dx::Vector{Cdouble})
370
365
if length (dx) != ndims (o)
371
366
throw (BoundsError ())
372
367
end
373
- ret = nlopt_set_initial_step (o, dx)
374
- return chk (o, ret)
368
+ return chk (o, nlopt_set_initial_step (o, dx))
375
369
end
376
370
377
371
function initial_step! (o:: Opt , dx:: AbstractVector{<:Real} )
378
372
return initial_step! (o, Array {Cdouble} (dx))
379
373
end
380
374
381
375
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))
384
377
end
385
378
386
379
function initial_step (o:: Opt , x:: Vector{Cdouble} , dx:: Vector{Cdouble} )
387
380
if length (x) != ndims (o) || length (dx) != ndims (o)
388
381
throw (BoundsError ())
389
382
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))
392
384
return dx
393
385
end
394
386
@@ -436,29 +428,25 @@ srand_time() = nlopt_srand_time()
436
428
# ###########################################################################
437
429
# Objective function:
438
430
439
- const empty_grad = Cdouble[] # for passing when grad == C_NULL
440
-
441
431
function nlopt_callback_wrapper (
442
432
n:: Cuint ,
443
- x :: Ptr{Cdouble} ,
444
- grad :: Ptr{Cdouble} ,
433
+ p_x :: Ptr{Cdouble} ,
434
+ p_grad :: Ptr{Cdouble} ,
445
435
d_:: Ptr{Cvoid} ,
446
- )
436
+ ):: Cdouble
447
437
d = unsafe_pointer_to_objref (d_):: Callback_Data
438
+ x = unsafe_wrap (Array, p_x, (n,))
439
+ grad = unsafe_wrap (Array, p_grad, (n,))
448
440
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)
454
442
catch e
455
443
if e isa ForcedStop
456
444
global nlopt_exception = e
457
445
else
458
446
global nlopt_exception = CapturedException (e, catch_backtrace ())
459
447
end
460
448
force_stop! (d. o:: Opt )
461
- return 0.0 # ignored by nlopt
449
+ return NaN
462
450
end
463
451
end
464
452
@@ -470,8 +458,7 @@ function min_objective!(o::Opt, f::Function)
470
458
Cdouble,
471
459
(Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid})
472
460
)
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))
475
462
end
476
463
477
464
function max_objective! (o:: Opt , f:: Function )
@@ -482,8 +469,7 @@ function max_objective!(o::Opt, f::Function)
482
469
Cdouble,
483
470
(Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid})
484
471
)
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))
487
473
end
488
474
489
475
# ###########################################################################
@@ -497,8 +483,7 @@ function inequality_constraint!(o::Opt, f::Function, tol::Real = 0.0)
497
483
Cdouble,
498
484
(Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid})
499
485
)
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))
502
487
end
503
488
504
489
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)
509
494
Cdouble,
510
495
(Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid})
511
496
)
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))
514
498
end
515
499
516
500
function remove_constraints! (o:: Opt )
517
501
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
523
505
end
524
506
525
507
# ###########################################################################
526
508
# Vector-valued constraints
527
509
528
- const empty_jac = Array {Cdouble} (undef, 0 , 0 ) # for passing when grad == C_NULL
529
-
530
510
function nlopt_vcallback_wrapper (
531
511
m:: Cuint ,
532
- res :: Ptr{Cdouble} ,
512
+ p_res :: Ptr{Cdouble} ,
533
513
n:: Cuint ,
534
- x :: Ptr{Cdouble} ,
535
- grad :: Ptr{Cdouble} ,
514
+ p_x :: Ptr{Cdouble} ,
515
+ p_grad :: Ptr{Cdouble} ,
536
516
d_:: Ptr{Cvoid} ,
537
517
)
538
518
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))
539
524
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)
546
526
catch e
547
527
if e isa ForcedStop
548
528
global nlopt_exception = e
@@ -551,7 +531,7 @@ function nlopt_vcallback_wrapper(
551
531
end
552
532
force_stop! (d. o:: Opt )
553
533
end
554
- return nothing
534
+ return
555
535
end
556
536
557
537
function inequality_constraint! (o:: Opt , f:: Function , tol:: Vector{Cdouble} )
@@ -562,8 +542,7 @@ function inequality_constraint!(o::Opt, f::Function, tol::Vector{Cdouble})
562
542
Cvoid,
563
543
(Cuint, Ptr{Cdouble}, Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid}),
564
544
)
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)
567
546
return chk (o, ret)
568
547
end
569
548
@@ -592,8 +571,7 @@ function equality_constraint!(o::Opt, f::Function, tol::Vector{Cdouble})
592
571
Cvoid,
593
572
(Cuint, Ptr{Cdouble}, Cuint, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cvoid}),
594
573
)
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))
597
575
end
598
576
599
577
function equality_constraint! (o:: Opt , f:: Function , tol:: AbstractVector{<:Real} )
611
589
OptParams <: AbstractDict{String, Float64}
612
590
613
591
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.
616
595
"""
617
596
struct OptParams <: AbstractDict{String,Float64}
618
597
o:: Opt
@@ -622,13 +601,13 @@ Base.length(p::OptParams)::Int = nlopt_num_params(p.o)
622
601
623
602
Base. haskey (p:: OptParams , s:: AbstractString ):: Bool = nlopt_has_param (p. o, s)
624
603
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 )
627
606
end
628
607
629
- function Base. get (p:: OptParams , s:: AbstractString , defaultval )
608
+ function Base. get (p:: OptParams , s:: AbstractString , default )
630
609
if ! haskey (p, s)
631
- return defaultval
610
+ return default
632
611
end
633
612
return nlopt_get_param (p. o, s, NaN )
634
613
end
@@ -643,13 +622,13 @@ function Base.setindex!(p::OptParams, v::Algorithm, s::AbstractString)
643
622
end
644
623
645
624
function Base. iterate (p:: OptParams , state = 0 )
646
- if state ≥ length (p)
625
+ if state >= length (p)
647
626
return nothing
648
627
end
649
628
name_ptr = nlopt_nth_param (p. o, state)
650
629
@assert name_ptr != C_NULL
651
630
name = unsafe_string (name_ptr)
652
- return ( name => p[name], state + 1 )
631
+ return name => p[name], state + 1
653
632
end
654
633
655
634
# ###########################################################################
@@ -780,7 +759,7 @@ function optimize!(o::Opt, x::Vector{Cdouble})
780
759
if length (x) != ndims (o)
781
760
throw (BoundsError ())
782
761
end
783
- opt_f = Array {Cdouble} (undef, 1 )
762
+ opt_f = Ref {Cdouble} (NaN )
784
763
ret:: Result = nlopt_optimize (o, x, opt_f)
785
764
# We do not need to check the value of `ret`, except if it is a FORCED_STOP
786
765
# with a Julia-related exception from a callback
@@ -792,7 +771,7 @@ function optimize!(o::Opt, x::Vector{Cdouble})
792
771
throw (e)
793
772
end
794
773
end
795
- return opt_f[1 ], x, Symbol (ret)
774
+ return opt_f[], x, Symbol (ret)
796
775
end
797
776
798
777
function optimize (o:: Opt , x:: AbstractVector{<:Real} )
0 commit comments