@@ -73,3 +73,154 @@ TEST(TreeStructuredParzenEstimatorTest, TPE_is_better_than_random_search_on_sphe
73
73
}
74
74
ASSERT_LT (mean_scores[0 ], mean_scores[1 ]);
75
75
}
76
+
77
+ // Additional tests for TreeStructuredParzenEstimator
78
+ TEST (TreeStructuredParzenEstimatorTest, MinimizationTest)
79
+ {
80
+ auto quadratic_function = [](const TreeStructuredParzenEstimator::Input & input) {
81
+ return input[0 ] * input[0 ] + input[1 ] * input[1 ];
82
+ };
83
+
84
+ constexpr int64_t k_trials_num = 100 ;
85
+ std::vector<double > sample_mean{0.0 , 0.0 };
86
+ std::vector<double > sample_stddev{1.0 , 1.0 };
87
+
88
+ double best_score = std::numeric_limits<double >::max ();
89
+ TreeStructuredParzenEstimator estimator (
90
+ TreeStructuredParzenEstimator::Direction::MINIMIZE, k_trials_num / 2 , sample_mean,
91
+ sample_stddev);
92
+
93
+ for (int64_t trial = 0 ; trial < k_trials_num; trial++) {
94
+ const TreeStructuredParzenEstimator::Input input = estimator.get_next_input ();
95
+ const double score = quadratic_function (input);
96
+ estimator.add_trial ({input, score});
97
+ best_score = std::min (best_score, score);
98
+ }
99
+
100
+ EXPECT_LT (best_score, 0.1 ); // Should find a solution close to 0
101
+ }
102
+
103
+ TEST (TreeStructuredParzenEstimatorTest, SingleDimensionTest)
104
+ {
105
+ auto linear_function = [](const TreeStructuredParzenEstimator::Input & input) {
106
+ return input[0 ];
107
+ };
108
+
109
+ constexpr int64_t k_trials_num = 50 ;
110
+ std::vector<double > sample_mean{0.0 };
111
+ std::vector<double > sample_stddev{1.0 };
112
+
113
+ double best_score = std::numeric_limits<double >::lowest ();
114
+ TreeStructuredParzenEstimator estimator (
115
+ TreeStructuredParzenEstimator::Direction::MAXIMIZE, k_trials_num / 2 , sample_mean,
116
+ sample_stddev);
117
+
118
+ for (int64_t trial = 0 ; trial < k_trials_num; trial++) {
119
+ const TreeStructuredParzenEstimator::Input input = estimator.get_next_input ();
120
+ const double score = linear_function (input);
121
+ estimator.add_trial ({input, score});
122
+ best_score = std::max (best_score, score);
123
+ }
124
+
125
+ EXPECT_GT (best_score, 2.0 ); // Should find a value in the right tail
126
+ }
127
+
128
+ TEST (TreeStructuredParzenEstimatorTest, EmptyTrials)
129
+ {
130
+ std::vector<double > sample_mean{0.0 , 0.0 };
131
+ std::vector<double > sample_stddev{1.0 , 1.0 };
132
+
133
+ TreeStructuredParzenEstimator estimator (
134
+ TreeStructuredParzenEstimator::Direction::MAXIMIZE, 10 , sample_mean, sample_stddev);
135
+
136
+ // Should not crash when getting input without any trials
137
+ const TreeStructuredParzenEstimator::Input input = estimator.get_next_input ();
138
+ EXPECT_EQ (input.size (), 2u );
139
+ }
140
+
141
+ TEST (TreeStructuredParzenEstimatorTest, SingleTrial)
142
+ {
143
+ std::vector<double > sample_mean{0.0 , 0.0 };
144
+ std::vector<double > sample_stddev{1.0 , 1.0 };
145
+
146
+ TreeStructuredParzenEstimator estimator (
147
+ TreeStructuredParzenEstimator::Direction::MAXIMIZE, 0 , sample_mean, sample_stddev);
148
+
149
+ // Add one trial
150
+ const TreeStructuredParzenEstimator::Input first_input{1.0 , 1.0 };
151
+ estimator.add_trial ({first_input, 1.0 });
152
+
153
+ // Should be able to get next input
154
+ const TreeStructuredParzenEstimator::Input next_input = estimator.get_next_input ();
155
+ EXPECT_EQ (next_input.size (), 2u );
156
+ }
157
+
158
+ TEST (TreeStructuredParzenEstimatorTest, LogGaussianPdf)
159
+ {
160
+ TreeStructuredParzenEstimator::Input input{1.0 , 2.0 };
161
+ TreeStructuredParzenEstimator::Input mu{1.0 , 2.0 };
162
+ TreeStructuredParzenEstimator::Input sigma{1.0 , 1.0 };
163
+
164
+ double log_pdf = TreeStructuredParzenEstimator::log_gaussian_pdf (input, mu, sigma);
165
+ EXPECT_NEAR (log_pdf, -1.8378770664093453 , 1e-6 ); // Expected value calculated manually
166
+
167
+ // Test with different sigma
168
+ TreeStructuredParzenEstimator::Input sigma2{0.5 , 0.5 };
169
+ log_pdf = TreeStructuredParzenEstimator::log_gaussian_pdf (input, mu, sigma2);
170
+ EXPECT_NEAR (log_pdf, -2.8378770664093453 , 1e-6 );
171
+ }
172
+
173
+ TEST (TreeStructuredParzenEstimatorTest, DifferentSampleStddev)
174
+ {
175
+ std::vector<double > sample_mean{0.0 , 0.0 , 0.0 };
176
+ std::vector<double > sample_stddev{1.0 , 0.1 , 10.0 }; // Different scales for different dimensions
177
+
178
+ TreeStructuredParzenEstimator estimator (
179
+ TreeStructuredParzenEstimator::Direction::MAXIMIZE, 10 , sample_mean, sample_stddev);
180
+
181
+ // Add some trials
182
+ for (int i = 0 ; i < 5 ; ++i) {
183
+ TreeStructuredParzenEstimator::Input input{0.1 * i, 0.01 * i, 1.0 * i};
184
+ estimator.add_trial ({input, static_cast <double >(i)});
185
+ }
186
+
187
+ // Get next input and verify it's within reasonable bounds
188
+ const TreeStructuredParzenEstimator::Input next_input = estimator.get_next_input ();
189
+ EXPECT_NEAR (next_input[0 ], 0.0 , 2.0 ); // stddev = 1.0
190
+ EXPECT_NEAR (next_input[1 ], 0.0 , 0.2 ); // stddev = 0.1
191
+ EXPECT_NEAR (next_input[2 ], 0.0 , 20.0 ); // stddev = 10.0
192
+ }
193
+
194
+ TEST (TreeStructuredParzenEstimatorTest, ComputeLogLikelihoodRatio)
195
+ {
196
+ std::vector<double > sample_mean{0.0 , 0.0 };
197
+ std::vector<double > sample_stddev{1.0 , 1.0 };
198
+
199
+ TreeStructuredParzenEstimator estimator (
200
+ TreeStructuredParzenEstimator::Direction::MAXIMIZE, 0 , sample_mean, sample_stddev);
201
+
202
+ // Add some trials
203
+ for (int i = 0 ; i < 10 ; ++i) {
204
+ TreeStructuredParzenEstimator::Input input{0.1 * i, 0.1 * i};
205
+ estimator.add_trial ({input, static_cast <double >(i)});
206
+ }
207
+
208
+ // Test likelihood ratio for a point
209
+ TreeStructuredParzenEstimator::Input test_point{0.5 , 0.5 };
210
+ double ratio = estimator.compute_log_likelihood_ratio (test_point);
211
+
212
+ // The exact value depends on the random sampling, but we can check it's finite
213
+ EXPECT_TRUE (std::isfinite (ratio));
214
+ }
215
+
216
+ TEST (TreeStructuredParzenEstimatorTest, FixedIndexTest)
217
+ {
218
+ // Test that the enum values are as expected
219
+ EXPECT_EQ (TreeStructuredParzenEstimator::TRANS_X, 0 );
220
+ EXPECT_EQ (TreeStructuredParzenEstimator::TRANS_Y, 1 );
221
+ EXPECT_EQ (TreeStructuredParzenEstimator::TRANS_Z, 2 );
222
+ EXPECT_EQ (TreeStructuredParzenEstimator::ANGLE_X, 3 );
223
+ EXPECT_EQ (TreeStructuredParzenEstimator::ANGLE_Y, 4 );
224
+ EXPECT_EQ (TreeStructuredParzenEstimator::ANGLE_Z, 5 );
225
+ EXPECT_EQ (TreeStructuredParzenEstimator::INDEX_NUM, 6 );
226
+ }
0 commit comments