F0: fondamenta della soluzione, script, documenti e harness di test
- ViaFerrea.sln con Core e Protocol (net10.0 + net8.0), Server, Tools, Tests - Directory.Build.props con versione unica e warning come errori - Script build/test/check-nuget/publish-image (+ build.ps1) - Documenti: GUIDA, GDD, ARCHITECTURE, PROTOCOL (bozza), STATE, ADR 0001 - Harness di test interno con i primi test su Rng (xoshiro256**) - Immagini di riferimento 01-05 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
This commit is contained in:
36 files changed
+1647
-2
No files matched your search
@@ -0,0 +1,130 @@
|
||||
namespace ViaFerrea.Core.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Generatore pseudo-casuale xoshiro256** (Blackman e Vigna), unico ammesso nella simulazione.
|
||||
/// Lo stato è di 256 bit, serializzabile e indipendente dalla piattaforma.
|
||||
/// Ogni sottosistema usa il proprio flusso ottenuto con <see cref="Fork"/>, così l'ordine
|
||||
/// delle estrazioni di un sistema non perturba gli altri.
|
||||
/// </summary>
|
||||
public sealed class Rng
|
||||
{
|
||||
private ulong _s0, _s1, _s2, _s3;
|
||||
|
||||
/// <summary>Crea un generatore dal seme con l'espansione SplitMix64 raccomandata dagli autori.</summary>
|
||||
public Rng(ulong seed)
|
||||
{
|
||||
ulong x = seed;
|
||||
_s0 = SplitMix64(ref x);
|
||||
_s1 = SplitMix64(ref x);
|
||||
_s2 = SplitMix64(ref x);
|
||||
_s3 = SplitMix64(ref x);
|
||||
if ((_s0 | _s1 | _s2 | _s3) == 0) _s0 = 1; // lo stato tutto a zero è l'unico vietato
|
||||
}
|
||||
|
||||
private Rng(ulong s0, ulong s1, ulong s2, ulong s3)
|
||||
{
|
||||
_s0 = s0; _s1 = s1; _s2 = s2; _s3 = s3;
|
||||
if ((_s0 | _s1 | _s2 | _s3) == 0) _s0 = 1;
|
||||
}
|
||||
|
||||
/// <summary>Crea un generatore con lo stato esatto (test e ripristino da salvataggio).</summary>
|
||||
public static Rng FromState(ulong s0, ulong s1, ulong s2, ulong s3) => new(s0, s1, s2, s3);
|
||||
|
||||
/// <summary>Stato corrente, per salvataggi e hash.</summary>
|
||||
public (ulong S0, ulong S1, ulong S2, ulong S3) State => (_s0, _s1, _s2, _s3);
|
||||
|
||||
/// <summary>
|
||||
/// Deriva un flusso indipendente e stabile identificato da un nome. Non consuma numeri
|
||||
/// da questo generatore: lo stesso nome sullo stesso stato dà sempre lo stesso flusso.
|
||||
/// </summary>
|
||||
public Rng Fork(string name)
|
||||
{
|
||||
ulong h = StableHash.Fnv1a64(name);
|
||||
ulong x = _s0 ^ RotateLeft(_s1, 17) ^ RotateLeft(_s2, 31) ^ RotateLeft(_s3, 47) ^ h;
|
||||
ulong a = SplitMix64(ref x), b = SplitMix64(ref x), c = SplitMix64(ref x), d = SplitMix64(ref x);
|
||||
return new Rng(a, b, c, d);
|
||||
}
|
||||
|
||||
/// <summary>Deriva un flusso indipendente da un nome e da un indice (per entità numerate).</summary>
|
||||
public Rng Fork(string name, long index)
|
||||
{
|
||||
ulong h = StableHash.Fnv1a64(name) ^ (ulong)index * 0x9E3779B97F4A7C15UL;
|
||||
ulong x = _s0 ^ RotateLeft(_s1, 17) ^ RotateLeft(_s2, 31) ^ RotateLeft(_s3, 47) ^ h;
|
||||
ulong a = SplitMix64(ref x), b = SplitMix64(ref x), c = SplitMix64(ref x), d = SplitMix64(ref x);
|
||||
return new Rng(a, b, c, d);
|
||||
}
|
||||
|
||||
/// <summary>Prossimo intero a 64 bit.</summary>
|
||||
public ulong NextU64()
|
||||
{
|
||||
ulong result = RotateLeft(_s1 * 5, 7) * 9;
|
||||
ulong t = _s1 << 17;
|
||||
_s2 ^= _s0;
|
||||
_s3 ^= _s1;
|
||||
_s1 ^= _s2;
|
||||
_s0 ^= _s3;
|
||||
_s2 ^= t;
|
||||
_s3 = RotateLeft(_s3, 45);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>Prossimo intero a 32 bit (bit alti, i migliori).</summary>
|
||||
public uint NextU32() => (uint)(NextU64() >> 32);
|
||||
|
||||
/// <summary>Intero uniforme in [0, maxExclusive) senza distorsione (metodo di Lemire).</summary>
|
||||
public int NextInt(int maxExclusive)
|
||||
{
|
||||
if (maxExclusive <= 0) throw new ArgumentOutOfRangeException(nameof(maxExclusive));
|
||||
ulong range = (ulong)maxExclusive;
|
||||
ulong x = NextU64() >> 32;
|
||||
ulong m = x * range;
|
||||
ulong low = m & 0xFFFFFFFFUL;
|
||||
if (low < range)
|
||||
{
|
||||
ulong threshold = (0x1_0000_0000UL - range) % range;
|
||||
while (low < threshold)
|
||||
{
|
||||
x = NextU64() >> 32;
|
||||
m = x * range;
|
||||
low = m & 0xFFFFFFFFUL;
|
||||
}
|
||||
}
|
||||
return (int)(m >> 32);
|
||||
}
|
||||
|
||||
/// <summary>Intero uniforme in [minInclusive, maxExclusive).</summary>
|
||||
public int NextInt(int minInclusive, int maxExclusive)
|
||||
{
|
||||
if (maxExclusive <= minInclusive) throw new ArgumentOutOfRangeException(nameof(maxExclusive));
|
||||
return minInclusive + NextInt(maxExclusive - minInclusive);
|
||||
}
|
||||
|
||||
/// <summary>Reale uniforme in [0, 1) con 53 bit di mantissa.</summary>
|
||||
public double NextDouble() => (NextU64() >> 11) * (1.0 / (1UL << 53));
|
||||
|
||||
/// <summary>Reale uniforme in [min, max).</summary>
|
||||
public double NextDouble(double min, double max) => min + (max - min) * NextDouble();
|
||||
|
||||
/// <summary>Vero con probabilità p.</summary>
|
||||
public bool Chance(double p) => p > 0 && (p >= 1 || NextDouble() < p);
|
||||
|
||||
/// <summary>Mescola un intervallo con Fisher-Yates.</summary>
|
||||
public void Shuffle<T>(Span<T> items)
|
||||
{
|
||||
for (int i = items.Length - 1; i > 0; i--)
|
||||
{
|
||||
int j = NextInt(i + 1);
|
||||
(items[i], items[j]) = (items[j], items[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private static ulong RotateLeft(ulong x, int k) => (x << k) | (x >> (64 - k));
|
||||
|
||||
private static ulong SplitMix64(ref ulong x)
|
||||
{
|
||||
ulong z = x += 0x9E3779B97F4A7C15UL;
|
||||
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL;
|
||||
z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL;
|
||||
return z ^ (z >> 31);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Text;
|
||||
|
||||
namespace ViaFerrea.Core.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Hash stabili tra processi e piattaforme (string.GetHashCode è randomizzato per processo e
|
||||
/// non va mai usato per effetti di simulazione).
|
||||
/// </summary>
|
||||
public static class StableHash
|
||||
{
|
||||
private const ulong FnvOffset = 0xCBF29CE484222325UL;
|
||||
private const ulong FnvPrime = 0x100000001B3UL;
|
||||
|
||||
/// <summary>FNV-1a a 64 bit sui byte UTF-8 della stringa.</summary>
|
||||
public static ulong Fnv1a64(string text)
|
||||
{
|
||||
ulong h = FnvOffset;
|
||||
Span<byte> buffer = stackalloc byte[256];
|
||||
int max = Encoding.UTF8.GetMaxByteCount(text.Length);
|
||||
byte[]? rented = max > buffer.Length ? new byte[max] : null;
|
||||
Span<byte> bytes = rented ?? buffer;
|
||||
int n = Encoding.UTF8.GetBytes(text, bytes);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
h ^= bytes[i];
|
||||
h *= FnvPrime;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
/// <summary>FNV-1a a 64 bit su un intervallo di byte.</summary>
|
||||
public static ulong Fnv1a64(ReadOnlySpan<byte> bytes, ulong seed = FnvOffset)
|
||||
{
|
||||
ulong h = seed;
|
||||
for (int i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
h ^= bytes[i];
|
||||
h *= FnvPrime;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<!-- Motore di simulazione deterministico: nessuna dipendenza oltre la BCL. -->
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net10.0;net8.0</TargetFrameworks>
|
||||
<RootNamespace>ViaFerrea.Core</RootNamespace>
|
||||
<IsTrimmable>true</IsTrimmable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in new issue
Block a user