Commit | Line | Data |
---|---|---|
cbd88fe5 BA |
1 | test_that("HungarianAlgorithm provides the correct answer on various inputs", |
2 | { | |
6dd5c2ac BA |
3 | for (n in c(2,3,10,50)) |
4 | { | |
5 | for (repetition in 1:3) | |
6 | { | |
7 | # Generate a random vector, and permute it: | |
8 | # we expect the algorithm to retrieve the permutation | |
2b3a6af5 BA |
9 | v <- runif(n, min=-1, max=1) |
10 | permutation <- sample(1:n) | |
11 | v_p <- v[permutation] | |
cbd88fe5 | 12 | |
6dd5c2ac | 13 | # v is reference, v_p is v after permutation |
2b3a6af5 BA |
14 | # distances[i,j] = distance between i-th component of v |
15 | # and j-th component of v_p | |
6dd5c2ac BA |
16 | # "in rows : reference; in columns: after permutation" |
17 | # "permutation" contains order on v to match v_p as closely as possible | |
2b3a6af5 BA |
18 | distances <- sapply(v_p, function(elt) ( abs(elt - v) ) ) |
19 | assignment <- .hungarianAlgorithm(distances) | |
6dd5c2ac BA |
20 | expect_equal(assignment, permutation) |
21 | } | |
22 | } | |
cbd88fe5 | 23 | }) |