Skip to content

Commit dd706ab

Browse files
authored
Add interrupt check (#178)
1 parent 624100b commit dd706ab

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

r-package/policytree/src/Rcppbindings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ Rcpp::List tree_search_rcpp(const Rcpp::NumericMatrix& X,
3636
size_t num_cols_x = X.cols();
3737
size_t num_cols_y = Y.cols();
3838
const Data* data = new Data(X.begin(), Y.begin(), num_rows, num_cols_x, num_cols_y);
39+
const auto interrupt_handler = []() { Rcpp::checkUserInterrupt(); };
3940

40-
std::unique_ptr<Node> root = tree_search(depth, split_step, min_node_size, data);
41+
std::unique_ptr<Node> root = tree_search(depth, split_step, min_node_size, data, interrupt_handler);
4142

4243
// We store the tree as the same list data structure (`nodes`) as GRF for seamless integration with
4344
// the plot and print methods. We also store the tree as an array (`tree_array`) for faster lookups.

r-package/policytree/src/tree_search.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ std::unique_ptr<Node> find_best_split(const std::vector<flat_set>& sorted_sets,
268268
int split_step,
269269
size_t min_node_size,
270270
const Data* data,
271-
std::vector<std::vector<double>>& sum_array) {
271+
std::vector<std::vector<double>>& sum_array,
272+
const std::function<void()>& interrupt_handler) {
272273
if (level == 0) {
273274
// this base case will only be hit if `find_best_split` is called directly with level = 0
274275
return level_zero_learning(sorted_sets, data);
@@ -291,6 +292,9 @@ std::unique_ptr<Node> find_best_split(const std::vector<flat_set>& sorted_sets,
291292
int split_counter = 0;
292293
size_t samples_counter = 0;
293294
for (size_t n = 0; n < num_points - 1; n++) {
295+
if ((n & 1023) == 0) {
296+
interrupt_handler(); // check for ctrl-c interrupt every 1024 iterations
297+
}
294298
auto point = right_sorted_sets[p].cbegin(); // O(1)
295299
Point point_bk = *point; // store the Point instance since the iterator will be invalid after erase
296300
right_sorted_sets[p].erase(point); // O(1)
@@ -317,8 +321,8 @@ std::unique_ptr<Node> find_best_split(const std::vector<flat_set>& sorted_sets,
317321
} else {
318322
continue;
319323
}
320-
auto left_child = find_best_split(left_sorted_sets, level - 1, split_step, min_node_size, data, sum_array);
321-
auto right_child = find_best_split(right_sorted_sets, level - 1, split_step, min_node_size, data, sum_array);
324+
auto left_child = find_best_split(left_sorted_sets, level - 1, split_step, min_node_size, data, sum_array, interrupt_handler);
325+
auto right_child = find_best_split(right_sorted_sets, level - 1, split_step, min_node_size, data, sum_array, interrupt_handler);
322326
if ((best_left_child == nullptr) ||
323327
(left_child->reward + right_child->reward >
324328
best_left_child->reward + best_right_child->reward)) {
@@ -350,7 +354,8 @@ std::unique_ptr<Node> find_best_split(const std::vector<flat_set>& sorted_sets,
350354
}
351355
}
352356

353-
std::unique_ptr<Node> tree_search(int depth, int split_step, size_t min_node_size, const Data* data) {
357+
std::unique_ptr<Node> tree_search(int depth, int split_step, size_t min_node_size, const Data* data,
358+
const std::function<void()>& interrupt_handler) {
354359
size_t num_rewards = data->num_rewards();
355360
size_t num_points = data->num_rows;
356361
auto sorted_sets = create_sorted_sets(data);
@@ -362,5 +367,5 @@ std::unique_ptr<Node> tree_search(int depth, int split_step, size_t min_node_siz
362367
v.resize(num_points + 1, 0.0);
363368
}
364369

365-
return find_best_split(sorted_sets, depth, split_step, min_node_size, data, sum_array);
370+
return find_best_split(sorted_sets, depth, split_step, min_node_size, data, sum_array, interrupt_handler);
366371
}

r-package/policytree/src/tree_search.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ struct Node {
9191
};
9292

9393

94-
std::unique_ptr<Node> tree_search(int, int, size_t, const Data*);
94+
std::unique_ptr<Node> tree_search(int, int, size_t, const Data*, const std::function<void()>&);
9595

9696
#endif // TREE_SEARCH_H

0 commit comments

Comments
 (0)