Skip to content

Commit 7cffe7d

Browse files
committed
feat(trigb): add banded trigonometric problem (Meta, ADNLP, PureJuMP)
1 parent 5c33ede commit 7cffe7d

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/ADNLPProblems/trigb.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export trigb
2+
3+
function trigb(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
4+
# Band-limited trigonometric objective
5+
# F(x) = sum_{i=1}^n i * [ (1 - cos(x_i)) + sin(x_{i-1}) - sin(x_{i+1}) ]
6+
# with boundary values x_0 = x_{n+1} = 0
7+
8+
function f(x; n = length(x))
9+
n_local = n
10+
s = zero(T)
11+
for i in 1:n_local
12+
xi = x[i]
13+
left = (i == 1) ? zero(T) : x[i - 1]
14+
right = (i == n_local) ? zero(T) : x[i + 1]
15+
term = T(i) * ( (one(T) - cos(xi)) + sin(left) - sin(right) )
16+
s += term
17+
end
18+
return s
19+
end
20+
21+
# default initial guess: xbar_i = 1 (use type-generic one(T))
22+
x0 = fill(one(T), n)
23+
return ADNLPModels.ADNLPModel(f, x0, name = "trigb"; kwargs...)
24+
end

src/Meta/trigb.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
trigb_meta = Dict(
2+
:nvar => 100,
3+
:variable_nvar => true,
4+
:ncon => 0,
5+
:variable_ncon => false,
6+
:minimize => true,
7+
:name => "trigb",
8+
:has_equalities_only => false,
9+
:has_inequalities_only => false,
10+
:has_bounds => false,
11+
:has_fixed_variables => false,
12+
:objtype => :other,
13+
:contype => :unconstrained,
14+
:best_known_lower_bound => -Inf,
15+
:best_known_upper_bound => 0.0,
16+
:is_feasible => true,
17+
:defined_everywhere => missing,
18+
:origin => :unknown,
19+
)
20+
21+
get_trigb_nvar(; n::Integer = default_nvar, kwargs...) = n
22+
get_trigb_ncon(; n::Integer = default_nvar, kwargs...) = 0
23+
get_trigb_nlin(; n::Integer = default_nvar, kwargs...) = 0
24+
get_trigb_nnln(; n::Integer = default_nvar, kwargs...) = 0
25+
get_trigb_nequ(; n::Integer = default_nvar, kwargs...) = 0
26+
get_trigb_nineq(; n::Integer = default_nvar, kwargs...) = 0
27+
get_trigb_nls_nequ(; n::Integer = default_nvar, kwargs...) = 2 * n

src/PureJuMP/trigb.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Banded trigonometric problem (Problem 16)
2+
## F(x) = sum_{i=1}^n i * [ (1 - cos(x_i)) + sin(x_{i-1}) - sin(x_{i+1}) ]
3+
## boundary: x_0 = x_{n+1} = 0
4+
5+
export trigb
6+
7+
function trigb(args...; n::Int = default_nvar, kwargs...)
8+
model = Model()
9+
@variable(model, x[i = 1:n], start = 1.0)
10+
11+
@objective(model, Min,
12+
sum(
13+
i * (
14+
(1 - cos(x[i])) +
15+
((i == 1) ? sin(0.0) : sin(x[i - 1])) -
16+
((i == n) ? sin(0.0) : sin(x[i + 1]))
17+
) for i = 1:n
18+
)
19+
)
20+
21+
return model
22+
end

0 commit comments

Comments
 (0)