@mark1russell7/stack-endurance 0.8.0 → 0.8.2
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 +38 -2
- package/build/add-on.js +1 -1
- package/build/index.d.ts +67 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +220 -2
- package/build/index.js.map +1 -1
- package/build/{join-Cglucg6Q.js → join-DQxIAP0v.js} +229 -55
- package/build/join-DQxIAP0v.js.map +1 -0
- package/package.json +3 -1
- package/build/join-Cglucg6Q.js.map +0 -1
package/README.md
CHANGED
|
@@ -28,10 +28,12 @@ Each function is a plain function with no clock, no network and no file. The uni
|
|
|
28
28
|
- A take of a resource uses its stock first, then the converters that make it, through each chain. Thus a direct use of the battery can use the fuel of the generator.
|
|
29
29
|
- The days of a draw are the largest `T` at which the stores give `T` days of its demand, after each higher draw.
|
|
30
30
|
- Each higher draw keeps back its own days times its demand. Thus the takes of all the draws are not more than the stores at any time.
|
|
31
|
-
- The take order is a rule, not the best share. The days are the days of this order. `takes` gives the amount of each resource that a draw takes.
|
|
31
|
+
- The take order is a rule, not the best share. The days are the days of this order. A draw with a shared device is different (refer to the limit `perDay` below). `takes` gives the amount of each resource that a draw takes.
|
|
32
|
+
- `jointDays` gives the best share for a set of draws. Refer to the section on the joint days.
|
|
32
33
|
- A store gives nothing on its due date and after it. A store or a converter whose part stops gives nothing.
|
|
33
34
|
- A converter can have a limit `perDay`, the most runs of one device in a day, and a count `devices` (default 1). Over `T` days, the converters of one device use `devices` times `T` days of the device or less. One run uses `1 / perDay` of a day of the device. A maker and its taker on one device share these days.
|
|
34
35
|
- The higher draws use the days of a device first. Each higher draw keeps back its part of each day for all the days of a lower draw. This is the safe side.
|
|
36
|
+
- A draw can reach two converters of one device, each with a limit `perDay` more than 0. Then the draw does not use the take order. A linear program with exact rationals gives the best share of its stocks and of the device-days. The days of the draw are the largest `T` of this share. The take order gave the device-days to the first converter of the list, also when a faster converter gave more.
|
|
35
37
|
- `capped` names the converters whose limit stops a draw while their inputs have more. After `days`, the shortfall counts each day as a full shortfall.
|
|
36
38
|
|
|
37
39
|
With a base stack, a cut also stops each part that cannot work without the cut part. Thus a pump that needs the grid stops with the grid.
|
|
@@ -60,11 +62,45 @@ Only the cuts of the supply, and of the parts that it needs through the stack, c
|
|
|
60
62
|
|
|
61
63
|
Two groups of cuts that share no minimal set are independent, so the independent stop takes the product over the groups. In a group of up to 12 cuts, it adds the chance of each state of the cuts. In a larger group, it uses the Shannon expansion of `pathSetReliability` (`@mark1russell7/collections`). The expansion merges two states with the same sets left, so sets with few shared cuts give a small expansion. The expansion is exponential in the worst case. The groups share one budget, `INDEPENDENT_MAX_WORK`, and the option `maxWork` changes it. When the expansions pass the budget, `independent` is `upper`, and `independentUpper` names the need.
|
|
62
64
|
|
|
65
|
+
## The joint days
|
|
66
|
+
|
|
67
|
+
`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.
|
|
68
|
+
|
|
69
|
+
- The function finds the best share of the stores and of the runs of the converters. It does not use the take order.
|
|
70
|
+
- It solves a linear program with exact rationals (`@mark1russell7/linear`). Only the change back to a number rounds.
|
|
71
|
+
- The variables are the days, the take of each direct use, the runs of each converter, and the share of each stock for each draw.
|
|
72
|
+
- Each draw of the set gets `d` times its demand. The takes of a resource are not more than its stock plus the gifts of its makers.
|
|
73
|
+
- The runs of a converter with a limit `perDay` are not more than `perDay` times `devices` times `d`. The converters of one device share the limit.
|
|
74
|
+
- After the largest `d`, the program takes the smallest sum of the shares of the stocks. Thus the takes have no waste.
|
|
75
|
+
- A third objective takes the fewest device-days of the converters. Thus the runs are not more than the plan uses.
|
|
76
|
+
- The draws that are not in the set are outside this plan. They take nothing.
|
|
77
|
+
- A draw of the set whose supply works, or whose demand is 0, holds without end. It goes in `holds`, as `enduranceOf` leaves it out.
|
|
78
|
+
- A need code that is not the need of a draw is an error, a `RangeError`.
|
|
79
|
+
- A store gives all of its amount when its due date is after the date of the query. This is the rule of `enduranceOf`.
|
|
80
|
+
- `binding` gives the resources with a store that the optimum uses up. `takes` gives the takes of each draw. `runs` gives the runs of each converter.
|
|
81
|
+
- `capped` gives the converters of the plan whose limit `perDay` stops more runs at the optimum. A maker that the plan does not use is not in the list.
|
|
82
|
+
|
|
83
|
+
An example: a tank holds 20 L of water. The drink takes 3 L each day, at the higher level. Only a cook feeds the food, 2000 kcal each day. One run of the cook takes 1 L of water, 0.5 kg of grain and 0.125 kg of fuel, and gives 2000 kcal.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { enduranceOf, jointDays } from "@mark1russell7/stack-endurance";
|
|
87
|
+
|
|
88
|
+
const ordered = enduranceOf(layer, dry, { at: "2026-12-01" });
|
|
89
|
+
// ordered.needs.drink.days is 6.667, and ordered.needs.food.days is 0.
|
|
90
|
+
const joint = jointDays(layer, dry, { at: "2026-12-01" }, ["drink", "food"]);
|
|
91
|
+
// joint.days is 5, and joint.binding is ["water"].
|
|
92
|
+
// joint.takes is { drink: { water: 15 }, food: { water: 5, grain: 2.5, fuel: 0.625 } }.
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
The take order gives the drink 6.667 days. The drink keeps back all the water for these days, so the food gets 0 days. Together, the two draws take 4 L each day. Thus the water holds 5 days for the two.
|
|
96
|
+
|
|
97
|
+
The joint days are not more than the days of each draw alone. The take order tells the days of a rule of priority. The joint days tell the days of the best share.
|
|
98
|
+
|
|
63
99
|
## The entries
|
|
64
100
|
|
|
65
101
|
| Entry | What it has |
|
|
66
102
|
| --- | --- |
|
|
67
|
-
| `@mark1russell7/stack-endurance` | The records of a layer, the endurance and shortfall queries, the gate, the join and a sample. |
|
|
103
|
+
| `@mark1russell7/stack-endurance` | The records of a layer, the endurance, joint days and shortfall queries, the gate, the join and a sample. |
|
|
68
104
|
| `@mark1russell7/stack-endurance/schema` | `enduranceSchema`, the zod schema of a layer, with `parseEndurance`. |
|
|
69
105
|
| `@mark1russell7/stack-endurance/add-on` | `enduranceAddOn`, the add-on in the form of the layer contract of the core. |
|
|
70
106
|
|
package/build/add-on.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { r as checkEndurance, t as joinEndurance } from "./join-
|
|
1
|
+
import { r as checkEndurance, t as joinEndurance } from "./join-DQxIAP0v.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
|
@@ -41,13 +41,16 @@ export declare function giftResource(converter: Converter): string | undefined;
|
|
|
41
41
|
* - The days of a draw are the largest `T` at which the stores give `T` days of its demand, after each higher draw.
|
|
42
42
|
* - Each higher draw keeps back its own days times its demand. Thus the takes of all the draws are not more than the stores at any time.
|
|
43
43
|
* - The take order is a rule, not the best share: the direct uses in the order of the list, then the converters. The days are the days of this order.
|
|
44
|
+
* A draw with a shared device is different. Refer to the last item.
|
|
44
45
|
* - A converter whose device stops gives nothing. A store whose part stops gives nothing.
|
|
45
46
|
* - A converter can give a resource. A take of a resource uses the stock first, then the converters that make it, through each chain.
|
|
46
47
|
* - A converter with a limit `perDay` runs `perDay` times `devices` times `T` times or less in `T` days. The converters of one device share it.
|
|
47
48
|
* - The higher draws use the runs of a limit first.
|
|
48
49
|
* - A higher draw keeps back its runs of each day for all the days of a lower draw, also when its own days are fewer. This is the safe side.
|
|
49
|
-
* - Two limited converters of one device can make the shortfall of
|
|
50
|
-
*
|
|
50
|
+
* - Two limited converters of one device can make the shortfall of the take order decrease when the days increase.
|
|
51
|
+
* Thus a draw whose ways reach two converters of one device, each with a limit `perDay` more than 0, does not use the take order.
|
|
52
|
+
* A linear program with exact rationals gives its days: the largest `T` with the best share of its stocks and of the device-days.
|
|
53
|
+
* At this `T`, the draw takes the smallest sum of its takes of the stocks, each over its stock, then the fewest device-days.
|
|
51
54
|
*
|
|
52
55
|
* 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.
|
|
53
56
|
*
|
|
@@ -57,6 +60,68 @@ export declare function giftResource(converter: Converter): string | undefined;
|
|
|
57
60
|
* @returns The endurance of each draw whose supply stops
|
|
58
61
|
*/
|
|
59
62
|
export declare function enduranceOf(layer: EnduranceLayer, caseCuts: CaseCuts, options: CaseOptions): Endurance;
|
|
63
|
+
/** The joint days of a set of draws in a case, with the share of the stores at the optimum. */
|
|
64
|
+
export interface JointDays {
|
|
65
|
+
readonly case: string;
|
|
66
|
+
readonly at: IsoDate;
|
|
67
|
+
/**
|
|
68
|
+
* The largest count of days that each draw of `plan` holds at the same time. It is `Infinity` when `plan` is empty,
|
|
69
|
+
* or when the stores give each draw of `plan` without end.
|
|
70
|
+
*/
|
|
71
|
+
readonly days: number;
|
|
72
|
+
/** The needs of the set that take from the stores: the case stops their supply, and their demand is more than 0. The order is the order of `caseDraws`. */
|
|
73
|
+
readonly plan: readonly string[];
|
|
74
|
+
/** The needs of the set that take nothing from the stores, in the order of their codes. The case does not stop their supply, or their demand is 0. */
|
|
75
|
+
readonly holds: readonly string[];
|
|
76
|
+
/**
|
|
77
|
+
* The amount of each resource that each draw of `plan` takes from the stores in `days`. The keys are the code of the need, then the code of the resource.
|
|
78
|
+
* A take through a converter that makes a resource counts the inputs of the converter. A resource with no take is not in the record.
|
|
79
|
+
* The record is empty when `days` is `Infinity`.
|
|
80
|
+
*/
|
|
81
|
+
readonly takes: Readonly<Record<string, Readonly<Record<string, number>>>>;
|
|
82
|
+
/**
|
|
83
|
+
* The resources that limit `days`, in the order of their codes. Each one has a store in the layer, is an input of `plan` through each chain,
|
|
84
|
+
* and has no stock left at the optimum. The list is empty when `days` is `Infinity`.
|
|
85
|
+
*/
|
|
86
|
+
readonly binding: readonly string[];
|
|
87
|
+
/** The count of runs of each converter at the optimum, by the `id` of the converter. A converter with no run is not in the record. */
|
|
88
|
+
readonly runs: Readonly<Record<string, number>>;
|
|
89
|
+
/**
|
|
90
|
+
* The converters of the plan whose limit `perDay` stops more runs at the optimum, in the order of their `id`. A converter of the plan
|
|
91
|
+
* gives a need of the plan, or makes an input of the plan through each chain.
|
|
92
|
+
* The list is empty when `days` is `Infinity` or 0. Else it has each converter of the plan with a limit of 0.
|
|
93
|
+
*/
|
|
94
|
+
readonly capped: readonly string[];
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* This function gives the joint days of a set of draws. These are the largest count of days `d` at which each draw holds `d` days.
|
|
98
|
+
* The draws hold at the same time, with the best share of the stores and of the runs of the converters.
|
|
99
|
+
*
|
|
100
|
+
* - The function solves a linear program over the totals of the span. Its variables are `d`, the take of each direct use,
|
|
101
|
+
* the runs of each converter, and the share of each stock.
|
|
102
|
+
* - Each draw of the plan gets `d` times its demand (`drawDemand`) from its direct uses and from the converters that give its need.
|
|
103
|
+
* - The takes of each resource are not more than its stock (`storeAmounts` at `at`) plus the gifts of the converters that make it.
|
|
104
|
+
* - A converter whose device stops gives nothing, and a store whose part stops gives nothing, as in `enduranceOf`.
|
|
105
|
+
* - The runs of a converter with a limit `perDay` are not more than `perDay` times `devices` times `d`. The converters of one device share it, as in `enduranceOf`.
|
|
106
|
+
* - A store counts with all of its amount when its due date is after `at`, as in `enduranceOf`. A due date in the span has no effect.
|
|
107
|
+
* - After the largest `d`, a second objective takes the smallest sum of the shares of the stocks. Thus the takes have no waste.
|
|
108
|
+
* - A third objective takes the fewest device-days of the converters. Thus the runs are not more than the plan uses.
|
|
109
|
+
* - The draws that are not in the set are outside this plan. They take nothing in the program.
|
|
110
|
+
* - A need of the set whose supply works, or whose demand is 0, holds without end. It goes in `holds`, as `enduranceOf` leaves it out.
|
|
111
|
+
*
|
|
112
|
+
* The take order of `enduranceOf` gives the stores to a higher draw first. This function gives one share for the whole set.
|
|
113
|
+
* Thus a draw of the set can get fewer days here than alone, and a lower draw can get more.
|
|
114
|
+
*
|
|
115
|
+
* The program uses exact rationals, so the result has no error of the method. Only the change back to a number rounds.
|
|
116
|
+
*
|
|
117
|
+
* @param layer - The endurance layer
|
|
118
|
+
* @param caseCuts - The case
|
|
119
|
+
* @param options - The date, and the stack or the parts that are also down
|
|
120
|
+
* @param needs - The codes of the needs of the draws of the set
|
|
121
|
+
* @returns The joint days, the takes of each draw, the binding resources and the runs of each converter
|
|
122
|
+
* @throws {RangeError} A code of `needs` is not the need of a draw of the layer, or a date is not `YYYY-MM-DD`.
|
|
123
|
+
*/
|
|
124
|
+
export declare function jointDays(layer: EnduranceLayer, caseCuts: CaseCuts, options: CaseOptions, needs: readonly string[]): JointDays;
|
|
60
125
|
/** This function gives the chance that a cut lasts more than `days`, linear between the points of the curve. After the last point it is 0. */
|
|
61
126
|
export declare function survivalAt(curve: Survival, days: number): number;
|
|
62
127
|
/**
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/endurance.ts","../src/shortfall.ts","../src/check.ts","../src/join.ts","../src/sample.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/endurance.ts","../src/joint.ts","../src/shortfall.ts","../src/check.ts","../src/join.ts","../src/sample.ts"],"mappings":";;;iBASiB;;WAEN,IAAI;;WAEJ,QAAQ;;WAER;;;;;;wBAOK,YAAY,UAAU,UAAU,SAAS,KAAK,iCAAiC;;;;;;;wBAoB/E,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0LxB,YAAY,OAAO,gBAAgB,UAAU,UAAU,SAAS,cAAc;;iBC7P7E;WACN;WACA,IAAI;;;;;WAKJ;;WAEA;;WAEA;;;;;;WAMA,OAAO,SAAS,eAAe,SAAS;;;;;WAKxC;;WAEA,MAAM,SAAS;;;;;;WAMf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA+BK,UAAU,OAAO,gBAAgB,UAAU,UAAU,SAAS,aAAa,2BAA2B;;wBC/DtG,WAAW,OAAO,UAAU;;;;;;wBAoB5B,SAAS,OAAO,UAAU,cAAc;;;;;;;;;;iBAqBvC,yBAAyB,KAAK,gCAAgC,KAAK;;WAEzE;;iBAGM,yBAAyB;;WAE/B;;;;;;wBAqBK,iBAAiB,OAAO,UAAU;;wBAWlC,WAAW,OAAO;;wBAKlB,SAAS,OAAO;;;;;;;;;;;;;;;;wBAoDhB,UAAU,iBAAiB,YAAY,QAAQ,sCAAsC;;;;;;;;wBAiBrF,YAAY,iBAAiB,aAAa;;;;;qBAU7C;;;;;wBAMG,aAAa,eAAe,QAAQ;;;;;;;;;;wBA8BpC,UAAU,iBAAiB,YAAY,mDAAmD;;qBAiB7F;;;;;;qBAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA6BG,gBAAgB,iBAAiB,YAAY,kDAAkD,mBAAyC;;;;;iBAiKvI;WACN,OAAO;;WAEP,aAAa;WACb,OAAO;;;;;WAKP;;WAEA;;;;;;;;;;;;;;;wBA+DK,WAAW,UAAU,UAAU,MAAM,MAAM,UAAS,mBAAwB;;iBAM3E;;;;;WAKN,QAAQ,SAAS;;WAEjB,aAAa,SAAS;;WAEtB,OAAO,SAAS;;;;;WAKhB;;WAEA;;WAEA;;;;;;;;;;;;wBAiBK,cAAc,OAAO,gBAAgB,UAAU,UAAU,WAAW,WAAW,UAAS,mBAAwB;;iBA4B/G,sBAAsB;WAC5B;;WAEA;aAA2B;aAAuB;aAAuB,WAAW;aAAoB,WAAW;;;;;;;;;;;;;;;;wBAgB9G,cAAc,OAAO,gBAAgB,eAAe,SAAS,cAAc,mBAAmB;;;;;;;;;;;;;;;;;;;;;;qBC7iBjG;;YAiCD,wBAAwB;;iBAGnB,uBAAuB;WAC7B,MAAM;;;;;;;;;;;wBAcD,eAAe,OAAO,gBAAgB,QAAQ,QAAQ;;wBA+ItD,kBAAkB,iBAAiB;;;;;;;;;;;;;;;;;wBCxMnC,cAAc,iBAAiB,WAAW,oBAAoB,WAAW;;qBCf5E,wBAAwB;;;;;;;;;qBAkBxB,kBAAkB"}
|
package/build/index.js
CHANGED
|
@@ -1,7 +1,225 @@
|
|
|
1
1
|
import { t as ENDURANCE_SCHEMA } from "./layer-a3Fr70Uh.js";
|
|
2
|
-
import { a as hasEnduranceError, c as drawDemand, d as giftResource, f as storeAmounts, i as curveFault, l as enduranceOf, n as ENDURANCE_CODES, o as caseDraws, r as checkEndurance, s as caseStopped, t as joinEndurance, u as giftNeed } from "./join-
|
|
2
|
+
import { _ as deviceKey, a as hasEnduranceError, c as drawDemand, d as giftResource, f as storeAmounts, g as canRun, h as exact, i as curveFault, l as enduranceOf, m as dense, n as ENDURANCE_CODES, o as caseDraws, p as addTo, r as checkEndurance, s as caseStopped, t as joinEndurance, u as giftNeed, v as inputsOf } from "./join-DQxIAP0v.js";
|
|
3
3
|
import { DEFAULT_MAX_SETS, STACK_SCHEMA, neededParts, stopSets } from "@mark1russell7/system-stack";
|
|
4
|
+
import { solveLexicographic } from "@mark1russell7/linear";
|
|
5
|
+
import { ONE, ZERO, add, div, equals, mul, neg, sign, toNumber } from "@mark1russell7/rational";
|
|
4
6
|
import { pathSetReliability, stronglyConnectedComponents } from "@mark1russell7/collections/graph";
|
|
7
|
+
// src/joint.ts
|
|
8
|
+
/**
|
|
9
|
+
* This function gives the joint days of a set of draws. These are the largest count of days `d` at which each draw holds `d` days.
|
|
10
|
+
* The draws hold at the same time, with the best share of the stores and of the runs of the converters.
|
|
11
|
+
*
|
|
12
|
+
* - The function solves a linear program over the totals of the span. Its variables are `d`, the take of each direct use,
|
|
13
|
+
* the runs of each converter, and the share of each stock.
|
|
14
|
+
* - Each draw of the plan gets `d` times its demand (`drawDemand`) from its direct uses and from the converters that give its need.
|
|
15
|
+
* - The takes of each resource are not more than its stock (`storeAmounts` at `at`) plus the gifts of the converters that make it.
|
|
16
|
+
* - A converter whose device stops gives nothing, and a store whose part stops gives nothing, as in `enduranceOf`.
|
|
17
|
+
* - The runs of a converter with a limit `perDay` are not more than `perDay` times `devices` times `d`. The converters of one device share it, as in `enduranceOf`.
|
|
18
|
+
* - A store counts with all of its amount when its due date is after `at`, as in `enduranceOf`. A due date in the span has no effect.
|
|
19
|
+
* - After the largest `d`, a second objective takes the smallest sum of the shares of the stocks. Thus the takes have no waste.
|
|
20
|
+
* - A third objective takes the fewest device-days of the converters. Thus the runs are not more than the plan uses.
|
|
21
|
+
* - The draws that are not in the set are outside this plan. They take nothing in the program.
|
|
22
|
+
* - A need of the set whose supply works, or whose demand is 0, holds without end. It goes in `holds`, as `enduranceOf` leaves it out.
|
|
23
|
+
*
|
|
24
|
+
* The take order of `enduranceOf` gives the stores to a higher draw first. This function gives one share for the whole set.
|
|
25
|
+
* Thus a draw of the set can get fewer days here than alone, and a lower draw can get more.
|
|
26
|
+
*
|
|
27
|
+
* The program uses exact rationals, so the result has no error of the method. Only the change back to a number rounds.
|
|
28
|
+
*
|
|
29
|
+
* @param layer - The endurance layer
|
|
30
|
+
* @param caseCuts - The case
|
|
31
|
+
* @param options - The date, and the stack or the parts that are also down
|
|
32
|
+
* @param needs - The codes of the needs of the draws of the set
|
|
33
|
+
* @returns The joint days, the takes of each draw, the binding resources and the runs of each converter
|
|
34
|
+
* @throws {RangeError} A code of `needs` is not the need of a draw of the layer, or a date is not `YYYY-MM-DD`.
|
|
35
|
+
*/
|
|
36
|
+
function jointDays(layer, caseCuts, options, needs) {
|
|
37
|
+
const known = new Set(layer.draws.map((draw) => draw.need));
|
|
38
|
+
const unknown = needs.filter((need) => !known.has(need));
|
|
39
|
+
if (unknown.length > 0) throw new RangeError(`The needs ${unknown.map((need) => `"${need}"`).join(", ")} are not the needs of draws of the layer.`);
|
|
40
|
+
const stoppedParts = caseStopped(caseCuts, options);
|
|
41
|
+
const stock = storeAmounts(layer, options.at, stoppedParts);
|
|
42
|
+
const wanted = new Set(needs);
|
|
43
|
+
const plan = caseDraws(layer, caseCuts, stoppedParts).filter((draw) => wanted.has(draw.need));
|
|
44
|
+
const planned = new Set(plan.map((draw) => draw.need));
|
|
45
|
+
const base = {
|
|
46
|
+
case: caseCuts.case,
|
|
47
|
+
at: options.at,
|
|
48
|
+
plan: plan.map((draw) => draw.need),
|
|
49
|
+
holds: [...wanted].filter((need) => !planned.has(need)).sort()
|
|
50
|
+
};
|
|
51
|
+
const endless = {
|
|
52
|
+
...base,
|
|
53
|
+
days: 1e400,
|
|
54
|
+
takes: {},
|
|
55
|
+
binding: [],
|
|
56
|
+
runs: {},
|
|
57
|
+
capped: []
|
|
58
|
+
};
|
|
59
|
+
if (plan.length === 0) return endless;
|
|
60
|
+
const direct = (layer.direct ?? []).filter((use) => planned.has(use.need) && use.amount > 0);
|
|
61
|
+
const live = (layer.converters ?? []).filter((converter) => canRun(converter, stoppedParts));
|
|
62
|
+
const cooks = live.filter((converter) => planned.has(giftNeed(converter) ?? ""));
|
|
63
|
+
const makers = live.filter((converter) => giftResource(converter) !== void 0);
|
|
64
|
+
const resources = /* @__PURE__ */ new Set([...direct.map((use) => use.resource), ...[...cooks, ...makers].flatMap((converter) => [...inputsOf(converter).keys()])]);
|
|
65
|
+
for (const maker of makers) resources.add(giftResource(maker) ?? "");
|
|
66
|
+
const stocked = [...resources].filter((resource) => (stock.get(resource) ?? 0) > 0).sort();
|
|
67
|
+
let count = 0;
|
|
68
|
+
const next = () => count++;
|
|
69
|
+
const days = next();
|
|
70
|
+
const useCol = direct.map(next);
|
|
71
|
+
const cookCol = cooks.map(next);
|
|
72
|
+
const makerCol = plan.map(() => makers.map(next));
|
|
73
|
+
const shareCol = plan.map(() => new Map(stocked.map((resource) => [resource, next()])));
|
|
74
|
+
const rows = [];
|
|
75
|
+
plan.forEach((draw, i) => {
|
|
76
|
+
const need = /* @__PURE__ */ new Map([[days, neg(exact(drawDemand(draw, caseCuts)))]]);
|
|
77
|
+
direct.forEach((use, k) => {
|
|
78
|
+
if (use.need === draw.need) addTo(need, useCol[k], exact(use.amount));
|
|
79
|
+
});
|
|
80
|
+
cooks.forEach((cook, c) => {
|
|
81
|
+
if (giftNeed(cook) === draw.need) addTo(need, cookCol[c], exact(cook.gives.amount));
|
|
82
|
+
});
|
|
83
|
+
rows.push({
|
|
84
|
+
row: need,
|
|
85
|
+
relation: ">=",
|
|
86
|
+
rhs: ZERO
|
|
87
|
+
});
|
|
88
|
+
const balance = new Map([...resources].map((resource) => [resource, /* @__PURE__ */ new Map()]));
|
|
89
|
+
const consume = (resource, index, amount) => addTo(balance.get(resource), index, amount);
|
|
90
|
+
direct.forEach((use, k) => {
|
|
91
|
+
if (use.need === draw.need) consume(use.resource, useCol[k], ONE);
|
|
92
|
+
});
|
|
93
|
+
cooks.forEach((cook, c) => {
|
|
94
|
+
if (giftNeed(cook) !== draw.need) return;
|
|
95
|
+
for (const [resource, each] of inputsOf(cook)) consume(resource, cookCol[c], exact(each));
|
|
96
|
+
});
|
|
97
|
+
makers.forEach((maker, m) => {
|
|
98
|
+
const index = makerCol[i]?.[m];
|
|
99
|
+
for (const [resource, each] of inputsOf(maker)) consume(resource, index, exact(each));
|
|
100
|
+
consume(giftResource(maker), index, neg(exact(maker.gives.amount)));
|
|
101
|
+
});
|
|
102
|
+
for (const [resource, index] of shareCol[i] ?? []) consume(resource, index, neg(ONE));
|
|
103
|
+
for (const row of balance.values()) if (row.size > 0) rows.push({
|
|
104
|
+
row,
|
|
105
|
+
relation: "<=",
|
|
106
|
+
rhs: ZERO
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
const runCols = new Map([...cooks.map((cook, c) => [cook, [cookCol[c]]]), ...makers.map((maker, m) => [maker, makerCol.map((row) => row[m])])]);
|
|
110
|
+
const limits = /* @__PURE__ */ new Map();
|
|
111
|
+
for (const [converter, indexes] of runCols) {
|
|
112
|
+
if (converter.perDay === void 0) continue;
|
|
113
|
+
if (!(converter.perDay > 0)) {
|
|
114
|
+
for (const index of indexes) rows.push({
|
|
115
|
+
row: /* @__PURE__ */ new Map([[index, ONE]]),
|
|
116
|
+
relation: "<=",
|
|
117
|
+
rhs: ZERO
|
|
118
|
+
});
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const key = deviceKey(converter);
|
|
122
|
+
const limit = limits.get(key) ?? {
|
|
123
|
+
row: /* @__PURE__ */ new Map([[days, neg(exact(converter.devices ?? 1))]]),
|
|
124
|
+
converters: []
|
|
125
|
+
};
|
|
126
|
+
limits.set(key, limit);
|
|
127
|
+
limit.converters.push(converter);
|
|
128
|
+
for (const index of indexes) addTo(limit.row, index, div(ONE, exact(converter.perDay)));
|
|
129
|
+
}
|
|
130
|
+
for (const { row } of limits.values()) rows.push({
|
|
131
|
+
row,
|
|
132
|
+
relation: "<=",
|
|
133
|
+
rhs: ZERO
|
|
134
|
+
});
|
|
135
|
+
for (const resource of stocked) {
|
|
136
|
+
const row = /* @__PURE__ */ new Map();
|
|
137
|
+
for (const shares of shareCol) addTo(row, shares.get(resource), ONE);
|
|
138
|
+
rows.push({
|
|
139
|
+
row,
|
|
140
|
+
relation: "<=",
|
|
141
|
+
rhs: exact(stock.get(resource) ?? 0)
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const constraints = rows.map(({ row, relation, rhs }) => ({
|
|
145
|
+
coefficients: dense(row, count),
|
|
146
|
+
relation,
|
|
147
|
+
rhs
|
|
148
|
+
}));
|
|
149
|
+
const most = /* @__PURE__ */ new Map([[days, ONE]]);
|
|
150
|
+
const least = /* @__PURE__ */ new Map();
|
|
151
|
+
for (const shares of shareCol) for (const [resource, index] of shares) addTo(least, index, div(ONE, exact(stock.get(resource) ?? 1)));
|
|
152
|
+
const fewest = /* @__PURE__ */ new Map();
|
|
153
|
+
for (const [converter, indexes] of runCols) {
|
|
154
|
+
const weight = converter.perDay !== void 0 && converter.perDay > 0 ? div(ONE, exact(converter.perDay)) : ONE;
|
|
155
|
+
for (const index of indexes) addTo(fewest, index, weight);
|
|
156
|
+
}
|
|
157
|
+
const objectives = [
|
|
158
|
+
{
|
|
159
|
+
sense: "max",
|
|
160
|
+
coefficients: dense(most, count)
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
sense: "min",
|
|
164
|
+
coefficients: dense(least, count)
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
sense: "min",
|
|
168
|
+
coefficients: dense(fewest, count)
|
|
169
|
+
}
|
|
170
|
+
];
|
|
171
|
+
const solved = solveLexicographic(constraints, objectives);
|
|
172
|
+
if (solved.status !== "optimal") return endless;
|
|
173
|
+
const x = solved.x;
|
|
174
|
+
const at = (index) => x[index] ?? ZERO;
|
|
175
|
+
const takes = {};
|
|
176
|
+
plan.forEach((draw, i) => {
|
|
177
|
+
const record = {};
|
|
178
|
+
for (const [resource, index] of shareCol[i] ?? []) if (sign(at(index)) > 0) record[resource] = toNumber(at(index));
|
|
179
|
+
takes[draw.need] = record;
|
|
180
|
+
});
|
|
181
|
+
const runs = {};
|
|
182
|
+
const ran = (converter, value) => {
|
|
183
|
+
if (sign(value) > 0) runs[converter.id] = toNumber(value);
|
|
184
|
+
};
|
|
185
|
+
cooks.forEach((cook, c) => ran(cook, at(cookCol[c])));
|
|
186
|
+
makers.forEach((maker, m) => ran(maker, makerCol.reduce((sum, row) => add(sum, at(row[m])), ZERO)));
|
|
187
|
+
const withStore = new Set(layer.stores.map((store) => store.resource));
|
|
188
|
+
const closure = inputClosure(direct.map((use) => use.resource), cooks, makers);
|
|
189
|
+
const reach = /* @__PURE__ */ new Set([...cooks, ...makers.filter((maker) => closure.has(giftResource(maker) ?? ""))]);
|
|
190
|
+
const binding = [...closure].filter((resource) => withStore.has(resource)).filter((resource) => {
|
|
191
|
+
const taken = shareCol.reduce((sum, shares) => {
|
|
192
|
+
const index = shares.get(resource);
|
|
193
|
+
return index === void 0 ? sum : add(sum, at(index));
|
|
194
|
+
}, ZERO);
|
|
195
|
+
return equals(taken, exact(stock.get(resource) ?? 0));
|
|
196
|
+
}).sort();
|
|
197
|
+
const best = solved.values[0] ?? ZERO;
|
|
198
|
+
const used = (row) => [...row].reduce((sum, [index, value]) => add(sum, mul(value, at(index))), ZERO);
|
|
199
|
+
const capped = sign(best) > 0 ? [...[...runCols.keys()].filter((converter) => converter.perDay !== void 0 && !(converter.perDay > 0)), ...[...limits.values()].filter(({ row }) => sign(used(row)) === 0).flatMap(({ converters }) => converters)].filter((converter) => reach.has(converter)).map((converter) => converter.id).sort() : [];
|
|
200
|
+
return {
|
|
201
|
+
...base,
|
|
202
|
+
days: toNumber(best),
|
|
203
|
+
takes,
|
|
204
|
+
binding,
|
|
205
|
+
runs,
|
|
206
|
+
capped
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* This function gives each resource that the plan can use: the direct resources and the inputs of the converters of the plan.
|
|
211
|
+
* It also gives the inputs of their makers, through each chain.
|
|
212
|
+
*/
|
|
213
|
+
function inputClosure(direct, cooks, makers) {
|
|
214
|
+
const out = /* @__PURE__ */ new Set();
|
|
215
|
+
const queue = [...direct, ...cooks.flatMap((cook) => [...inputsOf(cook).keys()])];
|
|
216
|
+
for (let resource = queue.shift(); resource !== void 0; resource = queue.shift()) {
|
|
217
|
+
if (out.has(resource)) continue;
|
|
218
|
+
out.add(resource);
|
|
219
|
+
for (const maker of makers) if (giftResource(maker) === resource) queue.push(...inputsOf(maker).keys());
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
5
223
|
// src/stops.ts
|
|
6
224
|
/**
|
|
7
225
|
* This function gives the minimal sets of cuts that stop a supply, from the paths of the needs of the stack (`stopSets` of the core).
|
|
@@ -788,6 +1006,6 @@ const SAMPLE_ENDURANCE = {
|
|
|
788
1006
|
asOf: "2025-12-01"
|
|
789
1007
|
}]
|
|
790
1008
|
};
|
|
791
|
-
export { ENDURANCE_CODES, ENDURANCE_SCHEMA, INDEPENDENT_MAX_WORK, INDEPENDENT_TOLERANCE, MINIMAL_STOP_CUTS, SAMPLE_ENDURANCE, SAMPLE_ENDURANCE_STACK, caseDraws, caseShortfall, caseStopped, checkEndurance, curveFault, drawDemand, enduranceOf, eventStop, giftNeed, giftResource, hasEnduranceError, independentStop, joinEndurance, longestStop, minimalStops, stopCurves, storeAmounts, survivalAt, survivalQuantile, tailDays, upperStop, yearChance, yearRate, yearShortfall };
|
|
1009
|
+
export { ENDURANCE_CODES, ENDURANCE_SCHEMA, INDEPENDENT_MAX_WORK, INDEPENDENT_TOLERANCE, MINIMAL_STOP_CUTS, SAMPLE_ENDURANCE, SAMPLE_ENDURANCE_STACK, caseDraws, caseShortfall, caseStopped, checkEndurance, curveFault, drawDemand, enduranceOf, eventStop, giftNeed, giftResource, hasEnduranceError, independentStop, joinEndurance, jointDays, longestStop, minimalStops, stopCurves, storeAmounts, survivalAt, survivalQuantile, tailDays, upperStop, yearChance, yearRate, yearShortfall };
|
|
792
1010
|
|
|
793
1011
|
//# sourceMappingURL=index.js.map
|