Skip to content

Commit 1cee776

Browse files
arnavk23tmigot
andauthored
Problem 9,10 and 16 (#396)
* Problem 9 and 10 * feat(trigb): add banded trigonometric problem (Meta, ADNLP, PureJuMP) * trigb.jl * Update trig.jl * Apply suggestions from code review Co-authored-by: Tangi Migot <tangi.migot@gmail.com> * removing unnecessary variables * fixing remaining mentions of xi variable * removing twice renditions * meta * PureJuMP * Update toint.jl * Update trig.jl * Update toint.jl * Update src/ADNLPProblems/toint.jl Co-authored-by: Tangi Migot <tangi.migot@gmail.com> * fixing Float64 issue in toint and trig problems * further changes * trig changes * review changes to toint and trig problems * fixing AD and JuMP inconsistency in toint.jl * fixing AD and JuMP incompatibility in toint.jl - 2 * Update toint.jl * Update toint.jl * Fix calculation in trigb.jl for sine terms * Apply suggestions from code review --------- Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
1 parent fe7e98f commit 1cee776

File tree

9 files changed

+312
-0
lines changed

9 files changed

+312
-0
lines changed

src/ADNLPProblems/toint.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export toint
2+
3+
function toint(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
4+
function f(x; n = length(x))
5+
s = zero(T)
6+
for i 1:n
7+
ci = 1 + (i // 10)
8+
jmin = max(1, i - 2)
9+
jmax = min(n, i + 2)
10+
for j jmin:jmax
11+
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
12+
bij = (i + j) // 10
13+
cj = 1 + j // 10
14+
s += aij * sin(bij + ci * x[i] + cj * x[j])
15+
end
16+
17+
if iseven(n)
18+
half = n ÷ 2
19+
j1 = i + half
20+
if 1 <= j1 <= n && (j1 < jmin || j1 > jmax)
21+
aij = 5 * (1 + mod(i, 5) + mod(j1, 5))
22+
bij = (i + j1) // 10
23+
cj = 1 + j1 // 10
24+
s += aij * sin(bij + ci * x[i] + cj * x[j1])
25+
end
26+
j2 = i - half
27+
if 1 <= j2 <= n && j2 != j1 && (j2 < jmin || j2 > jmax)
28+
aij = 5 * (1 + mod(i, 5) + mod(j2, 5))
29+
bij = (i + j2) // 10
30+
cj = 1 + j2 // 10
31+
s += aij * sin(bij + ci * x[i] + cj * x[j2])
32+
end
33+
end
34+
end
35+
return s / n
36+
end
37+
38+
x0 = fill(one(T), n)
39+
return ADNLPModels.ADNLPModel(f, x0, name = "toint"; kwargs...)
40+
end

src/ADNLPProblems/trig.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export trig
2+
3+
function trig(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
4+
function f(x; n = length(x))
5+
s = zero(T)
6+
for i = 1:n
7+
s += i * (1 - cos(x[i]))
8+
9+
jmin = max(1, i - 2)
10+
jmax = min(n, i + 2)
11+
for j in jmin:jmax
12+
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
13+
bij = (i + j) // 10
14+
s += aij * sin(x[j]) + bij * cos(x[j])
15+
end
16+
17+
if iseven(n)
18+
half = n ÷ 2
19+
j1 = i + half
20+
if 1 <= j1 <= n && (j1 < jmin || j1 > jmax)
21+
aij = 5 * (1 + mod(i, 5) + mod(j1, 5))
22+
bij = (i + j1) // 10
23+
s += aij * sin(x[j1]) + bij * cos(x[j1])
24+
end
25+
j2 = i - half
26+
if 1 <= j2 <= n && j2 != j1 && (j2 < jmin || j2 > jmax)
27+
aij = 5 * (1 + mod(i, 5) + mod(j2, 5))
28+
bij = (i + j2) // 10
29+
s += aij * sin(x[j2]) + bij * cos(x[j2])
30+
end
31+
end
32+
end
33+
return s / n
34+
end
35+
36+
x0 = fill(one(T) / n, n)
37+
return ADNLPModels.ADNLPModel(f, x0, name = "trig"; kwargs...)
38+
end

src/ADNLPProblems/trigb.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export trigb
2+
3+
function trigb(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
4+
function f(x; n = length(x))
5+
s = zero(T)
6+
for i = 1:n
7+
xim = (i == 1) ? zero(T) : x[i - 1]
8+
xip = (i == n) ? zero(T) : x[i + 1]
9+
s += i * (1 - cos(x[i]) + sin(xim) - sin(xip))
10+
end
11+
return s
12+
end
13+
14+
x0 = fill(one(T), n)
15+
return ADNLPModels.ADNLPModel(f, x0, name = "trigb"; kwargs...)
16+
end

src/Meta/toint.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
toint_meta = Dict(
2+
:nvar => 100,
3+
:variable_nvar => true,
4+
:ncon => 0,
5+
:variable_ncon => false,
6+
:minimize => true,
7+
:name => "toint",
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.8598396932382264,
16+
:is_feasible => true,
17+
:defined_everywhere => missing,
18+
:origin => :unknown,
19+
)
20+
21+
get_toint_nvar(; n::Integer = default_nvar, kwargs...) = n
22+
get_toint_ncon(; n::Integer = default_nvar, kwargs...) = 0
23+
get_toint_nlin(; n::Integer = default_nvar, kwargs...) = 0
24+
get_toint_nnln(; n::Integer = default_nvar, kwargs...) = 0
25+
get_toint_nequ(; n::Integer = default_nvar, kwargs...) = 0
26+
get_toint_nineq(; n::Integer = default_nvar, kwargs...) = 0

src/Meta/trig.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
trig_meta = Dict(
2+
:nvar => 100,
3+
:variable_nvar => true,
4+
:ncon => 0,
5+
:variable_ncon => false,
6+
:minimize => true,
7+
:name => "trig",
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 => 61.482500487413226,
16+
:is_feasible => true,
17+
:defined_everywhere => missing,
18+
:origin => :unknown,
19+
)
20+
21+
get_trig_nvar(; n::Integer = default_nvar, kwargs...) = n
22+
get_trig_ncon(; n::Integer = default_nvar, kwargs...) = 0
23+
get_trig_nlin(; n::Integer = default_nvar, kwargs...) = 0
24+
get_trig_nnln(; n::Integer = default_nvar, kwargs...) = 0
25+
get_trig_nequ(; n::Integer = default_nvar, kwargs...) = 0
26+
get_trig_nineq(; n::Integer = default_nvar, kwargs...) = 0

src/Meta/trigb.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 => 2404.778982861876,
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

src/PureJuMP/toint.jl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Toint trigonometric function
2+
#
3+
# Problem 10 in
4+
# L. Luksan, C. Matonoha and J. Vlcek
5+
# Sparse Test Problems for Unconstrained Optimization,
6+
# Technical Report 1064,
7+
# Institute of Computer Science,
8+
# Academy of Science of the Czech Republic
9+
#
10+
# https://www.researchgate.net/publication/325314400_Sparse_Test_Problems_for_Unconstrained_Optimization
11+
#
12+
export toint
13+
14+
function toint(args...; n::Int = default_nvar, kwargs...)
15+
model = Model()
16+
@variable(model, x[i = 1:n], start = 1)
17+
18+
@objective(
19+
model,
20+
Min,
21+
(1 / n) * sum(begin
22+
ci = 1 + (i // 10)
23+
s = zero(Float64)
24+
25+
jmin = max(1, i - 2)
26+
jmax = min(n, i + 2)
27+
for j in jmin:jmax
28+
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
29+
bij = (i + j) // 10
30+
cj = 1 + j // 10
31+
s += aij * sin(bij + ci * x[i] + cj * x[j])
32+
end
33+
34+
if iseven(n)
35+
half = n ÷ 2
36+
j1 = i + half
37+
if 1 <= j1 <= n && (j1 < jmin || j1 > jmax)
38+
aij = 5 * (1 + mod(i, 5) + mod(j1, 5))
39+
bij = (i + j1) // 10
40+
cj = 1 + j1 // 10
41+
s += aij * sin(bij + ci * x[i] + cj * x[j1])
42+
end
43+
j2 = i - half
44+
if 1 <= j2 <= n && j2 != j1 && (j2 < jmin || j2 > jmax)
45+
aij = 5 * (1 + mod(i, 5) + mod(j2, 5))
46+
bij = (i + j2) // 10
47+
cj = 1 + j2 // 10
48+
s += aij * sin(bij + ci * x[i] + cj * x[j2])
49+
end
50+
end
51+
s
52+
end for i = 1:n)
53+
)
54+
55+
return model
56+
end

src/PureJuMP/trig.jl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Another trigonometric function
2+
#
3+
# Problem 9 in
4+
# L. Luksan, C. Matonoha and J. Vlcek
5+
# Sparse Test Problems for Unconstrained Optimization,
6+
# Technical Report 1064,
7+
# Institute of Computer Science,
8+
# Academy of Science of the Czech Republic
9+
#
10+
# https://www.researchgate.net/publication/325314400_Sparse_Test_Problems_for_Unconstrained_Optimization
11+
#
12+
export trig
13+
14+
function trig(args...; n::Int = default_nvar, kwargs...)
15+
model = Model()
16+
@variable(model, x[i = 1:n], start = 1 / n)
17+
18+
@objective(
19+
model,
20+
Min,
21+
(1 / n) * sum(
22+
i * (1 - cos(x[i])) +
23+
(
24+
begin
25+
jmin = max(1, i - 2)
26+
jmax = min(n, i + 2)
27+
s = zero(Float64)
28+
for j in jmin:jmax
29+
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
30+
bij = (i + j) / 10
31+
s += aij * sin(x[j]) + bij * cos(x[j])
32+
end
33+
if iseven(n)
34+
half = n ÷ 2
35+
j1 = i + half
36+
if 1 <= j1 <= n && (j1 < jmin || j1 > jmax)
37+
aij = 5 * (1 + mod(i, 5) + mod(j1, 5))
38+
bij = (i + j1) / 10
39+
s += aij * sin(x[j1]) + bij * cos(x[j1])
40+
end
41+
j2 = i - half
42+
if 1 <= j2 <= n && j2 != j1 && (j2 < jmin || j2 > jmax)
43+
aij = 5 * (1 + mod(i, 5) + mod(j2, 5))
44+
bij = (i + j2) / 10
45+
s += aij * sin(x[j2]) + bij * cos(x[j2])
46+
end
47+
end
48+
s
49+
end
50+
) for i = 1:n
51+
)
52+
)
53+
54+
return model
55+
end

src/PureJuMP/trigb.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Banded trigonometric problem
2+
#
3+
# Problem 16 in
4+
# L. Luksan, C. Matonoha and J. Vlcek
5+
# Sparse Test Problems for Unconstrained Optimization,
6+
# Technical Report 1064,
7+
# Institute of Computer Science,
8+
# Academy of Science of the Czech Republic
9+
#
10+
# https://www.researchgate.net/publication/325314400_Sparse_Test_Problems_for_Unconstrained_Optimization
11+
#
12+
export trigb
13+
14+
function trigb(args...; n::Int = default_nvar, kwargs...)
15+
model = Model()
16+
@variable(model, x[i = 1:n], start = 1)
17+
18+
@objective(
19+
model,
20+
Min,
21+
sum(
22+
i *
23+
((1 - cos(x[i])) + ((i == 1) ? sin(0) : sin(x[i - 1])) - ((i == n) ? 0 : sin(x[i + 1])))
24+
for i = 1:n
25+
)
26+
)
27+
28+
return model
29+
end

0 commit comments

Comments
 (0)