gdp-wired 0.26.0 → 0.27.0

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.
@@ -9,7 +9,8 @@ export type SimplexVerdict = {
9
9
  };
10
10
  export declare class Simplex {
11
11
  readonly integral: boolean;
12
- constructor(integral?: boolean);
12
+ readonly boundsOnly: boolean;
13
+ constructor(integral?: boolean, boundsOnly?: boolean);
13
14
  private index;
14
15
  private lo;
15
16
  private hi;
@@ -20,12 +21,36 @@ export declare class Simplex {
20
21
  private rows;
21
22
  private colOf;
22
23
  pivots: number;
24
+ private nnzN;
25
+ private at;
26
+ private place;
23
27
  private varOf;
24
28
  private slackOf;
25
29
  load(rows: {
26
30
  lin: Lin;
27
31
  strict?: boolean;
28
32
  }[]): SimplexVerdict;
33
+ private ins;
34
+ private inLo;
35
+ private inHi;
36
+ private falseIn;
37
+ private crossing;
38
+ private moved;
39
+ private movedAt;
40
+ private settles;
41
+ assert(id: number, lin: Lin, strict?: boolean): void;
42
+ retract(id: number): void;
43
+ changes: number;
44
+ private move;
45
+ private vers;
46
+ version(j: number): number;
47
+ value(j: number): number;
48
+ settle(): SimplexVerdict;
49
+ indexOf(name: string): number | undefined;
50
+ low(j: number): number;
51
+ high(j: number): number;
52
+ lowWhy(j: number): number;
53
+ highWhy(j: number): number;
29
54
  private shift;
30
55
  check(maxPivots?: number): SimplexVerdict;
31
56
  private pivot;
@@ -10,7 +10,7 @@
10
10
  // row by its coefficients' divisor gives, which the exact elimination makes as well. Over the rationals (`integral`
11
11
  // false) nothing is rounded, and a strict row's bound is moved in by a hair (1e-7, relative): the floats only pick a
12
12
  // conflict's rows, which the exact elimination then refutes, strictness and all, or doesn't.
13
- import { gcd } from "../core/solver.js";
13
+ import { costs, gcd } from "../core/solver.js";
14
14
  const eps = 1e-9;
15
15
  // A bound's tolerance: relative past 1 (values drift by a few ulps a pivot).
16
16
  const tol = (b) => 1e-7 * Math.max(1, Math.abs(b));
@@ -38,8 +38,11 @@ function formOf(lin) {
38
38
  }
39
39
  export class Simplex {
40
40
  integral;
41
- constructor(integral = true) {
41
+ boundsOnly;
42
+ // `boundsOnly`: only the bounds are kept (assert, retract and their versions; no settle or check).
43
+ constructor(integral = true, boundsOnly = false) {
42
44
  this.integral = integral;
45
+ this.boundsOnly = boundsOnly;
43
46
  }
44
47
  index = new Map();
45
48
  lo = [];
@@ -53,6 +56,18 @@ export class Simplex {
53
56
  rows = [];
54
57
  colOf = [];
55
58
  pivots = 0;
59
+ nnzN = 0;
60
+ at = new WeakMap();
61
+ // (a row's variable and coefficient, kept per form)
62
+ place(lin) {
63
+ let at = this.at.get(lin);
64
+ if (at === undefined) {
65
+ const f = formOf(lin);
66
+ at = f === null ? null : 'single' in f ? { j: this.varOf(f.single), c: f.c } : { j: this.slackOf(f), c: f.g };
67
+ this.at.set(lin, at);
68
+ }
69
+ return at;
70
+ }
56
71
  varOf(name) {
57
72
  let j = this.index.get(name);
58
73
  if (j !== undefined)
@@ -88,6 +103,7 @@ export class Simplex {
88
103
  }
89
104
  const s = this.varOf(f.slack), r = this.rows.length;
90
105
  this.rows.push({ basic: s, coef });
106
+ this.nnzN += coef.size;
91
107
  this.rowOf[s] = r;
92
108
  for (const j of coef.keys())
93
109
  this.colOf[j].add(r);
@@ -136,19 +152,16 @@ export class Simplex {
136
152
  else
137
153
  tighten(j, false, b + d, i);
138
154
  };
139
- for (const [i, r] of rows.entries()) {
140
- const f = formOf(r.lin), k = r.lin['1'] ?? 0;
141
- if (f === null) {
155
+ for (let i = 0; i < rows.length; i++) {
156
+ const r = rows[i], k = r.lin['1'] ?? 0;
157
+ const at = this.place(r.lin);
158
+ if (at === null) {
142
159
  if (k > 0 || (!integral && r.strict && k >= 0))
143
160
  return { kind: 'conflict', rows: [i] };
144
161
  continue;
145
162
  }
146
- if ('single' in f) {
147
- bound(f.c, this.varOf(f.single), k, i, !integral && r.strict);
148
- continue;
149
- }
150
- // g·form + k <= 0
151
- bound(f.g, this.slackOf(f), k, i, !integral && r.strict);
163
+ // c·x + k <= 0 (a slack's: g·form + k <= 0)
164
+ bound(at.c, at.j, k, i, !integral && r.strict);
152
165
  }
153
166
  for (let j = 0; j < this.val.length; j++)
154
167
  if (this.lo[j] > this.hi[j] + eps)
@@ -169,6 +182,125 @@ export class Simplex {
169
182
  }
170
183
  return { kind: 'sat' };
171
184
  }
185
+ // Incremental use, a node's rows as the last node's with some in and some out (kernel/cdcl.ts's search): each row's
186
+ // bound asserted and retracted by an id, a variable's bounds the tightest of those in (the first asserted among
187
+ // equals); `settle` then finds a conflict among them (a constant row that is false, two bounds that cross: the least
188
+ // such variable) or moves each nonbasic value whose bounds changed into them, and `check` goes on as after `load`, a
189
+ // conflict's rows given by their ids. The basic values are computed afresh from their rows every 16 settles.
190
+ ins = new Map();
191
+ inLo = [];
192
+ inHi = [];
193
+ falseIn = new Set();
194
+ crossing = new Set();
195
+ moved = [];
196
+ movedAt = [];
197
+ settles = 0;
198
+ assert(id, lin, strict) {
199
+ const at = this.place(lin), k = lin['1'] ?? 0;
200
+ if (at === null) {
201
+ this.ins.set(id, null);
202
+ if (k > 0 || (!this.integral && strict && k >= 0))
203
+ this.falseIn.add(id);
204
+ return;
205
+ }
206
+ const { j, c } = at, b0 = c > 0 ? -k / c : k / -c, upper = c > 0, d = !this.integral && strict ? 1e-7 * Math.max(1, Math.abs(b0)) : 0;
207
+ const b = this.integral ? (upper ? Math.floor(b0 + eps) : Math.ceil(b0 - eps)) : upper ? b0 - d : b0 + d;
208
+ this.ins.set(id, { j, upper, b });
209
+ ((upper ? this.inHi : this.inLo)[j] ??= []).push([b, id]);
210
+ if (upper ? b < this.hi[j] : b > this.lo[j]) {
211
+ if (upper) {
212
+ this.hi[j] = b;
213
+ this.hiWhy[j] = id;
214
+ }
215
+ else {
216
+ this.lo[j] = b;
217
+ this.loWhy[j] = id;
218
+ }
219
+ this.move(j);
220
+ }
221
+ }
222
+ retract(id) {
223
+ const a = this.ins.get(id);
224
+ this.ins.delete(id);
225
+ if (a === undefined)
226
+ return;
227
+ if (a === null) {
228
+ this.falseIn.delete(id);
229
+ return;
230
+ }
231
+ const { j, upper } = a, list = (upper ? this.inHi : this.inLo)[j];
232
+ list.splice(list.findIndex(x => x[1] === id), 1);
233
+ if ((upper ? this.hiWhy : this.loWhy)[j] !== id)
234
+ return;
235
+ let b = upper ? Infinity : -Infinity, why = -1;
236
+ for (const [x, i] of list)
237
+ if (upper ? x < b : x > b) {
238
+ b = x;
239
+ why = i;
240
+ }
241
+ if (upper) {
242
+ this.hi[j] = b;
243
+ this.hiWhy[j] = why;
244
+ }
245
+ else {
246
+ this.lo[j] = b;
247
+ this.loWhy[j] = why;
248
+ }
249
+ this.move(j);
250
+ }
251
+ // (every change of any bound, counted)
252
+ changes = 0;
253
+ move(j) {
254
+ this.vers[j] = (this.vers[j] ?? 0) + 1;
255
+ this.changes++;
256
+ if (!this.boundsOnly && this.movedAt[j] !== this.settles + 1) {
257
+ this.movedAt[j] = this.settles + 1;
258
+ this.moved.push(j);
259
+ }
260
+ }
261
+ vers = [];
262
+ // How many times a variable's bounds have changed.
263
+ version(j) { return this.vers[j] ?? 0; }
264
+ value(j) { return this.val[j]; }
265
+ settle() {
266
+ for (const id of this.falseIn)
267
+ return { kind: 'conflict', rows: [id] };
268
+ for (const j of this.moved) {
269
+ if (this.lo[j] > this.hi[j] + eps)
270
+ this.crossing.add(j);
271
+ else
272
+ this.crossing.delete(j);
273
+ }
274
+ if (this.crossing.size) {
275
+ let j = Infinity;
276
+ for (const x of this.crossing)
277
+ if (x < j)
278
+ j = x;
279
+ return { kind: 'conflict', rows: [this.loWhy[j], this.hiWhy[j]].filter(x => x >= 0) };
280
+ }
281
+ for (const j of this.moved) {
282
+ if (this.rowOf[j] >= 0)
283
+ continue;
284
+ const x = this.val[j], to = x < this.lo[j] ? this.lo[j] : x > this.hi[j] ? this.hi[j] : x;
285
+ if (to !== x)
286
+ this.shift(j, to - x);
287
+ }
288
+ this.moved = [];
289
+ if (++this.settles % 16 === 0)
290
+ for (const row of this.rows) {
291
+ let value = 0;
292
+ for (const [j, c] of row.coef)
293
+ value += c * this.val[j];
294
+ this.val[row.basic] = value;
295
+ }
296
+ return { kind: 'sat' };
297
+ }
298
+ // A variable's index by name (undefined where no row has read it); its bounds and the ids that give them.
299
+ indexOf(name) { return this.index.get(name); }
300
+ low(j) { return this.lo[j]; }
301
+ high(j) { return this.hi[j]; }
302
+ lowWhy(j) { return this.loWhy[j]; }
303
+ highWhy(j) { return this.hiWhy[j]; }
172
304
  shift(j, d) {
173
305
  this.val[j] += d;
174
306
  for (const r of this.colOf[j]) {
@@ -221,6 +353,7 @@ export class Simplex {
221
353
  ecoef.set(b, 1 / a);
222
354
  for (const j of row.coef.keys())
223
355
  this.colOf[j].delete(r);
356
+ this.nnzN += ecoef.size - row.coef.size;
224
357
  row.basic = e;
225
358
  row.coef = ecoef;
226
359
  this.rowOf[e] = r;
@@ -228,10 +361,11 @@ export class Simplex {
228
361
  for (const j of ecoef.keys())
229
362
  this.colOf[j].add(r);
230
363
  // Every other row reading x_e: substitute.
364
+ costs.pivotRows += this.colOf[e].size;
231
365
  for (const q of [...this.colOf[e]]) {
232
366
  if (q === r)
233
367
  continue;
234
- const other = this.rows[q], c = other.coef.get(e);
368
+ const other = this.rows[q], c = other.coef.get(e), before = other.coef.size;
235
369
  other.coef.delete(e);
236
370
  this.colOf[e].delete(q);
237
371
  for (const [j, d] of ecoef) {
@@ -246,6 +380,7 @@ export class Simplex {
246
380
  other.coef.set(j, x);
247
381
  }
248
382
  }
383
+ this.nnzN += other.coef.size - before;
249
384
  }
250
385
  this.colOf[e].delete(r);
251
386
  for (const j of ecoef.keys())
@@ -265,7 +400,6 @@ export class Simplex {
265
400
  return x <= tol(x);
266
401
  }
267
402
  get size() { return this.rows.length; }
268
- get nnz() { let n = 0; for (const r of this.rows)
269
- n += r.coef.size; return n; }
403
+ get nnz() { return this.nnzN; }
270
404
  get vars() { return this.val.length; }
271
405
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gdp-wired",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "description": "An automatic prover for stateful rules (budgets, caps, windows, workflows, policies): laws proven with no proof to write, checked again in Lean, counterexamples found on the GPU, the rule shipped as code",
5
5
  "keywords": [
6
6
  "verification",