@mark1russell7/stack-endurance 0.8.17 → 0.8.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { t as ENDURANCE_SCHEMA } from "./layer-a3Fr70Uh.js";
2
2
  import { CUT_BOUNDS, bandFault, curveFault, curveFault as curveFault$1, dayNumber, intervalFault, isCutBound, joinRecords, joinSetting, stopped } from "@mark1russell7/system-stack";
3
- import { ONE, ZERO, add, div, fromFloat, mul, neg, sign, toNumber } from "@mark1russell7/rational";
3
+ import { ONE, ZERO, add, div, equals, fromFloat, mul, neg, sign, sub, toNumber } from "@mark1russell7/rational";
4
4
  import { solveLexicographic } from "@mark1russell7/linear";
5
5
  import { stronglyConnectedComponents } from "@mark1russell7/collections/graph";
6
6
  import { canonicalJson } from "@mark1russell7/rigor-generators";
@@ -139,7 +139,7 @@ var Program = class {
139
139
  /** This function gives the parts of a case for the draws of `needs`. */
140
140
  function caseParts(layer, stoppedParts, stock, needs) {
141
141
  const direct = (layer.direct ?? []).filter((use) => needs.has(use.need) && use.amount > 0);
142
- const live = (layer.converters ?? []).filter((converter) => canRun(converter, stoppedParts));
142
+ const live = (layer.converters ?? []).filter((converter) => converter.season === void 0 && canRun(converter, stoppedParts));
143
143
  return partsOf(stock, direct, live.filter((converter) => needs.has(giftNeed(converter) ?? "")), live.filter((converter) => giftResource(converter) !== void 0));
144
144
  }
145
145
  /** This function gives the parts of a case from its stock, its direct uses, its cooks and its makers. */
@@ -269,6 +269,477 @@ function reachOf(parts, needs) {
269
269
  reach: /* @__PURE__ */ new Set([...cooks, ...parts.makers.filter((maker) => closure.has(giftResource(maker) ?? ""))])
270
270
  };
271
271
  }
272
+ // src/seasonal.ts
273
+ /**
274
+ * This function gives the parts of the programs of a case with a season. `parts` are the parts of the case with the
275
+ * stock after the sowings (`seasonSupply`).
276
+ */
277
+ function seasonPlan(supply, parts, caseCuts, draws, devices) {
278
+ const total = new Map(supply.stock);
279
+ const harvests = supply.lots.map((lot, origin) => ({
280
+ ...lot,
281
+ origin
282
+ })).filter((lot) => parts.resources.has(lot.resource));
283
+ for (const lot of harvests) total.set(lot.resource, (total.get(lot.resource) ?? 0) + lot.amount);
284
+ const all = partsOf(total, parts.direct, parts.cooks, parts.makers);
285
+ const lots = [...all.stocked.filter((resource) => (supply.stock.get(resource) ?? 0) > 0).map((resource) => ({
286
+ resource,
287
+ day: 0,
288
+ spoils: 1e400,
289
+ amount: exact(supply.stock.get(resource) ?? 0)
290
+ })), ...harvests.map((lot) => ({
291
+ resource: lot.resource,
292
+ day: Math.max(0, lot.day),
293
+ spoils: lot.spoils,
294
+ amount: exact(lot.amount),
295
+ origin: lot.origin
296
+ }))];
297
+ const inside = (day) => day > 0 && day < supply.horizon;
298
+ return {
299
+ parts: all,
300
+ caseCuts,
301
+ draws,
302
+ devices,
303
+ lots,
304
+ edges: [.../* @__PURE__ */ new Set([
305
+ 0,
306
+ supply.horizon,
307
+ ...lots.flatMap((lot) => [lot.day, lot.spoils]).filter(inside)
308
+ ])].sort((a, b) => a - b)
309
+ };
310
+ }
311
+ const edgeOf = (plan, index) => exact(plan.edges[index] ?? 0);
312
+ const lengthOf = (plan, index) => sub(edgeOf(plan, index + 1), edgeOf(plan, index));
313
+ const segmentsOf = (plan) => plan.edges.length - 1;
314
+ /** This function gives the fed days of a fixed draw in a segment. It is all of a segment before the stop, the part to the stop, then 0. */
315
+ function fixedFed(plan, fix, segment) {
316
+ if (segment < fix.segment) return lengthOf(plan, segment);
317
+ if (segment === fix.segment) return sub(fix.stop, edgeOf(plan, segment));
318
+ return ZERO;
319
+ }
320
+ /**
321
+ * This function adds the columns of one draw in one segment. The gifts of the need are not less than the demand times the fed days.
322
+ * The share of each stock is the sum of the takes of the lots that are ready and not spoiled in the whole segment.
323
+ */
324
+ function addCell(program, plan, draw, segment, fed) {
325
+ const block = drawBlock(program, plan.parts, draw);
326
+ const demand = exact(drawDemand(draw, plan.caseCuts));
327
+ const need = new Map(block.gifts);
328
+ if (typeof fed === "number") {
329
+ addTo(need, fed, neg(demand));
330
+ program.push(need, ">=", ZERO);
331
+ } else program.push(need, ">=", mul(demand, fed));
332
+ const from = plan.edges[segment] ?? 0;
333
+ const to = plan.edges[segment + 1] ?? 1e400;
334
+ const takes = /* @__PURE__ */ new Map();
335
+ for (const [resource, share] of block.shares) {
336
+ const row = /* @__PURE__ */ new Map([[share, ONE]]);
337
+ for (const lot of plan.lots) {
338
+ if (lot.resource !== resource || lot.day > from || lot.spoils < to) continue;
339
+ const column = program.next();
340
+ takes.set(lot, column);
341
+ addTo(row, column, neg(ONE));
342
+ }
343
+ program.push(row, "=", ZERO);
344
+ }
345
+ return {
346
+ draw,
347
+ segment,
348
+ block,
349
+ fed,
350
+ takes
351
+ };
352
+ }
353
+ /** This function adds the row of each lot: the takes of all the cells are not more than its amount. */
354
+ function lotRows(program, plan, cells) {
355
+ for (const lot of plan.lots) {
356
+ const row = /* @__PURE__ */ new Map();
357
+ for (const cell of cells) {
358
+ const column = cell.takes.get(lot);
359
+ if (column !== void 0) addTo(row, column, ONE);
360
+ }
361
+ if (row.size > 0) program.push(row, "<=", lot.amount);
362
+ }
363
+ }
364
+ /**
365
+ * This function adds the cells of the fixed draws. Each one is fed to its first stop, and its runs use the device-days of each day
366
+ * that it keeps. It gives the device-days of each day that the fixed draws keep, by the index of the segment, then the key.
367
+ */
368
+ function addFixed(program, plan, fixed, cells) {
369
+ const kept = /* @__PURE__ */ new Map();
370
+ for (const draw of plan.draws) {
371
+ const fix = fixed.get(draw.need);
372
+ if (fix === void 0) continue;
373
+ for (let segment = 0; segment <= fix.segment; segment += 1) {
374
+ const fed = fixedFed(plan, fix, segment);
375
+ const keep = fix.keep.get(segment) ?? /* @__PURE__ */ new Map();
376
+ const into = kept.get(segment) ?? /* @__PURE__ */ new Map();
377
+ kept.set(segment, into);
378
+ for (const [key, value] of keep) into.set(key, add(into.get(key) ?? ZERO, value));
379
+ if (sign(fed) <= 0) continue;
380
+ const cell = addCell(program, plan, draw, segment, fed);
381
+ cells.push(cell);
382
+ const { limits, stopped } = deviceUse(cell.block.runs);
383
+ for (const index of stopped) program.push(/* @__PURE__ */ new Map([[index, ONE]]), "<=", ZERO);
384
+ for (const [key, limit] of limits) program.push(limit.row, "<=", mul(keep.get(key) ?? ZERO, fed));
385
+ }
386
+ }
387
+ return kept;
388
+ }
389
+ /** This function gives the device-days of each day that the fixed draws leave in a segment. */
390
+ function roomOf(plan, kept, segment, key, devices) {
391
+ const left = sub(exact(plan.devices.get(key) ?? devices), kept.get(segment)?.get(key) ?? ZERO);
392
+ return sign(left) > 0 ? left : ZERO;
393
+ }
394
+ /**
395
+ * This function gives the program of one step with the stop of the open draws in segment `segment`. The open draws are fed in
396
+ * each segment before it. In it, each one is fed for `t - edge` days or more, and their runs share the device-days that
397
+ * the fixed draws leave, for `t - edge` days. This is the safe side, as in `bestEndurance`.
398
+ */
399
+ function buildStep(plan, fixed, open, segment) {
400
+ const program = new Program();
401
+ const t = program.next();
402
+ program.push(/* @__PURE__ */ new Map([[t, ONE]]), "<=", edgeOf(plan, segment + 1));
403
+ const cells = [];
404
+ const kept = addFixed(program, plan, fixed, cells);
405
+ const opened = new Set(open.map((draw) => draw.need));
406
+ const floors = /* @__PURE__ */ new Map();
407
+ for (const draw of plan.draws) {
408
+ if (!opened.has(draw.need)) continue;
409
+ for (let index = 0; index < segment; index += 1) cells.push(addCell(program, plan, draw, index, lengthOf(plan, index)));
410
+ const fed = program.next();
411
+ program.push(/* @__PURE__ */ new Map([[fed, ONE]]), "<=", lengthOf(plan, segment));
412
+ floors.set(draw.need, program.push(/* @__PURE__ */ new Map([[fed, ONE], [t, neg(ONE)]]), ">=", neg(edgeOf(plan, segment))));
413
+ cells.push(addCell(program, plan, draw, segment, fed));
414
+ }
415
+ const shared = /* @__PURE__ */ new Map();
416
+ for (let index = 0; index <= segment; index += 1) {
417
+ const { limits, stopped } = deviceUse(cells.filter((cell) => cell.segment === index && opened.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs]));
418
+ for (const column of stopped) program.push(/* @__PURE__ */ new Map([[column, ONE]]), "<=", ZERO);
419
+ const bySegment = /* @__PURE__ */ new Map();
420
+ shared.set(index, bySegment);
421
+ for (const [key, limit] of limits) {
422
+ const room = roomOf(plan, kept, index, key, limit.devices);
423
+ bySegment.set(key, {
424
+ row: limit.row,
425
+ room,
426
+ converters: limit.converters
427
+ });
428
+ const row = new Map(limit.row);
429
+ if (index < segment) program.push(row, "<=", mul(room, lengthOf(plan, index)));
430
+ else {
431
+ addTo(row, t, neg(room));
432
+ program.push(row, "<=", neg(mul(room, edgeOf(plan, segment))));
433
+ }
434
+ }
435
+ }
436
+ lotRows(program, plan, cells);
437
+ return {
438
+ program,
439
+ t,
440
+ cells,
441
+ floors,
442
+ shared,
443
+ segment
444
+ };
445
+ }
446
+ /**
447
+ * This function gives the largest common first stop of the open draws, with the fixed draws at their stops. The days to
448
+ * a first stop are not a linear function of the takes, so the function tries the segments in order. When the days reach
449
+ * the end of a segment, it tries the next segment. The days of the last step are exact.
450
+ * After the largest days, the step takes the objectives of `tail`.
451
+ */
452
+ function firstStop(plan, fixed, open, tail = () => []) {
453
+ const segments = segmentsOf(plan);
454
+ for (let segment = 0; segment < segments; segment += 1) {
455
+ const step = buildStep(plan, fixed, open, segment);
456
+ const most = {
457
+ sense: "max",
458
+ coefficients: dense(/* @__PURE__ */ new Map([[step.t, ONE]]), step.program.count)
459
+ };
460
+ const solved = solveLexicographic(step.program.constraints(), [most]);
461
+ if (solved.status !== "optimal") throw new Error("The step of a season has no optimum. The point of the stop of the last segment is a point of it.");
462
+ const days = solved.values[0] ?? ZERO;
463
+ const end = edgeOf(plan, segment + 1);
464
+ if (segment + 1 < segments && equals(days, end)) continue;
465
+ const full = tail(step).length === 0 ? solved : solveLexicographic(step.program.constraints(), [most, ...tail(step)]);
466
+ if (full.status !== "optimal") throw new Error("The step of a season has no optimum for its later objectives.");
467
+ const first = full.stages[0]?.result;
468
+ return {
469
+ step,
470
+ x: full.x,
471
+ dual: first?.status === "optimal" ? first.dual : [],
472
+ days,
473
+ atHorizon: segment + 1 === segments && equals(days, end)
474
+ };
475
+ }
476
+ throw new Error("A season plan has no segment.");
477
+ }
478
+ /** This function gives the fewest device-days of the runs of the open cells of a step, as an objective. */
479
+ function fewestOpen(step, open) {
480
+ return {
481
+ sense: "min",
482
+ coefficients: dense(fewestRow(step.cells.filter((cell) => open.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs])), step.program.count)
483
+ };
484
+ }
485
+ /** This function gives the smallest sum of the shares of the stocks, each over the total of its lots, as an objective. */
486
+ function leastShares(plan, cells, count) {
487
+ const least = /* @__PURE__ */ new Map();
488
+ for (const cell of cells) for (const [resource, index] of cell.block.shares) addTo(least, index, div(ONE, exact(plan.parts.stock.get(resource) ?? 1)));
489
+ return {
490
+ sense: "min",
491
+ coefficients: dense(least, count)
492
+ };
493
+ }
494
+ /** This function fixes the open draws of `done` at the stop: the device-days of each day that they keep, and their capped converters. */
495
+ function fixStops(plan, stop, done, fixed) {
496
+ const { step, x, days } = stop;
497
+ const fedIn = (segment) => segment < step.segment ? lengthOf(plan, segment) : sub(days, edgeOf(plan, step.segment));
498
+ const full = /* @__PURE__ */ new Set();
499
+ for (const [segment, bySegment] of step.shared) for (const [key, limit] of bySegment) if (equals(valueAt(limit.row, x), mul(limit.room, fedIn(segment)))) full.add(`${String(segment)}\u0000${key}`);
500
+ for (const draw of done) {
501
+ const keep = /* @__PURE__ */ new Map();
502
+ const used = /* @__PURE__ */ new Set();
503
+ for (const cell of step.cells) {
504
+ if (cell.draw.need !== draw.need) continue;
505
+ const fed = fedIn(cell.segment);
506
+ const bySegment = /* @__PURE__ */ new Map();
507
+ keep.set(cell.segment, bySegment);
508
+ for (const [key, limit] of deviceUse(cell.block.runs).limits) {
509
+ bySegment.set(key, sign(fed) > 0 ? div(valueAt(limit.row, x), fed) : ZERO);
510
+ if (full.has(`${String(cell.segment)}\u0000${key}`)) for (const converter of limit.converters) used.add(converter);
511
+ }
512
+ }
513
+ const { reach } = reachOf(plan.parts, /* @__PURE__ */ new Set([draw.need]));
514
+ const capped = sign(days) > 0 && !stop.atHorizon ? [...reach].filter((converter) => converter.perDay !== void 0 && (!(converter.perDay > 0) || used.has(converter))).map((converter) => converter.id).sort() : [];
515
+ fixed.set(draw.need, {
516
+ stop: days,
517
+ segment: step.segment,
518
+ keep,
519
+ atHorizon: stop.atHorizon,
520
+ capped
521
+ });
522
+ }
523
+ }
524
+ /** This function gives the sum of the take of each resource of the cells of one need, more than 0 only. */
525
+ function takesOf(cells, need, x) {
526
+ const sums = /* @__PURE__ */ new Map();
527
+ for (const cell of cells) {
528
+ if (cell.draw.need !== need) continue;
529
+ for (const [resource, index] of cell.block.shares) sums.set(resource, add(sums.get(resource) ?? ZERO, x[index] ?? ZERO));
530
+ }
531
+ const out = {};
532
+ for (const [resource, value] of [...sums].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) if (sign(value) > 0) out[resource] = toNumber(value);
533
+ return out;
534
+ }
535
+ /** This function gives the resources of `closure` with a store in the layer whose lots are used up at `x`. They are in the order of their codes. */
536
+ function usedUp(plan, cells, x, closure, stored) {
537
+ return [...closure].filter((resource) => stored.has(resource)).filter((resource) => {
538
+ let taken = ZERO;
539
+ let amount = ZERO;
540
+ for (const lot of plan.lots) {
541
+ if (lot.resource !== resource) continue;
542
+ amount = add(amount, lot.amount);
543
+ for (const cell of cells) taken = add(taken, x[cell.takes.get(lot) ?? -1] ?? ZERO);
544
+ }
545
+ return equals(taken, amount);
546
+ }).sort();
547
+ }
548
+ /** This function tells if the cells of two draws or more of `cells` can use the limited device `key`. */
549
+ function sharedKey(cells, key) {
550
+ return new Set(cells.filter((cell) => deviceUse(cell.block.runs).limits.has(key)).map((cell) => cell.draw.need)).size > 1;
551
+ }
552
+ /**
553
+ * This function gives the last program of the best endurance with a season. Each draw keeps its first stop. After it, a later
554
+ * harvest can feed the draw again (`again`). The objectives come in this order:
555
+ * - the most fed days after the stops for each level, in the order of the levels.
556
+ * - the earliest fed days.
557
+ * - the smallest sum of the shares of the stocks.
558
+ * - the fewest device-days.
559
+ *
560
+ * In a segment after the stops, the cells of two draws or more that can use one limited device take turns. Their fed days
561
+ * together are not more than the segment, and each one has the device-days of each day that the stops leave. This is the
562
+ * safe side: the cells do not use one device at the same time. Each other cell starts at the start of the segment.
563
+ */
564
+ function seasonBest(plan, fixed, levels, stored) {
565
+ const program = new Program();
566
+ const cells = [];
567
+ const kept = addFixed(program, plan, fixed, cells);
568
+ const segments = segmentsOf(plan);
569
+ const again = /* @__PURE__ */ new Map();
570
+ const taking = /* @__PURE__ */ new Set();
571
+ for (const draw of plan.draws) {
572
+ const fix = fixed.get(draw.need);
573
+ for (let segment = fix.segment + 1; segment < segments; segment += 1) {
574
+ const fed = program.next();
575
+ program.push(/* @__PURE__ */ new Map([[fed, ONE]]), "<=", lengthOf(plan, segment));
576
+ const cell = addCell(program, plan, draw, segment, fed);
577
+ cells.push(cell);
578
+ again.set(cell, fed);
579
+ }
580
+ }
581
+ for (let segment = 0; segment < segments; segment += 1) {
582
+ const later = [...again.keys()].filter((cell) => cell.segment === segment);
583
+ const { limits, stopped } = deviceUse(later.flatMap((cell) => [...cell.block.runs]));
584
+ for (const column of stopped) program.push(/* @__PURE__ */ new Map([[column, ONE]]), "<=", ZERO);
585
+ const turns = new Set(later.filter((cell) => [...deviceUse(cell.block.runs).limits.keys()].some((key) => sharedKey(later, key))));
586
+ if (turns.size > 0) {
587
+ const row = /* @__PURE__ */ new Map();
588
+ for (const cell of turns) addTo(row, again.get(cell), ONE);
589
+ program.push(row, "<=", lengthOf(plan, segment));
590
+ for (const cell of turns) taking.add(cell);
591
+ }
592
+ for (const [key, limit] of limits) {
593
+ const room = roomOf(plan, kept, segment, key, limit.devices);
594
+ for (const cell of later) {
595
+ const own = deviceUse(cell.block.runs).limits.get(key);
596
+ if (own === void 0) continue;
597
+ const row = new Map(own.row);
598
+ addTo(row, again.get(cell), neg(room));
599
+ program.push(row, "<=", ZERO);
600
+ }
601
+ }
602
+ }
603
+ lotRows(program, plan, cells);
604
+ const objectives = levels.map((level) => {
605
+ const needs = new Set(level.map((draw) => draw.need));
606
+ const row = /* @__PURE__ */ new Map();
607
+ for (const [cell, fed] of again) if (needs.has(cell.draw.need)) addTo(row, fed, ONE);
608
+ return {
609
+ sense: "max",
610
+ coefficients: dense(row, program.count)
611
+ };
612
+ });
613
+ const early = /* @__PURE__ */ new Map();
614
+ const horizon = edgeOf(plan, segments);
615
+ for (const [cell, fed] of again) addTo(early, fed, sub(horizon, edgeOf(plan, cell.segment)));
616
+ objectives.push({
617
+ sense: "max",
618
+ coefficients: dense(early, program.count)
619
+ });
620
+ objectives.push(leastShares(plan, cells, program.count));
621
+ objectives.push({
622
+ sense: "min",
623
+ coefficients: dense(fewestRow(cells.flatMap((cell) => [...cell.block.runs])), program.count)
624
+ });
625
+ const solved = solveLexicographic(program.constraints(), objectives);
626
+ if (solved.status !== "optimal") throw new Error("The last program of the best endurance with a season has no optimum. The point of the last step is a point of it.");
627
+ const x = solved.x;
628
+ const turn = /* @__PURE__ */ new Map();
629
+ const offset = /* @__PURE__ */ new Map();
630
+ for (const cell of [...taking].sort((a, b) => plan.draws.indexOf(a.draw) - plan.draws.indexOf(b.draw))) {
631
+ const start = offset.get(cell.segment) ?? ZERO;
632
+ turn.set(cell, start);
633
+ offset.set(cell.segment, add(start, x[again.get(cell)] ?? ZERO));
634
+ }
635
+ const needs = {};
636
+ for (const draw of plan.draws) {
637
+ const fix = fixed.get(draw.need);
638
+ const spans = [];
639
+ for (const [cell, fed] of again) {
640
+ if (cell.draw.need !== draw.need) continue;
641
+ const value = x[fed] ?? ZERO;
642
+ if (sign(value) <= 0) continue;
643
+ const start = add(edgeOf(plan, cell.segment), taking.has(cell) ? turn.get(cell) ?? ZERO : ZERO);
644
+ const from = toNumber(start);
645
+ const to = toNumber(add(start, value));
646
+ const last = spans[spans.length - 1];
647
+ if (last !== void 0 && last.to === from) last.to = to;
648
+ else spans.push({
649
+ from,
650
+ to
651
+ });
652
+ }
653
+ const { closure } = reachOf(plan.parts, /* @__PURE__ */ new Set([draw.need]));
654
+ needs[draw.need] = {
655
+ days: toNumber(fix.stop),
656
+ binding: fix.atHorizon ? [] : usedUp(plan, cells, x, closure, stored),
657
+ takes: takesOf(cells, draw.need, x),
658
+ ...fix.capped.length === 0 ? {} : { capped: fix.capped },
659
+ ...fix.atHorizon ? { atHorizon: true } : {},
660
+ ...spans.length === 0 ? {} : { again: spans }
661
+ };
662
+ }
663
+ const taken = /* @__PURE__ */ new Map();
664
+ for (const lot of plan.lots) {
665
+ let sum = ZERO;
666
+ for (const cell of cells) sum = add(sum, x[cell.takes.get(lot) ?? -1] ?? ZERO);
667
+ taken.set(lot, toNumber(sum));
668
+ }
669
+ const perDay = /* @__PURE__ */ new Map();
670
+ for (const cell of cells) {
671
+ const fed = typeof cell.fed === "number" ? x[cell.fed] ?? ZERO : cell.fed;
672
+ if (sign(fed) <= 0) continue;
673
+ const into = perDay.get(cell.draw.need) ?? /* @__PURE__ */ new Map();
674
+ perDay.set(cell.draw.need, into);
675
+ for (const [key, limit] of deviceUse(cell.block.runs).limits) into.set(key, Math.max(into.get(key) ?? 0, toNumber(div(valueAt(limit.row, x), fed))));
676
+ }
677
+ return {
678
+ needs,
679
+ taken,
680
+ perDay
681
+ };
682
+ }
683
+ /**
684
+ * This function gives the joint days of the draws of a plan with a season. The objectives come in this order:
685
+ * - the largest common first stop.
686
+ * - the smallest sum of the shares of the stocks.
687
+ * - the fewest device-days.
688
+ */
689
+ function seasonJoint(plan, stored) {
690
+ const open = new Set(plan.draws.map((draw) => draw.need));
691
+ const stop = firstStop(plan, /* @__PURE__ */ new Map(), plan.draws, (step) => [leastShares(plan, step.cells, step.program.count), fewestOpen(step, open)]);
692
+ const { step, x } = stop;
693
+ const takes = {};
694
+ for (const draw of plan.draws) takes[draw.need] = takesOf(step.cells, draw.need, x);
695
+ const total = /* @__PURE__ */ new Map();
696
+ for (const cell of step.cells) for (const [converter, index] of cell.block.runs) total.set(converter, add(total.get(converter) ?? ZERO, x[index] ?? ZERO));
697
+ const runs = {};
698
+ for (const [converter, value] of [...total].sort(([a], [b]) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0)) if (sign(value) > 0) runs[converter.id] = toNumber(value);
699
+ const fixed = /* @__PURE__ */ new Map();
700
+ fixStops(plan, stop, plan.draws, fixed);
701
+ const capped = [...new Set([...fixed.values()].flatMap((fix) => fix.capped))].sort();
702
+ return {
703
+ days: toNumber(stop.days),
704
+ atHorizon: stop.atHorizon,
705
+ takes,
706
+ binding: usedUp(plan, step.cells, x, reachOf(plan.parts, open).closure, stored),
707
+ runs,
708
+ capped
709
+ };
710
+ }
711
+ /**
712
+ * This function gives the endurance of one draw of `enduranceOf` that reaches two limited converters of one device in a layer
713
+ * with a season (M2-grow-share-sweep). The take order cannot give the share of the device-days.
714
+ * Thus the programs of `bestEndurance` give it for this one draw: the first stop, then the most fed days after it (`again`).
715
+ *
716
+ * @param stock - The stocks that the higher draws leave.
717
+ * @param lots - The rest of each harvest lot that the higher draws leave.
718
+ * @param room - The device-days of each day that the higher draws leave, by the key of the device.
719
+ */
720
+ function seasonDraw(layer, stoppedParts, caseCuts, draw, stock, lots, horizon, room) {
721
+ const supply = {
722
+ stock: new Map(stock),
723
+ harvests: [],
724
+ lots,
725
+ horizon
726
+ };
727
+ const plan = seasonPlan(supply, caseParts(layer, stoppedParts, supply.stock, /* @__PURE__ */ new Set([draw.need])), caseCuts, [draw], room);
728
+ const fixed = /* @__PURE__ */ new Map();
729
+ const open = /* @__PURE__ */ new Set([draw.need]);
730
+ fixStops(plan, firstStop(plan, fixed, [draw], (step) => [fewestOpen(step, open)]), [draw], fixed);
731
+ const best = seasonBest(plan, fixed, [[draw]], new Set(layer.stores.map((store) => store.resource)));
732
+ const left = new Map(stock);
733
+ const rest = lots.map((lot) => lot.amount);
734
+ for (const [lot, amount] of best.taken) if (lot.origin === void 0) left.set(lot.resource, Math.max(0, (left.get(lot.resource) ?? 0) - amount));
735
+ else rest[lot.origin] = Math.max(0, (rest[lot.origin] ?? 0) - amount);
736
+ return {
737
+ need: best.needs[draw.need],
738
+ stock: left,
739
+ lots: rest,
740
+ perDay: best.perDay.get(draw.need) ?? /* @__PURE__ */ new Map()
741
+ };
742
+ }
272
743
  // src/share.ts
273
744
  /**
274
745
  * This function gives the best share of the stocks and of the device-days for one draw.
@@ -629,6 +1100,27 @@ function sowings(layer, stoppedParts, stock, makers, start, horizon) {
629
1100
  lots
630
1101
  };
631
1102
  }
1103
+ /**
1104
+ * This function gives the supply of a case with a season (M2-grow-share). `enduranceOf`, `bestEndurance` and `jointDays`
1105
+ * use it, so they read the same sowings. It throws a `RangeError` for a horizon that is not more than 0, or a date that is not `YYYY-MM-DD`.
1106
+ */
1107
+ function seasonSupply(layer, stoppedParts, options) {
1108
+ const stock = storeAmounts(layer, options.at, stoppedParts);
1109
+ const horizon = seasonHorizon(options.horizon);
1110
+ const sown = sowings(layer, stoppedParts, stock, makersOf(layer, stoppedParts), options.start ?? options.at, horizon);
1111
+ const lots = sown.lots.filter((lot) => lot.day < horizon && lot.left > 0).sort((a, b) => a.day - b.day).map((lot) => ({
1112
+ resource: lot.resource,
1113
+ day: lot.day,
1114
+ spoils: lot.spoils,
1115
+ amount: lot.left
1116
+ }));
1117
+ return {
1118
+ stock,
1119
+ harvests: sown.harvests,
1120
+ lots,
1121
+ horizon
1122
+ };
1123
+ }
632
1124
  /** This function tells if a layer has a converter with a season that can run in a case. */
633
1125
  function hasSeason(layer, stoppedParts) {
634
1126
  return (layer.converters ?? []).some((converter) => converter.season !== void 0 && canRun(converter, stoppedParts));
@@ -659,7 +1151,8 @@ function hasSeason(layer, stoppedParts) {
659
1151
  * - With a season, each draw takes its stocks over spans from harvest to harvest. `days` is the first stop of the
660
1152
  * draw. A later harvest can feed the draw again: `again` gives these spans, and `takes` is the sum to the horizon.
661
1153
  * A lower draw gets the stocks that the higher draws leave, the earliest stocks first taken. This is the safe side.
662
- * - A draw with a shared device and a season gets a `RangeError` (M2-grow-share).
1154
+ * - A draw with a shared device and a season gets the programs of `bestEndurance` for itself alone (M2-grow-share-sweep). Its stocks,
1155
+ * its harvest lots and its device-days are those that the higher draws leave. It has no field `spoiled`.
663
1156
  *
664
1157
  * The function is pure, but for an internal count of the tries of its fits for the tests. It throws a `RangeError` for a date that is not `YYYY-MM-DD`. The gate `checkEndurance` examines the layer before.
665
1158
  *
@@ -670,26 +1163,18 @@ function hasSeason(layer, stoppedParts) {
670
1163
  */
671
1164
  function enduranceOf(layer, caseCuts, options) {
672
1165
  const stoppedParts = caseStopped(caseCuts, options);
673
- const start = storeAmounts(layer, options.at, stoppedParts);
1166
+ const seasonal = hasSeason(layer, stoppedParts);
1167
+ const supply = seasonal ? seasonSupply(layer, stoppedParts, options) : void 0;
1168
+ const start = supply?.stock ?? storeAmounts(layer, options.at, stoppedParts);
674
1169
  const makers = makersOf(layer, stoppedParts);
675
1170
  const stocked = new Set(layer.stores.map((store) => store.resource));
676
1171
  const limited = (layer.converters ?? []).filter((converter) => converter.perDay !== void 0 && canRun(converter, stoppedParts));
677
1172
  const devices = new Map(limited.map((converter) => [deviceKey(converter), converter.devices ?? 1]));
678
1173
  const used = /* @__PURE__ */ new Map();
679
1174
  let before = new Map(start);
680
- const seasonal = hasSeason(layer, stoppedParts);
681
- const horizon = seasonal ? seasonHorizon(options.horizon) : 1e400;
682
- const sown = seasonal ? sowings(layer, stoppedParts, before, makers, options.start ?? options.at, horizon) : {
683
- harvests: [],
684
- lots: []
685
- };
686
- const harvests = sown.harvests;
687
- const later = sown.lots.filter((lot) => lot.day < horizon && lot.left > 0).sort((a, b) => a.day - b.day).map((lot) => ({
688
- day: lot.day,
689
- spoils: lot.spoils,
690
- resource: lot.resource,
691
- amount: lot.left
692
- }));
1175
+ const horizon = supply?.horizon ?? 1e400;
1176
+ const harvests = supply?.harvests ?? [];
1177
+ const later = (supply?.lots ?? []).map((lot) => ({ ...lot }));
693
1178
  const needs = {};
694
1179
  for (const draw of caseDraws(layer, caseCuts, stoppedParts)) {
695
1180
  const ways = waysOf(layer, draw.need, stoppedParts, makers);
@@ -720,7 +1205,20 @@ function enduranceOf(layer, caseCuts, options) {
720
1205
  };
721
1206
  };
722
1207
  const shares = sharesDevice(reach);
723
- if (shares && seasonal) throw new RangeError(`The draw "${draw.need}" reaches two limited converters of one device, and the layer has a converter with a season. The query does not give their share (M2-grow-share).`);
1208
+ if (shares && seasonal) {
1209
+ const lots = later.map((lot) => ({
1210
+ resource: lot.resource,
1211
+ day: lot.day,
1212
+ spoils: lot.spoils,
1213
+ amount: lot.amount
1214
+ }));
1215
+ const held = seasonDraw(layer, stoppedParts, caseCuts, draw, resourcesOnly(before), lots, horizon, room);
1216
+ needs[draw.need] = held.need;
1217
+ for (const [key, each] of held.perDay) used.set(key, (used.get(key) ?? 0) + each);
1218
+ before = held.stock;
1219
+ for (const [index, amount] of held.lots.entries()) later[index].amount = amount;
1220
+ continue;
1221
+ }
724
1222
  const share = shares ? bestShare({
725
1223
  draw,
726
1224
  stock: before,
@@ -1177,6 +1675,6 @@ function joinEndurance(pieces) {
1177
1675
  }
1178
1676
  };
1179
1677
  }
1180
- export { addTo as C, valueAt as S, exact as T, caseParts as _, hasEnduranceError as a, fewestRow as b, caseStopped as c, giftNeed as d, giftResource as f, Program as g, storeAmounts as h, curveFault as i, drawDemand as l, sowDays as m, ENDURANCE_CODES as n, SEASON_HORIZON_DAYS as o, hasSeason as p, checkEndurance as r, caseDraws as s, joinEndurance as t, enduranceOf as u, deviceUse as v, dense as w, reachOf as x, drawBlock as y };
1678
+ export { addTo as A, Program as C, fewestRow as D, drawBlock as E, exact as M, reachOf as O, seasonPlan as S, deviceUse as T, fewestOpen as _, hasEnduranceError as a, seasonBest as b, caseStopped as c, giftNeed as d, giftResource as f, storeAmounts as g, sowDays as h, curveFault as i, dense as j, valueAt as k, drawDemand as l, seasonSupply as m, ENDURANCE_CODES as n, SEASON_HORIZON_DAYS as o, hasSeason as p, checkEndurance as r, caseDraws as s, joinEndurance as t, enduranceOf as u, firstStop as v, caseParts as w, seasonJoint as x, fixStops as y };
1181
1679
 
1182
- //# sourceMappingURL=join-B7THRfQS.js.map
1680
+ //# sourceMappingURL=join-Du2gsJJ6.js.map