bfgs_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 ServicesOptimizeBfgs : 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  0.001,
77  1e-12,
78  10000,
79  1e-8,
80  10000000,
81  1e-8,
82  2000,
83  save_iterations, refresh,
84  callback,
85  logger,
86  init,
87  parameter);
88 
89  EXPECT_EQ(logger.call_count(), logger.call_count_info()) << "all output to info";
90  EXPECT_EQ(1, logger.find("Initial log joint probability = -1"));
91  EXPECT_EQ(1, logger.find("Optimization terminated normally: "));
92  EXPECT_EQ(1, logger.find(" Convergence detected: relative gradient magnitude is below tolerance"));
93 
94  EXPECT_EQ("0,0\n", init_ss.str());
95 
96  ASSERT_EQ(3, parameter.names_.size());
97  EXPECT_EQ("lp__", parameter.names_[0]);
98  EXPECT_EQ("x", parameter.names_[1]);
99  EXPECT_EQ("y", parameter.names_[2]);
100 
101  EXPECT_EQ(20, parameter.states_.size());
102  EXPECT_FLOAT_EQ(0, parameter.states_.front()[1])
103  << "initial value should be (0, 0)";
104  EXPECT_FLOAT_EQ(0, parameter.states_.front()[2])
105  << "initial value should be (0, 0)";
106  EXPECT_FLOAT_EQ(1, parameter.states_.back()[1])
107  << "optimal value should be (1, 1)";
108  EXPECT_FLOAT_EQ(1, parameter.states_.back()[2])
109  << "optimal value should be (1, 1)";
110  EXPECT_FLOAT_EQ(return_code, 0);
111  EXPECT_EQ(19, callback.n);
112 }
void operator()(const std::vector< double > &state)
Definition: bfgs_test.cpp:42
void operator()()
Definition: bfgs_test.cpp:12
std::stringstream parameter_ss
Definition: bfgs_test.cpp:56
stan::test::unit::instrumented_logger logger
Definition: bfgs_test.cpp:58
stan::callbacks::stream_writer init
Definition: bfgs_test.cpp:57
TEST_F(ServicesOptimizeBfgs, rosenbrock)
Definition: bfgs_test.cpp:65
unsigned int seed
Definition: runWimpSim.h:102
def callbacks(model_name, group, tensorboard=True)
Definition: regression.py:123
chain
Check that an output directory exists.
std::vector< std::vector< double > > states_
Definition: bfgs_test.cpp:22
values(std::ostream &stream)
Definition: bfgs_test.cpp:24
stan::io::empty_var_context context
Definition: bfgs_test.cpp:60
const XML_Char * context
Definition: expat.h:434
std::vector< std::string > names_
Definition: bfgs_test.cpp:21
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
void operator()(const std::vector< std::string > &names)
Definition: bfgs_test.cpp:33
const XML_Char XML_Content * model
Definition: expat.h:151
void refresh()
Definition: show_event.C:21