-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathpenalty1.jl
More file actions
27 lines (24 loc) · 917 Bytes
/
penalty1.jl
File metadata and controls
27 lines (24 loc) · 917 Bytes
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
export penalty1
function penalty1(; use_nls::Bool = false, kwargs...)
model = use_nls ? :nls : :nlp
return penalty1(Val(model); kwargs...)
end
function penalty1(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
function f(x; n = length(x), a = eltype(x)(sqrt(1e-5)))
return 1 // 2 * sum((a * (x[i] - 1))^2 for i = 1:n) +
1 // 2 * (sum(x[j]^2 for j = 1:n) - 1 // 4)^2
end
x0 = T[j for j = 1:n]
return ADNLPModels.ADNLPModel(f, x0, name = "penalty1"; kwargs...)
end
function penalty1(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
function F!(r, x; n = length(x), a = eltype(x)(sqrt(1e-5)))
for i = 1:n
r[i] = a * (x[i] - 1)
end
r[n + 1] = sum(x[j]^2 for j = 1:n) - 1 // 4
return r
end
x0 = T[j for j = 1:n]
return ADNLPModels.ADNLSModel!(F!, x0, n + 1, name = "penalty1-nls"; kwargs...)
end