Skip to content

Commit 3ab0987

Browse files
committed
* r/tests/testthat/test-hv.R: Test hv_contributions in 3D.
* r/tests/testthat/test-nondominated.R: Test 4D and 5D.
1 parent 8053509 commit 3ab0987

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

r/tests/testthat/test-hv.R

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3144
test_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
})

r/tests/testthat/test-nondominated.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,31 @@ test_that("bug 27", {
1212
expect_false(any_dominated(matrix(rep(5,5), ncol=1L), keep_weakly=TRUE))
1313

1414
})
15+
16+
test_that("is_nondominated 4D", {
17+
set.seed(42)
18+
pts <- matrix(runif(100), ncol = 4L) # 25 points in 4D
19+
20+
fd <- filter_dominated(pts)
21+
expect_true(nrow(fd) >= 1L)
22+
nd <- is_nondominated(fd)
23+
expect_true(is.logical(nd))
24+
expect_equal(length(nd), nrow(fd))
25+
# The non-dominated points should not dominate each other
26+
expect_true(all(nd))
27+
expect_false(any_dominated(fd))
28+
})
29+
30+
test_that("is_nondominated 5D", {
31+
set.seed(123)
32+
pts <- matrix(runif(50), ncol = 5) # 10 points in 5D
33+
34+
fd <- filter_dominated(pts)
35+
expect_true(nrow(fd) >= 1L)
36+
nd <- is_nondominated(fd)
37+
expect_true(is.logical(nd))
38+
expect_equal(length(nd), nrow(fd))
39+
# The non-dominated points should not dominate each other
40+
expect_true(all(nd))
41+
expect_false(any_dominated(fd))
42+
})

0 commit comments

Comments
 (0)