cpsat-js 0.1.0-beta.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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +165 -0
  3. package/build/cpsat.cjs +2 -0
  4. package/build/cpsat.wasm +0 -0
  5. package/dist/generated/cp_model_pb.d.ts +1625 -0
  6. package/dist/generated/cp_model_pb.d.ts.map +1 -0
  7. package/dist/generated/cp_model_pb.js +296 -0
  8. package/dist/generated/cp_model_pb.js.map +1 -0
  9. package/dist/generated/sat_parameters_pb.d.ts +2930 -0
  10. package/dist/generated/sat_parameters_pb.d.ts.map +1 -0
  11. package/dist/generated/sat_parameters_pb.js +427 -0
  12. package/dist/generated/sat_parameters_pb.js.map +1 -0
  13. package/dist/index.d.ts +10 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +8 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/model/constraint.d.ts +16 -0
  18. package/dist/model/constraint.d.ts.map +1 -0
  19. package/dist/model/constraint.js +20 -0
  20. package/dist/model/constraint.js.map +1 -0
  21. package/dist/model/cp-model.d.ts +35 -0
  22. package/dist/model/cp-model.d.ts.map +1 -0
  23. package/dist/model/cp-model.js +175 -0
  24. package/dist/model/cp-model.js.map +1 -0
  25. package/dist/model/domain.d.ts +13 -0
  26. package/dist/model/domain.d.ts.map +1 -0
  27. package/dist/model/domain.js +45 -0
  28. package/dist/model/domain.js.map +1 -0
  29. package/dist/model/int-var.d.ts +37 -0
  30. package/dist/model/int-var.d.ts.map +1 -0
  31. package/dist/model/int-var.js +87 -0
  32. package/dist/model/int-var.js.map +1 -0
  33. package/dist/model/interval-var.d.ts +14 -0
  34. package/dist/model/interval-var.d.ts.map +1 -0
  35. package/dist/model/interval-var.js +17 -0
  36. package/dist/model/interval-var.js.map +1 -0
  37. package/dist/model/linear-expr.d.ts +43 -0
  38. package/dist/model/linear-expr.d.ts.map +1 -0
  39. package/dist/model/linear-expr.js +129 -0
  40. package/dist/model/linear-expr.js.map +1 -0
  41. package/dist/solver/cp-solver.d.ts +36 -0
  42. package/dist/solver/cp-solver.d.ts.map +1 -0
  43. package/dist/solver/cp-solver.js +92 -0
  44. package/dist/solver/cp-solver.js.map +1 -0
  45. package/package.json +58 -0
@@ -0,0 +1,175 @@
1
+ import { create } from '@bufbuild/protobuf';
2
+ import { CpModelProtoSchema, ConstraintProtoSchema, IntegerVariableProtoSchema, AllDifferentConstraintProtoSchema, BoolArgumentProtoSchema, NoOverlapConstraintProtoSchema, CircuitConstraintProtoSchema, LinearConstraintProtoSchema, IntervalConstraintProtoSchema, CpObjectiveProtoSchema, } from '../generated/cp_model_pb.js';
3
+ import { IntVar, BoolVar } from './int-var.js';
4
+ import { BoundedLinearExpression, toLinearExpr } from './linear-expr.js';
5
+ import { IntervalVar } from './interval-var.js';
6
+ import { Constraint } from './constraint.js';
7
+ /**
8
+ * Builder for a CP-SAT model. Mirrors the Python CpModel API.
9
+ *
10
+ * Usage:
11
+ * const model = new CpModel();
12
+ * const x = model.newIntVar(0, 10, 'x');
13
+ * model.maximize(x);
14
+ */
15
+ export class CpModel {
16
+ proto;
17
+ constructor(name) {
18
+ this.proto = create(CpModelProtoSchema, { name: name ?? '' });
19
+ }
20
+ // ── Variable creation ──
21
+ newIntVar(lb, ub, name) {
22
+ const index = this.proto.variables.length;
23
+ this.proto.variables.push(create(IntegerVariableProtoSchema, {
24
+ name,
25
+ domain: [BigInt(lb), BigInt(ub)],
26
+ }));
27
+ return new IntVar(index, name);
28
+ }
29
+ newBoolVar(name) {
30
+ const index = this.proto.variables.length;
31
+ this.proto.variables.push(create(IntegerVariableProtoSchema, {
32
+ name,
33
+ domain: [0n, 1n],
34
+ }));
35
+ return new BoolVar(index, name);
36
+ }
37
+ newConstant(value) {
38
+ const v = BigInt(value);
39
+ const name = `const_${v}`;
40
+ const index = this.proto.variables.length;
41
+ this.proto.variables.push(create(IntegerVariableProtoSchema, {
42
+ name,
43
+ domain: [v, v],
44
+ }));
45
+ return new IntVar(index, name);
46
+ }
47
+ // ── Interval creation ──
48
+ newIntervalVar(start, size, end, name) {
49
+ const ct = this.addConstraintProto();
50
+ ct.name = name;
51
+ ct.constraint = {
52
+ case: 'interval',
53
+ value: create(IntervalConstraintProtoSchema, {
54
+ start: toLinearExpr(start).toProto(),
55
+ size: toLinearExpr(size).toProto(),
56
+ end: toLinearExpr(end).toProto(),
57
+ }),
58
+ };
59
+ return new IntervalVar(this.proto.constraints.length - 1, name);
60
+ }
61
+ // ── Constraints ──
62
+ /** Add a bounded linear constraint: lb <= expr <= ub */
63
+ add(bounded) {
64
+ const ct = this.addConstraintProto();
65
+ const vars = [];
66
+ const coeffs = [];
67
+ for (const [varIdx, coeff] of bounded.expr.terms) {
68
+ vars.push(varIdx);
69
+ coeffs.push(coeff);
70
+ }
71
+ ct.constraint = {
72
+ case: 'linear',
73
+ value: create(LinearConstraintProtoSchema, {
74
+ vars,
75
+ coeffs,
76
+ domain: [bounded.lb - bounded.expr.offset, bounded.ub - bounded.expr.offset],
77
+ }),
78
+ };
79
+ return new Constraint(ct);
80
+ }
81
+ addLinearConstraint(expr, lb, ub) {
82
+ const linearExpr = toLinearExpr(expr);
83
+ return this.add(new BoundedLinearExpression(linearExpr, BigInt(lb), BigInt(ub)));
84
+ }
85
+ addAllDifferent(exprs) {
86
+ const ct = this.addConstraintProto();
87
+ ct.constraint = {
88
+ case: 'allDiff',
89
+ value: create(AllDifferentConstraintProtoSchema, {
90
+ exprs: exprs.map((e) => toLinearExpr(e).toProto()),
91
+ }),
92
+ };
93
+ return new Constraint(ct);
94
+ }
95
+ addBoolOr(literals) {
96
+ const ct = this.addConstraintProto();
97
+ ct.constraint = {
98
+ case: 'boolOr',
99
+ value: create(BoolArgumentProtoSchema, {
100
+ literals: literals.map((lit) => (typeof lit === 'number' ? lit : lit.index)),
101
+ }),
102
+ };
103
+ return new Constraint(ct);
104
+ }
105
+ addBoolAnd(literals) {
106
+ const ct = this.addConstraintProto();
107
+ ct.constraint = {
108
+ case: 'boolAnd',
109
+ value: create(BoolArgumentProtoSchema, {
110
+ literals: literals.map((lit) => (typeof lit === 'number' ? lit : lit.index)),
111
+ }),
112
+ };
113
+ return new Constraint(ct);
114
+ }
115
+ addNoOverlap(intervals) {
116
+ const ct = this.addConstraintProto();
117
+ ct.constraint = {
118
+ case: 'noOverlap',
119
+ value: create(NoOverlapConstraintProtoSchema, {
120
+ intervals: intervals.map((iv) => iv.constraintIndex),
121
+ }),
122
+ };
123
+ return new Constraint(ct);
124
+ }
125
+ addCircuit(arcs) {
126
+ const tails = [];
127
+ const heads = [];
128
+ const literals = [];
129
+ for (const [tail, head, lit] of arcs) {
130
+ tails.push(tail);
131
+ heads.push(head);
132
+ literals.push(typeof lit === 'number' ? lit : lit.index);
133
+ }
134
+ const ct = this.addConstraintProto();
135
+ ct.constraint = {
136
+ case: 'circuit',
137
+ value: create(CircuitConstraintProtoSchema, { tails, heads, literals }),
138
+ };
139
+ return new Constraint(ct);
140
+ }
141
+ // ── Objective ──
142
+ minimize(expr) {
143
+ this.setObjective(expr, false);
144
+ }
145
+ maximize(expr) {
146
+ this.setObjective(expr, true);
147
+ }
148
+ // ── Serialization ──
149
+ toProto() {
150
+ return this.proto;
151
+ }
152
+ // ── Internal ──
153
+ addConstraintProto() {
154
+ const ct = create(ConstraintProtoSchema, {});
155
+ this.proto.constraints.push(ct);
156
+ return ct;
157
+ }
158
+ setObjective(expr, maximize) {
159
+ const linearExpr = toLinearExpr(expr);
160
+ const vars = [];
161
+ const coeffs = [];
162
+ for (const [varIdx, coeff] of linearExpr.terms) {
163
+ vars.push(varIdx);
164
+ // For maximization, negate coefficients (CP-SAT always minimizes)
165
+ coeffs.push(maximize ? -coeff : coeff);
166
+ }
167
+ this.proto.objective = create(CpObjectiveProtoSchema, {
168
+ vars,
169
+ coeffs,
170
+ offset: maximize ? -Number(linearExpr.offset) : Number(linearExpr.offset),
171
+ scalingFactor: maximize ? -1.0 : 1.0,
172
+ });
173
+ }
174
+ }
175
+ //# sourceMappingURL=cp-model.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cp-model.js","sourceRoot":"","sources":["../../src/model/cp-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAEL,kBAAkB,EAElB,qBAAqB,EACrB,0BAA0B,EAC1B,iCAAiC,EACjC,uBAAuB,EACvB,8BAA8B,EAC9B,4BAA4B,EAC5B,2BAA2B,EAC3B,6BAA6B,EAC7B,sBAAsB,GACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAuB,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,uBAAuB,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;;;;;GAOG;AACH,MAAM,OAAO,OAAO;IACD,KAAK,CAAe;IAErC,YAAY,IAAa;QACvB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,kBAAkB,EAAE,EAAE,IAAI,EAAE,IAAI,IAAI,EAAE,EAAE,CAAC,CAAC;IAChE,CAAC;IAED,0BAA0B;IAE1B,SAAS,CAAC,EAAmB,EAAE,EAAmB,EAAE,IAAY;QAC9D,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC;QAC1C,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CACvB,MAAM,CAAC,0BAA0B,EAAE;YACjC,IAAI;YACJ,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;SACjC,CAAC,CACH,CAAC;QACF,OAAO,IAAI,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,UA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AC;IAED,sBAAsB;IAEtB,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,iBAAiB;IAET,kBAAkB;QACxB,MAAM,EAAE,GAAG,MAAM,CAAC,qBAAqB,EAAE,EAAE,CAAC,CAAC;QAC7C,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAChC,OAAO,EAAE,CAAC;IACZ,CAAC;IAEO,YAAY,CAAC,IAAoB,EAAE,QAAiB;QAC1D,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;QACtC,MAAM,IAAI,GAAa,EAAE,CAAC;QAC1B,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,KAAK,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC;YAC/C,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAClB,kEAAkE;YAClE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACzC,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,MAAM,CAAC,sBAAsB,EAAE;YACpD,IAAI;YACJ,MAAM;YACN,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC;YACzE,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG;SACrC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Represents a domain as sorted disjoint intervals [min0, max0, min1, max1, ...].
3
+ * Used for integer variable bounds in CP-SAT.
4
+ */
5
+ export declare class Domain {
6
+ readonly intervals: readonly [bigint, bigint][];
7
+ private constructor();
8
+ static fromRange(min: number | bigint, max: number | bigint): Domain;
9
+ static fromValues(values: (number | bigint)[]): Domain;
10
+ static fromFlattenedIntervals(flat: bigint[]): Domain;
11
+ toFlatBigIntArray(): bigint[];
12
+ }
13
+ //# sourceMappingURL=domain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domain.d.ts","sourceRoot":"","sources":["../../src/model/domain.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,MAAM;IACjB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;IAEhD,OAAO;IAIP,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAIpE,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,MAAM;IAqBtD,MAAM,CAAC,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM;IAQrD,iBAAiB,IAAI,MAAM,EAAE;CAG9B"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Represents a domain as sorted disjoint intervals [min0, max0, min1, max1, ...].
3
+ * Used for integer variable bounds in CP-SAT.
4
+ */
5
+ export class Domain {
6
+ intervals;
7
+ constructor(intervals) {
8
+ this.intervals = intervals;
9
+ }
10
+ static fromRange(min, max) {
11
+ return new Domain([[BigInt(min), BigInt(max)]]);
12
+ }
13
+ static fromValues(values) {
14
+ if (values.length === 0) {
15
+ return new Domain([]);
16
+ }
17
+ const sorted = [...values].map(BigInt).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
18
+ const intervals = [];
19
+ let start = sorted[0];
20
+ let end = sorted[0];
21
+ for (let i = 1; i < sorted.length; i++) {
22
+ if (sorted[i] === end + 1n) {
23
+ end = sorted[i];
24
+ }
25
+ else {
26
+ intervals.push([start, end]);
27
+ start = sorted[i];
28
+ end = sorted[i];
29
+ }
30
+ }
31
+ intervals.push([start, end]);
32
+ return new Domain(intervals);
33
+ }
34
+ static fromFlattenedIntervals(flat) {
35
+ const intervals = [];
36
+ for (let i = 0; i < flat.length; i += 2) {
37
+ intervals.push([flat[i], flat[i + 1]]);
38
+ }
39
+ return new Domain(intervals);
40
+ }
41
+ toFlatBigIntArray() {
42
+ return this.intervals.flatMap(([min, max]) => [min, max]);
43
+ }
44
+ }
45
+ //# sourceMappingURL=domain.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domain.js","sourceRoot":"","sources":["../../src/model/domain.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAO,MAAM;IACR,SAAS,CAA8B;IAEhD,YAAoB,SAA6B;QAC/C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,GAAoB,EAAE,GAAoB;QACzD,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,MAA2B;QAC3C,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxB,OAAO,IAAI,MAAM,CAAC,EAAE,CAAC,CAAC;QACxB,CAAC;QACD,MAAM,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACpF,MAAM,SAAS,GAAuB,EAAE,CAAC;QACzC,IAAI,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;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ression } from './linear-expr.js';
2
+ export type LinearExprLike = LinearExpr | IntVar | number | bigint;
3
+ /**
4
+ * Represents an integer variable in a CP-SAT model.
5
+ * Wraps a variable index and provides expression-building methods.
6
+ */
7
+ export declare class IntVar {
8
+ readonly index: number;
9
+ readonly name: string;
10
+ constructor(index: number, name: string);
11
+ toLinearExpr(): LinearExpr;
12
+ plus(other: LinearExprLike): LinearExpr;
13
+ minus(other: LinearExprLike): LinearExpr;
14
+ times(scalar: number | bigint): LinearExpr;
15
+ negate(): LinearExpr;
16
+ /** expr == other */
17
+ equals(other: LinearExprLike): BoundedLinearExpression;
18
+ /** expr != other — encoded as domain with hole at 0 */
19
+ notEquals(other: LinearExprLike): BoundedLinearExpression;
20
+ /** expr <= other */
21
+ le(other: LinearExprLike): BoundedLinearExpression;
22
+ /** expr >= other */
23
+ ge(other: LinearExprLike): BoundedLinearExpression;
24
+ /** expr < other */
25
+ lt(other: LinearExprLike): BoundedLinearExpression;
26
+ /** expr > other */
27
+ gt(other: LinearExprLike): BoundedLinearExpression;
28
+ /** Negative literal: NOT this variable (for boolean context) */
29
+ not(): number;
30
+ }
31
+ /**
32
+ * A boolean variable (integer variable with domain [0, 1]).
33
+ */
34
+ export declare class BoolVar extends IntVar {
35
+ constructor(index: number, name: string);
36
+ }
37
+ //# sourceMappingURL=int-var.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"int-var.d.ts","sourceRoot":"","sources":["../../src/model/int-var.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,uBAAuB,EAAgB,MAAM,kBAAkB,CAAC;AAKrF,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAEnE;;;GAGG;AACH,qBAAa,MAAM;IACjB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAKvC,YAAY,IAAI,UAAU;IAI1B,IAAI,CAAC,KAAK,EAAE,cAAc,GAAG,UAAU;IAIvC,KAAK,CAAC,KAAK,EAAE,cAAc,GAAG,UAAU;IAIxC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU;IAI1C,MAAM,IAAI,UAAU;IAIpB,oBAAoB;IACpB,MAAM,CAAC,KAAK,EAAE,cAAc,GAAG,uBAAuB;IAOtD,uDAAuD;IACvD,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,uBAAuB;IASzD,oBAAoB;IACpB,EAAE,CAAC,KAAK,EAAE,cAAc,GAAG,uBAAuB;IAOlD,oBAAoB;IACpB,EAAE,CAAC,KAAK,EAAE,cAAc,GAAG,uBAAuB;IAOlD,mBAAmB;IACnB,EAAE,CAAC,KAAK,EAAE,cAAc,GAAG,uBAAuB;IAOlD,mBAAmB;IACnB,EAAE,CAAC,KAAK,EAAE,cAAc,GAAG,uBAAuB;IAOlD,gEAAgE;IAChE,GAAG,IAAI,MAAM;CAGd;AAED;;GAEG;AACH,qBAAa,OAAQ,SAAQ,MAAM;gBACrB,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAGxC"}
@@ -0,0 +1,87 @@
1
+ import { LinearExpr, BoundedLinearExpression, toLinearExpr } from './linear-expr.js';
2
+ const INT_VAR_MIN = -(2 ** 52);
3
+ const INT_VAR_MAX = 2 ** 52;
4
+ /**
5
+ * Represents an integer variable in a CP-SAT model.
6
+ * Wraps a variable index and provides expression-building methods.
7
+ */
8
+ export class IntVar {
9
+ index;
10
+ name;
11
+ constructor(index, name) {
12
+ this.index = index;
13
+ this.name = name;
14
+ }
15
+ toLinearExpr() {
16
+ return LinearExpr.fromVarIndex(this.index);
17
+ }
18
+ plus(other) {
19
+ return this.toLinearExpr().plus(toLinearExpr(other));
20
+ }
21
+ minus(other) {
22
+ return this.toLinearExpr().minus(toLinearExpr(other));
23
+ }
24
+ times(scalar) {
25
+ return this.toLinearExpr().times(scalar);
26
+ }
27
+ negate() {
28
+ return this.toLinearExpr().negate();
29
+ }
30
+ /** expr == other */
31
+ equals(other) {
32
+ const lhs = this.toLinearExpr();
33
+ const rhs = toLinearExpr(other);
34
+ const diff = lhs.minus(rhs);
35
+ return new BoundedLinearExpression(diff, 0n, 0n);
36
+ }
37
+ /** expr != other — encoded as domain with hole at 0 */
38
+ notEquals(other) {
39
+ const lhs = this.toLinearExpr();
40
+ const rhs = toLinearExpr(other);
41
+ const diff = lhs.minus(rhs);
42
+ // CP-SAT encodes != via domain: [INT_MIN, -1] U [1, INT_MAX]
43
+ // We'll handle this specially in CpModel.add()
44
+ return new BoundedLinearExpression(diff, BigInt(INT_VAR_MIN), BigInt(INT_VAR_MAX));
45
+ }
46
+ /** expr <= other */
47
+ le(other) {
48
+ const lhs = this.toLinearExpr();
49
+ const rhs = toLinearExpr(other);
50
+ const diff = lhs.minus(rhs);
51
+ return new BoundedLinearExpression(diff, BigInt(INT_VAR_MIN), 0n);
52
+ }
53
+ /** expr >= other */
54
+ ge(other) {
55
+ const lhs = this.toLinearExpr();
56
+ const rhs = toLinearExpr(other);
57
+ const diff = lhs.minus(rhs);
58
+ return new BoundedLinearExpression(diff, 0n, BigInt(INT_VAR_MAX));
59
+ }
60
+ /** expr < other */
61
+ lt(other) {
62
+ const lhs = this.toLinearExpr();
63
+ const rhs = toLinearExpr(other);
64
+ const diff = lhs.minus(rhs);
65
+ return new BoundedLinearExpression(diff, BigInt(INT_VAR_MIN), -1n);
66
+ }
67
+ /** expr > other */
68
+ gt(other) {
69
+ const lhs = this.toLinearExpr();
70
+ const rhs = toLinearExpr(other);
71
+ const diff = lhs.minus(rhs);
72
+ return new BoundedLinearExpression(diff, 1n, BigInt(INT_VAR_MAX));
73
+ }
74
+ /** Negative literal: NOT this variable (for boolean context) */
75
+ not() {
76
+ return -this.index - 1;
77
+ }
78
+ }
79
+ /**
80
+ * A boolean variable (integer variable with domain [0, 1]).
81
+ */
82
+ export class BoolVar extends IntVar {
83
+ constructor(index, name) {
84
+ super(index, name);
85
+ }
86
+ }
87
+ //# sourceMappingURL=int-var.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"int-var.js","sourceRoot":"","sources":["../../src/model/int-var.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,uBAAuB,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAErF,MAAM,WAAW,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;AAC/B,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,CAAC;AAI5B;;;GAGG;AACH,MAAM,OAAO,MAAM;IACR,KAAK,CAAS;IACd,IAAI,CAAS;IAEtB,YAAY,KAAa,EAAE,IAAY;QACrC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,YAAY;QACV,OAAO,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED,IAAI,CAAC,KAAqB;QACxB,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,KAAK,CAAC,KAAqB;QACzB,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,KAAK,CAAC,MAAuB;QAC3B,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC,MAAM,EAAE,CAAC;IACtC,CAAC;IAED,oBAAoB;IACpB,MAAM,CAAC,KAAqB;QAC1B,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAChC,MAAM,GAAG,GAAG,YAAY,CAAC,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AG,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAChC,MAAM,GAAG,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;QAChC,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC5B,OAAO,IAAI,uBAAuB,CAAC,IAAI,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACrE,CAAC;IAED,mBAAmB;IACnB,EAAE,CAAC,KAAqB;QACtB,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAChC,MAAM,GAAG,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;QAChC,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC5B,OAAO,IAAI,uBAAuB,CAAC,IAAI,EAAE,EAAE,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;IACpE,CAAC;IAED,gEAAgE;IAChE,GAAG;QACD,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;IACzB,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,OAAQ,SAAQ,MAAM;IACjC,YAAY,KAAa,EAAE,IAAY;QACrC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACrB,CAAC;CACF"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Represents an interval variable in a CP-SAT model.
3
+ * Used for scheduling constraints (noOverlap, cumulative).
4
+ *
5
+ * An interval is defined by its constraint index in the model.
6
+ * The constraint enforces: start + size == end.
7
+ */
8
+ export declare class IntervalVar {
9
+ /** Index of the interval constraint in the model's constraints array */
10
+ readonly constraintIndex: number;
11
+ readonly name: string;
12
+ constructor(constraintIndex: number, name: string);
13
+ }
14
+ //# sourceMappingURL=interval-var.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interval-var.d.ts","sourceRoot":"","sources":["../../src/model/interval-var.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,WAAW;IACtB,wEAAwE;IACxE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,eAAe,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAIlD"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Represents an interval variable in a CP-SAT model.
3
+ * Used for scheduling constraints (noOverlap, cumulative).
4
+ *
5
+ * An interval is defined by its constraint index in the model.
6
+ * The constraint enforces: start + size == end.
7
+ */
8
+ export class IntervalVar {
9
+ /** Index of the interval constraint in the model's constraints array */
10
+ constraintIndex;
11
+ name;
12
+ constructor(constraintIndex, name) {
13
+ this.constraintIndex = constraintIndex;
14
+ this.name = name;
15
+ }
16
+ }
17
+ //# sourceMappingURL=interval-var.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interval-var.js","sourceRoot":"","sources":["../../src/model/interval-var.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,OAAO,WAAW;IACtB,wEAAwE;IAC/D,eAAe,CAAS;IACxB,IAAI,CAAS;IAEtB,YAAY,eAAuB,EAAE,IAAY;QAC/C,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF"}
@@ -0,0 +1,43 @@
1
+ import { type LinearExpressionProto } from '../generated/cp_model_pb.js';
2
+ /**
3
+ * Represents a linear expression: sum(coeffs[i] * vars[i]) + offset.
4
+ * Used to build constraints and objectives in CP-SAT models.
5
+ */
6
+ export declare class LinearExpr {
7
+ /** Map from variable index to coefficient */
8
+ readonly terms: ReadonlyMap<number, bigint>;
9
+ readonly offset: bigint;
10
+ constructor(terms: Map<number, bigint>, offset: bigint);
11
+ static fromConstant(value: number | bigint): LinearExpr;
12
+ static fromVarIndex(varIndex: number, coeff?: bigint): LinearExpr;
13
+ plus(other: LinearExpr | number | bigint): LinearExpr;
14
+ minus(other: LinearExpr | number | bigint): LinearExpr;
15
+ times(scalar: number | bigint): LinearExpr;
16
+ negate(): LinearExpr;
17
+ /** expr == value */
18
+ equals(other: LinearExpr | number | bigint): BoundedLinearExpression;
19
+ /** expr <= value */
20
+ le(other: LinearExpr | number | bigint): BoundedLinearExpression;
21
+ /** expr >= value */
22
+ ge(other: LinearExpr | number | bigint): BoundedLinearExpression;
23
+ /** expr < value */
24
+ lt(other: LinearExpr | number | bigint): BoundedLinearExpression;
25
+ /** expr > value */
26
+ gt(other: LinearExpr | number | bigint): BoundedLinearExpression;
27
+ toProto(): LinearExpressionProto;
28
+ }
29
+ /**
30
+ * Represents a bounded linear expression: lb <= expr <= ub.
31
+ * Created by comparison methods on LinearExpr/IntVar.
32
+ */
33
+ export declare class BoundedLinearExpression {
34
+ readonly expr: LinearExpr;
35
+ readonly lb: bigint;
36
+ readonly ub: bigint;
37
+ constructor(expr: LinearExpr, lb: bigint, ub: bigint);
38
+ }
39
+ /** Helper to convert something that could be LinearExpr, number, or has toLinearExpr() */
40
+ export declare function toLinearExpr(value: LinearExpr | number | bigint | {
41
+ toLinearExpr(): LinearExpr;
42
+ }): LinearExpr;
43
+ //# sourceMappingURL=linear-expr.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"linear-expr.d.ts","sourceRoot":"","sources":["../../src/model/linear-expr.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,qBAAqB,EAC3B,MAAM,6BAA6B,CAAC;AAErC;;;GAGG;AACH,qBAAa,UAAU;IACrB,6CAA6C;IAC7C,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC5C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEZ,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM;IAKtD,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GA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{ create } from '@bufbuild/protobuf';
2
+ import { LinearExpressionProtoSchema, } from '../generated/cp_model_pb.js';
3
+ /**
4
+ * Represents a linear expression: sum(coeffs[i] * vars[i]) + offset.
5
+ * Used to build constraints and objectives in CP-SAT models.
6
+ */
7
+ export class LinearExpr {
8
+ /** Map from variable index to coefficient */
9
+ terms;
10
+ offset;
11
+ constructor(terms, offset) {
12
+ this.terms = terms;
13
+ this.offset = offset;
14
+ }
15
+ static fromConstant(value) {
16
+ return new LinearExpr(new Map(), BigInt(value));
17
+ }
18
+ static fromVarIndex(varIndex, coeff = 1n) {
19
+ return new LinearExpr(new Map([[varIndex, coeff]]), 0n);
20
+ }
21
+ plus(other) {
22
+ if (typeof other === 'number' || typeof other === 'bigint') {
23
+ return new LinearExpr(new Map(this.terms), this.offset + BigInt(other));
24
+ }
25
+ const newTerms = new Map(this.terms);
26
+ for (const [varIdx, coeff] of other.terms) {
27
+ const existing = newTerms.get(varIdx) ?? 0n;
28
+ const sum = existing + coeff;
29
+ if (sum === 0n) {
30
+ newTerms.delete(varIdx);
31
+ }
32
+ else {
33
+ newTerms.set(varIdx, sum);
34
+ }
35
+ }
36
+ return new LinearExpr(newTerms, this.offset + other.offset);
37
+ }
38
+ minus(other) {
39
+ if (typeof other === 'number' || typeof other === 'bigint') {
40
+ return new LinearExpr(new Map(this.terms), this.offset - BigInt(other));
41
+ }
42
+ return this.plus(other.times(-1));
43
+ }
44
+ times(scalar) {
45
+ const s = BigInt(scalar);
46
+ if (s === 0n) {
47
+ return LinearExpr.fromConstant(0);
48
+ }
49
+ const newTerms = new Map();
50
+ for (const [varIdx, coeff] of this.terms) {
51
+ newTerms.set(varIdx, coeff * s);
52
+ }
53
+ return new LinearExpr(newTerms, this.offset * s);
54
+ }
55
+ negate() {
56
+ return this.times(-1);
57
+ }
58
+ /** expr == value */
59
+ equals(other) {
60
+ const rhs = typeof other === 'number' || typeof other === 'bigint'
61
+ ? LinearExpr.fromConstant(other) : other;
62
+ const diff = this.minus(rhs);
63
+ return new BoundedLinearExpression(diff, 0n, 0n);
64
+ }
65
+ /** expr <= value */
66
+ le(other) {
67
+ const rhs = typeof other === 'number' || typeof other === 'bigint'
68
+ ? LinearExpr.fromConstant(other) : other;
69
+ const diff = this.minus(rhs);
70
+ return new BoundedLinearExpression(diff, -4503599627370496n, 0n);
71
+ }
72
+ /** expr >= value */
73
+ ge(other) {
74
+ const rhs = typeof other === 'number' || typeof other === 'bigint'
75
+ ? LinearExpr.fromConstant(other) : other;
76
+ const diff = this.minus(rhs);
77
+ return new BoundedLinearExpression(diff, 0n, 4503599627370496n);
78
+ }
79
+ /** expr < value */
80
+ lt(other) {
81
+ const rhs = typeof other === 'number' || typeof other === 'bigint'
82
+ ? LinearExpr.fromConstant(other) : other;
83
+ const diff = this.minus(rhs);
84
+ return new BoundedLinearExpression(diff, -4503599627370496n, -1n);
85
+ }
86
+ /** expr > value */
87
+ gt(other) {
88
+ const rhs = typeof other === 'number' || typeof other === 'bigint'
89
+ ? LinearExpr.fromConstant(other) : other;
90
+ const diff = this.minus(rhs);
91
+ return new BoundedLinearExpression(diff, 1n, 4503599627370496n);
92
+ }
93
+ toProto() {
94
+ const vars = [];
95
+ const coeffs = [];
96
+ for (const [varIdx, coeff] of this.terms) {
97
+ vars.push(varIdx);
98
+ coeffs.push(coeff);
99
+ }
100
+ return create(LinearExpressionProtoSchema, {
101
+ vars,
102
+ coeffs,
103
+ offset: this.offset,
104
+ });
105
+ }
106
+ }
107
+ /**
108
+ * Represents a bounded linear expression: lb <= expr <= ub.
109
+ * Created by comparison methods on LinearExpr/IntVar.
110
+ */
111
+ export class BoundedLinearExpression {
112
+ expr;
113
+ lb;
114
+ ub;
115
+ constructor(expr, lb, ub) {
116
+ this.expr = expr;
117
+ this.lb = lb;
118
+ this.ub = ub;
119
+ }
120
+ }
121
+ /** Helper to convert something that could be LinearExpr, number, or has toLinearExpr() */
122
+ export function toLinearExpr(value) {
123
+ if (value instanceof LinearExpr)
124
+ return value;
125
+ if (typeof value === 'number' || typeof value === 'bigint')
126
+ return LinearExpr.fromConstant(value);
127
+ return value.toLinearExpr();
128
+ }
129
+ //# sourceMappingURL=linear-expr.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"linear-expr.js","sourceRoot":"","sources":["../../src/model/linear-expr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EACL,2BAA2B,GAE5B,MAAM,6BAA6B,CAAC;AAErC;;;GAGG;AACH,MAAM,OAAO,UAAU;IACrB,6CAA6C;IACpC,KAAK,CAA8B;IACnC,MAAM,CAAS;IAExB,YAAY,KAA0B,EAAE,MAAc;QACpD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,KAAsB;QACxC,OAAO,IAAI,UAAU,CAAC,IAAI,GAAG,EAAE,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,QAAgB,EAAE,QAAgB,EAAE;QACtD,OAAO,IAAI,UAAU,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC1D,CAAC;IAED,IAAI,CAAC,KAAmC;QACtC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC3D,OAAO,IAAI,UAAU,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QAC1E,CAAC;QACD,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACrC,KAAK,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI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AI,CAAa;IACjB,EAAE,CAAS;IACX,EAAE,CAAS;IAEpB,YAAY,IAAgB,EAAE,EAAU,EAAE,EAAU;QAClD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACf,CAAC;CACF;AAED,0FAA0F;AAC1F,MAAM,UAAU,YAAY,CAAC,KAAoE;IAC/F,IAAI,KAAK,YAAY,UAAU;QAAE,OAAO,KAAK,CAAC;IAC9C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,UAAU,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAClG,OAAO,KAAK,CAAC,YAAY,EAAE,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { CpSolverStatus, type CpSolverResponse } from '../generated/cp_model_pb.js';
2
+ import type { CpModel } from '../model/cp-model.js';
3
+ import type { IntVar } from '../model/int-var.js';
4
+ export { CpSolverStatus };
5
+ export interface CpSolverOptions {
6
+ /** Custom path resolver for the WASM binary */
7
+ locateFile?: (path: string) => string;
8
+ }
9
+ export interface SolverParams {
10
+ maxTimeInSeconds?: number;
11
+ numWorkers?: number;
12
+ }
13
+ export interface CpSolverResult {
14
+ status: CpSolverStatus;
15
+ objectiveValue: number;
16
+ bestObjectiveBound: number;
17
+ wallTime: number;
18
+ /** Get the value of a variable in the solution */
19
+ value(variable: IntVar): number;
20
+ /** Raw response proto */
21
+ response: CpSolverResponse;
22
+ }
23
+ /**
24
+ * Loads the WASM module and solves CP-SAT models.
25
+ *
26
+ * Usage:
27
+ * const solver = await CpSolver.create();
28
+ * const result = solver.solve(model);
29
+ */
30
+ export declare class CpSolver {
31
+ private module;
32
+ private constructor();
33
+ static create(options?: CpSolverOptions): Promise<CpSolver>;
34
+ solve(model: CpModel, params?: SolverParams): CpSolverResult;
35
+ }
36
+ //# sourceMappingURL=cp-solver.d.ts.map