@mark1russell7/stack-endurance 0.8.16 → 0.8.18
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 +8 -3
- package/build/add-on.d.ts +1 -1
- package/build/add-on.js +1 -1
- package/build/index.d.ts +40 -4
- package/build/index.d.ts.map +1 -1
- package/build/index.js +478 -7
- package/build/index.js.map +1 -1
- package/build/{join-D-KCebAf.js → join-D4KzcbbY.js} +134 -41
- package/build/join-D4KzcbbY.js.map +1 -0
- package/build/{layer-CNAZnao7.d.ts → layer-BCQy77mt.d.ts} +21 -4
- package/build/layer-BCQy77mt.d.ts.map +1 -0
- package/build/layer-a3Fr70Uh.js.map +1 -1
- package/build/schema.d.ts +1 -1
- package/build/schema.js +2 -1
- package/build/schema.js.map +1 -1
- package/package.json +1 -1
- package/build/join-D-KCebAf.js.map +0 -1
- package/build/layer-CNAZnao7.d.ts.map +0 -1
package/build/index.js
CHANGED
|
@@ -1,9 +1,426 @@
|
|
|
1
1
|
import { t as ENDURANCE_SCHEMA } from "./layer-a3Fr70Uh.js";
|
|
2
|
-
import { C as
|
|
2
|
+
import { C as reachOf, D as exact, E as dense, S as partsOf, T as addTo, _ as Program, a as hasEnduranceError, b as drawBlock, c as caseStopped, d as giftNeed, f as giftResource, g as storeAmounts, h as sowDays, i as curveFault, l as drawDemand, m as seasonSupply, 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 caseParts, w as valueAt, x as fewestRow, y as deviceUse } from "./join-D4KzcbbY.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";
|
|
6
6
|
import { pathSetReliability, stronglyConnectedComponents } from "@mark1russell7/collections/graph";
|
|
7
|
+
// src/seasonal.ts
|
|
8
|
+
/**
|
|
9
|
+
* This function gives the parts of the programs of a case with a season. `parts` are the parts of the case with the
|
|
10
|
+
* stock after the sowings (`seasonSupply`).
|
|
11
|
+
*/
|
|
12
|
+
function seasonPlan(supply, parts, caseCuts, draws, devices) {
|
|
13
|
+
const total = new Map(supply.stock);
|
|
14
|
+
const harvests = supply.lots.filter((lot) => parts.resources.has(lot.resource));
|
|
15
|
+
for (const lot of harvests) total.set(lot.resource, (total.get(lot.resource) ?? 0) + lot.amount);
|
|
16
|
+
const all = partsOf(total, parts.direct, parts.cooks, parts.makers);
|
|
17
|
+
const lots = [...all.stocked.filter((resource) => (supply.stock.get(resource) ?? 0) > 0).map((resource) => ({
|
|
18
|
+
resource,
|
|
19
|
+
day: 0,
|
|
20
|
+
spoils: 1e400,
|
|
21
|
+
amount: exact(supply.stock.get(resource) ?? 0)
|
|
22
|
+
})), ...harvests.map((lot) => ({
|
|
23
|
+
resource: lot.resource,
|
|
24
|
+
day: Math.max(0, lot.day),
|
|
25
|
+
spoils: lot.spoils,
|
|
26
|
+
amount: exact(lot.amount)
|
|
27
|
+
}))];
|
|
28
|
+
const inside = (day) => day > 0 && day < supply.horizon;
|
|
29
|
+
return {
|
|
30
|
+
parts: all,
|
|
31
|
+
caseCuts,
|
|
32
|
+
draws,
|
|
33
|
+
devices,
|
|
34
|
+
lots,
|
|
35
|
+
edges: [.../* @__PURE__ */ new Set([
|
|
36
|
+
0,
|
|
37
|
+
supply.horizon,
|
|
38
|
+
...lots.flatMap((lot) => [lot.day, lot.spoils]).filter(inside)
|
|
39
|
+
])].sort((a, b) => a - b)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const edgeOf = (plan, index) => exact(plan.edges[index] ?? 0);
|
|
43
|
+
const lengthOf = (plan, index) => sub(edgeOf(plan, index + 1), edgeOf(plan, index));
|
|
44
|
+
const segmentsOf = (plan) => plan.edges.length - 1;
|
|
45
|
+
/** This function gives the fed days of a fixed draw in a segment. It is all of a segment before the stop, the part to the stop, then 0. */
|
|
46
|
+
function fixedFed(plan, fix, segment) {
|
|
47
|
+
if (segment < fix.segment) return lengthOf(plan, segment);
|
|
48
|
+
if (segment === fix.segment) return sub(fix.stop, edgeOf(plan, segment));
|
|
49
|
+
return ZERO;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* This function adds the columns of one draw in one segment. The gifts of the need are not less than the demand times the fed days.
|
|
53
|
+
* The share of each stock is the sum of the takes of the lots that are ready and not spoiled in the whole segment.
|
|
54
|
+
*/
|
|
55
|
+
function addCell(program, plan, draw, segment, fed) {
|
|
56
|
+
const block = drawBlock(program, plan.parts, draw);
|
|
57
|
+
const demand = exact(drawDemand(draw, plan.caseCuts));
|
|
58
|
+
const need = new Map(block.gifts);
|
|
59
|
+
if (typeof fed === "number") {
|
|
60
|
+
addTo(need, fed, neg(demand));
|
|
61
|
+
program.push(need, ">=", ZERO);
|
|
62
|
+
} else program.push(need, ">=", mul(demand, fed));
|
|
63
|
+
const from = plan.edges[segment] ?? 0;
|
|
64
|
+
const to = plan.edges[segment + 1] ?? 1e400;
|
|
65
|
+
const takes = /* @__PURE__ */ new Map();
|
|
66
|
+
for (const [resource, share] of block.shares) {
|
|
67
|
+
const row = /* @__PURE__ */ new Map([[share, ONE]]);
|
|
68
|
+
for (const lot of plan.lots) {
|
|
69
|
+
if (lot.resource !== resource || lot.day > from || lot.spoils < to) continue;
|
|
70
|
+
const column = program.next();
|
|
71
|
+
takes.set(lot, column);
|
|
72
|
+
addTo(row, column, neg(ONE));
|
|
73
|
+
}
|
|
74
|
+
program.push(row, "=", ZERO);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
draw,
|
|
78
|
+
segment,
|
|
79
|
+
block,
|
|
80
|
+
fed,
|
|
81
|
+
takes
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** This function adds the row of each lot: the takes of all the cells are not more than its amount. */
|
|
85
|
+
function lotRows(program, plan, cells) {
|
|
86
|
+
for (const lot of plan.lots) {
|
|
87
|
+
const row = /* @__PURE__ */ new Map();
|
|
88
|
+
for (const cell of cells) {
|
|
89
|
+
const column = cell.takes.get(lot);
|
|
90
|
+
if (column !== void 0) addTo(row, column, ONE);
|
|
91
|
+
}
|
|
92
|
+
if (row.size > 0) program.push(row, "<=", lot.amount);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* This function adds the cells of the fixed draws. Each one is fed to its first stop, and its runs use the device-days of each day
|
|
97
|
+
* that it keeps. It gives the device-days of each day that the fixed draws keep, by the index of the segment, then the key.
|
|
98
|
+
*/
|
|
99
|
+
function addFixed(program, plan, fixed, cells) {
|
|
100
|
+
const kept = /* @__PURE__ */ new Map();
|
|
101
|
+
for (const draw of plan.draws) {
|
|
102
|
+
const fix = fixed.get(draw.need);
|
|
103
|
+
if (fix === void 0) continue;
|
|
104
|
+
for (let segment = 0; segment <= fix.segment; segment += 1) {
|
|
105
|
+
const fed = fixedFed(plan, fix, segment);
|
|
106
|
+
const keep = fix.keep.get(segment) ?? /* @__PURE__ */ new Map();
|
|
107
|
+
const into = kept.get(segment) ?? /* @__PURE__ */ new Map();
|
|
108
|
+
kept.set(segment, into);
|
|
109
|
+
for (const [key, value] of keep) into.set(key, add(into.get(key) ?? ZERO, value));
|
|
110
|
+
if (sign(fed) <= 0) continue;
|
|
111
|
+
const cell = addCell(program, plan, draw, segment, fed);
|
|
112
|
+
cells.push(cell);
|
|
113
|
+
const { limits, stopped } = deviceUse(cell.block.runs);
|
|
114
|
+
for (const index of stopped) program.push(/* @__PURE__ */ new Map([[index, ONE]]), "<=", ZERO);
|
|
115
|
+
for (const [key, limit] of limits) program.push(limit.row, "<=", mul(keep.get(key) ?? ZERO, fed));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return kept;
|
|
119
|
+
}
|
|
120
|
+
/** This function gives the device-days of each day that the fixed draws leave in a segment. */
|
|
121
|
+
function roomOf(plan, kept, segment, key, devices) {
|
|
122
|
+
const left = sub(exact(plan.devices.get(key) ?? devices), kept.get(segment)?.get(key) ?? ZERO);
|
|
123
|
+
return sign(left) > 0 ? left : ZERO;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* This function gives the program of one step with the stop of the open draws in segment `segment`. The open draws are fed in
|
|
127
|
+
* each segment before it. In it, each one is fed for `t - edge` days or more, and their runs share the device-days that
|
|
128
|
+
* the fixed draws leave, for `t - edge` days. This is the safe side, as in `bestEndurance`.
|
|
129
|
+
*/
|
|
130
|
+
function buildStep$1(plan, fixed, open, segment) {
|
|
131
|
+
const program = new Program();
|
|
132
|
+
const t = program.next();
|
|
133
|
+
program.push(/* @__PURE__ */ new Map([[t, ONE]]), "<=", edgeOf(plan, segment + 1));
|
|
134
|
+
const cells = [];
|
|
135
|
+
const kept = addFixed(program, plan, fixed, cells);
|
|
136
|
+
const opened = new Set(open.map((draw) => draw.need));
|
|
137
|
+
const floors = /* @__PURE__ */ new Map();
|
|
138
|
+
for (const draw of plan.draws) {
|
|
139
|
+
if (!opened.has(draw.need)) continue;
|
|
140
|
+
for (let index = 0; index < segment; index += 1) cells.push(addCell(program, plan, draw, index, lengthOf(plan, index)));
|
|
141
|
+
const fed = program.next();
|
|
142
|
+
program.push(/* @__PURE__ */ new Map([[fed, ONE]]), "<=", lengthOf(plan, segment));
|
|
143
|
+
floors.set(draw.need, program.push(/* @__PURE__ */ new Map([[fed, ONE], [t, neg(ONE)]]), ">=", neg(edgeOf(plan, segment))));
|
|
144
|
+
cells.push(addCell(program, plan, draw, segment, fed));
|
|
145
|
+
}
|
|
146
|
+
const shared = /* @__PURE__ */ new Map();
|
|
147
|
+
for (let index = 0; index <= segment; index += 1) {
|
|
148
|
+
const { limits, stopped } = deviceUse(cells.filter((cell) => cell.segment === index && opened.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs]));
|
|
149
|
+
for (const column of stopped) program.push(/* @__PURE__ */ new Map([[column, ONE]]), "<=", ZERO);
|
|
150
|
+
const bySegment = /* @__PURE__ */ new Map();
|
|
151
|
+
shared.set(index, bySegment);
|
|
152
|
+
for (const [key, limit] of limits) {
|
|
153
|
+
const room = roomOf(plan, kept, index, key, limit.devices);
|
|
154
|
+
bySegment.set(key, {
|
|
155
|
+
row: limit.row,
|
|
156
|
+
room,
|
|
157
|
+
converters: limit.converters
|
|
158
|
+
});
|
|
159
|
+
const row = new Map(limit.row);
|
|
160
|
+
if (index < segment) program.push(row, "<=", mul(room, lengthOf(plan, index)));
|
|
161
|
+
else {
|
|
162
|
+
addTo(row, t, neg(room));
|
|
163
|
+
program.push(row, "<=", neg(mul(room, edgeOf(plan, segment))));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
lotRows(program, plan, cells);
|
|
168
|
+
return {
|
|
169
|
+
program,
|
|
170
|
+
t,
|
|
171
|
+
cells,
|
|
172
|
+
floors,
|
|
173
|
+
shared,
|
|
174
|
+
segment
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* This function gives the largest common first stop of the open draws, with the fixed draws at their stops. The days to
|
|
179
|
+
* a first stop are not a linear function of the takes, so the function tries the segments in order. When the days reach
|
|
180
|
+
* the end of a segment, it tries the next segment. The days of the last step are exact.
|
|
181
|
+
* After the largest days, the step takes the objectives of `tail`.
|
|
182
|
+
*/
|
|
183
|
+
function firstStop(plan, fixed, open, tail = () => []) {
|
|
184
|
+
const segments = segmentsOf(plan);
|
|
185
|
+
for (let segment = 0; segment < segments; segment += 1) {
|
|
186
|
+
const step = buildStep$1(plan, fixed, open, segment);
|
|
187
|
+
const most = {
|
|
188
|
+
sense: "max",
|
|
189
|
+
coefficients: dense(/* @__PURE__ */ new Map([[step.t, ONE]]), step.program.count)
|
|
190
|
+
};
|
|
191
|
+
const solved = solveLexicographic(step.program.constraints(), [most]);
|
|
192
|
+
if (solved.status !== "optimal") throw new Error("The step of a season has no optimum. The point of the stop of the last segment is a point of it.");
|
|
193
|
+
const days = solved.values[0] ?? ZERO;
|
|
194
|
+
const end = edgeOf(plan, segment + 1);
|
|
195
|
+
if (segment + 1 < segments && equals(days, end)) continue;
|
|
196
|
+
const full = tail(step).length === 0 ? solved : solveLexicographic(step.program.constraints(), [most, ...tail(step)]);
|
|
197
|
+
if (full.status !== "optimal") throw new Error("The step of a season has no optimum for its later objectives.");
|
|
198
|
+
const first = full.stages[0]?.result;
|
|
199
|
+
return {
|
|
200
|
+
step,
|
|
201
|
+
x: full.x,
|
|
202
|
+
dual: first?.status === "optimal" ? first.dual : [],
|
|
203
|
+
days,
|
|
204
|
+
atHorizon: segment + 1 === segments && equals(days, end)
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
throw new Error("A season plan has no segment.");
|
|
208
|
+
}
|
|
209
|
+
/** This function gives the fewest device-days of the runs of the open cells of a step, as an objective. */
|
|
210
|
+
function fewestOpen(step, open) {
|
|
211
|
+
return {
|
|
212
|
+
sense: "min",
|
|
213
|
+
coefficients: dense(fewestRow(step.cells.filter((cell) => open.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs])), step.program.count)
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** This function gives the smallest sum of the shares of the stocks, each over the total of its lots, as an objective. */
|
|
217
|
+
function leastShares(plan, cells, count) {
|
|
218
|
+
const least = /* @__PURE__ */ new Map();
|
|
219
|
+
for (const cell of cells) for (const [resource, index] of cell.block.shares) addTo(least, index, div(ONE, exact(plan.parts.stock.get(resource) ?? 1)));
|
|
220
|
+
return {
|
|
221
|
+
sense: "min",
|
|
222
|
+
coefficients: dense(least, count)
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/** This function fixes the open draws of `done` at the stop: the device-days of each day that they keep, and their capped converters. */
|
|
226
|
+
function fixStops(plan, stop, done, fixed) {
|
|
227
|
+
const { step, x, days } = stop;
|
|
228
|
+
const fedIn = (segment) => segment < step.segment ? lengthOf(plan, segment) : sub(days, edgeOf(plan, step.segment));
|
|
229
|
+
const full = /* @__PURE__ */ new Set();
|
|
230
|
+
for (const [segment, bySegment] of step.shared) for (const [key, limit] of bySegment) if (equals(valueAt(limit.row, x), mul(limit.room, fedIn(segment)))) full.add(`${String(segment)}\u0000${key}`);
|
|
231
|
+
for (const draw of done) {
|
|
232
|
+
const keep = /* @__PURE__ */ new Map();
|
|
233
|
+
const used = /* @__PURE__ */ new Set();
|
|
234
|
+
for (const cell of step.cells) {
|
|
235
|
+
if (cell.draw.need !== draw.need) continue;
|
|
236
|
+
const fed = fedIn(cell.segment);
|
|
237
|
+
const bySegment = /* @__PURE__ */ new Map();
|
|
238
|
+
keep.set(cell.segment, bySegment);
|
|
239
|
+
for (const [key, limit] of deviceUse(cell.block.runs).limits) {
|
|
240
|
+
bySegment.set(key, sign(fed) > 0 ? div(valueAt(limit.row, x), fed) : ZERO);
|
|
241
|
+
if (full.has(`${String(cell.segment)}\u0000${key}`)) for (const converter of limit.converters) used.add(converter);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const { reach } = reachOf(plan.parts, /* @__PURE__ */ new Set([draw.need]));
|
|
245
|
+
const capped = sign(days) > 0 ? [...reach].filter((converter) => converter.perDay !== void 0 && (!(converter.perDay > 0) || used.has(converter))).map((converter) => converter.id).sort() : [];
|
|
246
|
+
fixed.set(draw.need, {
|
|
247
|
+
stop: days,
|
|
248
|
+
segment: step.segment,
|
|
249
|
+
keep,
|
|
250
|
+
atHorizon: stop.atHorizon,
|
|
251
|
+
capped
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/** This function gives the sum of the take of each resource of the cells of one need, more than 0 only. */
|
|
256
|
+
function takesOf(cells, need, x) {
|
|
257
|
+
const sums = /* @__PURE__ */ new Map();
|
|
258
|
+
for (const cell of cells) {
|
|
259
|
+
if (cell.draw.need !== need) continue;
|
|
260
|
+
for (const [resource, index] of cell.block.shares) sums.set(resource, add(sums.get(resource) ?? ZERO, x[index] ?? ZERO));
|
|
261
|
+
}
|
|
262
|
+
const out = {};
|
|
263
|
+
for (const [resource, value] of [...sums].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) if (sign(value) > 0) out[resource] = toNumber(value);
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
/** This function gives the resources of `closure` with a store in the layer whose lots are used up at `x`. They are in the order of their codes. */
|
|
267
|
+
function usedUp(plan, cells, x, closure, stored) {
|
|
268
|
+
return [...closure].filter((resource) => stored.has(resource)).filter((resource) => {
|
|
269
|
+
let taken = ZERO;
|
|
270
|
+
let amount = ZERO;
|
|
271
|
+
for (const lot of plan.lots) {
|
|
272
|
+
if (lot.resource !== resource) continue;
|
|
273
|
+
amount = add(amount, lot.amount);
|
|
274
|
+
for (const cell of cells) taken = add(taken, x[cell.takes.get(lot) ?? -1] ?? ZERO);
|
|
275
|
+
}
|
|
276
|
+
return equals(taken, amount);
|
|
277
|
+
}).sort();
|
|
278
|
+
}
|
|
279
|
+
/** This function tells if the cells of two draws or more of `cells` can use the limited device `key`. */
|
|
280
|
+
function sharedKey(cells, key) {
|
|
281
|
+
return new Set(cells.filter((cell) => deviceUse(cell.block.runs).limits.has(key)).map((cell) => cell.draw.need)).size > 1;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* This function gives the last program of the best endurance with a season. Each draw keeps its first stop. After it, a later
|
|
285
|
+
* harvest can feed the draw again (`again`). The objectives come in this order:
|
|
286
|
+
* - the most fed days after the stops for each level, in the order of the levels.
|
|
287
|
+
* - the earliest fed days.
|
|
288
|
+
* - the smallest sum of the shares of the stocks.
|
|
289
|
+
* - the fewest device-days.
|
|
290
|
+
*
|
|
291
|
+
* In a segment after the stops, the cells of two draws or more that can use one limited device take turns. Their fed days
|
|
292
|
+
* together are not more than the segment, and each one has the device-days of each day that the stops leave. This is the
|
|
293
|
+
* safe side: the cells do not use one device at the same time. Each other cell starts at the start of the segment.
|
|
294
|
+
*/
|
|
295
|
+
function seasonBest(plan, fixed, levels, stored) {
|
|
296
|
+
const program = new Program();
|
|
297
|
+
const cells = [];
|
|
298
|
+
const kept = addFixed(program, plan, fixed, cells);
|
|
299
|
+
const segments = segmentsOf(plan);
|
|
300
|
+
const again = /* @__PURE__ */ new Map();
|
|
301
|
+
const taking = /* @__PURE__ */ new Set();
|
|
302
|
+
for (const draw of plan.draws) {
|
|
303
|
+
const fix = fixed.get(draw.need);
|
|
304
|
+
for (let segment = fix.segment + 1; segment < segments; segment += 1) {
|
|
305
|
+
const fed = program.next();
|
|
306
|
+
program.push(/* @__PURE__ */ new Map([[fed, ONE]]), "<=", lengthOf(plan, segment));
|
|
307
|
+
const cell = addCell(program, plan, draw, segment, fed);
|
|
308
|
+
cells.push(cell);
|
|
309
|
+
again.set(cell, fed);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
for (let segment = 0; segment < segments; segment += 1) {
|
|
313
|
+
const later = [...again.keys()].filter((cell) => cell.segment === segment);
|
|
314
|
+
const { limits, stopped } = deviceUse(later.flatMap((cell) => [...cell.block.runs]));
|
|
315
|
+
for (const column of stopped) program.push(/* @__PURE__ */ new Map([[column, ONE]]), "<=", ZERO);
|
|
316
|
+
const turns = new Set(later.filter((cell) => [...deviceUse(cell.block.runs).limits.keys()].some((key) => sharedKey(later, key))));
|
|
317
|
+
if (turns.size > 0) {
|
|
318
|
+
const row = /* @__PURE__ */ new Map();
|
|
319
|
+
for (const cell of turns) addTo(row, again.get(cell), ONE);
|
|
320
|
+
program.push(row, "<=", lengthOf(plan, segment));
|
|
321
|
+
for (const cell of turns) taking.add(cell);
|
|
322
|
+
}
|
|
323
|
+
for (const [key, limit] of limits) {
|
|
324
|
+
const room = roomOf(plan, kept, segment, key, limit.devices);
|
|
325
|
+
for (const cell of later) {
|
|
326
|
+
const own = deviceUse(cell.block.runs).limits.get(key);
|
|
327
|
+
if (own === void 0) continue;
|
|
328
|
+
const row = new Map(own.row);
|
|
329
|
+
addTo(row, again.get(cell), neg(room));
|
|
330
|
+
program.push(row, "<=", ZERO);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
lotRows(program, plan, cells);
|
|
335
|
+
const objectives = levels.map((level) => {
|
|
336
|
+
const needs = new Set(level.map((draw) => draw.need));
|
|
337
|
+
const row = /* @__PURE__ */ new Map();
|
|
338
|
+
for (const [cell, fed] of again) if (needs.has(cell.draw.need)) addTo(row, fed, ONE);
|
|
339
|
+
return {
|
|
340
|
+
sense: "max",
|
|
341
|
+
coefficients: dense(row, program.count)
|
|
342
|
+
};
|
|
343
|
+
});
|
|
344
|
+
const early = /* @__PURE__ */ new Map();
|
|
345
|
+
const horizon = edgeOf(plan, segments);
|
|
346
|
+
for (const [cell, fed] of again) addTo(early, fed, sub(horizon, edgeOf(plan, cell.segment)));
|
|
347
|
+
objectives.push({
|
|
348
|
+
sense: "max",
|
|
349
|
+
coefficients: dense(early, program.count)
|
|
350
|
+
});
|
|
351
|
+
objectives.push(leastShares(plan, cells, program.count));
|
|
352
|
+
objectives.push({
|
|
353
|
+
sense: "min",
|
|
354
|
+
coefficients: dense(fewestRow(cells.flatMap((cell) => [...cell.block.runs])), program.count)
|
|
355
|
+
});
|
|
356
|
+
const solved = solveLexicographic(program.constraints(), objectives);
|
|
357
|
+
if (solved.status !== "optimal") throw new Error("The last program of the best endurance with a season has no optimum. The point of the last step is a point of it.");
|
|
358
|
+
const x = solved.x;
|
|
359
|
+
const turn = /* @__PURE__ */ new Map();
|
|
360
|
+
const offset = /* @__PURE__ */ new Map();
|
|
361
|
+
for (const cell of [...taking].sort((a, b) => plan.draws.indexOf(a.draw) - plan.draws.indexOf(b.draw))) {
|
|
362
|
+
const start = offset.get(cell.segment) ?? ZERO;
|
|
363
|
+
turn.set(cell, start);
|
|
364
|
+
offset.set(cell.segment, add(start, x[again.get(cell)] ?? ZERO));
|
|
365
|
+
}
|
|
366
|
+
const needs = {};
|
|
367
|
+
for (const draw of plan.draws) {
|
|
368
|
+
const fix = fixed.get(draw.need);
|
|
369
|
+
const spans = [];
|
|
370
|
+
for (const [cell, fed] of again) {
|
|
371
|
+
if (cell.draw.need !== draw.need) continue;
|
|
372
|
+
const value = x[fed] ?? ZERO;
|
|
373
|
+
if (sign(value) <= 0) continue;
|
|
374
|
+
const start = add(edgeOf(plan, cell.segment), taking.has(cell) ? turn.get(cell) ?? ZERO : ZERO);
|
|
375
|
+
const from = toNumber(start);
|
|
376
|
+
const to = toNumber(add(start, value));
|
|
377
|
+
const last = spans[spans.length - 1];
|
|
378
|
+
if (last !== void 0 && last.to === from) last.to = to;
|
|
379
|
+
else spans.push({
|
|
380
|
+
from,
|
|
381
|
+
to
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
const { closure } = reachOf(plan.parts, /* @__PURE__ */ new Set([draw.need]));
|
|
385
|
+
needs[draw.need] = {
|
|
386
|
+
days: toNumber(fix.stop),
|
|
387
|
+
binding: usedUp(plan, cells, x, closure, stored),
|
|
388
|
+
takes: takesOf(cells, draw.need, x),
|
|
389
|
+
...fix.capped.length === 0 ? {} : { capped: fix.capped },
|
|
390
|
+
...fix.atHorizon ? { atHorizon: true } : {},
|
|
391
|
+
...spans.length === 0 ? {} : { again: spans }
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
return { needs };
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* This function gives the joint days of the draws of a plan with a season. The objectives come in this order:
|
|
398
|
+
* - the largest common first stop.
|
|
399
|
+
* - the smallest sum of the shares of the stocks.
|
|
400
|
+
* - the fewest device-days.
|
|
401
|
+
*/
|
|
402
|
+
function seasonJoint(plan, stored) {
|
|
403
|
+
const open = new Set(plan.draws.map((draw) => draw.need));
|
|
404
|
+
const stop = firstStop(plan, /* @__PURE__ */ new Map(), plan.draws, (step) => [leastShares(plan, step.cells, step.program.count), fewestOpen(step, open)]);
|
|
405
|
+
const { step, x } = stop;
|
|
406
|
+
const takes = {};
|
|
407
|
+
for (const draw of plan.draws) takes[draw.need] = takesOf(step.cells, draw.need, x);
|
|
408
|
+
const total = /* @__PURE__ */ new Map();
|
|
409
|
+
for (const cell of step.cells) for (const [converter, index] of cell.block.runs) total.set(converter, add(total.get(converter) ?? ZERO, x[index] ?? ZERO));
|
|
410
|
+
const runs = {};
|
|
411
|
+
for (const [converter, value] of [...total].sort(([a], [b]) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0)) if (sign(value) > 0) runs[converter.id] = toNumber(value);
|
|
412
|
+
const fixed = /* @__PURE__ */ new Map();
|
|
413
|
+
fixStops(plan, stop, plan.draws, fixed);
|
|
414
|
+
const capped = [...new Set([...fixed.values()].flatMap((fix) => fix.capped))].sort();
|
|
415
|
+
return {
|
|
416
|
+
days: toNumber(stop.days),
|
|
417
|
+
atHorizon: stop.atHorizon,
|
|
418
|
+
takes,
|
|
419
|
+
binding: usedUp(plan, step.cells, x, reachOf(plan.parts, open).closure, stored),
|
|
420
|
+
runs,
|
|
421
|
+
capped
|
|
422
|
+
};
|
|
423
|
+
}
|
|
7
424
|
// src/joint.ts
|
|
8
425
|
/**
|
|
9
426
|
* 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.
|
|
@@ -26,20 +443,23 @@ import { pathSetReliability, stronglyConnectedComponents } from "@mark1russell7/
|
|
|
26
443
|
*
|
|
27
444
|
* The program uses exact rationals, so the result has no error of the method. Only the change back to a number rounds.
|
|
28
445
|
*
|
|
446
|
+
* With a converter with a season, `days` is the largest common first stop of the plan (M2-grow-share). The sowings, the lots and
|
|
447
|
+
* the segments are those of `bestEndurance`. The plan holds the horizon at most (default `SEASON_HORIZON_DAYS`), with `atHorizon`.
|
|
448
|
+
*
|
|
29
449
|
* @param layer - The endurance layer
|
|
30
450
|
* @param caseCuts - The case
|
|
31
451
|
* @param options - The date, and the stack or the parts that are also down
|
|
32
452
|
* @param needs - The codes of the needs of the draws of the set
|
|
33
453
|
* @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`. Or a
|
|
454
|
+
* @throws {RangeError} A code of `needs` is not the need of a draw of the layer, or a date is not `YYYY-MM-DD`. Or the horizon of a layer with a season is not more than 0.
|
|
35
455
|
*/
|
|
36
456
|
function jointDays(layer, caseCuts, options, needs) {
|
|
37
457
|
const known = new Set(layer.draws.map((draw) => draw.need));
|
|
38
458
|
const unknown = needs.filter((need) => !known.has(need));
|
|
39
459
|
if (unknown.length > 0) throw new RangeError(`The needs ${unknown.map((need) => `"${need}"`).join(", ")} are not the needs of draws of the layer.`);
|
|
40
460
|
const stoppedParts = caseStopped(caseCuts, options);
|
|
41
|
-
|
|
42
|
-
const stock = storeAmounts(layer, options.at, stoppedParts);
|
|
461
|
+
const supply = hasSeason(layer, stoppedParts) ? seasonSupply(layer, stoppedParts, options) : void 0;
|
|
462
|
+
const stock = supply?.stock ?? storeAmounts(layer, options.at, stoppedParts);
|
|
43
463
|
const wanted = new Set(needs);
|
|
44
464
|
const plan = caseDraws(layer, caseCuts, stoppedParts).filter((draw) => wanted.has(draw.need));
|
|
45
465
|
const planned = new Set(plan.map((draw) => draw.need));
|
|
@@ -59,6 +479,14 @@ function jointDays(layer, caseCuts, options, needs) {
|
|
|
59
479
|
};
|
|
60
480
|
if (plan.length === 0) return endless;
|
|
61
481
|
const parts = caseParts(layer, stoppedParts, stock, planned);
|
|
482
|
+
if (supply !== void 0) {
|
|
483
|
+
const { atHorizon, ...joint } = seasonJoint(seasonPlan(supply, parts, caseCuts, plan, new Map([...deviceUse([...parts.cooks, ...parts.makers].map((converter) => [converter, 0])).limits].map(([key, limit]) => [key, limit.devices]))), new Set(layer.stores.map((store) => store.resource)));
|
|
484
|
+
return {
|
|
485
|
+
...base,
|
|
486
|
+
...joint,
|
|
487
|
+
...atHorizon ? { atHorizon: true } : {}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
62
490
|
const program = new Program();
|
|
63
491
|
const days = program.next();
|
|
64
492
|
const blocks = plan.map((draw) => {
|
|
@@ -168,16 +596,25 @@ function horizonOf$1(horizon) {
|
|
|
168
596
|
* The programs use exact rationals, so the result has no error of the method. Only the change back to a number rounds.
|
|
169
597
|
* The fewest days of the first level are the joint days (`jointDays`) of the draws of that level.
|
|
170
598
|
*
|
|
599
|
+
* A layer with a converter with a season has the programs of a season (M2-grow-share):
|
|
600
|
+
* - The sowings take their inputs before the draws, as in `enduranceOf`. Each harvest is a lot from its day, and its rest spoils on its spoil day.
|
|
601
|
+
* - The days go in segments that end at each harvest day, at each spoil day and at the horizon (default `SEASON_HORIZON_DAYS`). A segment takes only
|
|
602
|
+
* the lots that are ready and not spoiled in all of it.
|
|
603
|
+
* - `days` is the first stop of each draw, with the same leximin share. The days to a stop are not linear, so each step tries the segments in order.
|
|
604
|
+
* - After the stops, a last program gives the most fed days to each level in order. `again` gives the spans in which a later harvest feeds a draw.
|
|
605
|
+
* In such a span, the runs share the device-days of the whole segment that the stops leave.
|
|
606
|
+
* - The result has the field `harvests`. It has no field `spoiled`: the share of a lot between the draws has no spoil of one draw.
|
|
607
|
+
*
|
|
171
608
|
* @param layer - The endurance layer
|
|
172
609
|
* @param caseCuts - The case
|
|
173
610
|
* @param options - The date, the stack or the parts that are also down, and the horizon
|
|
174
611
|
* @returns The endurance of each draw whose supply stops
|
|
175
|
-
* @throws {RangeError} A date is not `YYYY-MM-DD`, or the horizon is not more than 0.
|
|
612
|
+
* @throws {RangeError} A date is not `YYYY-MM-DD`, or the horizon is not more than 0.
|
|
176
613
|
*/
|
|
177
614
|
function bestEndurance(layer, caseCuts, options) {
|
|
178
615
|
const horizon = horizonOf$1(options.horizon);
|
|
179
616
|
const stoppedParts = caseStopped(caseCuts, options);
|
|
180
|
-
if (hasSeason(layer, stoppedParts))
|
|
617
|
+
if (hasSeason(layer, stoppedParts)) return bestSeason(layer, caseCuts, options, stoppedParts);
|
|
181
618
|
const stock = storeAmounts(layer, options.at, stoppedParts);
|
|
182
619
|
const draws = caseDraws(layer, caseCuts, stoppedParts);
|
|
183
620
|
const parts = caseParts(layer, stoppedParts, stock, new Set(draws.map((draw) => draw.need)));
|
|
@@ -278,6 +715,40 @@ function bestEndurance(layer, caseCuts, options) {
|
|
|
278
715
|
needs
|
|
279
716
|
};
|
|
280
717
|
}
|
|
718
|
+
/** This function gives the best endurance of a case with a season (M2-grow-share). */
|
|
719
|
+
function bestSeason(layer, caseCuts, options, stoppedParts) {
|
|
720
|
+
const supply = seasonSupply(layer, stoppedParts, options);
|
|
721
|
+
const draws = caseDraws(layer, caseCuts, stoppedParts);
|
|
722
|
+
const parts = caseParts(layer, stoppedParts, supply.stock, new Set(draws.map((draw) => draw.need)));
|
|
723
|
+
const plan = seasonPlan(supply, parts, caseCuts, draws, new Map([...deviceUse([...parts.cooks, ...parts.makers].map((converter) => [converter, 0])).limits].map(([key, limit]) => [key, limit.devices])));
|
|
724
|
+
const harvests = supply.harvests.length === 0 ? {} : { harvests: supply.harvests };
|
|
725
|
+
if (draws.length === 0) return {
|
|
726
|
+
case: caseCuts.case,
|
|
727
|
+
at: options.at,
|
|
728
|
+
needs: {},
|
|
729
|
+
...harvests
|
|
730
|
+
};
|
|
731
|
+
const levels = levelGroups(layer, draws);
|
|
732
|
+
const fixed = /* @__PURE__ */ new Map();
|
|
733
|
+
for (const level of levels) {
|
|
734
|
+
let open = level;
|
|
735
|
+
while (open.length > 0) {
|
|
736
|
+
const opened = new Set(open.map((draw) => draw.need));
|
|
737
|
+
const stop = firstStop(plan, fixed, open, (step) => [fewestOpen(step, opened)]);
|
|
738
|
+
let done = stop.atHorizon ? open : open.filter((draw) => sign(stop.dual[stop.step.floors.get(draw.need)] ?? ZERO) !== 0);
|
|
739
|
+
if (done.length === 0) done = open;
|
|
740
|
+
fixStops(plan, stop, done, fixed);
|
|
741
|
+
open = open.filter((draw) => !done.includes(draw));
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
const { needs } = seasonBest(plan, fixed, levels, new Set(layer.stores.map((store) => store.resource)));
|
|
745
|
+
return {
|
|
746
|
+
case: caseCuts.case,
|
|
747
|
+
at: options.at,
|
|
748
|
+
needs,
|
|
749
|
+
...harvests
|
|
750
|
+
};
|
|
751
|
+
}
|
|
281
752
|
/** 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. */
|
|
282
753
|
function levelGroups(layer, draws) {
|
|
283
754
|
const rank = (draw) => {
|
|
@@ -1386,6 +1857,6 @@ const SAMPLE_ENDURANCE = {
|
|
|
1386
1857
|
asOf: "2025-12-01"
|
|
1387
1858
|
}]
|
|
1388
1859
|
};
|
|
1389
|
-
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, joinEndurance, jointDays, longestStop, minimalStops, sowDays, stopCurves, storeAmounts, survivalAt, survivalQuantile, tailDays, upperStop, yearChance, yearRate, yearShortfall };
|
|
1860
|
+
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, joinEndurance, jointDays, longestStop, minimalStops, seasonSupply, sowDays, stopCurves, storeAmounts, survivalAt, survivalQuantile, tailDays, upperStop, yearChance, yearRate, yearShortfall };
|
|
1390
1861
|
|
|
1391
1862
|
//# sourceMappingURL=index.js.map
|