Source code for whalrus.rules.rule_simplified_dodgson

# -*- coding: utf-8 -*-
Copyright Sylvain Bouveret, Yann Chevaleyre and Fran├žois Durand,,

This file is part of Whalrus.

Whalrus is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Whalrus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Whalrus.  If not, see <>.
from whalrus.rules.rule_score_num import RuleScoreNum
from whalrus.converters_ballot.converter_ballot_to_order import ConverterBallotToOrder
from whalrus.utils.utils import cached_property, NiceDict, convert_number
from whalrus.converters_ballot.converter_ballot import ConverterBallot
from whalrus.matrices.matrix import Matrix
from whalrus.matrices.matrix_weighted_majority import MatrixWeightedMajority

[docs]class RuleSimplifiedDodgson(RuleScoreNum): """ Simplified Dodgson rule. The score of a candidate is the sum of the negative non-diagonal coefficient on its raw of :attr:`matrix_weighted_majority_`. Parameters ---------- args Cf. parent class. converter : ConverterBallot Default: :class:`ConverterBallotToOrder`. matrix_weighted_majority : Matrix Default: :class:`MatrixWeightedMajority` with ``antisymmetric=True``. kwargs Cf. parent class. Examples -------- >>> rule = RuleSimplifiedDodgson(ballots=['a > b > c', 'b > a > c', 'c > a > b'], ... weights=[3, 3, 2]) >>> rule.matrix_weighted_majority_.as_array_ array([[0, Fraction(1, 4), Fraction(1, 2)], [Fraction(-1, 4), 0, Fraction(1, 2)], [Fraction(-1, 2), Fraction(-1, 2), 0]], dtype=object) >>> rule.scores_ {'a': 0, 'b': Fraction(-1, 4), 'c': -1} >>> rule.winner_ 'a' """ def __init__(self, *args, converter: ConverterBallot = None, matrix_weighted_majority: Matrix = None, **kwargs): if converter is None: converter = ConverterBallotToOrder() if matrix_weighted_majority is None: matrix_weighted_majority = MatrixWeightedMajority(antisymmetric=True) self.matrix_weighted_majority = matrix_weighted_majority super().__init__(*args, converter=converter, **kwargs) @cached_property def matrix_weighted_majority_(self): """Matrix: The weighted majority matrix (once computed with the given profile). """ return self.matrix_weighted_majority(self.profile_converted_) @cached_property def scores_(self) -> NiceDict: matrix = self.matrix_weighted_majority_ return NiceDict({ c: convert_number(sum([v for (i, j), v in matrix.as_dict_.items() if i == c and j != c and v < 0])) for c in matrix.candidates_})