cpsat-js 1.3.0 → 1.4.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.
- package/README.md +39 -0
- package/dist/proto.d.ts +16 -0
- package/dist/proto.d.ts.map +1 -0
- package/dist/proto.js +14 -0
- package/dist/proto.js.map +1 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -89,6 +89,8 @@ if (result.status === CpSolverStatus.OPTIMAL) {
|
|
|
89
89
|
- `addBoolOr([...literals])` / `addBoolAnd([...literals])` — boolean logic
|
|
90
90
|
- `addNoOverlap([...intervals])` — scheduling / disjunctive constraints
|
|
91
91
|
- `addCircuit([[tail, head, literal], ...])` — routing / TSP
|
|
92
|
+
- `addMultiplicationEquality`, `addDivisionEquality`, `addModuloEquality` — products, integer division, remainders
|
|
93
|
+
- `addMaxEquality` / `addMinEquality` — maximum / minimum of expressions
|
|
92
94
|
- `minimize(expr)` / `maximize(expr)` — optimization objectives
|
|
93
95
|
- `.onlyEnforceIf(literal)` — conditional enforcement (half-reification)
|
|
94
96
|
|
|
@@ -164,9 +166,29 @@ Builder for constraint programming models.
|
|
|
164
166
|
- `addBoolAnd(literals): Constraint`
|
|
165
167
|
- `addNoOverlap(intervals): Constraint`
|
|
166
168
|
- `addCircuit(arcs): Constraint`
|
|
169
|
+
- `addMultiplicationEquality(target, exprs): Constraint` — `target == exprs[0] * exprs[1] * ...`
|
|
170
|
+
- `addDivisionEquality(target, num, denom): Constraint` — `target == num / denom`, rounded towards zero
|
|
171
|
+
- `addModuloEquality(target, expr, mod): Constraint` — `target == expr % mod`, with `mod > 0`
|
|
172
|
+
- `addMaxEquality(target, exprs): Constraint` / `addMinEquality(target, exprs): Constraint`
|
|
167
173
|
- `minimize(expr)` / `maximize(expr)`
|
|
168
174
|
- `addHint(variable, value)` / `clearHints()` — suggest where the search should start
|
|
169
175
|
|
|
176
|
+
#### Non-linear constraints
|
|
177
|
+
|
|
178
|
+
CP-SAT only accepts multiplication, division and modulo over expressions with at most one variable, such as `x`, `x.times(2)` or `x.minus(3)`. Passing `x.plus(y)` throws when the constraint is built. To use it, introduce an intermediate variable:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
const x = model.newIntVar(0, 10, 'x');
|
|
182
|
+
const y = model.newIntVar(0, 10, 'y');
|
|
183
|
+
const sum = model.newIntVar(0, 20, 'sum');
|
|
184
|
+
const sq = model.newIntVar(0, 400, 'sq');
|
|
185
|
+
|
|
186
|
+
model.add(sum.equals(x.plus(y)));
|
|
187
|
+
model.addMultiplicationEquality(sq, [sum, sum]); // sq == (x + y)^2
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`addMaxEquality` and `addMinEquality` accept any linear expression.
|
|
191
|
+
|
|
170
192
|
#### Hints
|
|
171
193
|
|
|
172
194
|
A hint is advisory. It constrains nothing, cannot change the optimal value, and a wrong
|
|
@@ -275,6 +297,23 @@ What `onSolution` receives: everything `CpSolverResult` has, including `value()`
|
|
|
275
297
|
The shape is deliberately the same as a final result, so an incumbent and an answer can
|
|
276
298
|
be read by the same code.
|
|
277
299
|
|
|
300
|
+
### `cpsat-js/proto`
|
|
301
|
+
|
|
302
|
+
The protobuf boundary, for solving a model somewhere other than this WASM — native
|
|
303
|
+
OR-Tools reads the same `CpModelProto` bytes:
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
import { toBinary, fromBinary, CpModelProtoSchema, CpSolverResponseSchema } from 'cpsat-js/proto';
|
|
307
|
+
|
|
308
|
+
const bytes = toBinary(CpModelProtoSchema, model.toProto());
|
|
309
|
+
// ...solve `bytes` elsewhere, get response bytes back...
|
|
310
|
+
const response = fromBinary(CpSolverResponseSchema, responseBytes);
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Exports `CpModelProtoSchema`, `CpSolverResponseSchema` and `SatParametersSchema`, with
|
|
314
|
+
`create`/`toBinary`/`fromBinary` re-exported from `@bufbuild/protobuf` so encoding uses the
|
|
315
|
+
same runtime the schemas were generated against.
|
|
316
|
+
|
|
278
317
|
## Architecture
|
|
279
318
|
|
|
280
319
|
- **TypeScript wrapper** builds a `CpModelProto` via a fluent API
|
package/dist/proto.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The protobuf boundary, for solving a model somewhere other than this WASM —
|
|
3
|
+
* native OR-Tools, say, which reads the same CpModelProto bytes.
|
|
4
|
+
*
|
|
5
|
+
* import { toBinary, CpModelProtoSchema } from 'cpsat-js/proto';
|
|
6
|
+
* const bytes = toBinary(CpModelProtoSchema, model.toProto());
|
|
7
|
+
*
|
|
8
|
+
* The protobuf runtime is re-exported with the schemas so a caller encodes with
|
|
9
|
+
* the same @bufbuild/protobuf instance the schemas were generated against.
|
|
10
|
+
*/
|
|
11
|
+
export { create, toBinary, fromBinary } from '@bufbuild/protobuf';
|
|
12
|
+
export { CpModelProtoSchema, CpSolverResponseSchema } from './generated/cp_model_pb.js';
|
|
13
|
+
export type { CpModelProto, CpSolverResponse } from './generated/cp_model_pb.js';
|
|
14
|
+
export { SatParametersSchema } from './generated/sat_parameters_pb.js';
|
|
15
|
+
export type { SatParameters } from './generated/sat_parameters_pb.js';
|
|
16
|
+
//# sourceMappingURL=proto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proto.d.ts","sourceRoot":"","sources":["../src/proto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACxF,YAAY,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,YAAY,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC"}
|
package/dist/proto.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The protobuf boundary, for solving a model somewhere other than this WASM —
|
|
3
|
+
* native OR-Tools, say, which reads the same CpModelProto bytes.
|
|
4
|
+
*
|
|
5
|
+
* import { toBinary, CpModelProtoSchema } from 'cpsat-js/proto';
|
|
6
|
+
* const bytes = toBinary(CpModelProtoSchema, model.toProto());
|
|
7
|
+
*
|
|
8
|
+
* The protobuf runtime is re-exported with the schemas so a caller encodes with
|
|
9
|
+
* the same @bufbuild/protobuf instance the schemas were generated against.
|
|
10
|
+
*/
|
|
11
|
+
export { create, toBinary, fromBinary } from '@bufbuild/protobuf';
|
|
12
|
+
export { CpModelProtoSchema, CpSolverResponseSchema } from './generated/cp_model_pb.js';
|
|
13
|
+
export { SatParametersSchema } from './generated/sat_parameters_pb.js';
|
|
14
|
+
//# sourceMappingURL=proto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proto.js","sourceRoot":"","sources":["../src/proto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAExF,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cpsat-js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "WebAssembly port of Google OR-Tools CP-SAT constraint programming solver",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.portable.js",
|
|
@@ -19,6 +19,10 @@
|
|
|
19
19
|
"./portable": {
|
|
20
20
|
"types": "./dist/index.d.ts",
|
|
21
21
|
"default": "./dist/index.portable.js"
|
|
22
|
+
},
|
|
23
|
+
"./proto": {
|
|
24
|
+
"types": "./dist/proto.d.ts",
|
|
25
|
+
"default": "./dist/proto.js"
|
|
22
26
|
}
|
|
23
27
|
},
|
|
24
28
|
"files": [
|