-
Notifications
You must be signed in to change notification settings - Fork 214
/
api.jl
184 lines (159 loc) · 6.85 KB
/
api.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Test multivariate optimization
let
rosenbrock = Optim.UnconstrainedProblems.examples["Rosenbrock"]
f = rosenbrock.f
g! = rosenbrock.g!
h! = rosenbrock.h!
initial_x = rosenbrock.initial_x
d1 = DifferentiableFunction(f)
d2 = DifferentiableFunction(f, g!)
d3 = TwiceDifferentiableFunction(f, g!, h!)
Optim.optimize(f, initial_x, BFGS())
Optim.optimize(f, g!, initial_x, BFGS())
Optim.optimize(f, g!, h!, initial_x, BFGS())
Optim.optimize(d2, initial_x, BFGS())
Optim.optimize(d3, initial_x, BFGS())
Optim.optimize(f, initial_x, BFGS(), Optim.Options())
Optim.optimize(f, g!, initial_x, BFGS(), Optim.Options())
Optim.optimize(f, g!, h!, initial_x, BFGS(), Optim.Options())
Optim.optimize(d2, initial_x, BFGS(), Optim.Options())
Optim.optimize(d3, initial_x, BFGS(), Optim.Options())
Optim.optimize(d1, initial_x, BFGS())
Optim.optimize(d2, initial_x, BFGS())
Optim.optimize(d1, initial_x, GradientDescent())
Optim.optimize(d2, initial_x, GradientDescent())
Optim.optimize(d1, initial_x, LBFGS())
Optim.optimize(d2, initial_x, LBFGS())
Optim.optimize(f, initial_x, NelderMead())
Optim.optimize(d3, initial_x, Newton())
Optim.optimize(f, initial_x, SimulatedAnnealing())
optimize(f, initial_x, BFGS())
optimize(f, g!, initial_x, BFGS())
optimize(f, g!, h!, initial_x, BFGS())
optimize(f, initial_x, GradientDescent())
optimize(f, g!, initial_x, GradientDescent())
optimize(f, g!, h!, initial_x, GradientDescent())
optimize(f, initial_x, LBFGS())
optimize(f, g!, initial_x, LBFGS())
optimize(f, g!, h!, initial_x, LBFGS())
optimize(f, initial_x, NelderMead())
optimize(f, g!, initial_x, NelderMead())
optimize(f, g!, h!, initial_x, NelderMead())
optimize(f, g!, h!, initial_x, Newton())
optimize(f, initial_x, SimulatedAnnealing())
optimize(f, g!, initial_x, SimulatedAnnealing())
optimize(f, g!, h!, initial_x, SimulatedAnnealing())
options = Optim.Options(g_tol = 1e-12, iterations = 10,
store_trace = true, show_trace = false)
res = optimize(f, g!, h!,
initial_x,
BFGS(),
options)
options_g = Optim.Options(g_tol = 1e-12, iterations = 10,
store_trace = true, show_trace = false)
options_f = Optim.Options(g_tol = 1e-12, iterations = 10,
store_trace = true, show_trace = false)
res = optimize(f, g!, h!,
initial_x,
GradientDescent(),
options_g)
res = optimize(f, g!, h!,
initial_x,
LBFGS(),
options_g)
res = optimize(f, g!, h!,
initial_x,
NelderMead(),
options_f)
res = optimize(f, g!, h!,
initial_x,
Newton(),
options_g)
options_sa = Optim.Options(iterations = 10, store_trace = true,
show_trace = false)
res = optimize(f, g!, h!,
initial_x,
SimulatedAnnealing(),
options_sa)
res = optimize(f, g!, h!,
initial_x,
BFGS(),
options_g)
options_ext = Optim.Options(g_tol = 1e-12, iterations = 10,
store_trace = true, show_trace = false,
extended_trace = true)
res_ext = optimize(f, g!, h!,
initial_x,
BFGS(),
options_ext)
@test Optim.method(res) == "BFGS"
@test Optim.minimum(res) ≈ 0.055119582904897345
@test Optim.minimizer(res) ≈ [0.7731690866149542; 0.5917345966396391]
@test Optim.iterations(res) == 10
@test Optim.f_calls(res) == 48
@test Optim.g_calls(res) == 48
@test Optim.converged(res) == false
@test Optim.x_converged(res) == false
@test Optim.f_converged(res) == false
@test Optim.g_converged(res) == false
@test Optim.x_tol(res) == 1e-32
@test Optim.f_tol(res) == 1e-32
@test Optim.g_tol(res) == 1e-12
@test Optim.iteration_limit_reached(res) == true
@test Optim.initial_state(res) == [0.0; 0.0]
@test haskey(Optim.trace(res_ext)[1].metadata,"x")
# just testing if it runs
Optim.trace(res)
Optim.f_trace(res)
Optim.g_norm_trace(res)
@test_throws ErrorException Optim.x_trace(res)
@test_throws ErrorException Optim.x_lower_trace(res)
@test_throws ErrorException Optim.x_upper_trace(res)
@test_throws ErrorException Optim.lower_bound(res)
@test_throws ErrorException Optim.upper_bound(res)
@test_throws ErrorException Optim.rel_tol(res)
@test_throws ErrorException Optim.abs_tol(res)
options_extended = Optim.Options(store_trace = true, extended_trace = true)
res_extended = Optim.optimize(f, g!, initial_x, BFGS(), options_extended)
@test haskey(Optim.trace(res_extended)[1].metadata,"~inv(H)")
@test haskey(Optim.trace(res_extended)[1].metadata,"g(x)")
@test haskey(Optim.trace(res_extended)[1].metadata,"x")
options_extended_nm = Optim.Options(store_trace = true, extended_trace = true)
res_extended_nm = Optim.optimize(f, g!, initial_x, NelderMead(), options_extended_nm)
@test haskey(Optim.trace(res_extended_nm)[1].metadata,"centroid")
@test haskey(Optim.trace(res_extended_nm)[1].metadata,"step_type")
end
# Test univariate API
let
f(x) = 2x^2+3x+1
res = optimize(f, -2.0, 1.0, GoldenSection())
@test Optim.method(res) == "Golden Section Search"
@test Optim.minimum(res) ≈ -0.125
@test Optim.minimizer(res) ≈ -0.749999994377939
@test Optim.iterations(res) == 38
@test Optim.iteration_limit_reached(res) == false
@test_throws ErrorException Optim.trace(res)
@test_throws ErrorException Optim.x_trace(res)
@test_throws ErrorException Optim.x_lower_trace(res)
@test_throws ErrorException Optim.x_upper_trace(res)
@test_throws ErrorException Optim.f_trace(res)
@test Optim.lower_bound(res) == -2.0
@test Optim.upper_bound(res) == 1.0
@test Optim.rel_tol(res) ≈ 1.4901161193847656e-8
@test Optim.abs_tol(res) ≈ 2.220446049250313e-16
@test_throws ErrorException Optim.initial_state(res)
@test_throws ErrorException Optim.g_norm_trace(res)
@test_throws ErrorException Optim.g_calls(res)
@test_throws ErrorException Optim.x_converged(res)
@test_throws ErrorException Optim.f_converged(res)
@test_throws ErrorException Optim.g_converged(res)
@test_throws ErrorException Optim.x_tol(res)
@test_throws ErrorException Optim.f_tol(res)
@test_throws ErrorException Optim.g_tol(res)
options =
res = optimize(f, -2.0, 1.0, GoldenSection(), store_trace = true, extended_trace = true)
# Right now, these just "test" if they run
Optim.x_trace(res)
Optim.x_lower_trace(res)
Optim.x_upper_trace(res)
end