A chain that does not settle has no price: author refuses it, L-price reports it; the clamp is deleted. Pin the L-price wiring, the floor of 1 and upkeep
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
24b000eba8
commit
305d9cde73
5 changed files with 128 additions and 17 deletions
|
|
@ -46,10 +46,15 @@ public sealed class BalanceReport
|
|||
public static class EconomyBalance
|
||||
{
|
||||
public const string HMargin = "H-margin", HWage = "H-wage", HConverge = "H-converge";
|
||||
private const int SweepCap = 10000;
|
||||
private const int SweepCap = 10000, SettleWindow = 100;
|
||||
|
||||
// The stored price a derived price becomes: whole credits, at least 1. A chain that does not settle diverges; it reads as the largest price.
|
||||
public static int Rounded(double derived) => !(derived < int.MaxValue) ? int.MaxValue : (int) Math.Max(1, Math.Round(derived, MidpointRounding.AwayFromZero));
|
||||
// The stored price a derived price becomes: whole credits, at least 1.
|
||||
public static int Rounded(double derived) => (int) Math.Max(1, Math.Round(derived, MidpointRounding.AwayFromZero));
|
||||
|
||||
// The words for goods whose price does not settle.
|
||||
public static string Unsettled(CultCache catalog, IEnumerable<string> keys) =>
|
||||
$"No price settles within {SweepCap} sweeps: these goods sit on a cycle of recipes whose requirements do not shrink: " +
|
||||
string.Join(", ", keys.Select(k => catalog.Get<ItemData>(new CultRecordKey(k))?.Name ?? k));
|
||||
|
||||
private static double Price(CultCache catalog, string key) => catalog.Get<ItemData>(new CultRecordKey(key))?.Price ?? 0;
|
||||
|
||||
|
|
@ -99,12 +104,14 @@ public static class EconomyBalance
|
|||
|
||||
// Value iteration from zero over every recipe at one wage. A good in fixedOutputs, or with no recipe, holds its authored price.
|
||||
// Two recipes for one output: the cheaper sets its price, as a role takes its cheapest filler.
|
||||
private static Dictionary<string, double> Sweep(CultCache catalog, List<EconomyChecks.Column> columns, double wage, ISet<string> fixedOutputs, out bool converged)
|
||||
private static Dictionary<string, double> Sweep(CultCache catalog, List<EconomyChecks.Column> columns, double wage, ISet<string> fixedOutputs, out List<string> unsettled)
|
||||
{
|
||||
var derived = new Dictionary<string, double>(StringComparer.Ordinal);
|
||||
foreach (var c in columns) if (!fixedOutputs.Contains(c.Output)) derived[c.Output] = 0;
|
||||
double Cost(string key) => derived.TryGetValue(key, out var v) ? v : Price(catalog, key);
|
||||
converged = false;
|
||||
var converged = false;
|
||||
// The last sweep each good moved in: a cycle can move one good per sweep, so the last sweep alone would name half of it.
|
||||
var lastMoved = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
for (var sweep = 0; sweep < SweepCap && !converged; sweep++)
|
||||
{
|
||||
converged = true;
|
||||
|
|
@ -120,21 +127,24 @@ public static class EconomyBalance
|
|||
}
|
||||
foreach (var (output, next) in best)
|
||||
{
|
||||
if (Math.Abs(next - derived[output]) > 1e-12 * Math.Max(1, Math.Abs(next))) converged = false;
|
||||
// A price that is not a finite number is not settled, however little it moved.
|
||||
if (!double.IsFinite(next) || Math.Abs(next - derived[output]) > 1e-12 * Math.Max(1, Math.Abs(next))) { converged = false; lastMoved[output] = sweep; }
|
||||
derived[output] = next;
|
||||
}
|
||||
}
|
||||
unsettled = converged ? new List<string>() : lastMoved.Where(p => p.Value >= SweepCap - SettleWindow).Select(p => p.Key).OrderBy(k => k, StringComparer.Ordinal).ToList();
|
||||
return derived;
|
||||
}
|
||||
|
||||
// The price of every made commodity (a commodity whose recipe is not raw) at the going wage: its inputs, upkeep and cheapest
|
||||
// role fillers at their prices, plus the wage times its labour, over its output count. Raw and recipeless goods keep their
|
||||
// authored price; designs are not priced here.
|
||||
public static Dictionary<string, double> Prices(CultCache catalog)
|
||||
// authored price; designs are not priced here. Unsettled names the goods still moving when the sweep gave up: a cycle of recipes
|
||||
// whose requirements do not shrink has no price, so none of the values are a price.
|
||||
public static Dictionary<string, double> Prices(CultCache catalog, out List<string> unsettled)
|
||||
{
|
||||
var columns = EconomyChecks.Columns(catalog);
|
||||
var raw = new HashSet<string>(columns.Where(c => Recipes.IsRaw(catalog, c.Recipe)).Select(c => c.Output), StringComparer.Ordinal);
|
||||
var derived = Sweep(catalog, columns, RawWage(catalog, columns, Links(catalog, columns)), raw, out _);
|
||||
var derived = Sweep(catalog, columns, RawWage(catalog, columns, Links(catalog, columns)), raw, out unsettled);
|
||||
return columns.Where(c => c.IsCommodity && !raw.Contains(c.Output)).Select(c => c.Output).Distinct(StringComparer.Ordinal)
|
||||
.ToDictionary(key => key, key => derived[key], StringComparer.Ordinal);
|
||||
}
|
||||
|
|
@ -147,7 +157,8 @@ public static class EconomyBalance
|
|||
foreach (var l in links) l.WageRatio = reference > 0 ? l.Wage / reference : null;
|
||||
|
||||
// Derived cost: nothing fixed, so an ore costs its labour; belt rows show price minus cost, the rent.
|
||||
var derived = Sweep(catalog, columns, reference, new HashSet<string>(), out var converged);
|
||||
var derived = Sweep(catalog, columns, reference, new HashSet<string>(), out var unsettled);
|
||||
var converged = unsettled.Count == 0;
|
||||
double Cost(string key) => derived.TryGetValue(key, out var v) ? v : Price(catalog, key);
|
||||
foreach (var l in links) l.Cost = derived[l.Output];
|
||||
|
||||
|
|
|
|||
|
|
@ -168,7 +168,13 @@ public static class EconomyChecks
|
|||
public static List<EconomyFault> PriceCheck(CultCache catalog)
|
||||
{
|
||||
var faults = new List<EconomyFault>();
|
||||
foreach (var (key, derived) in EconomyBalance.Prices(catalog).OrderBy(p => p.Key, StringComparer.Ordinal))
|
||||
var prices = EconomyBalance.Prices(catalog, out var unsettled);
|
||||
if (unsettled.Count > 0)
|
||||
{
|
||||
faults.Add(new EconomyFault(LPrice, unsettled[0], EconomyBalance.Unsettled(catalog, unsettled)));
|
||||
return faults;
|
||||
}
|
||||
foreach (var (key, derived) in prices.OrderBy(p => p.Key, StringComparer.Ordinal))
|
||||
{
|
||||
var item = catalog.Get<ItemData>(new CultRecordKey(key));
|
||||
if (item == null) continue;
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ public sealed class DerivedPriceTests
|
|||
{
|
||||
using var f = Chain();
|
||||
Assert.Equal(6.25, EconomyBalance.Wage(f.Cache), 9);
|
||||
var prices = EconomyBalance.Prices(f.Cache);
|
||||
var prices = EconomyBalance.Prices(f.Cache, out _);
|
||||
// bar = (3 x 50 + 6.25 x 10) / 2; the ore stays at its authored 50, not its labour cost 125.
|
||||
Assert.Equal(106.25, prices["commodity:bar"], 9);
|
||||
Assert.Equal(2 * 106.25 + 6.25 * 4, prices["commodity:part"], 9);
|
||||
|
|
@ -137,8 +137,8 @@ public sealed class DerivedPriceTests
|
|||
{
|
||||
using var one = Chain();
|
||||
using var hundred = Chain(100);
|
||||
var a = EconomyBalance.Prices(one.Cache);
|
||||
var b = EconomyBalance.Prices(hundred.Cache);
|
||||
var a = EconomyBalance.Prices(one.Cache, out _);
|
||||
var b = EconomyBalance.Prices(hundred.Cache, out _);
|
||||
foreach (var (key, price) in a) Assert.Equal(price * 100, b[key], 7);
|
||||
}
|
||||
|
||||
|
|
@ -147,10 +147,102 @@ public sealed class DerivedPriceTests
|
|||
{
|
||||
using var cache = RestoredHullsTests.OpenCatalog();
|
||||
Assert.Empty(EconomyChecks.PriceCheck(cache));
|
||||
var prices = EconomyBalance.Prices(cache);
|
||||
var prices = EconomyBalance.Prices(cache, out _);
|
||||
Assert.NotEmpty(prices);
|
||||
Assert.All(prices.Keys, key => Assert.True(cache.Get<ItemData>(new CultRecordKey(key)).Price >= 1));
|
||||
foreach (var name in new[] { "Water", "Methane", "Diamond" })
|
||||
Assert.True(cache.GetAll<SimpleCommodityData>().Concat<ItemData>(cache.GetAll<CompoundCommodityData>()).Single(i => i.Name == name).Price > 0, name);
|
||||
}
|
||||
|
||||
// ore (raw, price 50) feeds a, a feeds b, b feeds a again: the cycle's radius is sqrt(ratio), so 1 and 1.0005 are the edge cases.
|
||||
private static EconomyFixture Cycle(int bPerA, int perBatch)
|
||||
{
|
||||
var f = new EconomyFixture();
|
||||
f.Good("ore", workforce: 1, cycle: 1);
|
||||
Price(f, "commodity:ore", 50);
|
||||
f.Good("a", inputs: new[] { ("ore", 1), ("b", 1) });
|
||||
f.Good("b", inputs: new[] { ("a", bPerA) }, outputCount: perBatch);
|
||||
return f;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(4, 1)]
|
||||
[InlineData(1001, 1000)]
|
||||
[InlineData(1, 1)]
|
||||
public void AChainThatDoesNotSettleHasNoPriceAndAuthorRefusesIt(int bPerA, int perBatch)
|
||||
{
|
||||
using var f = Cycle(bPerA, perBatch);
|
||||
EconomyBalance.Prices(f.Cache, out var unsettled);
|
||||
Assert.Equal(new[] { "commodity:a", "commodity:b" }, unsettled);
|
||||
var fault = Assert.Single(EconomyChecks.Check(f.Cache).Faults, x => x.Invariant == EconomyChecks.LPrice);
|
||||
Assert.Contains("a, b", fault.Message);
|
||||
f.Cache.FlushAsync().GetAwaiter().GetResult();
|
||||
var bytes = File.ReadAllBytes(f.CatalogPath);
|
||||
var code = Author(f, true, out var output);
|
||||
Assert.Equal(1, code);
|
||||
Assert.Contains("No price settles", output);
|
||||
Assert.Contains("a, b", output);
|
||||
Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ACycleThatShrinksSettlesAndIsPriced()
|
||||
{
|
||||
using var f = Cycle(1, 2);
|
||||
var prices = EconomyBalance.Prices(f.Cache, out var unsettled);
|
||||
Assert.Empty(unsettled);
|
||||
// a = 50 + b, b = a / 2: a = 100, b = 50.
|
||||
Assert.Equal(100, prices["commodity:a"], 6);
|
||||
Assert.Equal(50, prices["commodity:b"], 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ADriftedMadePriceIsInTheReportAndAnIntroducedOneRefusesAuthor()
|
||||
{
|
||||
using var f = Chain();
|
||||
Assert.Equal(2, EconomyChecks.Check(f.Cache).Faults.Count(x => x.Invariant == EconomyChecks.LPrice));
|
||||
// Tool upkeep is a made good: pricing it moves the raw wage the made price reads, so the repriced catalog is itself off its derived price.
|
||||
using var g = new EconomyFixture();
|
||||
g.Good("crude", workforce: 1, cycle: 1);
|
||||
g.Good("fuel", inputs: new[] { ("crude", 1) }, workforce: 1, cycle: 1);
|
||||
g.Good("ore", workforce: 1, cycle: 1, upkeep: new[] { ("fuel", 5) });
|
||||
Price(g, "commodity:ore", 300);
|
||||
Price(g, "commodity:fuel", 1);
|
||||
Price(g, "commodity:crude", 100);
|
||||
g.Cache.FlushAsync().GetAwaiter().GetResult();
|
||||
var bytes = File.ReadAllBytes(g.CatalogPath);
|
||||
Assert.Equal(1, Author(g, true, out var output));
|
||||
Assert.Contains("L-price", output);
|
||||
Assert.Equal(bytes, File.ReadAllBytes(g.CatalogPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AMadePriceIsWholeCreditsAtLeastOneRoundedHalfAwayFromZero()
|
||||
{
|
||||
Assert.Equal(1, EconomyBalance.Rounded(0.2));
|
||||
Assert.Equal(1, EconomyBalance.Rounded(0));
|
||||
Assert.Equal(3, EconomyBalance.Rounded(2.5));
|
||||
Assert.Equal(4, EconomyBalance.Rounded(3.5));
|
||||
using var f = new EconomyFixture();
|
||||
f.Good("ore", workforce: 1, cycle: 1);
|
||||
f.Good("dust", inputs: new[] { ("ore", 1) }, outputCount: 10, workforce: 1, cycle: 1);
|
||||
Price(f, "commodity:ore", 1);
|
||||
Assert.Equal(0.2, EconomyBalance.Prices(f.Cache, out _)["commodity:dust"], 9);
|
||||
Assert.Equal(0, Author(f, true, out _));
|
||||
using var after = Read(f);
|
||||
Assert.Equal(1, after.Get<ItemData>(new CultRecordKey("commodity:dust")).Price);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpkeepIsPartOfAMadeGoodsPrice()
|
||||
{
|
||||
using var f = new EconomyFixture();
|
||||
f.Good("ore", workforce: 1, cycle: 1);
|
||||
f.Good("tool", workforce: 1, cycle: 1);
|
||||
f.Good("bar", inputs: new[] { ("ore", 1) }, upkeep: new[] { ("tool", 2) }, workforce: 2, cycle: 1);
|
||||
Price(f, "commodity:ore", 10);
|
||||
Price(f, "commodity:tool", 5);
|
||||
// Raw wages 10 and 5: wage 7.5. bar = 10 + 2 x 5 + 7.5 x 2.
|
||||
Assert.Equal(35, EconomyBalance.Prices(f.Cache, out _)["commodity:bar"], 9);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ public sealed class EconomyFixture : IDisposable
|
|||
// Every made commodity at the price its chain derives, as economy author leaves it.
|
||||
public void Settle()
|
||||
{
|
||||
foreach (var (key, derived) in EconomyBalance.Prices(Cache))
|
||||
foreach (var (key, derived) in EconomyBalance.Prices(Cache, out _))
|
||||
{
|
||||
var item = Cache.Get<ItemData>(new CultRecordKey(key));
|
||||
item.Price = EconomyBalance.Rounded(derived);
|
||||
|
|
|
|||
|
|
@ -270,7 +270,9 @@ public static class EconomyCommand
|
|||
// Every made commodity takes the price its chain derives. The one writer of a made price: batches may set any, this overwrites it.
|
||||
private static void Reprice(EconomyAuthor author)
|
||||
{
|
||||
foreach (var (key, derived) in EconomyBalance.Prices(author.Catalog).OrderBy(p => p.Key, StringComparer.Ordinal))
|
||||
var prices = EconomyBalance.Prices(author.Catalog, out var unsettled);
|
||||
if (unsettled.Count > 0) throw new InvalidOperationException(EconomyBalance.Unsettled(author.Catalog, unsettled));
|
||||
foreach (var (key, derived) in prices.OrderBy(p => p.Key, StringComparer.Ordinal))
|
||||
{
|
||||
var price = EconomyBalance.Rounded(derived);
|
||||
if (author.Catalog.Get<ItemData>(new CultRecordKey(key))?.Price is int held && held != price) author.SetPrice(key, price);
|
||||
|
|
|
|||
Loading…
Reference in a new issue