Skip to content

Instantly share code, notes, and snippets.

@ManDeJan
Last active March 22, 2021 11:07
Show Gist options
  • Save ManDeJan/543a9f63be76f26be4c1f8637de3aeb9 to your computer and use it in GitHub Desktop.
Save ManDeJan/543a9f63be76f26be4c1f8637de3aeb9 to your computer and use it in GitHub Desktop.
Tooltje om mogelijke coalities te checken
from itertools import combinations, chain
from compatibility import Voorkeur, Voorkeuren
partijen = {
'VVD': 35,
'D66': 23,
'PVV': 17,
'CDA': 15,
'SP': 9,
'PvdA': 9,
'GL': 8,
'FvD': 8,
'PvdD': 6,
'CU': 5,
'VOLT': 3,
'JA21': 3,
'SGP': 3,
'DENK': 3,
'50+': 1,
'BBB': 1,
'BIJ1': 1,
}
totaal_zetels = sum(partijen.values())
meerderheid = totaal_zetels // 2 + 1
max_partijen_in_coalitie = 7 # len(partijen)
kan_niet_met_minder = lambda partij_comb, aantal: aantal - min(partij_comb) < meerderheid
coalitie_combs = list(filter(lambda p: (zetels := dict(p).values()) and # krijg een lijst van zetelaantallen in combinatie
(aantal := sum(zetels)) >= meerderheid and # check of zetels in combinatie een meerderheid haalt
kan_niet_met_minder(zetels, aantal), # als een coalitie zonder de kleinste partij nogsteeds een meerderheid haalt filteren we dit ook weg
chain( # maak alle combinaties tot N partijen die samen een meerderheid vormen
*[combinations(partijen.items(), n)
for n in range(2, max_partijen_in_coalitie + 1)])))
realistic_combs = list(filter(lambda c: not c[1]['NEE'], # partijen die absoluut niet willen regeren halen we weg
[(comb, # maak een tuple van de combinatie en de voorkeurs aantallen per combinatie
{e.name: [Voorkeuren[paar]
for paar in combinations(dict(comb).keys(), 2)].count(e) # list comprehension in een dict comprehension in een list comprehension kek
for e in Voorkeur})
for comb in coalitie_combs]))
coalitie_combs_len = len(coalitie_combs)
realistic_combs_len = len(realistic_combs)
realistic_combs_sorted = sorted(realistic_combs, key=lambda c: c[1]['MISS'])
print(f'Aantal mogelijke coalities (tot aan {max_partijen_in_coalitie} partijen): {coalitie_combs_len}')
for partij in partijen.items():
count = sum([partij in coalitie for coalitie in coalitie_combs])
print(f'{partij[0]:4} komt in {count:4}/{coalitie_combs_len} voor ({count/coalitie_combs_len*100:6.2f}%)')
print('\n----------------------------------------\n')
print(f'Aantal realistische coalities (tot aan {max_partijen_in_coalitie} partijen): {realistic_combs_len}')
for partij in partijen.items():
count = sum([partij in coalitie for (coalitie, _) in realistic_combs])
print(f'{partij[0]:4} komt in {count:3}/{realistic_combs_len} voor ({count/realistic_combs_len*100:6.2f}%)')
print('\n----------------------------------------\n')
print('Coalities gesorteerd op waarschijnlijkheid')
for comb in realistic_combs_sorted:
print(f'{", ".join([partij[0] for partij in comb[0]]):34}| {sum(dict(comb[0]).values()):2} zetels | {comb[1]}')
from enum import Flag
Voorkeur = Flag('Voorkeur', 'JA NEE MISS')
# Bron: https://www.volkskrant.nl/kijkverder/2021/coalitiewijzer-2021~v424905/#/
Voorkeuren = {
('VVD', 'D66' ): Voorkeur.JA,
('VVD', 'PVV' ): Voorkeur.NEE,
('VVD', 'CDA' ): Voorkeur.JA,
('VVD', 'SP' ): Voorkeur.MISS,
('VVD', 'PvdA'): Voorkeur.MISS,
('VVD', 'GL' ): Voorkeur.MISS,
('VVD', 'FvD' ): Voorkeur.NEE,
('VVD', 'PvdD'): Voorkeur.NEE,
('VVD', 'CU' ): Voorkeur.JA,
('VVD', 'VOLT'): Voorkeur.MISS,
('VVD', 'JA21'): Voorkeur.MISS,
('VVD', 'SGP' ): Voorkeur.JA,
('VVD', 'DENK'): Voorkeur.MISS,
('VVD', '50+' ): Voorkeur.JA,
('VVD', 'BBB' ): Voorkeur.MISS,
('VVD', 'BIJ1' ): Voorkeur.NEE,
('D66', 'PVV' ): Voorkeur.NEE,
('D66', 'CDA' ): Voorkeur.JA,
('D66', 'SP' ): Voorkeur.MISS,
('D66', 'PvdA'): Voorkeur.JA,
('D66', 'GL' ): Voorkeur.JA,
('D66', 'FvD' ): Voorkeur.NEE,
('D66', 'PvdD'): Voorkeur.MISS,
('D66', 'CU' ): Voorkeur.JA,
('D66', 'VOLT'): Voorkeur.JA,
('D66', 'JA21'): Voorkeur.MISS,
('D66', 'SGP' ): Voorkeur.MISS,
('D66', 'DENK'): Voorkeur.MISS,
('D66', '50+' ): Voorkeur.MISS,
('D66', 'BBB' ): Voorkeur.NEE,
('D66', 'BIJ1' ): Voorkeur.MISS,
('PVV', 'CDA' ): Voorkeur.NEE,
('PVV', 'SP' ): Voorkeur.NEE,
('PVV', 'PvdA'): Voorkeur.NEE,
('PVV', 'GL' ): Voorkeur.NEE,
('PVV', 'FvD' ): Voorkeur.JA,
('PVV', 'PvdD'): Voorkeur.NEE,
('PVV', 'CU' ): Voorkeur.NEE,
('PVV', 'VOLT'): Voorkeur.NEE,
('PVV', 'JA21'): Voorkeur.JA,
('PVV', 'SGP' ): Voorkeur.MISS,
('PVV', 'DENK'): Voorkeur.NEE,
('PVV', '50+' ): Voorkeur.MISS,
('PVV', 'BBB' ): Voorkeur.JA,
('PVV', 'BIJ1' ): Voorkeur.NEE,
('CDA', 'SP' ): Voorkeur.MISS,
('CDA', 'PvdA'): Voorkeur.JA,
('CDA', 'GL' ): Voorkeur.JA,
('CDA', 'FvD' ): Voorkeur.NEE,
('CDA', 'PvdD'): Voorkeur.NEE,
('CDA', 'CU' ): Voorkeur.JA,
('CDA', 'VOLT'): Voorkeur.MISS,
('CDA', 'JA21'): Voorkeur.MISS,
('CDA', 'SGP' ): Voorkeur.JA,
('CDA', 'DENK'): Voorkeur.MISS,
('CDA', '50+' ): Voorkeur.JA,
('CDA', 'BBB' ): Voorkeur.JA,
('CDA', 'BIJ1' ): Voorkeur.MISS,
('SP', 'PvdA'): Voorkeur.JA,
('SP', 'GL' ): Voorkeur.JA,
('SP', 'FvD' ): Voorkeur.NEE,
('SP', 'PvdD'): Voorkeur.JA,
('SP', 'CU' ): Voorkeur.JA,
('SP', 'VOLT'): Voorkeur.NEE,
('SP', 'JA21'): Voorkeur.NEE,
('SP', 'SGP' ): Voorkeur.MISS,
('SP', 'DENK'): Voorkeur.MISS,
('SP', '50+' ): Voorkeur.JA,
('SP', 'BBB' ): Voorkeur.MISS,
('SP', 'BIJ1' ): Voorkeur.JA,
('PvdA', 'GL' ): Voorkeur.JA,
('PvdA', 'FvD' ): Voorkeur.NEE,
('PvdA', 'PvdD'): Voorkeur.MISS,
('PvdA', 'CU' ): Voorkeur.JA,
('PvdA', 'VOLT'): Voorkeur.JA,
('PvdA', 'JA21'): Voorkeur.MISS,
('PvdA', 'SGP' ): Voorkeur.MISS,
('PvdA', 'DENK'): Voorkeur.MISS,
('PvdA', '50+' ): Voorkeur.JA,
('PvdA', 'BBB' ): Voorkeur.MISS,
('PvdA', 'BIJ1' ): Voorkeur.MISS,
('GL', 'FvD' ): Voorkeur.NEE,
('GL', 'PvdD'): Voorkeur.JA,
('GL', 'CU' ): Voorkeur.JA,
('GL', 'VOLT'): Voorkeur.JA,
('GL', 'JA21'): Voorkeur.NEE,
('GL', 'SGP' ): Voorkeur.NEE,
('GL', 'DENK'): Voorkeur.MISS,
('GL', '50+' ): Voorkeur.MISS,
('GL', 'BBB' ): Voorkeur.NEE,
('GL', 'BIJ1' ): Voorkeur.JA,
('FvD', 'PvdD'): Voorkeur.NEE,
('FvD', 'CU' ): Voorkeur.NEE,
('FvD', 'VOLT'): Voorkeur.NEE,
('FvD', 'JA21'): Voorkeur.JA,
('FvD', 'SGP' ): Voorkeur.JA,
('FvD', 'DENK'): Voorkeur.NEE,
('FvD', '50+' ): Voorkeur.MISS,
('FvD', 'BBB' ): Voorkeur.MISS,
('FvD', 'BIJ1' ): Voorkeur.NEE,
('PvdD', 'CU' ): Voorkeur.MISS,
('PvdD', 'VOLT'): Voorkeur.MISS,
('PvdD', 'JA21'): Voorkeur.NEE,
('PvdD', 'SGP' ): Voorkeur.NEE,
('PvdD', 'DENK'): Voorkeur.MISS,
('PvdD', '50+' ): Voorkeur.MISS,
('PvdD', 'BBB' ): Voorkeur.NEE,
('PvdD', 'BIJ1' ): Voorkeur.JA,
('CU', 'VOLT'): Voorkeur.NEE,
('CU', 'JA21'): Voorkeur.MISS,
('CU', 'SGP' ): Voorkeur.JA,
('CU', 'DENK'): Voorkeur.MISS,
('CU', '50+' ): Voorkeur.MISS,
('CU', 'BBB' ): Voorkeur.MISS,
('CU', 'BIJ1' ): Voorkeur.NEE,
('VOLT', 'JA21'): Voorkeur.NEE,
('VOLT', 'SGP' ): Voorkeur.NEE,
('VOLT', 'DENK'): Voorkeur.MISS,
('VOLT', '50+' ): Voorkeur.MISS,
('VOLT', 'BBB' ): Voorkeur.NEE,
('VOLT', 'BIJ1' ): Voorkeur.MISS,
('JA21', 'SGP' ): Voorkeur.JA,
('JA21', 'DENK'): Voorkeur.NEE,
('JA21', '50+' ): Voorkeur.MISS,
('JA21', 'BBB' ): Voorkeur.MISS,
('JA21', 'BIJ1' ): Voorkeur.NEE,
('SGP', 'DENK'): Voorkeur.NEE,
('SGP', '50+' ): Voorkeur.MISS,
('SGP', 'BBB' ): Voorkeur.JA,
('SGP', 'BIJ1' ): Voorkeur.NEE,
('DENK', '50+' ): Voorkeur.MISS,
('DENK', 'BBB' ): Voorkeur.MISS,
('DENK', 'BIJ1' ): Voorkeur.JA,
('50+', 'BBB' ): Voorkeur.MISS,
('50+', 'BIJ1' ): Voorkeur.MISS,
('BBB', 'BIJ1' ): Voorkeur.MISS,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment