@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.
package/README.md CHANGED
@@ -59,13 +59,22 @@ A converter can have a `season`. It has a window of sow days in each year (`from
59
59
 
60
60
  A converter with a limit `perDay` more than 0 can take nothing. It is a source, for example a solar panel. A converter can also have `monthly`: 12 shares from 0 to 1, January first. In a day of month `m`, the converter has `devices` times `monthly[m]` device-days. Thus a panel with `perDay` 1500 and a share of 0.2 in January gives 300 runs in a day of January. Since 0.8.20 (M2-solar).
61
61
 
62
- - The energy of a day that no draw takes is lost. A store does not take it. Thus a battery is a store with no charge from the panel. This is the safe side.
62
+ - The energy of a day that no draw takes is lost, unless a store of its resource has a `capacity`. Then the store takes a charge (the next section).
63
63
  - A take uses a source first, before the stores and the other converters. The runs of a source in a day are lost when no draw uses them.
64
64
  - A layer with a source or with shares goes in segments to the horizon, as a layer with a season. A segment also ends at the start of each month. A source can feed a draw with no end, thus the horizon is the upper limit of its days.
65
65
  - After a stop, a draw can hold again from the start of a month with more device-days. `again` gives these spans.
66
66
  - A higher draw keeps the largest part of the device-days that it uses in a month, for each month of a lower draw. This is the safe side.
67
67
  - The converters of one device have the same shares. A converter with shares has a limit `perDay` and no season. The gate refuses the other cases (`endurance-monthly`).
68
68
 
69
+ ### A battery: a store with a capacity
70
+
71
+ A store can have a `capacity`: the most amount that it holds. A source that gives the resource of the store charges it with the runs that no draw takes on their day. Example: a battery of 6000 Wh takes the energy of a sunny day, and gives it back on a dark day. Since 0.8.21 (M2-solar-charge).
72
+
73
+ - `bestEndurance` and `jointDays` give the charge in each segment of the days. In a segment, the takes are not more than the level at the start of the segment. Also the level at the start plus the charge is not more than the capacity. Thus each order of the takes and the charges in the segment is a plan. This is the safe side.
74
+ - The sweep of `enduranceOf` takes one draw at a time, and it gives no charge. There a store with a capacity is a store with no charge. The follow-up M2-solar-charge-sweep adds the charge to the sweep.
75
+ - Only a source charges a store. A maker with inputs gives no charge, because its inputs keep to the day of the take.
76
+ - The stores of one resource all have a capacity, or none has one. A store with a capacity has no due date, and its capacity is not less than its amount. The gate refuses the other cases (`endurance-capacity`).
77
+
69
78
  ## The rule of shortfall
70
79
 
71
80
  - For one draw in one case, the shortfall is `E[max(0, D - T)] * users`. `T` is the endurance of the draw. `D` is the duration of the stop of its supply.
@@ -117,6 +126,31 @@ Two groups of cuts that share no minimal set are independent, so the independent
117
126
  - `holds` names the draws whose supply the case does not stop. `unread` names the draws whose stop has no curve.
118
127
  - `band` gives the same values with each cut at the low end of its band (`shorter`) and at the high end (`longer`). The days do not change. Each end holds only at each day, so the two values are a range of the sampling error, not a joint interval. A draw has no `band` when a cut of its stop has a curve and no band. Since 0.8.14.
119
128
 
129
+ ## The sensitivity of a layer
130
+
131
+ `enduranceSensitivity(layer, caseCuts, options)` tells which input moves the days most. For each input, it gives the change in the days of each draw for one step of the input. Then it ranks the inputs. Since 0.8.22 (M2-endurance-sensitivity).
132
+
133
+ - The inputs are the amount of each store and the rate `perDay` of each draw. Each converter gives its gift, each take, its limit `perDay` and its count of devices.
134
+ - The key of an input tells what it is, for example `store:jugs`, `draw:hydration.drink`, `converter:cook-rice.takes.water.stored` or `device:stove.devices`.
135
+ - The step adds to a store, a gift, a limit and a count of devices. It takes off a draw and a take, because less of them gives more days. Thus a lower draw is an input that costs nothing.
136
+ - The option `step` sets the step, in the unit of each input. The default is `SENSITIVITY_STEP`, 1 unit. A count of devices always takes 1 more device. When a draw or a take is not more than the step, the step takes off half of it.
137
+ - The change of a draw is a finite difference of `enduranceOf`: the days after the step minus the days before it. `perUnit` gives the change for each unit of the input.
138
+ - An input that no draw of the case uses has a change of 0.
139
+ - The rank is by level, as the take order. It compares the sum of the changes of the first level, then of the next level, and so on. The largest sum comes first. Two equal inputs go in the order of their keys.
140
+ - `chance` gives the chance that the draw gets its need through the event (`lasts` of `caseOutlook`), before and after the step. It also gives the values at the two ends of the sampling bands of the cuts, before and after the step. The change of the chance is the change of the point values, with no interval.
141
+
142
+ An example on the sample:
143
+
144
+ ```ts
145
+ import { enduranceSensitivity, SAMPLE_ENDURANCE } from "@mark1russell7/stack-endurance";
146
+
147
+ const storm = SAMPLE_ENDURANCE.cases?.find((item) => item.case === "storm");
148
+ if (storm !== undefined) {
149
+ const rank = enduranceSensitivity(SAMPLE_ENDURANCE, storm, { at: "2026-12-01" });
150
+ // rank.inputs[0].id is "draw:hydration.drink": 1 L less of drink each day gives 0.644 more days of water.
151
+ }
152
+ ```
153
+
120
154
  ## The joint days
121
155
 
122
156
  `jointDays(layer, caseCuts, options, needs)` gives the joint days of a set of draws. These are the largest count of days `d` at which each draw of the set holds `d` days together.
@@ -202,7 +236,7 @@ With no horizon, the drink takes all the water for 10 days, and the food gets 0
202
236
 
203
237
  | Entry | What it has |
204
238
  | --- | --- |
205
- | `@mark1russell7/stack-endurance` | The records of a layer, the endurance, joint days, best endurance and shortfall queries, the gate, the join and a sample. |
239
+ | `@mark1russell7/stack-endurance` | The records of a layer, the endurance, joint days, best endurance, shortfall and sensitivity queries, the gate, the join and a sample. |
206
240
  | `@mark1russell7/stack-endurance/schema` | `enduranceSchema`, the zod schema of a layer, with `parseEndurance`. |
207
241
  | `@mark1russell7/stack-endurance/add-on` | `enduranceAddOn`, the add-on in the form of the layer contract of the core. |
208
242
 
package/build/add-on.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { u as EnduranceLayer } from "./layer-CsVKqL3L.js";
1
+ import { u as EnduranceLayer } from "./layer-DuGjz7Cl.js";
2
2
  import { StackJoiner } from "@mark1russell7/system-stack";
3
3
  /**
4
4
  * This function gives the endurance add-on.
package/build/add-on.js CHANGED
@@ -1,4 +1,4 @@
1
- import { r as checkEndurance, t as joinEndurance } from "./join-BB1qLU4e.js";
1
+ import { r as checkEndurance, t as joinEndurance } from "./join-OKGUUCVQ.js";
2
2
  import { t as ENDURANCE_SCHEMA } from "./layer-a3Fr70Uh.js";
3
3
  import { parseEndurance } from "./schema.js";
4
4
  // src/add-on.ts
package/build/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { _ as Season, a as DirectUse, c as ENDURANCE_SCHEMA, d as Gift, f as Harvest, g as Resource, h as Prior, i as CutBound, l as Endurance, m as MonthDay, n as Converter, o as Draw, p as IsoDate, r as Cut, s as DrawEndurance, t as CaseCuts, u as EnduranceLayer, v as Store, y as Survival } from "./layer-CsVKqL3L.js";
1
+ import { _ as Season, a as DirectUse, c as ENDURANCE_SCHEMA, d as Gift, f as Harvest, g as Resource, h as Prior, i as CutBound, l as Endurance, m as MonthDay, n as Converter, o as Draw, p as IsoDate, r as Cut, s as DrawEndurance, t as CaseCuts, u as EnduranceLayer, v as Store, y as Survival } from "./layer-DuGjz7Cl.js";
2
2
  import { CutBound as CutBound$1, Fault, LayerParse, LayerPiece, PathOptions, Stack, curveFault, survivalAt } from "@mark1russell7/system-stack";
3
3
  /**
4
4
  * This function tells if a converter is a source (M2-solar): it takes nothing, and it has a limit `perDay` more than 0.
@@ -657,8 +657,11 @@ export declare function caseOutlook(layer: EnduranceLayer, caseCuts: CaseCuts, o
657
657
  * `perDay` or `devices`. Since 0.8.16 (M2-grow).
658
658
  * - `endurance-monthly`: the shares `monthly` of a converter are not 12 numbers from 0 to 1. Or the converter has
659
659
  * `monthly` and no `perDay`, or a season. Or two converters of one device have different shares. Since 0.8.20 (M2-solar).
660
+ * - `endurance-capacity`: the capacity of a store is not a finite number more than 0, or it is less than the amount.
661
+ * 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.
662
+ * Since 0.8.21 (M2-solar-charge).
660
663
  */
661
- export declare const ENDURANCE_CODES: readonly ["endurance-twice", "endurance-level-unknown", "endurance-resource-unknown", "endurance-part-unknown", "endurance-range", "endurance-date", "endurance-converter-empty", "endurance-curve", "endurance-bound", "endurance-prior-case", "endurance-cycle", "endurance-devices", "endurance-device-mix", "endurance-no-way", "endurance-cut-no-curve", "endurance-season", "endurance-monthly"];
664
+ export declare const ENDURANCE_CODES: readonly ["endurance-twice", "endurance-level-unknown", "endurance-resource-unknown", "endurance-part-unknown", "endurance-range", "endurance-date", "endurance-converter-empty", "endurance-curve", "endurance-bound", "endurance-prior-case", "endurance-cycle", "endurance-devices", "endurance-device-mix", "endurance-no-way", "endurance-cut-no-curve", "endurance-season", "endurance-monthly", "endurance-capacity"];
662
665
  /** A code of `ENDURANCE_CODES`. */
663
666
  export type EnduranceCode = (typeof ENDURANCE_CODES)[number];
664
667
  /** A fault of the gate of an endurance layer. */
@@ -705,5 +708,105 @@ export declare const SAMPLE_ENDURANCE_STACK: Stack;
705
708
  * - The filled bottles are past their due date, so they give nothing.
706
709
  */
707
710
  export declare const SAMPLE_ENDURANCE: EnduranceLayer;
711
+ /** The default step of `enduranceSensitivity`: 1 unit of each input. */
712
+ export declare const SENSITIVITY_STEP = 1;
713
+ /** The options of `enduranceSensitivity`: the options of a case, the horizon of the chance, and the step. */
714
+ export interface SensitivityOptions extends CaseOptions, ShortfallOptions {
715
+ /** The step of each input, a number more than 0, in the unit of the input. The default is `SENSITIVITY_STEP`. */
716
+ readonly step?: number | undefined;
717
+ }
718
+ /** The kind of an input: the amount of a store, the rate of a draw, or a parameter of a converter. */
719
+ export type SensitivityKind = "store" | "draw" | "converter";
720
+ /**
721
+ * The change in the chance that a draw gets its need through the event (`DrawOutlook.lasts`), for one input.
722
+ * The chance comes from the curves of the stop. Only the days change, so the curves are the same before and after.
723
+ */
724
+ export interface SensitivityChance {
725
+ /** The chance before the step, for each curve of the stop. */
726
+ readonly before: CurveValues;
727
+ /** The chance after the step, for each curve of the stop. */
728
+ readonly after: CurveValues;
729
+ /** The chance after the step minus the chance before it, for each curve. It is the change of the point values, not an interval. */
730
+ readonly change: CurveValues;
731
+ /** The values before the step at the two ends of the sampling bands of the cuts (`DrawOutlook.band`). */
732
+ readonly beforeBand?: DrawBand | undefined;
733
+ /** The values after the step at the two ends of the sampling bands of the cuts. */
734
+ readonly afterBand?: DrawBand | undefined;
735
+ }
736
+ /** The change in the days of one draw for one input. */
737
+ export interface SensitivityNeed {
738
+ readonly need: string;
739
+ readonly level: string;
740
+ /** The days of the draw before the step (`DrawEndurance.days`). */
741
+ readonly before: number;
742
+ /** The days of the draw after the step. */
743
+ readonly after: number;
744
+ /** The days after the step minus the days before it. */
745
+ readonly change: number;
746
+ /** The change for each unit of the input: `change` divided by the size of `delta`. */
747
+ readonly perUnit: number;
748
+ /** The change in the chance. It is missing when the stop of the draw has no curve (`CaseOutlook.unread`). */
749
+ readonly chance?: SensitivityChance | undefined;
750
+ }
751
+ /** One input of a layer, and the change in the days of each draw for its step. */
752
+ export interface SensitivityInput {
753
+ /**
754
+ * The key of the input: `store:<id>`, `draw:<need>`, `converter:<id>.gives`, `converter:<id>.takes.<resource>` or
755
+ * `converter:<id>.perDay`. The count of devices has the key of the device and `.devices`, for example `device:stove.devices`.
756
+ */
757
+ readonly id: string;
758
+ readonly kind: SensitivityKind;
759
+ /** The `id` of the store, the need of the draw, the `id` of the converter, or the device of a count of devices. */
760
+ readonly target: string;
761
+ /** The field that the step changes: `amount`, `perDay`, `gives`, `takes.<resource>` or `devices`. */
762
+ readonly field: string;
763
+ /** The value of the field before the step. */
764
+ readonly value: number;
765
+ /** The change of the value: more than 0 for more of the input, less than 0 for a lower draw or a lower take. */
766
+ readonly delta: number;
767
+ /** The change of each draw whose supply the case stops, in the order of `enduranceOf`. */
768
+ readonly needs: readonly SensitivityNeed[];
769
+ /** The sum of the changes of the draws of each level, by the level. The rank compares these sums. */
770
+ readonly levels: Readonly<Record<string, number>>;
771
+ }
772
+ /** The sensitivity of a layer in a case: each input with its change, the largest change first. */
773
+ export interface Sensitivity {
774
+ readonly case: string;
775
+ readonly at: IsoDate;
776
+ /** The step of the query. */
777
+ readonly step: number;
778
+ /** The endurance before each step. */
779
+ readonly base: Endurance;
780
+ /** The rank of the inputs. Refer to `enduranceSensitivity`. */
781
+ readonly inputs: readonly SensitivityInput[];
782
+ }
783
+ /**
784
+ * This function gives the change in the days of each draw for one step of each input of a layer, ranked.
785
+ * The rank names the input that moves the days most. A lower draw costs nothing, so the rank also shows such inputs.
786
+ *
787
+ * - The inputs are the amount of each store and the rate `perDay` of each draw. Each converter gives the amount of its
788
+ * gift, the amount of each take, its limit `perDay` and its count of devices.
789
+ * - The step adds to a store, to a gift, to a limit `perDay` and to a count of devices. It takes off a draw and a take,
790
+ * because less of them gives more days. A count of devices takes 1 more device.
791
+ * - When the rate of a draw or a take is not more than the step, the step takes off half the value. Thus the value stays more than 0.
792
+ * - A store with a capacity gets a capacity that is not less than its new amount.
793
+ * - The change of a draw is a finite difference: the days of `enduranceOf` after the step minus the days before it.
794
+ * `perUnit` divides it by the size of the change of the value.
795
+ * - An input that no draw of the case uses has a change of 0, for example a store past its due date.
796
+ * - The rank is by level. It compares the sum of the changes of the draws of the first level, then of the next level, and so on.
797
+ * The largest sum comes first. Two equal inputs go in the order of their keys. Thus the order is stable.
798
+ * - The days are exact for the inputs. `chance` is the chance that a draw gets its need through the event. It comes
799
+ * from `caseOutlook` with the share `take`, before and after the step, with the bands of the cuts. The change of the
800
+ * chance is the change of the point values. It has no interval.
801
+ *
802
+ * The cost is one `enduranceOf` and one `caseOutlook` for each input. The function is pure.
803
+ *
804
+ * @param layer - The endurance layer
805
+ * @param caseCuts - The case
806
+ * @param options - The date, the stack or the parts that are also down, the horizon and the step
807
+ * @returns The endurance before the steps, and the inputs in the order of the rank
808
+ * @throws RangeError for a step that is not a finite number more than 0, and for the errors of `enduranceOf` and `caseOutlook`
809
+ */
810
+ export declare function enduranceSensitivity(layer: EnduranceLayer, caseCuts: CaseCuts, options: SensitivityOptions): Sensitivity;
708
811
  export { type CaseCuts, Converter, type Cut, type CutBound, DirectUse, Draw, DrawEndurance, ENDURANCE_SCHEMA, Endurance, EnduranceLayer, Gift, Harvest, IsoDate, MonthDay, type Prior, Resource, Season, Store, type Survival, curveFault, survivalAt };
709
812
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":["CutBound"],"sources":["../src/converter.ts","../src/endurance.ts","../src/joint.ts","../src/best.ts","../src/shortfall.ts","../src/check.ts","../src/join.ts","../src/sample.ts"],"mappings":";;;;;;wBAegB,SAAS,WAAW;;iBCJnB;;WAEN,IAAI;;WAEJ,QAAQ;;WAER;;;;;;WAMA,QAAQ;;;;;;WAMR;;;qBAIE;;;;;wBAMG,YAAY,UAAU,UAAU,SAAS,KAAK,iCAAiC;;wBAiB/E,QAAQ,OAAO,SAAS;;;;;wBASxB,WAAW,OAAO,SAAS;;wBAc3B,cAAc,YAAY,SAAS,aAAa;;wBAOhD,QAAQ,QAAQ,wCAAwC,aAAa;;;;;;;wBAarE,aAAa,OAAO,gBAAgB,IAAI,SAAS,eAAc,sBAAkC;;wBAYjG,WAAW,MAAM,MAAM,UAAU;;;;;wBAQjC,UAAU,OAAO,gBAAgB,UAAU,UAAU,cAAc,sBAAsB;;wBAWzF,SAAS,WAAW;;wBAKpB,aAAa,WAAW;;;;;;;;wBAmMxB,QAAQ,QAAQ,KAAK,wBAAwB,OAAO,SAAS;;iBAkG5D;WACN;WACA;WACA;WACA;;;iBAIM;;WAEN,OAAO;WACP,UAAU;;WAEV,eAAe;;WAEf;;WAEA,OAAO;;;;;;wBAOF,aAAa,OAAO,gBAAgB,cAAc,qBAAqB,SAAS,cAAc;;;;;;wBAkB9F,UAAU,OAAO,gBAAgB,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA2C/C,YAAY,OAAO,gBAAgB,UAAU,UAAU,SAAS,cAAc;;iBCrf7E;WACN;WACA,IAAI;;;;;WAKJ;;WAEA;;WAEA;;;;;;WAMA,OAAO,SAAS,eAAe,SAAS;;;;;WAKxC;;WAEA,MAAM,SAAS;;;;;;WAMf;;WAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkCK,UAAU,OAAO,gBAAgB,UAAU,UAAU,SAAS,aAAa,2BAA2B;;iBCpD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;WAEA;;WAEA,yBAAyB,aAAa;;WAEtC,WAAW;;WAEX,WAAW;;WAEX,kBAAkB;;;iBAIZ;WACN,OAAO;WACP,WAAW;;;iBAIL,sBAAsB;WAC5B;;WAEA,gBAAgB;;WAEhB,QAAQ;;;;;;;;;;;;;;;;;;;wBAoBH,cAAc,OAAO,gBAAgB,eAAe,SAAS,uBAAuB;;YA0ExF,iBAAiB;;iBAGZ;;WAEN;;WAEA;;WAEA;;;iBAIM;WACN,OAAO;WACP,WAAW;;;;;;;iBAQL;WACN,SAAS;WACT,QAAQ;;;iBAIF;WACN;WACA;WACA;WACA;;WAEA;;WAEA;;;;;;WAMA,OAAO;;WAEP,WAAW;;WAEX,OAAOA;;WAEP;;;;;WAKA,OAAO;;;iBAID;WACN;WACA;WACA;WACA,OAAO;;WAEP;;WAEA,gBAAgB;;WAEhB;;WAEA;;WAEA,OAAOA;;;;;;;;;;;;;;;;wBAiBF,YAAY,OAAO,gBAAgB,UAAU,UAAU,SAAS,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBCtzBpF;;YAuCD,wBAAwB;;iBAGnB,uBAAuB;WAC7B,MAAM;;;;;;;;;;;wBAcD,eAAe,OAAO,gBAAgB,QAAQ,QAAQ;;wBA2MtD,kBAAkB,iBAAiB;;;;;;;;;;;;;;;;;wBCjRnC,cAAc,iBAAiB,WAAW,oBAAoB,WAAW;;qBCf5E,wBAAwB;;;;;;;;;qBAkBxB,kBAAkB"}
1
+ {"version":3,"file":"index.d.ts","names":["CutBound"],"sources":["../src/converter.ts","../src/endurance.ts","../src/joint.ts","../src/best.ts","../src/shortfall.ts","../src/check.ts","../src/join.ts","../src/sample.ts","../src/sensitivity.ts"],"mappings":";;;;;;wBAegB,SAAS,WAAW;;iBCJnB;;WAEN,IAAI;;WAEJ,QAAQ;;WAER;;;;;;WAMA,QAAQ;;;;;;WAMR;;;qBAIE;;;;;wBAMG,YAAY,UAAU,UAAU,SAAS,KAAK,iCAAiC;;wBAiB/E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P,WAAW;;;;;;;iBAQL;WACN,SAAS;WACT,QAAQ;;;iBAIF;WACN;WACA;WACA;WACA;;WAEA;;WAEA;;;;;;WAMA,OAAO;;WAEP,WAAW;;WAEX,OAAOA;;WAEP;;;;;WAKA,OAAO;;;iBAID;WACN;WACA;WACA;WACA,OAAO;;WAEP;;WAEA,gBAAgB;;WAEhB;;WAEA;;WAEA,OAAOA;;;;;;;;;;;;;;;;wBAiBF,YAAY,OAAO,gBAAgB,UAAU,UAAU,SAAS,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBCnzBpF;;YAyCD,wBAAwB;;iBAGnB,uBAAuB;WAC7B,MAAM;;;;;;;;;;;wBAcD,eAAe,OAAO,gBAAgB,QAAQ,QAAQ;;wBAwNtD,kBAAkB,iBAAiB;;;;;;;;;;;;;;;;;wBCnSnC,cAAc,iBAAiB,WAAW,oBAAoB,WAAW;;qBCf5E,wBAAwB;;;;;;;;;qBAkBxB,kBAAkB;;qBChBlB;;iBAGI,2BAA2B,aAAa;;WAE9C;;;YAIC;;;;;iBAMK;;WAEN,QAAQ;;WAER,OAAO;;WAEP,QAAQ;;WAER,aAAa;;WAEb,YAAY;;;iBAIN;WACN;WACA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA,SAAS;;;iBAIH;;;;;WAKN;WACA,MAAM;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA,gBAAgB;;WAEhB,QAAQ,SAAS;;;iBAIX;WACN;WACA,IAAI;;WAEJ;;WAEA,MAAM;;WAEN,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA2GZ,qBAAqB,OAAO,gBAAgB,UAAU,UAAU,SAAS,qBAAqB"}
package/build/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as ENDURANCE_SCHEMA } from "./layer-a3Fr70Uh.js";
2
- import { A as drawBlock, C as fixStops, D as Program, E as seasonPlan, F as dense, I as exact, L as isSource, M as reachOf, N as valueAt, O as caseParts, P as addTo, S as firstStop, T as seasonJoint, _ as seasonSupply, a as hasEnduranceError, b as storeAmounts, c as caseStopped, d as giftNeed, f as giftResource, g as monthlyShares, h as monthOf, i as curveFault, j as fewestRow, k as deviceUse, l as drawDemand, m as monthEdges, n as ENDURANCE_CODES, o as SEASON_HORIZON_DAYS, p as hasSeason, r as checkEndurance, s as caseDraws, t as joinEndurance, u as enduranceOf, v as shareOf, w as seasonBest, x as fewestOpen, y as sowDays } from "./join-BB1qLU4e.js";
2
+ import { A as deviceUse, C as fixStops, D as storeCapacities, E as seasonPlan, F as addTo, I as dense, L as exact, M as fewestRow, N as reachOf, O as Program, P as valueAt, R as deviceKey, S as firstStop, T as seasonJoint, _ as seasonSupply, a as hasEnduranceError, b as storeAmounts, c as caseStopped, d as giftNeed, f as giftResource, g as monthlyShares, h as monthOf, i as curveFault, j as drawBlock, k as caseParts, l as drawDemand, m as monthEdges, n as ENDURANCE_CODES, o as SEASON_HORIZON_DAYS, p as hasSeason, r as checkEndurance, s as caseDraws, t as joinEndurance, u as enduranceOf, v as shareOf, w as seasonBest, x as fewestOpen, y as sowDays, z as isSource } from "./join-OKGUUCVQ.js";
3
3
  import { DEFAULT_MAX_SETS, STACK_SCHEMA, dateOfDay, dayNumber, neededParts, stopSets, survivalAt, weakestBound } from "@mark1russell7/system-stack";
4
4
  import { ONE, ZERO, add, div, equals, mul, neg, sign, sub, toNumber } from "@mark1russell7/rational";
5
5
  import { solveLexicographic } from "@mark1russell7/linear";
@@ -64,7 +64,7 @@ function jointDays(layer, caseCuts, options, needs) {
64
64
  const parts = caseParts(layer, stoppedParts, stock, planned);
65
65
  if (supply !== void 0) {
66
66
  const devices = new Map([...deviceUse([...parts.cooks, ...parts.makers].map((converter) => [converter, 0])).limits].map(([key, limit]) => [key, limit.devices]));
67
- const { atHorizon, ...joint } = seasonJoint(seasonPlan(supply, parts, caseCuts, plan, devices), new Set(layer.stores.map((store) => store.resource)));
67
+ const { atHorizon, ...joint } = seasonJoint(seasonPlan(supply, parts, caseCuts, plan, devices, storeCapacities(layer, stoppedParts)), new Set(layer.stores.map((store) => store.resource)));
68
68
  return {
69
69
  ...base,
70
70
  ...joint,
@@ -305,7 +305,7 @@ function bestSeason(layer, caseCuts, options, stoppedParts) {
305
305
  const draws = caseDraws(layer, caseCuts, stoppedParts);
306
306
  const parts = caseParts(layer, stoppedParts, supply.stock, new Set(draws.map((draw) => draw.need)));
307
307
  const devices = new Map([...deviceUse([...parts.cooks, ...parts.makers].map((converter) => [converter, 0])).limits].map(([key, limit]) => [key, limit.devices]));
308
- const plan = seasonPlan(supply, parts, caseCuts, draws, devices);
308
+ const plan = seasonPlan(supply, parts, caseCuts, draws, devices, storeCapacities(layer, stoppedParts));
309
309
  const harvests = supply.harvests.length === 0 ? {} : { harvests: supply.harvests };
310
310
  if (draws.length === 0) return {
311
311
  case: caseCuts.case,
@@ -1442,6 +1442,236 @@ const SAMPLE_ENDURANCE = {
1442
1442
  asOf: "2025-12-01"
1443
1443
  }]
1444
1444
  };
1445
- export { ENDURANCE_CODES, ENDURANCE_SCHEMA, INDEPENDENT_MAX_WORK, INDEPENDENT_TOLERANCE, MINIMAL_STOP_CUTS, SAMPLE_ENDURANCE, SAMPLE_ENDURANCE_STACK, SEASON_HORIZON_DAYS, SHARES, YEAR_STARTS, bestEndurance, caseDraws, caseOutlook, caseShortfall, caseStopped, checkEndurance, curveFault, drawDemand, enduranceOf, eventStop, giftNeed, giftResource, hasEnduranceError, hasSeason, heldTail, independentStop, isSource, joinEndurance, jointDays, longestStop, minimalStops, monthEdges, monthOf, monthlyShares, seasonSupply, shareOf, sowDays, stopCurves, storeAmounts, survivalAt, survivalQuantile, tailDays, upperStop, yearChance, yearRate, yearShortfall };
1445
+ // src/sensitivity.ts
1446
+ /** The default step of `enduranceSensitivity`: 1 unit of each input. */
1447
+ const SENSITIVITY_STEP = 1;
1448
+ /** This function gives the size of a step down from a value. It is the step, or half the value when the value is not more than the step. */
1449
+ function stepDown(value, step) {
1450
+ return value > step ? step : value / 2;
1451
+ }
1452
+ /** This function gives each input of a layer and the layer after its step. */
1453
+ function variantsOf(layer, step) {
1454
+ const out = [];
1455
+ for (const store of layer.stores) {
1456
+ const amount = store.amount + step;
1457
+ const changed = {
1458
+ ...store,
1459
+ amount,
1460
+ ...store.capacity !== void 0 && amount > store.capacity ? { capacity: amount } : {}
1461
+ };
1462
+ out.push({
1463
+ id: `store:${store.id}`,
1464
+ kind: "store",
1465
+ target: store.id,
1466
+ field: "amount",
1467
+ value: store.amount,
1468
+ delta: step,
1469
+ layer: {
1470
+ ...layer,
1471
+ stores: layer.stores.map((item) => item === store ? changed : item)
1472
+ }
1473
+ });
1474
+ }
1475
+ for (const draw of layer.draws) {
1476
+ if (!(draw.perDay > 0)) continue;
1477
+ const delta = -stepDown(draw.perDay, step);
1478
+ const changed = {
1479
+ ...draw,
1480
+ perDay: draw.perDay + delta
1481
+ };
1482
+ out.push({
1483
+ id: `draw:${draw.need}`,
1484
+ kind: "draw",
1485
+ target: draw.need,
1486
+ field: "perDay",
1487
+ value: draw.perDay,
1488
+ delta,
1489
+ layer: {
1490
+ ...layer,
1491
+ draws: layer.draws.map((item) => item === draw ? changed : item)
1492
+ }
1493
+ });
1494
+ }
1495
+ const converters = layer.converters ?? [];
1496
+ const withConverters = (change) => ({
1497
+ ...layer,
1498
+ converters: converters.map(change)
1499
+ });
1500
+ const devicesSeen = /* @__PURE__ */ new Set();
1501
+ for (const converter of converters) {
1502
+ const key = `converter:${converter.id}`;
1503
+ if (converter.gives.amount > 0) {
1504
+ const gives = {
1505
+ ...converter.gives,
1506
+ amount: converter.gives.amount + step
1507
+ };
1508
+ out.push({
1509
+ id: `${key}.gives`,
1510
+ kind: "converter",
1511
+ target: converter.id,
1512
+ field: "gives",
1513
+ value: converter.gives.amount,
1514
+ delta: step,
1515
+ layer: withConverters((item) => item === converter ? {
1516
+ ...item,
1517
+ gives
1518
+ } : item)
1519
+ });
1520
+ }
1521
+ for (const input of converter.takes) {
1522
+ if (!(input.amount > 0)) continue;
1523
+ const delta = -stepDown(input.amount, step);
1524
+ const takes = converter.takes.map((item) => item === input ? {
1525
+ ...item,
1526
+ amount: item.amount + delta
1527
+ } : item);
1528
+ out.push({
1529
+ id: `${key}.takes.${input.resource}`,
1530
+ kind: "converter",
1531
+ target: converter.id,
1532
+ field: `takes.${input.resource}`,
1533
+ value: input.amount,
1534
+ delta,
1535
+ layer: withConverters((item) => item === converter ? {
1536
+ ...item,
1537
+ takes
1538
+ } : item)
1539
+ });
1540
+ }
1541
+ if (converter.perDay === void 0) continue;
1542
+ const perDay = converter.perDay + step;
1543
+ out.push({
1544
+ id: `${key}.perDay`,
1545
+ kind: "converter",
1546
+ target: converter.id,
1547
+ field: "perDay",
1548
+ value: converter.perDay,
1549
+ delta: step,
1550
+ layer: withConverters((item) => item === converter ? {
1551
+ ...item,
1552
+ perDay
1553
+ } : item)
1554
+ });
1555
+ const device = deviceKey(converter);
1556
+ if (devicesSeen.has(device)) continue;
1557
+ devicesSeen.add(device);
1558
+ const count = converter.devices ?? 1;
1559
+ out.push({
1560
+ id: `${device}.devices`,
1561
+ kind: "converter",
1562
+ target: converter.device ?? converter.id,
1563
+ field: "devices",
1564
+ value: count,
1565
+ delta: 1,
1566
+ layer: withConverters((item) => deviceKey(item) === device ? {
1567
+ ...item,
1568
+ devices: count + 1
1569
+ } : item)
1570
+ });
1571
+ }
1572
+ return out;
1573
+ }
1574
+ /** This function gives `a` minus `b` for each curve. */
1575
+ function minus(a, b) {
1576
+ return {
1577
+ event: a.event - b.event,
1578
+ independent: a.independent - b.independent,
1579
+ upper: a.upper - b.upper
1580
+ };
1581
+ }
1582
+ /** This function compares two inputs for the rank: the sums of the levels in the order of the levels, the largest first, then the key. */
1583
+ function rankOrder(levels) {
1584
+ return (a, b) => {
1585
+ for (const level of levels) {
1586
+ const diff = (b.levels[level] ?? 0) - (a.levels[level] ?? 0);
1587
+ if (diff > 0) return 1;
1588
+ if (diff < 0) return -1;
1589
+ }
1590
+ return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
1591
+ };
1592
+ }
1593
+ /**
1594
+ * This function gives the change in the days of each draw for one step of each input of a layer, ranked.
1595
+ * The rank names the input that moves the days most. A lower draw costs nothing, so the rank also shows such inputs.
1596
+ *
1597
+ * - The inputs are the amount of each store and the rate `perDay` of each draw. Each converter gives the amount of its
1598
+ * gift, the amount of each take, its limit `perDay` and its count of devices.
1599
+ * - The step adds to a store, to a gift, to a limit `perDay` and to a count of devices. It takes off a draw and a take,
1600
+ * because less of them gives more days. A count of devices takes 1 more device.
1601
+ * - When the rate of a draw or a take is not more than the step, the step takes off half the value. Thus the value stays more than 0.
1602
+ * - A store with a capacity gets a capacity that is not less than its new amount.
1603
+ * - The change of a draw is a finite difference: the days of `enduranceOf` after the step minus the days before it.
1604
+ * `perUnit` divides it by the size of the change of the value.
1605
+ * - An input that no draw of the case uses has a change of 0, for example a store past its due date.
1606
+ * - The rank is by level. It compares the sum of the changes of the draws of the first level, then of the next level, and so on.
1607
+ * The largest sum comes first. Two equal inputs go in the order of their keys. Thus the order is stable.
1608
+ * - The days are exact for the inputs. `chance` is the chance that a draw gets its need through the event. It comes
1609
+ * from `caseOutlook` with the share `take`, before and after the step, with the bands of the cuts. The change of the
1610
+ * chance is the change of the point values. It has no interval.
1611
+ *
1612
+ * The cost is one `enduranceOf` and one `caseOutlook` for each input. The function is pure.
1613
+ *
1614
+ * @param layer - The endurance layer
1615
+ * @param caseCuts - The case
1616
+ * @param options - The date, the stack or the parts that are also down, the horizon and the step
1617
+ * @returns The endurance before the steps, and the inputs in the order of the rank
1618
+ * @throws RangeError for a step that is not a finite number more than 0, and for the errors of `enduranceOf` and `caseOutlook`
1619
+ */
1620
+ function enduranceSensitivity(layer, caseCuts, options) {
1621
+ const { step: given, ...caseOptions } = options;
1622
+ const step = given ?? 1;
1623
+ if (!(step > 0) || !Number.isFinite(step)) throw new RangeError(`The step ${String(given)} is not a finite number more than 0.`);
1624
+ const base = enduranceOf(layer, caseCuts, caseOptions);
1625
+ const outlookOf = (changed) => new Map(caseOutlook(changed, caseCuts, {
1626
+ ...caseOptions,
1627
+ share: "take"
1628
+ }).draws.map((draw) => [draw.need, draw]));
1629
+ const baseOutlook = outlookOf(layer);
1630
+ const levelOf = new Map(layer.draws.map((draw) => [draw.need, draw.level]));
1631
+ const inputs = variantsOf(layer, step).map(({ layer: changed, ...variant }) => {
1632
+ const after = enduranceOf(changed, caseCuts, caseOptions);
1633
+ const outlook = baseOutlook.size === 0 ? void 0 : outlookOf(changed);
1634
+ const sums = new Map(layer.levels.map((level) => [level, 0]));
1635
+ const needs = Object.entries(base.needs).map(([need, held]) => {
1636
+ const days = after.needs[need]?.days ?? held.days;
1637
+ const change = days === held.days ? 0 : days - held.days;
1638
+ const level = levelOf.get(need) ?? "";
1639
+ const sum = sums.get(level);
1640
+ if (sum !== void 0) sums.set(level, sum + change);
1641
+ const from = baseOutlook.get(need);
1642
+ const to = outlook?.get(need);
1643
+ const chance = from === void 0 || to === void 0 ? {} : { chance: {
1644
+ before: from.lasts,
1645
+ after: to.lasts,
1646
+ change: minus(to.lasts, from.lasts),
1647
+ ...from.band === void 0 ? {} : { beforeBand: from.band },
1648
+ ...to.band === void 0 ? {} : { afterBand: to.band }
1649
+ } };
1650
+ return {
1651
+ need,
1652
+ level,
1653
+ before: held.days,
1654
+ after: days,
1655
+ change,
1656
+ perUnit: change / Math.abs(variant.delta),
1657
+ ...chance
1658
+ };
1659
+ });
1660
+ return {
1661
+ ...variant,
1662
+ needs,
1663
+ levels: Object.fromEntries(sums)
1664
+ };
1665
+ });
1666
+ inputs.sort(rankOrder(layer.levels));
1667
+ return {
1668
+ case: caseCuts.case,
1669
+ at: options.at,
1670
+ step,
1671
+ base,
1672
+ inputs
1673
+ };
1674
+ }
1675
+ export { ENDURANCE_CODES, ENDURANCE_SCHEMA, INDEPENDENT_MAX_WORK, INDEPENDENT_TOLERANCE, MINIMAL_STOP_CUTS, SAMPLE_ENDURANCE, SAMPLE_ENDURANCE_STACK, SEASON_HORIZON_DAYS, SENSITIVITY_STEP, SHARES, YEAR_STARTS, bestEndurance, caseDraws, caseOutlook, caseShortfall, caseStopped, checkEndurance, curveFault, drawDemand, enduranceOf, enduranceSensitivity, eventStop, giftNeed, giftResource, hasEnduranceError, hasSeason, heldTail, independentStop, isSource, joinEndurance, jointDays, longestStop, minimalStops, monthEdges, monthOf, monthlyShares, seasonSupply, shareOf, sowDays, stopCurves, storeAmounts, survivalAt, survivalQuantile, tailDays, upperStop, yearChance, yearRate, yearShortfall };
1446
1676
 
1447
1677
  //# sourceMappingURL=index.js.map