# OK to post homework # RDB, 2022-03-27, HW16 # 1. # 2. read "C16.txt": IsCondorcet := proc(P) local v, n: v := nops(P): n := nops(P[1]): not member(n - 1, SV(Tour(P))): end: # 3. ExactCond := proc(n, v) option remember: local count: count := coeff(add(IsCondorcet(p), p in RP(n, v)), true): count / n!^v end: # 4. EstCond := proc(n, v, K) local count: count := coeff(add(IsCondorcet(RandP(n, v)), k=1..K), true): evalf(count / K): end: # 5. Borda := proc(P) local n, points, ballot, i: n := nops(P[1]): points := [0 $ n]: for ballot in P do for i from 1 to n do points[ballot[i]] += n - i: od: od: points: end: # 6 / 7. with(ListTools): Ranks := proc(P, f) local ranking, vals, classes, i, v: ranking := f(P): # I'm going to use the fact that sets are sorted by default in my Maple. I # don't love this. vals := {op(ranking)}: classes := table(): for i from 1 to nops(ranking) do v := ranking[i]: if not assigned(classes[v]) then classes[v] := {} fi: classes[v] := classes[v] union {i}: od: Reverse([entries(classes, 'nolist')]): end: CondorcetRank := proc(P) Ranks(P, x -> SV(Tour(x))): end: BordaRank := proc(P) Ranks(P, x -> Borda(x)): end: