Implement civilization flow mechanics and profiling for resource allocation

- Added `civilization_flow.gd` to manage continuous expected flows and resource allocation within civilizations.
- Introduced `allocate_profiled` and `whole_quotas` functions in `quantity_math.gd` for precise stock allocation based on float shares.
- Enhanced `credit` logic in `object_ledger.gd` to handle production remainders more effectively.
- Updated `append_batch` in `object_batches.gd` to prevent batch creation for invalid IDs.
- Introduced `ratio_approx` function in `exact.gd` for better handling of float profiles.
- Added a benchmark script `benchmark_civilization_flows.gd` to evaluate the performance of civilization flow mechanics.
- Refactored various functions to improve clarity and efficiency in handling resource flows and allocations.
This commit is contained in:
Kirill
2026-10-07 22:51:55 +03:00
parent 6ee66a627d
commit 3fcc7211ce
41 changed files with 247959 additions and 817 deletions
+98
View File
@@ -109,5 +109,103 @@ func run() -> void:
world.batches.withdraw_batches(world.state, "N", 1)
check(world.state.batches.N.is_empty(), "Scaling invalidates readiness and removes zero cohorts")
dispose(world)
for selected_speed in [4, 5, 6]:
world = fixture()
world.speed = selected_speed
world.state.time = {"datetime": 37, "year": 37}
var cycle_width: Variant = E.multiply(E.power(10, selected_speed - 1), E.of("0.2"))
world.catalog.entities.HG.lifespan = E.multiply(cycle_width, 3)
world.state.batches = {}
var life: Variant = world.batches.effective_lifespan(world.state, "HG")
var deadline: Variant = E.add(37, E.multiply(cycle_width, 4))
world.batches.append_batch(world.state, "HG", huge, E.add(37, 1), E.add(37, 1))
world.batches.append_batch(world.state, "HG", 7, E.add(37, 2), E.add(37, 2))
check(world.state.batches.HG.size() == 1 and E.eq(world.state.batches.HG[0].amount, E.add(huge, 7)), "Births in the same death cycle merge exactly")
var point: Dictionary = world.state.batches.HG[0]
check(E.eq(point.start_year, deadline) and E.eq(point.end_year, deadline), "High-speed stellar cohort transitions at one cycle boundary")
var born: Variant = E.add(37, 1)
var delay: Variant = E.subtract(deadline, E.add(born, life))
check(E.ge(delay, 0) and E.le(delay, cycle_width), "Transition is delayed by at most one cycle")
var encoded: Dictionary = E.decode(E.encode(world.state))
check(E.eq(encoded.batches, world.state.batches), "Cycle cohorts survive exact save roundtrip")
check(world.batches.withdraw_batches(world.state, "HG", E.subtract(deadline, 1)).is_empty(), "Cycle cohort is not withdrawn partially")
var expired: Array = world.batches.withdraw_batches(world.state, "HG", deadline)
check(expired.size() == 1 and E.eq(expired[0].amount, E.add(huge, 7)) and world.state.batches.HG.is_empty(), "Boundary withdraws the exact whole cohort once")
check(world.batches.withdraw_batches(world.state, "HG", deadline).is_empty(), "Repeated boundary cannot withdraw twice")
world.batches.append_batch(world.state, "HG", 3, E.add(37, 1), E.add(37, 1))
check(E.eq(world.state.batches.HG[0].amount, 3), "Withdrawal invalidates cycle insertion references")
world.state.batches.HG = []
world.batches.append_batch(world.state, "HG", 11, 37, E.add(37, E.multiply(cycle_width, 3)))
var total: Variant = 0
for batch: Dictionary in world.state.batches.HG:
total = E.add(total, batch.amount)
check(E.eq(batch.start_year, batch.end_year), "Distributed births create point cohorts")
check(E.eq(total, 11) and world.state.batches.HG.size() == 3, "Distributed births retain all integers across death cycles")
var before_coarsen: Dictionary = world.state.duplicate(true)
world.batches.coarsen(world.state)
check(E.eq(before_coarsen, world.state), "Coarsening cannot smear point stellar transitions")
world.state.batches.HG = []
var completed: Dictionary = {}
var cutoff: Variant = E.add(37, E.multiply(cycle_width, 10))
var integral: Variant = world.batches.append_batch(world.state, "HG", 11, 37, E.add(37, E.multiply(cycle_width, 3)), completed, cutoff)
var reference_integral: Variant = 0
total = 0
for part: Dictionary in completed.HG:
total = E.add(total, part.amount)
reference_integral = E.add(reference_integral, E.multiply(part.amount, E.subtract(cutoff, part.to)))
reference_integral = E.subtract(E.multiply(11, E.subtract(cutoff, E.add(37, E.multiply(cycle_width, E.of("1.5"))))), reference_integral)
check(E.eq(total, 11) and world.state.batches.HG.is_empty(), "Already completed stars retain counts without stored batches")
check(E.eq(integral, reference_integral), "Lifetime production agrees with point transitions")
world.speed = 1
world.state.batches.HG = []
world.batches.append_batch(world.state, "HG", 10, 100, 100)
check(E.gt(world.state.batches.HG[0].end_year, world.state.batches.HG[0].start_year), "Low speed retains gradual stellar transitions")
dispose(world)
world = fixture()
world.speed = 6
world.state.amount.RHG = 5
world.batches.append_batch(world.state, "RHG", 5, 0, 0)
world.ledger.refresh_groups(world.state)
var validation: String = root.get_node("SaveManager").validate_data({"format_version": 6, "app_version": "", "ui": {"current_page": 0}, "state": world.state})
check(validation.is_empty(), "Cycle-star state born at zero validates: " + validation)
var lifespan: Variant = world.batches.effective_lifespan(world.state, "RHG")
check(E.eq(world.state.batches.RHG[0].start_year, lifespan), "Birth exactly aligned with a death boundary adds no extra cycle")
dispose(world)
world = fixture()
world.speed = 6
world.state.batches.RD = []
world.batches.append_batch(world.state, "RD", huge, 0, 2000000)
check(world.state.batches.RD.size() == 1, "Long-lived stars are not subdivided into cycle buckets")
var long_cohort: Dictionary = world.state.batches.RD[0].duplicate(true)
world.batches.append_batch(world.state, "RD", 7, 10000, 20000)
check(world.state.batches.RD.size() == 1 and E.eq(world.state.batches.RD[0].amount, E.add(huge, 7)), "New births still merge into their original wide stellar group")
check(E.eq(world.state.batches.RD[0].start_year, long_cohort.start_year) and E.eq(world.state.batches.RD[0].end_year, long_cohort.end_year), "Long-lived transition interval is preserved")
world.state.batches.RD = [
{"amount": huge, "start_year": 1000, "end_year": 2000, "withdraw_remainder": 0.25},
{"amount": 7, "start_year": 3000, "end_year": 4000, "withdraw_remainder": 0.5},
{"amount": 9, "start_year": 19000, "end_year": 21000},
{"amount": 11, "start_year": 22000, "end_year": 1000000},
{"amount": 13, "start_year": 21000, "end_year": 22000},
]
var spanning: Dictionary = world.state.batches.RD[2].duplicate(true)
var wide: Dictionary = world.state.batches.RD[3].duplicate(true)
world.batches.coarsen(world.state)
check(world.state.batches.RD.size() == 4, "Only cohorts ending entirely within the same cycle are merged")
var merged: Dictionary = {}
var broad_kept: bool = false
var crossing_kept: bool = false
for batch: Dictionary in world.state.batches.RD:
if E.eq(batch.start_year, 20000) and E.eq(batch.end_year, 20000): merged = batch
if E.eq(batch, wide): broad_kept = true
if E.eq(batch, spanning): crossing_kept = true
check(not merged.is_empty() and E.eq(merged.amount, E.add(huge, 7)) and E.eq(merged.withdraw_remainder, 0.75), "Merged stellar cohort retains exact population and carries")
check(broad_kept and crossing_kept, "Wide and cycle-crossing cohorts remain unchanged")
var once: Dictionary = world.state.duplicate(true)
world.batches.coarsen(world.state)
check(E.eq(world.state, once), "Dynamic stellar merging is idempotent")
world.state.batches.HG = [{"amount": 3, "start_year": 20000, "end_year": 20000}, {"amount": 4, "start_year": 20001, "end_year": 21000}]
world.batches.coarsen(world.state)
check(world.state.batches.HG.size() == 2, "An exact boundary belongs to the preceding cycle")
dispose(world)
print(("Batch boundaries: %d checks, %d failures" % [checks, failures]))
quit((0 if E.eq(failures, 0) else 1))