-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathpowellsg.jl
More file actions
45 lines (42 loc) · 1.41 KB
/
powellsg.jl
File metadata and controls
45 lines (42 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
export powellsg
function powellsg(; use_nls::Bool = false, kwargs...)
model = use_nls ? :nls : :nlp
return powellsg(Val(model); kwargs...)
end
function powellsg(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
n_orig = n
n = 4 * max(1, div(n, 4))
@adjust_nvar_warn("powellsg", n_orig, n)
function f(x; n = length(x))
return sum(
(x[j] + 10 * x[j + 1])^2 +
5 * (x[j + 2] - x[j + 3])^2 +
(x[j + 1] - 2 * x[j + 2])^4 +
10 * (x[j] - x[j + 3])^4 for j = 1:4:n
)
end
x0 = zeros(T, n)
x0[4 * (collect(1:div(n, 4))) .- 3] .= 3
x0[4 * (collect(1:div(n, 4))) .- 2] .= -1
x0[4 * (collect(1:div(n, 4)))] .= 1
return ADNLPModels.ADNLPModel(f, x0, name = "powellsg"; kwargs...)
end
function powellsg(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
n_orig = n
n = 4 * max(1, div(n, 4))
@adjust_nvar_warn("powellsg", n_orig, n)
function F!(r, x; n = length(x))
@inbounds for j = 1:4:n
r[j] = x[j] + 10 * x[j + 1]
r[j + 1] = T(sqrt(5)) * (x[j + 2] - x[j + 3])
r[j + 2] = (x[j + 1] - 2 * x[j + 2])^2
r[j + 3] = T(sqrt(10)) * (x[j] - x[j + 3])^2
end
return r
end
x0 = zeros(T, n)
x0[4 * (collect(1:div(n, 4))) .- 3] .= 3
x0[4 * (collect(1:div(n, 4))) .- 2] .= -1
x0[4 * (collect(1:div(n, 4)))] .= 1
return ADNLPModels.ADNLSModel!(F!, x0, n, name = "powellsg-nls"; kwargs...)
end