Skip to content

Commit d1c06f9

Browse files
committed
easier implementation of the algorithm.
1 parent dc2916d commit d1c06f9

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/PureJuMP/nazareth.jl

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,34 @@ export nazareth
1212

1313
function nazareth(; n::Int = default_nvar, type::Type{T} = Float64) where {T}
1414
nh = div(n, 2)
15-
neighbors = Vector{Vector{Int}}(undef, n)
16-
a_row = Vector{Vector{T}}(undef, n)
17-
b_row = Vector{Vector{T}}(undef, n)
18-
for i in 1:n
19-
lo = max(1, i - 2)
20-
hi = min(n, i + 2)
21-
neigh = collect(lo:hi)
22-
j1 = i - nh
23-
j2 = i + nh
24-
for j in (j1, j2)
25-
if 1 <= j <= n && !(j >= lo && j <= hi)
26-
push!(neigh, j)
27-
end
28-
end
29-
neighbors[i] = neigh
30-
a_row[i] = [5 * (1 + mod(i, 5) + mod(j, 5)) for j in neigh]
31-
b_row[i] = [(i + j) / 10 for j in neigh]
32-
end
3315

3416
nlp = Model()
35-
@variable(nlp, x[1:n], start = one(T) / n)
17+
@variable(nlp, x[1:n], start = one(T) / T(n))
3618

3719
@objective(nlp, Min,
38-
sum(
39-
(n + i - sum(a_row[i][k] * sin(x[ neighbors[i][k] ]) + b_row[i][k] * cos(x[ neighbors[i][k] ]) for k in 1:length(neighbors[i])))^2
40-
for i = 1:n
41-
) / n
20+
sum((begin
21+
lo = max(1, i - 2)
22+
hi = min(n, i + 2)
23+
24+
sumtrig = 0
25+
for j in lo:hi
26+
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
27+
bij = (i + j) / 10
28+
sumtrig += aij * sin(x[j]) + bij * cos(x[j])
29+
end
30+
31+
j1 = i - nh
32+
j2 = i + nh
33+
for j in (j1, j2)
34+
if 1 <= j <= n && (j < lo || j > hi)
35+
aij = 5 * (1 + mod(i, 5) + mod(j, 5))
36+
bij = (i + j) / 10
37+
sumtrig += aij * sin(x[j]) + bij * cos(x[j])
38+
end
39+
end
40+
41+
(n + i - sumtrig)^2
42+
end) for i = 1:n) / T(n)
4243
)
4344
return nlp
4445
end

0 commit comments

Comments
 (0)