Files
Rail_Enterprise/tests/ViaFerrea.Tests/Economy/AiTests.cs
T
Claude 7879ac502d F5: manutenzione delle industrie, livelli, borsa e scalate, agenti IA
- Sezione 11: usura e recupero degli impianti, fermi e incidenti, ricambi e macchinari come
  domanda esplicita (treno, trasporto locale, trasbordo nel bacino), revisione e ammodernamento,
  acquisto delle industrie dal livello 3, conguaglio mensile con il proprietario (ADR 0011).
- Livelli della compagnia con soglie ritarate e ritaratura economica misurata con le IA.
- Sezione 12.2–12.3: libro dei soci, prezzo con sentiment, quotazione, riacquisto, dividendi,
  controllo e fusione (comando 39), offerte pubbliche con difese, esiti per difficoltà,
  aste delle IA fallite, nuovi concorrenti, fine partita e Cronaca finale (ADR 0012).
- Sezione 12.1: agenti IA con candidati, Dijkstra sulle celle, TrackPreview e ROI, tre
  personalità, linee dei ricambi, rimedio agli stalli, borsa (ADR 0013).
- Salvataggio versione 3, protocollo (messaggi 126–128, blocchi di estensione), server,
  simulate --trace-ai; test di 14.5 su IA e borsa e accettazione F5 (120 test).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
2026-10-12 10:36:31 +00:00

88 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using ViaFerrea.Core.Commands;
using ViaFerrea.Core.Common;
using ViaFerrea.Core.Persistence;
using ViaFerrea.Core.Simulation;
using ViaFerrea.Core.Simulation.Ai;
using ViaFerrea.Tests.Harness;
namespace ViaFerrea.Tests.Economy;
/// <summary>Concorrenti IA (guida 12.1, test di 14.5): comandi validi, crescita, determinismo.</summary>
public static class AiTests
{
[Test(Slow = true, Description = "Le IA agiscono solo con i comandi e ne vedono rifiutati al massimo il 5 %")]
public static void AiCommandsAreRarelyRejected()
{
var w = TestWorlds.Small(42);
w.RunYears(5);
Assert.True(w.AiStates.Count >= 2, "uno stato per ogni IA");
long issued = 0;
foreach (var s in w.AiStates)
{
issued += s.Issued;
Assert.True(s.RejectedShare <= w.Data.Ai.Planning.MaxRejectedShare,
$"{w.Company(s.Company).Name}: {s.Rejected} rifiutati su {s.Issued}");
}
Assert.True(issued > 20, $"le IA costruiscono e gestiscono la rete ({issued} comandi)");
Assert.True(w.AiStates.Any(s => s.Projects.Any(p => p.Stage == AiProjectStage.Running)), "almeno una linea in esercizio");
// i binari, i treni e le stazioni delle IA esistono davvero nel mondo
foreach (var s in w.AiStates)
{
foreach (var p in s.Projects.Where(p => p.Stage == AiProjectStage.Running))
{
Assert.True(w.Network.IsAlive(w.Network.Stations[p.StationA.Value]) || w.Company(s.Company).Defunct);
Assert.True(p.Trains.All(t => w.Trains[t.Value].Owner == s.Company || w.Company(s.Company).Merged));
}
}
}
[Test(Slow = true, Description = "Un'IA con capitale ×2 supera il giocatore passivo in 10 anni")]
public static void AiWithDoubleCapitalBeatsAPassivePlayer()
{
var w = TestWorlds.Small(42, "imprenditore", ("aiCapitalMultiplier", 2));
w.RunYears(10);
var player = w.Player;
var best = w.Companies.Where(c => !c.IsPlayer).MaxBy(c => c.NetWorth.Cents)!;
Assert.True(best.NetWorth > player.NetWorth, $"{best.Name}: {best.NetWorth} contro {player.NetWorth}");
Assert.True(best.NetWorth.Cents > w.Difficulty.InitialCapital * 2, $"{best.Name} ha fatto crescere il capitale: {best.NetWorth}");
}
[Test(Slow = true, Description = "Determinismo con le IA: stesso seme e stessi comandi, stesso hash dopo 5 anni (anche passando da un salvataggio)")]
public static void SameSeedWithAiGivesSameHash()
{
static World Make()
{
var w = TestWorlds.Small(77, "magnate");
w.Enqueue(new TakeLoanCommand(Money.FromLire(50_000)) { Issuer = w.Player.Id, Seq = 1 });
return w;
}
var a = Make();
var b = Make();
a.RunYears(2);
b.RunYears(2);
Assert.Equal(StateHasher.Compute(a), StateHasher.Compute(b));
var c = SaveFile.Load(TestData.Data, SaveFile.Save(b, "test"));
a.RunYears(3);
c.RunYears(3);
Assert.Equal(StateHasher.Compute(a), StateHasher.Compute(c));
Assert.True(a.AiStates.Sum(s => s.Issued) > 0, "le IA hanno agito");
}
[Test(Slow = true, Description = "Accettazione F5: in «Magnate», con il giocatore passivo, almeno un'IA raggiunge il livello 3 in 15 anni")]
public static void OnMagnateAnAiReachesTierThreeIn15Years()
{
var w = WorldFactory.Create(TestData.Data, new NewGameSettings { Seed = 7, SizeKm = 128, DifficultyId = "magnate" });
int best = 1;
for (int y = 0; y < 15 && best < 3; y++)
{
w.RunYears(1);
best = w.Companies.Where(c => !c.IsPlayer).Max(c => c.Tier);
}
var leader = w.Companies.Where(c => !c.IsPlayer).MaxBy(c => (c.Tier, c.NetWorth.Cents))!;
Console.WriteLine($" {leader.Name}: livello {leader.Tier} nel {w.Clock.Year}, patrimonio {leader.NetWorth}, rating {leader.Rating}, " +
$"città collegate {Core.Simulation.Systems.Tiers.ConnectedTowns(w, leader.Id, 1000)}");
Assert.True(best >= 3, $"livello massimo delle IA: {best}");
Assert.Equal(1, w.Player.Tier, "il giocatore passivo resta al livello 1");
}
}