Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | 63x 63x 798x 798x 93x 93x 798x 11112x 11112x 11112x 11112x 798x 11152x 11152x 22200x 22200x 11152x 11152x 798x 972x 972x 972x 972x 972x 972x 972x 972x 972x 972x 972x 972x 972x 972x 972x 972x 972x 36x 36x 36x 36x 36x 36x 12x 36x 36x 2x 4x 2x 2x 36x 36x 36x 36x 36x 36x 972x 10376x 10376x 902x 902x 902x 10376x 10376x 972x 8267x 8267x 972x 6168x 6168x 6168x 6168x 6168x 972x 4121x 4121x 4121x 4121x 4121x 4097x 4097x 4121x 972x 1666x 1666x 1666x 1666x 1666x 1666x 972x 87x 22x 22x 22x 65x 65x 65x 65x 65x 654x 654x 65x 65x 65x 87x 87x 87x 972x 2156x 2156x 2080x 2080x 2156x 5x 5x 5x 2156x 25x 25x 25x 2050x 2050x 2156x 52x 52x 52x 75x 52x 52x 52x 52x 52x 1998x 1998x 2156x 2156x 2156x 2156x 2337x 2156x 2156x 2156x 2156x 671x 1342x 1342x 1342x 1342x 1342x 2228x 2228x 2228x 2228x 2228x 1342x 671x 671x 671x 671x 671x 63x 63x 63x 671x 377x 671x 671x 269x 269x 327x 327x 13x 13x 13x 327x 269x 269x 269x 269x 269x 269x 269x 269x 671x 1666x 1666x 1666x 2142x 11152x 11152x 11152x 11152x 11152x 11152x 11152x 11152x 11152x 1666x 1666x 1666x 1666x 1666x 1666x 1666x 1666x 1666x 2156x 2156x 2156x 859x 859x 859x 859x 859x 859x 807x 807x 807x 807x 807x 807x 807x 1666x 2142x 2156x 2156x 16x 16x 16x 16x 16x 16x 16x 1650x 1650x 1650x 2142x 11112x 11112x 18x 18x 18x 18x 18x 18x 11112x 1650x 2142x 1650x 2142x 1650x 1650x 2142x 1650x 2156x 972x 798x 63x | 'use strict';
module.exports = ({ numeric: { rational: { zero, one, add, sub, mul, div, neg, cmp }, }, linearProgram: { maximize }, }) => {
const expression = (constant, coefficients) => ({ constant, coefficients: coefficients || new Map() });
function scale(value, factor) {
return expression(mul(value.constant, factor), new Map([...value.coefficients].map(([key, coefficient]) => [key, mul(coefficient, factor)])));
}
function subtractVariable(value, variable) {
const coefficients = new Map(value.coefficients);
coefficients.set(variable, sub(coefficients.get(variable) || zero, one));
return expression(value.constant, coefficients);
}
function evaluate(value, witness) {
let result = value.constant;
for (const [variable, coefficient] of value.coefficients) {
result = add(result, mul(coefficient, witness[variable]));
}
return result;
}
class AllocationHistory {
constraints;
groups;
invalid;
roundingUnit;
totals;
variables;
witness;
constructor(roundingUnit) {
this.roundingUnit = roundingUnit;
this.groups = new Map();
this.constraints = [];
this.variables = 0;
this.witness = [];
this.totals = new Map();
this.invalid = false;
}
merge(other) {
if (other === this)
return;
const offset = this.variables;
const remap = (coefficients) => new Map([...coefficients]
.map(([key, value]) => [key + offset, value]));
this.constraints.push(...other.constraints.map(({ coefficients, bound, strict }) => ({
coefficients: remap(coefficients), bound, strict,
})));
for (const [account, groups] of other.groups) {
this.holdings(account).push(...groups.map(({ price, remaining }) => ({
price, remaining: expression(remaining.constant, remap(remaining.coefficients)),
})));
}
for (const [account, total] of other.totals)
this.changeTotal(account, total.quantity, total.cost);
this.variables += other.variables;
this.witness.push(...other.witness);
this.invalid ||= other.invalid;
}
holdings(account) {
let groups = this.groups.get(account);
if (!groups) {
groups = [];
this.groups.set(account, groups);
}
return groups;
}
total(account) {
return this.totals.get(account) || { quantity: zero, cost: zero };
}
changeTotal(account, quantity, cost) {
const previous = this.total(account);
this.totals.set(account, {
quantity: add(previous.quantity, quantity), cost: add(previous.cost, cost),
});
}
acquire(account, quantity, cost) {
this.changeTotal(account, quantity, cost);
const price = div(cost, quantity);
const group = this.holdings(account).find((candidate) => !candidate.remaining.coefficients.size && cmp(candidate.price, price) === 0);
if (group)
group.remaining.constant = add(group.remaining.constant, quantity);
else
this.holdings(account).push({ price, remaining: expression(quantity) });
}
equality(coefficients, bound) {
this.constraints.push({ coefficients, bound });
this.constraints.push({
coefficients: new Map([...coefficients].map(([key, value]) => [key, neg(value)])),
bound: neg(bound),
});
}
findWitness(constraints) {
if (!constraints.some(({ strict }) => strict)) {
const result = maximize(this.variables, constraints, new Map());
return result.status === 'optimal' ? result.solution : null;
}
// A closed simplex can optimize infima/suprema, but a boundary-only
// witness must not satisfy an open rounding interval. One shared positive
// slack proves that all strict inequalities can hold simultaneously.
const slack = this.variables;
const closed = constraints.map(({ coefficients, bound, strict }) => ({
coefficients: strict ? new Map([...coefficients, [slack, one]]) : coefficients,
bound,
}));
closed.push({ coefficients: new Map([[slack, one]]), bound: one });
const result = maximize(this.variables + 1, closed, new Map([[slack, one]]));
return result.status === 'optimal' && result.value.n > 0n
? result.solution.slice(0, this.variables) : null;
}
dispose(account, quantity, cost, destination, destinationQuantity) {
if (this.invalid)
return null;
const groups = this.holdings(account);
const total = this.total(account);
if (cmp(quantity, total.quantity) > 0) {
this.invalid = true;
return { insufficient: true };
}
if (cmp(quantity, total.quantity) === 0 && cmp(cost, total.cost) !== 0) {
this.invalid = true;
return { minimum: total.cost, maximum: total.cost };
}
// A complete transfer or split carries both the booked remainder and
// the original exact groups, including any accumulated rounding gap.
if (destination && cmp(quantity, total.quantity) === 0) {
const ratio = div(destinationQuantity, quantity);
this.groups.set(account, []);
this.holdings(destination).push(...groups.map((group) => ({
price: div(group.price, ratio), remaining: scale(group.remaining, ratio),
})));
this.changeTotal(account, neg(quantity), neg(cost));
this.changeTotal(destination, destinationQuantity, cost);
return null;
}
// Only multiples of the declared monetary step are rounded values.
// Explicitly finer amounts retain their exact meaning.
const rounded = this.roundingUnit.n > 0n && div(cost, this.roundingUnit).d === 1n;
const lowerCost = rounded ? sub(cost, this.roundingUnit) : cost;
const upperCost = rounded ? add(cost, this.roundingUnit) : cost;
const outside = (minimum, maximum) => rounded
? cmp(upperCost, minimum) <= 0 || cmp(lowerCost, maximum) >= 0
: cmp(cost, minimum) < 0 || cmp(cost, maximum) > 0;
// Greedy bounds avoid the solver while remaining quantities are fixed.
// With rounding, even a booked endpoint can admit other allocations.
if (groups.every((group) => group.remaining.coefficients.size === 0)) {
const consume = (descending) => {
let needed = quantity;
let value = zero;
const taken = new Map();
const ordered = [...groups].sort((a, b) => cmp(a.price, b.price) * descending);
for (const group of ordered) {
const amount = cmp(needed, group.remaining.constant) < 0 ? needed : group.remaining.constant;
taken.set(group, amount);
needed = sub(needed, amount);
value = add(value, mul(amount, group.price));
}
return { needed, value, taken };
};
const lower = consume(1);
const upper = consume(-1);
if (lower.needed.n > 0n) {
this.invalid = true;
return { insufficient: true };
}
if (outside(lower.value, upper.value)) {
this.invalid = true;
return { minimum: lower.value, maximum: upper.value };
}
const endpoint = cmp(lower.value, upper.value) === 0 ? lower : (!rounded
? (cmp(cost, lower.value) === 0 ? lower : (cmp(cost, upper.value) === 0 ? upper : null))
: null);
if (endpoint) {
const moved = [];
for (const [group, amount] of endpoint.taken) {
group.remaining.constant = sub(group.remaining.constant, amount);
if (destination && amount.n) {
const ratio = div(destinationQuantity, quantity);
moved.push({ price: div(group.price, ratio), remaining: expression(mul(amount, ratio)) });
}
}
this.groups.set(account, groups.filter((group) => group.remaining.constant.n));
if (destination)
this.holdings(destination).push(...moved);
this.changeTotal(account, neg(quantity), neg(cost));
if (destination)
this.changeTotal(destination, destinationQuantity, cost);
return null;
}
}
const amountCoefficients = new Map();
const costCoefficients = new Map();
const allocated = [];
for (const group of groups) {
const variable = this.variables++;
const coefficients = new Map([...group.remaining.coefficients]
.map(([key, value]) => [key, neg(value)]));
coefficients.set(variable, one);
this.constraints.push({ coefficients, bound: group.remaining.constant });
amountCoefficients.set(variable, one);
costCoefficients.set(variable, group.price);
allocated.push({ group, variable });
}
this.equality(amountCoefficients, quantity);
// Proportional consumption of a feasible historical witness is itself a
// feasible allocation. Retain the full constraints so later sales may
// still choose a different history.
const ratio = div(quantity, total.quantity);
const average = allocated.map(({ group }) => mul(evaluate(group.remaining, this.witness), ratio));
const averageQuantity = average.reduce(add, zero);
const averageCost = average.reduce((sum, amount, index) => add(sum, mul(amount, allocated[index].group.price)), zero);
const averageFits = cmp(averageQuantity, quantity) === 0 &&
!outside(averageCost, averageCost);
const costConstraints = [];
if (rounded) {
costConstraints.push({ coefficients: costCoefficients, bound: upperCost, strict: true });
costConstraints.push({
coefficients: new Map([...costCoefficients].map(([key, value]) => [key, neg(value)])),
bound: neg(lowerCost), strict: true,
});
}
else {
costConstraints.push({ coefficients: costCoefficients, bound: cost });
costConstraints.push({
coefficients: new Map([...costCoefficients].map(([key, value]) => [key, neg(value)])),
bound: neg(cost),
});
}
const witness = averageFits
? [...this.witness, ...average]
: this.findWitness([...this.constraints, ...costConstraints]);
if (!witness) {
const upper = maximize(this.variables, this.constraints, costCoefficients);
const lower = maximize(this.variables, this.constraints, new Map([...costCoefficients].map(([key, value]) => [key, neg(value)])));
this.invalid = true;
if (upper.status !== 'optimal' || lower.status !== 'optimal')
return { insufficient: true };
return { minimum: neg(lower.value), maximum: upper.value };
}
this.witness = witness;
this.constraints.push(...costConstraints);
const transferred = [];
for (const { group, variable } of allocated) {
group.remaining = subtractVariable(group.remaining, variable);
if (destination) {
const ratio = div(destinationQuantity, quantity);
transferred.push({
price: div(group.price, ratio),
remaining: scale(expression(zero, new Map([[variable, one]])), ratio),
});
}
}
if (cmp(quantity, total.quantity) === 0)
this.groups.set(account, []);
if (destination)
this.holdings(destination).push(...transferred);
this.changeTotal(account, neg(quantity), neg(cost));
if (destination)
this.changeTotal(destination, destinationQuantity, cost);
return null;
}
}
return { AllocationHistory };
};
|