Skip to content

Instantly share code, notes, and snippets.

@sprgchma
Last active March 25, 2020 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sprgchma/fa0b3f18fcef66fefed2ee9e61c3f647 to your computer and use it in GitHub Desktop.
Save sprgchma/fa0b3f18fcef66fefed2ee9e61c3f647 to your computer and use it in GitHub Desktop.
Parliament coalition
using System;
namespace Sketch {
public class Parliament {
public static string[] parties = new string[] {
"Likud",
"KahulLaval",
"JointList",
"Shas",
"YahadutHatora",
"IsrealBeitenu",
"AvodaGesherMeretz",
"Yemina", // 8
};
public static int[] seats = new int[] {
36,
33,
15,
9,
7,
7,
7,
6,
};
public static double[,] likes = new double[,] {
// likud, kahul, joint, shas, yahdut, beitenu, avoda, yemina
{ 1.0, -0.5, -1.0, 0.95, 0.95, -0.2, -0.8, 0.95}, // likud
{ -0.9, 1.0, -0.6, -0.5, -0.5, 0.5, 0.8, -0.2}, // kahul
{ -0.9, -0.3, 1.0, -0.6, -0.6, -1.0, -0.2, -1.0}, // joint
{ 0.96, -0.7, -0.6, 1.0, 0.9, -1.0, -0.7, 0.8}, // shas
{ 0.97, -0.6, -0.6, 0.92, 1.0, -1.0, -0.6, 0.7}, // yahadut
{ -0.4, -0.1, -1.0, -0.99, -0.99, 1.0, -0.6, 0.1}, // beitenu
{ -0.95, 0.98, 0.3, -0.89, -0.89, -0.01, 1.0, -0.75}, // avoda
{ 0.999, -0.92, -1.0, 0.86, 0.85, -0.3, -0.4, 1.0}, // yemina
};
public static void Main() {
int states=1<<parties.Length; //2^parties.Length
double bestRank=-1e10;
int bestState=0;
/* state is a bit array, 1-party is in coalision, 0-party is in opposition */
for (int state=1; state<states; state+=1) {
if (GetSeats(state)<61) continue;
var rank=GetRank(state);
if (rank>bestRank) { bestRank=rank; bestState=state; }
//PrintParties(state, rank);
}
if (bestState==0) Console.WriteLine("New election, please!");
else PrintParties(bestState, bestRank);
}
public static int GetSeats(int state) {
int ttl=0;
for (int ix=0; state>0; state=state>>1, ix+=1) {
if ((state&1)==0) continue;
ttl+=seats[ix];
}
return ttl;
}
public static double GetRank(int state) {
double rank=0; int ttl=0;
for (int ix=0; state>0; state=state>>1, ix+=1) {
if ((state&1)==0) continue;
ttl+=1;
/* exclude current party from the state */
int otherState=state>>1;
for (int otherIx=ix+1; otherState>0; otherState=otherState>>1, otherIx+=1) {
if ((otherState&1)==0) continue;
rank+=likes[ix,otherIx]+likes[otherIx,ix];
}
}
return rank/(0.5*ttl*(ttl-1)); /* normalize by count of parties pairs */
}
public static void PrintParties(int state, double rank) {
Console.Write("Rank: {0}, Seats: {1}, Parties:", rank, GetSeats(state));
for (int ix=0; state>0; state=state>>1, ix+=1) {
if ((state&1)==0) continue;
Console.Write(" {0}", parties[ix]);
}
Console.WriteLine("");
}
}
}
Rank: 0.6029, Seats: 65, Parties: Likud Shas YahadutHatora IsrealBeitenu Yemina
@sprgchma
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment