Learn R Programming

BayesMallowsSMC2 (version 0.2.1)

precompute_topological_sorts: Precompute All Topological Sorts

Description

This function precomputes all topological sorts for a given preference matrix. Topological sorts are consistent orderings of items that respect the given pairwise preference constraints.

Usage

precompute_topological_sorts(prefs, n_items, save_frac)

Value

A list with two elements:

sort_count

An integer giving the total number of topological sorts.

sort_matrix

A matrix where each column represents one topological sort. The number of columns is approximately save_frac times sort_count. If save_frac = 0, this is an empty matrix with dimensions c(0, 0).

Arguments

prefs

A matrix representing the preference relations. This matrix must have two columns, the first of which represents the preferred item and the second of which represents the disfavored item.

n_items

An integer specifying the number of items to sort.

save_frac

Number between 0 and 1 specifying which fraction of sorts to save.

Details

The function generates all possible topological sorts for the provided preference matrix and saves approximately save_frac of the sorts in a matrix which is returned.

Examples

Run this code
# Extract preferences from user 1 in the included example data.
prefs <- pairwise_preferences[
 pairwise_preferences$user == 1, c("top_item", "bottom_item"), drop = FALSE]

# Generate all topological sorts, but don't save them:
sorts <- precompute_topological_sorts(
  prefs = as.matrix(prefs),
  n_items = 5,
  save_frac = 0
)
# Number of sorts
sorts$sort_count
# Empty matrix
sorts$sort_matrix

# Generate all topological sorts and save them:
sorts <- precompute_topological_sorts(
  prefs = as.matrix(prefs),
  n_items = 5,
  save_frac = 1
)
# Number of sorts
sorts$sort_count
# Matrix with all of them
sorts$sort_matrix

Run the code above in your browser using DataLab