// Census exact des 12 profils purs et frontiere mu(be) = 1/3
public static class ExactCensus
{
public static int UExact(string h, int joueur, Dictionary<(string iset, string action), double> sigma)
{
var noeud = ARBRE[h];
if (noeud.type == "terminal")
{
var p = PAYOFFS_ALPHA[h];
return joueur == 1 ? p.u1 : joueur == 2 ? p.u2 : p.u3;
}
var choisi = noeud.enfants.First(x => sigma[(noeud.infoSet, x.action)] == 1.0);
return UExact(choisi.enfant, joueur, sigma);
}
// Ecarts d'utilite exacts (payoffs entiers) entre l'action choisie et chaque
// alternative, noeud par noeud de l'ensemble d'information : le WSE exact
// raisonne sur ces ecarts sans re-executer de payoff espere.
public static Dictionary<string, Dictionary<string, int>> Ecarts(
string iset,
int joueur,
Dictionary<(string iset, string action), double> sigma)
{
List<string> hs = InfoSets()[iset];
string actionChoisie = ARBRE[hs[0]].enfants
.First(x => sigma[(iset, x.action)] == 1.0).action;
var resultat = new Dictionary<string, Dictionary<string, int>>();
foreach (var alternative in ARBRE[hs[0]].enfants.Where(x => x.action != actionChoisie))
{
resultat[alternative.action] = hs.ToDictionary(
h => h,
h => UExact(MoteurEval.Hist(h, actionChoisie), joueur, sigma)
- UExact(MoteurEval.Hist(h, alternative.action), joueur, sigma));
}
return resultat;
}
// Faisabilite : sur un ensemble ATTEINT, la contrainte est une constante ;
// sur un ensemble LIBRE, elle devient lineaire en mu et se satisfait a la
// frontiere (d'ou le seuil mu(be) = 1/3 du titre de la cellule).
public static bool Faisable(
string iset,
int joueur,
Dictionary<(string iset, string action), double> sigma,
Dictionary<string, double?> baseMu)
{
List<string> hs = InfoSets()[iset];
Dictionary<string, Dictionary<string, int>> ecarts = Ecarts(iset, joueur, sigma);
foreach (Dictionary<string, int> contrainte in ecarts.Values)
{
if (baseMu[hs[0]].HasValue)
{
// Ensemble ATTEINT : la contrainte est une constante, vraie ou fausse.
double valeur = hs.Sum(h => baseMu[h].Value * contrainte[h]);
if (valeur < 0) { return false; }
}
else if (ecarts.Count > 1)
{
// Plusieurs actions alternatives sur un ensemble LIBRE : cas non couvert ici.
return false;
}
else
{
// Ensemble LIBRE : la contrainte est faisable ssi sa borne haute est non negative.
if (contrainte.Values.Max() < 0) { return false; }
}
}
return true;
}
// Etiquette courte d'un historique : "a|d" devient "ad".
public static string Etiq(string h)
{
return h.Replace("|", "");
}
public static string Forme(string profil, Dictionary<(string iset, string action), double> sigma)
{
Dictionary<string, int> c = Ecarts("I3", 3, sigma).Values.First();
return string.Join(" ", c.Select(x =>
(x.Value >= 0 ? "+" : "") + x.Value + "*mu[" + Etiq(x.Key) + "]"));
}
// Diagnostic par ensemble d'information : type (fixe / libre) et ecart signe.
public static List<string> Diagnostic(
Dictionary<(string iset, string action), double> sigma,
Dictionary<string, double?> baseMu)
{
var lignes = new List<string>();
foreach (var kv in InfoSets())
{
string iset = kv.Key;
List<string> hs = kv.Value;
int joueur = ARBRE[hs[0]].joueur;
string choisie = ARBRE[hs[0]].enfants.First(x => sigma[(iset, x.action)] == 1.0).action;
bool atteint = baseMu[hs[0]].HasValue;
Dictionary<string, Dictionary<string, int>> ecarts = Ecarts(iset, joueur, sigma);
foreach (var alternative in ARBRE[hs[0]].enfants.Where(x => x.action != choisie))
{
Dictionary<string, int> c = ecarts[alternative.action];
string forme = string.Join(" ", c.Select(x =>
(x.Value >= 0 ? "+" : "") + x.Value + "*mu[" + Etiq(x.Key) + "]"));
if (atteint)
{
double valeur = 0.0;
foreach (string h in hs)
{
if (baseMu[h].HasValue) { valeur += baseMu[h].Value * c[h]; }
}
lignes.Add(iset + " [fixe] ecart " + choisie + " - " + alternative.action +
" = " + Num(valeur, "F2") + " (exigence : " + choisie + " >= " + alternative.action +
") -> " + (valeur >= 0 ? "satisfaite" : "VIOLATION"));
}
else
{
bool faisable = c.Values.Max() >= 0;
lignes.Add(iset + " [libre] contrainte " + forme + " >= 0 -> " +
(faisable ? "faisable" : "impossible"));
}
}
}
return lignes;
}
public static (string sens, double seuil) SeuilMuBe(
string profil,
Dictionary<(string iset, string action), double> sigma)
{
Dictionary<string, int> c = Ecarts("I3", 3, sigma).Values.First();
int cBe = c["b|e"];
int autres = c.Where(x => x.Key != "b|e").Select(x => x.Value).Distinct().Single();
double seuil = -autres / (double)(cBe - autres);
return (cBe > autres ? ">=" : "<=", seuil);
}
// Grille du simplexe, recalculee ici pour rendre la cellule autonome.
public static IEnumerable<double[]> GrilleSimplexe(int n, double pas)
{
int k = (int)Math.Round(1.0 / pas);
if (n == 1)
{
yield return new[] { 1.0 };
yield break;
}
foreach (int[] c in Combinaisons(k + 1, n - 1))
{
int somme = c.Sum();
if (somme <= k)
{
yield return c.Select(x => (double)x / k)
.Concat(new[] { (double)(k - somme) / k })
.ToArray();
}
}
}
private static IEnumerable<int[]> Combinaisons(int nombreValeurs, int longueur)
{
int[] courante = new int[longueur];
return CombinaisonsAux(nombreValeurs, courante, 0);
}
private static IEnumerable<int[]> CombinaisonsAux(int nombreValeurs, int[] courante, int position)
{
if (position == courante.Length)
{
yield return (int[])courante.Clone();
yield break;
}
for (int v = 0; v < nombreValeurs; v++)
{
courante[position] = v;
foreach (int[] suite in CombinaisonsAux(nombreValeurs, courante, position + 1))
{
yield return suite;
}
}
}
private static IEnumerable<int[]> IndicesRecursifs(List<List<double[]>> grilles, int position)
{
if (position == grilles.Count)
{
yield return new int[grilles.Count];
yield break;
}
for (int i = 0; i < grilles[position].Count; i++)
{
foreach (int[] suite in IndicesRecursifs(grilles, position + 1))
{
suite[position] = i;
yield return suite;
}
}
}
public static (bool wse, Dictionary<string, double?> mu) EstWseGrille(
Dictionary<(string iset, string action), double> sigma,
Dictionary<string, (int u1, int u2, int u3)> payoffs,
double pas)
{
Dictionary<string, double?> baseMu = MoteurEval.Bayes(sigma);
Dictionary<string, List<string>> sets = InfoSets();
List<string> libres = sets.Where(kv => !baseMu[kv.Value[0]].HasValue)
.Select(kv => kv.Key).ToList();
List<List<double[]>> grilles = libres
.Select(i => GrilleSimplexe(sets[i].Count, pas).ToList())
.ToList();
IEnumerable<int[]> indices = grilles.Count == 0
? new[] { new int[0] }
: IndicesRecursifs(grilles, 0);
foreach (int[] ix in indices)
{
Dictionary<string, double?> mu = baseMu.ToDictionary(x => x.Key, x => x.Value);
for (int i = 0; i < libres.Count; i++)
{
double[] valeurs = grilles[i][ix[i]];
for (int j = 0; j < sets[libres[i]].Count; j++)
{
mu[sets[libres[i]][j]] = valeurs[j];
}
}
if (MoteurEval.Seq(sigma, mu, payoffs).Count == 0)
{
return (true, mu);
}
}
return (false, null);
}
public static Dictionary<string, bool> WsePursExacts()
{
var resultat = new Dictionary<string, bool>();
foreach (string x in new[] { "a", "b", "c" })
foreach (string y in new[] { "d", "e" })
foreach (string z in new[] { "f", "g" })
{
string profil = x + y + z;
Dictionary<(string iset, string action), double> sigma = MoteurEval.Sigma(
new Dictionary<string, string> { { "I1", x }, { "I2", y }, { "I3", z } });
Dictionary<string, double?> baseMu = MoteurEval.Bayes(sigma);
bool feasible = true;
foreach (var kv in InfoSets())
{
feasible = feasible && Faisable(kv.Key, ARBRE[kv.Value[0]].joueur, sigma, baseMu);
}
resultat[profil] = feasible;
}
return resultat;
}
}
Dictionary<string, bool> exact = ExactCensus.WsePursExacts();
Console.WriteLine("Census EXACT des 12 profils purs (contraintes entieres)");
Console.WriteLine(new string('=', 78));
foreach (var kv in exact)
{
string profil = kv.Key;
Dictionary<(string iset, string action), double> sigma = MoteurEval.Sigma(
new Dictionary<string, string>
{
{ "I1", profil[0].ToString() },
{ "I2", profil[1].ToString() },
{ "I3", profil[2].ToString() }
});
Dictionary<string, double?> baseMu = MoteurEval.Bayes(sigma);
Console.WriteLine(" sigma = " + profil.PadRight(3) + " " + (kv.Value ? "WSE" : " -") +
" toutes les contraintes sont satisfiables : " + kv.Value);
foreach (string ligne in ExactCensus.Diagnostic(sigma, baseMu))
{
Console.WriteLine(" " + ligne);
}
}
List<string> exacts = exact.Where(x => x.Value).Select(x => x.Key).OrderBy(x => x).ToList();
List<string> grille = new List<string>();
foreach (string x in new[] { "a", "b", "c" })
foreach (string y in new[] { "d", "e" })
foreach (string z in new[] { "f", "g" })
{
Dictionary<(string iset, string action), double> sigma = MoteurEval.Sigma(
new Dictionary<string, string> { { "I1", x }, { "I2", y }, { "I3", z } });
if (ExactCensus.EstWseGrille(sigma, PAYOFFS_ALPHA, 0.25).wse) { grille.Add(x + y + z); }
}
Console.WriteLine("\nWSE purs : census exact [" + string.Join(", ", exacts) +
"] | grille au pas 1/4 [" + string.Join(", ", grille) + "]");
Dictionary<(string iset, string action), double> sigmaCdf = MoteurEval.Sigma(
new Dictionary<string, string> { { "I1", "c" }, { "I2", "d" }, { "I3", "f" } });
Dictionary<(string iset, string action), double> sigmaCdg = MoteurEval.Sigma(
new Dictionary<string, string> { { "I1", "c" }, { "I2", "d" }, { "I3", "g" } });
foreach (string profil in new[] { "cdf", "cdg" })
{
Dictionary<(string iset, string action), double> sigma = MoteurEval.Sigma(
new Dictionary<string, string>
{
{ "I1", profil[0].ToString() },
{ "I2", profil[1].ToString() },
{ "I3", profil[2].ToString() }
});
(string sens, double seuil) = ExactCensus.SeuilMuBe(profil, sigma);
Console.WriteLine("\nsigma = " + profil + " : " + ExactCensus.Forme(profil, sigma) +
" >= 0 <=> mu(be) " + sens + " " + Num(seuil, "F6"));
foreach ((double p, string etiquette) in new[]
{
(seuil, "frontiere"),
(seuil + 0.01, "seuil + 1/100"),
(seuil - 0.01, "seuil - 1/100"),
(0.25, "1/4"),
(0.5, "1/2")
})
{
Dictionary<string, double?> mu = MoteurEval.Bayes(sigma)
.ToDictionary(x => x.Key, x => x.Value);
foreach (string h in new[] { "a|d", "a|e", "b|d", "b|e" }) { mu[h] = 0.0; }
mu["b|e"] = p;
mu["a|d"] = 1.0 - p;
mu["a"] = 0.5;
mu["b"] = 0.5;
Console.WriteLine(" mu(be)=" + Num(p, "F4").PadRight(8) + " (" + etiquette.PadRight(14) +
") -> rationnel : " + (MoteurEval.Seq(sigma, mu, PAYOFFS_ALPHA).Count == 0));
}
}
Console.WriteLine("\nSur la face mu(be)=1/3, le reste se repartit librement :");
foreach (var (etiquette, part) in new[]
{
("tout sur ad", (1.0, 0.0, 0.0)),
("tout sur ae", (0.0, 1.0, 0.0)),
("tout sur bd", (0.0, 0.0, 1.0)),
("ad et ae egaux", (0.5, 0.5, 0.0)),
("1/2 1/4 1/4", (0.5, 0.25, 0.25)),
("melange 1/3", (1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0))
})
{
Dictionary<string, double?> mu = new Dictionary<string, double?>();
foreach (var kv in InfoSets()) { foreach (string h in kv.Value) { mu[h] = 0.0; } }
mu["a|d"] = (1.0 / 3.0) * part.Item1;
mu["a|e"] = (1.0 / 3.0) * part.Item2;
mu["b|d"] = (1.0 / 3.0) * part.Item3;
mu["b|e"] = 1.0 / 3.0;
mu["a"] = 0.5;
mu["b"] = 0.5;
Console.WriteLine(" " + etiquette.PadRight(16) +
" -> cdf: " + (MoteurEval.Seq(sigmaCdf, mu, PAYOFFS_ALPHA).Count == 0) +
" | cdg: " + (MoteurEval.Seq(sigmaCdg, mu, PAYOFFS_ALPHA).Count == 0));
}
Console.WriteLine("\nHors de la face mu(be) = 1/3 :");
foreach (double p in new[] { 0.25, 0.5 })
{
Dictionary<string, double?> mu = MoteurEval.Bayes(sigmaCdf)
.ToDictionary(x => x.Key, x => x.Value);
foreach (string h in new[] { "a|d", "a|e", "b|d", "b|e" }) { mu[h] = 0.0; }
mu["a|d"] = 1.0 - p;
mu["b|e"] = p;
mu["a"] = 0.5;
mu["b"] = 0.5;
Console.WriteLine(" mu(be)=" + Num(p, "F2").PadRight(6) + " -> cdf : " +
(MoteurEval.Seq(sigmaCdf, mu, PAYOFFS_ALPHA).Count == 0) +
" | cdg : " + (MoteurEval.Seq(sigmaCdg, mu, PAYOFFS_ALPHA).Count == 0));
}
Console.WriteLine("\n1/3 est-il un multiple de 1/4 ? " + ((1.0 / 3.0 * 4.0) % 1.0 == 0.0));
// Contre-exemple : mu(be) = 0, tout le poids de I3 porte par ad.
// La croyance mu(ad) = 1 rend g strictement meilleur que f (2 contre 0).
Dictionary<string, double?> muZero = new Dictionary<string, double?>();
foreach (var kv in InfoSets()) { foreach (string h in kv.Value) { muZero[h] = 0.0; } }
muZero[""] = 1.0;
muZero["a"] = 0.5;
muZero["b"] = 0.5;
muZero["a|d"] = 1.0;
muZero["b|e"] = 0.0;
var violationsZero = MoteurEval.Seq(sigmaCdf, muZero, PAYOFFS_ALPHA);
Console.WriteLine("sigma = cdf, mu(be) = 0 (tout le poids de I3 sur ad) : rationnel : " +
(violationsZero.Count == 0));
foreach (var v in violationsZero)
{
Console.WriteLine(" violation en " + v.Item1 + " : {" +
string.Join(", ", v.Item2.Select(x => x.Key + "=" + Num(x.Value, "F3"))) +
"} ecart = " + Num(v.Item3, "F3"));
}