doRanking(c(a=1,b=2))
# b > a
doRanking(c(a=2,b=2))
# a ~ b
# a custom ranking function. Here, we implement the following ranking solution:
# disregard any big coalitions and only rank elements based on their individual performances
# iRj if and only if {i} >= {j}
singletonRanking <- function(pr) {
scores <- sapply(pr$elements, equivalenceClassIndex, powerRelation = pr)
# note that coalitions in higher indexed equivalence classes are less preferable
# hence, scores should be sorted in an increasing order
doRanking(scores, decreasing = FALSE)
}
pr <- as.PowerRelation("abc > ab > ac > b ~ c ~ bc > a")
singletonRanking(pr)
# b ~ c > a
# a reverse lexcel ranking, where vectors are compared right to left
# here, we introduce a compare function. It returns:
# * 0, if a and b are identical
# * a positive value, if a[i] > b[i] and every value after that is equal
# * a negative value, if a[i] < b[i] and every value after that is equal
reverseLexcelCompare <- function(a, b) {
i <- which(a != b) |> rev()
if(length(i) == 0) 0
else a[i[1]] - b[i[1]]
}
scores <- unclass(cumulativeScores(pr))
# R cannot natively sort a class. Instead:
# Method 1 - utilize the compare parameter
doRanking(scores, compare = reverseLexcelCompare)
# Method 2 - introduce S3 class
`[.RevLex` <- function(x, i, ...) structure(unclass(x)[i], class = "RevLex")
`==.RevLex` <- function(a, b) reverseLexcelCompare(a[[1]],b[[1]]) == 0
`>.RevLex` <- function(a, b) reverseLexcelCompare(a[[1]],b[[1]]) > 0
is.na.RevLex <- function(x) FALSE
doRanking(structure(scores, class = "RevLex"))
stopifnot(
doRanking(scores, compare = reverseLexcelCompare) ==
doRanking(structure(scores, class = "RevLex"))
)
Run the code above in your browser using DataLab