Skip to content

Commit

Permalink
improve test cov
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyhanson committed Jul 10, 2024
1 parent 3924a17 commit 9cd2dc6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/rcpp_expected_value_of_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ double expected_value_of_action(
return spp_prob.sum();
}

// this function is used for internal debugging when we need to calculate
// the expected value of actions using methods that produce correct
// (not approximation) results

// nocov start
double exact_expected_value_of_action(

Check warning on line 53 in src/rcpp_expected_value_of_action.cpp

View check run for this annotation

Codecov / codecov/patch

src/rcpp_expected_value_of_action.cpp#L53

Added line #L53 was not covered by tests
Eigen::MatrixXd &pij,
Rcpp::IntegerVector &target_values) {
Expand All @@ -70,6 +75,7 @@ double exact_expected_value_of_action(
}
return out;
}
// nocov end

double approx_expected_value_of_action(
std::vector<std::vector<double>> &pij,
Expand Down Expand Up @@ -130,15 +136,6 @@ double approx_expected_value_of_action(
return out;
}

double log_proxy_expected_value_of_action(
Eigen::MatrixXd &log_1m_pij) {
Eigen::VectorXd out = log_1m_pij.rowwise().sum();
// note we that we use log_substract(0, ..) because 0 is log(1.0)
for (auto itr = out.data(); itr != out.data() + out.size(); ++itr)
*itr = log_subtract(0.0, *itr);
return log_sum(out);
}

// [[Rcpp::export]]
double rcpp_expected_value_of_action(
std::vector<bool> solution,
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test_greedy_heuristic_prioritization.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,40 @@ test_that("expected result", {
(1 - ((1 - 0.8) * (1 - 0.8))) * 2
)
})

test_that("expected result (default locked in and locked out)", {
# data
site_data <- sf::st_as_sf(
tibble::tibble(
x = seq_len(3),
y = x,
management_cost = c(100, 100, 100),
locked_in = c(FALSE, FALSE, FALSE),
locked_out = c(FALSE, FALSE, FALSE),
p1 = c(0.8, 0.8, 0.8),
p2 = c(0.05, 0.8, 0.8)
),
coords = c("x", "y")
)
feature_data <- tibble::tibble(
name = letters[1:2],
target = c(1, 1)
)
budget = 300
# results
x <- greedy_heuristic_prioritization(
site_data,
feature_data,
site_probability_columns = c("p1", "p2"),
site_management_cost_column = "management_cost",
feature_target_column = "target",
total_budget = budget
)
# tests
expect_equal(x$x, c(FALSE, TRUE, TRUE))
expect_equal(
x$objval,
# sum of probability values that each species meets its target
(1 - ((1 - 0.8) * (1 - 0.8))) * 2
)
})

0 comments on commit 9cd2dc6

Please sign in to comment.