// Imports + recette observable, REPLIQUE EXACTE d'ICT-15j (cellules f22b81e8/ff929034)
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
// Le carnet utilise C=1 pour compter les coups, tandis que le module IPD utilise C=0.
const int C = 1, D = 0;
public sealed class IctRandom
{
private ulong state;
public IctRandom(int seed) { state = (ulong)(uint)seed; if (state == 0) state = 0x9e3779b97f4a7c15UL; }
public ulong Next64() { unchecked { ulong x = state; x ^= x >> 12; x ^= x << 25; x ^= x >> 27; state = x; return x * 2685821657736338717UL; } }
public double NextDouble() => (Next64() >> 11) * (1.0 / 9007199254740992.0);
public int NextInt(int n) => (int)(NextDouble() * n);
}
public static class IctObservable
{
// Valeurs propres d'une matrice symétrique réelle par rotations de Jacobi.
public static double[] Eigenvalues(double[,] matrix)
{
int n = matrix.GetLength(0);
var a = (double[,])matrix.Clone();
for (int iter = 0; iter < 10000; iter++)
{
int p = 0, q = 1; double largest = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (Math.Abs(a[i,j]) > largest) { largest = Math.Abs(a[i,j]); p = i; q = j; }
if (largest < 1e-14) break;
double angle = 0.5 * Math.Atan2(2 * a[p,q], a[q,q] - a[p,p]);
double c = Math.Cos(angle), s = Math.Sin(angle);
double app = a[p,p], aqq = a[q,q], apq = a[p,q];
for (int k = 0; k < n; k++)
{
if (k == p || k == q) continue;
double akp = a[k,p], akq = a[k,q];
a[k,p] = a[p,k] = c * akp - s * akq;
a[k,q] = a[q,k] = s * akp + c * akq;
}
a[p,p] = c*c*app - 2*s*c*apq + s*s*aqq;
a[q,q] = s*s*app + 2*s*c*apq + c*c*aqq;
a[p,q] = a[q,p] = 0;
}
return Enumerable.Range(0, n).Select(i => a[i,i]).OrderBy(x => x).ToArray();
}
public static double[] Proxies(int[] states, int n)
{
// TPM lissée pour le spectre ; adjacence observée pour les sensibilités.
var p = new double[n,n];
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) p[i,j] = 1e-9;
for (int i = 0; i + 1 < states.Length; i++) p[states[i], states[i+1]] += 1;
for (int i = 0; i < n; i++)
{
double sum = 0; for (int j = 0; j < n; j++) sum += p[i,j];
for (int j = 0; j < n; j++) p[i,j] /= sum;
}
var lap = new double[n,n];
for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++)
{
double w = (p[i,j] + p[j,i]) / 2;
lap[i,j] = lap[j,i] = -w; lap[i,i] += w; lap[j,j] += w;
}
var eigen = Eigenvalues(lap);
var ids = new Dictionary<int,int>();
var encoded = new int[states.Length];
for (int i = 0; i < states.Length; i++)
{
if (!ids.ContainsKey(states[i])) ids[states[i]] = ids.Count;
encoded[i] = ids[states[i]];
}
var adjacency = new bool[n,n];
for (int i = 0; i + 1 < encoded.Length; i++)
{
int a = encoded[i], b = encoded[i+1];
if (a != b) adjacency[a,b] = adjacency[b,a] = true;
}
var degrees = new double[ids.Count];
for (int i = 0; i < degrees.Length; i++) for (int j = 0; j < n; j++) if (adjacency[i,j]) degrees[i]++;
return new[] { eigen[1] - eigen[0], degrees.Length == 0 ? 0 : degrees.Average(), degrees.Length == 0 ? 0 : degrees.Max() };
}
public static double Quantile(double[] sorted, double probability)
{
double position = (sorted.Length - 1) * probability;
int lo = (int)Math.Floor(position), hi = (int)Math.Ceiling(position);
return sorted[lo] + (sorted[hi] - sorted[lo]) * (position - lo);
}
public sealed class Simplex
{
public int A, B, C, Dim;
public double Filtration;
public Simplex(int a, int b, int c, int dim, double filtration) { A=a; B=b; C=c; Dim=dim; Filtration=filtration; }
}
public static double MaxPersistence(double[,] points)
{
// Filtration Rips complète : réduction des frontières sur F2 jusqu'aux triangles.
int n = points.GetLength(0), d = points.GetLength(1);
var normalized = new double[n,d];
for (int j = 0; j < d; j++)
{
double mean = 0; for (int i = 0; i < n; i++) mean += points[i,j]; mean /= n;
double variance = 0; for (int i = 0; i < n; i++) variance += (points[i,j]-mean)*(points[i,j]-mean);
double std = Math.Sqrt(variance/n); if (std <= 1e-12) std = 1;
for (int i = 0; i < n; i++) normalized[i,j] = (points[i,j]-mean)/std;
}
var dist = new double[n,n];
for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++)
{
double sum = 0; for (int k = 0; k < d; k++) { double x = normalized[i,k]-normalized[j,k]; sum += x*x; }
dist[i,j] = dist[j,i] = Math.Sqrt(sum);
}
var simplices = new List<Simplex>();
for (int i = 0; i < n; i++) simplices.Add(new Simplex(i,-1,-1,0,0));
for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) simplices.Add(new Simplex(i,j,-1,1,dist[i,j]));
for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) for (int k = j+1; k < n; k++)
simplices.Add(new Simplex(i,j,k,2,Math.Max(dist[i,j],Math.Max(dist[i,k],dist[j,k]))));
simplices = simplices.OrderBy(s => s.Filtration).ThenBy(s => s.Dim).ThenBy(s => s.A).ThenBy(s => s.B).ThenBy(s => s.C).ToList();
var lookup = new Dictionary<(int,int,int),int>();
for (int i = 0; i < simplices.Count; i++) { var s = simplices[i]; lookup[(s.A,s.B,s.C)] = i; }
var reduced = new Dictionary<int,HashSet<int>>();
double maximum = 0;
for (int j = 0; j < simplices.Count; j++)
{
var s = simplices[j]; var column = new HashSet<int>();
if (s.Dim == 1) { column.Add(lookup[(s.A,-1,-1)]); column.Add(lookup[(s.B,-1,-1)]); }
if (s.Dim == 2) { column.Add(lookup[(s.A,s.B,-1)]); column.Add(lookup[(s.A,s.C,-1)]); column.Add(lookup[(s.B,s.C,-1)]); }
while (column.Count > 0)
{
int pivot = column.Max();
if (!reduced.TryGetValue(pivot, out var previous))
{
reduced[pivot] = column;
if (s.Dim == 2) maximum = Math.Max(maximum, s.Filtration - simplices[pivot].Filtration);
break;
}
column.SymmetricExceptWith(previous);
}
}
return maximum;
}
public static (double b1, int symbols) CoopToB1(double[] coop, string name, int nSymbols = 8)
{
var sorted = coop.OrderBy(x => x).ToArray();
var cuts = Enumerable.Range(1,nSymbols-1).Select(i => Quantile(sorted,(double)i/nSymbols)).ToArray();
var states = coop.Select(x => cuts.Count(q => x >= q)).ToArray();
var values = new List<double>[] { new List<double>(), new List<double>(), new List<double>() };
for (int w = 0; w < 30; w++)
{
int start = w * states.Length / 30;
int end = Math.Max(start+2,(w+1)*states.Length/30);
var chunk = states.Skip(start).Take(end-start).ToArray();
if (chunk.Length < 2) chunk = states.Skip(start).Take(4).ToArray();
try { var p = Proxies(chunk,nSymbols); for (int j=0;j<3;j++) values[j].Add(p[j]); }
catch { for (int j=0;j<3;j++) values[j].Add(double.NaN); }
}
var points = new double[30,3];
for (int j=0;j<3;j++)
{
var valid = values[j].Where(x => !double.IsNaN(x) && !double.IsInfinity(x)).ToArray();
for (int i=0;i<valid.Length && i<30;i++) points[i,j] = valid[i];
}
return (MaxPersistence(points), states.Distinct().Count());
}
}
// Six stratégies du module stratégique : C=0 et D=1.
public static class IctIpd
{
public static int Play(int strategy, List<int> own, List<int> opp, IctRandom rng)
{
if (strategy == 0) return 0;
if (strategy == 1) return 1;
if (strategy == 2) return opp.Count == 0 ? 0 : opp[opp.Count-1];
if (strategy == 3) return opp.Count == 0 || opp[opp.Count-1] == 0 || rng.NextDouble() < 1.0/3.0 ? 0 : 1;
if (strategy == 4)
{
if (own.Count == 0) return 0;
int a=own[own.Count-1], b=opp[opp.Count-1];
return Payoff(a,b) >= 3 ? a : 1-a;
}
return opp.Contains(1) ? 1 : 0;
}
public static double Payoff(int a, int b) => a == 0 ? (b == 0 ? 3 : 0) : (b == 0 ? 5 : 1);
}
(double b1, int symbols) coop_to_b1(double[] coop, string name, int n_symbols = 8) => IctObservable.CoopToB1(coop, name, n_symbols);
Console.WriteLine("recette observable chargee (replique ICT-15j).");