Banco di prova ripetibile per tarare l'economia (ADR 0025, obiettivi O1–O14 scritti prima della taratura): - railenterprise-tools balance: semi × difficoltà × bot × anni in parallelo, rapporto deterministico (rapporto.txt con gli obiettivi, anni/mesi/prezzi/ esecuzioni.csv, tempi.txt); simulate --bot <id>. - Bot giocatore (PlayerBot): l'agente delle IA con le personalità di ai.json → bots guida la compagnia del giocatore; pianifica per primo; non è salvato né nell'hash, il formato dei salvataggi non cambia. - Le IA usano la compravendita in proprio (supera ADR 0016 §9) con una regola mensile deterministica e parametri in ai.json → trading. - Agente corretto: un trasformatore è fonte solo con gli input alimentati (prima i bot fallivano su linee rimaste senza merce finite le scorte iniziali); la linea dei ricambi di una rete nuova esce dal budget iniziale se l'utile la ripaga entro planning.partsDeferYears. - Dati: market.world volumeShare 0,5 → 1,0 ed elasticity 1,0 → 2,5; townBonus 1,8 → 3,0, townRoiFactor 0,5 → 0,35. Matrice predefinita (72 esecuzioni da 12 anni, 533 s): obiettivi da 5 a 9 su 14; livello 2 del bot competente in «Imprenditore» al 4° anno (prima 7°), nessun fallimento dei bot in «Apprendista» e «Imprenditore», prezzi medi tra 0,77 e 1,42 volte il base (prima 0,38–2,78). Restano aperti O7–O11. EconomyTests.PriceRisesWhenDemandExceedsSupply: la soglia passa da 1,5 a 1,25 volte in 120 giorni perché il mondo a volume pieno ed elasticità 2,5 assorbe parte dello squilibrio (aumento misurato ~1,36). Accettazioni F2, F4 e F5 invariate e verdi; 222 test verdi. Refs #47. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
6.1 KiB
C#
108 lines
6.1 KiB
C#
using RailEnterprise.Core.Commands;
|
|
using RailEnterprise.Core.Common;
|
|
using RailEnterprise.Core.Economy;
|
|
using RailEnterprise.Core.Persistence;
|
|
using RailEnterprise.Core.Rail;
|
|
using RailEnterprise.Core.Simulation;
|
|
using RailEnterprise.Core.Simulation.Ai;
|
|
using RailEnterprise.Core.Simulation.Systems;
|
|
using RailEnterprise.Tests.Harness;
|
|
using RailEnterprise.Tools.Balance;
|
|
using static RailEnterprise.Tests.Rail.RailFixture;
|
|
|
|
namespace RailEnterprise.Tests.Balance;
|
|
|
|
/// <summary>Banco di bilanciamento con i bot giocatore e compravendita delle IA (F6, issue #47, ADR 0025).</summary>
|
|
public static class BalanceTests
|
|
{
|
|
[Test(Description = "Il banco è deterministico: stesso seme, stesso rapporto (testo, CSV e hash finale)")]
|
|
public static void SameSeedGivesTheSameReport()
|
|
{
|
|
var o = new BalanceOptions { Seeds = [5], Difficulties = ["imprenditore"], Bots = ["bot_competent"], Years = 2, SizeKm = 64, Threads = 1 };
|
|
var a = BalanceBench.Run(TestData.Data, o);
|
|
var b = BalanceBench.Run(TestData.Data, o with { Threads = 2 });
|
|
Assert.Equal(BalanceReport.Text(TestData.Data, o, a), BalanceReport.Text(TestData.Data, o, b));
|
|
Assert.Equal(BalanceReport.YearsCsv(a), BalanceReport.YearsCsv(b));
|
|
Assert.Equal(BalanceReport.MonthsCsv(TestData.Data, a), BalanceReport.MonthsCsv(TestData.Data, b));
|
|
Assert.Equal(BalanceReport.RunsCsv(TestData.Data, a), BalanceReport.RunsCsv(TestData.Data, b));
|
|
Assert.Equal(a[0].FinalHash, b[0].FinalHash);
|
|
Assert.True(a[0].BotCommands > 0, "il bot ha giocato");
|
|
Assert.True(a[0].InvariantsOk, "invarianti di denaro e merci");
|
|
Assert.Equal(24, a[0].Months.Count);
|
|
}
|
|
|
|
[Test(Slow = true, Description = "Banco ridotto (ADR 0025): in «Imprenditore» il bot competente arriva al livello 2 tra il 3° e il 6° anno senza fallire, prezzi medi nella fascia, pochi rifiuti, invarianti")]
|
|
public static void BenchMeetsTheMainGoals()
|
|
{
|
|
var o = new BalanceOptions { Seeds = [1, 2, 3], Difficulties = ["imprenditore"], Bots = ["bot_competent"], Years = 6 };
|
|
var runs = BalanceBench.Run(TestData.Data, o);
|
|
var goals = BalanceReport.Goals(TestData.Data, runs);
|
|
foreach (var g in goals) Console.WriteLine($" {(g.Met is null ? "—" : g.Met.Value ? "sì" : "NO")} {g.Id} {g.Text}: {g.Measured}");
|
|
foreach (var id in new[] { "O1", "O5", "O6", "O12", "O13", "O14" })
|
|
{
|
|
var g = goals.Single(x => x.Id == id);
|
|
Assert.True(g.Met == true, $"{g.Id} {g.Text}: {g.Measured}");
|
|
}
|
|
}
|
|
|
|
[Test(Description = "Il bot guida la compagnia del giocatore con i comandi, non entra nel salvataggio e il mondo resta deterministico")]
|
|
public static void BotDrivesThePlayerAndIsNotSaved()
|
|
{
|
|
static World Make()
|
|
{
|
|
var w = TestWorlds.Small(11);
|
|
w.PlayerBot = PlayerBot.Create(w, "bot_competent");
|
|
return w;
|
|
}
|
|
var a = Make();
|
|
var b = Make();
|
|
a.RunYears(1);
|
|
b.RunYears(1);
|
|
Assert.Equal(StateHasher.Compute(a), StateHasher.Compute(b));
|
|
Assert.True(a.PlayerBot!.State.Issued > 0, "il bot emette comandi");
|
|
Assert.True(a.Network.Stations.Any(s => s.Owner == a.Player.Id), "stazioni del giocatore costruite dal bot");
|
|
Assert.True(a.PlayerBot.State.RejectedShare <= a.Data.Ai.Planning.MaxRejectedShare, $"rifiutati {a.PlayerBot.State.Rejected} su {a.PlayerBot.State.Issued}");
|
|
Assert.Equal(a.Companies.Count(c => !c.IsPlayer), a.AiStates.Count);
|
|
var loaded = SaveFile.Load(TestData.Data, SaveFile.Save(a, "test"));
|
|
Assert.True(loaded.PlayerBot is null, "il bot non è salvato");
|
|
Assert.Equal(StateHasher.Compute(a), StateHasher.Compute(loaded));
|
|
Assert.Throws<ArgumentException>(() => PlayerBot.Create(a, "expansive"));
|
|
}
|
|
|
|
[Test(Description = "Compravendita delle IA (ADR 0016 §9): con il produttore pieno e il consumatore a secco nello stesso bacino l'agente compra e vende, senza comandi rifiutati")]
|
|
public static void AgentTradesWhenTheMarginPays()
|
|
{
|
|
var f = Valdoria();
|
|
var w = f.W;
|
|
// stazione tra l'acciaieria e la fabbrica di attrezzi: produttore e consumatore dell'acciaio nello stesso bacino
|
|
int hub = f.Ok(new BuildStationCommand(K(11.7, 3.7), 2, "Acciaierie"));
|
|
var station = w.Network.Stations[hub];
|
|
var mill = w.Industries.First(i => i.Def.Id == "steel_mill");
|
|
var tools = w.Industries.First(i => i.Def.Id == "tool_factory");
|
|
Assert.True(station.Industries.Contains(mill.Id) && station.Industries.Contains(tools.Id), "bacino con acciaieria e fabbrica");
|
|
w.Player.Tier = w.Data.Tiers.Tiers.First(t => t.Unlocks.Trading).Level;
|
|
int steel = w.Data.Goods.First(g => g.Id == "steel").Index;
|
|
int cap = IndustryMath.OutputCapacity(mill, 0, w.Calendar.DaysPerMonth);
|
|
w.Goods.Produced[steel] += cap - mill.OutputStock[0];
|
|
mill.OutputStock[0] = cap;
|
|
int input = tools.InputIndexOf(steel);
|
|
w.Goods.Decayed[steel] += tools.InputStock[input];
|
|
tools.InputStock[input] = 0;
|
|
var q = TradingMath.Quote(w, station, steel);
|
|
Assert.True((q.SellPrice - q.BuyPrice) / q.BuyPrice > w.Data.Ai.Trading.MinMargin, $"margine atteso: compra {q.BuyPrice:0}, vende {q.SellPrice:0}");
|
|
|
|
w.PlayerBot = PlayerBot.Create(w, "bot_competent");
|
|
long millCash = mill.Cash.Cents, toolsCash = tools.Cash.Cents;
|
|
w.RunDays(w.Calendar.DaysPerMonth + 1);
|
|
long trading = w.Player.YearEntries[(int)LedgerEntry.Trading];
|
|
Assert.True(mill.Cash.Cents > millCash, "l'acciaieria ha venduto acciaio alla compagnia");
|
|
Assert.True(tools.Cash.Cents < toolsCash, "la fabbrica di attrezzi ha comprato l'acciaio");
|
|
Assert.True(trading > 0, $"utile della compravendita: {trading} centesimi");
|
|
Assert.Equal(0, station.StockOf(steel), "rivenduto subito alla fabbrica");
|
|
Assert.Equal(0L, w.PlayerBot.State.Rejected, "nessun comando rifiutato");
|
|
Assert.Equal(0L, w.MoneyBalanceSum());
|
|
for (int g = 0; g < w.Data.GoodCount; g++)
|
|
Assert.Equal(w.Goods.ExpectedStock(g), w.TotalStock(g), $"merce {w.Data.Goods[g].Id}");
|
|
}
|
|
}
|