-
Notifications
You must be signed in to change notification settings - Fork 221
/
optimize.jl
57 lines (48 loc) · 1.87 KB
/
optimize.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
@testset "optimize" begin
eta = 0.9
function f1(x)
(1.0 / 2.0) * (x[1]^2 + eta * x[2]^2)
end
function g1(storage, x)
storage[1] = x[1]
storage[2] = eta * x[2]
end
function h1(storage, x)
storage[1, 1] = 1.0
storage[1, 2] = 0.0
storage[2, 1] = 0.0
storage[2, 2] = eta
end
results = optimize(f1, g1, h1, [127.0, 921.0])
@test Optim.g_converged(results)
@test norm(Optim.minimizer(results) - [0.0, 0.0]) < 0.01
results = optimize(f1, g1, [127.0, 921.0])
@test Optim.g_converged(results)
@test norm(Optim.minimizer(results) - [0.0, 0.0]) < 0.01
results = optimize(f1, [127.0, 921.0])
@test Optim.g_converged(results)
@test norm(Optim.minimizer(results) - [0.0, 0.0]) < 0.01
results = optimize(f1, [127.0, 921.0])
@test Optim.g_converged(results)
@test norm(Optim.minimizer(results) - [0.0, 0.0]) < 0.01
# tests for bfgs_initial_invH
initial_invH = zeros(2,2)
h1(initial_invH, [127.0, 921.0])
initial_invH = diagm(diag(initial_invH))
results = optimize(f1, g1, [127.0, 921.0], BFGS(initial_invH = x -> initial_invH), Optim.Options())
@test Optim.g_converged(results)
@test norm(Optim.minimizer(results) - [0.0, 0.0]) < 0.01
# Tests for PR #302
results = optimize(cos, 0, 2pi);
@test norm(Optim.minimizer(results) - pi) < 0.01
results = optimize(cos, 0.0, 2pi);
@test norm(Optim.minimizer(results) - pi) < 0.01
results = optimize(cos, 0, 2pi, Brent());
@test norm(Optim.minimizer(results) - pi) < 0.01
results = optimize(cos, 0.0, 2pi, Brent());
@test norm(Optim.minimizer(results) - pi) < 0.01
results = optimize(cos, 0, 2pi, method = Brent());
@test norm(Optim.minimizer(results) - pi) < 0.01
results = optimize(cos, 0.0, 2pi, method = Brent());
@test norm(Optim.minimizer(results) - pi) < 0.01
end