@mark1russell7/stack-endurance 0.8.28 → 0.8.30
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 +6 -3
- package/build/add-on.js +1 -1
- package/build/index.d.ts +44 -42
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1726 -38
- package/build/index.js.map +1 -1
- package/build/join-CsR-FKzr.js +386 -0
- package/build/join-CsR-FKzr.js.map +1 -0
- package/build/schema.d.ts.map +1 -1
- package/build/schema.js +1 -0
- package/build/schema.js.map +1 -1
- package/package.json +3 -3
- package/build/join-r5ht3vQ9.js +0 -2050
- package/build/join-r5ht3vQ9.js.map +0 -1
package/build/join-r5ht3vQ9.js
DELETED
|
@@ -1,2050 +0,0 @@
|
|
|
1
|
-
import { t as ENDURANCE_SCHEMA } from "./layer-a3Fr70Uh.js";
|
|
2
|
-
import { CUT_BOUNDS, bandFault, countsFault, curveFault, curveFault as curveFault$1, dayNumber, intervalFault, isCutBound, joinRecords, joinSetting, stopped } from "@mark1russell7/system-stack";
|
|
3
|
-
import { ONE, ZERO, add, div, equals, fromFloat, mul, neg, sign, sub, toNumber } from "@mark1russell7/rational";
|
|
4
|
-
import { solveLexicographic } from "@mark1russell7/linear";
|
|
5
|
-
import { stronglyConnectedComponents } from "@mark1russell7/collections/graph";
|
|
6
|
-
import { canonicalJson } from "@mark1russell7/rigor-generators";
|
|
7
|
-
// src/converter.ts
|
|
8
|
-
/** This function gives the amount of each resource that one run of a converter takes. Two lines of one resource add. */
|
|
9
|
-
function inputsOf(converter) {
|
|
10
|
-
const out = /* @__PURE__ */ new Map();
|
|
11
|
-
for (const input of converter.takes) if (input.amount > 0) out.set(input.resource, (out.get(input.resource) ?? 0) + input.amount);
|
|
12
|
-
return out;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* This function tells if a converter is a source (M2-solar): it takes nothing, and it has a limit `perDay` more than 0.
|
|
16
|
-
* Its runs of a day are lost when no draw uses them. Thus a take uses a source before a store.
|
|
17
|
-
*/
|
|
18
|
-
function isSource(converter) {
|
|
19
|
-
if (!(converter.perDay !== void 0 && converter.perDay > 0)) return false;
|
|
20
|
-
for (const input of converter.takes) if (input.amount > 0) return false;
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* This function tells if a converter can run in a case. Its device works, it takes a resource or it is a source, and it
|
|
25
|
-
* gives an amount more than 0.
|
|
26
|
-
*/
|
|
27
|
-
function canRun(converter, stoppedParts) {
|
|
28
|
-
return converter.gives.amount > 0 && (inputsOf(converter).size > 0 || isSource(converter)) && (converter.device === void 0 || !stoppedParts.has(converter.device));
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* This function gives the key of the device-days that a converter uses. The converters of one device share one key.
|
|
32
|
-
* A converter with no device has a key of its own.
|
|
33
|
-
*/
|
|
34
|
-
function deviceKey(converter) {
|
|
35
|
-
return converter.device === void 0 ? `converter:${converter.id}` : `device:${converter.device}`;
|
|
36
|
-
}
|
|
37
|
-
// src/fit.ts
|
|
38
|
-
/** The relative tolerance of a take and of a fit. */
|
|
39
|
-
const TOLERANCE = 1e-13;
|
|
40
|
-
/**
|
|
41
|
-
* The count of the tries of `largestFit`, for the tests of the cost. It is not in the public API, and it does not change a result.
|
|
42
|
-
* It is the one side effect of `largestFit` and of the queries that use it.
|
|
43
|
-
*/
|
|
44
|
-
const fitTries = { count: 0 };
|
|
45
|
-
/**
|
|
46
|
-
* This function gives the largest `x` in `[0, most]` at which `attempt` has no shortfall, to the relative tolerance `TOLERANCE`.
|
|
47
|
-
*
|
|
48
|
-
* - A try fits when its shortfall is not more than `TOLERANCE` times its need.
|
|
49
|
-
* - The shortfall must not decrease when `x` increases. A take of a chain of converters gives a shortfall that is linear in parts.
|
|
50
|
-
* - The first try is `most`. After it, each step tries the zero of the line through the last two tries that do not fit.
|
|
51
|
-
* With one try that does not fit, the step tries `x` times the part of the need that the stores gave.
|
|
52
|
-
* - When a step does not halve the interval, the next step is a bisection. Thus a fit takes at most approximately twice the steps of a bisection.
|
|
53
|
-
* - A step on the lower end tries a point just above it, so a correct estimate ends the fit in one more try.
|
|
54
|
-
* - An estimate that fits with a shortfall above 0 is in the band of the tolerance above the zero, so the fit ends there.
|
|
55
|
-
* A bisection step or a probe at the lower end can fit with a shortfall of rounding below the zero. The fit does not end there.
|
|
56
|
-
*
|
|
57
|
-
* @param most - The upper limit of `x`
|
|
58
|
-
* @param attempt - The try at an amount. It must not change its input
|
|
59
|
-
* @returns The largest amount that fits, and the value of its try
|
|
60
|
-
*/
|
|
61
|
-
function largestFit(most, attempt) {
|
|
62
|
-
const fits = (tried) => tried.short <= TOLERANCE * tried.need;
|
|
63
|
-
fitTries.count += 1;
|
|
64
|
-
const first = attempt(most);
|
|
65
|
-
if (fits(first)) return {
|
|
66
|
-
x: most,
|
|
67
|
-
value: first.value
|
|
68
|
-
};
|
|
69
|
-
let low = 0;
|
|
70
|
-
let best;
|
|
71
|
-
let high = most;
|
|
72
|
-
let highShort = first.short;
|
|
73
|
-
let highNeed = first.need;
|
|
74
|
-
let before;
|
|
75
|
-
let bisect = false;
|
|
76
|
-
for (let step = 0; step < 400 && high - low > 4e-13 * high; step += 1) {
|
|
77
|
-
const width = high - low;
|
|
78
|
-
let x;
|
|
79
|
-
if (bisect) x = (low + high) / 2;
|
|
80
|
-
else if (before !== void 0 && before.short > highShort) x = high - highShort * (before.x - high) / (before.short - highShort);
|
|
81
|
-
else x = high * (1 - highShort / highNeed);
|
|
82
|
-
const estimate = !bisect && x < high;
|
|
83
|
-
if (!(x < high)) x = (low + high) / 2;
|
|
84
|
-
const floor = low + 2 * TOLERANCE * high;
|
|
85
|
-
const clamped = !(x > floor);
|
|
86
|
-
if (clamped) x = floor;
|
|
87
|
-
fitTries.count += 1;
|
|
88
|
-
const tried = attempt(x);
|
|
89
|
-
if (fits(tried)) {
|
|
90
|
-
low = x;
|
|
91
|
-
best = tried.value;
|
|
92
|
-
if (tried.short > 0 && estimate && !clamped) break;
|
|
93
|
-
} else {
|
|
94
|
-
before = {
|
|
95
|
-
x: high,
|
|
96
|
-
short: highShort
|
|
97
|
-
};
|
|
98
|
-
high = x;
|
|
99
|
-
highShort = tried.short;
|
|
100
|
-
highNeed = tried.need;
|
|
101
|
-
}
|
|
102
|
-
bisect = !bisect && high - low > width / 2;
|
|
103
|
-
}
|
|
104
|
-
return {
|
|
105
|
-
x: low,
|
|
106
|
-
value: best
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
// src/rows.ts
|
|
110
|
-
/** This function adds `value` to the coefficient of the variable `index` in `row`. It changes `row`. */
|
|
111
|
-
function addTo(row, index, value) {
|
|
112
|
-
row.set(index, add(row.get(index) ?? ZERO, value));
|
|
113
|
-
}
|
|
114
|
-
/** This function gives the dense form of a row: one coefficient for each of `count` variables. */
|
|
115
|
-
function dense(row, count) {
|
|
116
|
-
const out = new Array(count).fill(ZERO);
|
|
117
|
-
for (const [index, value] of row) out[index] = value;
|
|
118
|
-
return out;
|
|
119
|
-
}
|
|
120
|
-
/** This function gives the exact rational of a finite double, with no rounding. A double is a dyadic rational. */
|
|
121
|
-
function exact(value) {
|
|
122
|
-
return fromFloat(value);
|
|
123
|
-
}
|
|
124
|
-
// src/program.ts
|
|
125
|
-
/** A program in construction: the count of its variables and its rows. */
|
|
126
|
-
var Program = class {
|
|
127
|
-
count = 0;
|
|
128
|
-
rows = [];
|
|
129
|
-
/** This method adds a variable and gives its index. */
|
|
130
|
-
next() {
|
|
131
|
-
return this.count++;
|
|
132
|
-
}
|
|
133
|
-
/** This method adds a row and gives its index. */
|
|
134
|
-
push(row, relation, rhs) {
|
|
135
|
-
this.rows.push({
|
|
136
|
-
row,
|
|
137
|
-
relation,
|
|
138
|
-
rhs
|
|
139
|
-
});
|
|
140
|
-
return this.rows.length - 1;
|
|
141
|
-
}
|
|
142
|
-
/** This method gives the dense rows for `solveLexicographic`. */
|
|
143
|
-
constraints() {
|
|
144
|
-
return this.rows.map(({ row, relation, rhs }) => ({
|
|
145
|
-
coefficients: dense(row, this.count),
|
|
146
|
-
relation,
|
|
147
|
-
rhs
|
|
148
|
-
}));
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
/** This function gives the parts of a case for the draws of `needs`. */
|
|
152
|
-
function caseParts(layer, stoppedParts, stock, needs) {
|
|
153
|
-
const direct = (layer.direct ?? []).filter((use) => needs.has(use.need) && use.amount > 0);
|
|
154
|
-
const live = (layer.converters ?? []).filter((converter) => converter.season === void 0 && canRun(converter, stoppedParts));
|
|
155
|
-
return partsOf(stock, direct, live.filter((converter) => needs.has(giftNeed(converter) ?? "")), live.filter((converter) => giftResource(converter) !== void 0));
|
|
156
|
-
}
|
|
157
|
-
/** This function gives the parts of a case from its stock, its direct uses, its cooks and its makers. */
|
|
158
|
-
function partsOf(stock, direct, cooks, makers) {
|
|
159
|
-
const resources = /* @__PURE__ */ new Set([...direct.map((use) => use.resource), ...[...cooks, ...makers].flatMap((converter) => [...inputsOf(converter).keys()])]);
|
|
160
|
-
for (const maker of makers) resources.add(giftResource(maker) ?? "");
|
|
161
|
-
return {
|
|
162
|
-
stock,
|
|
163
|
-
direct,
|
|
164
|
-
cooks,
|
|
165
|
-
makers,
|
|
166
|
-
resources,
|
|
167
|
-
stocked: [...resources].filter((resource) => (stock.get(resource) ?? 0) > 0).sort()
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* This function adds the columns of one draw to a program, and the balance row of each of its resources.
|
|
172
|
-
* In a balance row, the takes of the draw are not more than its share of the stock plus the gifts of its makers.
|
|
173
|
-
* The caller adds the row of the need, with the term of the days.
|
|
174
|
-
*
|
|
175
|
-
* A draw has columns and rows only for the resources and the makers that it can use (`reachOf`).
|
|
176
|
-
* A share of another resource or the runs of another maker cannot give the need of the draw. Thus the optimum of each program is the same.
|
|
177
|
-
* This keeps each program small: the programs are dense, so each column of each draw costs a column in each row.
|
|
178
|
-
*
|
|
179
|
-
* @param stocked - False for a draw that takes no stock. Its shares are not columns.
|
|
180
|
-
*/
|
|
181
|
-
function drawBlock(program, parts, draw, stocked = true) {
|
|
182
|
-
const gifts = /* @__PURE__ */ new Map();
|
|
183
|
-
const { closure, reach } = reachOf(parts, /* @__PURE__ */ new Set([draw.need]));
|
|
184
|
-
const balance = new Map([...closure].map((resource) => [resource, /* @__PURE__ */ new Map()]));
|
|
185
|
-
const consume = (resource, index, amount) => addTo(balance.get(resource), index, amount);
|
|
186
|
-
for (const use of parts.direct) {
|
|
187
|
-
if (use.need !== draw.need) continue;
|
|
188
|
-
const index = program.next();
|
|
189
|
-
addTo(gifts, index, exact(use.amount));
|
|
190
|
-
consume(use.resource, index, ONE);
|
|
191
|
-
}
|
|
192
|
-
const runs = /* @__PURE__ */ new Map();
|
|
193
|
-
for (const cook of parts.cooks) {
|
|
194
|
-
if (giftNeed(cook) !== draw.need) continue;
|
|
195
|
-
const index = program.next();
|
|
196
|
-
runs.set(cook, index);
|
|
197
|
-
addTo(gifts, index, exact(cook.gives.amount));
|
|
198
|
-
for (const [resource, each] of inputsOf(cook)) consume(resource, index, exact(each));
|
|
199
|
-
}
|
|
200
|
-
for (const maker of parts.makers) {
|
|
201
|
-
if (!reach.has(maker)) continue;
|
|
202
|
-
const index = program.next();
|
|
203
|
-
runs.set(maker, index);
|
|
204
|
-
for (const [resource, each] of inputsOf(maker)) consume(resource, index, exact(each));
|
|
205
|
-
consume(giftResource(maker), index, neg(exact(maker.gives.amount)));
|
|
206
|
-
}
|
|
207
|
-
const shares = new Map(stocked ? parts.stocked.filter((resource) => closure.has(resource)).map((resource) => [resource, program.next()]) : []);
|
|
208
|
-
for (const [resource, index] of shares) consume(resource, index, neg(ONE));
|
|
209
|
-
for (const row of balance.values()) if (row.size > 0) program.push(row, "<=", ZERO);
|
|
210
|
-
return {
|
|
211
|
-
draw,
|
|
212
|
-
gifts,
|
|
213
|
-
shares,
|
|
214
|
-
runs
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* This function gives the device-days of each device of a set of run columns, by the key of the device (`deviceKey`).
|
|
219
|
-
* One run uses `1 / perDay` of a day of one device. The converters of one device share one limit. A converter with no device has a limit of its own.
|
|
220
|
-
* A converter with no limit `perDay` is not in the record. The list `stopped` has the columns of the converters with a limit of 0.
|
|
221
|
-
*/
|
|
222
|
-
function deviceUse(runs) {
|
|
223
|
-
const limits = /* @__PURE__ */ new Map();
|
|
224
|
-
const stopped = [];
|
|
225
|
-
for (const [converter, index] of runs) {
|
|
226
|
-
if (converter.perDay === void 0) continue;
|
|
227
|
-
if (!(converter.perDay > 0)) {
|
|
228
|
-
stopped.push(index);
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
const key = deviceKey(converter);
|
|
232
|
-
const limit = limits.get(key) ?? {
|
|
233
|
-
row: /* @__PURE__ */ new Map(),
|
|
234
|
-
devices: converter.devices ?? 1,
|
|
235
|
-
converters: []
|
|
236
|
-
};
|
|
237
|
-
limits.set(key, limit);
|
|
238
|
-
if (!limit.converters.includes(converter)) limit.converters.push(converter);
|
|
239
|
-
addTo(limit.row, index, div(ONE, exact(converter.perDay)));
|
|
240
|
-
}
|
|
241
|
-
return {
|
|
242
|
-
limits,
|
|
243
|
-
stopped
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* This function gives the row of the fewest device-days of a set of run columns: the runs over `perDay`, or the runs with no limit.
|
|
248
|
-
* As an objective, it gives no more runs than the plan uses.
|
|
249
|
-
*/
|
|
250
|
-
function fewestRow(runs) {
|
|
251
|
-
const out = /* @__PURE__ */ new Map();
|
|
252
|
-
for (const [converter, index] of runs) addTo(out, index, converter.perDay !== void 0 && converter.perDay > 0 ? div(ONE, exact(converter.perDay)) : ONE);
|
|
253
|
-
return out;
|
|
254
|
-
}
|
|
255
|
-
/** This function gives the value of a sparse row at the point `x`. */
|
|
256
|
-
function valueAt(row, x) {
|
|
257
|
-
let out = ZERO;
|
|
258
|
-
for (const [index, value] of row) out = add(out, mul(value, x[index] ?? ZERO));
|
|
259
|
-
return out;
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* This function gives each resource that a draw can use: the resources of its direct uses and the inputs of its cooks.
|
|
263
|
-
* It also gives the inputs of their makers, through each chain.
|
|
264
|
-
*/
|
|
265
|
-
function inputClosure$1(direct, cooks, makers) {
|
|
266
|
-
const out = /* @__PURE__ */ new Set();
|
|
267
|
-
const queue = [...direct, ...cooks.flatMap((cook) => [...inputsOf(cook).keys()])];
|
|
268
|
-
for (let resource = queue.shift(); resource !== void 0; resource = queue.shift()) {
|
|
269
|
-
if (out.has(resource)) continue;
|
|
270
|
-
out.add(resource);
|
|
271
|
-
for (const maker of makers) if (giftResource(maker) === resource) queue.push(...inputsOf(maker).keys());
|
|
272
|
-
}
|
|
273
|
-
return out;
|
|
274
|
-
}
|
|
275
|
-
/** This function gives the resources that the draws of `needs` can use, and the converters that they can use. */
|
|
276
|
-
function reachOf(parts, needs) {
|
|
277
|
-
const cooks = parts.cooks.filter((cook) => needs.has(giftNeed(cook) ?? ""));
|
|
278
|
-
const closure = inputClosure$1(parts.direct.filter((use) => needs.has(use.need)).map((use) => use.resource), cooks, parts.makers);
|
|
279
|
-
return {
|
|
280
|
-
closure,
|
|
281
|
-
reach: /* @__PURE__ */ new Set([...cooks, ...parts.makers.filter((maker) => closure.has(giftResource(maker) ?? ""))])
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
// src/seasonal.ts
|
|
285
|
-
/** This function gives the least of a list of rationals, or `none` for an empty list. */
|
|
286
|
-
function leastOf(values, none) {
|
|
287
|
-
let low;
|
|
288
|
-
for (const value of values) if (low === void 0 || sign(sub(value, low)) < 0) low = value;
|
|
289
|
-
return low ?? none;
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* This function gives the capacity of each resource whose stores have a capacity (M2-solar-charge). A store whose part is
|
|
293
|
-
* in `stoppedParts` has no capacity. A store with a due date has no charge: the gate refuses a capacity with a due date.
|
|
294
|
-
*/
|
|
295
|
-
function storeCapacities(layer, stoppedParts) {
|
|
296
|
-
const out = /* @__PURE__ */ new Map();
|
|
297
|
-
for (const store of layer.stores) {
|
|
298
|
-
if (store.capacity === void 0 || store.due !== void 0) continue;
|
|
299
|
-
if (store.part !== void 0 && stoppedParts.has(store.part)) continue;
|
|
300
|
-
out.set(store.resource, (out.get(store.resource) ?? 0) + store.capacity);
|
|
301
|
-
}
|
|
302
|
-
return out;
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* This function gives the parts of the programs of a case with a season. `parts` are the parts of the case with the
|
|
306
|
-
* stock after the sowings (`seasonSupply`).
|
|
307
|
-
*
|
|
308
|
-
* @param rests - The rest of each store with a capacity that the higher draws of the sweep leave (M2-solar-charge-sweep).
|
|
309
|
-
* A segment gets the least rest of the spans that it touches. This is the safe side.
|
|
310
|
-
*/
|
|
311
|
-
function seasonPlan(supply, parts, caseCuts, draws, devices, capacities = /* @__PURE__ */ new Map(), rests = /* @__PURE__ */ new Map()) {
|
|
312
|
-
const total = new Map(supply.stock);
|
|
313
|
-
const harvests = supply.lots.map((lot, origin) => ({
|
|
314
|
-
...lot,
|
|
315
|
-
origin
|
|
316
|
-
})).filter((lot) => parts.resources.has(lot.resource));
|
|
317
|
-
for (const lot of harvests) total.set(lot.resource, (total.get(lot.resource) ?? 0) + lot.amount);
|
|
318
|
-
const found = partsOf(total, parts.direct, parts.cooks, parts.makers);
|
|
319
|
-
const sourcesOf = (resource) => found.makers.filter((maker) => isSource(maker) && giftResource(maker) === resource);
|
|
320
|
-
const charged = [...capacities.keys()].filter((resource) => found.resources.has(resource) && sourcesOf(resource).length > 0).sort();
|
|
321
|
-
const all = {
|
|
322
|
-
...found,
|
|
323
|
-
stocked: [.../* @__PURE__ */ new Set([...found.stocked, ...charged])].sort()
|
|
324
|
-
};
|
|
325
|
-
const storeLot = (resource) => ({
|
|
326
|
-
resource,
|
|
327
|
-
day: 0,
|
|
328
|
-
spoils: 1e400,
|
|
329
|
-
amount: exact(supply.stock.get(resource) ?? 0)
|
|
330
|
-
});
|
|
331
|
-
const lots = [...all.stocked.filter((resource) => (supply.stock.get(resource) ?? 0) > 0 || charged.includes(resource)).map(storeLot), ...harvests.map((lot) => ({
|
|
332
|
-
resource: lot.resource,
|
|
333
|
-
day: Math.max(0, lot.day),
|
|
334
|
-
spoils: lot.spoils,
|
|
335
|
-
amount: exact(lot.amount),
|
|
336
|
-
origin: lot.origin
|
|
337
|
-
}))];
|
|
338
|
-
const inside = (day) => day > 0 && day < supply.horizon;
|
|
339
|
-
const shares = monthlyShares([...all.cooks, ...all.makers]);
|
|
340
|
-
const months = shares.size === 0 ? [] : monthEdges(supply.start, supply.horizon);
|
|
341
|
-
const edges = [.../* @__PURE__ */ new Set([
|
|
342
|
-
0,
|
|
343
|
-
supply.horizon,
|
|
344
|
-
...months,
|
|
345
|
-
...lots.flatMap((lot) => [lot.day, lot.spoils]).filter(inside)
|
|
346
|
-
])].sort((a, b) => a - b);
|
|
347
|
-
const charges = charged.map((resource) => {
|
|
348
|
-
const lot = lots.find((item) => item.resource === resource && item.origin === void 0);
|
|
349
|
-
const capacity = exact(Math.max(capacities.get(resource) ?? 0, toNumber(lot.amount)));
|
|
350
|
-
const rest = rests.get(resource) ?? [];
|
|
351
|
-
const spans = edges.slice(0, -1).map((from, index) => rest.filter((item) => item.from < (edges[index + 1] ?? 1e400) && item.to > from));
|
|
352
|
-
const take = spans.map((list) => leastOf(list.map((item) => item.take), lot.amount));
|
|
353
|
-
const fill = spans.map((list) => leastOf(list.map((item) => item.fill), sub(capacity, lot.amount)));
|
|
354
|
-
return {
|
|
355
|
-
resource,
|
|
356
|
-
capacity,
|
|
357
|
-
lot,
|
|
358
|
-
sources: sourcesOf(resource),
|
|
359
|
-
take,
|
|
360
|
-
fill,
|
|
361
|
-
higher: rest.length > 0
|
|
362
|
-
};
|
|
363
|
-
});
|
|
364
|
-
return {
|
|
365
|
-
parts: all,
|
|
366
|
-
caseCuts,
|
|
367
|
-
draws,
|
|
368
|
-
devices,
|
|
369
|
-
lots,
|
|
370
|
-
edges,
|
|
371
|
-
shares,
|
|
372
|
-
months: edges.slice(0, -1).map((edge) => shares.size === 0 ? 0 : monthOf(supply.start, edge)),
|
|
373
|
-
charges
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
const edgeOf = (plan, index) => exact(plan.edges[index] ?? 0);
|
|
377
|
-
const lengthOf = (plan, index) => sub(edgeOf(plan, index + 1), edgeOf(plan, index));
|
|
378
|
-
const segmentsOf = (plan) => plan.edges.length - 1;
|
|
379
|
-
/** 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. */
|
|
380
|
-
function fixedFed(plan, fix, segment) {
|
|
381
|
-
if (segment < fix.segment) return lengthOf(plan, segment);
|
|
382
|
-
if (segment === fix.segment) return sub(fix.stop, edgeOf(plan, segment));
|
|
383
|
-
return ZERO;
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* 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.
|
|
387
|
-
* The share of each stock is the sum of the takes of the lots that are ready and not spoiled in the whole segment.
|
|
388
|
-
*/
|
|
389
|
-
function addCell(program, plan, draw, segment, fed) {
|
|
390
|
-
const block = drawBlock(program, plan.parts, draw);
|
|
391
|
-
const demand = exact(drawDemand(draw, plan.caseCuts));
|
|
392
|
-
const need = new Map(block.gifts);
|
|
393
|
-
if (typeof fed === "number") {
|
|
394
|
-
addTo(need, fed, neg(demand));
|
|
395
|
-
program.push(need, ">=", ZERO);
|
|
396
|
-
} else program.push(need, ">=", mul(demand, fed));
|
|
397
|
-
const from = plan.edges[segment] ?? 0;
|
|
398
|
-
const to = plan.edges[segment + 1] ?? 1e400;
|
|
399
|
-
const takes = /* @__PURE__ */ new Map();
|
|
400
|
-
for (const [resource, share] of block.shares) {
|
|
401
|
-
const row = /* @__PURE__ */ new Map([[share, ONE]]);
|
|
402
|
-
for (const lot of plan.lots) {
|
|
403
|
-
if (lot.resource !== resource || lot.day > from || lot.spoils < to) continue;
|
|
404
|
-
const column = program.next();
|
|
405
|
-
takes.set(lot, column);
|
|
406
|
-
addTo(row, column, neg(ONE));
|
|
407
|
-
}
|
|
408
|
-
program.push(row, "=", ZERO);
|
|
409
|
-
}
|
|
410
|
-
return {
|
|
411
|
-
draw,
|
|
412
|
-
segment,
|
|
413
|
-
block,
|
|
414
|
-
fed,
|
|
415
|
-
takes
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
/** This function adds the row of each lot: the takes of all the cells are not more than its amount. The rows of `addCharges` cover the lot of a charge. */
|
|
419
|
-
function lotRows(program, plan, cells) {
|
|
420
|
-
const charged = new Set(plan.charges.map((charge) => charge.lot));
|
|
421
|
-
for (const lot of plan.lots) {
|
|
422
|
-
if (charged.has(lot)) continue;
|
|
423
|
-
const row = /* @__PURE__ */ new Map();
|
|
424
|
-
for (const cell of cells) {
|
|
425
|
-
const column = cell.takes.get(lot);
|
|
426
|
-
if (column !== void 0) addTo(row, column, ONE);
|
|
427
|
-
}
|
|
428
|
-
if (row.size > 0) program.push(row, "<=", lot.amount);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* This function adds the charge of each store with a capacity in the segments before `through` (M2-solar-charge).
|
|
433
|
-
* Each source has one charge column in each segment before the last one: no take of the program uses the charge of the last one.
|
|
434
|
-
* The level at the start of a segment is the amount of the lot plus the charges of the segments before it, less their takes.
|
|
435
|
-
* - The takes of a segment are not more than the level at its start.
|
|
436
|
-
* - The level at the start of a segment plus its charge is not more than the capacity.
|
|
437
|
-
* Thus each order of the takes and the charges in a segment is a plan. The caller adds the charge columns to the rows of the device-days.
|
|
438
|
-
*
|
|
439
|
-
* With no higher draws, the right side of the takes is the amount of the lot. The right side of the charge is the capacity less that amount.
|
|
440
|
-
* With higher draws (M2-solar-charge-sweep), the right sides are the rest that they leave (`PlanCharge.take` and `fill`).
|
|
441
|
-
* Then each segment has the row of its charge, and two more rows keep the level after `through` in the rest of the later segments.
|
|
442
|
-
*/
|
|
443
|
-
function addCharges(program, plan, cells, through) {
|
|
444
|
-
const pairs = /* @__PURE__ */ new Map();
|
|
445
|
-
const gifts = /* @__PURE__ */ new Map();
|
|
446
|
-
const segments = /* @__PURE__ */ new Map();
|
|
447
|
-
for (const charge of plan.charges) {
|
|
448
|
-
const level = /* @__PURE__ */ new Map();
|
|
449
|
-
const given = /* @__PURE__ */ new Map();
|
|
450
|
-
const each = [];
|
|
451
|
-
gifts.set(charge.resource, given);
|
|
452
|
-
segments.set(charge.resource, each);
|
|
453
|
-
for (let segment = 0; segment < through; segment += 1) {
|
|
454
|
-
const takes = /* @__PURE__ */ new Map();
|
|
455
|
-
for (const cell of cells) {
|
|
456
|
-
const column = cell.segment === segment ? cell.takes.get(charge.lot) : void 0;
|
|
457
|
-
if (column !== void 0) addTo(takes, column, ONE);
|
|
458
|
-
}
|
|
459
|
-
const most = new Map(takes);
|
|
460
|
-
for (const [column, value] of level) addTo(most, column, neg(value));
|
|
461
|
-
program.push(most, "<=", charge.take[segment] ?? charge.lot.amount);
|
|
462
|
-
const full = new Map(level);
|
|
463
|
-
const own = /* @__PURE__ */ new Map();
|
|
464
|
-
each.push(own);
|
|
465
|
-
if (segment + 1 < through) {
|
|
466
|
-
for (const source of charge.sources) {
|
|
467
|
-
const column = program.next();
|
|
468
|
-
pairs.set(segment, [...pairs.get(segment) ?? [], [source, column]]);
|
|
469
|
-
const amount = exact(source.gives.amount);
|
|
470
|
-
addTo(full, column, amount);
|
|
471
|
-
addTo(level, column, amount);
|
|
472
|
-
addTo(given, column, amount);
|
|
473
|
-
addTo(own, column, amount);
|
|
474
|
-
}
|
|
475
|
-
program.push(full, "<=", charge.fill[segment] ?? sub(charge.capacity, charge.lot.amount));
|
|
476
|
-
} else if (charge.higher) program.push(full, "<=", charge.fill[segment] ?? ZERO);
|
|
477
|
-
for (const [column, value] of takes) addTo(level, column, neg(value));
|
|
478
|
-
}
|
|
479
|
-
if (charge.higher && through < segmentsOf(plan)) {
|
|
480
|
-
const less = /* @__PURE__ */ new Map();
|
|
481
|
-
for (const [column, value] of level) addTo(less, column, neg(value));
|
|
482
|
-
program.push(less, "<=", leastOf(charge.take.slice(through), ZERO));
|
|
483
|
-
program.push(new Map(level), "<=", leastOf(charge.fill.slice(through), ZERO));
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
return {
|
|
487
|
-
pairs,
|
|
488
|
-
gifts,
|
|
489
|
-
segments
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
* This function gives the most charge, the earliest first, as an objective (M2-solar-charge-sweep). The weight of the charge of
|
|
494
|
-
* a segment is the days from its start to the horizon. Thus the store keeps the highest level that it can for the lower draws.
|
|
495
|
-
*/
|
|
496
|
-
function mostCharge(plan, charges, count) {
|
|
497
|
-
const most = /* @__PURE__ */ new Map();
|
|
498
|
-
const horizon = edgeOf(plan, segmentsOf(plan));
|
|
499
|
-
for (const rows of charges.segments.values()) rows.forEach((row, segment) => {
|
|
500
|
-
for (const [column, value] of row) addTo(most, column, mul(value, sub(horizon, edgeOf(plan, segment))));
|
|
501
|
-
});
|
|
502
|
-
return {
|
|
503
|
-
sense: "max",
|
|
504
|
-
coefficients: dense(most, count)
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* This function gives the rest of each store with a capacity in each segment (M2-solar-charge-sweep). It is the rest that the
|
|
509
|
-
* draws of a plan and the higher draws leave. The takes and the charges of the plan come out of the rest that the plan got.
|
|
510
|
-
*/
|
|
511
|
-
function restsOf(plan, cells, x, charges) {
|
|
512
|
-
const out = /* @__PURE__ */ new Map();
|
|
513
|
-
for (const charge of plan.charges) {
|
|
514
|
-
const rows = charges.segments.get(charge.resource) ?? [];
|
|
515
|
-
let added = ZERO;
|
|
516
|
-
const list = [];
|
|
517
|
-
for (let segment = 0; segment < segmentsOf(plan); segment += 1) {
|
|
518
|
-
let taken = ZERO;
|
|
519
|
-
for (const cell of cells) if (cell.segment === segment) taken = add(taken, x[cell.takes.get(charge.lot) ?? -1] ?? ZERO);
|
|
520
|
-
const row = rows[segment];
|
|
521
|
-
const given = row === void 0 ? ZERO : valueAt(row, x);
|
|
522
|
-
const take = add(sub(charge.take[segment] ?? charge.lot.amount, taken), added);
|
|
523
|
-
const fill = sub(sub(charge.fill[segment] ?? ZERO, given), added);
|
|
524
|
-
list.push({
|
|
525
|
-
from: plan.edges[segment] ?? 0,
|
|
526
|
-
to: plan.edges[segment + 1] ?? 0,
|
|
527
|
-
take,
|
|
528
|
-
fill
|
|
529
|
-
});
|
|
530
|
-
added = add(added, sub(given, taken));
|
|
531
|
-
}
|
|
532
|
-
out.set(charge.resource, list);
|
|
533
|
-
}
|
|
534
|
-
return out;
|
|
535
|
-
}
|
|
536
|
-
/**
|
|
537
|
-
* This function gives the most device-days of one day that the charges of a plan use, by the key of the device (M2-solar-charge-sweep).
|
|
538
|
-
* The value comes before the shares `monthly`. The charge of a segment can come in equal parts on each of its days.
|
|
539
|
-
*/
|
|
540
|
-
function chargeDaysOf(plan, charges, x) {
|
|
541
|
-
const out = /* @__PURE__ */ new Map();
|
|
542
|
-
for (const [segment, list] of charges.pairs) {
|
|
543
|
-
const length = toNumber(lengthOf(plan, segment));
|
|
544
|
-
if (!(length > 0)) continue;
|
|
545
|
-
for (const [key, limit] of deviceUse(list).limits) {
|
|
546
|
-
const share = shareIn(plan, segment, key);
|
|
547
|
-
if (share > 0) out.set(key, Math.max(out.get(key) ?? 0, toNumber(valueAt(limit.row, x)) / length / share));
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
return out;
|
|
551
|
-
}
|
|
552
|
-
/**
|
|
553
|
-
* 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
|
|
554
|
-
* 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.
|
|
555
|
-
*/
|
|
556
|
-
function addFixed(program, plan, fixed, cells) {
|
|
557
|
-
const kept = /* @__PURE__ */ new Map();
|
|
558
|
-
for (const draw of plan.draws) {
|
|
559
|
-
const fix = fixed.get(draw.need);
|
|
560
|
-
if (fix === void 0) continue;
|
|
561
|
-
for (let segment = 0; segment <= fix.segment; segment += 1) {
|
|
562
|
-
const fed = fixedFed(plan, fix, segment);
|
|
563
|
-
const keep = fix.keep.get(segment) ?? /* @__PURE__ */ new Map();
|
|
564
|
-
const into = kept.get(segment) ?? /* @__PURE__ */ new Map();
|
|
565
|
-
kept.set(segment, into);
|
|
566
|
-
for (const [key, value] of keep) into.set(key, add(into.get(key) ?? ZERO, value));
|
|
567
|
-
if (sign(fed) <= 0) continue;
|
|
568
|
-
const cell = addCell(program, plan, draw, segment, fed);
|
|
569
|
-
cells.push(cell);
|
|
570
|
-
const { limits, stopped } = deviceUse(cell.block.runs);
|
|
571
|
-
for (const index of stopped) program.push(/* @__PURE__ */ new Map([[index, ONE]]), "<=", ZERO);
|
|
572
|
-
for (const [key, limit] of limits) program.push(limit.row, "<=", mul(keep.get(key) ?? ZERO, fed));
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
return kept;
|
|
576
|
-
}
|
|
577
|
-
/** The share of the device-days of a key in a segment (M2-solar): its share `monthly` in the month of the segment, or 1. */
|
|
578
|
-
function shareIn(plan, segment, key) {
|
|
579
|
-
return shareOf(plan.shares, key, plan.months[segment] ?? 0);
|
|
580
|
-
}
|
|
581
|
-
/** This function gives the device-days of each day that the fixed draws leave in a segment: the devices times the share, less the kept device-days. */
|
|
582
|
-
function roomOf(plan, kept, segment, key, devices) {
|
|
583
|
-
const left = sub(mul(exact(plan.devices.get(key) ?? devices), exact(shareIn(plan, segment, key))), kept.get(segment)?.get(key) ?? ZERO);
|
|
584
|
-
return sign(left) > 0 ? left : ZERO;
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* This function gives the program of one step with the stop of the open draws in segment `segment`. The open draws are fed in
|
|
588
|
-
* each segment before it. In it, each one is fed for `t - edge` days or more, and their runs share the device-days that
|
|
589
|
-
* the fixed draws leave, for `t - edge` days. This is the safe side, as in `bestEndurance`.
|
|
590
|
-
*/
|
|
591
|
-
function buildStep(plan, fixed, open, segment) {
|
|
592
|
-
const program = new Program();
|
|
593
|
-
const t = program.next();
|
|
594
|
-
program.push(/* @__PURE__ */ new Map([[t, ONE]]), "<=", edgeOf(plan, segment + 1));
|
|
595
|
-
const cells = [];
|
|
596
|
-
const kept = addFixed(program, plan, fixed, cells);
|
|
597
|
-
const opened = new Set(open.map((draw) => draw.need));
|
|
598
|
-
const floors = /* @__PURE__ */ new Map();
|
|
599
|
-
for (const draw of plan.draws) {
|
|
600
|
-
if (!opened.has(draw.need)) continue;
|
|
601
|
-
for (let index = 0; index < segment; index += 1) cells.push(addCell(program, plan, draw, index, lengthOf(plan, index)));
|
|
602
|
-
const fed = program.next();
|
|
603
|
-
program.push(/* @__PURE__ */ new Map([[fed, ONE]]), "<=", lengthOf(plan, segment));
|
|
604
|
-
floors.set(draw.need, program.push(/* @__PURE__ */ new Map([[fed, ONE], [t, neg(ONE)]]), ">=", neg(edgeOf(plan, segment))));
|
|
605
|
-
cells.push(addCell(program, plan, draw, segment, fed));
|
|
606
|
-
}
|
|
607
|
-
const charges = addCharges(program, plan, cells, segment + 1);
|
|
608
|
-
const shared = /* @__PURE__ */ new Map();
|
|
609
|
-
for (let index = 0; index <= segment; index += 1) {
|
|
610
|
-
const { limits, stopped } = deviceUse([...cells.filter((cell) => cell.segment === index && opened.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs]), ...charges.pairs.get(index) ?? []]);
|
|
611
|
-
for (const column of stopped) program.push(/* @__PURE__ */ new Map([[column, ONE]]), "<=", ZERO);
|
|
612
|
-
const bySegment = /* @__PURE__ */ new Map();
|
|
613
|
-
shared.set(index, bySegment);
|
|
614
|
-
for (const [key, limit] of limits) {
|
|
615
|
-
const room = roomOf(plan, kept, index, key, limit.devices);
|
|
616
|
-
bySegment.set(key, {
|
|
617
|
-
row: limit.row,
|
|
618
|
-
room,
|
|
619
|
-
converters: limit.converters
|
|
620
|
-
});
|
|
621
|
-
const row = new Map(limit.row);
|
|
622
|
-
if (index < segment) program.push(row, "<=", mul(room, lengthOf(plan, index)));
|
|
623
|
-
else {
|
|
624
|
-
addTo(row, t, neg(room));
|
|
625
|
-
program.push(row, "<=", neg(mul(room, edgeOf(plan, segment))));
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
lotRows(program, plan, cells);
|
|
630
|
-
return {
|
|
631
|
-
program,
|
|
632
|
-
t,
|
|
633
|
-
cells,
|
|
634
|
-
floors,
|
|
635
|
-
shared,
|
|
636
|
-
segment,
|
|
637
|
-
charges
|
|
638
|
-
};
|
|
639
|
-
}
|
|
640
|
-
/**
|
|
641
|
-
* This function gives the largest common first stop of the open draws, with the fixed draws at their stops. The days to
|
|
642
|
-
* a first stop are not a linear function of the takes, so the function tries the segments in order. When the days reach
|
|
643
|
-
* the end of a segment, it tries the next segment. The days of the last step are exact.
|
|
644
|
-
* After the largest days, the step takes the objectives of `tail`.
|
|
645
|
-
*/
|
|
646
|
-
function firstStop(plan, fixed, open, tail = () => []) {
|
|
647
|
-
const segments = segmentsOf(plan);
|
|
648
|
-
for (let segment = 0; segment < segments; segment += 1) {
|
|
649
|
-
const step = buildStep(plan, fixed, open, segment);
|
|
650
|
-
const most = {
|
|
651
|
-
sense: "max",
|
|
652
|
-
coefficients: dense(/* @__PURE__ */ new Map([[step.t, ONE]]), step.program.count)
|
|
653
|
-
};
|
|
654
|
-
const solved = solveLexicographic(step.program.constraints(), [most]);
|
|
655
|
-
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.");
|
|
656
|
-
const days = solved.values[0] ?? ZERO;
|
|
657
|
-
const end = edgeOf(plan, segment + 1);
|
|
658
|
-
if (segment + 1 < segments && equals(days, end)) continue;
|
|
659
|
-
const later = tail(step);
|
|
660
|
-
const full = later.length === 0 ? solved : solveLexicographic([...step.program.constraints(), {
|
|
661
|
-
coefficients: most.coefficients,
|
|
662
|
-
relation: "=",
|
|
663
|
-
rhs: days
|
|
664
|
-
}], later, { warm: true });
|
|
665
|
-
if (full.status !== "optimal") throw new Error("The step of a season has no optimum for its later objectives.");
|
|
666
|
-
const first = solved.stages[0]?.result;
|
|
667
|
-
return {
|
|
668
|
-
step,
|
|
669
|
-
x: full.x,
|
|
670
|
-
dual: first?.status === "optimal" ? first.dual : [],
|
|
671
|
-
days,
|
|
672
|
-
atHorizon: segment + 1 === segments && equals(days, end)
|
|
673
|
-
};
|
|
674
|
-
}
|
|
675
|
-
throw new Error("A season plan has no segment.");
|
|
676
|
-
}
|
|
677
|
-
/** This function gives the fewest device-days of the runs of the open cells of a step, as an objective. */
|
|
678
|
-
function fewestOpen(step, open) {
|
|
679
|
-
return {
|
|
680
|
-
sense: "min",
|
|
681
|
-
coefficients: dense(fewestRow(step.cells.filter((cell) => open.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs])), step.program.count)
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
/** This function gives the smallest sum of the shares of the stocks, each over the total of its lots, as an objective. */
|
|
685
|
-
function leastShares(plan, cells, count) {
|
|
686
|
-
const least = /* @__PURE__ */ new Map();
|
|
687
|
-
const weight = (resource) => div(ONE, exact((plan.parts.stock.get(resource) ?? 0) > 0 ? plan.parts.stock.get(resource) : 1));
|
|
688
|
-
for (const cell of cells) for (const [resource, index] of cell.block.shares) addTo(least, index, weight(resource));
|
|
689
|
-
return {
|
|
690
|
-
sense: "min",
|
|
691
|
-
coefficients: dense(least, count)
|
|
692
|
-
};
|
|
693
|
-
}
|
|
694
|
-
/** This function fixes the open draws of `done` at the stop: the device-days of each day that they keep, and their capped converters. */
|
|
695
|
-
function fixStops(plan, stop, done, fixed) {
|
|
696
|
-
const { step, x, days } = stop;
|
|
697
|
-
const fedIn = (segment) => segment < step.segment ? lengthOf(plan, segment) : sub(days, edgeOf(plan, step.segment));
|
|
698
|
-
const full = /* @__PURE__ */ new Set();
|
|
699
|
-
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}`);
|
|
700
|
-
for (const draw of done) {
|
|
701
|
-
const keep = /* @__PURE__ */ new Map();
|
|
702
|
-
const used = /* @__PURE__ */ new Set();
|
|
703
|
-
for (const cell of step.cells) {
|
|
704
|
-
if (cell.draw.need !== draw.need) continue;
|
|
705
|
-
const fed = fedIn(cell.segment);
|
|
706
|
-
const bySegment = /* @__PURE__ */ new Map();
|
|
707
|
-
keep.set(cell.segment, bySegment);
|
|
708
|
-
for (const [key, limit] of deviceUse(cell.block.runs).limits) {
|
|
709
|
-
bySegment.set(key, sign(fed) > 0 ? div(valueAt(limit.row, x), fed) : ZERO);
|
|
710
|
-
if (full.has(`${String(cell.segment)}\u0000${key}`)) for (const converter of limit.converters) used.add(converter);
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
const { reach } = reachOf(plan.parts, /* @__PURE__ */ new Set([draw.need]));
|
|
714
|
-
const capped = sign(days) > 0 && !stop.atHorizon ? [...reach].filter((converter) => converter.perDay !== void 0 && (!(converter.perDay > 0) || used.has(converter))).map((converter) => converter.id).sort() : [];
|
|
715
|
-
fixed.set(draw.need, {
|
|
716
|
-
stop: days,
|
|
717
|
-
segment: step.segment,
|
|
718
|
-
keep,
|
|
719
|
-
atHorizon: stop.atHorizon,
|
|
720
|
-
capped
|
|
721
|
-
});
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
/** This function gives the sum of the take of each resource of the cells of one need, more than 0 only. */
|
|
725
|
-
function takesOf(cells, need, x) {
|
|
726
|
-
const sums = /* @__PURE__ */ new Map();
|
|
727
|
-
for (const cell of cells) {
|
|
728
|
-
if (cell.draw.need !== need) continue;
|
|
729
|
-
for (const [resource, index] of cell.block.shares) sums.set(resource, add(sums.get(resource) ?? ZERO, x[index] ?? ZERO));
|
|
730
|
-
}
|
|
731
|
-
const out = {};
|
|
732
|
-
for (const [resource, value] of [...sums].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) if (sign(value) > 0) out[resource] = toNumber(value);
|
|
733
|
-
return out;
|
|
734
|
-
}
|
|
735
|
-
/** 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. */
|
|
736
|
-
function usedUp(plan, cells, x, closure, stored, charges) {
|
|
737
|
-
return [...closure].filter((resource) => stored.has(resource)).filter((resource) => {
|
|
738
|
-
let taken = ZERO;
|
|
739
|
-
const given = charges?.gifts.get(resource);
|
|
740
|
-
let amount = given === void 0 ? ZERO : valueAt(given, x);
|
|
741
|
-
for (const lot of plan.lots) {
|
|
742
|
-
if (lot.resource !== resource) continue;
|
|
743
|
-
amount = add(amount, lot.amount);
|
|
744
|
-
for (const cell of cells) taken = add(taken, x[cell.takes.get(lot) ?? -1] ?? ZERO);
|
|
745
|
-
}
|
|
746
|
-
return equals(taken, amount);
|
|
747
|
-
}).sort();
|
|
748
|
-
}
|
|
749
|
-
/** This function tells if the cells of two draws or more of `cells` can use the limited device `key`. */
|
|
750
|
-
function sharedKey(cells, key) {
|
|
751
|
-
return new Set(cells.filter((cell) => deviceUse(cell.block.runs).limits.has(key)).map((cell) => cell.draw.need)).size > 1;
|
|
752
|
-
}
|
|
753
|
-
/**
|
|
754
|
-
* This function gives the last program of the best endurance with a season. Each draw keeps its first stop. After it, a later
|
|
755
|
-
* harvest can feed the draw again (`again`). The objectives come in this order:
|
|
756
|
-
* - the most fed days after the stops for each level, in the order of the levels.
|
|
757
|
-
* - the earliest fed days.
|
|
758
|
-
* - the smallest sum of the shares of the stocks.
|
|
759
|
-
* - the fewest device-days.
|
|
760
|
-
*
|
|
761
|
-
* In a segment after the stops, the cells of two draws or more that can use one limited device take turns. Their fed days
|
|
762
|
-
* together are not more than the segment, and each one has the device-days of each day that the stops leave. This is the
|
|
763
|
-
* safe side: the cells do not use one device at the same time. Each other cell starts at the start of the segment.
|
|
764
|
-
*
|
|
765
|
-
* @param fill - True for the sweep of `enduranceOf` (M2-solar-charge-sweep). Then the last objective is the most charge, the
|
|
766
|
-
* earliest first, and the result gives the rest of each store with a capacity. A store with a capacity is binding if the
|
|
767
|
-
* takes of the segment of the stop use all the level at its start.
|
|
768
|
-
*/
|
|
769
|
-
function seasonBest(plan, fixed, levels, stored, fill = false) {
|
|
770
|
-
const program = new Program();
|
|
771
|
-
const cells = [];
|
|
772
|
-
const kept = addFixed(program, plan, fixed, cells);
|
|
773
|
-
const segments = segmentsOf(plan);
|
|
774
|
-
const again = /* @__PURE__ */ new Map();
|
|
775
|
-
const taking = /* @__PURE__ */ new Set();
|
|
776
|
-
for (const draw of plan.draws) {
|
|
777
|
-
const fix = fixed.get(draw.need);
|
|
778
|
-
for (let segment = fix.segment + 1; segment < segments; segment += 1) {
|
|
779
|
-
const fed = program.next();
|
|
780
|
-
program.push(/* @__PURE__ */ new Map([[fed, ONE]]), "<=", lengthOf(plan, segment));
|
|
781
|
-
const cell = addCell(program, plan, draw, segment, fed);
|
|
782
|
-
cells.push(cell);
|
|
783
|
-
again.set(cell, fed);
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
for (let segment = 0; segment < segments; segment += 1) {
|
|
787
|
-
const later = [...again.keys()].filter((cell) => cell.segment === segment);
|
|
788
|
-
const { limits, stopped } = deviceUse(later.flatMap((cell) => [...cell.block.runs]));
|
|
789
|
-
for (const column of stopped) program.push(/* @__PURE__ */ new Map([[column, ONE]]), "<=", ZERO);
|
|
790
|
-
const turns = new Set(later.filter((cell) => [...deviceUse(cell.block.runs).limits.keys()].some((key) => sharedKey(later, key))));
|
|
791
|
-
if (turns.size > 0) {
|
|
792
|
-
const row = /* @__PURE__ */ new Map();
|
|
793
|
-
for (const cell of turns) addTo(row, again.get(cell), ONE);
|
|
794
|
-
program.push(row, "<=", lengthOf(plan, segment));
|
|
795
|
-
for (const cell of turns) taking.add(cell);
|
|
796
|
-
}
|
|
797
|
-
for (const [key, limit] of limits) {
|
|
798
|
-
const room = roomOf(plan, kept, segment, key, limit.devices);
|
|
799
|
-
for (const cell of later) {
|
|
800
|
-
const own = deviceUse(cell.block.runs).limits.get(key);
|
|
801
|
-
if (own === void 0) continue;
|
|
802
|
-
const row = new Map(own.row);
|
|
803
|
-
addTo(row, again.get(cell), neg(room));
|
|
804
|
-
program.push(row, "<=", ZERO);
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
const charges = addCharges(program, plan, cells, segments);
|
|
809
|
-
for (const [segment, list] of charges.pairs) {
|
|
810
|
-
const { limits } = deviceUse([...[...again.keys()].filter((cell) => cell.segment === segment).flatMap((cell) => [...cell.block.runs]), ...list]);
|
|
811
|
-
const keys = new Set(list.map(([source]) => deviceKey(source)));
|
|
812
|
-
for (const [key, limit] of limits) if (keys.has(key)) program.push(limit.row, "<=", mul(roomOf(plan, kept, segment, key, limit.devices), lengthOf(plan, segment)));
|
|
813
|
-
}
|
|
814
|
-
lotRows(program, plan, cells);
|
|
815
|
-
const objectives = levels.map((level) => {
|
|
816
|
-
const needs = new Set(level.map((draw) => draw.need));
|
|
817
|
-
const row = /* @__PURE__ */ new Map();
|
|
818
|
-
for (const [cell, fed] of again) if (needs.has(cell.draw.need)) addTo(row, fed, ONE);
|
|
819
|
-
return {
|
|
820
|
-
sense: "max",
|
|
821
|
-
coefficients: dense(row, program.count)
|
|
822
|
-
};
|
|
823
|
-
});
|
|
824
|
-
const early = /* @__PURE__ */ new Map();
|
|
825
|
-
const horizon = edgeOf(plan, segments);
|
|
826
|
-
for (const [cell, fed] of again) addTo(early, fed, sub(horizon, edgeOf(plan, cell.segment)));
|
|
827
|
-
objectives.push({
|
|
828
|
-
sense: "max",
|
|
829
|
-
coefficients: dense(early, program.count)
|
|
830
|
-
});
|
|
831
|
-
objectives.push(leastShares(plan, cells, program.count));
|
|
832
|
-
objectives.push({
|
|
833
|
-
sense: "min",
|
|
834
|
-
coefficients: dense(fewestRow(cells.flatMap((cell) => [...cell.block.runs])), program.count)
|
|
835
|
-
});
|
|
836
|
-
if (fill) objectives.push(mostCharge(plan, charges, program.count));
|
|
837
|
-
const solved = solveLexicographic(program.constraints(), objectives, { warm: true });
|
|
838
|
-
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.");
|
|
839
|
-
const x = solved.x;
|
|
840
|
-
const rests = fill ? restsOf(plan, cells, x, charges) : void 0;
|
|
841
|
-
const charged = new Set(plan.charges.map((charge) => charge.resource));
|
|
842
|
-
const bound = (closure, segment) => {
|
|
843
|
-
const used = usedUp(plan, cells, x, closure, stored, charges);
|
|
844
|
-
if (rests === void 0) return used;
|
|
845
|
-
const empty = [...charged].filter((resource) => closure.has(resource) && stored.has(resource) && sign(rests.get(resource)?.[segment]?.take ?? ONE) <= 0);
|
|
846
|
-
return [...used.filter((resource) => !charged.has(resource)), ...empty].sort();
|
|
847
|
-
};
|
|
848
|
-
const turn = /* @__PURE__ */ new Map();
|
|
849
|
-
const offset = /* @__PURE__ */ new Map();
|
|
850
|
-
for (const cell of [...taking].sort((a, b) => plan.draws.indexOf(a.draw) - plan.draws.indexOf(b.draw))) {
|
|
851
|
-
const start = offset.get(cell.segment) ?? ZERO;
|
|
852
|
-
turn.set(cell, start);
|
|
853
|
-
offset.set(cell.segment, add(start, x[again.get(cell)] ?? ZERO));
|
|
854
|
-
}
|
|
855
|
-
const needs = {};
|
|
856
|
-
for (const draw of plan.draws) {
|
|
857
|
-
const fix = fixed.get(draw.need);
|
|
858
|
-
const spans = [];
|
|
859
|
-
for (const [cell, fed] of again) {
|
|
860
|
-
if (cell.draw.need !== draw.need) continue;
|
|
861
|
-
const value = x[fed] ?? ZERO;
|
|
862
|
-
if (sign(value) <= 0) continue;
|
|
863
|
-
const start = add(edgeOf(plan, cell.segment), taking.has(cell) ? turn.get(cell) ?? ZERO : ZERO);
|
|
864
|
-
const from = toNumber(start);
|
|
865
|
-
const to = toNumber(add(start, value));
|
|
866
|
-
const last = spans[spans.length - 1];
|
|
867
|
-
if (last !== void 0 && last.to === from) last.to = to;
|
|
868
|
-
else spans.push({
|
|
869
|
-
from,
|
|
870
|
-
to
|
|
871
|
-
});
|
|
872
|
-
}
|
|
873
|
-
const { closure } = reachOf(plan.parts, /* @__PURE__ */ new Set([draw.need]));
|
|
874
|
-
needs[draw.need] = {
|
|
875
|
-
days: toNumber(fix.stop),
|
|
876
|
-
binding: fix.atHorizon ? [] : bound(closure, fix.segment),
|
|
877
|
-
takes: takesOf(cells, draw.need, x),
|
|
878
|
-
...fix.capped.length === 0 ? {} : { capped: fix.capped },
|
|
879
|
-
...fix.atHorizon ? { atHorizon: true } : {},
|
|
880
|
-
...spans.length === 0 ? {} : { again: spans }
|
|
881
|
-
};
|
|
882
|
-
}
|
|
883
|
-
const taken = /* @__PURE__ */ new Map();
|
|
884
|
-
for (const lot of plan.lots) {
|
|
885
|
-
let sum = ZERO;
|
|
886
|
-
for (const cell of cells) sum = add(sum, x[cell.takes.get(lot) ?? -1] ?? ZERO);
|
|
887
|
-
taken.set(lot, toNumber(sum));
|
|
888
|
-
}
|
|
889
|
-
const perDay = /* @__PURE__ */ new Map();
|
|
890
|
-
for (const cell of cells) {
|
|
891
|
-
const fed = typeof cell.fed === "number" ? x[cell.fed] ?? ZERO : cell.fed;
|
|
892
|
-
if (sign(fed) <= 0) continue;
|
|
893
|
-
const into = perDay.get(cell.draw.need) ?? /* @__PURE__ */ new Map();
|
|
894
|
-
perDay.set(cell.draw.need, into);
|
|
895
|
-
for (const [key, limit] of deviceUse(cell.block.runs).limits) {
|
|
896
|
-
const share = shareIn(plan, cell.segment, key);
|
|
897
|
-
if (share > 0) into.set(key, Math.max(into.get(key) ?? 0, toNumber(div(valueAt(limit.row, x), fed)) / share));
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
return {
|
|
901
|
-
needs,
|
|
902
|
-
taken,
|
|
903
|
-
perDay,
|
|
904
|
-
rests,
|
|
905
|
-
chargeDays: fill ? chargeDaysOf(plan, charges, x) : void 0
|
|
906
|
-
};
|
|
907
|
-
}
|
|
908
|
-
/**
|
|
909
|
-
* This function gives the joint days of the draws of a plan with a season. The objectives come in this order:
|
|
910
|
-
* - the largest common first stop.
|
|
911
|
-
* - the smallest sum of the shares of the stocks.
|
|
912
|
-
* - the fewest device-days.
|
|
913
|
-
*/
|
|
914
|
-
function seasonJoint(plan, stored) {
|
|
915
|
-
const open = new Set(plan.draws.map((draw) => draw.need));
|
|
916
|
-
const stop = firstStop(plan, /* @__PURE__ */ new Map(), plan.draws, (step) => [leastShares(plan, step.cells, step.program.count), fewestOpen(step, open)]);
|
|
917
|
-
const { step, x } = stop;
|
|
918
|
-
const takes = {};
|
|
919
|
-
for (const draw of plan.draws) takes[draw.need] = takesOf(step.cells, draw.need, x);
|
|
920
|
-
const total = /* @__PURE__ */ new Map();
|
|
921
|
-
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));
|
|
922
|
-
const runs = {};
|
|
923
|
-
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);
|
|
924
|
-
const fixed = /* @__PURE__ */ new Map();
|
|
925
|
-
fixStops(plan, stop, plan.draws, fixed);
|
|
926
|
-
const capped = [...new Set([...fixed.values()].flatMap((fix) => fix.capped))].sort();
|
|
927
|
-
return {
|
|
928
|
-
days: toNumber(stop.days),
|
|
929
|
-
atHorizon: stop.atHorizon,
|
|
930
|
-
takes,
|
|
931
|
-
binding: usedUp(plan, step.cells, x, reachOf(plan.parts, open).closure, stored, step.charges),
|
|
932
|
-
runs,
|
|
933
|
-
capped
|
|
934
|
-
};
|
|
935
|
-
}
|
|
936
|
-
/**
|
|
937
|
-
* This function gives the endurance of one draw of `enduranceOf` that reaches two limited converters of one device in a layer
|
|
938
|
-
* with a season (M2-grow-share-sweep). The take order cannot give the share of the device-days.
|
|
939
|
-
* Thus the programs of `bestEndurance` give it for this one draw: the first stop, then the most fed days after it (`again`).
|
|
940
|
-
*
|
|
941
|
-
* The function also gives the endurance of a draw that can take a store with a capacity that a source charges (M2-solar-charge-sweep).
|
|
942
|
-
* - The takes of the store come from the rest that the higher draws leave (`rests`). Thus the takes and the charges of the higher draws stay possible.
|
|
943
|
-
* - The first stop takes the fewest stocks, thus the draw keeps the most runs of a source and the most level for the lower draws.
|
|
944
|
-
* - The last program charges the store as much as it can, the earliest first. The draw gives the rest that it leaves to the lower draws.
|
|
945
|
-
* - The device-days of the charge are a part of the device-days of the draw (`perDay`). Thus a lower draw cannot use them.
|
|
946
|
-
*
|
|
947
|
-
* @param stock - The stocks that the higher draws leave. The stock of a store with a charge is its amount on day 0, because the rest gives the takes of the higher draws.
|
|
948
|
-
* @param lots - The rest of each harvest lot that the higher draws leave.
|
|
949
|
-
* @param room - The device-days of each day that the higher draws leave, by the key of the device, before the shares `monthly`.
|
|
950
|
-
* @param start - The first day of the event, for the months of the shares.
|
|
951
|
-
* @param capacities - The capacity of each resource whose stores have a capacity (`storeCapacities`).
|
|
952
|
-
* @param rests - The rest of each store with a capacity that the higher draws leave. With no rest, the store has its amount and its capacity.
|
|
953
|
-
*/
|
|
954
|
-
function seasonDraw(layer, stoppedParts, caseCuts, draw, stock, lots, horizon, room, start, capacities = /* @__PURE__ */ new Map(), rests = /* @__PURE__ */ new Map()) {
|
|
955
|
-
const supply = {
|
|
956
|
-
stock: new Map(stock),
|
|
957
|
-
harvests: [],
|
|
958
|
-
lots,
|
|
959
|
-
horizon,
|
|
960
|
-
start
|
|
961
|
-
};
|
|
962
|
-
const plan = seasonPlan(supply, caseParts(layer, stoppedParts, supply.stock, /* @__PURE__ */ new Set([draw.need])), caseCuts, [draw], room, capacities, rests);
|
|
963
|
-
const fixed = /* @__PURE__ */ new Map();
|
|
964
|
-
const open = /* @__PURE__ */ new Set([draw.need]);
|
|
965
|
-
const filled = plan.charges.length > 0;
|
|
966
|
-
const tail = (step) => filled ? [leastShares(plan, step.cells, step.program.count), fewestOpen(step, open)] : [fewestOpen(step, open)];
|
|
967
|
-
fixStops(plan, firstStop(plan, fixed, [draw], tail), [draw], fixed);
|
|
968
|
-
const best = seasonBest(plan, fixed, [[draw]], new Set(layer.stores.map((store) => store.resource)), filled);
|
|
969
|
-
const left = new Map(stock);
|
|
970
|
-
const rest = lots.map((lot) => lot.amount);
|
|
971
|
-
const chargedLots = new Set(plan.charges.map((charge) => charge.lot));
|
|
972
|
-
for (const [lot, amount] of best.taken) {
|
|
973
|
-
if (chargedLots.has(lot)) continue;
|
|
974
|
-
if (lot.origin === void 0) left.set(lot.resource, Math.max(0, (left.get(lot.resource) ?? 0) - amount));
|
|
975
|
-
else rest[lot.origin] = Math.max(0, (rest[lot.origin] ?? 0) - amount);
|
|
976
|
-
}
|
|
977
|
-
const perDay = new Map(best.perDay.get(draw.need) ?? []);
|
|
978
|
-
for (const [key, each] of best.chargeDays ?? /* @__PURE__ */ new Map()) perDay.set(key, (perDay.get(key) ?? 0) + each);
|
|
979
|
-
return {
|
|
980
|
-
need: best.needs[draw.need],
|
|
981
|
-
stock: left,
|
|
982
|
-
lots: rest,
|
|
983
|
-
perDay,
|
|
984
|
-
rests: best.rests ?? /* @__PURE__ */ new Map()
|
|
985
|
-
};
|
|
986
|
-
}
|
|
987
|
-
// src/share.ts
|
|
988
|
-
/**
|
|
989
|
-
* This function gives the best share of the stocks and of the device-days for one draw.
|
|
990
|
-
* It gives `undefined` when the program has no optimum.
|
|
991
|
-
*
|
|
992
|
-
* - The variables are the days `T`, the take of each direct use, the runs of each converter and maker, and the take of each stock.
|
|
993
|
-
* - The gifts of the direct uses and of the converters are not less than `T` times the demand.
|
|
994
|
-
* - The takes of each resource are not more than the take of its stock plus the gifts of its makers. The take of a stock is not more than the stock.
|
|
995
|
-
* - The runs of the converters of one device use `room` times `T` device-days or less. One run uses `1 / perDay` of a device-day. A limit of 0 stops the converter.
|
|
996
|
-
* - The first objective takes the largest `T`. The second takes the smallest sum of the takes of the stocks, each over its stock. The third takes the fewest device-days.
|
|
997
|
-
*
|
|
998
|
-
* The gifts at `T` days are a concave function of `T`, and they are 0 at 0. Thus each count of days up to the largest `T` also fits.
|
|
999
|
-
*
|
|
1000
|
-
* @param input - The draw, the stocks, the device-days of each day, the ways of the draw and its demand
|
|
1001
|
-
* @returns The days, the takes of the stocks and the device-days of each device
|
|
1002
|
-
*/
|
|
1003
|
-
function bestShare(input) {
|
|
1004
|
-
const parts = partsOf(input.stock, input.direct, input.converters, input.makers);
|
|
1005
|
-
const program = new Program();
|
|
1006
|
-
const days = program.next();
|
|
1007
|
-
const block = drawBlock(program, parts, input.draw);
|
|
1008
|
-
const need = new Map(block.gifts);
|
|
1009
|
-
addTo(need, days, neg(exact(input.demand)));
|
|
1010
|
-
program.push(need, ">=", ZERO);
|
|
1011
|
-
for (const [resource, index] of block.shares) program.push(/* @__PURE__ */ new Map([[index, ONE]]), "<=", exact(input.stock.get(resource) ?? 0));
|
|
1012
|
-
const { limits, stopped } = deviceUse(block.runs);
|
|
1013
|
-
for (const index of stopped) program.push(/* @__PURE__ */ new Map([[index, ONE]]), "<=", ZERO);
|
|
1014
|
-
for (const [key, limit] of limits) {
|
|
1015
|
-
const row = new Map(limit.row);
|
|
1016
|
-
addTo(row, days, neg(exact(input.room.get(key) ?? 0)));
|
|
1017
|
-
program.push(row, "<=", ZERO);
|
|
1018
|
-
}
|
|
1019
|
-
const least = /* @__PURE__ */ new Map();
|
|
1020
|
-
for (const [resource, index] of block.shares) addTo(least, index, div(ONE, exact(input.stock.get(resource) ?? 1)));
|
|
1021
|
-
const objectives = [
|
|
1022
|
-
{
|
|
1023
|
-
sense: "max",
|
|
1024
|
-
coefficients: dense(/* @__PURE__ */ new Map([[days, ONE]]), program.count)
|
|
1025
|
-
},
|
|
1026
|
-
{
|
|
1027
|
-
sense: "min",
|
|
1028
|
-
coefficients: dense(least, program.count)
|
|
1029
|
-
},
|
|
1030
|
-
{
|
|
1031
|
-
sense: "min",
|
|
1032
|
-
coefficients: dense(fewestRow(block.runs), program.count)
|
|
1033
|
-
}
|
|
1034
|
-
];
|
|
1035
|
-
const solved = solveLexicographic(program.constraints(), objectives, { warm: true });
|
|
1036
|
-
if (solved.status !== "optimal") return void 0;
|
|
1037
|
-
const at = (index) => solved.x[index] ?? ZERO;
|
|
1038
|
-
const taken = /* @__PURE__ */ new Map();
|
|
1039
|
-
for (const [resource, index] of block.shares) if (sign(at(index)) > 0) taken.set(resource, toNumber(at(index)));
|
|
1040
|
-
const used = /* @__PURE__ */ new Map();
|
|
1041
|
-
for (const [key, limit] of limits) {
|
|
1042
|
-
const sum = valueAt(limit.row, solved.x);
|
|
1043
|
-
if (sign(sum) > 0) used.set(key, toNumber(sum));
|
|
1044
|
-
}
|
|
1045
|
-
return {
|
|
1046
|
-
days: toNumber(at(days)),
|
|
1047
|
-
taken,
|
|
1048
|
-
used
|
|
1049
|
-
};
|
|
1050
|
-
}
|
|
1051
|
-
// src/endurance.ts
|
|
1052
|
-
/** The default horizon of a layer with a converter with a season: 730 days, thus two seasons of each year. */
|
|
1053
|
-
const SEASON_HORIZON_DAYS = 730;
|
|
1054
|
-
/**
|
|
1055
|
-
* This function gives the `id` of each part that does not work in a case: the cut parts and the parts of `down`.
|
|
1056
|
-
* With a stack, it also gives each part that stops.
|
|
1057
|
-
*/
|
|
1058
|
-
function caseStopped(caseCuts, options) {
|
|
1059
|
-
const down = [...caseCuts.cuts.map((cut) => cut.part), ...options.down ?? []];
|
|
1060
|
-
const out = new Set(down);
|
|
1061
|
-
if (options.stack !== void 0) for (const id of stopped(options.stack, down)) out.add(id);
|
|
1062
|
-
return out;
|
|
1063
|
-
}
|
|
1064
|
-
/** This function gives the day number of a date, or throws a `RangeError` for a date that is not `YYYY-MM-DD`. */
|
|
1065
|
-
function dayOf(date) {
|
|
1066
|
-
const day = dayNumber(date);
|
|
1067
|
-
if (day === void 0) throw new RangeError(`The date "${date}" is not a date YYYY-MM-DD.`);
|
|
1068
|
-
return day;
|
|
1069
|
-
}
|
|
1070
|
-
const DAY_MS = 864e5;
|
|
1071
|
-
/** This function gives the month of the day `day` after the start, 0 for January (M2-solar). It throws a `RangeError` for a bad date. */
|
|
1072
|
-
function monthOf(start, day) {
|
|
1073
|
-
dayOf(start);
|
|
1074
|
-
return new Date(Date.parse(`${start}T00:00:00Z`) + Math.floor(day) * DAY_MS).getUTCMonth();
|
|
1075
|
-
}
|
|
1076
|
-
/**
|
|
1077
|
-
* This function gives the days from the start to the first day of each month, in increasing order (M2-solar). Each
|
|
1078
|
-
* day is more than 0 and less than the horizon. It throws a `RangeError` for a date that is not `YYYY-MM-DD`.
|
|
1079
|
-
*/
|
|
1080
|
-
function monthEdges(start, horizon) {
|
|
1081
|
-
dayOf(start);
|
|
1082
|
-
const first = Date.parse(`${start}T00:00:00Z`);
|
|
1083
|
-
const date = new Date(first);
|
|
1084
|
-
const out = [];
|
|
1085
|
-
for (let month = date.getUTCMonth() + 1;; month += 1) {
|
|
1086
|
-
const day = Math.round((Date.UTC(date.getUTCFullYear(), month, 1) - first) / DAY_MS);
|
|
1087
|
-
if (!(day < horizon)) break;
|
|
1088
|
-
out.push(day);
|
|
1089
|
-
}
|
|
1090
|
-
return out;
|
|
1091
|
-
}
|
|
1092
|
-
/** This function gives the shares `monthly` of each device key of the converters with a limit and shares (M2-solar). */
|
|
1093
|
-
function monthlyShares(converters) {
|
|
1094
|
-
const out = /* @__PURE__ */ new Map();
|
|
1095
|
-
for (const converter of converters) if (converter.monthly !== void 0 && converter.perDay !== void 0) out.set(deviceKey(converter), converter.monthly);
|
|
1096
|
-
return out;
|
|
1097
|
-
}
|
|
1098
|
-
/** This function gives the share of the device-days of a key in a month: its share `monthly`, or 1. */
|
|
1099
|
-
function shareOf(shares, key, month) {
|
|
1100
|
-
return shares.get(key)?.[month] ?? 1;
|
|
1101
|
-
}
|
|
1102
|
-
/**
|
|
1103
|
-
* This function gives the amount of each resource in the stores at a date, by the code of the resource.
|
|
1104
|
-
*
|
|
1105
|
-
* - A store gives nothing on its due date and after it.
|
|
1106
|
-
* - A store gives nothing when its part is in `stoppedParts`.
|
|
1107
|
-
*/
|
|
1108
|
-
function storeAmounts(layer, at, stoppedParts = /* @__PURE__ */ new Set()) {
|
|
1109
|
-
const today = dayOf(at);
|
|
1110
|
-
const out = /* @__PURE__ */ new Map();
|
|
1111
|
-
for (const store of layer.stores) {
|
|
1112
|
-
if (store.due !== void 0 && today >= dayOf(store.due)) continue;
|
|
1113
|
-
if (store.part !== void 0 && stoppedParts.has(store.part)) continue;
|
|
1114
|
-
out.set(store.resource, (out.get(store.resource) ?? 0) + store.amount);
|
|
1115
|
-
}
|
|
1116
|
-
return out;
|
|
1117
|
-
}
|
|
1118
|
-
/** This function gives the amount of a need for each day in a case: the rate of the draw times the factor of the case. */
|
|
1119
|
-
function drawDemand(draw, caseCuts) {
|
|
1120
|
-
return draw.perDay * (caseCuts.factors?.[draw.need] ?? 1);
|
|
1121
|
-
}
|
|
1122
|
-
/**
|
|
1123
|
-
* This function gives the draws that take from the stores in a case: each draw whose supply stops, with a demand more than 0.
|
|
1124
|
-
* The sequence is by the level (the order of `levels`), then by the code of the need.
|
|
1125
|
-
*/
|
|
1126
|
-
function caseDraws(layer, caseCuts, stoppedParts) {
|
|
1127
|
-
const rank = (level) => {
|
|
1128
|
-
const index = layer.levels.indexOf(level);
|
|
1129
|
-
return index < 0 ? layer.levels.length : index;
|
|
1130
|
-
};
|
|
1131
|
-
return layer.draws.filter((draw) => stoppedParts.has(draw.supply) && drawDemand(draw, caseCuts) > 0).sort((a, b) => rank(a.level) - rank(b.level) || (a.need < b.need ? -1 : a.need > b.need ? 1 : 0));
|
|
1132
|
-
}
|
|
1133
|
-
/** This function gives the need that a converter gives, or `undefined` when it gives a resource. */
|
|
1134
|
-
function giftNeed(converter) {
|
|
1135
|
-
return "need" in converter.gives ? converter.gives.need : void 0;
|
|
1136
|
-
}
|
|
1137
|
-
/** This function gives the resource that a converter gives, or `undefined` when it gives a need. */
|
|
1138
|
-
function giftResource(converter) {
|
|
1139
|
-
return "resource" in converter.gives ? converter.gives.resource : void 0;
|
|
1140
|
-
}
|
|
1141
|
-
function makersOf(layer, stoppedParts) {
|
|
1142
|
-
const out = /* @__PURE__ */ new Map();
|
|
1143
|
-
for (const converter of layer.converters ?? []) {
|
|
1144
|
-
const resource = giftResource(converter);
|
|
1145
|
-
if (resource === void 0 || converter.season !== void 0 || !canRun(converter, stoppedParts)) continue;
|
|
1146
|
-
out.set(resource, [...out.get(resource) ?? [], converter]);
|
|
1147
|
-
}
|
|
1148
|
-
return out;
|
|
1149
|
-
}
|
|
1150
|
-
function waysOf(layer, need, stoppedParts, makers) {
|
|
1151
|
-
const converters = (layer.converters ?? []).filter((converter) => giftNeed(converter) === need && canRun(converter, stoppedParts));
|
|
1152
|
-
const sources = converters.filter(isSource);
|
|
1153
|
-
return {
|
|
1154
|
-
direct: (layer.direct ?? []).filter((use) => use.need === need && use.amount > 0),
|
|
1155
|
-
converters,
|
|
1156
|
-
makers,
|
|
1157
|
-
sources,
|
|
1158
|
-
others: sources.length === 0 ? converters : converters.filter((converter) => !isSource(converter)),
|
|
1159
|
-
open: [.../* @__PURE__ */ new Set([...sources, ...[...makers.values()].flat().filter(isSource)])]
|
|
1160
|
-
};
|
|
1161
|
-
}
|
|
1162
|
-
const NO_PATH = /* @__PURE__ */ new Set();
|
|
1163
|
-
const RUNS = "\0runs:";
|
|
1164
|
-
function runsKey(converter) {
|
|
1165
|
-
return RUNS + deviceKey(converter);
|
|
1166
|
-
}
|
|
1167
|
-
/** This function gives the device-days that a converter has left in `left`, or undefined with no limit. */
|
|
1168
|
-
function daysLeft(left, converter) {
|
|
1169
|
-
return converter.perDay === void 0 ? void 0 : left.get(runsKey(converter));
|
|
1170
|
-
}
|
|
1171
|
-
/** This function gives the map with no runs left of the converters: a map of the resources only. */
|
|
1172
|
-
function resourcesOnly(left) {
|
|
1173
|
-
return new Map([...left].filter(([key]) => !key.startsWith(RUNS)));
|
|
1174
|
-
}
|
|
1175
|
-
/**
|
|
1176
|
-
* This function gives the most of a resource that `left` gives: its stock, and the runs of each maker.
|
|
1177
|
-
* It is an upper limit. When two inputs come from one resource, the true amount can be less.
|
|
1178
|
-
*/
|
|
1179
|
-
function mostResource(left, makers, resource, path) {
|
|
1180
|
-
let total = left.get(resource) ?? 0;
|
|
1181
|
-
if (path.has(resource)) return total;
|
|
1182
|
-
const next = new Set(path).add(resource);
|
|
1183
|
-
for (const maker of makers.get(resource) ?? []) total += mostRuns(left, makers, maker, next) * maker.gives.amount;
|
|
1184
|
-
return total;
|
|
1185
|
-
}
|
|
1186
|
-
/** This function gives the most runs of a converter that `left` gives. It is an upper limit, as for `mostResource`. */
|
|
1187
|
-
function mostRuns(left, makers, converter, path) {
|
|
1188
|
-
const limit = daysLeft(left, converter);
|
|
1189
|
-
let runs = limit === void 0 ? 1e400 : limit * (converter.perDay ?? 0);
|
|
1190
|
-
for (const [resource, each] of inputsOf(converter)) runs = Math.min(runs, mostResource(left, makers, resource, path) / each);
|
|
1191
|
-
return Number.isFinite(runs) ? runs : 0;
|
|
1192
|
-
}
|
|
1193
|
-
/**
|
|
1194
|
-
* This function takes an amount of a resource from `left`: the stock first, then the makers in the order of the layer.
|
|
1195
|
-
* It changes `left`, and it gives the amount that it could not take.
|
|
1196
|
-
*/
|
|
1197
|
-
function takeResource(left, makers, resource, amount, path) {
|
|
1198
|
-
let rest = amount;
|
|
1199
|
-
const ways = path.has(resource) ? void 0 : makers.get(resource);
|
|
1200
|
-
let next;
|
|
1201
|
-
if (ways !== void 0 && ways.some(isSource)) {
|
|
1202
|
-
next = new Set(path).add(resource);
|
|
1203
|
-
for (const maker of ways) {
|
|
1204
|
-
if (rest <= 0) break;
|
|
1205
|
-
if (isSource(maker)) rest -= run(left, makers, maker, rest / maker.gives.amount, next) * maker.gives.amount;
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1208
|
-
const has = left.get(resource) ?? 0;
|
|
1209
|
-
const got = Math.min(Math.max(0, rest), has);
|
|
1210
|
-
left.set(resource, has - got);
|
|
1211
|
-
rest -= got;
|
|
1212
|
-
if (rest <= 0 || ways === void 0) return Math.max(0, rest);
|
|
1213
|
-
next ??= new Set(path).add(resource);
|
|
1214
|
-
for (const maker of ways) {
|
|
1215
|
-
if (rest <= 0) break;
|
|
1216
|
-
if (!isSource(maker)) rest -= run(left, makers, maker, rest / maker.gives.amount, next) * maker.gives.amount;
|
|
1217
|
-
}
|
|
1218
|
-
return Math.max(0, rest);
|
|
1219
|
-
}
|
|
1220
|
-
/**
|
|
1221
|
-
* This function runs a converter up to `wanted` times with the resources of `left`, and it gives the count of runs.
|
|
1222
|
-
* It changes `left`. When two inputs come from one resource, `largestFit` finds the runs that the resources give.
|
|
1223
|
-
*/
|
|
1224
|
-
function run(left, makers, converter, wanted, path) {
|
|
1225
|
-
const inputs = inputsOf(converter);
|
|
1226
|
-
const most = Math.min(wanted, mostRuns(left, makers, converter, path));
|
|
1227
|
-
if (!(most > 0)) return 0;
|
|
1228
|
-
const attempt = (runs) => {
|
|
1229
|
-
const copy = new Map(left);
|
|
1230
|
-
let need = 0;
|
|
1231
|
-
let short = 0;
|
|
1232
|
-
const limit = daysLeft(copy, converter);
|
|
1233
|
-
if (limit !== void 0 && converter.perDay !== void 0 && converter.perDay > 0) {
|
|
1234
|
-
const wanted = runs / converter.perDay;
|
|
1235
|
-
const got = Math.min(wanted, limit);
|
|
1236
|
-
copy.set(runsKey(converter), limit - got);
|
|
1237
|
-
need += runs;
|
|
1238
|
-
short += (wanted - got) * converter.perDay;
|
|
1239
|
-
}
|
|
1240
|
-
for (const [resource, each] of inputs) {
|
|
1241
|
-
need += runs * each;
|
|
1242
|
-
short += takeResource(copy, makers, resource, runs * each, path);
|
|
1243
|
-
}
|
|
1244
|
-
return {
|
|
1245
|
-
short,
|
|
1246
|
-
need,
|
|
1247
|
-
value: copy
|
|
1248
|
-
};
|
|
1249
|
-
};
|
|
1250
|
-
const { x: runs, value: best } = largestFit(most, attempt);
|
|
1251
|
-
if (best === void 0) return 0;
|
|
1252
|
-
for (const [resource, amount] of best) left.set(resource, amount);
|
|
1253
|
-
return runs;
|
|
1254
|
-
}
|
|
1255
|
-
/**
|
|
1256
|
-
* This function takes an amount of a need from the resources in `left`: the direct uses first, then the converters.
|
|
1257
|
-
* A direct use or a converter takes each resource from its stock first, then from its makers.
|
|
1258
|
-
* It changes `left`, and it gives the amount that it could not take.
|
|
1259
|
-
*/
|
|
1260
|
-
function take(left, ways, amount) {
|
|
1261
|
-
let rest = amount;
|
|
1262
|
-
for (const converter of ways.sources) {
|
|
1263
|
-
if (rest <= 0) break;
|
|
1264
|
-
rest -= run(left, ways.makers, converter, rest / converter.gives.amount, NO_PATH) * converter.gives.amount;
|
|
1265
|
-
}
|
|
1266
|
-
for (const use of ways.direct) {
|
|
1267
|
-
if (rest <= 0) break;
|
|
1268
|
-
const wanted = rest / use.amount;
|
|
1269
|
-
rest -= (wanted - takeResource(left, ways.makers, use.resource, wanted, NO_PATH)) * use.amount;
|
|
1270
|
-
}
|
|
1271
|
-
for (const converter of ways.others) {
|
|
1272
|
-
if (rest <= 0) break;
|
|
1273
|
-
rest -= run(left, ways.makers, converter, rest / converter.gives.amount, NO_PATH) * converter.gives.amount;
|
|
1274
|
-
}
|
|
1275
|
-
return Math.max(0, rest);
|
|
1276
|
-
}
|
|
1277
|
-
/** This function gives each resource that a need can use: the inputs of its ways, and the inputs of their makers, through each chain. */
|
|
1278
|
-
function inputClosure(ways) {
|
|
1279
|
-
return inputClosure$1(ways.direct.map((use) => use.resource), ways.converters, [...ways.makers.values()].flat());
|
|
1280
|
-
}
|
|
1281
|
-
/** The relative tolerance of a used-up resource. */
|
|
1282
|
-
const USED_UP = 1e-9;
|
|
1283
|
-
/** This function gives the day number of a day `MM-DD` of a year. A day that the year does not have gives undefined. */
|
|
1284
|
-
function yearDay(year, monthDay) {
|
|
1285
|
-
return dayNumber(`${String(year).padStart(4, "0")}-${monthDay}`);
|
|
1286
|
-
}
|
|
1287
|
-
/**
|
|
1288
|
-
* This function gives the sow days of a season, in days from the start of the event, before the horizon, in increasing
|
|
1289
|
-
* order (M2-grow). The sow day of a year is the first day of its window on the start or after it. A window with `from`
|
|
1290
|
-
* after `to` goes across the end of the year. A year that does not have a day of the window (`02-29`) has no sowing.
|
|
1291
|
-
*
|
|
1292
|
-
* @throws RangeError when the start is not `YYYY-MM-DD`, or the horizon is not more than 0.
|
|
1293
|
-
*/
|
|
1294
|
-
function sowDays(season, start, horizon) {
|
|
1295
|
-
if (!(horizon > 0)) throw new RangeError(`The horizon ${String(horizon)} is not a number of days more than 0.`);
|
|
1296
|
-
const first = dayOf(start);
|
|
1297
|
-
const out = [];
|
|
1298
|
-
for (let year = Number(start.slice(0, 4)) - 1;; year += 1) {
|
|
1299
|
-
const from = yearDay(year, season.from);
|
|
1300
|
-
const sameYear = yearDay(year, season.to);
|
|
1301
|
-
const to = from !== void 0 && sameYear !== void 0 && sameYear < from ? yearDay(year + 1, season.to) : sameYear;
|
|
1302
|
-
if ((from ?? yearDay(year, "01-01")) - first >= horizon) break;
|
|
1303
|
-
if (from === void 0 || to === void 0 || to < first) continue;
|
|
1304
|
-
const day = Math.max(from, first) - first;
|
|
1305
|
-
if (day < horizon) out.push(day);
|
|
1306
|
-
}
|
|
1307
|
-
return out;
|
|
1308
|
-
}
|
|
1309
|
-
/** This function gives a lot of a store: it is ready before the event, and it does not spoil. */
|
|
1310
|
-
function storeLot(resource, amount) {
|
|
1311
|
-
return {
|
|
1312
|
-
resource,
|
|
1313
|
-
day: -1e400,
|
|
1314
|
-
spoils: 1e400,
|
|
1315
|
-
left: amount,
|
|
1316
|
-
lost: 0
|
|
1317
|
-
};
|
|
1318
|
-
}
|
|
1319
|
-
/** This function compares two numbers for a sort, also two that are Infinity. */
|
|
1320
|
-
function compare(a, b) {
|
|
1321
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
1322
|
-
}
|
|
1323
|
-
/**
|
|
1324
|
-
* This function takes the use of each resource from `from` to `to` from the lots, first expiring first out. It takes
|
|
1325
|
-
* the lot that spoils first, then the lot of the earliest day. A store comes after each lot with a spoil day, and before each
|
|
1326
|
-
* harvest with no spoil day. With no spoil day, the order is thus the stores, then each harvest in the order of its day.
|
|
1327
|
-
*/
|
|
1328
|
-
function useLots(lots, from, to) {
|
|
1329
|
-
for (const [resource, had] of from) {
|
|
1330
|
-
let rest = had - (to.get(resource) ?? 0);
|
|
1331
|
-
if (!(rest > 0)) continue;
|
|
1332
|
-
const order = lots.filter((lot) => lot.resource === resource && lot.left > 0).sort((a, b) => compare(a.spoils, b.spoils) || compare(a.day, b.day));
|
|
1333
|
-
for (const lot of order) {
|
|
1334
|
-
if (!(rest > 0)) break;
|
|
1335
|
-
const taken = Math.min(lot.left, rest);
|
|
1336
|
-
lot.left -= taken;
|
|
1337
|
-
rest -= taken;
|
|
1338
|
-
}
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
/**
|
|
1342
|
-
* This function gives the sowings of the converters with a season that can operate in a case, up to the horizon. It
|
|
1343
|
-
* also gives the lot of each harvest. The order is the order of their sow days, then of the layer. Each sowing takes its inputs
|
|
1344
|
-
* from `stock`, and from each harvest that is ready and not spoiled on its sow day (M2-grow-seed). It changes `stock`,
|
|
1345
|
-
* and the `left` of each lot is the rest of its harvest after the later sowings.
|
|
1346
|
-
*/
|
|
1347
|
-
function sowings(layer, stoppedParts, stock, makers, start, horizon) {
|
|
1348
|
-
const plan = [];
|
|
1349
|
-
(layer.converters ?? []).forEach((converter, order) => {
|
|
1350
|
-
const season = converter.season;
|
|
1351
|
-
if (season === void 0 || !(season.runs > 0) || !canRun(converter, stoppedParts)) return;
|
|
1352
|
-
for (const sow of sowDays(season, start, horizon)) plan.push({
|
|
1353
|
-
converter,
|
|
1354
|
-
season,
|
|
1355
|
-
sow,
|
|
1356
|
-
order
|
|
1357
|
-
});
|
|
1358
|
-
});
|
|
1359
|
-
plan.sort((a, b) => a.sow - b.sow || a.order - b.order);
|
|
1360
|
-
const stores = [...stock].map(([resource, amount]) => storeLot(resource, amount));
|
|
1361
|
-
const lots = [];
|
|
1362
|
-
const out = [];
|
|
1363
|
-
for (const { converter, season, sow } of plan) {
|
|
1364
|
-
const ready = [...stores, ...lots.filter((lot) => lot.day <= sow && lot.spoils > sow)];
|
|
1365
|
-
const pool = /* @__PURE__ */ new Map();
|
|
1366
|
-
for (const lot of ready) pool.set(lot.resource, (pool.get(lot.resource) ?? 0) + lot.left);
|
|
1367
|
-
const had = new Map(pool);
|
|
1368
|
-
const runs = run(pool, makers, converter, season.runs, NO_PATH);
|
|
1369
|
-
useLots(ready, had, pool);
|
|
1370
|
-
const resource = giftResource(converter);
|
|
1371
|
-
if (resource === void 0) continue;
|
|
1372
|
-
const day = sow + season.delayDays;
|
|
1373
|
-
const amount = runs * converter.gives.amount;
|
|
1374
|
-
const spoils = season.shelfDays === void 0 ? 1e400 : day + season.shelfDays;
|
|
1375
|
-
out.push({
|
|
1376
|
-
converter: converter.id,
|
|
1377
|
-
sow,
|
|
1378
|
-
day,
|
|
1379
|
-
runs,
|
|
1380
|
-
resource,
|
|
1381
|
-
amount,
|
|
1382
|
-
...season.shelfDays === void 0 ? {} : { spoils }
|
|
1383
|
-
});
|
|
1384
|
-
lots.push({
|
|
1385
|
-
resource,
|
|
1386
|
-
day,
|
|
1387
|
-
spoils,
|
|
1388
|
-
left: amount,
|
|
1389
|
-
lost: 0
|
|
1390
|
-
});
|
|
1391
|
-
}
|
|
1392
|
-
for (const store of stores) stock.set(store.resource, store.left);
|
|
1393
|
-
return {
|
|
1394
|
-
harvests: out,
|
|
1395
|
-
lots
|
|
1396
|
-
};
|
|
1397
|
-
}
|
|
1398
|
-
/**
|
|
1399
|
-
* This function gives the supply of a case with a season (M2-grow-share). `enduranceOf`, `bestEndurance` and `jointDays`
|
|
1400
|
-
* use it, so they read the same sowings. It throws a `RangeError` for a horizon that is not more than 0, or a date that is not `YYYY-MM-DD`.
|
|
1401
|
-
*/
|
|
1402
|
-
function seasonSupply(layer, stoppedParts, options) {
|
|
1403
|
-
const stock = storeAmounts(layer, options.at, stoppedParts);
|
|
1404
|
-
const horizon = seasonHorizon(options.horizon);
|
|
1405
|
-
const start = options.start ?? options.at;
|
|
1406
|
-
const sown = sowings(layer, stoppedParts, stock, makersOf(layer, stoppedParts), start, horizon);
|
|
1407
|
-
const lots = sown.lots.filter((lot) => lot.day < horizon && lot.left > 0).sort((a, b) => a.day - b.day).map((lot) => ({
|
|
1408
|
-
resource: lot.resource,
|
|
1409
|
-
day: lot.day,
|
|
1410
|
-
spoils: lot.spoils,
|
|
1411
|
-
amount: lot.left
|
|
1412
|
-
}));
|
|
1413
|
-
return {
|
|
1414
|
-
stock,
|
|
1415
|
-
harvests: sown.harvests,
|
|
1416
|
-
lots,
|
|
1417
|
-
horizon,
|
|
1418
|
-
start
|
|
1419
|
-
};
|
|
1420
|
-
}
|
|
1421
|
-
/**
|
|
1422
|
-
* This function tells if a layer has a converter with a season, with shares `monthly` or a source (M2-solar) that can run
|
|
1423
|
-
* in a case. Such a layer needs the queries in segments of days to a horizon. A harvest, a spoil day and the start of a
|
|
1424
|
-
* month end a segment. A source can feed a draw with no end, thus the horizon is the upper limit of its days.
|
|
1425
|
-
*/
|
|
1426
|
-
function hasSeason(layer, stoppedParts) {
|
|
1427
|
-
return (layer.converters ?? []).some((converter) => (converter.season !== void 0 || converter.monthly !== void 0 || isSource(converter)) && canRun(converter, stoppedParts));
|
|
1428
|
-
}
|
|
1429
|
-
/**
|
|
1430
|
-
* This function gives the days that each draw holds when a case stops its supply, and the resources that are used up then.
|
|
1431
|
-
*
|
|
1432
|
-
* - A draw takes from the stores only when the case stops its supply part. Each other draw holds without end, and the result leaves it out.
|
|
1433
|
-
* - The demand of a draw for each day is its rate times the factor of the case.
|
|
1434
|
-
* - The draws take the resources in the order of their level, then of their codes. A draw takes from its direct uses first, then from its converters.
|
|
1435
|
-
* - The days of a draw are the largest `T` at which the stores give `T` days of its demand, after each higher draw.
|
|
1436
|
-
* - 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.
|
|
1437
|
-
* - 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.
|
|
1438
|
-
* A draw with a shared device is different. Refer to the last item.
|
|
1439
|
-
* - A converter whose device stops gives nothing. A store whose part stops gives nothing.
|
|
1440
|
-
* - A converter can give a resource. A take of a resource uses the stock first, then the converters that make it, through each chain.
|
|
1441
|
-
* - 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.
|
|
1442
|
-
* - The higher draws use the runs of a limit first.
|
|
1443
|
-
* - 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.
|
|
1444
|
-
* - Two limited converters of one device can make the shortfall of the take order decrease when the days increase.
|
|
1445
|
-
* 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.
|
|
1446
|
-
* A linear program with exact rationals gives its days: the largest `T` with the best share of its stocks and of the device-days.
|
|
1447
|
-
* At this `T`, the draw takes the smallest sum of its takes of the stocks, each over its stock, then the fewest device-days.
|
|
1448
|
-
|
|
1449
|
-
* - A converter with a season (M2-grow) is not a maker of a take. Its sowings take their inputs from the stores before
|
|
1450
|
-
* the first draw, and each harvest adds to the stocks on its day. Refer to `Season`. The query reads the sowings
|
|
1451
|
-
* and the harvests up to the horizon (`horizon`, default `SEASON_HORIZON_DAYS`), and a draw holds the horizon at most.
|
|
1452
|
-
* - With a season, each draw takes its stocks over spans from harvest to harvest. `days` is the first stop of the
|
|
1453
|
-
* draw. A later harvest can feed the draw again: `again` gives these spans, and `takes` is the sum to the horizon.
|
|
1454
|
-
* A lower draw gets the stocks that the higher draws leave, the earliest stocks first taken. This is the safe side.
|
|
1455
|
-
* - A draw with a shared device and a season gets the programs of `bestEndurance` for itself alone (M2-grow-share-sweep). Its stocks,
|
|
1456
|
-
* its harvest lots and its device-days are those that the higher draws leave. It has no field `spoiled`.
|
|
1457
|
-
* - A source (M2-solar) takes nothing and has a limit `perDay`. A take uses it first: its runs of a day are lost when no
|
|
1458
|
-
* draw uses them. With shares `monthly`, the device-days of a day are the devices times the share of its month. Each
|
|
1459
|
-
* span also ends at the start of a month, and a draw can hold again from the start of a month.
|
|
1460
|
-
* - A store with a capacity that a source charges (M2-solar-charge-sweep) takes a charge. Each draw that can take it gets the
|
|
1461
|
-
* programs of `bestEndurance` for itself alone, in the order of the levels. Thus one draw gets the days of `bestEndurance`.
|
|
1462
|
-
* A lower draw takes only the level that the higher draws leave. It charges the store only with the device-days that they leave.
|
|
1463
|
-
*
|
|
1464
|
-
* 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.
|
|
1465
|
-
*
|
|
1466
|
-
* @param layer - The endurance layer
|
|
1467
|
-
* @param caseCuts - The case
|
|
1468
|
-
* @param options - The date, and the stack or the parts that are also down
|
|
1469
|
-
* @returns The endurance of each draw whose supply stops
|
|
1470
|
-
*/
|
|
1471
|
-
function enduranceOf(layer, caseCuts, options) {
|
|
1472
|
-
const stoppedParts = caseStopped(caseCuts, options);
|
|
1473
|
-
const seasonal = hasSeason(layer, stoppedParts);
|
|
1474
|
-
const supply = seasonal ? seasonSupply(layer, stoppedParts, options) : void 0;
|
|
1475
|
-
const start = supply?.stock ?? storeAmounts(layer, options.at, stoppedParts);
|
|
1476
|
-
const makers = makersOf(layer, stoppedParts);
|
|
1477
|
-
const stocked = new Set(layer.stores.map((store) => store.resource));
|
|
1478
|
-
const limited = (layer.converters ?? []).filter((converter) => converter.perDay !== void 0 && canRun(converter, stoppedParts));
|
|
1479
|
-
const devices = new Map(limited.map((converter) => [deviceKey(converter), converter.devices ?? 1]));
|
|
1480
|
-
const monthShares = monthlyShares(limited);
|
|
1481
|
-
const begin = options.start ?? options.at;
|
|
1482
|
-
const shareAt = (key, at) => monthShares.size === 0 ? 1 : shareOf(monthShares, key, monthOf(begin, at));
|
|
1483
|
-
const months = monthShares.size === 0 || supply === void 0 ? [] : monthEdges(begin, supply.horizon);
|
|
1484
|
-
const nextMonth = (at) => months.find((day) => day > at) ?? 1e400;
|
|
1485
|
-
const used = /* @__PURE__ */ new Map();
|
|
1486
|
-
let before = new Map(start);
|
|
1487
|
-
const horizon = supply?.horizon ?? 1e400;
|
|
1488
|
-
const harvests = supply?.harvests ?? [];
|
|
1489
|
-
const later = (supply?.lots ?? []).map((lot) => ({ ...lot }));
|
|
1490
|
-
const capacities = storeCapacities(layer, stoppedParts);
|
|
1491
|
-
const chargedResources = new Set([...capacities.keys()].filter((resource) => (makers.get(resource) ?? []).some(isSource)));
|
|
1492
|
-
const rests = /* @__PURE__ */ new Map();
|
|
1493
|
-
const needs = {};
|
|
1494
|
-
for (const draw of caseDraws(layer, caseCuts, stoppedParts)) {
|
|
1495
|
-
const ways = waysOf(layer, draw.need, stoppedParts, makers);
|
|
1496
|
-
const demand = drawDemand(draw, caseCuts);
|
|
1497
|
-
const room = new Map([...devices].map(([key, count]) => [key, Math.max(0, count - (used.get(key) ?? 0))]));
|
|
1498
|
-
const closure = inputClosure(ways);
|
|
1499
|
-
const reach = /* @__PURE__ */ new Set([...ways.converters, ...[...closure].flatMap((resource) => makers.get(resource) ?? [])]);
|
|
1500
|
-
const after = (stock, days, at = 0) => {
|
|
1501
|
-
const tried = new Map(stock);
|
|
1502
|
-
for (const [key, free] of room) tried.set(RUNS + key, free * shareAt(key, at) * days);
|
|
1503
|
-
return {
|
|
1504
|
-
left: tried,
|
|
1505
|
-
short: take(tried, ways, days * demand)
|
|
1506
|
-
};
|
|
1507
|
-
};
|
|
1508
|
-
const span = (stock, limit, bound, at = 0) => {
|
|
1509
|
-
const fit = largestFit(Math.min(limit, bound / demand), (days) => {
|
|
1510
|
-
const tried = after(stock, days, at);
|
|
1511
|
-
return {
|
|
1512
|
-
short: tried.short,
|
|
1513
|
-
need: days * demand,
|
|
1514
|
-
value: tried.left
|
|
1515
|
-
};
|
|
1516
|
-
});
|
|
1517
|
-
return {
|
|
1518
|
-
days: fit.x,
|
|
1519
|
-
left: fit.value ?? after(stock, fit.x, at).left
|
|
1520
|
-
};
|
|
1521
|
-
};
|
|
1522
|
-
const shares = sharesDevice(reach);
|
|
1523
|
-
const charging = [...closure].some((resource) => chargedResources.has(resource));
|
|
1524
|
-
if ((shares || charging) && seasonal) {
|
|
1525
|
-
const lots = later.map((lot) => ({
|
|
1526
|
-
resource: lot.resource,
|
|
1527
|
-
day: lot.day,
|
|
1528
|
-
spoils: lot.spoils,
|
|
1529
|
-
amount: lot.amount
|
|
1530
|
-
}));
|
|
1531
|
-
const held = seasonDraw(layer, stoppedParts, caseCuts, draw, resourcesOnly(before), lots, horizon, room, begin, capacities, rests);
|
|
1532
|
-
needs[draw.need] = held.need;
|
|
1533
|
-
for (const [key, each] of held.perDay) used.set(key, (used.get(key) ?? 0) + each);
|
|
1534
|
-
for (const [resource, rest] of held.rests) rests.set(resource, rest);
|
|
1535
|
-
before = held.stock;
|
|
1536
|
-
for (const [index, amount] of held.lots.entries()) later[index].amount = amount;
|
|
1537
|
-
continue;
|
|
1538
|
-
}
|
|
1539
|
-
const share = shares ? bestShare({
|
|
1540
|
-
draw,
|
|
1541
|
-
stock: before,
|
|
1542
|
-
room,
|
|
1543
|
-
direct: ways.direct,
|
|
1544
|
-
converters: ways.converters,
|
|
1545
|
-
makers: [...reach].filter((converter) => !ways.converters.includes(converter)),
|
|
1546
|
-
demand
|
|
1547
|
-
}) : void 0;
|
|
1548
|
-
let low;
|
|
1549
|
-
let left;
|
|
1550
|
-
let final;
|
|
1551
|
-
const came = new Map(before);
|
|
1552
|
-
const lost = /* @__PURE__ */ new Map();
|
|
1553
|
-
const pool = [...before].map(([resource, amount]) => storeLot(resource, amount));
|
|
1554
|
-
const arrived = [];
|
|
1555
|
-
const again = [];
|
|
1556
|
-
let atHorizon = false;
|
|
1557
|
-
const perDay = /* @__PURE__ */ new Map();
|
|
1558
|
-
const count = (spanLeft, days, at = 0) => {
|
|
1559
|
-
if (days > 0) for (const [key, free] of room) {
|
|
1560
|
-
const share = shareAt(key, at);
|
|
1561
|
-
if (!(share > 0)) continue;
|
|
1562
|
-
const each = (free * share * days - (spanLeft.get(RUNS + key) ?? 0)) / days / share;
|
|
1563
|
-
perDay.set(key, perDay.has(key) ? Math.max(perDay.get(key), each) : each);
|
|
1564
|
-
}
|
|
1565
|
-
};
|
|
1566
|
-
let spanDays;
|
|
1567
|
-
let spanAt = 0;
|
|
1568
|
-
if (share !== void 0) {
|
|
1569
|
-
low = share.days;
|
|
1570
|
-
spanDays = low;
|
|
1571
|
-
left = new Map(before);
|
|
1572
|
-
for (const [resource, amount] of share.taken) left.set(resource, Math.max(0, (left.get(resource) ?? 0) - amount));
|
|
1573
|
-
for (const [key, free] of room) left.set(RUNS + key, Math.max(0, free * low - (share.used.get(key) ?? 0)));
|
|
1574
|
-
final = left;
|
|
1575
|
-
count(left, low);
|
|
1576
|
-
} else if (later.length === 0 && !seasonal) {
|
|
1577
|
-
({days: low, left} = span(before, 1e400, capacity(start, ways)));
|
|
1578
|
-
spanDays = low;
|
|
1579
|
-
final = left;
|
|
1580
|
-
count(left, low);
|
|
1581
|
-
} else {
|
|
1582
|
-
let stock = new Map(before);
|
|
1583
|
-
let next = 0;
|
|
1584
|
-
let at = 0;
|
|
1585
|
-
let first;
|
|
1586
|
-
let resumed = 0;
|
|
1587
|
-
const advance = () => {
|
|
1588
|
-
while (next < later.length && later[next].day <= at) {
|
|
1589
|
-
const harvest = later[next];
|
|
1590
|
-
const lot = {
|
|
1591
|
-
resource: harvest.resource,
|
|
1592
|
-
day: harvest.day,
|
|
1593
|
-
spoils: harvest.spoils,
|
|
1594
|
-
left: harvest.amount,
|
|
1595
|
-
lost: 0
|
|
1596
|
-
};
|
|
1597
|
-
pool.push(lot);
|
|
1598
|
-
arrived.push({
|
|
1599
|
-
lot,
|
|
1600
|
-
harvest
|
|
1601
|
-
});
|
|
1602
|
-
stock.set(lot.resource, (stock.get(lot.resource) ?? 0) + lot.left);
|
|
1603
|
-
came.set(lot.resource, (came.get(lot.resource) ?? 0) + lot.left);
|
|
1604
|
-
next += 1;
|
|
1605
|
-
}
|
|
1606
|
-
for (const lot of pool) {
|
|
1607
|
-
if (!(lot.left > 0) || lot.spoils > at) continue;
|
|
1608
|
-
stock.set(lot.resource, Math.max(0, (stock.get(lot.resource) ?? 0) - lot.left));
|
|
1609
|
-
lost.set(lot.resource, (lost.get(lot.resource) ?? 0) + lot.left);
|
|
1610
|
-
lot.lost += lot.left;
|
|
1611
|
-
lot.left = 0;
|
|
1612
|
-
}
|
|
1613
|
-
};
|
|
1614
|
-
advance();
|
|
1615
|
-
for (;;) {
|
|
1616
|
-
const spoil = Math.min(...pool.filter((lot) => lot.left > 0).map((lot) => lot.spoils));
|
|
1617
|
-
const end = Math.min(next < later.length ? later[next].day : 1e400, spoil, horizon, nextMonth(at));
|
|
1618
|
-
const part = span(stock, end - at, capacity(stock, ways, shareAt, at), at);
|
|
1619
|
-
count(part.left, part.days, at);
|
|
1620
|
-
const rest = resourcesOnly(part.left);
|
|
1621
|
-
useLots(pool, stock, rest);
|
|
1622
|
-
stock = rest;
|
|
1623
|
-
const stop = at + part.days;
|
|
1624
|
-
if (part.days < end - at) {
|
|
1625
|
-
if (first === void 0) first = {
|
|
1626
|
-
days: stop,
|
|
1627
|
-
left: part.left,
|
|
1628
|
-
spanDays: part.days,
|
|
1629
|
-
spanAt: at
|
|
1630
|
-
};
|
|
1631
|
-
else if (stop > resumed) again.push({
|
|
1632
|
-
from: resumed,
|
|
1633
|
-
to: stop
|
|
1634
|
-
});
|
|
1635
|
-
const resume = Math.min(next < later.length ? later[next].day : 1e400, nextMonth(at));
|
|
1636
|
-
if (!(resume < horizon)) break;
|
|
1637
|
-
at = resume;
|
|
1638
|
-
resumed = at;
|
|
1639
|
-
} else {
|
|
1640
|
-
at = end;
|
|
1641
|
-
if (at >= horizon) {
|
|
1642
|
-
if (first === void 0) atHorizon = true;
|
|
1643
|
-
else again.push({
|
|
1644
|
-
from: resumed,
|
|
1645
|
-
to: horizon
|
|
1646
|
-
});
|
|
1647
|
-
break;
|
|
1648
|
-
}
|
|
1649
|
-
}
|
|
1650
|
-
advance();
|
|
1651
|
-
}
|
|
1652
|
-
low = first?.days ?? horizon;
|
|
1653
|
-
left = first?.left ?? stock;
|
|
1654
|
-
spanDays = first?.spanDays ?? horizon;
|
|
1655
|
-
spanAt = first?.spanAt ?? 0;
|
|
1656
|
-
final = stock;
|
|
1657
|
-
}
|
|
1658
|
-
const stores = resourcesOnly(left);
|
|
1659
|
-
const binding = atHorizon ? [] : [...closure].filter((resource) => !makers.has(resource) || stocked.has(resource)).filter((resource) => mostResource(stores, makers, resource, NO_PATH) <= USED_UP * Math.max(1, start.get(resource) ?? 0)).sort();
|
|
1660
|
-
const takes = {};
|
|
1661
|
-
for (const [resource, amount] of came) {
|
|
1662
|
-
const taken = amount - (final.get(resource) ?? 0) - (lost.get(resource) ?? 0);
|
|
1663
|
-
if (taken > 0) takes[resource] = taken;
|
|
1664
|
-
}
|
|
1665
|
-
const spoiled = Object.fromEntries([...lost].filter(([, amount]) => amount > 0));
|
|
1666
|
-
const runsLeft = (converter) => (daysLeft(left, converter) ?? 0) * (converter.perDay ?? 0);
|
|
1667
|
-
const runsGiven = (converter) => (devices.get(deviceKey(converter)) ?? 1) * shareAt(deviceKey(converter), spanAt) * (converter.perDay ?? 0) * spanDays;
|
|
1668
|
-
const capped = atHorizon ? [] : limited.filter((converter) => reach.has(converter)).filter((converter) => runsLeft(converter) <= USED_UP * Math.max(1, runsGiven(converter))).filter((converter) => mostRuns(stores, makers, converter, NO_PATH) > USED_UP).map((converter) => converter.id).sort();
|
|
1669
|
-
needs[draw.need] = {
|
|
1670
|
-
days: low,
|
|
1671
|
-
binding,
|
|
1672
|
-
takes,
|
|
1673
|
-
...capped.length === 0 ? {} : { capped },
|
|
1674
|
-
...atHorizon ? { atHorizon: true } : {},
|
|
1675
|
-
...again.length === 0 ? {} : { again },
|
|
1676
|
-
...Object.keys(spoiled).length === 0 ? {} : { spoiled }
|
|
1677
|
-
};
|
|
1678
|
-
for (const [key, each] of perDay) used.set(key, (used.get(key) ?? 0) + each);
|
|
1679
|
-
if (later.length === 0 && !seasonal) {
|
|
1680
|
-
before = resourcesOnly(final);
|
|
1681
|
-
continue;
|
|
1682
|
-
}
|
|
1683
|
-
for (const lot of pool) if (lot.day === -1e400) before.set(lot.resource, lot.left);
|
|
1684
|
-
for (const { lot, harvest } of arrived) harvest.amount = lot.left + lot.lost;
|
|
1685
|
-
}
|
|
1686
|
-
return {
|
|
1687
|
-
case: caseCuts.case,
|
|
1688
|
-
at: options.at,
|
|
1689
|
-
needs,
|
|
1690
|
-
...harvests.length === 0 ? {} : { harvests }
|
|
1691
|
-
};
|
|
1692
|
-
}
|
|
1693
|
-
/** This function gives the horizon of a layer with a season, or throws a `RangeError` for a horizon that is not more than 0. */
|
|
1694
|
-
function seasonHorizon(horizon) {
|
|
1695
|
-
const value = horizon ?? 730;
|
|
1696
|
-
if (!(value > 0) || !Number.isFinite(value)) throw new RangeError(`The horizon ${String(horizon)} is not a number of days more than 0.`);
|
|
1697
|
-
return value;
|
|
1698
|
-
}
|
|
1699
|
-
/** This function tells if two converters of `reach` with a limit `perDay` more than 0 share one device. */
|
|
1700
|
-
function sharesDevice(reach) {
|
|
1701
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1702
|
-
for (const converter of reach) {
|
|
1703
|
-
if (converter.perDay === void 0 || !(converter.perDay > 0)) continue;
|
|
1704
|
-
const key = deviceKey(converter);
|
|
1705
|
-
if (seen.has(key)) return true;
|
|
1706
|
-
seen.add(key);
|
|
1707
|
-
}
|
|
1708
|
-
return false;
|
|
1709
|
-
}
|
|
1710
|
-
/**
|
|
1711
|
-
* This function gives an upper limit of a need that the stores give with no other draw, through each chain. A source
|
|
1712
|
-
* (M2-solar) with a share more than 0 gives no upper limit, thus the limit is Infinity. The span of the sweep is finite.
|
|
1713
|
-
*/
|
|
1714
|
-
function capacity(start, ways, shareAt, at = 0) {
|
|
1715
|
-
for (const converter of ways.open) if ((shareAt === void 0 ? 1 : shareAt(deviceKey(converter), at)) > 0) return 1e400;
|
|
1716
|
-
let total = 0;
|
|
1717
|
-
for (const use of ways.direct) total += mostResource(start, ways.makers, use.resource, NO_PATH) * use.amount;
|
|
1718
|
-
for (const converter of ways.converters) total += mostRuns(start, ways.makers, converter, NO_PATH) * converter.gives.amount;
|
|
1719
|
-
return total;
|
|
1720
|
-
}
|
|
1721
|
-
// src/check.ts
|
|
1722
|
-
/**
|
|
1723
|
-
* These are the codes of the gate. Each code is an error, except `endurance-device-mix`, `endurance-no-way` and `endurance-cut-no-curve`, which are warnings.
|
|
1724
|
-
*
|
|
1725
|
-
* - `endurance-twice`: two or more records of one list have one key. A key is a level, a resource, a store, a need, a converter or a case.
|
|
1726
|
-
* The key of a prior is its case and its place. A converter takes each resource on one line, and a case cuts each part one time.
|
|
1727
|
-
* - `endurance-level-unknown`: the level of a draw is not a level of the layer.
|
|
1728
|
-
* - `endurance-resource-unknown`: a store, a use or a converter names a resource that the layer does not have.
|
|
1729
|
-
* - `endurance-part-unknown`: a supply, a device, the part of a store or a cut is not a part of the base stack.
|
|
1730
|
-
* - `endurance-range`: an amount, a rate, a count or a factor is not a finite number of 0 or more. Or a reach is not from 0 to 1.
|
|
1731
|
-
* - `endurance-date`: a date is not a date `YYYY-MM-DD`.
|
|
1732
|
-
* - `endurance-converter-empty`: a converter takes nothing and has no limit `perDay` more than 0, or it gives an amount of 0.
|
|
1733
|
-
* A converter with a limit can take nothing: it is a source, for example a solar panel. Since 0.8.20 (M2-solar).
|
|
1734
|
-
* - `endurance-curve`: a survival curve does not start at `[0, 1]`, or it does not end at a chance of 0.
|
|
1735
|
-
* Or its days do not increase, or its chance increases.
|
|
1736
|
-
* Or the band or the counts of a cut do not agree with its curve (`bandFault`, `countsFault`), or the cut has them and no curve.
|
|
1737
|
-
* - `endurance-bound`: the bound of a cut is not a value of `CUT_BOUNDS` of the core. Since 0.8.4.
|
|
1738
|
-
* - `endurance-prior-case`: a prior names a case that the layer does not have.
|
|
1739
|
-
* - `endurance-cycle`: converters make a resource from itself, through one converter or a chain. One fault names each cycle.
|
|
1740
|
-
* - `endurance-devices`: two converters of one device give two different counts of devices.
|
|
1741
|
-
* - `endurance-device-mix` (warning): a device has a converter with a limit `perDay` and a converter with no limit. The
|
|
1742
|
-
* converter with no limit uses no device-days, so its runs do not reduce the runs of the limited converter.
|
|
1743
|
-
* - `endurance-no-way` (warning): a draw has no direct use and no converter, so it holds 0 days when its supply stops.
|
|
1744
|
-
* - `endurance-cut-no-curve` (warning): a cut has no curve of its duration. The draws whose stop it can be part of are no read in the shortfall.
|
|
1745
|
-
* - `endurance-season`: the season of a converter has a day that is not `MM-DD`. Or its delay or its shelf life is not more than 0.
|
|
1746
|
-
* Or its count of sowings is not a finite number of 0 or more. Or the converter with a season gives a need, or it has
|
|
1747
|
-
* `perDay` or `devices`. Since 0.8.16 (M2-grow).
|
|
1748
|
-
* - `endurance-monthly`: the shares `monthly` of a converter are not 12 numbers from 0 to 1. Or the converter has
|
|
1749
|
-
* `monthly` and no `perDay`, or a season. Or two converters of one device have different shares. Since 0.8.20 (M2-solar).
|
|
1750
|
-
* - `endurance-capacity`: the capacity of a store is not a finite number more than 0, or it is less than the amount.
|
|
1751
|
-
* Or a store with a capacity has a due date. Or a resource has a store with a capacity and a store with no capacity.
|
|
1752
|
-
* Since 0.8.21 (M2-solar-charge).
|
|
1753
|
-
*/
|
|
1754
|
-
const ENDURANCE_CODES = [
|
|
1755
|
-
"endurance-twice",
|
|
1756
|
-
"endurance-level-unknown",
|
|
1757
|
-
"endurance-resource-unknown",
|
|
1758
|
-
"endurance-part-unknown",
|
|
1759
|
-
"endurance-range",
|
|
1760
|
-
"endurance-date",
|
|
1761
|
-
"endurance-converter-empty",
|
|
1762
|
-
"endurance-curve",
|
|
1763
|
-
"endurance-bound",
|
|
1764
|
-
"endurance-prior-case",
|
|
1765
|
-
"endurance-cycle",
|
|
1766
|
-
"endurance-devices",
|
|
1767
|
-
"endurance-device-mix",
|
|
1768
|
-
"endurance-no-way",
|
|
1769
|
-
"endurance-cut-no-curve",
|
|
1770
|
-
"endurance-season",
|
|
1771
|
-
"endurance-monthly",
|
|
1772
|
-
"endurance-capacity"
|
|
1773
|
-
];
|
|
1774
|
-
/**
|
|
1775
|
-
* This function gives each fault of an endurance layer. With a base stack, it also examines the `id` of each part. A list with no error tells that the gate passes.
|
|
1776
|
-
*
|
|
1777
|
-
* The faults are in the order of the layer. The function is pure.
|
|
1778
|
-
*
|
|
1779
|
-
* @param layer - The endurance layer
|
|
1780
|
-
* @param stack - The base stack, with the layer. With no stack, the gate does not examine the parts.
|
|
1781
|
-
* @returns The faults
|
|
1782
|
-
*/
|
|
1783
|
-
function checkEndurance(layer, stack) {
|
|
1784
|
-
const faults = [];
|
|
1785
|
-
const add = (level, code, at, message) => {
|
|
1786
|
-
faults.push({
|
|
1787
|
-
level,
|
|
1788
|
-
code,
|
|
1789
|
-
at,
|
|
1790
|
-
message
|
|
1791
|
-
});
|
|
1792
|
-
};
|
|
1793
|
-
const once = (what, keys) => {
|
|
1794
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1795
|
-
for (const key of keys) {
|
|
1796
|
-
if (seen.has(key)) add("error", "endurance-twice", key, `Two or more ${what} have the key "${key}".`);
|
|
1797
|
-
seen.add(key);
|
|
1798
|
-
}
|
|
1799
|
-
};
|
|
1800
|
-
const range = (at, what, value) => {
|
|
1801
|
-
if (!(Number.isFinite(value) && value >= 0)) add("error", "endurance-range", at, `The ${what} of "${at}" is ${String(value)}. It must be a finite number of 0 or more.`);
|
|
1802
|
-
};
|
|
1803
|
-
const date = (at, value) => {
|
|
1804
|
-
if (dayNumber(value) === void 0) add("error", "endurance-date", at, `The date "${value}" of "${at}" is not a date YYYY-MM-DD.`);
|
|
1805
|
-
};
|
|
1806
|
-
const parts = stack === void 0 ? void 0 : new Set(stack.parts.map((part) => part.id));
|
|
1807
|
-
const part = (at, what, id) => {
|
|
1808
|
-
if (parts !== void 0 && id !== void 0 && !parts.has(id)) add("error", "endurance-part-unknown", at, `The ${what} "${id}" of "${at}" is not a part of the stack.`);
|
|
1809
|
-
};
|
|
1810
|
-
const resources = new Set(layer.resources.map((resource) => resource.code));
|
|
1811
|
-
const resource = (at, code) => {
|
|
1812
|
-
if (!resources.has(code)) add("error", "endurance-resource-unknown", at, `The resource "${code}" of "${at}" is not a resource of the layer.`);
|
|
1813
|
-
};
|
|
1814
|
-
const direct = layer.direct ?? [];
|
|
1815
|
-
const converters = layer.converters ?? [];
|
|
1816
|
-
const cases = layer.cases ?? [];
|
|
1817
|
-
const priors = layer.priors ?? [];
|
|
1818
|
-
once("levels", layer.levels);
|
|
1819
|
-
once("resources", layer.resources.map((item) => item.code));
|
|
1820
|
-
once("stores", layer.stores.map((store) => store.id));
|
|
1821
|
-
once("draws", layer.draws.map((draw) => draw.need));
|
|
1822
|
-
once("converters", converters.map((converter) => converter.id));
|
|
1823
|
-
once("cases", cases.map((item) => item.case));
|
|
1824
|
-
once("priors", priors.map((prior) => `${prior.case}@${prior.place}`));
|
|
1825
|
-
for (const store of layer.stores) {
|
|
1826
|
-
resource(store.id, store.resource);
|
|
1827
|
-
range(store.id, "amount", store.amount);
|
|
1828
|
-
if (store.due !== void 0) date(store.id, store.due);
|
|
1829
|
-
part(store.id, "part", store.part);
|
|
1830
|
-
if (store.capacity !== void 0) {
|
|
1831
|
-
if (!(Number.isFinite(store.capacity) && store.capacity > 0 && store.capacity >= store.amount)) add("error", "endurance-capacity", store.id, `The capacity of "${store.id}" is ${String(store.capacity)}. It must be a finite number more than 0, and not less than the amount.`);
|
|
1832
|
-
if (store.due !== void 0) add("error", "endurance-capacity", store.id, `The store "${store.id}" has a capacity and a due date. A store with a charge has no due date.`);
|
|
1833
|
-
}
|
|
1834
|
-
}
|
|
1835
|
-
for (const code of new Set(layer.stores.map((store) => store.resource))) {
|
|
1836
|
-
const all = layer.stores.filter((store) => store.resource === code);
|
|
1837
|
-
const charged = all.filter((store) => store.capacity !== void 0);
|
|
1838
|
-
if (charged.length > 0 && charged.length < all.length) add("error", "endurance-capacity", code, `The resource "${code}" has a store with a capacity and a store with no capacity. Its stores all have a capacity, or none has one.`);
|
|
1839
|
-
}
|
|
1840
|
-
const levels = new Set(layer.levels);
|
|
1841
|
-
for (const draw of layer.draws) {
|
|
1842
|
-
if (!levels.has(draw.level)) add("error", "endurance-level-unknown", draw.need, `The level "${draw.level}" of the draw "${draw.need}" is not a level of the layer.`);
|
|
1843
|
-
range(draw.need, "rate", draw.perDay);
|
|
1844
|
-
if (draw.users !== void 0) range(draw.need, "count of users", draw.users);
|
|
1845
|
-
part(draw.need, "supply", draw.supply);
|
|
1846
|
-
if (!(direct.some((use) => use.need === draw.need) || converters.some((converter) => giftNeed(converter) === draw.need))) add("warning", "endurance-no-way", draw.need, `The draw "${draw.need}" has no direct use and no converter. It holds 0 days when its supply stops.`);
|
|
1847
|
-
}
|
|
1848
|
-
for (const use of direct) {
|
|
1849
|
-
const at = `${use.resource}>${use.need}`;
|
|
1850
|
-
resource(at, use.resource);
|
|
1851
|
-
range(at, "amount", use.amount);
|
|
1852
|
-
}
|
|
1853
|
-
for (const converter of converters) {
|
|
1854
|
-
once(`resources of the converter "${converter.id}"`, converter.takes.map((input) => input.resource));
|
|
1855
|
-
for (const input of converter.takes) {
|
|
1856
|
-
resource(converter.id, input.resource);
|
|
1857
|
-
range(converter.id, "amount", input.amount);
|
|
1858
|
-
}
|
|
1859
|
-
range(converter.id, "amount", converter.gives.amount);
|
|
1860
|
-
const made = giftResource(converter);
|
|
1861
|
-
if (made !== void 0) resource(converter.id, made);
|
|
1862
|
-
if (!(converter.perDay !== void 0 && converter.perDay > 0) && !converter.takes.some((input) => input.amount > 0) || converter.gives.amount === 0) add("error", "endurance-converter-empty", converter.id, `The converter "${converter.id}" takes nothing, or it gives nothing.`);
|
|
1863
|
-
part(converter.id, "device", converter.device);
|
|
1864
|
-
if (converter.perDay !== void 0) range(converter.id, "rate perDay", converter.perDay);
|
|
1865
|
-
if (converter.devices !== void 0 && !(Number.isSafeInteger(converter.devices) && converter.devices >= 1)) add("error", "endurance-range", converter.id, `The count of devices of "${converter.id}" is ${String(converter.devices)}. It must be a whole number of 1 or more.`);
|
|
1866
|
-
if (converter.season !== void 0) for (const message of seasonFaults(converter)) add("error", "endurance-season", converter.id, message);
|
|
1867
|
-
for (const message of monthlyFaults(converter)) add("error", "endurance-monthly", converter.id, message);
|
|
1868
|
-
}
|
|
1869
|
-
const shares = /* @__PURE__ */ new Map();
|
|
1870
|
-
for (const converter of converters) {
|
|
1871
|
-
if (converter.device === void 0 || converter.perDay === void 0) continue;
|
|
1872
|
-
const seen = shares.get(converter.device);
|
|
1873
|
-
if (seen === void 0) shares.set(converter.device, converter);
|
|
1874
|
-
else if (!sameShares(seen.monthly, converter.monthly)) add("error", "endurance-monthly", converter.id, `The converter "${converter.id}" and the converter "${seen.id}" of the device "${converter.device}" have different shares monthly. The converters of one device have the same shares.`);
|
|
1875
|
-
}
|
|
1876
|
-
const counts = /* @__PURE__ */ new Map();
|
|
1877
|
-
for (const converter of converters) {
|
|
1878
|
-
if (converter.device === void 0) continue;
|
|
1879
|
-
const count = converter.devices ?? 1;
|
|
1880
|
-
const seen = counts.get(converter.device);
|
|
1881
|
-
if (seen !== void 0 && seen !== count) add("error", "endurance-devices", converter.id, `The converter "${converter.id}" gives the count ${String(count)} for the device "${converter.device}". An other converter gives the count ${String(seen)}.`);
|
|
1882
|
-
if (seen === void 0) counts.set(converter.device, count);
|
|
1883
|
-
}
|
|
1884
|
-
const mixed = /* @__PURE__ */ new Set();
|
|
1885
|
-
for (const converter of converters) {
|
|
1886
|
-
const device = converter.device;
|
|
1887
|
-
if (device === void 0 || converter.perDay !== void 0 || mixed.has(device)) continue;
|
|
1888
|
-
const limited = converters.find((other) => other.device === device && other.perDay !== void 0);
|
|
1889
|
-
if (limited === void 0) continue;
|
|
1890
|
-
mixed.add(device);
|
|
1891
|
-
add("warning", "endurance-device-mix", converter.id, `The device "${device}" has the limited converter "${limited.id}" and the converter "${converter.id}" with no limit. "${converter.id}" uses no device-days.`);
|
|
1892
|
-
}
|
|
1893
|
-
const caseCodes = new Set(cases.map((item) => item.case));
|
|
1894
|
-
for (const item of cases) {
|
|
1895
|
-
once(`cuts of the case "${item.case}"`, item.cuts.map((cut) => cut.part));
|
|
1896
|
-
for (const cut of item.cuts) {
|
|
1897
|
-
const at = `${item.case}/${cut.part}`;
|
|
1898
|
-
part(at, "cut", cut.part);
|
|
1899
|
-
if (cut.bound !== void 0 && !isCutBound(cut.bound)) add("error", "endurance-bound", at, `The bound of the cut "${at}" is ${JSON.stringify(cut.bound)}. It must be one of ${CUT_BOUNDS.join(", ")}.`);
|
|
1900
|
-
if (cut.duration === void 0) {
|
|
1901
|
-
add("warning", "endurance-cut-no-curve", at, `The cut "${at}" has no curve of its duration, so the draws that it can stop are no read.`);
|
|
1902
|
-
if (cut.band !== void 0) add("error", "endurance-curve", at, `The cut "${at}" has a band and no curve of its duration.`);
|
|
1903
|
-
if (cut.counts !== void 0) add("error", "endurance-curve", at, `The cut "${at}" has counts and no curve of its duration.`);
|
|
1904
|
-
continue;
|
|
1905
|
-
}
|
|
1906
|
-
const fault = curveFault$1(cut.duration);
|
|
1907
|
-
if (fault !== void 0) add("error", "endurance-curve", at, `The duration of the cut "${at}": ${fault}`);
|
|
1908
|
-
const band = cut.band === void 0 || fault !== void 0 ? void 0 : bandFault(cut.duration, cut.band);
|
|
1909
|
-
if (band !== void 0) add("error", "endurance-curve", at, `The band of the cut "${at}": ${band}`);
|
|
1910
|
-
const counts = cut.counts === void 0 || fault !== void 0 ? void 0 : countsFault(cut.duration, cut.counts);
|
|
1911
|
-
if (counts !== void 0) add("error", "endurance-curve", at, `The counts of the cut "${at}": ${counts}`);
|
|
1912
|
-
}
|
|
1913
|
-
for (const [need, factor] of Object.entries(item.factors ?? {})) range(`${item.case}/${need}`, "factor", factor);
|
|
1914
|
-
if (item.leadSeconds !== void 0) range(item.case, "lead time", item.leadSeconds);
|
|
1915
|
-
}
|
|
1916
|
-
for (const cycle of resourceCycles(converters)) {
|
|
1917
|
-
const [first = ""] = cycle;
|
|
1918
|
-
const names = cycle.map((code) => `"${code}"`).join(", ");
|
|
1919
|
-
add("error", "endurance-cycle", first, `${cycle.length === 1 ? `A converter makes the resource ${names} from itself.` : `Converters make the resources ${names} from each other.`} A chain of converters cannot end.`);
|
|
1920
|
-
}
|
|
1921
|
-
for (const prior of priors) {
|
|
1922
|
-
const at = `${prior.case}@${prior.place}`;
|
|
1923
|
-
if (!caseCodes.has(prior.case)) add("error", "endurance-prior-case", at, `The prior "${at}" names the case "${prior.case}", which the layer does not have.`);
|
|
1924
|
-
range(at, "count of events in one year", prior.perYear);
|
|
1925
|
-
if (!(prior.reach >= 0 && prior.reach <= 1)) add("error", "endurance-range", at, `The reach of "${at}" is ${String(prior.reach)}. It must be from 0 to 1.`);
|
|
1926
|
-
date(at, prior.asOf);
|
|
1927
|
-
const fault = prior.interval === void 0 ? void 0 : intervalFault(prior.interval);
|
|
1928
|
-
if (fault !== void 0) add("error", "endurance-range", at, `The interval of the count of events in one year of "${at}" is not valid. ${fault}`);
|
|
1929
|
-
}
|
|
1930
|
-
return faults;
|
|
1931
|
-
}
|
|
1932
|
-
/** This function tells if a text is a day `MM-DD` of a leap year. */
|
|
1933
|
-
function isMonthDay(value) {
|
|
1934
|
-
return /^\d{2}-\d{2}$/.test(value) && dayNumber(`2000-${value}`) !== void 0;
|
|
1935
|
-
}
|
|
1936
|
-
/** This function gives the messages of the faults of the season of a converter. */
|
|
1937
|
-
function seasonFaults(converter) {
|
|
1938
|
-
const season = converter.season;
|
|
1939
|
-
if (season === void 0) return [];
|
|
1940
|
-
const out = [];
|
|
1941
|
-
for (const day of [season.from, season.to]) if (!isMonthDay(day)) out.push(`The season of "${converter.id}" has the day "${day}". It is not a day MM-DD.`);
|
|
1942
|
-
if (!(Number.isFinite(season.delayDays) && season.delayDays > 0)) out.push(`The delay of the season of "${converter.id}" is ${String(season.delayDays)}. It must be a finite number of days more than 0.`);
|
|
1943
|
-
if (season.shelfDays !== void 0 && !(Number.isFinite(season.shelfDays) && season.shelfDays > 0)) out.push(`The shelf life of the season of "${converter.id}" is ${String(season.shelfDays)}. It must be a finite number of days more than 0.`);
|
|
1944
|
-
if (!(Number.isFinite(season.runs) && season.runs >= 0)) out.push(`The runs of the season of "${converter.id}" are ${String(season.runs)}. They must be a finite number of 0 or more.`);
|
|
1945
|
-
if (giftResource(converter) === void 0) out.push(`The converter "${converter.id}" has a season and gives a need. A converter with a season gives a resource.`);
|
|
1946
|
-
if (converter.perDay !== void 0 || converter.devices !== void 0) out.push(`The converter "${converter.id}" has a season and a limit perDay or devices. A converter with a season has no such limit.`);
|
|
1947
|
-
return out;
|
|
1948
|
-
}
|
|
1949
|
-
/** This function gives the messages of the faults of the shares `monthly` of a converter (M2-solar). */
|
|
1950
|
-
function monthlyFaults(converter) {
|
|
1951
|
-
const monthly = converter.monthly;
|
|
1952
|
-
if (monthly === void 0) return [];
|
|
1953
|
-
const out = [];
|
|
1954
|
-
if (monthly.length !== 12 || !monthly.every((share) => Number.isFinite(share) && share >= 0 && share <= 1)) out.push(`The shares monthly of "${converter.id}" are not 12 numbers from 0 to 1.`);
|
|
1955
|
-
if (converter.perDay === void 0) out.push(`The converter "${converter.id}" has shares monthly and no limit perDay. The shares are parts of the limit.`);
|
|
1956
|
-
if (converter.season !== void 0) out.push(`The converter "${converter.id}" has shares monthly and a season. A converter has one or the other.`);
|
|
1957
|
-
return out;
|
|
1958
|
-
}
|
|
1959
|
-
/** This function tells if two lists of shares `monthly` are equal. No list is equal to a list of 12 ones. */
|
|
1960
|
-
function sameShares(a, b) {
|
|
1961
|
-
const full = (shares) => shares ?? Array.from({ length: 12 }, () => 1);
|
|
1962
|
-
const left = full(a);
|
|
1963
|
-
const right = full(b);
|
|
1964
|
-
return left.length === right.length && left.every((share, index) => share === right[index]);
|
|
1965
|
-
}
|
|
1966
|
-
/**
|
|
1967
|
-
* This function gives each cycle of resources: a resource is made from a resource that a converter makes from it. Each cycle is a list of codes.
|
|
1968
|
-
* A converter with a season is not part of a cycle: its harvest comes after its sowing, thus a chain of it ends.
|
|
1969
|
-
*/
|
|
1970
|
-
function resourceCycles(converters) {
|
|
1971
|
-
const from = /* @__PURE__ */ new Map();
|
|
1972
|
-
for (const converter of converters) {
|
|
1973
|
-
const made = giftResource(converter);
|
|
1974
|
-
if (made === void 0 || converter.season !== void 0) continue;
|
|
1975
|
-
const inputs = from.get(made) ?? /* @__PURE__ */ new Set();
|
|
1976
|
-
for (const input of converter.takes) if (input.amount > 0) inputs.add(input.resource);
|
|
1977
|
-
from.set(made, inputs);
|
|
1978
|
-
}
|
|
1979
|
-
const successors = (code) => from.get(code) ?? [];
|
|
1980
|
-
return stronglyConnectedComponents(from.keys(), successors).filter((component) => {
|
|
1981
|
-
const [only = ""] = component;
|
|
1982
|
-
return component.length > 1 || (from.get(only)?.has(only) ?? false);
|
|
1983
|
-
});
|
|
1984
|
-
}
|
|
1985
|
-
/** This function tells if a list of faults has an error. */
|
|
1986
|
-
function hasEnduranceError(faults) {
|
|
1987
|
-
return faults.some((fault) => fault.level === "error");
|
|
1988
|
-
}
|
|
1989
|
-
// src/join.ts
|
|
1990
|
-
/**
|
|
1991
|
-
* This function joins the endurance layers of the fragments of a stack into one layer.
|
|
1992
|
-
*
|
|
1993
|
-
* - The levels are one setting: each fragment that has levels gives the same list, else the join has a fault.
|
|
1994
|
-
* - The records of each list are in the order of the fragments.
|
|
1995
|
-
* - Two resources with one code in two fragments join into one resource when their records are equal. A different field is a fault.
|
|
1996
|
-
* Its message gives the canonical JSON of the record of each fragment.
|
|
1997
|
-
* - A code that one fragment has two times stays in the joined layer. The gate `checkEndurance` finds it.
|
|
1998
|
-
* - One key of each other list in two or more fragments is a fault, also when the records are equal.
|
|
1999
|
-
* - The key of a direct use is its resource and its need.
|
|
2000
|
-
*
|
|
2001
|
-
* The function is pure, and it changes no layer. The gate `checkEndurance` examines the joined layer afterward.
|
|
2002
|
-
*
|
|
2003
|
-
* @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.
|
|
2004
|
-
* @returns The joined layer, or the faults of the join.
|
|
2005
|
-
*/
|
|
2006
|
-
function joinEndurance(pieces) {
|
|
2007
|
-
const levels = joinSetting(pieces, (layer) => layer.levels.length > 0 ? layer.levels.join("\n") : void 0, "levels");
|
|
2008
|
-
const resources = joinRecords(pieces, (layer) => layer.resources, (item) => item.code, "resource", {
|
|
2009
|
-
equal: (first, other) => canonicalJson(first) === canonicalJson(other),
|
|
2010
|
-
text: canonicalJson
|
|
2011
|
-
});
|
|
2012
|
-
const stores = joinRecords(pieces, (layer) => layer.stores, (item) => item.id, "store");
|
|
2013
|
-
const draws = joinRecords(pieces, (layer) => layer.draws, (item) => item.need, "draw");
|
|
2014
|
-
const direct = joinRecords(pieces, (layer) => layer.direct, (item) => `${item.resource}>${item.need}`, "direct use");
|
|
2015
|
-
const converters = joinRecords(pieces, (layer) => layer.converters, (item) => item.id, "converter");
|
|
2016
|
-
const cases = joinRecords(pieces, (layer) => layer.cases, (item) => item.case, "case");
|
|
2017
|
-
const priors = joinRecords(pieces, (layer) => layer.priors, (item) => `${item.case}@${item.place}`, "prior");
|
|
2018
|
-
const errors = [
|
|
2019
|
-
levels,
|
|
2020
|
-
resources,
|
|
2021
|
-
stores,
|
|
2022
|
-
draws,
|
|
2023
|
-
direct,
|
|
2024
|
-
converters,
|
|
2025
|
-
cases,
|
|
2026
|
-
priors
|
|
2027
|
-
].flatMap((join) => join.errors);
|
|
2028
|
-
if (errors.length > 0) return {
|
|
2029
|
-
ok: false,
|
|
2030
|
-
errors
|
|
2031
|
-
};
|
|
2032
|
-
const has = (key) => pieces.some((piece) => piece.layer[key] !== void 0);
|
|
2033
|
-
return {
|
|
2034
|
-
ok: true,
|
|
2035
|
-
layer: {
|
|
2036
|
-
schema: ENDURANCE_SCHEMA,
|
|
2037
|
-
levels: levels.value === void 0 ? [] : levels.value.split("\n"),
|
|
2038
|
-
resources: resources.items,
|
|
2039
|
-
stores: stores.items,
|
|
2040
|
-
draws: draws.items,
|
|
2041
|
-
...has("direct") ? { direct: direct.items } : {},
|
|
2042
|
-
...has("converters") ? { converters: converters.items } : {},
|
|
2043
|
-
...has("cases") ? { cases: cases.items } : {},
|
|
2044
|
-
...has("priors") ? { priors: priors.items } : {}
|
|
2045
|
-
}
|
|
2046
|
-
};
|
|
2047
|
-
}
|
|
2048
|
-
export { deviceUse as A, fixStops as C, storeCapacities as D, seasonPlan as E, addTo as F, dense as I, exact as L, fewestRow as M, reachOf as N, Program as O, valueAt as P, deviceKey as R, firstStop as S, seasonJoint as T, seasonSupply as _, hasEnduranceError as a, storeAmounts as b, caseStopped as c, giftNeed as d, giftResource as f, monthlyShares as g, monthOf as h, curveFault as i, drawBlock as j, caseParts as k, drawDemand as l, monthEdges as m, ENDURANCE_CODES as n, SEASON_HORIZON_DAYS as o, hasSeason as p, checkEndurance as r, caseDraws as s, joinEndurance as t, enduranceOf as u, shareOf as v, seasonBest as w, fewestOpen as x, sowDays as y, isSource as z };
|
|
2049
|
-
|
|
2050
|
-
//# sourceMappingURL=join-r5ht3vQ9.js.map
|