Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use prefio for ballots. #32

Merged
merged 11 commits into from
Sep 7, 2023
Merged
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
^CRAN-SUBMISSION$
^LICENSE$
^README\.(Rmd|md|html)$
^\.github$
^\.lintr$
^\.pre-commit-config\.yaml$
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ repos:
- methods
- Rdpack
- R6
- prefio
- id: use-tidy-description
- id: spell-check
exclude: >
Expand Down Expand Up @@ -61,6 +62,7 @@ repos:
(?x)^(
(.*/|)RcppExports\.(cpp|r|R)|
R/catch-routine-registration.R|
README\.(Rmd|md)|
)$
- id: readme-rmd-rendered
- id: parsable-R
Expand Down
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Authors@R: c(
person("Damjan", "Vukcevic", email = "damjan@vukcevic.net",
role = c("aut"), comment = c(ORCID = "0000-0001-7780-9586"))
)
Version: 1.1.2
Version: 2.0.0
Depends: R (>= 4.0.0)
SystemRequirements: C++17
License: GPL-3
LazyData: TRUE
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
LinkingTo:
Rcpp,
RcppThread,
Expand All @@ -25,7 +25,8 @@ Imports:
methods,
Rdpack,
R6,
parallel
parallel,
prefio (>= 0.1.1)
RdMacros: Rdpack
Suggests:
covr,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method("[",ranked_ballots)
S3method(social_choice,ranked_ballots)
S3method(update,dirichlet_tree)
export(dirichlet_tree)
export(dirtree)
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# elections.dtree 1.1.2
# elections.dtree 2.0.0

Rewrote some of the interface to make use of the `prefio` library for preference
data.

## 1.1.2

* Improved the `sample_posterior` multithreading so that the remainder is spread
out among the worker threads instead of the entire workload being run on the
Expand Down
69 changes: 9 additions & 60 deletions R/ballots.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ validate_rankedballots <- function(ballots, candidates = NULL, ...) {
#' Construct a set of ranked ballots.
#'
#' @description
#' Deprecated in favour of functionality from the `prefio` package.
#' \code{ranked_ballots} is used to easily construct a set of ranked ballots.
#'
#' @examples
Expand All @@ -68,6 +69,8 @@ validate_rankedballots <- function(ballots, candidates = NULL, ...) {
#'
#' @export
ranked_ballots <- function(x, candidates = NULL, ...) {
warning("`ranked_ballots` is deprecated in favour of `prefio::preferences`.")

# If a single vector is passed, add it to a singleton list.
if (typeof(x) == "character") {
x <- list(x)
Expand Down Expand Up @@ -96,6 +99,7 @@ ranked_ballots <- function(x, candidates = NULL, ...) {
#' Write \code{ranked_ballots} to a file.
#'
#' @description
#' Deprecated in favour of fucntionality from the `prefio` package.
#' Writes a set of ballots to a new file. This follows the
#' ballot:count standard, with a header describing the candidates.
#'
Expand All @@ -121,6 +125,8 @@ write_ballots <- function(ballots,
filename = "",
return_lines = FALSE,
suppress = FALSE) {
warning("`write_ballots` is deprecated in favour of `prefio::write_preflib`.")

stopifnot(class(ballots) %in% .ballot_types)

if (filename == "") {
Expand Down Expand Up @@ -187,6 +193,7 @@ count_ballots <- function(ballots, candidates) {
#' Read \code{ranked_ballots} from a file.
#'
#' @description
#' Deprecated in favour of `prefio` plus PrefLib formats.
#' Reads a set of partial IRV ballots from a file. The file is expected to
#' follow the ballot:count standard, with a header describing all participating
#' candidates.
Expand All @@ -196,6 +203,8 @@ count_ballots <- function(ballots, candidates) {
#'
#' @export
read_ballots <- function(file) {
warning("`read_ballots` is deprecated in favour of `prefio::read_preflib`.")

ballots <- list()

# Read the file.
Expand Down Expand Up @@ -240,66 +249,6 @@ read_ballots <- function(file) {
return(ballots)
}

#' @name social_choice
#'
#' @title
#' Compute the outcome of an election.
#'
#' @description
#' \code{social_choice} reads a set of ballots, and computes the
#' outcome of the election. The outcome is described by a vector of winning
#' candidates, along with the elimination order of the losing candidates.
#'
#' @param x
#' The set of ballots for which to compute the outcome of the social
#' choice function.
#'
#' @param \\dots
#' Additional parameters to pass to \code{social_choice}.
#'
#' @examples
#' social_choice(ranked_ballots(list(LETTERS[1], LETTERS[1], LETTERS[2])))
#'
#' @export
social_choice <- function(x, ...) UseMethod("social_choice", x)

#' @name social_choice.ranked_ballots
#'
#' @title
#' Evaluate a social choice function on a set of \code{ranked_ballots}.
#'
#' @description
#' \code{social_choice.ranked_ballots} reads a set of ranked ballots and
#' computes an election outcome. The outcome depends on the chosen
#' social choice function, along with any its associated parameters.
#'
#' @param x
#' The set of ballots for which to compute the outcome of the IRV
#' social choice function.
#'
#' @param n_winners
#' The number of candidates to elect.
#'
#' @param fn
#' The social choice function to use. \describe{
#' \item{\code{"irv"}}{The IRV social choice function.}
#' }
#'
#' @param \\dots Unused.
#'
#' @export
social_choice.ranked_ballots <- function(x,
n_winners = 1,
fn = "irv",
...) {
stopifnot(class(x) %in% .ballot_types)
if (fn == "irv") {
return(social_choice_irv(x, n_winners, attr(x, "candidates"), gseed()))
} else {
stop(paste0("Social choice function `", fn, "` not implemented."))
}
}

# Helper function to get a random seed string to pass to CPP methods
gseed <- function() {
return(paste(sample(LETTERS, 10), collapse = ""))
Expand Down
Loading
Loading