economy balance: fix printer crash, label unpriced links by cause, cheaper recipe sets derived cost
verb-crashes: the link format string now gets the name argument; a test runs the full printer on the shipped catalog. unpriced-label-and-filler: BalanceLink.Unpriced says output or input; any Price 0 filler leaves a role unpriced. derived-last-writer: two recipes for one output take the cheaper derived cost, order independent. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a2e8e89292
commit
0650711537
4 changed files with 94 additions and 10 deletions
|
|
@ -4,13 +4,16 @@ using System.Linq;
|
|||
using GameCult.Caching;
|
||||
|
||||
// One recipe read as a chain link. Wage is the implied credits per worker-second at the authored prices; null when the link
|
||||
// is unpriced (an input, filler or output at Price 0) or has no labour. Cost is the derived cost of one output unit.
|
||||
// is unpriced (an input, any filler of a role, or the output at Price 0) or has no labour. Cost is the derived cost of one output unit.
|
||||
public sealed class BalanceLink
|
||||
{
|
||||
public string Output, Name, StationKind;
|
||||
public double Labour, Price, Cost;
|
||||
public double? Wage;
|
||||
public double? WageRatio;
|
||||
// Why Wage is null: "output" (the output is at Price 0), "input" (an input, upkeep or role filler is at Price 0), "labour" (no labour);
|
||||
// null when priced. The first that applies, in that order.
|
||||
public string Unpriced;
|
||||
}
|
||||
|
||||
// One ore a kind of belt holds: its share of the kind's abundance, its extraction link's wage over the median, its price and cost.
|
||||
|
|
@ -50,29 +53,30 @@ public static class EconomyBalance
|
|||
var columns = EconomyChecks.Columns(catalog);
|
||||
double Price(string key) => catalog.Get<ItemData>(new CultRecordKey(key))?.Price ?? 0;
|
||||
|
||||
// Authored-price wage per link; the cheapest priced filler fills a role.
|
||||
double? CheapestPriced(string[] fillers) => fillers.Select(Price).Where(p => p > 0).Select(p => (double?) p).Min();
|
||||
// Authored-price wage per link; the cheapest filler fills a role, and a role with any filler at Price 0 leaves the link unpriced.
|
||||
double? CheapestPriced(string[] fillers) => fillers.Length == 0 || fillers.Any(f => Price(f) <= 0) ? (double?) null : fillers.Min(Price);
|
||||
var links = new List<BalanceLink>();
|
||||
foreach (var c in columns)
|
||||
{
|
||||
var link = new BalanceLink { Output = c.Output, Name = catalog.Get(c.Recipe.Output)?.Name ?? c.Output, Labour = c.Labour, Price = Price(c.Output), StationKind = catalog.Get(c.Recipe.StationKind)?.Name ?? "no kind" };
|
||||
var cost = 0.0;
|
||||
var priced = link.Price > 0 && c.Labour > 0;
|
||||
var inputsPriced = true;
|
||||
foreach (var (commodity, count) in c.Inputs.Concat(c.Upkeep))
|
||||
{
|
||||
if (count <= 0) continue;
|
||||
var p = Price(commodity);
|
||||
if (p <= 0) priced = false;
|
||||
if (p <= 0) inputsPriced = false;
|
||||
cost += count * p;
|
||||
}
|
||||
foreach (var slot in c.Slots)
|
||||
{
|
||||
if (slot.Count <= 0) continue;
|
||||
var p = CheapestPriced(slot.Fillers);
|
||||
if (p == null) priced = false;
|
||||
if (p == null) inputsPriced = false;
|
||||
else cost += slot.Count * p.Value;
|
||||
}
|
||||
if (priced) link.Wage = (c.OutputCount * link.Price - cost) / c.Labour;
|
||||
link.Unpriced = link.Price <= 0 ? "output" : !inputsPriced ? "input" : c.Labour <= 0 ? "labour" : null;
|
||||
if (link.Unpriced == null) link.Wage = (c.OutputCount * link.Price - cost) / c.Labour;
|
||||
links.Add(link);
|
||||
}
|
||||
|
||||
|
|
@ -88,6 +92,8 @@ public static class EconomyBalance
|
|||
for (var sweep = 0; sweep < SweepCap && !converged; sweep++)
|
||||
{
|
||||
converged = true;
|
||||
// Two recipes for one output: the cheaper sets its cost, as a role takes its cheapest filler.
|
||||
var best = new Dictionary<string, double>(StringComparer.Ordinal);
|
||||
foreach (var c in columns)
|
||||
{
|
||||
var sum = reference * c.Labour;
|
||||
|
|
@ -95,8 +101,12 @@ public static class EconomyBalance
|
|||
foreach (var slot in c.Slots)
|
||||
if (slot.Count > 0 && slot.Fillers.Length > 0) sum += slot.Count * slot.Fillers.Min(Cost);
|
||||
var next = sum / c.OutputCount;
|
||||
if (Math.Abs(next - derived[c.Output]) > 1e-12 * Math.Max(1, Math.Abs(next))) converged = false;
|
||||
derived[c.Output] = next;
|
||||
if (!best.TryGetValue(c.Output, out var held) || next < held) best[c.Output] = next;
|
||||
}
|
||||
foreach (var (output, next) in best)
|
||||
{
|
||||
if (Math.Abs(next - derived[output]) > 1e-12 * Math.Max(1, Math.Abs(next))) converged = false;
|
||||
derived[output] = next;
|
||||
}
|
||||
}
|
||||
foreach (var l in links) l.Cost = derived[l.Output];
|
||||
|
|
|
|||
|
|
@ -107,6 +107,65 @@ public sealed class EconomyBalanceTests
|
|||
Assert.Empty(report.Faults);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnUnpricedLinkNamesWhetherItsOutputOrAnInputIsFree()
|
||||
{
|
||||
using var f = new EconomyFixture();
|
||||
f.Good("ore", workforce: 1, cycle: 1);
|
||||
f.Good("bar", inputs: new[] { ("ore", 1) }, workforce: 1, cycle: 1);
|
||||
f.Good("tin", workforce: 1, cycle: 1);
|
||||
Price(f, "commodity:bar", 80);
|
||||
Price(f, "commodity:tin", 7);
|
||||
var report = EconomyBalance.Balance(f.Cache);
|
||||
Assert.Equal("output", Link(report, "commodity:ore").Unpriced);
|
||||
Assert.Equal("input", Link(report, "commodity:bar").Unpriced);
|
||||
Assert.Null(Link(report, "commodity:tin").Unpriced);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AFreeFillerLeavesTheRoleUnpricedEvenWithAPricedOneBesideIt()
|
||||
{
|
||||
using var f = new EconomyFixture();
|
||||
f.Good("ore", workforce: 1, cycle: 1);
|
||||
f.Good("a-free", inputs: new[] { ("ore", 1) }, fills: new[] { "Plate" }, workforce: 1, cycle: 1);
|
||||
f.Good("z-priced", inputs: new[] { ("ore", 1) }, fills: new[] { "Plate" }, workforce: 1, cycle: 1);
|
||||
var gear = f.Gear("laser", new[] { ("Plate", 1) });
|
||||
Labour(f, gear, 1, 1);
|
||||
Price(f, "commodity:ore", 10);
|
||||
Price(f, "commodity:z-priced", 30);
|
||||
f.EditItem<GearData>(gear, g => g.Price = 100);
|
||||
var link = Link(EconomyBalance.Balance(f.Cache), gear);
|
||||
Assert.Null(link.Wage);
|
||||
Assert.Equal("input", link.Unpriced);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TwoRecipesForOneOutputTakeTheCheaperDerivedCostInEitherOrder()
|
||||
{
|
||||
foreach (var cheapFirst in new[] { true, false })
|
||||
{
|
||||
using var f = new EconomyFixture();
|
||||
f.Good("ore", workforce: 1, cycle: 1);
|
||||
f.Good("bar", inputs: new[] { ("ore", cheapFirst ? 1 : 3) }, workforce: 1, cycle: 1);
|
||||
f.Good("tin", workforce: 1, cycle: 1);
|
||||
Price(f, "commodity:ore", 10);
|
||||
Price(f, "commodity:bar", 80);
|
||||
Price(f, "commodity:tin", 7);
|
||||
// A second recipe for bar under its own key, written after the first, from a different input count.
|
||||
var first = f.Cache.Get<RecipeData>(RecipeData.KeyOf(new CultRecordKey("commodity:bar")));
|
||||
f.Cache.Upsert(typeof(RecipeData), new RecipeData
|
||||
{
|
||||
Output = first.Output, OutputCount = 1, Cycle = 1, Workforce = 1, StationKind = first.StationKind, LoreHook = "a good",
|
||||
Inputs = new System.Collections.Generic.List<CommodityCount> { new CommodityCount { Commodity = EconomyFixture.Item("commodity:ore"), Count = cheapFirst ? 3 : 1 } }
|
||||
}, new CultRecordKey("recipe:commodity:bar-alt"));
|
||||
var report = EconomyBalance.Balance(f.Cache);
|
||||
// Cost of one bar from one ore: ore cost plus the wage reference for one worker-second.
|
||||
var ore = Link(report, "commodity:ore").Cost;
|
||||
var cheap = ore + report.WageReference;
|
||||
Assert.All(report.Links.Where(l => l.Output == "commodity:bar"), l => Assert.Equal(cheap, l.Cost, 9));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShippedCatalogBalanceListsTheMaterializedChain()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -334,6 +334,21 @@ public sealed class EconomyCommandTests
|
|||
Assert.Contains("economy report | author", output.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BalanceOnTheShippedCatalogPrintsEveryLinkAndBeltRow()
|
||||
{
|
||||
using var db = AetherDb.Open();
|
||||
var report = EconomyBalance.Balance(db.Cache);
|
||||
var output = new StringWriter();
|
||||
EconomyCommand.Balance(db.Cache, 2, output);
|
||||
var text = output.ToString();
|
||||
Assert.Contains("Links by wage over the median", text);
|
||||
Assert.Contains("Belts (", text);
|
||||
foreach (var l in report.Links) Assert.Contains($" {l.Output} ({l.Name}) | ", text);
|
||||
foreach (var belt in report.Belts) Assert.Contains($" {belt.Kind}: price ", text);
|
||||
foreach (var l in report.Links.Where(l => l.Wage == null)) Assert.Contains($"unpriced {l.Unpriced}", text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReportOnTheShippedCatalogGoesToTheGivenWriterAndNeverToConsole()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ public static class EconomyCommand
|
|||
output.WriteLine("\nLinks by wage over the median (output | station kind | labour | price | derived cost | wage | ratio)");
|
||||
foreach (var l in report.Links.OrderBy(l => l.WageRatio ?? double.PositiveInfinity).ThenBy(l => l.Output, StringComparer.Ordinal))
|
||||
output.WriteLine(string.Format(inv, " {0} ({7}) | {1} | {2:0.#} | {3:0.##} | {4:0.##} | {5} | {6}", l.Output, l.StationKind, l.Labour, l.Price, l.Cost,
|
||||
l.Wage.HasValue ? l.Wage.Value.ToString("0.#####", inv) : "unpriced", l.WageRatio.HasValue ? l.WageRatio.Value.ToString("0.##", inv) + "x" : "-"));
|
||||
l.Wage.HasValue ? l.Wage.Value.ToString("0.#####", inv) : "unpriced " + l.Unpriced, l.WageRatio.HasValue ? l.WageRatio.Value.ToString("0.##", inv) + "x" : "-", l.Name));
|
||||
output.WriteLine("\nBelts (kind: abundance-weighted price and derived cost per unit; each ore: share, extraction wage ratio, price, cost)");
|
||||
foreach (var belt in report.Belts)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue