// Appareil : port C# (.NET 9, BCL) du paquet `ict` -- synthesis / free_energy /
// compression / self_sorting / bistable / strategic_morphodynamics. On reutilise
// les trajectoires de ICT-Synthese (meme S1/S2/S3), reproduction fidele de la
// discretisation. Le RNG numpy (PCG64) n'est pas reproductible en .NET : on le
// remplace par un xorshift64* seedé, lui aussi deterministe par graine.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
// Lecon c.94 : les 3 setters de culture sont requis (CurrentCulture seul ne
// persiste pas cross-cell, threads du pool distincts).
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
int[] SEEDS = { 0, 1, 7, 42, 99 };
int SEED0 = SEEDS[0];
int N_SHUF = 30;
// --------------------------------------------------------------- RNG deterministe
// xorshift64* (Marsaglia) : reproductible inter-executions, remplace np.random.default_rng.
public sealed class Rng
{
private ulong _s;
public Rng(long seed)
{
_s = (ulong)seed * 0x9E3779B97F4A7C15UL + 0xD1B54A32D192ED03UL;
if (_s == 0) _s = 0x853C49E6748FEA9BUL;
NextUlong(); NextUlong(); NextUlong(); // echauffement
}
public ulong NextUlong()
{
_s ^= _s >> 12; _s ^= _s << 25; _s ^= _s >> 27;
return _s * 0x2545F4914F6CDD1DUL;
}
public double NextDouble() => (NextUlong() >> 11) * (1.0 / 9007199254740992.0);
public int NextInt(int n) => (int)(NextUlong() % (ulong)n);
// Gaussienne centree reduite (Box-Muller).
public double StandardNormal()
{
double u1 = 1.0 - NextDouble(), u2 = NextDouble();
return Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Cos(2.0 * Math.PI * u2);
}
// Permutation uniforme de [0..n-1] (Fisher-Yates).
public int[] Permutation(int n)
{
var a = Enumerable.Range(0, n).ToArray();
for (int i = n - 1; i > 0; i--) { int j = NextInt(i + 1); (a[i], a[j]) = (a[j], a[i]); }
return a;
}
// Dirichlet(1,...,1) : exponentielles normalisees (gamma de forme 1).
public double[] DirichletUniform(int k)
{
var e = new double[k];
double s = 0.0;
for (int i = 0; i < k; i++) { e[i] = -Math.Log(1.0 - NextDouble()); s += e[i]; }
for (int i = 0; i < k; i++) e[i] /= s;
return e;
}
}
// --------------------------------------------------------------- TPM estimee (ict.tpm_estimation)
public static class TpmEstimation
{
// Mapping ordre de premiere apparition (reproductible).
public static Dictionary<int, int> StateIndexMap(List<int> states)
{
var map = new Dictionary<int, int>();
foreach (var s in states) if (!map.ContainsKey(s)) map[s] = map.Count;
return map;
}
// TPM empirique etat-a-etat d'une suite d'etats consecutifs, unseen="self".
public static double[][] FromTrajectory(List<int> states)
{
var map = StateIndexMap(states);
int n = map.Count;
var counts = new double[n][];
for (int i = 0; i < n; i++) counts[i] = new double[n];
for (int t = 0; t + 1 < states.Count; t++)
counts[map[states[t]]][map[states[t + 1]]] += 1.0;
var tpm = new double[n][];
for (int i = 0; i < n; i++)
{
tpm[i] = new double[n];
double sum = counts[i].Sum();
if (sum <= 0) { tpm[i][i] = 1.0; } // unseen = self
else for (int j = 0; j < n; j++) tpm[i][j] = counts[i][j] / sum;
}
return tpm;
}
}
// --------------------------------------------------------------- emergence causale (ict.causal_emergence)
public static class Causalite
{
public static double EntropyBits(double[] p)
{
double h = 0.0;
foreach (var v in p) if (v > 0) h -= v * Math.Log2(v);
return h;
}
public static double Determinism(double[][] tpm)
{
int n = tpm.Length;
if (n < 2) return 1.0;
double mean = 0.0;
for (int i = 0; i < n; i++) mean += EntropyBits(tpm[i]);
return 1.0 - mean / n / Math.Log2(n);
}
public static double Degeneracy(double[][] tpm)
{
int n = tpm.Length;
if (n < 2) return 0.0;
var marginal = new double[n];
for (int j = 0; j < n; j++)
for (int i = 0; i < n; i++) marginal[j] += tpm[i][j] / n;
return 1.0 - EntropyBits(marginal) / Math.Log2(n);
}
public static double Effectiveness(double[][] tpm) => Determinism(tpm) - Degeneracy(tpm);
public static double EffectiveInformation(double[][] tpm)
{
int n = tpm.Length;
if (n < 2) return 0.0;
return Effectiveness(tpm) * Math.Log2(n);
}
// Fusion des etats i et j (Hoel 2013) : colonnes sommees, lignes moyenees, renormalisees.
// Le macro-etat occupe la position min(i, j), l'autre ligne est retiree.
public static double[][] MergeStates(double[][] tpm, int i, int j)
{
int n = tpm.Length;
int a = Math.Min(i, j), b = Math.Max(i, j);
// 1) fusion des colonnes : P(arriver dans le macro) = P(->a) + P(->b)
var m = new double[n][];
for (int r = 0; r < n; r++)
{
m[r] = new double[n - 1];
int c2 = 0;
for (int c = 0; c < n; c++)
{
if (c == b) continue;
m[r][c2] = tpm[r][c] + (c == a ? tpm[r][b] : 0.0);
c2++;
}
}
// 2) fusion des lignes : effet du macro = moyenne des deux micro-effets ; 3) renormalise
var macro = new double[n - 1][];
for (int r = 0; r < n - 1; r++)
{
int src = r < b ? r : r + 1; // decalage apres suppression de la ligne b
double[] row = src == a
? m[a].Zip(m[b], (x, y) => 0.5 * (x + y)).ToArray()
: m[src];
macro[r] = new double[n - 1];
double s = row.Sum();
for (int c = 0; c < n - 1; c++) macro[r][c] = s > 0 ? row[c] / s : row[c];
}
return macro;
}
// Chemin d'echelles glouton : deltas de primitive (effectiveness) positifs le long du chemin.
public static List<double> GreedyDeltas(double[][] tpm, double eps = 1e-9)
{
var deltas = new List<double>();
var cur = tpm;
double curVal = Effectiveness(cur);
while (cur.Length > 1)
{
int sz = cur.Length;
double best = double.NegativeInfinity;
double[][] bestTpm = null;
for (int i = 0; i < sz; i++)
for (int j = i + 1; j < sz; j++)
{
var cand = MergeStates(cur, i, j);
double val = Effectiveness(cand);
if (val > best) { best = val; bestTpm = cand; }
}
double delta = best - curVal;
if (delta < eps) break; // plus aucune fusion n'ameliorere : emergence epuisee
deltas.Add(delta);
cur = bestTpm; curVal = best;
}
return deltas;
}
// EC = log2(L) - somme p log2 p sur les deltas positifs (Hoel 2025).
public static double EmergentComplexity(List<double> deltas)
{
var d = deltas.Where(x => x > 0).ToArray();
int L = d.Length;
if (L <= 1) return 0.0;
double s = d.Sum();
double h = 0.0;
foreach (var x in d) { double p = x / s; h -= p * Math.Log2(p); }
return Math.Log2(L) + h;
}
}
// --------------------------------------------------------------- batterie (ict.synthesis)
public static class Synthese
{
// Batterie d'une trajectoire : TPM estimee -> EI + EC.
public static (double ei, double ec, int nStates) TrajectoryBattery(List<int> states)
{
var tpm = TpmEstimation.FromTrajectory(states);
double ei = Causalite.EffectiveInformation(tpm);
double ec = Causalite.EmergentComplexity(Causalite.GreedyDeltas(tpm));
return (ei, ec, TpmEstimation.StateIndexMap(states).Count);
}
// Controle degenere : permutation conservant exactement le multi-ensemble d'etats.
public static List<int> ShuffleStates(List<int> states, Rng rng)
{
var seq = states.ToList();
int n = seq.Count;
for (int i = n - 1; i > 0; i--) { int j = rng.NextInt(i + 1); (seq[i], seq[j]) = (seq[j], seq[i]); }
return seq;
}
public static (double eiMean, double eiStd, double ecMean, double ecStd) ShuffledBaseline(
List<int> states, Rng rng, int nShuffles)
{
var eis = new List<double>(); var ecs = new List<double>();
for (int k = 0; k < nShuffles; k++)
{
var b = TrajectoryBattery(ShuffleStates(states, rng));
eis.Add(b.ei); ecs.Add(b.ec);
}
return (eis.Average(), Std(eis), ecs.Average(), Std(ecs));
}
private static double Std(List<double> xs)
{
double m = xs.Average();
return Math.Sqrt(xs.Select(x => (x - m) * (x - m)).Sum() / xs.Count); // ddof=0 comme np.std
}
// Les trois facettes, meme rng (donc meme discipline de shuffle), retro-compatible ec/ei.
public static (double ecGain, double feGain, double kGain, bool credited, double eiReal, double ecReal)
EmergenceGain(List<int> states, Rng rng, int nShuffles)
{
var real = TrajectoryBattery(states);
var baseline = ShuffledBaseline(states, rng, nShuffles);
double ecGain = real.ec - baseline.ecMean;
bool credited = ecGain > baseline.ecStd + 1e-12;
var fe = FreeEnergy.SurpriseGain(states, rng, nShuffles);
var kp = CompressionK.CompressionGain(states, rng, nShuffles);
return (ecGain, fe.feGain, kp.kGain, credited, real.ei, real.ec);
}
// Tau de Kendall O(n^2), paires a egalite ignorees.
public static double KendallTau(double[] xs, double[] ys)
{
int n = xs.Length;
if (n < 2) return 0.0;
int conc = 0, disc = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
{
double sx = Math.Sign(xs[i] - xs[j]), sy = Math.Sign(ys[i] - ys[j]);
if (sx == 0 || sy == 0) continue;
if (sx == sy) conc++; else disc++;
}
int total = conc + disc;
return total == 0 ? 0.0 : (double)(conc - disc) / total;
}
}
// --------------------------------------------------------------- jambe F (ict.free_energy)
public static class FreeEnergy
{
// Cross-entropie held-out d'une TPM ordre-1, lissage Laplace alpha (Jeffreys 0.5).
public static double TransitionSurprise(List<int> states, double split = 0.5, double alpha = 0.5)
{
int n = states.Count;
int nTrain = Math.Max(2, (int)Math.Round(split * n, MidpointRounding.ToEven));
if (nTrain >= n) nTrain = Math.Max(1, n / 2);
var train = states.Take(nTrain).ToList();
var test = states.Skip(nTrain).ToList();
var mapping = new Dictionary<int, int>();
foreach (var s in train) if (!mapping.ContainsKey(s)) mapping[s] = mapping.Count;
foreach (var s in test) if (!mapping.ContainsKey(s)) mapping[s] = mapping.Count;
var counts = new Dictionary<int, Dictionary<int, double>>();
foreach (var s in mapping) counts[s.Key] = new Dictionary<int, double>();
for (int t = 0; t + 1 < train.Count; t++)
{
var row = counts[train[t]];
row[train[t + 1]] = row.GetValueOrDefault(train[t + 1]) + 1.0;
}
int nVu = mapping.Count;
var surprisals = new List<double>();
for (int t = 0; t + 1 < test.Count; t++)
{
int a = test[t], b = test[t + 1];
if (!mapping.ContainsKey(a)) continue; // source jamais vue au train
var row = counts[a];
double total = row.Values.Sum() + alpha * nVu;
if (total <= 0) continue;
double p = (row.GetValueOrDefault(b) + alpha) / total;
surprisals.Add(-Math.Log2(p));
}
return surprisals.Count == 0 ? double.NaN : surprisals.Average();
}
// fe_gain = F_shuffled - F_real : la structure d'ordre rend la trajectoire plus previsible.
public static (double fReal, double fShuffled, double feGain) SurpriseGain(
List<int> states, Rng rng, int nShuffles)
{
double fReal = TransitionSurprise(states);
var fs = new List<double>();
for (int k = 0; k < nShuffles; k++)
fs.Add(TransitionSurprise(Synthese.ShuffleStates(states, rng)));
double fShuffled = fs.Where(x => !double.IsNaN(x)).DefaultIfEmpty(double.NaN).Average();
double gain = (double.IsNaN(fReal) || double.IsNaN(fShuffled)) ? double.NaN : fShuffled - fReal;
return (fReal, fShuffled, gain);
}
}
// --------------------------------------------------------------- jambe K (ict.compression, zlib -> Deflate BCL)
public static class CompressionK
{
// Re-indexation par ordre de premiere apparition (canonique, reproductible).
public static List<int> CanonicalIntSequence(List<int> states)
{
var mapping = new Dictionary<int, int>();
var outs = new List<int>();
foreach (var s in states)
{
if (!mapping.ContainsKey(s)) mapping[s] = mapping.Count;
outs.Add(mapping[s]);
}
return outs;
}
// Longueur (octets) de la sequence canonique compressee par Deflate (BCL, Optimal).
// zlib (Python) et Deflate (.NET) sont la meme famille d'algorithme : le contraste
// shuffle annule l'overhead constant de framing, le gain k est comparable.
public static int CompressedLength(List<int> states)
{
var seq = CanonicalIntSequence(states);
if (seq.Count == 0) return 0;
byte[] payload;
if (seq.Max() < 256) payload = seq.Select(v => (byte)v).ToArray();
else
{
var buf = new List<byte>();
foreach (var v in seq)
{
int x = v;
while (true)
{
byte b = (byte)(x & 0x7F);
x >>= 7;
if (x != 0) buf.Add((byte)(b | 0x80));
else { buf.Add(b); break; }
}
}
payload = buf.ToArray();
}
using var ms = new MemoryStream();
using (var ds = new DeflateStream(ms, CompressionLevel.Optimal, leaveOpen: true))
ds.Write(payload, 0, payload.Length);
return (int)ms.Length;
}
// k_gain = (len_shuffle - len_real) / len_shuffle.
public static (int lenReal, double lenShuffled, double kGain) CompressionGain(
List<int> states, Rng rng, int nShuffles)
{
int lenReal = CompressedLength(states);
var lens = new List<int>();
for (int k = 0; k < nShuffles; k++)
lens.Add(CompressedLength(Synthese.ShuffleStates(states, rng)));
double shuffled = lens.Count > 0 ? lens.Average() : lenReal;
double denom = shuffled > 0 ? shuffled : 1.0;
return (lenReal, shuffled, (shuffled - lenReal) / denom);
}
}
// --------------------------------------------------------------- substrats (ict.self_sorting / bistable / strategic_morphodynamics)
public static class Substrats
{
// Nombre de paires (i < j) telles que values[i] > values[j].
public static int InversionCount(int[] values)
{
int c = 0;
for (int i = 0; i < values.Length; i++)
for (int j = i + 1; j < values.Length; j++)
if (values[i] > values[j]) c++;
return c;
}
// S1 - tri auto-organise (Zhang, Goldstein & Levin 2025) : cellules bubble autonomes,
// activation asynchrone seedee ; etat = nombre d'inversions (coarse, ICT-2/6).
public static List<int> TrajS1(int seed, int nRuns = 6, int maxSteps = 1500)
{
var states = new List<int>();
for (int r = 0; r < nRuns; r++)
{
var arr = new Rng(seed + r).Permutation(6); // configuration initiale aleatoire
var rngAct = new Rng(seed + r); // activateur asynchrone seede
int n = arr.Length;
var snapshots = new List<int[]> { (int[])arr.Clone() };
bool HasMove()
{
for (int i = 0; i + 1 < n; i++) if (arr[i] > arr[i + 1]) return true;
return false;
}
int steps = 0;
while (steps < maxSteps && HasMove())
{
int i = rngAct.NextInt(n);
if (i + 1 < n && arr[i] > arr[i + 1]) (arr[i], arr[i + 1]) = (arr[i + 1], arr[i]);
steps++;
snapshots.Add((int[])arr.Clone());
}
states.AddRange(snapshots.Select(InversionCount));
}
return states;
}
// Percentile numpy methode 'linear' (interpolation).
public static double Percentile(double[] xs, double q)
{
var s = (double[])xs.Clone();
Array.Sort(s);
double rank = q * (s.Length - 1);
int lo = (int)Math.Floor(rank);
int hi = Math.Min(lo + 1, s.Length - 1);
return s[lo] + (rank - lo) * (s[hi] - s[lo]);
}
// Discretisation en deciles (digitize side='right' + clip 0..10), 11 etats.
public static List<int> DiscretiseDeciles(double[] xs)
{
double lo = Percentile(xs, 0.01), hi = Percentile(xs, 0.99);
var bins = new double[11];
for (int i = 0; i < 11; i++) bins[i] = lo + (hi - lo) * i / 10.0;
var states = new List<int>();
foreach (var x in xs)
{
int d = 0;
while (d < bins.Length && bins[d] <= x) d++; // bins[i-1] <= x < bins[i]
states.Add(Math.Clamp(d, 0, 10));
}
return states;
}
// S2 - modele de paturage de May (1977) : dx/dt = r x (1 - x/K) - c x^2/(x^2 + h^2).
public sealed class GrazingModel
{
public double R = 1.0, Kg = 10.0, H = 1.0;
public double Rate(double x, double c) =>
R * x * (1.0 - x / Kg) - c * x * x / (x * x + H * H);
// Racines positives reelles du cubique x^3 - K x^2 + (h^2 + cK/r) x - K h^2 = 0
// (trigonometrie de Cardan : exact, remplace np.roots).
public List<double> PositiveRoots(double c)
{
double a2 = -Kg, a1 = H * H + c * Kg / R, a0 = -Kg * H * H;
double p = a1 - a2 * a2 / 3.0;
double q = a0 - a2 * a1 / 3.0 + 2.0 * a2 * a2 * a2 / 27.0;
double shift = -a2 / 3.0;
var roots = new List<double>();
double disc = (q / 2) * (q / 2) + (p / 3) * (p / 3) * (p / 3);
if (disc <= 0 && p < 0)
{
// 3 racines reelles : t_k = 2 sqrt(-p/3) cos(theta/3 - 2 pi k/3)
double rr = Math.Sqrt(-p * p * p / 27.0);
double arg = rr > 0 ? -q / (2.0 * rr) : 0.0;
double phi = Math.Acos(Math.Clamp(arg, -1.0, 1.0));
double m = 2.0 * Math.Sqrt(-p / 3.0);
for (int k = 0; k < 3; k++)
roots.Add(m * Math.Cos(phi / 3.0 - 2.0 * Math.PI * k / 3.0) + shift);
}
else
{
// 1 racine reelle (Cardan)
double s = Math.Sqrt(disc);
roots.Add(Math.Cbrt(-q / 2 + s) + Math.Cbrt(-q / 2 - s) + shift);
}
return roots.Where(x => x > 1e-9).OrderBy(x => x).ToList();
}
// c_fold : plus grand c de la grille ou subsiste un equilibre haut (>= 2 racines positives).
public double FindFold(double cLo = 1.5, double cHi = 3.5, int n = 4000)
{
double last = double.NaN;
for (int k = 0; k < n; k++)
{
double c = cLo + (cHi - cLo) * k / (n - 1);
if (PositiveRoots(c).Count >= 2) last = c;
}
return last;
}
// Trajectoire stochastique Euler-Maruyama, bruit additif, reflexion a 0.
public double[] SimulateSde(double c, double x0, double sigma, double dt, int T, int seed)
{
var g = new Rng(seed);
double sq = Math.Sqrt(dt);
double x = x0;
var xs = new double[T];
for (int t = 0; t < T; t++)
{
x = x + dt * Rate(x, c) + sigma * sq * g.StandardNormal();
if (x < 0) x = 0;
xs[t] = x;
}
return xs;
}
}
// S2 discretise : decile de la biomasse (11 etats, coarse-graining documente d'ICT-Synthese).
public static List<int> TrajS2(int seed, int T = 1500, double sigma = 0.55, double cFactor = 0.92)
{
var gm = new GrazingModel();
double c = cFactor * gm.FindFold();
var xs = gm.SimulateSde(c, x0: 8.0, sigma, dt: 0.02, T, seed);
return DiscretiseDeciles(xs);
}
// ---- IPD (Axelrod) : C=0, D=1 ; gains T=5, R=3, P=1, S=0.
public const int C = 0, D = 1;
private static (double, double) PayoffPair(int a, int b) => (a, b) switch
{
(0, 0) => (3.0, 3.0),
(0, 1) => (0.0, 5.0),
(1, 0) => (5.0, 0.0),
_ => (1.0, 1.0),
};
public delegate int Strategie(List<int> own, List<int> opp, Rng rng);
// Les 6 strategies canoniques d'ICT-13 (ordre du dict Python : allc..grim).
public static (string[] Noms, Strategie[] Regles) MakeStrategies()
{
int AllC(List<int> o, List<int> p, Rng r) => C;
int AllD(List<int> o, List<int> p, Rng r) => D;
int Tft(List<int> o, List<int> p, Rng r) => p.Count == 0 ? C : p[p.Count - 1];
int GTft(List<int> o, List<int> p, Rng r) =>
p.Count == 0 ? C : (p[p.Count - 1] == C ? C : (r.NextDouble() < 1.0 / 3.0 ? C : D));
int Pavlov(List<int> o, List<int> p, Rng r)
{
if (o.Count == 0) return C;
int last = o[o.Count - 1], oppLast = p[p.Count - 1];
double pa = PayoffPair(last, oppLast).Item1;
return pa >= 3.0 ? last : 1 - last; // win-stay, lose-shift
}
int Grim(List<int> o, List<int> p, Rng r) => p.Contains(D) ? D : C;
return (new[] { "allc", "alld", "tft", "gtft", "pavlov", "grim" },
new Strategie[] { AllC, AllD, Tft, GTft, Pavlov, Grim });
}
// Match IPD de nRounds coups ; toutes les decisions aleatoires du match viennent du meme rng.
public static (double, double) PlayMatch(Strategie s1, Strategie s2, int nRounds, Rng rng)
{
var own1 = new List<int>(); var own2 = new List<int>();
double g1 = 0, g2 = 0;
for (int t = 0; t < nRounds; t++)
{
int a = s1(own1, own2, rng);
int b = s2(own2, own1, rng);
var (pa, pb) = PayoffPair(a, b);
g1 += pa; g2 += pb;
own1.Add(a); own2.Add(b);
}
return (g1 / nRounds, g2 / nRounds);
}
// A[i,j] = score moyen de la strategie i face a j (nReps matchs de 200 coups, sans bruit).
public static double[,] PayoffMatrix(Strategie[] regles, Rng rng, int nRounds = 200, int nReps = 3)
{
int n = regles.Length;
var A = new double[n, n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
var scores = new List<double>();
for (int rep = 0; rep < nReps; rep++)
scores.Add(PlayMatch(regles[i], regles[j], nRounds, rng).Item1);
A[i, j] = scores.Average();
}
return A;
}
// Replicateur discret : x_i(t+1) = x_i f_i / <f>, f = A x.
public static List<double[]> ReplicatorTrajectory(double[,] A, double[] x0, int nSteps)
{
int k = x0.Length;
var x = (double[])x0.Clone();
double s0 = x.Sum(); for (int i = 0; i < k; i++) x[i] /= s0;
var traj = new List<double[]> { (double[])x.Clone() };
for (int t = 0; t < nSteps; t++)
{
var f = new double[k];
double avg = 0;
for (int i = 0; i < k; i++)
{
f[i] = 0;
for (int j = 0; j < k; j++) f[i] += A[i, j] * x[j];
avg += f[i] * x[i];
}
if (avg < 1e-12) break;
for (int i = 0; i < k; i++) x[i] = Math.Max(0.0, x[i] * f[i] / avg);
double s = x.Sum();
if (s > 1e-12) for (int i = 0; i < k; i++) x[i] /= s;
traj.Add((double[])x.Clone());
}
return traj;
}
// S3 discretise : strategie dominante (argmax) le long de nRuns trajectoires replicateur.
public static List<int> TrajS3(int seed, int nRuns = 10, int nSteps = 300)
{
var rng = new Rng(seed);
var (noms, regles) = MakeStrategies();
var A = PayoffMatrix(regles, rng);
var states = new List<int>();
for (int run = 0; run < nRuns; run++)
{
var x0 = rng.DirichletUniform(regles.Length);
var traj = ReplicatorTrajectory(A, x0, nSteps);
foreach (var row in traj)
{
int best = 0;
for (int i = 1; i < row.Length; i++) if (row[i] > row[best]) best = i;
states.Add(best);
}
}
return states;
}
}
Console.WriteLine("Appareil : emergence_gain enrichi des 3 facettes (ec_gain/fe_gain/k_gain).");
Console.WriteLine("Substrats : S1 tri, S2 bistable, S3 replicateur. Graines gate 4 : (" + string.Join(", ", SEEDS) + ")");
// Sanity check (reprise de la sortie d'appareil de la source Python) :
// un echantillon par substrat sur la graine representative.
var s1Check = Substrats.TrajS1(SEED0);
var s2Check = Substrats.TrajS2(SEED0);
var s3Check = Substrats.TrajS3(SEED0);
Console.WriteLine($"{"S1_tri",-12} longueur={s1Check.Count,5} etats distincts={s1Check.Distinct().Count(),3}");
Console.WriteLine($"{"S2_bistable",-12} longueur={s2Check.Count,5} etats distincts={s2Check.Distinct().Count(),3}");
Console.WriteLine($"{"S3_replic",-12} longueur={s3Check.Count,5} etats distincts={s3Check.Distinct().Count(),3}");