leg-aware capacity and wagon sharing

This commit is contained in:
Marshal
2026-07-30 19:08:06 +00:00
parent acef6870e9
commit f891f6abe2
7 changed files with 113 additions and 25 deletions

View File

@@ -565,9 +565,22 @@ export function validateMixedTrainLimitsPerEdge(
return [...violations];
}
/**
* The slot fields per-edge usage math actually reads — lets callers feed
* persisted TrainSetWagon rows (or any structural subset), not only plan slots.
*/
export type EdgeUsageSlot = Pick<
WagonPlanSlot,
'lengthMeters' | 'tareWeightTons' | 'assignedWeightTons'
> & {
boardYardId?: string | null;
alightYardId?: string | null;
allocations?: unknown[];
};
/** Per-slot stop-index spans; a yard missing from the stop list keeps the slot on the whole route. */
function slotSpans(
wagonPlan: WagonPlanSlot[],
wagonPlan: EdgeUsageSlot[],
stops: string[],
): Array<{ from: number; to: number }> {
const lastIdx = stops.length - 1;
@@ -587,26 +600,28 @@ function slotSpans(
* Two stops or fewer degrade to the whole-train totals.
*/
export function maxEdgeConsistUsage(
wagonPlan: WagonPlanSlot[],
wagonPlan: EdgeUsageSlot[],
stops: string[],
): { grossWeightTons: number; lengthMeters: number } {
const totals = (slots: WagonPlanSlot[]) => ({
): { grossWeightTons: number; lengthMeters: number; loadedWagonCount: number } {
const totals = (slots: EdgeUsageSlot[]) => ({
grossWeightTons: slots.reduce(
(sum, w) =>
sum + Number(w.tareWeightTons ?? 0) + Number(w.assignedWeightTons ?? 0),
0,
),
lengthMeters: slots.reduce((sum, w) => sum + Number(w.lengthMeters ?? 0), 0),
loadedWagonCount: slots.filter((w) => (w.allocations?.length ?? 1) > 0).length,
});
if (stops.length <= 2) return totals(wagonPlan);
const spans = slotSpans(wagonPlan, stops);
const usage = { grossWeightTons: 0, lengthMeters: 0 };
const usage = { grossWeightTons: 0, lengthMeters: 0, loadedWagonCount: 0 };
for (let edge = 0; edge < stops.length - 1; edge += 1) {
const active = totals(
wagonPlan.filter((_, i) => spans[i].from <= edge && edge < spans[i].to),
);
usage.grossWeightTons = Math.max(usage.grossWeightTons, active.grossWeightTons);
usage.lengthMeters = Math.max(usage.lengthMeters, active.lengthMeters);
usage.loadedWagonCount = Math.max(usage.loadedWagonCount, active.loadedWagonCount);
}
return usage;
}