F2: pendolarismo su rotaia, carichi per linea, accettazione Valdoria e bench
- Abbinamento mensile dei pendolari sul LineGraph (60/90 minuti con i tratti a piedi, quote proporzionali ai lavoratori liberi, capacità residua delle linee), pacchetti giornalieri, coperture e scioglimento dei contratti. - LineGraph con maschere dei carichi: merci, lavoratori e personale viaggiano solo su linee i cui treni li possono portare; durata dei giri con le soste di carico e scarico. - Industria servita = merce ritirata o consegnata dal vettore. - Parametri residui spostati in data/*.json. - Test di accettazione F2 sullo scenario Valdoria con tre linee, test del pendolarismo (copertura > 0,9 in 60 giorni, capacità, scioglimento). - viaferrea-tools bench: 2.000 treni, tick medio 0,84 ms. - ADR 0007 e 0008, ARCHITECTURE e STATE aggiornati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
This commit is contained in:
27 files changed
+953
-84
No files matched your search
@@ -0,0 +1,103 @@
|
||||
using ViaFerrea.Core.Commands;
|
||||
using ViaFerrea.Core.Common;
|
||||
using ViaFerrea.Core.Data;
|
||||
using ViaFerrea.Core.Economy;
|
||||
using ViaFerrea.Core.Rail;
|
||||
using ViaFerrea.Tests.Harness;
|
||||
using static ViaFerrea.Tests.Rail.RailFixture;
|
||||
|
||||
namespace ViaFerrea.Tests.Rail;
|
||||
|
||||
public static class CommutingTests
|
||||
{
|
||||
/// <summary>Linea operai Borgo Ceresio ↔ acciaieria con un deposito sulla diramazione.</summary>
|
||||
private static (RailFixture F, Industry Mill, int Train, int Line) WorkersLine(int coaches, int staffCoaches = 0)
|
||||
{
|
||||
var f = Valdoria();
|
||||
int mill = f.Ok(new BuildStationCommand(K(10.6, 5.4), 2, "Acciaieria"));
|
||||
int borgo = f.Ok(new BuildStationCommand(K(12.4, 8.0), 2, "Borgo Ceresio"));
|
||||
int depot = f.Ok(new BuildDepotCommand(K(11.6, 7.4), "small", "Deposito di Borgo"));
|
||||
f.Ok(new BuildTrackCommand(f.Scenario!.Route("borgo", f.Me).Polyline));
|
||||
f.Ok(new BuildTrackCommand(new[] { K(11.6, 7.4), K(11.7, 6.85) }));
|
||||
Assert.True(f.RunUntil(() => f.W.Network.Edges.All(e => e.State == EdgeState.Active), 400), "cantieri completati");
|
||||
int train = staffCoaches > 0
|
||||
? f.Train(depot, "Operai 1", ("loco_steam_1850", 1), ("coach_worker", coaches), ("coach_staff", staffCoaches))
|
||||
: f.Train(depot, "Operai 1", ("loco_steam_1850", 1), ("coach_worker", coaches));
|
||||
int line = f.Ok(new CreateLineCommand("Operai di Borgo", "#4f7cac",
|
||||
new[] { new LineStop(new StationId(borgo)), new LineStop(new StationId(mill)) }));
|
||||
f.Ok(new AssignTrainCommand(new TrainId(train), new LineId(line)));
|
||||
return (f, f.W.Industries.First(i => i.Def.Id == "steel_mill"), train, line);
|
||||
}
|
||||
|
||||
/// <summary>Avanza fino a dopo il prossimo abbinamento mensile.</summary>
|
||||
private static void RunToNextMonth(RailFixture f)
|
||||
{
|
||||
long month = f.W.Clock.Day / f.W.Calendar.DaysPerMonth;
|
||||
while (f.W.Clock.Day / f.W.Calendar.DaysPerMonth == month) f.W.RunDays(1);
|
||||
f.W.RunDays(1);
|
||||
}
|
||||
|
||||
[Test(Description = "Pendolarismo (14.5): con città raggiungibile e capacità sufficiente la copertura supera 0.9 in 60 giorni")]
|
||||
public static void ReachableTownWithCapacityCoversJobsWithinSixtyDays()
|
||||
{
|
||||
var (f, mill, _, _) = WorkersLine(coaches: 2);
|
||||
RunToNextMonth(f);
|
||||
var contracts = f.W.Commutes.Where(c => !c.Walking && c.Workplace.Index == mill.Id.Value && c.Category == PaxClass.Worker).ToList();
|
||||
Assert.True(contracts.Count > 0, "contratti su rotaia creati all'abbinamento mensile");
|
||||
Assert.Equal(mill.JobsWorkers - (int)Math.Round(mill.JobsWorkers * f.W.Data.Balance.Industry.OnSiteWorkerShare), contracts.Sum(c => c.Headcount));
|
||||
foreach (var c in contracts)
|
||||
{
|
||||
Assert.Equal("Borgo Ceresio", f.W.Towns[c.Town.Value].Name);
|
||||
Assert.True(c.ExpectedMinutes <= f.W.Data.Balance.Employment.WorkerMaxMinutes);
|
||||
}
|
||||
f.W.RunDays(60);
|
||||
foreach (var c in contracts) Assert.True(c.Coverage > 0.9, $"copertura del contratto {c.Coverage:F2}");
|
||||
Assert.True(mill.CovWorkers > 0.9, $"copertura dei lavoratori dell'acciaieria {mill.CovWorkers:F2}");
|
||||
Assert.True(f.W.Player.YearEntries[(int)LedgerEntry.PassengerRevenue] > 0, "tariffe incassate");
|
||||
Assert.Equal(0L, f.W.MoneyBalanceSum());
|
||||
}
|
||||
|
||||
[Test(Description = "L'abbinamento non supera la capacità residua stimata della linea")]
|
||||
public static void MatchingRespectsLineCapacity()
|
||||
{
|
||||
var (f, mill, _, line) = WorkersLine(coaches: 1);
|
||||
// l'acciaieria al livello 3 offre più posti di quanti una carrozza ne porti
|
||||
mill.Level = 3;
|
||||
RunToNextMonth(f);
|
||||
var graph = f.W.LineGraphs.For(f.W, f.Me);
|
||||
double seats = graph.DailyCapacity[line].Workers * f.W.Data.Balance.Employment.LineCapacityUtilization;
|
||||
int contracted = f.W.Commutes.Where(c => !c.Walking).Sum(c => c.Headcount);
|
||||
Assert.True(contracted > 0 && contracted <= seats, $"{contracted} pendolari per {seats:F0} posti al giorno");
|
||||
Assert.True(contracted < EmploymentOpen(f, mill), "restano posti scoperti per mancanza di carrozze");
|
||||
}
|
||||
|
||||
private static int EmploymentOpen(RailFixture f, Industry mill) =>
|
||||
mill.JobsWorkers - (int)Math.Round(mill.JobsWorkers * f.W.Data.Balance.Industry.OnSiteWorkerShare);
|
||||
|
||||
[Test(Description = "Senza più treni sulla linea i contratti si sciolgono e la città se ne lamenta")]
|
||||
public static void ContractsDissolveWhenTheRouteDisappears()
|
||||
{
|
||||
var (f, mill, train, _) = WorkersLine(coaches: 2);
|
||||
RunToNextMonth(f);
|
||||
Assert.True(f.W.Commutes.Any(c => !c.Walking));
|
||||
var borgo = f.W.Towns.First(t => t.Name == "Borgo Ceresio");
|
||||
int employedBefore = borgo.EmployedWorkers;
|
||||
f.Ok(new AssignTrainCommand(new TrainId(train), LineId.None));
|
||||
RunToNextMonth(f);
|
||||
Assert.False(f.W.Commutes.Any(c => !c.Walking), "contratti sciolti");
|
||||
Assert.True(borgo.WorkDissatisfaction > 0, "insoddisfazione «lavoro»");
|
||||
Assert.True(borgo.EmployedWorkers < employedBefore, "i lavoratori tornano liberi");
|
||||
Assert.True(f.W.Events.Recent.Any(e => e.Text.Contains("perdono il posto", StringComparison.Ordinal)));
|
||||
Assert.Equal(0, mill.ContractedWorkers);
|
||||
}
|
||||
|
||||
[Test(Description = "Il personale qualificato viene solo da città con servizi: Borgo Ceresio non ne fornisce")]
|
||||
public static void StaffComesOnlyFromTownsWithServices()
|
||||
{
|
||||
var (f, mill, _, _) = WorkersLine(coaches: 2, staffCoaches: 1);
|
||||
RunToNextMonth(f);
|
||||
Assert.True(f.W.Commutes.Any(c => !c.Walking && c.Category == PaxClass.Worker));
|
||||
Assert.False(f.W.Commutes.Any(c => !c.Walking && c.Category == PaxClass.Staff));
|
||||
Assert.True(mill.StaffPresent < mill.JobsStaff, "il personale resta scoperto");
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,14 @@ public sealed class RailFixture
|
||||
return Ok(new ComposeTrainCommand(new DepotId(depot), ids, name));
|
||||
}
|
||||
|
||||
/// <summary>Compra veicoli di più tipi e compone un treno nel deposito.</summary>
|
||||
public int Train(int depot, string name, params (string Type, int Count)[] vehicles)
|
||||
{
|
||||
var ids = new List<VehicleId>();
|
||||
foreach (var (type, count) in vehicles) ids.AddRange(W_Buy(type, depot, count));
|
||||
return Ok(new ComposeTrainCommand(new DepotId(depot), ids, name));
|
||||
}
|
||||
|
||||
private IEnumerable<VehicleId> W_Buy(string type, int depot, int count) =>
|
||||
Do(new BuyVehicleCommand(type, new DepotId(depot), count)).Created.Select(i => new VehicleId(i));
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ public static class TrainTests
|
||||
Assert.True(t.Odometer > 50_000, $"il treno ha viaggiato ({t.Odometer / 1000:F1} km)");
|
||||
Assert.True(steel.InputStock.All(s => s > 0), "l'acciaieria riceve minerale e carbone");
|
||||
Assert.True(f.W.Player.YearEntries[(int)LedgerEntry.FreightRevenue] > revenueBefore, "tariffe incassate");
|
||||
Assert.True(steel.ServedWithin(f.W.Clock.Day, 1), "industria servita");
|
||||
Assert.True(steel.ServedWithin(f.W.Clock.Day, 10), "industria servita dal vettore");
|
||||
Assert.Equal(0L, f.W.MoneyBalanceSum());
|
||||
for (int g = 0; g < f.W.Data.GoodCount; g++)
|
||||
Assert.Equal(f.W.Goods.ExpectedStock(g), f.W.TotalStock(g), $"merce {f.W.Data.Goods[g].Id}");
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
using ViaFerrea.Core.Commands;
|
||||
using ViaFerrea.Core.Common;
|
||||
using ViaFerrea.Core.Data;
|
||||
using ViaFerrea.Core.Rail;
|
||||
using ViaFerrea.Tests.Harness;
|
||||
using ViaFerrea.Tests.Rail;
|
||||
using static ViaFerrea.Tests.Rail.RailFixture;
|
||||
|
||||
namespace ViaFerrea.Tests.Scenarios;
|
||||
|
||||
public static class CommutingAcceptanceTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Rete di Valdoria con tre linee: merci (miniere → acciaieria → Valdoria, con minerale, carbone
|
||||
/// e acciaio), lavoratori (Borgo Ceresio → acciaieria) e personale (Valdoria → acciaieria).
|
||||
/// </summary>
|
||||
private static (RailFixture F, Core.Economy.Industry Mill) ThreeLines()
|
||||
{
|
||||
var f = Valdoria();
|
||||
var (mine, mill, depot) = f.BuildFreightNetwork(waitForConstruction: false);
|
||||
int borgo = f.Ok(new BuildStationCommand(K(12.4, 8.0), 2, "Borgo Ceresio"));
|
||||
int valdoria = f.Ok(new BuildStationCommand(K(12.8, 4.9), 2, "Valdoria"));
|
||||
f.Ok(new BuildTrackCommand(f.Scenario!.Route("borgo", f.Me).Polyline));
|
||||
f.Ok(new BuildTrackCommand(f.Scenario!.Route("valdoria", f.Me).Polyline));
|
||||
Assert.True(f.RunUntil(() => f.W.Network.Edges.All(e => e.State == EdgeState.Active), 500), "cantieri completati");
|
||||
|
||||
var steel = f.W.Industries.First(i => i.Def.Id == "steel_mill");
|
||||
Assert.True(steel.Efficiency < 0.4, $"efficienza iniziale {steel.Efficiency:P0}");
|
||||
|
||||
int freight = f.Train(depot, "Merci 1", ("loco_steam_1850", 1), ("wagon_hopper", 6), ("wagon_flat", 2));
|
||||
int workers = f.Train(depot, "Operai 1", ("loco_steam_1850", 1), ("coach_worker", 2));
|
||||
int staff = f.Train(depot, "Tecnici 1", ("loco_steam_1850", 1), ("coach_staff", 1));
|
||||
int lineFreight = f.Ok(new CreateLineCommand("Linea 1, minerale, carbone e acciaio", "#e0a526",
|
||||
new[] { new LineStop(new StationId(mine)), new LineStop(new StationId(mill)), new LineStop(new StationId(valdoria)) }));
|
||||
int lineWorkers = f.Ok(new CreateLineCommand("Linea 2, operai di Borgo", "#4f7cac",
|
||||
new[] { new LineStop(new StationId(borgo)), new LineStop(new StationId(mill)) }));
|
||||
int lineStaff = f.Ok(new CreateLineCommand("Linea 3, tecnici di Valdoria", "#b3573e",
|
||||
new[] { new LineStop(new StationId(valdoria)), new LineStop(new StationId(mill)) }));
|
||||
f.Ok(new AssignTrainCommand(new TrainId(freight), new LineId(lineFreight)));
|
||||
f.Ok(new AssignTrainCommand(new TrainId(workers), new LineId(lineWorkers)));
|
||||
f.Ok(new AssignTrainCommand(new TrainId(staff), new LineId(lineStaff)));
|
||||
return (f, steel);
|
||||
}
|
||||
|
||||
[Test(Description = "Accettazione F2: con tre linee (merci, lavoratori, personale) l'acciaieria passa da < 40 % a > 80 % in 2 anni senza stalli")]
|
||||
public static void ThreeLinesBringTheSteelMillAboveEightyPercent()
|
||||
{
|
||||
var (f, steel) = ThreeLines();
|
||||
int startLevel = steel.Level;
|
||||
double bestMonth = 0;
|
||||
int dpm = f.W.Calendar.DaysPerMonth;
|
||||
for (int month = 0; month < 24; month++)
|
||||
{
|
||||
// media mensile dell'efficienza giornaliera: un picco di un giorno non basta
|
||||
double sum = 0;
|
||||
for (int d = 0; d < dpm; d++)
|
||||
{
|
||||
f.W.RunDays(1);
|
||||
sum += steel.Efficiency;
|
||||
}
|
||||
bestMonth = Math.Max(bestMonth, sum / dpm);
|
||||
}
|
||||
Assert.True(bestMonth > 0.8, $"efficienza media mensile massima {bestMonth:P0}");
|
||||
Assert.False(steel.Closed);
|
||||
Assert.True(steel.Level >= startLevel, $"livello {steel.Level}");
|
||||
Assert.False(f.W.Events.Recent.Any(e => e.Text.Contains("possibile stallo", StringComparison.Ordinal)), "nessuno stallo");
|
||||
Assert.True(f.W.Commutes.Any(c => !c.Walking && c.Category == PaxClass.Worker), "contratti di lavoratori su rotaia");
|
||||
Assert.True(f.W.Commutes.Any(c => !c.Walking && c.Category == PaxClass.Staff), "contratti di personale su rotaia");
|
||||
Assert.True(f.W.Player.YearEntries[(int)Core.Economy.LedgerEntry.PassengerRevenue] > 0, "tariffe dei pendolari incassate");
|
||||
Assert.Equal(0L, f.W.MoneyBalanceSum());
|
||||
for (int g = 0; g < f.W.Data.GoodCount; g++)
|
||||
Assert.Equal(f.W.Goods.ExpectedStock(g), f.W.TotalStock(g), $"merce {f.W.Data.Goods[g].Id}");
|
||||
}
|
||||
}
|
||||
@@ -28,8 +28,8 @@
|
||||
{ "name": "Sant'Elia", "x": 10.2, "y": 1.2, "population": 2200 }
|
||||
],
|
||||
"industries": [
|
||||
{ "type": "iron_mine", "name": "Miniera di ferro Rocca Bruna", "x": 1.2, "y": 3.0, "level": 2, "reserveTonnes": 600000 },
|
||||
{ "type": "coal_mine", "name": "Miniera di carbone Monteferro", "x": 1.8, "y": 1.4, "level": 2, "reserveTonnes": 600000 },
|
||||
{ "type": "iron_mine", "name": "Miniera di ferro Rocca Bruna", "x": 1.2, "y": 3.0, "level": 4, "reserveTonnes": 600000 },
|
||||
{ "type": "coal_mine", "name": "Miniera di carbone Monteferro", "x": 1.8, "y": 1.4, "level": 3, "reserveTonnes": 600000 },
|
||||
{ "type": "steel_mill", "name": "Acciaieria di Valdoria", "x": 10.8, "y": 4.4, "level": 1 },
|
||||
{ "type": "tool_factory", "name": "Fabbrica di attrezzi", "x": 12.6, "y": 3.0, "level": 1 },
|
||||
{ "type": "farm", "name": "Fattoria Piana Verde", "x": 12.0, "y": 6.6, "level": 1 }
|
||||
|
||||
Reference in new issue
Block a user