-
Notifications
You must be signed in to change notification settings - Fork 0
/
Powells_example.jl
90 lines (69 loc) · 1.55 KB
/
Powells_example.jl
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using ForwardDiff
using LaTeXStrings
using StatsPlots
using Plots
pyplot()
let
"""Function"""
# Objective
f(x::Vector) = -(x[1]*x[2] + x[2]*x[3] + x[1]*x[3])
Ω(x::Vector) = sum((abs.(x) .- 1).^2)
h(x::Vector) = f(x) + Ω(x)
# Minimization step
function g(x::Vector, i::Integer)
if i == 1
# Closed-form solution
g_i = (1 + abs(x[2] + x[3])/2)*sign(x[2] + x[3])
elseif i == 2
# Closed form solution
g_i = (1 + abs(x[1] + x[3])/2)*sign(x[1] + x[3])
elseif i == 3
# Closed form solution
g_i = (1 + abs(x[1] + x[2])/2)*sign(x[1] + x[2])
end
return g_i
end
"""Coordinate Descent"""
# Number of iterations
K = 2
# Fix step size
α = 1.0
# Initialize coordinates
ϵ = 1e-3
x = [-1. - ϵ, 1. + ϵ/2, -1. - ϵ/4]
# Preallocate result array
global xx = zeros(K*3,3)
# Initalize visualization
scatter([x[1]], [x[2]], [x[3]],
color="red",
markerstrokecolor=nothing,
label="x^0",
xlims=(-1.5, 1.5),
ylims=(-1.5, 1.5),
zlims=(-1.5, 1.5),
xlabel=L"x_1",
ylabel=L"x_2",
zlabel=L"x_3")
# Visualize stationary points
scatter!([+1], [+1], [+1], color="green", label="min", markerstrokecolor=nothing)
scatter!([-1], [-1], [-1], color="green", label="", markerstrokecolor=nothing)
# Loop over iterations
anim = @animate for k = 1:K*3
# Keep track of x
println(x)
xx[k, :] = x
# Update index
i = (k-1) % 3 + 1
println(i)
# Update coordinate
x[i] = g(x, i)
# Visualization
plot!([xx[k,1], x[1]],
[xx[k,2], x[2]],
[xx[k,3], x[3]],
color="blue",
label="",
markerstrokecolor=nothing)
end
gif(anim, "powells.gif", fps=2)
end