]> Git — Sourcephile - majurity.git/blob - hjugement/Majority/Gauge.hs
protocol: bump versions
[majurity.git] / hjugement / Majority / Gauge.hs
1 -- | WARNING: the 'MajorityGauge' is a simplified 'MajorityValue'
2 -- which is sufficient to determine the 'MajorityRanking'
3 -- when the number of judges is large.
4 -- It is an approximation, it can perfectly lead to a wrong ranking
5 -- wrt. the 'MajorityRanking' done by using 'majorityValue'.
6 module Majority.Gauge where
7
8 import Data.Bool
9 import Data.Eq (Eq(..))
10 import Data.Function (($), (.))
11 import Data.Functor ((<$>))
12 import Data.Maybe (Maybe(..), listToMaybe)
13 import Data.Ord (Ord(..), Ordering(..), Down(..))
14 import Data.Tuple (snd)
15 import Prelude (Num(..))
16 import Text.Show (Show(..))
17 import qualified Data.HashMap.Strict as HM
18 import qualified Data.List as List
19 import qualified Data.Map.Strict as Map
20
21 import Majority.Merit
22
23 -- * Type 'MajorityGauge'
24 -- | The 'MajorityGauge' is a simplification of the 'majorityValue'
25 -- from which may be deduced the 'majorityRanking'
26 -- among the propositions in many cases;
27 -- in particular, when there are many judges.
28 --
29 -- However, when two propositions are tied with the same 'MajorityGauge',
30 -- they are not necessarily tied according to their 'majorityValue's.
31 data MajorityGauge g
32 = MajorityGauge
33 { mgLower :: Share -- ^ Number of 'grade's given which are worse than 'mgGrade'.
34 , mgGrade :: g -- ^ 'majorityGrade'.
35 , mgHigher :: Share -- ^ Number of 'grade's given which are better than 'mgGrade'.
36 } deriving (Eq)
37 instance Show g => Show (MajorityGauge g) where
38 showsPrec p (MajorityGauge w g b) = showsPrec p (w,g,b)
39
40 -- ** Type 'Sign'
41 data Sign = Minus | Plus
42 deriving (Eq, Show)
43
44 -- | If 'mgHigher' is higher than 'mgLower'
45 -- then the 'majorityGrade' is completed by a 'Plus';
46 -- otherwise the 'majorityGrade' is completed by a 'Minus'.
47 --
48 -- This indicates the side of the next 'majorityGrade'
49 -- which is different than the current one:
50 -- 'Minus' when it is lower and 'Plus' otherwise.
51 mgSign :: MajorityGauge g -> Sign
52 mgSign g = if mgHigher g > mgLower g then Plus else Minus
53
54 -- | The 'MajorityGauge'-ranking, first tries to rank
55 -- according to the 'majorityGrade' 'mgGrade'.
56 --
57 -- If both 'MajorityGauge's have the same 'mgGrade',
58 -- it tries to rank according to the 'mgSign' of both 'MajorityGauge's:
59 -- a 'Plus' is ahead of a 'Minus'.
60 --
61 -- If both 'mgSign' are 'Plus',
62 -- the one having the higher 'mgHigher' is ahead,
63 -- or if both 'mgSign' are 'Minus',
64 -- the one having the higher 'mgLower' is behind.
65 --
66 -- Otherwise, the 'MajorityGauge'-ranking is a tie.
67 instance Ord g => Ord (MajorityGauge g) where
68 x `compare` y =
69 case mgGrade x `compare` mgGrade y of
70 EQ ->
71 case (mgSign x, mgSign y) of
72 (Minus, Plus) -> LT
73 (Plus , Minus) -> GT
74 (Plus , Plus) -> mgHigher x `compare` mgHigher y
75 (Minus, Minus) -> mgLower y `compare` mgLower x
76 o -> o
77
78 majorityGauge :: Ord grade => Merit grade -> Maybe (MajorityGauge grade)
79 majorityGauge = listToMaybe . majorityGauges
80
81 majorityGauges :: Ord grade => Merit grade -> [MajorityGauge grade]
82 majorityGauges (Merit m) = go Map.empty m
83 where
84 go done gs = case snd (Map.foldlWithKey untilMajGrade (0,[]) gs) of
85 [] -> []
86 (mg,c):_ -> add mg done:go (Map.insert (mgGrade mg) c done) (Map.delete (mgGrade mg) gs)
87 where
88 add = Map.foldrWithKey $ \g c (MajorityGauge w mg b) ->
89 if g >= mg then MajorityGauge w mg (b+c)
90 else MajorityGauge (w+c) mg b
91 total = List.sum gs
92 untilMajGrade (t,[]) g c | 2*tc >= total = (tc,[(MajorityGauge t g 0,c)])
93 | otherwise = (tc,[])
94 where tc = t+c
95 untilMajGrade (t,(mg,c):_) _g c' = (t,[(mg{mgHigher=mgHigher mg + c'},c)])
96
97 -- * Type 'MajorityGaugeRanking'
98 type MajorityGaugeRanking choice grade = [(choice, [MajorityGauge grade])]
99
100 majorityGaugesByChoice :: Ord grade => MeritByChoice choice grade -> HM.HashMap choice [MajorityGauge grade]
101 majorityGaugesByChoice (MeritByChoice ms) = majorityGauges <$> ms
102
103 majorityGaugeRanking :: Ord grade => MeritByChoice choice grade -> MajorityGaugeRanking choice grade
104 majorityGaugeRanking = List.sortOn (Down . snd) . HM.toList . majorityGaugesByChoice