A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
feat(sim): population and aliyah from sourced series
eyalgolanab8f5786f992
diff --git a/sim/Statecraft.Sim.Tests/PopulationTests.cs b/sim/Statecraft.Sim.Tests/PopulationTests.csnew file mode 100644index 000000000..8d4b5ce63--- /dev/null+++ b/sim/Statecraft.Sim.Tests/PopulationTests.cs@@ -0,0 +1,53 @@+using System;+using System.Collections.Generic;+using Statecraft.Sim;++public class PopulationTests+{+ static readonly Content C = ContentLoader.Load(Fixture.Write());++ [Fact]+ public void ArrivalsAreSpreadByOverlappingDays()+ {+ // Fixture period 1948-05-15..1948-12-31 inclusive = 231 days, 230 from europe.+ var a = PopulationSystem.Arrivals(C, new DateTime(1948, 5, 15), new DateTime(1948, 8, 15)); // 92 days+ Assert.Equal(230.0 * 92 / 231, a["europe"], 9);+ Assert.Equal(0.0, a["asia"], 9);+ }++ [Fact]+ public void WholePeriodSumsToSeriesTotal()+ {+ var a = PopulationSystem.Arrivals(C, new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));+ Assert.Equal(230.0, a["europe"], 9);+ }++ [Fact]+ public void NoArrivalsOutsideSeries() =>+ Assert.Empty(PopulationSystem.Arrivals(C, new DateTime(1950, 1, 1), new DateTime(1950, 4, 1)));++ [Fact]+ public void ApplyAddsNaturalIncreaseThenArrivals()+ {+ var s = new GameState { Date = new DateTime(1948, 5, 15) };+ PopulationSystem.Init(C, s);+ var why = new List<Cause>();+ PopulationSystem.Apply(C, s, new DateTime(1948, 8, 15), 0.25, why);+ Assert.Equal(1000 * (1 + 0.017 * 0.25), s.Population["veterans"], 9);+ Assert.Equal(230.0 * 92 / 231, s.Population["europe"], 9);+ Assert.Contains(why, w => w.Target == "europe");+ }++ [Fact]+ public void ArrivalsAcrossLeapDayAndYearEndSumToThePeriodTotal()+ {+ // 1951-12-10..1952-03-20 inclusive = 22 + 31 + 29 (leap year) + 20 = 102 days.+ var aliyah = "series:\n - from: 1951-12-10\n to: 1952-03-20\n origins: { europe: 1000 }\n";+ var c = ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["aliyah.yaml"] = aliyah }));+ double sum = 0;+ for (var d = new DateTime(1951, 10, 1); d < new DateTime(1952, 7, 1); d = d.AddMonths(3))+ sum += PopulationSystem.Arrivals(c, d, d.AddMonths(3)).TryGetValue("europe", out var v) ? v : 0;+ Assert.Equal(1000.0, sum, 6);+ Assert.Equal(1000.0 * 80 / 102, PopulationSystem.Arrivals(c, new DateTime(1952, 1, 1), new DateTime(1952, 4, 1))["europe"], 6);+ }+}diff --git a/sim/Statecraft.Sim/PopulationSystem.cs b/sim/Statecraft.Sim/PopulationSystem.csnew file mode 100644index 000000000..e9a246aaa--- /dev/null+++ b/sim/Statecraft.Sim/PopulationSystem.cs@@ -0,0 +1,41 @@+using System;+using System.Collections.Generic;+using System.Linq;++namespace Statecraft.Sim+{+ /// <summary>Jewish population by origin group. Historical mode takes arrivals from the sourced aliyah series.</summary>+ public static class PopulationSystem+ {+ public static void Init(Content c, GameState s) => s.Population = new Dictionary<string, double>(c.Rules.InitialPopulation);++ /// <summary>Arrivals in [from, to). Each series period is inclusive of both dates and spread evenly by day.</summary>+ public static Dictionary<string, double> Arrivals(Content c, DateTime from, DateTime to)+ {+ var result = new Dictionary<string, double>();+ foreach (var p in c.Aliyah)+ {+ var pEnd = p.To.AddDays(1);+ var start = from > p.From ? from : p.From;+ var end = to < pEnd ? to : pEnd;+ var overlap = (end - start).TotalDays;+ if (overlap <= 0) continue;+ var share = overlap / (pEnd - p.From).TotalDays;+ foreach (var kv in p.Origins)+ result[kv.Key] = (result.TryGetValue(kv.Key, out var v) ? v : 0) + kv.Value * share;+ }+ return result;+ }++ public static void Apply(Content c, GameState s, DateTime to, double yearFraction, List<Cause> why)+ {+ var rate = c.Rules.NaturalIncreasePerYear * yearFraction;+ foreach (var k in s.Population.Keys.ToList()) s.Population[k] *= 1 + rate;+ foreach (var kv in Arrivals(c, s.Date, to).OrderBy(k => k.Key, StringComparer.Ordinal))+ {+ s.Population[kv.Key] = (s.Population.TryGetValue(kv.Key, out var v) ? v : 0) + kv.Value;+ if (kv.Value > 0) why.Add(new Cause("population", kv.Key, kv.Value, $"{kv.Value:N0} immigrants arrived from {kv.Key}"));+ }+ }+ }+}