Skip to content

Commit fd771c8

Browse files
Merge pull request #920 from thimotedupuch/more_concise_functions
Improve code conciseness in file OptimizationNLopt.jl
2 parents 83328bc + 86f0f34 commit fd771c8

File tree

1 file changed

+16
-60
lines changed

1 file changed

+16
-60
lines changed

lib/OptimizationNLopt/src/OptimizationNLopt.jl

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,38 @@ using Optimization: deduce_retcode
1010
SciMLBase.allowsbounds(opt::Union{NLopt.Algorithm, NLopt.Opt}) = true
1111
SciMLBase.supports_opt_cache_interface(opt::Union{NLopt.Algorithm, NLopt.Opt}) = true
1212

13-
function SciMLBase.requiresgradient(opt::Union{NLopt.Algorithm, NLopt.Opt}) #https://github.com/JuliaOpt/NLopt.jl/blob/master/src/NLopt.jl#L18C7-L18C16
14-
str_opt = if opt isa NLopt.Algorithm
15-
string(opt)
16-
else
17-
string(opt.algorithm)
18-
end
19-
if str_opt[2] == 'N'
20-
return false
21-
else
22-
return true
23-
end
13+
function SciMLBase.requiresgradient(opt::Union{NLopt.Algorithm, NLopt.Opt})
14+
# https://github.com/JuliaOpt/NLopt.jl/blob/master/src/NLopt.jl#L18C7-L18C16
15+
str_opt = string(opt isa NLopt.Algorithm ? opt : opt.algorithm)
16+
return str_opt[2] != 'N'
2417
end
2518

2619
#interferes with callback handling
2720
# function SciMLBase.allowsfg(opt::Union{NLopt.Algorithm, NLopt.Opt})
28-
# str_opt = if opt isa NLopt.Algorithm
29-
# string(opt)
30-
# else
31-
# string(opt.algorithm)
32-
# end
33-
# if str_opt[2] == 'D'
34-
# return true
35-
# else
36-
# return false
37-
# end
21+
# str_opt = string(opt isa NLopt.Algorithm ? opt : opt.algorithm)
22+
# return str_opt[2] == 'D'
3823
# end
3924

40-
function SciMLBase.requireshessian(opt::Union{NLopt.Algorithm, NLopt.Opt}) #https://github.com/JuliaOpt/NLopt.jl/blob/master/src/NLopt.jl#L18C7-L18C16
41-
str_opt = if opt isa NLopt.Algorithm
42-
string(opt)
43-
else
44-
string(opt.algorithm)
45-
end
46-
47-
if str_opt[2] == 'N' || occursin("LD_LBFGS", str_opt) || occursin("LD_SLSQP", str_opt)
48-
return false
49-
else
50-
return true
51-
end
25+
function SciMLBase.requireshessian(opt::Union{NLopt.Algorithm, NLopt.Opt})
26+
# https://github.com/JuliaOpt/NLopt.jl/blob/master/src/NLopt.jl#L18C7-L18C16
27+
str_opt = string(opt isa NLopt.Algorithm ? opt : opt.algorithm)
28+
return !(str_opt[2] == 'N' || occursin(r"LD_LBFGS|LD_SLSQP", str_opt))
5229
end
5330

54-
function SciMLBase.requiresconsjac(opt::Union{NLopt.Algorithm, NLopt.Opt}) #https://github.com/JuliaOpt/NLopt.jl/blob/master/src/NLopt.jl#L18C7-L18C16
55-
str_opt = if opt isa NLopt.Algorithm
56-
string(opt)
57-
else
58-
string(opt.algorithm)
59-
end
60-
if str_opt[3] == 'O' || str_opt[3] == 'I' || str_opt[5] == 'G'
61-
return true
62-
else
63-
return false
64-
end
31+
function SciMLBase.requiresconsjac(opt::Union{NLopt.Algorithm, NLopt.Opt})
32+
# https://github.com/JuliaOpt/NLopt.jl/blob/master/src/NLopt.jl#L18C7-L18C16
33+
str_opt = string(opt isa NLopt.Algorithm ? opt : opt.algorithm)
34+
return str_opt[3] ['O', 'I'] || str_opt[5] == 'G'
6535
end
6636

6737
function SciMLBase.allowsconstraints(opt::NLopt.Algorithm)
6838
str_opt = string(opt)
69-
if occursin("AUGLAG", str_opt) || occursin("CCSA", str_opt) ||
70-
occursin("MMA", str_opt) || occursin("COBYLA", str_opt) ||
71-
occursin("ISRES", str_opt) || occursin("AGS", str_opt) ||
72-
occursin("ORIG_DIRECT", str_opt) || occursin("SLSQP", str_opt)
73-
return true
74-
else
75-
return false
76-
end
39+
return occursin(r"AUGLAG|CCSA|MMA|COBYLA|ISRES|AGS|ORIG_DIRECT|SLSQP", str_opt)
7740
end
7841

7942
function SciMLBase.requiresconsjac(opt::NLopt.Algorithm)
8043
str_opt = string(opt)
81-
if occursin("AUGLAG", str_opt) || occursin("CCSA", str_opt) ||
82-
occursin("MMA", str_opt) || occursin("COBYLA", str_opt) ||
83-
occursin("ISRES", str_opt) || occursin("AGS", str_opt) ||
84-
occursin("ORIG_DIRECT", str_opt) || occursin("SLSQP", str_opt)
85-
return true
86-
else
87-
return false
88-
end
44+
return occursin(r"AUGLAG|CCSA|MMA|COBYLA|ISRES|AGS|ORIG_DIRECT|SLSQP", str_opt)
8945
end
9046

9147
function SciMLBase.__init(prob::SciMLBase.OptimizationProblem, opt::NLopt.Algorithm,

0 commit comments

Comments
 (0)