corankco.algorithms package

Subpackages

Submodules

corankco.algorithms.algorithm_choice module

Module that allows to select an algorithm using an enumeration, with the parameters of the algorithm.

class corankco.algorithms.algorithm_choice.Algorithm(value)

Bases: Enum

Enum representing the available ranking algorithms.

BIOCO = 3
BIOCONSERT = 2
BORDACOUNT = 6
COPELANDMETHOD = 7
EXACT = 0
KWIKSORTRANDOM = 4
PARCONS = 1
PICKAPERM = 5
static get_all() List[Algorithm]

Returns a list of all available algorithms.

Returns:

A List of all the available algorithms.

Return type:

List[Algorithm]

static get_all_compatible_with_any_scoring_scheme() List[Algorithm]

Returns a list of algorithms that are compatible with any scoring scheme.

The returned algorithms have implementations that can handle any scoring scheme.

Returns:

A List of all the algorithms compatible with any scoring scheme.

Return type:

List[Algorithm]

class corankco.algorithms.algorithm_choice.AlgorithmEnumeration

Bases: object

Contains a list of classes for all available ranking algorithms.

median_ranking_algorithms = [<class 'corankco.algorithms.exact.exactalgorithm.ExactAlgorithm'>, <class 'corankco.algorithms.parcons.parcons.ParCons'>, <class 'corankco.algorithms.bioconsert.bioconsert.BioConsert'>, <class 'corankco.algorithms.bioconsert.bioco.BioCo'>, <class 'corankco.algorithms.kwiksort.kwiksortrandom.KwikSortRandom'>, <class 'corankco.algorithms.borda.borda.BordaCount'>, <class 'corankco.algorithms.copeland.copeland.CopelandMethod'>, <class 'corankco.algorithms.pickaperm.pickaperm.PickAPerm'>]
corankco.algorithms.algorithm_choice.get_algorithm(alg: Algorithm, parameters: Dict | None = None) RankAggAlgorithm

Returns an instance of the specified algorithm.

Parameters:
  • alg (Algorithm) – The algorithm to instantiate. Must be an instance of the Algorithm enum.

  • parameters (Dict, optional) – The parameters to pass to the algorithm’s constructor. If None, an empty dict will be used.

Returns:

An instance of the specified algorithm.

Return type:

RankAggAlgorithm

Raises:

TypeError – If alg is not an instance of Algorithm, or if parameters is not a dict.

corankco.algorithms.pairwisebasedalgorithm module

Module that implements generic functions about pairwise based rank aggregation algorithm. Module for code factorisation.

class corankco.algorithms.pairwisebasedalgorithm.PairwiseBasedAlgorithm

Bases: object

Class to gather several useful methods for pairwise based algorithms. Class for code factorisation.

static can_be_all_tied(id_elements_to_check: Set[int], cost_matrix: ndarray) bool

Check if all elements in a given set can be tied together with minimal cost.

This method goes through all pairs of elements in the given set, checking the cost of tying them together. If the cost of tying any pair is higher than placing one before or after the other, the function returns False, indicating that not all elements in the set can be tied together with minimal cost.

Parameters:
  • id_elements_to_check (Set[int]) – a set of IDs of the elements to be checked.

  • cost_matrix (ndarray) – a 3D matrix where cost_matrix[i][j][k] denotes the cost of placing i and j in k-th relative position in the consensus.

Returns:

True if all elements can be tied together with minimal cost, False otherwise.

Return type:

bool

static graph_of_elements(positions: ndarray, scoring_scheme: ScoringScheme, weights: ndarray | None = None) Tuple[Graph, ndarray]

Compute the graph of elements, the cost of pairwise relative positions and the set of robust arcs defined in the Future Generation Computer Systems article (as mentioned in the Class docstring)

This function generates a graph of elements as defined in the Future Generation Computer Systems article (as mentioned in the Class docstring) and computes the cost of pairwise relative positions. The latter is a 3D matrix where matrix[i][j][0], then [1], then [2] denote the cost to have i before j, i after j, i tied with j in the consensus according to the scoring scheme.

Parameters:
  • positions – a matrix where pos[i][j] denotes the position of element i in ranking j (-1 if non-ranked)

  • scoring_scheme – the Scoring Scheme to compute the cost matrix

  • weights – a 1D float array that associates a weight for each ranking

Returns:

A tuple containing the Graph of elements defined in the FGCS article, the 3D matrix of costs of

pairwise relative positions

static graph_of_elements_with_robust_arcs(positions: ndarray, scoring_scheme: ScoringScheme, weights: ndarray | None = None) Tuple[Graph, ndarray, Set[Tuple[int, int]]]

Compute the graph of elements, the cost of pairwise relative positions and the set of robust arcs defined in the Future Generation Computer Systems article (as mentioned in the Class docstring)

This function generates a graph of elements as defined in the Future Generation Computer Systems article (as mentioned in the Class docstring) and computes the cost of pairwise relative positions. The latter is a 3D matrix where matrix[i][j][0], then [1], then [2] denote the cost to have i before j, i after j, i tied with j in the consensus according to the scoring scheme.

Parameters:
  • positions – a matrix where pos[i][j] denotes the position of element i in ranking j (-1 if non-ranked)

  • scoring_scheme – the scoring scheme to compute the cost matrix

  • weights – a 1D float array that associates a weight for each ranking

Returns:

A tuple containing the Graph of elements defined in the FGCS article, the 3D matrix of costs of

pairwise relative positions, and the set of the robust arcs defined in the FGCS article

static pairwise_cost_matrix(positions: ndarray, scoring_scheme: ScoringScheme, weights: ndarray | None = None) ndarray

Compute the cost of pairwise relative positions.

This function computes the cost of pairwise relative positions. The latter is a 3D matrix where matrix[i][j][0], then [1], then [2] denote the cost to have i before j, i after j, i tied with j in the consensus according to the scoring scheme.

Parameters:
  • positions – a matrix where pos[i][j] denotes the position of element i in ranking j (-1 if non-ranked)

  • scoring_scheme – the scoring scheme to compute the cost matrix

  • weights – a 1D float array that associates a weight for each ranking

Returns:

The 3D matrix of costs of pairwise relative positions.

corankco.algorithms.rank_aggregation_algorithm module

Module that contains an abstract class to define the functions to be implemented for a rank aggregation algorithm.

class corankco.algorithms.rank_aggregation_algorithm.RankAggAlgorithm

Bases: object

The RankAggAlgorithm class serves as an interface for implementing consensus ranking algorithms. It defines several methods that each consensus ranking algorithm should implement, including the computation of consensus rankings, determining whether a given scoring scheme is relevant for incomplete rankings, and benchmarking the consensus computation time. An algorithm implemented using this interface should also provide a full name and a string representation of itself.

bench_time_consensus(dataset: Dataset, scoring_scheme: ScoringScheme, return_at_most_one_ranking: bool = True, lower_bound_time: float = 1.0) float

Calculate and return the average computation time for a given dataset and scoring scheme

Parameters:
  • dataset (Dataset) – The dataset of rankings to be aggregated.

  • scoring_scheme (ScoringScheme) – The scoring scheme to be used for calculating consensus.

  • return_at_most_one_ranking (bool) – If True, the algorithm should return at most one ranking.

  • lower_bound_time (float) – The lower bound on the total computation time.

Returns:

The average computation time.

Return type:

float

compute_consensus_rankings(dataset: Dataset, scoring_scheme: ScoringScheme, return_at_most_one_ranking: bool = True, bench_mode: bool = False) Consensus

Calculate and return the consensus rankings based on the given dataset and scoring scheme.

Parameters:
  • dataset (Dataset) – The dataset of rankings to be aggregated.

  • scoring_scheme (ScoringScheme) – The scoring scheme to be used for calculating consensus.

  • return_at_most_one_ranking (bool) – If True, the algorithm should return at most one ranking.

  • bench_mode (bool) – If True, the algorithm may return additional information for benchmarking purposes.

Returns:

Consensus rankings. If the algorithm is unable to provide multiple consensuses or

return_at_most_one_ranking is True, a single consensus ranking is returned. :rtype: Consensus :raise ScoringSchemeNotHandledException: When the algorithm cannot compute the consensus because the implementation does not support the given scoring scheme.

get_full_name() str

Get the full name of the algorithm.

Returns:

The full name of the algorithm.

Return type:

str

is_scoring_scheme_relevant_when_incomplete_rankings(scoring_scheme: ScoringScheme) bool

Determine whether the provided scoring scheme is relevant when dealing with incomplete rankings.

Parameters:

scoring_scheme (ScoringScheme) – The scoring scheme to be evaluated.

Returns:

True if the scoring scheme is relevant for incomplete rankings, False otherwise.

Return type:

bool

exception corankco.algorithms.rank_aggregation_algorithm.ScoringSchemeNotHandledException

Bases: Exception

Custom exception, when the scoring scheme cannot be used with the chosen algorithm given the dataset. Note that this exception cannot be raised if the dataset is complete.

Module contents

Module that allows to choose a rank aggregation algorithm from an enumeration.