-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathoptimization_output_test.cpp
More file actions
189 lines (157 loc) · 6.16 KB
/
optimization_output_test.cpp
File metadata and controls
189 lines (157 loc) · 6.16 KB
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
185
186
187
188
189
#include <test/utility.hpp>
#include <stan/mcmc/chains.hpp>
#include <boost/algorithm/string.hpp>
#include <gtest/gtest.h>
#include <fstream>
using cmdstan::test::convert_model_path;
using cmdstan::test::idx_first_match;
using cmdstan::test::parse_sample;
using cmdstan::test::run_command;
using cmdstan::test::run_command_output;
// outimization_model - matrix of 4 normals
// [[1, 10000], [100, 1000000]] - column major:
// lp__, y.1.1, y.2.1, y.1.2, y.2.2
// 0, 1, 100, 10000, 1e+06
class CmdStan : public testing::Test {
public:
void SetUp() {
optimization_model = {"src", "test", "test-models", "optimization_output"};
simple_jacobian_model
= {"src", "test", "test-models", "simple_jacobian_model"};
output1_csv = {"test", "output1.csv"};
output2_csv = {"test", "output2.csv"};
algorithm = "algorithm = ";
jacobian = "jacobian = ";
}
std::vector<std::string> optimization_model;
std::vector<std::string> simple_jacobian_model;
std::vector<std::string> output1_csv;
std::vector<std::string> output2_csv;
std::string algorithm;
std::string jacobian;
};
TEST_F(CmdStan, optimize_default) {
std::stringstream ss;
ss << convert_model_path(optimization_model)
<< " output file=" << convert_model_path(output1_csv)
<< " method=optimize 2>&1";
std::string cmd = ss.str();
run_command_output out = run_command(cmd);
ASSERT_EQ(0, out.err_code);
std::vector<std::string> config;
std::vector<std::string> header;
std::vector<double> values;
parse_sample(convert_model_path(output1_csv), config, header, values);
int algo_idx = idx_first_match(config, algorithm);
EXPECT_NE(algo_idx, -1);
EXPECT_TRUE(boost::contains(config[algo_idx], "(Default)"));
int jacobian_idx = idx_first_match(config, jacobian);
EXPECT_NE(jacobian_idx, -1);
EXPECT_TRUE(boost::contains(config[jacobian_idx], "= false (Default)"));
ASSERT_NEAR(0, values[0], 0.00001);
auto outputs = values.size();
EXPECT_FLOAT_EQ(1, values[outputs - 4]);
EXPECT_FLOAT_EQ(100, values[outputs - 3]);
EXPECT_FLOAT_EQ(10000, values[outputs - 2]);
EXPECT_FLOAT_EQ(1000000, values[outputs - 1]);
}
TEST_F(CmdStan, optimize_bfgs) {
std::stringstream ss;
ss << convert_model_path(optimization_model)
<< " output file=" << convert_model_path(output1_csv)
<< " method=optimize algorithm=bfgs 2>&1";
std::string cmd = ss.str();
run_command_output out = run_command(cmd);
ASSERT_EQ(0, out.err_code);
std::vector<std::string> config;
std::vector<std::string> header;
std::vector<double> values;
parse_sample(convert_model_path(output1_csv), config, header, values);
int algo_idx = idx_first_match(config, algorithm);
EXPECT_NE(algo_idx, -1);
EXPECT_TRUE(boost::contains(config[algo_idx], "bfgs"));
EXPECT_FALSE(boost::contains(config[algo_idx], "lbfgs"));
ASSERT_NEAR(0, values[0], 0.00001);
auto outputs = values.size();
EXPECT_FLOAT_EQ(1, values[outputs - 4]);
EXPECT_FLOAT_EQ(100, values[outputs - 3]);
EXPECT_FLOAT_EQ(10000, values[outputs - 2]);
EXPECT_FLOAT_EQ(1000000, values[outputs - 1]);
}
TEST_F(CmdStan, optimize_lbfgs) {
std::stringstream ss;
ss << convert_model_path(optimization_model)
<< " output file=" << convert_model_path(output1_csv)
<< " method=optimize algorithm=lbfgs 2>&1";
std::string cmd = ss.str();
run_command_output out = run_command(cmd);
ASSERT_EQ(0, out.err_code);
std::vector<std::string> config;
std::vector<std::string> header;
std::vector<double> values;
parse_sample(convert_model_path(output1_csv), config, header, values);
int algo_idx = idx_first_match(config, algorithm);
EXPECT_NE(algo_idx, -1);
EXPECT_TRUE(boost::contains(config[algo_idx], "lbfgs"));
ASSERT_NEAR(0, values[0], 0.00001);
auto outputs = values.size();
EXPECT_FLOAT_EQ(1, values[outputs - 4]);
EXPECT_FLOAT_EQ(100, values[outputs - 3]);
EXPECT_FLOAT_EQ(10000, values[outputs - 2]);
EXPECT_FLOAT_EQ(1000000, values[outputs - 1]);
}
TEST_F(CmdStan, optimize_newton) {
std::stringstream ss;
ss << convert_model_path(optimization_model)
<< " output file=" << convert_model_path(output1_csv)
<< " method=optimize algorithm=newton 2>&1";
std::string cmd = ss.str();
run_command_output out = run_command(cmd);
ASSERT_EQ(0, out.err_code);
std::vector<std::string> config;
std::vector<std::string> header;
std::vector<double> values;
parse_sample(convert_model_path(output1_csv), config, header, values);
int algo_idx = idx_first_match(config, algorithm);
EXPECT_NE(algo_idx, -1);
EXPECT_TRUE(boost::contains(config[algo_idx], "newton"));
ASSERT_NEAR(0, values[0], 0.00001);
auto outputs = values.size();
EXPECT_FLOAT_EQ(1, values[outputs - 4]);
EXPECT_FLOAT_EQ(100, values[outputs - 3]);
EXPECT_FLOAT_EQ(10000, values[outputs - 2]);
EXPECT_FLOAT_EQ(1000000, values[outputs - 1]);
}
TEST_F(CmdStan, optimize_jacobian_adjust) {
std::stringstream ss;
ss << convert_model_path(simple_jacobian_model) << " random seed=1234"
<< " output file=" << convert_model_path(output1_csv)
<< " method=optimize 2>&1";
std::string cmd = ss.str();
run_command_output out = run_command(cmd);
ASSERT_FALSE(out.hasError);
std::vector<std::string> config1;
std::vector<std::string> header;
std::vector<double> values1;
parse_sample(convert_model_path(output1_csv), config1, header, values1);
int jacobian_idx = idx_first_match(config1, jacobian);
EXPECT_NE(jacobian_idx, -1);
EXPECT_TRUE(boost::contains(config1[jacobian_idx], "= false (Default)"));
auto outputs = values1.size();
ASSERT_NEAR(0, values1[0], 0.00001);
ASSERT_NEAR(3, values1[outputs - 1], 0.01);
ss.str(std::string());
ss << convert_model_path(simple_jacobian_model) << " random seed=1234"
<< " output file=" << convert_model_path(output2_csv)
<< " method=optimize jacobian=1 2>&1";
cmd = ss.str();
out = run_command(cmd);
ASSERT_FALSE(out.hasError);
std::vector<std::string> config2;
std::vector<double> values2;
parse_sample(convert_model_path(output2_csv), config2, header, values2);
jacobian_idx = idx_first_match(config2, jacobian);
EXPECT_NE(jacobian_idx, -1);
EXPECT_TRUE(boost::contains(config2[jacobian_idx], "= true"));
ASSERT_NEAR(3.3, values2[outputs - 1], 0.01);
}