lbfgs_test.cpp
Go to the documentation of this file.
2 #include <gtest/gtest.h>
4 #include <test/test-models/good/optimization/rosenbrock.hpp>
7 
9  int n;
10  mock_callback() : n(0) { }
11 
12  void operator()() {
13  n++;
14  }
15 };
16 
17 
18 class values
20 public:
21  std::vector<std::string> names_;
22  std::vector<std::vector<double> > states_;
23 
24  values(std::ostream& stream)
25  : stan::callbacks::stream_writer(stream) {
26  }
27 
28  /**
29  * Writes a set of names.
30  *
31  * @param[in] names Names in a std::vector
32  */
33  void operator()(const std::vector<std::string>& names) {
34  names_ = names;
35  }
36 
37  /**
38  * Writes a set of values.
39  *
40  * @param[in] state Values in a std::vector
41  */
42  void operator()(const std::vector<double>& state) {
43  states_.push_back(state);
44  }
45 
46 };
47 
48 
49 class ServicesOptimizeLbfgs : public testing::Test {
50 public:
52  : init(init_ss),
53  parameter(parameter_ss),
54  model(context, &model_ss) {}
55 
56  std::stringstream init_ss, parameter_ss, model_ss;
61  stan_model model;
62 };
63 
64 
66  unsigned int seed = 0;
67  unsigned int chain = 1;
68  double init_radius = 0;
69 
70  bool save_iterations = true;
71  int refresh = 0;
72  mock_callback callback;
73 
75  seed, chain, init_radius,
76  5,
77  0.001,
78  1e-12,
79  10000,
80  1e-8,
81  10000000,
82  1e-8,
83  2000,
84  save_iterations, refresh,
85  callback,
86  logger,
87  init,
88  parameter);
89 
90  EXPECT_EQ(logger.call_count(), logger.call_count_info()) << "all output to info";
91  EXPECT_EQ(1, logger.find("Initial log joint probability = -1"));
92  EXPECT_EQ(1, logger.find("Optimization terminated normally: "));
93  EXPECT_EQ(1, logger.find(" Convergence detected: relative gradient magnitude is below tolerance"));
94 
95  EXPECT_EQ("0,0\n", init_ss.str());
96 
97  ASSERT_EQ(3, parameter.names_.size());
98  EXPECT_EQ("lp__", parameter.names_[0]);
99  EXPECT_EQ("x", parameter.names_[1]);
100  EXPECT_EQ("y", parameter.names_[2]);
101 
102  EXPECT_EQ(23, parameter.states_.size());
103  EXPECT_FLOAT_EQ(0, parameter.states_.front()[1])
104  << "initial value should be (0, 0)";
105  EXPECT_FLOAT_EQ(0, parameter.states_.front()[2])
106  << "initial value should be (0, 0)";
107  EXPECT_FLOAT_EQ(0.99998301, parameter.states_.back()[1])
108  << "optimal value should be (1, 1)";
109  EXPECT_FLOAT_EQ(0.99996597, parameter.states_.back()[2])
110  << "optimal value should be (1, 1)";
111  EXPECT_FLOAT_EQ(return_code, 0);
112  EXPECT_EQ(22, callback.n);
113 }
void operator()(const std::vector< double > &state)
Definition: lbfgs_test.cpp:42
void operator()()
Definition: lbfgs_test.cpp:12
stan::test::unit::instrumented_logger logger
Definition: lbfgs_test.cpp:58
unsigned int seed
Definition: runWimpSim.h:102
TEST_F(ServicesOptimizeLbfgs, rosenbrock)
Definition: lbfgs_test.cpp:65
def callbacks(model_name, group, tensorboard=True)
Definition: regression.py:123
chain
Check that an output directory exists.
stan::io::empty_var_context context
Definition: lbfgs_test.cpp:60
std::stringstream parameter_ss
Definition: lbfgs_test.cpp:56
values(std::ostream &stream)
Definition: lbfgs_test.cpp:24
const XML_Char * context
Definition: expat.h:434
stan::callbacks::stream_writer init
Definition: lbfgs_test.cpp:57
Float_t e
Definition: plot.C:35
void operator()(const std::vector< std::string > &names)
Definition: lbfgs_test.cpp:33
const XML_Char XML_Content * model
Definition: expat.h:151
int lbfgs(Model &model, stan::io::var_context &init, unsigned int random_seed, unsigned int chain, double init_radius, int history_size, 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: lbfgs.hpp:55
void refresh()
Definition: show_event.C:21