bfgs_minimizer_test.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
3 #include <test/test-models/good/optimization/rosenbrock.hpp>
4 #include <sstream>
5 
6 typedef rosenbrock_model_namespace::rosenbrock_model Model;
9 
10 class OptimizationBfgsMinimizer : public testing::Test {
11 public:
12  Eigen::Matrix<double,Eigen::Dynamic,1> cont_vector;
13  std::vector<int> disc_vector;
14 
15  void SetUp() {
16  cont_vector.resize(2);
17  cont_vector[0] = -1; cont_vector[1] = 1;
18 
19  }
20 };
21 
22 
24  static const std::string DATA("");
25  std::stringstream data_stream(DATA);
26  stan::io::dump dummy_context(data_stream);
27  Model rb_model(dummy_context);
28  std::stringstream out;
29  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
30  EXPECT_EQ("", out.str());
31  Optimizer bfgs(_adaptor);
32 }
33 
35  static const std::string DATA("");
36  std::stringstream data_stream(DATA);
37  stan::io::dump dummy_context(data_stream);
38  Model rb_model(dummy_context);
39  std::stringstream out;
40  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
41  EXPECT_EQ("", out.str());
42  Optimizer bfgs(_adaptor);
43 
44  EXPECT_FLOAT_EQ(bfgs._ls_opts.c1, 1e-4);
45  EXPECT_FLOAT_EQ(bfgs._ls_opts.c2, 0.9);
46  EXPECT_FLOAT_EQ(bfgs._ls_opts.minAlpha, 1e-12);
47  EXPECT_FLOAT_EQ(bfgs._ls_opts.alpha0, 1e-3);
48 }
49 
51  static const std::string DATA("");
52  std::stringstream data_stream(DATA);
53  stan::io::dump dummy_context(data_stream);
54  Model rb_model(dummy_context);
55  std::stringstream out;
56  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
57  EXPECT_EQ("", out.str());
58  Optimizer bfgs(_adaptor);
59 
60  EXPECT_FLOAT_EQ(bfgs._conv_opts.maxIts, 10000);
61  EXPECT_FLOAT_EQ(bfgs._conv_opts.fScale, 1);
62  EXPECT_FLOAT_EQ(bfgs._conv_opts.tolAbsX, 1e-8);
63  EXPECT_FLOAT_EQ(bfgs._conv_opts.tolAbsF, 1e-12);
64  EXPECT_FLOAT_EQ(bfgs._conv_opts.tolAbsGrad, 1e-8);
65  EXPECT_FLOAT_EQ(bfgs._conv_opts.tolRelF, 1e+4);
66  EXPECT_FLOAT_EQ(bfgs._conv_opts.tolRelGrad, 1e+3);
67 }
68 
70  static const std::string DATA("");
71  std::stringstream data_stream(DATA);
72  stan::io::dump dummy_context(data_stream);
73  Model rb_model(dummy_context);
74  std::stringstream out;
75  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
76  EXPECT_EQ("", out.str());
77  Optimizer bfgs(_adaptor);
78  bfgs.initialize(cont_vector);
79 
80  bfgs.get_qnupdate();
81 }
82 
84  static const std::string DATA("");
85  std::stringstream data_stream(DATA);
86  stan::io::dump dummy_context(data_stream);
87  Model rb_model(dummy_context);
88  std::stringstream out;
89  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
90  EXPECT_EQ("", out.str());
91  Optimizer bfgs(_adaptor);
92  bfgs.initialize(cont_vector);
93 
94  EXPECT_FLOAT_EQ(bfgs.curr_f(), 4);
95 }
96 
98  static const std::string DATA("");
99  std::stringstream data_stream(DATA);
100  stan::io::dump dummy_context(data_stream);
101  Model rb_model(dummy_context);
102  std::stringstream out;
103  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
104  EXPECT_EQ("", out.str());
105  Optimizer bfgs(_adaptor);
106  bfgs.initialize(cont_vector);
107 
108  EXPECT_FLOAT_EQ(bfgs.curr_x()[0], -1);
109  EXPECT_FLOAT_EQ(bfgs.curr_x()[1], 1);
110 }
111 
113  static const std::string DATA("");
114  std::stringstream data_stream(DATA);
115  stan::io::dump dummy_context(data_stream);
116  Model rb_model(dummy_context);
117  std::stringstream out;
118  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
119  EXPECT_EQ("", out.str());
120  Optimizer bfgs(_adaptor);
121  bfgs.initialize(cont_vector);
122 
123  EXPECT_FLOAT_EQ(bfgs.curr_g()[0], -4);
124  EXPECT_FLOAT_EQ(bfgs.curr_g()[1], 0);
125 }
126 
128  static const std::string DATA("");
129  std::stringstream data_stream(DATA);
130  stan::io::dump dummy_context(data_stream);
131  Model rb_model(dummy_context);
132  std::stringstream out;
133  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
134  EXPECT_EQ("", out.str());
135  Optimizer bfgs(_adaptor);
136  bfgs.initialize(cont_vector);
137 
138  EXPECT_FLOAT_EQ(bfgs.curr_p()[0], 4);
139  EXPECT_FLOAT_EQ(bfgs.curr_p()[1], 0);
140 }
141 
143  static const std::string DATA("");
144  std::stringstream data_stream(DATA);
145  stan::io::dump dummy_context(data_stream);
146  Model rb_model(dummy_context);
147  std::stringstream out;
148  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
149  EXPECT_EQ("", out.str());
150  Optimizer bfgs(_adaptor);
151  bfgs.initialize(cont_vector);
152 
153  bfgs.step();
154  EXPECT_FLOAT_EQ(bfgs.prev_f(), 4);
155 }
156 
158  static const std::string DATA("");
159  std::stringstream data_stream(DATA);
160  stan::io::dump dummy_context(data_stream);
161  Model rb_model(dummy_context);
162  std::stringstream out;
163  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
164  EXPECT_EQ("", out.str());
165  Optimizer bfgs(_adaptor);
166  bfgs.initialize(cont_vector);
167 
168  bfgs.step();
169  EXPECT_FLOAT_EQ(bfgs.prev_x()[0], -1);
170  EXPECT_FLOAT_EQ(bfgs.prev_x()[1], 1);
171 }
172 
174  static const std::string DATA("");
175  std::stringstream data_stream(DATA);
176  stan::io::dump dummy_context(data_stream);
177  Model rb_model(dummy_context);
178  std::stringstream out;
179  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
180  EXPECT_EQ("", out.str());
181  Optimizer bfgs(_adaptor);
182  bfgs.initialize(cont_vector);
183 
184  bfgs.step();
185  EXPECT_FLOAT_EQ(bfgs.prev_g()[0], -4);
186  EXPECT_FLOAT_EQ(bfgs.prev_g()[1], 0);
187 }
188 
190  static const std::string DATA("");
191  std::stringstream data_stream(DATA);
192  stan::io::dump dummy_context(data_stream);
193  Model rb_model(dummy_context);
194  std::stringstream out;
195  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
196  EXPECT_EQ("", out.str());
197  Optimizer bfgs(_adaptor);
198  bfgs.initialize(cont_vector);
199 
200  bfgs.step();
201  EXPECT_FLOAT_EQ(bfgs.prev_p()[0], 0.0040116129);
202  EXPECT_FLOAT_EQ(bfgs.prev_p()[1], 0);
203 }
204 
206  static const std::string DATA("");
207  std::stringstream data_stream(DATA);
208  stan::io::dump dummy_context(data_stream);
209  Model rb_model(dummy_context);
210  std::stringstream out;
211  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
212  EXPECT_EQ("", out.str());
213  Optimizer bfgs(_adaptor);
214  bfgs.initialize(cont_vector);
215 
216  bfgs.step();
217  EXPECT_FLOAT_EQ(bfgs.prev_step_size(), 0.0040000002);
218 }
219 
221  static const std::string DATA("");
222  std::stringstream data_stream(DATA);
223  stan::io::dump dummy_context(data_stream);
224  Model rb_model(dummy_context);
225  std::stringstream out;
226  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
227  EXPECT_EQ("", out.str());
228  Optimizer bfgs(_adaptor);
229  bfgs.initialize(cont_vector);
230 
231  bfgs.step();
232  EXPECT_FLOAT_EQ(bfgs.rel_grad_norm(), 0.0012151747);
233 }
234 
235 TEST_F(OptimizationBfgsMinimizer, rel_obj_decrease) {
236  static const std::string DATA("");
237  std::stringstream data_stream(DATA);
238  stan::io::dump dummy_context(data_stream);
239  Model rb_model(dummy_context);
240  std::stringstream out;
241  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
242  EXPECT_EQ("", out.str());
243  Optimizer bfgs(_adaptor);
244  bfgs.initialize(cont_vector);
245 
246  bfgs.step();
247  EXPECT_FLOAT_EQ(bfgs.rel_obj_decrease(), 0.0024023936);
248 }
249 
251  static const std::string DATA("");
252  std::stringstream data_stream(DATA);
253  stan::io::dump dummy_context(data_stream);
254  Model rb_model(dummy_context);
255  std::stringstream out;
256  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
257  EXPECT_EQ("", out.str());
258  Optimizer bfgs(_adaptor);
259  bfgs.initialize(cont_vector);
260 
261  bfgs.step();
262  EXPECT_FLOAT_EQ(bfgs.alpha0(), 0.001);
263 }
264 
266  static const std::string DATA("");
267  std::stringstream data_stream(DATA);
268  stan::io::dump dummy_context(data_stream);
269  Model rb_model(dummy_context);
270  std::stringstream out;
271  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
272  EXPECT_EQ("", out.str());
273  Optimizer bfgs(_adaptor);
274  bfgs.initialize(cont_vector);
275 
276  bfgs.step();
277  EXPECT_FLOAT_EQ(bfgs.alpha(), 0.001);
278 }
279 
281  static const std::string DATA("");
282  std::stringstream data_stream(DATA);
283  stan::io::dump dummy_context(data_stream);
284  Model rb_model(dummy_context);
285  std::stringstream out;
286  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
287  EXPECT_EQ("", out.str());
288  Optimizer bfgs(_adaptor);
289  bfgs.initialize(cont_vector);
290 
291  EXPECT_FLOAT_EQ(bfgs.iter_num(), 0);
292  bfgs.step();
293  EXPECT_FLOAT_EQ(bfgs.iter_num(), 1);
294 }
295 
297  static const std::string DATA("");
298  std::stringstream data_stream(DATA);
299  stan::io::dump dummy_context(data_stream);
300  Model rb_model(dummy_context);
301  std::stringstream out;
302  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
303  EXPECT_EQ("", out.str());
304  Optimizer bfgs(_adaptor);
305  bfgs.initialize(cont_vector);
306 
307  bfgs.step();
308  EXPECT_TRUE(bfgs.note() == "");
309 }
310 
312  static const std::string DATA("");
313  std::stringstream data_stream(DATA);
314  stan::io::dump dummy_context(data_stream);
315  Model rb_model(dummy_context);
316  std::stringstream out;
317  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
318  EXPECT_EQ("", out.str());
319  Optimizer bfgs(_adaptor);
320  bfgs.initialize(cont_vector);
321 
322  EXPECT_TRUE(bfgs.get_code_string(0) == "Successful step completed");
323  EXPECT_TRUE(bfgs.get_code_string(10) == "Convergence detected: absolute parameter change was below tolerance");
324  EXPECT_TRUE(bfgs.get_code_string(20) == "Convergence detected: absolute change in objective function was below tolerance");
325  EXPECT_TRUE(bfgs.get_code_string(21) == "Convergence detected: relative change in objective function was below tolerance");
326  EXPECT_TRUE(bfgs.get_code_string(30) == "Convergence detected: gradient norm is below tolerance");
327  EXPECT_TRUE(bfgs.get_code_string(31) == "Convergence detected: relative gradient magnitude is below tolerance");
328  EXPECT_TRUE(bfgs.get_code_string(40) == "Maximum number of iterations hit, may not be at an optima");
329  EXPECT_TRUE(bfgs.get_code_string(-1) == "Line search failed to achieve a sufficient decrease, no more progress can be made");
330  EXPECT_TRUE(bfgs.get_code_string(42) == "Unknown termination code");
331  EXPECT_TRUE(bfgs.get_code_string(32) == "Unknown termination code");
332  EXPECT_TRUE(bfgs.get_code_string(23) == "Unknown termination code");
333  EXPECT_TRUE(bfgs.get_code_string(94) == "Unknown termination code");
334 }
335 
337  static const std::string DATA("");
338  std::stringstream data_stream(DATA);
339  stan::io::dump dummy_context(data_stream);
340  Model rb_model(dummy_context);
341  std::stringstream out;
342  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
343  EXPECT_EQ("", out.str());
344  Optimizer bfgs(_adaptor);
345 
346  EXPECT_FLOAT_EQ(bfgs.curr_x().size(), 0);
347  EXPECT_FLOAT_EQ(bfgs.curr_p().size(), 0);
348  EXPECT_FLOAT_EQ(bfgs.curr_g().size(), 0);
349 
350  bfgs.initialize(cont_vector);
351  EXPECT_FLOAT_EQ(bfgs.curr_x().size(), 2);
352  EXPECT_FLOAT_EQ(bfgs.curr_x()[0], -1);
353  EXPECT_FLOAT_EQ(bfgs.curr_x()[1], 1);
354  EXPECT_FLOAT_EQ(bfgs.curr_p().size(), 2);
355  EXPECT_FLOAT_EQ(bfgs.curr_p()[0], 4);
356  EXPECT_FLOAT_EQ(bfgs.curr_p()[1], 0);
357  EXPECT_FLOAT_EQ(bfgs.curr_g().size(), 2);
358  EXPECT_FLOAT_EQ(bfgs.curr_g()[0], -4);
359  EXPECT_FLOAT_EQ(bfgs.curr_g()[1], 0);
360  EXPECT_FLOAT_EQ(bfgs.iter_num(), 0);
361  EXPECT_TRUE(bfgs.note() == "");
362 }
363 
365  static const std::string DATA("");
366  std::stringstream data_stream(DATA);
367  stan::io::dump dummy_context(data_stream);
368  Model rb_model(dummy_context);
369  std::stringstream out;
370  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
371  EXPECT_EQ("", out.str());
372  Optimizer bfgs(_adaptor);
373  bfgs.initialize(cont_vector);
374  bfgs.step();
375 }
376 
378  static const std::string DATA("");
379  std::stringstream data_stream(DATA);
380  stan::io::dump dummy_context(data_stream);
381  Model rb_model(dummy_context);
382  std::stringstream out;
383  stan::optimization::ModelAdaptor<Model> _adaptor(rb_model, disc_vector, &out);
384  EXPECT_EQ("", out.str());
385  Optimizer bfgs(_adaptor);
386 
387  EXPECT_FLOAT_EQ(bfgs.minimize(cont_vector), 31);
388 }
stan::optimization::BFGSMinimizer< stan::optimization::ModelAdaptor< Model >, stan::optimization::BFGSUpdate_HInv<> > Optimizer
TEST_F(OptimizationBfgsMinimizer, constructor)
void minimize()
rosenbrock_model_namespace::rosenbrock_model Model
std::vector< double > initialize(Model &model, stan::io::var_context &init, RNG &rng, double init_radius, bool print_timing, stan::callbacks::logger &logger, stan::callbacks::writer &init_writer)
Definition: initialize.hpp:68
Float_t e
Definition: plot.C:35
int bfgs(Model &model, stan::io::var_context &init, unsigned int random_seed, unsigned int chain, double init_radius, double init_alpha, double tol_obj, double tol_rel_obj, double tol_grad, double tol_rel_grad, double tol_param, int num_iterations, bool save_iterations, int refresh, callbacks::interrupt &interrupt, callbacks::logger &logger, callbacks::writer &init_writer, callbacks::writer &parameter_writer)
Definition: bfgs.hpp:54
Eigen::Matrix< double, Eigen::Dynamic, 1 > cont_vector
enum BeamMode string