@@ -28,23 +28,38 @@ test_that("hv_dim0_dim1", {
2828 expect_equal(hypervolume(x , ref = ref ), ref - min(x ))
2929})
3030
31+ hv_contributions_slow <- function (dataset , reference , maximise = FALSE )
32+ hypervolume(dataset , reference , maximise ) -
33+ sapply(seq_len(nrow(dataset )), function (x ) hypervolume(dataset [- x , , drop = FALSE ], reference , maximise ))
34+
35+ hv_contributions_nondom_slow <- function (dataset , reference , maximise = FALSE ) {
36+ nondom <- is_nondominated(dataset , maximise = maximise , keep_weakly = TRUE )
37+ hvc <- numeric (nrow(dataset ))
38+ dataset <- dataset [nondom , , drop = FALSE ]
39+ hvc [nondom ] <- hypervolume(dataset , reference , maximise ) -
40+ sapply(seq_len(nrow(dataset )), function (x ) hypervolume(dataset [- x , , drop = FALSE ], reference = reference , maximise = maximise ))
41+ hvc
42+ }
43+
3144test_that(" hv_contributions" , {
32- hv_contributions_slow <- function (dataset , reference , maximise )
33- hypervolume(dataset , reference , maximise ) -
34- sapply(1 : nrow(dataset ), function (x ) hypervolume(dataset [- x ,], reference , maximise ))
35-
36- hv_contributions_nondom_slow <- function (dataset , reference , maximise ) {
37- nondom <- is_nondominated(dataset , maximise = maximise , keep_weakly = TRUE )
38- hvc <- numeric (nrow(dataset ))
39- dataset <- dataset [nondom , , drop = FALSE ]
40- hvc [nondom ] <- hypervolume(dataset , reference , maximise ) -
41- sapply(1 : nrow(dataset ), function (x ) hypervolume(dataset [- x , , drop = FALSE ], reference = reference , maximise = maximise ))
42- hvc
43- }
4445 reference = c(250 ,0 )
4546 maximise = c(FALSE ,TRUE )
4647 expect_equal(hv_contributions(SPEA2minstoptimeRichmond [, 1 : 2 ], reference = reference , maximise = maximise , ignore_dominated = FALSE ),
4748 hv_contributions_slow(SPEA2minstoptimeRichmond [, 1 : 2 ], reference = reference , maximise = maximise ))
4849 expect_equal(hv_contributions(SPEA2minstoptimeRichmond [, 1 : 2 ], reference = reference , maximise = maximise ),
49- hv_contributions_nondom_slow(SPEA2minstoptimeRichmond [, 1 : 2 ], reference = reference , maximise = maximise ))
50+ hv_contributions_nondom_slow(SPEA2minstoptimeRichmond [, 1 : 2 ], reference = reference , maximise = maximise ))
51+
52+ })
53+
54+ test_that(" hv_contributions 3D" , {
55+
56+ set.seed(42 )
57+ pts <- matrix (runif(30L ), ncol = 3L )
58+ ref <- c(2 , 2 , 2 )
59+ expect_equal(tolerance = 1e-10 ,
60+ hv_contributions(pts , reference = ref , ignore_dominated = FALSE ),
61+ hv_contributions_slow(pts , ref ))
62+ expect_equal(tolerance = 1e-10 ,
63+ hv_contributions(pts , reference = ref , ignore_dominated = TRUE ),
64+ hv_contributions_nondom_slow(pts , ref ))
5065})
0 commit comments