@mark1russell7/stack-endurance 0.8.6 → 0.8.8
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 +31 -2
- package/build/add-on.js +1 -1
- package/build/index.d.ts +31 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +284 -124
- package/build/index.js.map +1 -1
- package/build/{join-DdVFmRh6.js → join-BJJdchyI.js} +180 -99
- package/build/join-BJJdchyI.js.map +1 -0
- package/package.json +2 -2
- package/build/join-DdVFmRh6.js.map +0 -1
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ Each function is a plain function with no clock, no network and no file. The uni
|
|
|
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
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
|
|
32
|
+
- `jointDays` gives the best share for a set of draws. `bestEndurance` gives the best share for all the draws, level by level. Refer to the sections on the joint days and on the best endurance.
|
|
33
33
|
- A store gives nothing on its due date and after it. A store or a converter whose part stops gives nothing.
|
|
34
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.
|
|
35
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.
|
|
@@ -107,11 +107,40 @@ The take order gives the drink 6.667 days. The drink keeps back all the water fo
|
|
|
107
107
|
|
|
108
108
|
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.
|
|
109
109
|
|
|
110
|
+
## The best endurance
|
|
111
|
+
|
|
112
|
+
`bestEndurance(layer, caseCuts, options)` gives the days of each draw with the best share of the stores and of the devices. Its result has the form of `enduranceOf`, so `caseShortfall` takes it.
|
|
113
|
+
|
|
114
|
+
- The share is lexicographic by level. The draws of the first level get their best days. Then the draws of the next level get their best days, and so on.
|
|
115
|
+
- In one level, the share is the best for the draw with the fewest days, then for the next draw, and so on. This is the leximin share.
|
|
116
|
+
- Thus a draw cannot take a store that another draw of its level needs more. A higher draw does not take a store that a lower draw needs, when the higher draw has a second way.
|
|
117
|
+
- Each step is a linear program with exact rationals. It takes the largest common floor of the open draws.
|
|
118
|
+
- A draw whose floor row has a dual value that is not 0 cannot get more days. The step fixes its days. Each step fixes one draw or more.
|
|
119
|
+
- A fixed draw keeps its days. Its share of the stocks can change in a later step.
|
|
120
|
+
- A fixed draw keeps the device-days of each day that it uses, for all the days of the later draws. This is the safe side, as in `enduranceOf`.
|
|
121
|
+
- A draw with no end gets `Infinity` days. It takes no stock.
|
|
122
|
+
- The stores, the uses, the converters and the devices follow the rules of `jointDays`.
|
|
123
|
+
- The fewest days of the first level are the joint days of the draws of that level.
|
|
124
|
+
|
|
125
|
+
An example: the draw `a` drinks water or juice, and the draw `b` drinks only water. Each draw takes 1 L each day, and the stores have 10 L of each. The two draws are at one level.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { bestEndurance, enduranceOf } from "@mark1russell7/stack-endurance";
|
|
129
|
+
|
|
130
|
+
const ordered = enduranceOf(layer, dry, { at: "2026-12-01" });
|
|
131
|
+
// ordered.needs.a.days is 20, and ordered.needs.b.days is 0.
|
|
132
|
+
const best = bestEndurance(layer, dry, { at: "2026-12-01" });
|
|
133
|
+
// best.needs.a is { days: 10, binding: ["juice", "water"], takes: { juice: 10 } }.
|
|
134
|
+
// best.needs.b is { days: 10, binding: ["water"], takes: { water: 10 } }.
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The take order gives the water to `a` first, so `b` gets 0 days. The best share gives the juice to `a` and the water to `b`.
|
|
138
|
+
|
|
110
139
|
## The entries
|
|
111
140
|
|
|
112
141
|
| Entry | What it has |
|
|
113
142
|
| --- | --- |
|
|
114
|
-
| `@mark1russell7/stack-endurance` | The records of a layer, the endurance, joint days and shortfall queries, the gate, the join and a sample. |
|
|
143
|
+
| `@mark1russell7/stack-endurance` | The records of a layer, the endurance, joint days, best endurance and shortfall queries, the gate, the join and a sample. |
|
|
115
144
|
| `@mark1russell7/stack-endurance/schema` | `enduranceSchema`, the zod schema of a layer, with `parseEndurance`. |
|
|
116
145
|
| `@mark1russell7/stack-endurance/add-on` | `enduranceAddOn`, the add-on in the form of the layer contract of the core. |
|
|
117
146
|
|
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-BJJdchyI.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
|
@@ -122,6 +122,37 @@ export interface JointDays {
|
|
|
122
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
123
|
*/
|
|
124
124
|
export declare function jointDays(layer: EnduranceLayer, caseCuts: CaseCuts, options: CaseOptions, needs: readonly string[]): JointDays;
|
|
125
|
+
/**
|
|
126
|
+
* This function gives the days that each draw holds when a case stops its supply. The stores and the devices have their best share.
|
|
127
|
+
* The result has the form of `enduranceOf`, so `caseShortfall` takes it.
|
|
128
|
+
*
|
|
129
|
+
* The share is lexicographic. The draws of the first level get their best days, then the draws of the next level get theirs, and so on.
|
|
130
|
+
* In one level, the share is the best for the draw with the fewest days, then for the next draw, and so on (the leximin share).
|
|
131
|
+
* Thus a draw of a level cannot take a store that another draw of the level needs more.
|
|
132
|
+
*
|
|
133
|
+
* - The function solves a sequence of linear programs over the totals of the spans. Each program has the days of the open draws
|
|
134
|
+
* and their common floor. For each draw, it has the take of each direct use, the runs of each converter and the share of each stock.
|
|
135
|
+
* - Each step takes the largest floor. A draw whose floor row has a dual value that is not 0 cannot get more days. The step fixes its days.
|
|
136
|
+
* The dual values of the floor rows add to 1, so each step fixes one draw or more.
|
|
137
|
+
* - A fixed draw keeps its days in each later step. Its share of the stocks can change, so a later draw can get a store that it does not need.
|
|
138
|
+
* - The stores, the direct uses, the converters and the devices follow the rules of `jointDays`. A store counts with all of its amount when its due date is after `at`.
|
|
139
|
+
* - A fixed draw keeps the device-days of each day that it uses at its step, for all the days of the later draws. This is the safe side, as in `enduranceOf`.
|
|
140
|
+
* The exact share of a device between two spans is not a linear program: the device-days of a day are the runs over the span.
|
|
141
|
+
* - A step with no largest floor gives the open draws no end. Each of them takes no stock, and keeps the fewest device-days of each day.
|
|
142
|
+
* - At the end, a program takes the smallest sum of the shares of the stocks, each over its stock, then the fewest device-days.
|
|
143
|
+
* These are the takes of the result. A resource in `binding` has no stock left at this point.
|
|
144
|
+
* - A draw in `capped` used a device with no device-days left at the step that fixed it.
|
|
145
|
+
*
|
|
146
|
+
* The programs use exact rationals, so the result has no error of the method. Only the change back to a number rounds.
|
|
147
|
+
* The fewest days of the first level are the joint days (`jointDays`) of the draws of that level.
|
|
148
|
+
*
|
|
149
|
+
* @param layer - The endurance layer
|
|
150
|
+
* @param caseCuts - The case
|
|
151
|
+
* @param options - The date, and the stack or the parts that are also down
|
|
152
|
+
* @returns The endurance of each draw whose supply stops
|
|
153
|
+
* @throws {RangeError} A date is not `YYYY-MM-DD`.
|
|
154
|
+
*/
|
|
155
|
+
export declare function bestEndurance(layer: EnduranceLayer, caseCuts: CaseCuts, options: CaseOptions): Endurance;
|
|
125
156
|
/**
|
|
126
157
|
* This function gives the expected days of a cut after a time, up to a horizon: `E[max(0, min(D, to) - from)]`.
|
|
127
158
|
* It is the area under the curve from `from` to `to`. The curve is linear between its points, so the area is exact.
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":["CutBound"],"sources":["../src/endurance.ts","../src/joint.ts","../src/shortfall.ts","../src/check.ts","../src/join.ts","../src/sample.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["CutBound"],"sources":["../src/endurance.ts","../src/joint.ts","../src/best.ts","../src/shortfall.ts","../src/check.ts","../src/join.ts","../src/sample.ts"],"mappings":";;;iBAUiB;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAuLxB,YAAY,OAAO,gBAAgB,UAAU,UAAU,SAAS,cAAc;;iBC3P7E;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBCNtG,cAAc,OAAO,gBAAgB,UAAU,UAAU,SAAS,cAAc;;;;;;wBClDhF,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;;;;;;WAMA,OAAOA;;;;;;;;;;;;;;;;wBAmEF,WAAW,UAAU,UAAU,MAAM,MAAM,UAAS,mBAAwB;;iBAM3E;;;;;WAKN,QAAQ,SAAS;;WAEjB,aAAa,SAAS;;WAEtB,OAAO,SAAS;;;;;WAKhB;;WAEA;;WAEA;;;;;;;;;WASA,OAAOA;;;;;;;;;;;;wBAiBF,cAAc,OAAO,gBAAgB,UAAU,UAAU,WAAW,WAAW,UAAS,mBAAwB;;iBA8B/G,sBAAsB;WAC5B;;WAEA;aAA2B;aAAuB;aAAuB,WAAW;aAAoB,WAAW;;;;;;;;;;;;;;;;;wBAiB9G,cAAc,OAAO,gBAAgB,eAAe,SAAS,cAAc,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;qBCrjBjG;;YAmCD,wBAAwB;;iBAGnB,uBAAuB;WAC7B,MAAM;;;;;;;;;;;wBAcD,eAAe,OAAO,gBAAgB,QAAQ,QAAQ;;wBAiJtD,kBAAkB,iBAAiB;;;;;;;;;;;;;;;;;wBC7MnC,cAAc,iBAAiB,WAAW,oBAAoB,WAAW;;qBCf5E,wBAAwB;;;;;;;;;qBAkBxB,kBAAkB"}
|
package/build/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { t as ENDURANCE_SCHEMA } from "./layer-a3Fr70Uh.js";
|
|
2
|
-
import { _ as
|
|
2
|
+
import { S as exact, _ as fewestRow, a as hasEnduranceError, b as addTo, c as drawDemand, d as giftResource, f as storeAmounts, g as drawBlock, h as deviceUse, i as curveFault, l as enduranceOf, m as caseParts, n as ENDURANCE_CODES, o as caseDraws, p as Program, r as checkEndurance, s as caseStopped, t as joinEndurance, u as giftNeed, v as reachOf, x as dense, y as valueAt } from "./join-BJJdchyI.js";
|
|
3
3
|
import { DEFAULT_MAX_SETS, STACK_SCHEMA, neededParts, stopSets, survivalAt, weakestBound } from "@mark1russell7/system-stack";
|
|
4
|
+
import { ONE, ZERO, add, div, equals, mul, neg, sign, sub, toNumber } from "@mark1russell7/rational";
|
|
4
5
|
import { solveLexicographic } from "@mark1russell7/linear";
|
|
5
|
-
import { ONE, ZERO, add, div, equals, mul, neg, sign, toNumber } from "@mark1russell7/rational";
|
|
6
6
|
import { pathSetReliability, stronglyConnectedComponents } from "@mark1russell7/collections/graph";
|
|
7
7
|
// src/joint.ts
|
|
8
8
|
/**
|
|
@@ -57,146 +57,70 @@ function jointDays(layer, caseCuts, options, needs) {
|
|
|
57
57
|
capped: []
|
|
58
58
|
};
|
|
59
59
|
if (plan.length === 0) return endless;
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
});
|
|
60
|
+
const parts = caseParts(layer, stoppedParts, stock, planned);
|
|
61
|
+
const program = new Program();
|
|
62
|
+
const days = program.next();
|
|
63
|
+
const blocks = plan.map((draw) => {
|
|
64
|
+
const block = drawBlock(program, parts, draw);
|
|
65
|
+
const need = new Map(block.gifts);
|
|
66
|
+
addTo(need, days, neg(exact(drawDemand(draw, caseCuts))));
|
|
67
|
+
program.push(need, ">=", ZERO);
|
|
68
|
+
return block;
|
|
108
69
|
});
|
|
109
|
-
const runCols =
|
|
110
|
-
const limits
|
|
111
|
-
for (const [
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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)));
|
|
70
|
+
const runCols = blocks.flatMap((block) => [...block.runs]);
|
|
71
|
+
const { limits, stopped } = deviceUse(runCols);
|
|
72
|
+
for (const index of stopped) program.push(/* @__PURE__ */ new Map([[index, ONE]]), "<=", ZERO);
|
|
73
|
+
for (const limit of limits.values()) {
|
|
74
|
+
const row = new Map(limit.row);
|
|
75
|
+
addTo(row, days, neg(exact(limit.devices)));
|
|
76
|
+
program.push(row, "<=", ZERO);
|
|
129
77
|
}
|
|
130
|
-
for (const
|
|
131
|
-
row,
|
|
132
|
-
relation: "<=",
|
|
133
|
-
rhs: ZERO
|
|
134
|
-
});
|
|
135
|
-
for (const resource of stocked) {
|
|
78
|
+
for (const resource of parts.stocked) {
|
|
136
79
|
const row = /* @__PURE__ */ new Map();
|
|
137
|
-
for (const
|
|
138
|
-
|
|
139
|
-
row,
|
|
140
|
-
relation: "<=",
|
|
141
|
-
rhs: exact(stock.get(resource) ?? 0)
|
|
142
|
-
});
|
|
80
|
+
for (const block of blocks) addTo(row, block.shares.get(resource), ONE);
|
|
81
|
+
program.push(row, "<=", exact(stock.get(resource) ?? 0));
|
|
143
82
|
}
|
|
144
|
-
const constraints = rows.map(({ row, relation, rhs }) => ({
|
|
145
|
-
coefficients: dense(row, count),
|
|
146
|
-
relation,
|
|
147
|
-
rhs
|
|
148
|
-
}));
|
|
149
83
|
const most = /* @__PURE__ */ new Map([[days, ONE]]);
|
|
150
84
|
const least = /* @__PURE__ */ new Map();
|
|
151
|
-
for (const
|
|
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
|
-
}
|
|
85
|
+
for (const block of blocks) for (const [resource, index] of block.shares) addTo(least, index, div(ONE, exact(stock.get(resource) ?? 1)));
|
|
157
86
|
const objectives = [
|
|
158
87
|
{
|
|
159
88
|
sense: "max",
|
|
160
|
-
coefficients: dense(most, count)
|
|
89
|
+
coefficients: dense(most, program.count)
|
|
161
90
|
},
|
|
162
91
|
{
|
|
163
92
|
sense: "min",
|
|
164
|
-
coefficients: dense(least, count)
|
|
93
|
+
coefficients: dense(least, program.count)
|
|
165
94
|
},
|
|
166
95
|
{
|
|
167
96
|
sense: "min",
|
|
168
|
-
coefficients: dense(
|
|
97
|
+
coefficients: dense(fewestRow(runCols), program.count)
|
|
169
98
|
}
|
|
170
99
|
];
|
|
171
|
-
const solved = solveLexicographic(constraints, objectives);
|
|
100
|
+
const solved = solveLexicographic(program.constraints(), objectives);
|
|
172
101
|
if (solved.status !== "optimal") return endless;
|
|
173
102
|
const x = solved.x;
|
|
174
|
-
const at = (index) => x[index] ?? ZERO;
|
|
175
103
|
const takes = {};
|
|
176
|
-
|
|
104
|
+
for (const block of blocks) {
|
|
177
105
|
const record = {};
|
|
178
|
-
for (const [resource, index] of
|
|
179
|
-
takes[draw.need] = record;
|
|
180
|
-
}
|
|
106
|
+
for (const [resource, index] of block.shares) if (sign(x[index] ?? ZERO) > 0) record[resource] = toNumber(x[index] ?? ZERO);
|
|
107
|
+
takes[block.draw.need] = record;
|
|
108
|
+
}
|
|
109
|
+
const total = /* @__PURE__ */ new Map();
|
|
110
|
+
for (const [converter, index] of runCols) total.set(converter, add(total.get(converter) ?? ZERO, x[index] ?? ZERO));
|
|
181
111
|
const runs = {};
|
|
182
|
-
const
|
|
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)));
|
|
112
|
+
for (const [converter, value] of total) if (sign(value) > 0) runs[converter.id] = toNumber(value);
|
|
187
113
|
const withStore = new Set(layer.stores.map((store) => store.resource));
|
|
188
|
-
const closure =
|
|
189
|
-
const reach = /* @__PURE__ */ new Set([...cooks, ...makers.filter((maker) => closure.has(giftResource(maker) ?? ""))]);
|
|
114
|
+
const { closure, reach } = reachOf(parts, planned);
|
|
190
115
|
const binding = [...closure].filter((resource) => withStore.has(resource)).filter((resource) => {
|
|
191
|
-
const taken =
|
|
192
|
-
const index = shares.get(resource);
|
|
193
|
-
return index === void 0 ? sum : add(sum,
|
|
116
|
+
const taken = blocks.reduce((sum, block) => {
|
|
117
|
+
const index = block.shares.get(resource);
|
|
118
|
+
return index === void 0 ? sum : add(sum, x[index] ?? ZERO);
|
|
194
119
|
}, ZERO);
|
|
195
120
|
return equals(taken, exact(stock.get(resource) ?? 0));
|
|
196
121
|
}).sort();
|
|
197
122
|
const best = solved.values[0] ?? ZERO;
|
|
198
|
-
const
|
|
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() : [];
|
|
123
|
+
const capped = sign(best) > 0 ? [...[...total.keys()].filter((converter) => converter.perDay !== void 0 && !(converter.perDay > 0)), ...[...limits.values()].filter((limit) => equals(valueAt(limit.row, x), mul(exact(limit.devices), best))).flatMap((limit) => limit.converters)].filter((converter) => reach.has(converter)).map((converter) => converter.id).sort() : [];
|
|
200
124
|
return {
|
|
201
125
|
...base,
|
|
202
126
|
days: toNumber(best),
|
|
@@ -206,20 +130,256 @@ function jointDays(layer, caseCuts, options, needs) {
|
|
|
206
130
|
capped
|
|
207
131
|
};
|
|
208
132
|
}
|
|
133
|
+
// src/best.ts
|
|
209
134
|
/**
|
|
210
|
-
* This function gives
|
|
211
|
-
*
|
|
135
|
+
* This function gives the days that each draw holds when a case stops its supply. The stores and the devices have their best share.
|
|
136
|
+
* The result has the form of `enduranceOf`, so `caseShortfall` takes it.
|
|
137
|
+
*
|
|
138
|
+
* The share is lexicographic. The draws of the first level get their best days, then the draws of the next level get theirs, and so on.
|
|
139
|
+
* In one level, the share is the best for the draw with the fewest days, then for the next draw, and so on (the leximin share).
|
|
140
|
+
* Thus a draw of a level cannot take a store that another draw of the level needs more.
|
|
141
|
+
*
|
|
142
|
+
* - The function solves a sequence of linear programs over the totals of the spans. Each program has the days of the open draws
|
|
143
|
+
* and their common floor. For each draw, it has the take of each direct use, the runs of each converter and the share of each stock.
|
|
144
|
+
* - Each step takes the largest floor. A draw whose floor row has a dual value that is not 0 cannot get more days. The step fixes its days.
|
|
145
|
+
* The dual values of the floor rows add to 1, so each step fixes one draw or more.
|
|
146
|
+
* - A fixed draw keeps its days in each later step. Its share of the stocks can change, so a later draw can get a store that it does not need.
|
|
147
|
+
* - The stores, the direct uses, the converters and the devices follow the rules of `jointDays`. A store counts with all of its amount when its due date is after `at`.
|
|
148
|
+
* - A fixed draw keeps the device-days of each day that it uses at its step, for all the days of the later draws. This is the safe side, as in `enduranceOf`.
|
|
149
|
+
* The exact share of a device between two spans is not a linear program: the device-days of a day are the runs over the span.
|
|
150
|
+
* - A step with no largest floor gives the open draws no end. Each of them takes no stock, and keeps the fewest device-days of each day.
|
|
151
|
+
* - At the end, a program takes the smallest sum of the shares of the stocks, each over its stock, then the fewest device-days.
|
|
152
|
+
* These are the takes of the result. A resource in `binding` has no stock left at this point.
|
|
153
|
+
* - A draw in `capped` used a device with no device-days left at the step that fixed it.
|
|
154
|
+
*
|
|
155
|
+
* The programs use exact rationals, so the result has no error of the method. Only the change back to a number rounds.
|
|
156
|
+
* The fewest days of the first level are the joint days (`jointDays`) of the draws of that level.
|
|
157
|
+
*
|
|
158
|
+
* @param layer - The endurance layer
|
|
159
|
+
* @param caseCuts - The case
|
|
160
|
+
* @param options - The date, and the stack or the parts that are also down
|
|
161
|
+
* @returns The endurance of each draw whose supply stops
|
|
162
|
+
* @throws {RangeError} A date is not `YYYY-MM-DD`.
|
|
212
163
|
*/
|
|
213
|
-
function
|
|
214
|
-
const
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
164
|
+
function bestEndurance(layer, caseCuts, options) {
|
|
165
|
+
const stoppedParts = caseStopped(caseCuts, options);
|
|
166
|
+
const stock = storeAmounts(layer, options.at, stoppedParts);
|
|
167
|
+
const draws = caseDraws(layer, caseCuts, stoppedParts);
|
|
168
|
+
const parts = caseParts(layer, stoppedParts, stock, new Set(draws.map((draw) => draw.need)));
|
|
169
|
+
const devices = new Map([...deviceUse([...parts.cooks, ...parts.makers].map((converter) => [converter, 0])).limits].map(([key, limit]) => [key, limit.devices]));
|
|
170
|
+
const fixed = /* @__PURE__ */ new Map();
|
|
171
|
+
for (const level of levelGroups(layer, draws)) {
|
|
172
|
+
let open = level;
|
|
173
|
+
while (open.length > 0) {
|
|
174
|
+
const step = buildStep(parts, caseCuts, draws, fixed, open, devices, "span");
|
|
175
|
+
const solved = solveLexicographic(step.program.constraints(), [{
|
|
176
|
+
sense: "max",
|
|
177
|
+
coefficients: dense(/* @__PURE__ */ new Map([[step.floor, ONE]]), step.program.count)
|
|
178
|
+
}, {
|
|
179
|
+
sense: "min",
|
|
180
|
+
coefficients: dense(fewestRow(step.openRuns), step.program.count)
|
|
181
|
+
}]);
|
|
182
|
+
if (solved.status !== "optimal") {
|
|
183
|
+
if (solved.status === "infeasible") throw new Error("The step of the best endurance has no point. The point of the last step is a point of it.");
|
|
184
|
+
fixEndless(parts, caseCuts, draws, fixed, open, devices);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
const floor = solved.values[0] ?? ZERO;
|
|
188
|
+
const first = solved.stages[0]?.result;
|
|
189
|
+
const dual = first?.status === "optimal" ? first.dual : [];
|
|
190
|
+
let done = open.filter((draw) => sign(dual[step.floors.get(draw.need)] ?? ZERO) !== 0);
|
|
191
|
+
if (done.length === 0) done = open;
|
|
192
|
+
const full = new Set([...step.shared].filter(([, limit]) => equals(valueAt(limit.row, solved.x), mul(limit.room, floor))).map(([key]) => key));
|
|
193
|
+
for (const draw of done) {
|
|
194
|
+
const block = step.blocks.find((item) => item.draw.need === draw.need);
|
|
195
|
+
const keep = /* @__PURE__ */ new Map();
|
|
196
|
+
for (const [key, limit] of deviceUse(block.runs).limits) keep.set(key, sign(floor) > 0 ? div(valueAt(limit.row, solved.x), floor) : ZERO);
|
|
197
|
+
const { reach } = reachOf(parts, /* @__PURE__ */ new Set([draw.need]));
|
|
198
|
+
const capped = sign(floor) > 0 ? [...reach].filter((converter) => converter.perDay !== void 0 && (!(converter.perDay > 0) || [...full].some((key) => step.shared.get(key)?.converters.includes(converter)))).map((converter) => converter.id).sort() : [];
|
|
199
|
+
fixed.set(draw.need, {
|
|
200
|
+
span: floor,
|
|
201
|
+
endless: false,
|
|
202
|
+
keep,
|
|
203
|
+
capped
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const fixedNow = new Set(done.map((draw) => draw.need));
|
|
207
|
+
open = open.filter((draw) => !fixedNow.has(draw.need));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const needs = {};
|
|
211
|
+
if (draws.length === 0) return {
|
|
212
|
+
case: caseCuts.case,
|
|
213
|
+
at: options.at,
|
|
214
|
+
needs
|
|
215
|
+
};
|
|
216
|
+
const last = buildStep(parts, caseCuts, draws, fixed, [], devices, "span");
|
|
217
|
+
const least = /* @__PURE__ */ new Map();
|
|
218
|
+
for (const block of last.blocks) for (const [resource, index] of block.shares) addTo(least, index, div(ONE, exact(stock.get(resource) ?? 1)));
|
|
219
|
+
const objectives = [{
|
|
220
|
+
sense: "min",
|
|
221
|
+
coefficients: dense(least, last.program.count)
|
|
222
|
+
}, {
|
|
223
|
+
sense: "min",
|
|
224
|
+
coefficients: dense(fewestRow(last.blocks.flatMap((block) => [...block.runs])), last.program.count)
|
|
225
|
+
}];
|
|
226
|
+
const solved = solveLexicographic(last.program.constraints(), objectives);
|
|
227
|
+
if (solved.status !== "optimal") throw new Error("The last program of the best endurance has no optimum. The point of the last step is a point of it.");
|
|
228
|
+
const withStore = new Set(layer.stores.map((store) => store.resource));
|
|
229
|
+
for (const block of last.blocks) {
|
|
230
|
+
const need = block.draw.need;
|
|
231
|
+
const fix = fixed.get(need);
|
|
232
|
+
if (fix.endless) {
|
|
233
|
+
needs[need] = {
|
|
234
|
+
days: 1e400,
|
|
235
|
+
binding: [],
|
|
236
|
+
takes: {}
|
|
237
|
+
};
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const takes = {};
|
|
241
|
+
for (const [resource, index] of block.shares) if (sign(solved.x[index] ?? ZERO) > 0) takes[resource] = toNumber(solved.x[index] ?? ZERO);
|
|
242
|
+
const { closure } = reachOf(parts, /* @__PURE__ */ new Set([need]));
|
|
243
|
+
const binding = [...closure].filter((resource) => withStore.has(resource)).filter((resource) => {
|
|
244
|
+
const taken = last.blocks.reduce((sum, item) => {
|
|
245
|
+
const index = item.shares.get(resource);
|
|
246
|
+
return index === void 0 ? sum : add(sum, solved.x[index] ?? ZERO);
|
|
247
|
+
}, ZERO);
|
|
248
|
+
return equals(taken, exact(stock.get(resource) ?? 0));
|
|
249
|
+
}).sort();
|
|
250
|
+
const days = toNumber(fix.span);
|
|
251
|
+
needs[need] = fix.capped.length === 0 ? {
|
|
252
|
+
days,
|
|
253
|
+
binding,
|
|
254
|
+
takes
|
|
255
|
+
} : {
|
|
256
|
+
days,
|
|
257
|
+
binding,
|
|
258
|
+
takes,
|
|
259
|
+
capped: fix.capped
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
case: caseCuts.case,
|
|
264
|
+
at: options.at,
|
|
265
|
+
needs
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/** This function gives the draws of each level, in the order of `levels`. The draws of a level that is not in `levels` come last, together. */
|
|
269
|
+
function levelGroups(layer, draws) {
|
|
270
|
+
const rank = (draw) => {
|
|
271
|
+
const index = layer.levels.indexOf(draw.level);
|
|
272
|
+
return index < 0 ? layer.levels.length : index;
|
|
273
|
+
};
|
|
274
|
+
const out = [];
|
|
275
|
+
let previous = -1;
|
|
276
|
+
for (const draw of draws) {
|
|
277
|
+
if (rank(draw) !== previous) out.push([]);
|
|
278
|
+
previous = rank(draw);
|
|
279
|
+
out[out.length - 1]?.push(draw);
|
|
220
280
|
}
|
|
221
281
|
return out;
|
|
222
282
|
}
|
|
283
|
+
/**
|
|
284
|
+
* This function gives the program of one step. The fixed draws keep their spans and their device-days. The open draws have a common floor.
|
|
285
|
+
*
|
|
286
|
+
* - With `"span"`, each open draw has a column of its days, not less than the floor. Its runs share the device-days that the fixed draws leave, for the floor.
|
|
287
|
+
* The device-days of each day are the runs over the span. A span not less than the floor uses fewer of them, so this is the safe side.
|
|
288
|
+
* - With `"day"`, each open draw has a span of one day and takes no stock. Its runs share the device-days of one day.
|
|
289
|
+
*/
|
|
290
|
+
function buildStep(parts, caseCuts, draws, fixed, open, devices, mode) {
|
|
291
|
+
const program = new Program();
|
|
292
|
+
const floor = program.next();
|
|
293
|
+
const opened = new Set(open.map((draw) => draw.need));
|
|
294
|
+
const blocks = [];
|
|
295
|
+
const floors = /* @__PURE__ */ new Map();
|
|
296
|
+
const openRuns = [];
|
|
297
|
+
const kept = /* @__PURE__ */ new Map();
|
|
298
|
+
for (const draw of draws) {
|
|
299
|
+
const fix = fixed.get(draw.need);
|
|
300
|
+
if (fix === void 0 && !opened.has(draw.need)) continue;
|
|
301
|
+
const endless = fix?.endless ?? mode === "day";
|
|
302
|
+
const block = drawBlock(program, parts, draw, !endless);
|
|
303
|
+
blocks.push(block);
|
|
304
|
+
const demand = exact(drawDemand(draw, caseCuts));
|
|
305
|
+
const need = new Map(block.gifts);
|
|
306
|
+
if (fix !== void 0) {
|
|
307
|
+
program.push(need, ">=", mul(demand, fix.span));
|
|
308
|
+
const { limits, stopped } = deviceUse(block.runs);
|
|
309
|
+
for (const index of stopped) program.push(/* @__PURE__ */ new Map([[index, ONE]]), "<=", ZERO);
|
|
310
|
+
for (const [key, limit] of limits) program.push(limit.row, "<=", mul(fix.keep.get(key) ?? ZERO, fix.span));
|
|
311
|
+
for (const [key, value] of fix.keep) kept.set(key, add(kept.get(key) ?? ZERO, value));
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
openRuns.push(...block.runs);
|
|
315
|
+
if (mode === "day") {
|
|
316
|
+
program.push(need, ">=", demand);
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const days = program.next();
|
|
320
|
+
addTo(need, days, neg(demand));
|
|
321
|
+
program.push(need, ">=", ZERO);
|
|
322
|
+
floors.set(draw.need, program.push(/* @__PURE__ */ new Map([[days, ONE], [floor, neg(ONE)]]), ">=", ZERO));
|
|
323
|
+
}
|
|
324
|
+
const shared = /* @__PURE__ */ new Map();
|
|
325
|
+
const { limits, stopped } = deviceUse(openRuns);
|
|
326
|
+
for (const index of stopped) program.push(/* @__PURE__ */ new Map([[index, ONE]]), "<=", ZERO);
|
|
327
|
+
for (const [key, limit] of limits) {
|
|
328
|
+
const left = sub(exact(devices.get(key) ?? limit.devices), kept.get(key) ?? ZERO);
|
|
329
|
+
const room = sign(left) > 0 ? left : ZERO;
|
|
330
|
+
shared.set(key, {
|
|
331
|
+
row: limit.row,
|
|
332
|
+
room,
|
|
333
|
+
converters: limit.converters
|
|
334
|
+
});
|
|
335
|
+
if (mode === "day") {
|
|
336
|
+
program.push(limit.row, "<=", room);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const row = new Map(limit.row);
|
|
340
|
+
addTo(row, floor, neg(room));
|
|
341
|
+
program.push(row, "<=", ZERO);
|
|
342
|
+
}
|
|
343
|
+
for (const resource of parts.stocked) {
|
|
344
|
+
const row = /* @__PURE__ */ new Map();
|
|
345
|
+
for (const block of blocks) {
|
|
346
|
+
const index = block.shares.get(resource);
|
|
347
|
+
if (index !== void 0) addTo(row, index, ONE);
|
|
348
|
+
}
|
|
349
|
+
if (row.size > 0) program.push(row, "<=", exact(parts.stock.get(resource) ?? 0));
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
program,
|
|
353
|
+
blocks,
|
|
354
|
+
floor,
|
|
355
|
+
floors,
|
|
356
|
+
openRuns,
|
|
357
|
+
shared
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* This function fixes the open draws with no end. Each open draw takes no stock, and keeps the fewest device-days of each day.
|
|
362
|
+
* A step with no largest floor has a direction with no take of a stock, so this program has a point.
|
|
363
|
+
*/
|
|
364
|
+
function fixEndless(parts, caseCuts, draws, fixed, open, devices) {
|
|
365
|
+
const step = buildStep(parts, caseCuts, draws, fixed, open, devices, "day");
|
|
366
|
+
const solved = solveLexicographic(step.program.constraints(), [{
|
|
367
|
+
sense: "min",
|
|
368
|
+
coefficients: dense(fewestRow(step.openRuns), step.program.count)
|
|
369
|
+
}]);
|
|
370
|
+
if (solved.status !== "optimal") throw new Error("The draws with no end of the best endurance have no point of one day.");
|
|
371
|
+
for (const draw of open) {
|
|
372
|
+
const block = step.blocks.find((item) => item.draw.need === draw.need);
|
|
373
|
+
const keep = /* @__PURE__ */ new Map();
|
|
374
|
+
for (const [key, limit] of deviceUse(block.runs).limits) keep.set(key, valueAt(limit.row, solved.x));
|
|
375
|
+
fixed.set(draw.need, {
|
|
376
|
+
span: ONE,
|
|
377
|
+
endless: true,
|
|
378
|
+
keep,
|
|
379
|
+
capped: []
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}
|
|
223
383
|
// src/stops.ts
|
|
224
384
|
/**
|
|
225
385
|
* 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).
|
|
@@ -1003,6 +1163,6 @@ const SAMPLE_ENDURANCE = {
|
|
|
1003
1163
|
asOf: "2025-12-01"
|
|
1004
1164
|
}]
|
|
1005
1165
|
};
|
|
1006
|
-
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 };
|
|
1166
|
+
export { ENDURANCE_CODES, ENDURANCE_SCHEMA, INDEPENDENT_MAX_WORK, INDEPENDENT_TOLERANCE, MINIMAL_STOP_CUTS, SAMPLE_ENDURANCE, SAMPLE_ENDURANCE_STACK, bestEndurance, 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 };
|
|
1007
1167
|
|
|
1008
1168
|
//# sourceMappingURL=index.js.map
|