Nothing
#' BOLT algorithm for the median ranking
#'
#' Find the median ranking according to the Kemeny's axiomatic approach with
#' BOLT (Bucket-Order Local search with optimal Tie partitioning), a
#' deterministic and scalable heuristic that works entirely on the combined
#' input matrix.
#'
#' @param X A N by M data matrix, in which there are N judges and M objects to
#' be judged. Each row is a ranking of the objects which are represented by
#' the columns. If X contains the rankings observed only once, the argument wk
#' can be used. Either X or cij must be given
#' @param wk Optional: the frequency of each ranking in the data
#' @param full Logical. If TRUE, the median ranking is searched in the space of
#' full rankings (permutations), i.e. without ties. Default FALSE
#' @param otp Logical. If TRUE (default), the local search is alternated with the
#' exact optimal tie partitioning (the "full" configuration of the paper); if
#' FALSE, only the tie-aware local search is used (the "fast" configuration).
#' Ignored when \code{full = TRUE}
#' @param maxjudges Maximum number of distinct rankings of the judges used as
#' starting solutions, the most frequent first. Default Inf (all of them);
#' 0 uses only the starting solutions computed from the combined input matrix
#' @param ps Logical. If TRUE, displays information on screen. Default FALSE
#' @param cij The combined input matrix (M by M, see \code{\link{combinpmatr}}),
#' to be given instead of X. With cij, the argument wk is ignored and the
#' rankings of the judges are not used as starting solutions
#' @param nj The number of judges (the sum of the weights, if any) that produced
#' cij. Required when cij is given: it is used to compute the averaged TauX
#'
#' @return A list containing the following components:
#' \tabular{lll}{
#' Consensus \tab \tab the consensus ranking(s)\cr
#' Tau \tab \tab averaged TauX rank correlation coefficient\cr
#' Eltime \tab \tab elapsed time in seconds
#' }
#'
#' @details
#' The median ranking maximizes the averaged TauX rank correlation coefficient
#' with the rankings of the judges, which depends on the data only through the
#' combined input matrix C (Emond and Mason, 2002). BOLT works entirely on C and
#' combines three ingredients:
#' \enumerate{
#' \item starting solutions computed from C (net flow, Copeland, a Markov chain
#' score, a bucket pivot order and the starting solution of the branch-and-bound
#' algorithm) and the rankings of the judges;
#' \item a tie-aware insertion local search: an object is moved to another
#' position or tied with a group of objects, and each move is evaluated in O(M)
#' time;
#' \item the optimal tie partitioning: for a fixed order of the objects, the tie
#' structure maximizing TauX is computed exactly by dynamic programming in
#' O(M^2) time.
#' }
#' The local search and the optimal tie partitioning are alternated until no
#' improvement, from each starting solution; the best solution(s) are returned.
#' When ties are allowed (\code{full = FALSE}), a first phase runs the local
#' search without ties from each starting solution (ties broken) and adds the
#' resulting full rankings to the starting solutions: the result is never worse
#' than the one with \code{full = TRUE}.
#' The procedure is deterministic: no random starting solutions are used.
#'
#' Instead of the data X, the combined input matrix cij can be given, together
#' with the number of judges nj.
#'
#' @examples
#' data(EMD)
#' CR <- bolt(EMD[, 1:15], wk = EMD[, 16])
#' CR$Consensus
#' CR$Tau
#' # the same result from the combined input matrix
#' CR2 <- bolt(cij = combinpmatr(EMD[, 1:15], EMD[, 16]), nj = sum(EMD[, 16]),
#' maxjudges = 0)
#'
#' @references
#' Romano, M., D'Ambrosio, A., and Conversano, C. A deterministic and scalable
#' heuristic for median ranking detection: tie-aware local search with optimal
#' tie partitioning on the combined input matrix. Manuscript under review.
#'
#' Emond, E. J., and Mason, D. W. (2002). A new rank correlation coefficient with
#' application to the consensus ranking problem. Journal of Multi-Criteria
#' Decision Analysis, 11(1), 17-28.
#'
#' @author Maurizio Romano
#'
#' @seealso \code{\link{consrank}}, \code{\link{mcbo}}, \code{\link{combinpmatr}}
#'
#' @keywords Median ranking
#' @keywords Consensus ranking
#' @keywords Bucket order
#'
#' @export
bolt <- function(X = NULL, wk = NULL, full = FALSE, otp = TRUE, maxjudges = Inf,
ps = FALSE, cij = NULL, nj = NULL) {
tic <- proc.time()[3]
cij <- checkXcij(X, cij, nj, wk)
if (!is.null(cij)) {
out <- boltcij(cij, nj, full = full, otp = otp, seeds = NULL, ps = ps)
colnames(out$Consensus) <- colnames(cij)
out$Eltime <- proc.time()[3] - tic
return(out)
}
if (is(X, "data.frame")) X <- as.matrix(X)
if (is.null(nrow(X))) X <- matrix(X, nrow = 1)
N <- ncol(X)
if (nrow(X) == 1) {
OJ <- onejudge(X, full) # single judge (ties broken if full = TRUE)
return(list(Consensus = reordering(OJ$consensus), Tau = OJ$tau, Eltime = proc.time()[3] - tic))
}
if (!is.null(wk)) {
wk <- as.numeric(wk)
if (length(wk) != nrow(X)) stop("Length of wk must equal number of rows in X")
}
cij <- combinpmatr(X, if (is.null(wk)) NULL else matrix(wk, ncol = 1))
nj <- if (is.null(wk)) nrow(X) else sum(wk)
judges <- boltjudges(X, wk, maxjudges)
out <- boltcij(cij, nj, full = full, otp = otp, seeds = judges, ps = ps)
colnames(out$Consensus) <- colnames(X)
out$Eltime <- proc.time()[3] - tic
out
}
# ══════════════════════════════════════════════════════════════════════════
# BOLT on the combined input matrix (internal)
# ══════════════════════════════════════════════════════════════════════════
boltcij <- function(cij, nj, full = FALSE, otp = TRUE, seeds = NULL, ps = FALSE) {
N <- ncol(cij)
S <- boltseeds(cij, seeds, full)
tol <- 1e-10 * max(1, sum(abs(cij)))
if (!full) {
# first phase: from each seed (ties broken) the local search without ties
# gives a full ranking; these full rankings are added to the seeds, so that
# the result is never worse than the one with full = TRUE (on flat data the
# optimum has few ties, and the original seeds can lead to local optima
# with too many ties)
Sf <- boltseeds(cij, seeds, full = TRUE)
Rf <- bolt_run_impl(t(Sf), cij, TRUE, otp, tol)$rankings
S <- unique(rbind(S, t(apply(Rf, 2, boltdense))))
}
res <- bolt_run_impl(t(S), cij, full, otp, tol)
top <- which(res$value >= max(res$value) - tol)
cons <- unique(reordering(t(res$rankings[, top, drop = FALSE])))
tau <- bolt_obj_impl(t(cons), cij) / (nj * N * (N - 1))
if (ps) {
message("BOLT: ", nrow(S), " starting solutions, ", nrow(cons),
" solution(s) with Tau = ", round(max(tau), 5))
}
list(Consensus = cons, Tau = if (nrow(cons) == 1) tau else matrix(tau, ncol = 1))
}
# BOLT for the median constrained bucket order: exactly nb buckets (internal,
# used by mcbo)
boltcijbuckets <- function(cij, nb, nj, seeds = NULL, ps = FALSE) {
N <- ncol(cij)
S <- boltseeds(cij, seeds, full = FALSE)
tol <- 1e-10 * max(1, sum(abs(cij)))
res <- bolt_run_b_impl(t(S), cij, nb, tol)
top <- which(res$value >= max(res$value) - tol)
cons <- unique(reordering(t(res$rankings[, top, drop = FALSE])))
tau <- bolt_obj_impl(t(cons), cij) / (nj * N * (N - 1))
if (ps) {
message("BOLT: ", nrow(S), " starting solutions, ", nrow(cons),
" solution(s) with Tau = ", round(max(tau), 5))
}
list(Consensus = cons, Tau = if (nrow(cons) == 1) tau else matrix(tau, ncol = 1))
}
# Checks the pattern of bucket sizes and returns it in standard form: one
# element per bucket, a positive whole number where the size is prescribed and
# NA where it is free. A pattern whose given sizes do not use all the objects
# gets one free bucket at the end, so that c(1, 1, 1) with ten objects means
# three buckets of one object and a fourth one with the remaining seven
checkbucketsizes <- function(bucketsizes, N) {
s <- bucketsizes
if (is.logical(s) && all(is.na(s))) s <- as.numeric(s) # rep(NA, b) is logical
if (!is.numeric(s) || length(s) < 1) {
stop("bucketsizes must be a numeric vector with one element per bucket")
}
given <- s[!is.na(s)]
if (length(given) && (any(given != round(given)) || any(given < 1))) {
stop("the sizes in bucketsizes must be whole numbers greater than zero, or NA for a free size")
}
s <- as.integer(round(s))
if (sum(is.na(s)) == 0 && sum(s) < N) {
s <- c(s, NA_integer_) # one free bucket takes the rest
}
nfree <- sum(is.na(s)) # buckets whose size is free
tot <- sum(s, na.rm = TRUE) # objects already assigned
if (length(s) > N) {
stop("bucketsizes cannot ask for more buckets than the number of objects (", N, ")")
}
if (tot + nfree > N) {
stop("bucketsizes asks for more objects than there are (", N, ")")
}
if (nfree == 0 && tot != N) {
stop("the sizes in bucketsizes must sum to the number of objects (", N, ")")
}
s
}
# BOLT for the median bucket order whose bucket sizes follow a pattern
# (internal, used by mcbo): same structure as boltcijbuckets, with the pattern
# in place of the number of buckets
boltcijpattern <- function(cij, sizes, nj, seeds = NULL, ps = FALSE) {
N <- ncol(cij)
S <- boltseeds(cij, seeds, full = FALSE)
tol <- 1e-10 * max(1, sum(abs(cij)))
res <- bolt_run_pat_impl(t(S), cij, as.integer(sizes), tol)
if (all(!is.finite(res$value))) {
stop("no bucket order satisfies bucketsizes")
}
top <- which(res$value >= max(res$value) - tol)
cons <- unique(reordering(t(res$rankings[, top, drop = FALSE])))
tau <- bolt_obj_impl(t(cons), cij) / (nj * N * (N - 1))
if (ps) {
message("BOLT: ", nrow(S), " starting solutions, ", nrow(cons),
" solution(s) with Tau = ", round(max(tau), 5))
}
list(Consensus = cons, Tau = if (nrow(cons) == 1) tau else matrix(tau, ncol = 1))
}
# starting solutions: computed from cij and given by the user (seeds); one in
# each row, dense rankings, without repetitions
boltseeds <- function(cij, seeds = NULL, full = FALSE) {
N <- ncol(cij)
d <- cij - t(cij)
sig <- cij + t(cij)
S <- rbind(boltrank(rowSums(d)), # net flow (Borda)
boltrank(rowSums(sign(d))), # Copeland
boltmarkov(d), # Markov chain score
boltpivot(d, sig), # bucket pivot order
findconsensusBB(cij)) # starting point of the BB algorithm
if (!is.null(seeds)) {
seeds <- as.matrix(seeds)
if (ncol(seeds) != N) stop("seeds must have ", N, " columns")
S <- rbind(S, t(apply(seeds, 1, boltdense)))
}
if (full) S <- t(apply(S, 1, rank, ties.method = "first"))
S <- t(apply(S, 1, boltdense))
unique(S)
}
# rankings of the judges used as starting solutions: the distinct rows of X,
# the most frequent first, at most maxjudges (missing objects tied last)
boltjudges <- function(X, wk, maxjudges) {
if (maxjudges <= 0) return(NULL)
X[is.na(X)] <- max(X, na.rm = TRUE) + 1
key <- apply(X, 1, paste, collapse = " ")
w <- if (is.null(wk)) rep(1, nrow(X)) else as.numeric(wk)
tot <- tapply(w, key, sum)
first <- X[match(names(tot), key), , drop = FALSE]
first <- first[order(-tot, seq_along(tot)), , drop = FALSE]
first[seq_len(min(nrow(first), maxjudges)), , drop = FALSE]
}
# dense ranking 1, 2, ... (ties share the same value)
boltdense <- function(r) {
match(r, sort(unique(r)))
}
# ranking from a score (the larger the better; equal scores are tied)
boltrank <- function(score) {
boltdense(-score)
}
# stationary distribution of a random walk moving from each object to the
# objects that beat it, with weights the positive net preferences
boltmarkov <- function(d, damp = 0.85, iter = 1000) {
N <- nrow(d)
Q <- pmax(t(d), 0)
rs <- rowSums(Q)
Q[rs == 0, ] <- 0
diag(Q)[rs == 0] <- 1
Q <- Q / rowSums(Q)
p <- rep(1 / N, N)
for (it in seq_len(iter)) {
new <- damp * drop(p %*% Q) + (1 - damp) / N
done <- max(abs(new - p)) < 1e-14
p <- new
if (done) break
}
boltrank(round(p, 12))
}
# bucket pivot order (least indecision pivot): the pivot is the most "decided"
# object, the others are tied with it, before it or after it
boltpivot <- function(d, sig) {
N <- nrow(d)
r <- integer(N)
b <- 0L
# stack of groups still to split (done = FALSE) and of buckets ready to be
# placed (done = TRUE); the top of the stack is always the leftmost element,
# so the buckets are placed from the best to the worst
stack <- list(list(items = seq_len(N), done = FALSE))
while (length(stack) > 0) {
top <- stack[[length(stack)]]
stack[[length(stack)]] <- NULL
items <- top$items
if (top$done || length(items) == 1) {
b <- b + 1L
r[items] <- b
next
}
piv <- items[which.max(rowSums(abs(d[items, items, drop = FALSE])))]
rest <- items[items != piv]
tie <- rest[sig[piv, rest] > abs(d[piv, rest])]
rest <- rest[!rest %in% tie]
bef <- rest[d[rest, piv] > 0]
aft <- rest[!rest %in% bef]
if (length(aft) > 0) stack[[length(stack) + 1]] <- list(items = aft, done = FALSE)
stack[[length(stack) + 1]] <- list(items = c(piv, tie), done = TRUE)
if (length(bef) > 0) stack[[length(stack) + 1]] <- list(items = bef, done = FALSE)
}
r
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.