@mark1russell7/stack-endurance 0.8.20 → 0.8.22

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.
@@ -283,23 +283,43 @@ function reachOf(parts, needs) {
283
283
  }
284
284
  // src/seasonal.ts
285
285
  /**
286
+ * This function gives the capacity of each resource whose stores have a capacity (M2-solar-charge). A store whose part is
287
+ * in `stoppedParts` has no capacity. A store with a due date has no charge: the gate refuses a capacity with a due date.
288
+ */
289
+ function storeCapacities(layer, stoppedParts) {
290
+ const out = /* @__PURE__ */ new Map();
291
+ for (const store of layer.stores) {
292
+ if (store.capacity === void 0 || store.due !== void 0) continue;
293
+ if (store.part !== void 0 && stoppedParts.has(store.part)) continue;
294
+ out.set(store.resource, (out.get(store.resource) ?? 0) + store.capacity);
295
+ }
296
+ return out;
297
+ }
298
+ /**
286
299
  * This function gives the parts of the programs of a case with a season. `parts` are the parts of the case with the
287
300
  * stock after the sowings (`seasonSupply`).
288
301
  */
289
- function seasonPlan(supply, parts, caseCuts, draws, devices) {
302
+ function seasonPlan(supply, parts, caseCuts, draws, devices, capacities = /* @__PURE__ */ new Map()) {
290
303
  const total = new Map(supply.stock);
291
304
  const harvests = supply.lots.map((lot, origin) => ({
292
305
  ...lot,
293
306
  origin
294
307
  })).filter((lot) => parts.resources.has(lot.resource));
295
308
  for (const lot of harvests) total.set(lot.resource, (total.get(lot.resource) ?? 0) + lot.amount);
296
- const all = partsOf(total, parts.direct, parts.cooks, parts.makers);
297
- const lots = [...all.stocked.filter((resource) => (supply.stock.get(resource) ?? 0) > 0).map((resource) => ({
309
+ const found = partsOf(total, parts.direct, parts.cooks, parts.makers);
310
+ const sourcesOf = (resource) => found.makers.filter((maker) => isSource(maker) && giftResource(maker) === resource);
311
+ const charged = [...capacities.keys()].filter((resource) => found.resources.has(resource) && sourcesOf(resource).length > 0).sort();
312
+ const all = {
313
+ ...found,
314
+ stocked: [.../* @__PURE__ */ new Set([...found.stocked, ...charged])].sort()
315
+ };
316
+ const storeLot = (resource) => ({
298
317
  resource,
299
318
  day: 0,
300
319
  spoils: 1e400,
301
320
  amount: exact(supply.stock.get(resource) ?? 0)
302
- })), ...harvests.map((lot) => ({
321
+ });
322
+ const lots = [...all.stocked.filter((resource) => (supply.stock.get(resource) ?? 0) > 0 || charged.includes(resource)).map(storeLot), ...harvests.map((lot) => ({
303
323
  resource: lot.resource,
304
324
  day: Math.max(0, lot.day),
305
325
  spoils: lot.spoils,
@@ -315,6 +335,15 @@ function seasonPlan(supply, parts, caseCuts, draws, devices) {
315
335
  ...months,
316
336
  ...lots.flatMap((lot) => [lot.day, lot.spoils]).filter(inside)
317
337
  ])].sort((a, b) => a - b);
338
+ const charges = charged.map((resource) => {
339
+ const lot = lots.find((item) => item.resource === resource && item.origin === void 0);
340
+ return {
341
+ resource,
342
+ capacity: exact(Math.max(capacities.get(resource) ?? 0, toNumber(lot.amount))),
343
+ lot,
344
+ sources: sourcesOf(resource)
345
+ };
346
+ });
318
347
  return {
319
348
  parts: all,
320
349
  caseCuts,
@@ -323,7 +352,8 @@ function seasonPlan(supply, parts, caseCuts, draws, devices) {
323
352
  lots,
324
353
  edges,
325
354
  shares,
326
- months: edges.slice(0, -1).map((edge) => shares.size === 0 ? 0 : monthOf(supply.start, edge))
355
+ months: edges.slice(0, -1).map((edge) => shares.size === 0 ? 0 : monthOf(supply.start, edge)),
356
+ charges
327
357
  };
328
358
  }
329
359
  const edgeOf = (plan, index) => exact(plan.edges[index] ?? 0);
@@ -368,9 +398,11 @@ function addCell(program, plan, draw, segment, fed) {
368
398
  takes
369
399
  };
370
400
  }
371
- /** This function adds the row of each lot: the takes of all the cells are not more than its amount. */
401
+ /** This function adds the row of each lot: the takes of all the cells are not more than its amount. The rows of `addCharges` cover the lot of a charge. */
372
402
  function lotRows(program, plan, cells) {
403
+ const charged = new Set(plan.charges.map((charge) => charge.lot));
373
404
  for (const lot of plan.lots) {
405
+ if (charged.has(lot)) continue;
374
406
  const row = /* @__PURE__ */ new Map();
375
407
  for (const cell of cells) {
376
408
  const column = cell.takes.get(lot);
@@ -380,6 +412,50 @@ function lotRows(program, plan, cells) {
380
412
  }
381
413
  }
382
414
  /**
415
+ * This function adds the charge of each store with a capacity in the segments before `through` (M2-solar-charge).
416
+ * Each source has one charge column in each segment before the last one: no take of the program uses the charge of the last one.
417
+ * The level at the start of a segment is the amount of the lot plus the charges of the segments before it, less their takes.
418
+ * - The takes of a segment are not more than the level at its start.
419
+ * - The level at the start of a segment plus its charge is not more than the capacity.
420
+ * Thus each order of the takes and the charges in a segment is a plan. The caller adds the charge columns to the rows of the device-days.
421
+ */
422
+ function addCharges(program, plan, cells, through) {
423
+ const pairs = /* @__PURE__ */ new Map();
424
+ const gifts = /* @__PURE__ */ new Map();
425
+ for (const charge of plan.charges) {
426
+ const level = /* @__PURE__ */ new Map();
427
+ const given = /* @__PURE__ */ new Map();
428
+ gifts.set(charge.resource, given);
429
+ for (let segment = 0; segment < through; segment += 1) {
430
+ const takes = /* @__PURE__ */ new Map();
431
+ for (const cell of cells) {
432
+ const column = cell.segment === segment ? cell.takes.get(charge.lot) : void 0;
433
+ if (column !== void 0) addTo(takes, column, ONE);
434
+ }
435
+ const most = new Map(takes);
436
+ for (const [column, value] of level) addTo(most, column, neg(value));
437
+ program.push(most, "<=", charge.lot.amount);
438
+ if (segment + 1 < through) {
439
+ const full = new Map(level);
440
+ for (const source of charge.sources) {
441
+ const column = program.next();
442
+ pairs.set(segment, [...pairs.get(segment) ?? [], [source, column]]);
443
+ const each = exact(source.gives.amount);
444
+ addTo(full, column, each);
445
+ addTo(level, column, each);
446
+ addTo(given, column, each);
447
+ }
448
+ program.push(full, "<=", sub(charge.capacity, charge.lot.amount));
449
+ }
450
+ for (const [column, value] of takes) addTo(level, column, neg(value));
451
+ }
452
+ }
453
+ return {
454
+ pairs,
455
+ gifts
456
+ };
457
+ }
458
+ /**
383
459
  * 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
384
460
  * 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.
385
461
  */
@@ -434,9 +510,10 @@ function buildStep(plan, fixed, open, segment) {
434
510
  floors.set(draw.need, program.push(/* @__PURE__ */ new Map([[fed, ONE], [t, neg(ONE)]]), ">=", neg(edgeOf(plan, segment))));
435
511
  cells.push(addCell(program, plan, draw, segment, fed));
436
512
  }
513
+ const charges = addCharges(program, plan, cells, segment + 1);
437
514
  const shared = /* @__PURE__ */ new Map();
438
515
  for (let index = 0; index <= segment; index += 1) {
439
- const { limits, stopped } = deviceUse(cells.filter((cell) => cell.segment === index && opened.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs]));
516
+ const { limits, stopped } = deviceUse([...cells.filter((cell) => cell.segment === index && opened.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs]), ...charges.pairs.get(index) ?? []]);
440
517
  for (const column of stopped) program.push(/* @__PURE__ */ new Map([[column, ONE]]), "<=", ZERO);
441
518
  const bySegment = /* @__PURE__ */ new Map();
442
519
  shared.set(index, bySegment);
@@ -462,7 +539,8 @@ function buildStep(plan, fixed, open, segment) {
462
539
  cells,
463
540
  floors,
464
541
  shared,
465
- segment
542
+ segment,
543
+ charges
466
544
  };
467
545
  }
468
546
  /**
@@ -507,7 +585,8 @@ function fewestOpen(step, open) {
507
585
  /** This function gives the smallest sum of the shares of the stocks, each over the total of its lots, as an objective. */
508
586
  function leastShares(plan, cells, count) {
509
587
  const least = /* @__PURE__ */ new Map();
510
- 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)));
588
+ const weight = (resource) => div(ONE, exact((plan.parts.stock.get(resource) ?? 0) > 0 ? plan.parts.stock.get(resource) : 1));
589
+ for (const cell of cells) for (const [resource, index] of cell.block.shares) addTo(least, index, weight(resource));
511
590
  return {
512
591
  sense: "min",
513
592
  coefficients: dense(least, count)
@@ -555,10 +634,11 @@ function takesOf(cells, need, x) {
555
634
  return out;
556
635
  }
557
636
  /** 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. */
558
- function usedUp(plan, cells, x, closure, stored) {
637
+ function usedUp(plan, cells, x, closure, stored, charges) {
559
638
  return [...closure].filter((resource) => stored.has(resource)).filter((resource) => {
560
639
  let taken = ZERO;
561
- let amount = ZERO;
640
+ const given = charges?.gifts.get(resource);
641
+ let amount = given === void 0 ? ZERO : valueAt(given, x);
562
642
  for (const lot of plan.lots) {
563
643
  if (lot.resource !== resource) continue;
564
644
  amount = add(amount, lot.amount);
@@ -622,6 +702,12 @@ function seasonBest(plan, fixed, levels, stored) {
622
702
  }
623
703
  }
624
704
  }
705
+ const charges = addCharges(program, plan, cells, segments);
706
+ for (const [segment, list] of charges.pairs) {
707
+ const { limits } = deviceUse([...[...again.keys()].filter((cell) => cell.segment === segment).flatMap((cell) => [...cell.block.runs]), ...list]);
708
+ const keys = new Set(list.map(([source]) => deviceKey(source)));
709
+ for (const [key, limit] of limits) if (keys.has(key)) program.push(limit.row, "<=", mul(roomOf(plan, kept, segment, key, limit.devices), lengthOf(plan, segment)));
710
+ }
625
711
  lotRows(program, plan, cells);
626
712
  const objectives = levels.map((level) => {
627
713
  const needs = new Set(level.map((draw) => draw.need));
@@ -675,7 +761,7 @@ function seasonBest(plan, fixed, levels, stored) {
675
761
  const { closure } = reachOf(plan.parts, /* @__PURE__ */ new Set([draw.need]));
676
762
  needs[draw.need] = {
677
763
  days: toNumber(fix.stop),
678
- binding: fix.atHorizon ? [] : usedUp(plan, cells, x, closure, stored),
764
+ binding: fix.atHorizon ? [] : usedUp(plan, cells, x, closure, stored, charges),
679
765
  takes: takesOf(cells, draw.need, x),
680
766
  ...fix.capped.length === 0 ? {} : { capped: fix.capped },
681
767
  ...fix.atHorizon ? { atHorizon: true } : {},
@@ -728,7 +814,7 @@ function seasonJoint(plan, stored) {
728
814
  days: toNumber(stop.days),
729
815
  atHorizon: stop.atHorizon,
730
816
  takes,
731
- binding: usedUp(plan, step.cells, x, reachOf(plan.parts, open).closure, stored),
817
+ binding: usedUp(plan, step.cells, x, reachOf(plan.parts, open).closure, stored, step.charges),
732
818
  runs,
733
819
  capped
734
820
  };
@@ -1521,6 +1607,9 @@ function capacity(start, ways, shareAt, at = 0) {
1521
1607
  * `perDay` or `devices`. Since 0.8.16 (M2-grow).
1522
1608
  * - `endurance-monthly`: the shares `monthly` of a converter are not 12 numbers from 0 to 1. Or the converter has
1523
1609
  * `monthly` and no `perDay`, or a season. Or two converters of one device have different shares. Since 0.8.20 (M2-solar).
1610
+ * - `endurance-capacity`: the capacity of a store is not a finite number more than 0, or it is less than the amount.
1611
+ * Or a store with a capacity has a due date. Or a resource has a store with a capacity and a store with no capacity.
1612
+ * Since 0.8.21 (M2-solar-charge).
1524
1613
  */
1525
1614
  const ENDURANCE_CODES = [
1526
1615
  "endurance-twice",
@@ -1539,7 +1628,8 @@ const ENDURANCE_CODES = [
1539
1628
  "endurance-no-way",
1540
1629
  "endurance-cut-no-curve",
1541
1630
  "endurance-season",
1542
- "endurance-monthly"
1631
+ "endurance-monthly",
1632
+ "endurance-capacity"
1543
1633
  ];
1544
1634
  /**
1545
1635
  * This function gives each fault of an endurance layer. With a base stack, it also examines the `id` of each part. A list with no error tells that the gate passes.
@@ -1597,6 +1687,15 @@ function checkEndurance(layer, stack) {
1597
1687
  range(store.id, "amount", store.amount);
1598
1688
  if (store.due !== void 0) date(store.id, store.due);
1599
1689
  part(store.id, "part", store.part);
1690
+ if (store.capacity !== void 0) {
1691
+ if (!(Number.isFinite(store.capacity) && store.capacity > 0 && store.capacity >= store.amount)) add("error", "endurance-capacity", store.id, `The capacity of "${store.id}" is ${String(store.capacity)}. It must be a finite number more than 0, and not less than the amount.`);
1692
+ if (store.due !== void 0) add("error", "endurance-capacity", store.id, `The store "${store.id}" has a capacity and a due date. A store with a charge has no due date.`);
1693
+ }
1694
+ }
1695
+ for (const code of new Set(layer.stores.map((store) => store.resource))) {
1696
+ const all = layer.stores.filter((store) => store.resource === code);
1697
+ const charged = all.filter((store) => store.capacity !== void 0);
1698
+ if (charged.length > 0 && charged.length < all.length) add("error", "endurance-capacity", code, `The resource "${code}" has a store with a capacity and a store with no capacity. Its stores all have a capacity, or none has one.`);
1600
1699
  }
1601
1700
  const levels = new Set(layer.levels);
1602
1701
  for (const draw of layer.draws) {
@@ -1803,6 +1902,6 @@ function joinEndurance(pieces) {
1803
1902
  }
1804
1903
  };
1805
1904
  }
1806
- export { drawBlock as A, fixStops as C, Program as D, seasonPlan as E, dense as F, exact as I, isSource as L, reachOf as M, valueAt as N, caseParts as O, addTo as P, firstStop as S, seasonJoint as T, seasonSupply as _, hasEnduranceError as a, storeAmounts as b, caseStopped as c, giftNeed as d, giftResource as f, monthlyShares as g, monthOf as h, curveFault as i, fewestRow as j, deviceUse as k, drawDemand as l, monthEdges 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, shareOf as v, seasonBest as w, fewestOpen as x, sowDays as y };
1905
+ export { deviceUse as A, fixStops as C, storeCapacities as D, seasonPlan as E, addTo as F, dense as I, exact as L, fewestRow as M, reachOf as N, Program as O, valueAt as P, deviceKey as R, firstStop as S, seasonJoint as T, seasonSupply as _, hasEnduranceError as a, storeAmounts as b, caseStopped as c, giftNeed as d, giftResource as f, monthlyShares as g, monthOf as h, curveFault as i, drawBlock as j, caseParts as k, drawDemand as l, monthEdges 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, shareOf as v, seasonBest as w, fewestOpen as x, sowDays as y, isSource as z };
1807
1906
 
1808
- //# sourceMappingURL=join-BB1qLU4e.js.map
1907
+ //# sourceMappingURL=join-OKGUUCVQ.js.map