mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 01:20:55 +00:00
fix issue and add consolidation
This commit is contained in:
@@ -5,6 +5,7 @@ import { WagonType } from '../wagon-types/entities/wagon-type.entity';
|
||||
import {
|
||||
bookingCargoTons,
|
||||
bulkItemsFitFor,
|
||||
bulkTonsPerWagon,
|
||||
bulkWagonsForAllowedTypes,
|
||||
} from './train-capacity.util';
|
||||
import {
|
||||
@@ -187,8 +188,10 @@ const addAllocation = (
|
||||
* containers/tonnage placed on wagons whose type is allowed for its container
|
||||
* or cargo type) or is deferred with the shortfall reason. Wagon purity rules:
|
||||
* a wagon carries one kind at a time — containers pack by TEU (one 40ft, or
|
||||
* two 20ft, never mixed sizes), bulk fills by weight and never shares a wagon
|
||||
* with a different cargo type.
|
||||
* two 20ft, never mixed sizes); a bulk wagon carries ONE booking's cargo only,
|
||||
* filled to the cargo type's per-wagon cap. Type choice is scarcity-aware:
|
||||
* least-shareable wagon type first, so bulk with a PW2 alternative leaves the
|
||||
* container-capable NW5s to the containers.
|
||||
*/
|
||||
export function planWagonsWithStock(params: {
|
||||
bookings: Booking[];
|
||||
@@ -221,6 +224,38 @@ export function planWagonsWithStock(params: {
|
||||
const deferred: DeferredBookingRow[] = [];
|
||||
const configIssues = new Set<string>();
|
||||
|
||||
// Scarcity rank: how many distinct demand groups (container types / bulk
|
||||
// cargo types) among THESE bookings can ride each wagon type. When a cargo
|
||||
// can choose, it takes the least-shareable type first, keeping versatile
|
||||
// types (e.g. container-capable NW5) free for the cargo that has no
|
||||
// alternative. A type nobody else wants ranks 1; unranked types rank 1 too
|
||||
// (nothing competes for them).
|
||||
const demandGroups = new Map<string, WagonType[]>();
|
||||
for (const b of bookings) {
|
||||
if (b.freightType === 'CONTAINER') {
|
||||
for (const line of b.bookingContainers ?? []) {
|
||||
const containerTypeId = line.containerTypeId ?? line.containerType?.id;
|
||||
if (!containerTypeId) continue;
|
||||
demandGroups.set(
|
||||
`C:${containerTypeId}`,
|
||||
allowed.byContainerTypeId.get(containerTypeId) ?? [],
|
||||
);
|
||||
}
|
||||
} else {
|
||||
const cargoTypeId = b.cargoTypeId ?? b.cargoType?.id;
|
||||
if (cargoTypeId) {
|
||||
demandGroups.set(`B:${cargoTypeId}`, allowed.byCargoTypeId.get(cargoTypeId) ?? []);
|
||||
}
|
||||
}
|
||||
}
|
||||
const scarcityRank = new Map<string, number>();
|
||||
for (const types of demandGroups.values()) {
|
||||
for (const wt of types) {
|
||||
scarcityRank.set(wt.id, (scarcityRank.get(wt.id) ?? 0) + 1);
|
||||
}
|
||||
}
|
||||
const rankOf = (wt: WagonType): number => scarcityRank.get(wt.id) ?? 1;
|
||||
|
||||
const legFor = (booking: Booking): BookingLeg => {
|
||||
const leg = legs?.get(booking.id);
|
||||
if (!leg || leg.from < 0 || leg.to > edgeCount || leg.from >= leg.to) {
|
||||
@@ -277,18 +312,26 @@ export function planWagonsWithStock(params: {
|
||||
kind: SlotLoadType,
|
||||
cargoTypeId: string | null,
|
||||
leg: BookingLeg,
|
||||
/** Bulk only: the booking's cargo type, for its per-wagon tonnage cap. */
|
||||
cargoType?: Booking['cargoType'],
|
||||
): OpenSlot | PlacementProblem => {
|
||||
const inStock = candidates.filter((wt) => availableFor(wt.id, leg) > 0);
|
||||
if (!inStock.length) {
|
||||
return { kind: 'stock', message: noStockMessage(candidates, leg), candidates };
|
||||
}
|
||||
// Bulk favors the largest wagon (fewest wagons for the tonnage); containers
|
||||
// Least-shareable type first (see scarcityRank) so cargo with alternatives
|
||||
// never starves cargo without one. Bulk then favors the biggest per-wagon
|
||||
// take for THIS cargo (its configured cap, not the raw rating); containers
|
||||
// favor the deepest stock so the consist drains evenly. Ties keep config order.
|
||||
const bulkTakeOf = (wt: WagonType): number =>
|
||||
bulkTonsPerWagon(cargoType, wt.id, Number(wt.capacityTons));
|
||||
const chosen = [...inStock].sort((a, b) =>
|
||||
kind === 'BULK'
|
||||
? Number(b.capacityTons) - Number(a.capacityTons) ||
|
||||
? rankOf(a) - rankOf(b) ||
|
||||
bulkTakeOf(b) - bulkTakeOf(a) ||
|
||||
availableFor(b.id, leg) - availableFor(a.id, leg)
|
||||
: availableFor(b.id, leg) - availableFor(a.id, leg),
|
||||
: rankOf(a) - rankOf(b) ||
|
||||
availableFor(b.id, leg) - availableFor(a.id, leg),
|
||||
)[0];
|
||||
const pool = poolOf(leg);
|
||||
const row = usedRow(rowKeyFor(chosen.id, pool));
|
||||
@@ -298,7 +341,10 @@ export function planWagonsWithStock(params: {
|
||||
teuPerEdge: new Array<number>(edgeCount).fill(0),
|
||||
kind,
|
||||
cargoTypeId,
|
||||
freeCapacityTons: Number(chosen.capacityTons),
|
||||
// A bulk wagon fills to the cargo type's configured per-wagon cap
|
||||
// (Perishable: 20T on PW2, 30T on NW5), never the raw 70T rating.
|
||||
freeCapacityTons:
|
||||
kind === 'BULK' ? bulkTakeOf(chosen) : Number(chosen.capacityTons),
|
||||
legKey: legKeyOf(leg),
|
||||
covered: { ...leg },
|
||||
pool,
|
||||
@@ -411,7 +457,6 @@ export function planWagonsWithStock(params: {
|
||||
message: `Cargo type "${booking.cargoType?.cargoTypeName ?? booking.cargoType?.code ?? 'unknown'}" has no wagon types configured — set them in its configuration before scheduling.`,
|
||||
};
|
||||
}
|
||||
const allowedIds = new Set(candidates.map((wt) => wt.id));
|
||||
// Break-bulk (PER_ITEM): `cargoTotalWeightVgm` is the ITEM COUNT and the
|
||||
// real tonnage lives in `bulkTotalWeightTons` — bookingCargoTons resolves
|
||||
// it either way. Items are indivisible, so a wagon takes whole items only,
|
||||
@@ -423,68 +468,41 @@ export function planWagonsWithStock(params: {
|
||||
const perItemTons = perItem ? remainingWeight / quantity : 0;
|
||||
let remainingItems = perItem ? quantity : 0;
|
||||
|
||||
/** Whole items one wagon of this slot's type can still take. */
|
||||
const itemRoomOf = (open: OpenSlot): number =>
|
||||
Math.min(
|
||||
open.freeItems ?? Number.MAX_SAFE_INTEGER,
|
||||
perItemTons > 0 ? Math.floor(open.freeCapacityTons / perItemTons) : 0,
|
||||
);
|
||||
/** Fresh wagon's whole-item budget: items-fit map floor'd by tonnage. */
|
||||
/** Fresh wagon's whole-item budget: items-fit map floor'd by (capped) tonnage. */
|
||||
const itemBudgetOf = (open: OpenSlot): number => {
|
||||
const fit = bulkItemsFitFor(booking.cargoType, open.slot.wagonTypeId);
|
||||
const byTonnage =
|
||||
perItemTons > 0
|
||||
? Math.max(1, Math.floor(Number(open.slot.capacityTons) / perItemTons))
|
||||
? Math.max(1, Math.floor(open.freeCapacityTons / perItemTons))
|
||||
: 1;
|
||||
return Math.min(fit ?? Number.MAX_SAFE_INTEGER, byTonnage);
|
||||
};
|
||||
let placedAnywhere = false;
|
||||
|
||||
// Per-item: prefer the type carrying the most whole items per wagon.
|
||||
// openSlot's own capacity sort is stable, so this order breaks its ties.
|
||||
// Per-item: least-shareable type first (same scarcity rule as openSlot),
|
||||
// then the type carrying the most whole items per wagon.
|
||||
const itemBudgetOfType = (wt: WagonType): number =>
|
||||
Math.min(
|
||||
bulkItemsFitFor(booking.cargoType, wt.id) ?? Number.MAX_SAFE_INTEGER,
|
||||
perItemTons > 0
|
||||
? Math.max(1, Math.floor(Number(wt.capacityTons) / perItemTons))
|
||||
? Math.max(
|
||||
1,
|
||||
Math.floor(
|
||||
bulkTonsPerWagon(booking.cargoType, wt.id, Number(wt.capacityTons)) /
|
||||
perItemTons,
|
||||
),
|
||||
)
|
||||
: 1,
|
||||
);
|
||||
const orderedCandidates = perItem
|
||||
? [...candidates].sort((a, b) => itemBudgetOfType(b) - itemBudgetOfType(a))
|
||||
? [...candidates].sort(
|
||||
(a, b) => rankOf(a) - rankOf(b) || itemBudgetOfType(b) - itemBudgetOfType(a),
|
||||
)
|
||||
: candidates;
|
||||
|
||||
// Top off wagons already carrying THIS cargo type before opening new ones.
|
||||
// ponytail: per-item cargo only shares wagons that were opened per-item
|
||||
// (freeItems tracked); mixing itemized and loose loads of one cargo type
|
||||
// on one wagon is not modeled — open a new wagon instead.
|
||||
for (const open of openSlots) {
|
||||
if (perItem ? remainingItems <= 0 : remainingWeight <= 0) break;
|
||||
if (open.kind !== 'BULK') continue;
|
||||
if (open.legKey !== legKey) continue;
|
||||
if (open.cargoTypeId !== cargoTypeId) continue;
|
||||
if (!allowedIds.has(open.slot.wagonTypeId)) continue;
|
||||
if (open.freeCapacityTons <= 0) continue;
|
||||
if (perItem !== (open.freeItems !== undefined)) continue;
|
||||
const takeItems = perItem ? Math.min(itemRoomOf(open), remainingItems) : 0;
|
||||
if (perItem && takeItems <= 0) continue;
|
||||
const take = perItem
|
||||
? roundTons(takeItems * perItemTons)
|
||||
: roundTons(Math.min(open.freeCapacityTons, remainingWeight));
|
||||
addAllocation(
|
||||
open.slot,
|
||||
booking.id,
|
||||
booking.reference,
|
||||
take,
|
||||
AllocationLoadType.Bulk,
|
||||
);
|
||||
open.freeCapacityTons = roundTons(open.freeCapacityTons - take);
|
||||
if (perItem) {
|
||||
open.freeItems = (open.freeItems ?? 0) - takeItems;
|
||||
remainingItems -= takeItems;
|
||||
}
|
||||
remainingWeight = roundTons(remainingWeight - take);
|
||||
placedAnywhere = true;
|
||||
}
|
||||
// One bulk booking per wagon: a wagon carrying bulk takes that one
|
||||
// booking's cargo only — never topped up from another booking, even of
|
||||
// the same cargo type. Every bulk booking therefore opens its own wagons.
|
||||
|
||||
while ((perItem ? remainingItems > 0 : remainingWeight > 0) || !placedAnywhere) {
|
||||
// Per-item: openSlot's stock-depth tie-break would override the fit
|
||||
@@ -498,6 +516,7 @@ export function planWagonsWithStock(params: {
|
||||
'BULK',
|
||||
cargoTypeId,
|
||||
leg,
|
||||
booking.cargoType,
|
||||
);
|
||||
if ('message' in openedSlot) return openedSlot;
|
||||
let take: number;
|
||||
|
||||
Reference in New Issue
Block a user