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.
- package/LICENSE +21 -0
- package/README.md +165 -0
- package/build/cpsat.cjs +2 -0
- package/build/cpsat.wasm +0 -0
- package/dist/generated/cp_model_pb.d.ts +1625 -0
- package/dist/generated/cp_model_pb.d.ts.map +1 -0
- package/dist/generated/cp_model_pb.js +296 -0
- package/dist/generated/cp_model_pb.js.map +1 -0
- package/dist/generated/sat_parameters_pb.d.ts +2930 -0
- package/dist/generated/sat_parameters_pb.d.ts.map +1 -0
- package/dist/generated/sat_parameters_pb.js +427 -0
- package/dist/generated/sat_parameters_pb.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/model/constraint.d.ts +16 -0
- package/dist/model/constraint.d.ts.map +1 -0
- package/dist/model/constraint.js +20 -0
- package/dist/model/constraint.js.map +1 -0
- package/dist/model/cp-model.d.ts +35 -0
- package/dist/model/cp-model.d.ts.map +1 -0
- package/dist/model/cp-model.js +175 -0
- package/dist/model/cp-model.js.map +1 -0
- package/dist/model/domain.d.ts +13 -0
- package/dist/model/domain.d.ts.map +1 -0
- package/dist/model/domain.js +45 -0
- package/dist/model/domain.js.map +1 -0
- package/dist/model/int-var.d.ts +37 -0
- package/dist/model/int-var.d.ts.map +1 -0
- package/dist/model/int-var.js +87 -0
- package/dist/model/int-var.js.map +1 -0
- package/dist/model/interval-var.d.ts +14 -0
- package/dist/model/interval-var.d.ts.map +1 -0
- package/dist/model/interval-var.js +17 -0
- package/dist/model/interval-var.js.map +1 -0
- package/dist/model/linear-expr.d.ts +43 -0
- package/dist/model/linear-expr.d.ts.map +1 -0
- package/dist/model/linear-expr.js +129 -0
- package/dist/model/linear-expr.js.map +1 -0
- package/dist/solver/cp-solver.d.ts +36 -0
- package/dist/solver/cp-solver.d.ts.map +1 -0
- package/dist/solver/cp-solver.js +92 -0
- package/dist/solver/cp-solver.js.map +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cp-solver.d.ts","sourceRoot":"","sources":["../../src/solver/cp-solver.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,cAAc,EACd,KAAK,gBAAgB,EACtB,MAAM,6BAA6B,CAAC;AAIrC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,cAAc,EAAE,CAAC;AAE1B,MAAM,WAAW,eAAe;IAC9B,+CAA+C;IAC/C,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CACvC;AAED,MAAM,WAAW,YAAY;IAC3B,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,cAAc,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,kDAAkD;IAClD,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IAChC,yBAAyB;IACzB,QAAQ,EAAE,gBAAgB,CAAC;CAC5B;AAWD;;;;;;GAMG;AACH,qBAAa,QAAQ;IACnB,OAAO,CAAC,MAAM,CAAc;IAE5B,OAAO;WAIM,MAAM,CAAC,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC;IAOjE,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,cAAc;CAuD7D"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { create, toBinary, fromBinary } from '@bufbuild/protobuf';
|
|
2
|
+
import { CpModelProtoSchema, CpSolverResponseSchema, CpSolverStatus, } from '../generated/cp_model_pb.js';
|
|
3
|
+
import { SatParametersSchema, } from '../generated/sat_parameters_pb.js';
|
|
4
|
+
export { CpSolverStatus };
|
|
5
|
+
/**
|
|
6
|
+
* Loads the WASM module and solves CP-SAT models.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* const solver = await CpSolver.create();
|
|
10
|
+
* const result = solver.solve(model);
|
|
11
|
+
*/
|
|
12
|
+
export class CpSolver {
|
|
13
|
+
module;
|
|
14
|
+
constructor(module) {
|
|
15
|
+
this.module = module;
|
|
16
|
+
}
|
|
17
|
+
static async create(options) {
|
|
18
|
+
// Dynamic import of the Emscripten-generated JS glue
|
|
19
|
+
const createModule = await loadWasmFactory(options?.locateFile);
|
|
20
|
+
const module = await createModule();
|
|
21
|
+
return new CpSolver(module);
|
|
22
|
+
}
|
|
23
|
+
solve(model, params) {
|
|
24
|
+
const modelProto = model.toProto();
|
|
25
|
+
const modelBytes = toBinary(CpModelProtoSchema, modelProto);
|
|
26
|
+
const satParams = create(SatParametersSchema, {});
|
|
27
|
+
// Force single worker for WASM (no threading support in MVP)
|
|
28
|
+
satParams.numWorkers = 1;
|
|
29
|
+
if (params?.maxTimeInSeconds !== undefined) {
|
|
30
|
+
satParams.maxTimeInSeconds = params.maxTimeInSeconds;
|
|
31
|
+
}
|
|
32
|
+
if (params?.numWorkers !== undefined) {
|
|
33
|
+
satParams.numWorkers = params.numWorkers;
|
|
34
|
+
}
|
|
35
|
+
const paramsBytes = toBinary(SatParametersSchema, satParams);
|
|
36
|
+
// Allocate WASM heap memory for model
|
|
37
|
+
const modelPtr = this.module._malloc(modelBytes.length);
|
|
38
|
+
this.module.HEAPU8.set(modelBytes, modelPtr);
|
|
39
|
+
// Allocate WASM heap memory for params
|
|
40
|
+
const paramsPtr = this.module._malloc(paramsBytes.length);
|
|
41
|
+
this.module.HEAPU8.set(paramsBytes, paramsPtr);
|
|
42
|
+
let resultLen;
|
|
43
|
+
try {
|
|
44
|
+
resultLen = this.module._solve(modelPtr, modelBytes.length, paramsPtr, paramsBytes.length);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
this.module._free(modelPtr);
|
|
48
|
+
this.module._free(paramsPtr);
|
|
49
|
+
}
|
|
50
|
+
// Read result from WASM memory
|
|
51
|
+
const resultPtr = this.module._get_result_ptr();
|
|
52
|
+
const resultBytes = new Uint8Array(this.module.HEAPU8.buffer, resultPtr, resultLen);
|
|
53
|
+
// Copy before freeing
|
|
54
|
+
const resultCopy = new Uint8Array(resultBytes);
|
|
55
|
+
this.module._free_result();
|
|
56
|
+
const response = fromBinary(CpSolverResponseSchema, resultCopy);
|
|
57
|
+
return {
|
|
58
|
+
status: response.status,
|
|
59
|
+
objectiveValue: response.objectiveValue,
|
|
60
|
+
bestObjectiveBound: response.bestObjectiveBound,
|
|
61
|
+
wallTime: response.wallTime,
|
|
62
|
+
value(variable) {
|
|
63
|
+
return Number(response.solution[variable.index]);
|
|
64
|
+
},
|
|
65
|
+
response,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async function loadWasmFactory(locateFile) {
|
|
70
|
+
try {
|
|
71
|
+
// Emscripten outputs CommonJS; use createRequire in ESM context
|
|
72
|
+
const { createRequire } = await import('node:module');
|
|
73
|
+
const { fileURLToPath } = await import('node:url');
|
|
74
|
+
const { dirname, join } = await import('node:path');
|
|
75
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
76
|
+
const __dirname = dirname(__filename);
|
|
77
|
+
const require = createRequire(import.meta.url);
|
|
78
|
+
const factory = require(join(__dirname, '..', '..', 'build', 'cpsat.cjs'));
|
|
79
|
+
if (typeof factory !== 'function') {
|
|
80
|
+
throw new Error('WASM module did not export a factory function');
|
|
81
|
+
}
|
|
82
|
+
if (locateFile) {
|
|
83
|
+
return () => factory({ locateFile });
|
|
84
|
+
}
|
|
85
|
+
return factory;
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
throw new Error('Failed to load cpsat.wasm. Ensure the WASM build has been completed. ' +
|
|
89
|
+
`Run \`npm run build:wasm\` to compile the WASM binary. (${err})`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=cp-solver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cp-solver.js","sourceRoot":"","sources":["../../src/solver/cp-solver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,cAAc,GAEf,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,mBAAmB,GACpB,MAAM,mCAAmC,CAAC;AAI3C,OAAO,EAAE,cAAc,EAAE,CAAC;AAgC1B;;;;;;GAMG;AACH,MAAM,OAAO,QAAQ;IACX,MAAM,CAAc;IAE5B,YAAoB,MAAmB;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAyB;QAC3C,qDAAqD;QACrD,MAAM,YAAY,GAAG,MAAM,eAAe,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;QAChE,MAAM,MAAM,GAAG,MAAM,YAAY,EAAiB,CAAC;QACnD,OAAO,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,KAAc,EAAE,MAAqB;QACzC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;QACnC,MAAM,UAAU,GAAG,QAAQ,CAAC,kBAAkB,EAAE,UAAU,CAAC,CAAC;QAE5D,MAAM,SAAS,GAAG,MAAM,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAC;QAClD,6DAA6D;QAC7D,SAAS,CAAC,UAAU,GAAG,CAAC,CAAC;QACzB,IAAI,MAAM,EAAE,gBAAgB,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;QACnE,CAAC;QACD,IAAI,UAAU,EAAE,CAAC;YACf,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;QACvC,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,uEAAuE;YACvE,2DAA2D,GAAG,GAAG,CAClE,CAAC;IACJ,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cpsat-js",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "WebAssembly port of Google OR-Tools CP-SAT constraint programming solver",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./cpsat.wasm": "./build/cpsat.wasm"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/",
|
|
17
|
+
"build/cpsat.cjs",
|
|
18
|
+
"build/cpsat.wasm"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build:wasm": "./build.sh",
|
|
22
|
+
"build:proto": "npx buf generate src/proto",
|
|
23
|
+
"build:ts": "tsc",
|
|
24
|
+
"build": "npm run build:proto && npm run build:ts",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"test:unit": "vitest run test/unit",
|
|
27
|
+
"test:integration": "vitest run test/integration"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"or-tools",
|
|
31
|
+
"cp-sat",
|
|
32
|
+
"constraint-programming",
|
|
33
|
+
"optimization",
|
|
34
|
+
"solver",
|
|
35
|
+
"wasm",
|
|
36
|
+
"webassembly"
|
|
37
|
+
],
|
|
38
|
+
"author": "owen-lacey",
|
|
39
|
+
"license": "Apache-2.0",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+ssh://git@github.com/owen-lacey/ortools-wasm.git"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/owen-lacey/ortools-wasm#readme",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/owen-lacey/ortools-wasm/issues"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@bufbuild/protobuf": "^2.0.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@bufbuild/buf": "^1.47.0",
|
|
53
|
+
"@bufbuild/protoc-gen-es": "^2.0.0",
|
|
54
|
+
"@types/node": "^25.6.0",
|
|
55
|
+
"typescript": "^5.6.0",
|
|
56
|
+
"vitest": "^3.0.0"
|
|
57
|
+
}
|
|
58
|
+
}
|