-- | WARNING: the 'MajorityGauge' is a simplified 'MajorityValue' -- which is sufficient to determine the 'MajorityRanking' -- when the number of judges is large. -- It is an approximation, it can perfectly lead to a wrong ranking -- wrt. the 'MajorityRanking' done by using 'majorityValue'. module Majority.Gauge where import Data.Bool import Data.Eq (Eq(..)) import Data.Function (($), (.)) import Data.Functor ((<$>)) import Data.Maybe (Maybe(..), listToMaybe) import Data.Ord (Ord(..), Ordering(..), Down(..)) import Data.Tuple (snd) import Prelude (Num(..)) import Text.Show (Show(..)) import qualified Data.HashMap.Strict as HM import qualified Data.List as List import qualified Data.Map.Strict as Map import Majority.Merit -- * Type 'MajorityGauge' -- | The 'MajorityGauge' is a simplification of the 'majorityValue' -- from which may be deduced the 'majorityRanking' -- among the propositions in many cases; -- in particular, when there are many judges. -- -- However, when two propositions are tied with the same 'MajorityGauge', -- they are not necessarily tied according to their 'majorityValue's. data MajorityGauge g = MajorityGauge { mgLower :: Share -- ^ Number of 'grade's given which are worse than 'mgGrade'. , mgGrade :: g -- ^ 'majorityGrade'. , mgHigher :: Share -- ^ Number of 'grade's given which are better than 'mgGrade'. } deriving (Eq) instance Show g => Show (MajorityGauge g) where showsPrec p (MajorityGauge w g b) = showsPrec p (w,g,b) -- ** Type 'Sign' data Sign = Minus | Plus deriving (Eq, Show) -- | If 'mgHigher' is higher than 'mgLower' -- then the 'majorityGrade' is completed by a 'Plus'; -- otherwise the 'majorityGrade' is completed by a 'Minus'. -- -- This indicates the side of the next 'majorityGrade' -- which is different than the current one: -- 'Minus' when it is lower and 'Plus' otherwise. mgSign :: MajorityGauge g -> Sign mgSign g = if mgHigher g > mgLower g then Plus else Minus -- | The 'MajorityGauge'-ranking, first tries to rank -- according to the 'majorityGrade' 'mgGrade'. -- -- If both 'MajorityGauge's have the same 'mgGrade', -- it tries to rank according to the 'mgSign' of both 'MajorityGauge's: -- a 'Plus' is ahead of a 'Minus'. -- -- If both 'mgSign' are 'Plus', -- the one having the higher 'mgHigher' is ahead, -- or if both 'mgSign' are 'Minus', -- the one having the higher 'mgLower' is behind. -- -- Otherwise, the 'MajorityGauge'-ranking is a tie. instance Ord g => Ord (MajorityGauge g) where x `compare` y = case mgGrade x `compare` mgGrade y of EQ -> case (mgSign x, mgSign y) of (Minus, Plus) -> LT (Plus , Minus) -> GT (Plus , Plus) -> mgHigher x `compare` mgHigher y (Minus, Minus) -> mgLower y `compare` mgLower x o -> o majorityGauge :: Ord grade => Merit grade -> Maybe (MajorityGauge grade) majorityGauge = listToMaybe . majorityGauges majorityGauges :: Ord grade => Merit grade -> [MajorityGauge grade] majorityGauges (Merit m) = go Map.empty m where go done gs = case snd (Map.foldlWithKey untilMajGrade (0,[]) gs) of [] -> [] (mg,c):_ -> add mg done:go (Map.insert (mgGrade mg) c done) (Map.delete (mgGrade mg) gs) where add = Map.foldrWithKey $ \g c (MajorityGauge w mg b) -> if g >= mg then MajorityGauge w mg (b+c) else MajorityGauge (w+c) mg b total = List.sum gs untilMajGrade (t,[]) g c | 2*tc >= total = (tc,[(MajorityGauge t g 0,c)]) | otherwise = (tc,[]) where tc = t+c untilMajGrade (t,(mg,c):_) _g c' = (t,[(mg{mgHigher=mgHigher mg + c'},c)]) -- * Type 'MajorityGaugeRanking' type MajorityGaugeRanking choice grade = [(choice, [MajorityGauge grade])] majorityGaugesByChoice :: Ord grade => MeritByChoice choice grade -> HM.HashMap choice [MajorityGauge grade] majorityGaugesByChoice (MeritByChoice ms) = majorityGauges <$> ms majorityGaugeRanking :: Ord grade => MeritByChoice choice grade -> MajorityGaugeRanking choice grade majorityGaugeRanking = List.sortOn (Down . snd) . HM.toList . majorityGaugesByChoice