Skip to content

Commit d566d21

Browse files
committed
review changes to toint and trig problems
1 parent c626f5f commit d566d21

4 files changed

Lines changed: 88 additions & 19 deletions

File tree

src/ADNLPProblems/toint.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,32 @@ function toint(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) wher
55
s = zero(T)
66
for i 1:n
77
ci = 1 + (i // 10)
8-
js = max(1, i-2):min(n, i+2)
9-
if iseven(n)
10-
js = sort(collect(union(collect(js), [j for j in (i + n ÷ 2, i - n ÷ 2) if 1 <= j <= n])))
11-
end
12-
13-
for j js
8+
jmin = max(1, i - 2)
9+
jmax = min(n, i + 2)
10+
for j jmin:jmax
1411
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
1512
bij = (i + j) // 10
1613
cj = (1 + j) // 10
1714
s += aij * sin(bij + ci * x[i] + cj * x[j])
1815
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
1934
end
2035
return s / n
2136
end

src/ADNLPProblems/trig.jl

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,29 @@ function trig(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where
66
for i = 1:n
77
s += i * (1 - cos(x[i]))
88

9-
js = max(1, i - 2):min(n, i + 2)
10-
if iseven(n)
11-
js = sort(collect(union(collect(js), [j for j in (i + n ÷ 2, i - n ÷ 2) if 1 <= j <= n])))
12-
end
13-
for j in js
9+
jmin = max(1, i - 2)
10+
jmax = min(n, i + 2)
11+
for j in jmin:jmax
1412
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
1513
bij = (i + j) // 10
1614
s += aij * sin(x[j]) + bij * cos(x[j])
1715
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
1832
end
1933
return s / n
2034
end

src/PureJuMP/toint.jl

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,33 @@ function toint(args...; n::Int = default_nvar, kwargs...)
2121
(1 / n) * sum(begin
2222
ci = 1 + i / 10
2323
s = zero(Float64)
24-
js = max(1, i - 2):min(n, i + 2)
25-
if iseven(n)
26-
js = sort(collect(union(collect(js), [j for j in (i + n ÷ 2, i - n ÷ 2) if 1 <= j <= n])))
27-
end
28-
for j in js
24+
25+
jmin = max(1, i - 2)
26+
jmax = min(n, i + 2)
27+
for j in jmin:jmax
2928
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
3029
bij = (i + j) / 10
3130
cj = 1 + j / 10
3231
s += aij * sin(bij + ci * x[i] + cj * x[j])
3332
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
3451
s
3552
end for i = 1:n)
3653
)

src/PureJuMP/trig.jl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,32 @@ function trig(args...; n::Int = default_nvar, kwargs...)
2121
(1 / n) * sum(
2222
i * (1 - cos(x[i])) +
2323
(
24-
sum(5 * (1 + mod(i, 5) + mod(j, 5)) * sin(x[j]) + (i + j) / 10 * cos(x[j]) for j in (
25-
iseven(n) ? sort(collect(union(collect(max(1, i - 2):min(n, i + 2)), [j for j in (i + n ÷ 2, i - n ÷ 2) if 1 <= j <= n]))) : max(1, i - 2):min(n, i + 2)
26-
))
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
2750
) for i = 1:n
2851
)
2952
)

0 commit comments

Comments
 (0)