Priority

class whalrus.Priority(name: str)[source]

A priority setting, i.e. a policy to break ties and indifference classes.

Parameters

name (str) – The name of this priority setting.

UNAMBIGUOUS

Shortcut for PriorityUnambiguous.

ABSTAIN

Shortcut for PriorityAbstain.

ASCENDING

Shortcut for PriorityAscending.

DESCENDING

Shortcut for PriorityDescending.

RANDOM

Shortcut for PriorityRandom.

Examples

Typical usage:

>>> priority = Priority.ASCENDING
>>> priority.choice({'c', 'a', 'b'})
'a'
>>> priority.sort({'c', 'a', 'b'})
['a', 'b', 'c']
choice(x: Union[set, list], reverse: bool = False) → object[source]

Choose an element from a list, set, etc.

Parameters
  • x (list, set, etc.) – The list, set, etc where the element is to be chosen.

  • reverse (bool) – If False (default), then we choose the “first” or “best” element in this priority order. For example, if this is the ascending priority, we choose the lowest element. If True, then we choose the “last” or “worst” element. This is used, for example, in RuleVeto.

Returns

The chosen element (or None). When x is empty, return None. When x has one element, return this element.

Return type

object

compare(c, d) → int[source]

Compare two candidates.

Parameters
  • c (candidate) –

  • d (candidate.) –

Returns

0 if c = d, -1 if the tie is broken in favor of c over d, 1 otherwise.

Return type

int

sort(x: Union[set, list], reverse: bool = False) → Optional[list][source]

Sort a list, set, etc.

The original list x is not modified.

Parameters
  • x (list, set, etc.) –

  • reverse (bool) – If True, we use the reverse priority order.

Returns

A sorted list (or None).

Return type

list or None

sort_pairs_rp(x: Union[set, list], reverse: bool = False) → Optional[list][source]

Sort a list, set, etc. of pairs of candidates (for Ranked Pairs).

By default, it is in the normal priority order for the first element of the pair, and in the reverse priority order for the second element of the pair.

The original list x is not modified.

Parameters
  • x (list, set, etc.) –

  • reverse (bool) – If True, we use the reverse priority order.

Returns

A sorted list (or None).

Return type

list or None