-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.d.ts
825 lines (785 loc) · 20 KB
/
index.d.ts
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/**
* Main class for solving MiniZinc instances.
*
* This API allows you to add `.mzn`, `.dzn`, `.json` and `.mpc` files using
* the `addFile()` method, and then run MiniZinc on the files using the
* `solve()` method.
*
* Code can also be added programmatically using the `addString()` (and similar)
* methods.
*
* @example
* ```js
* const model = new MiniZinc.Model();
* // Add a file with a given name and string contents
* model.addFile('test.mzn', 'var 1..3: x; int: y;');
* // Add model code from a string
* model.addString('int: z;');
* // Add data in DZN format
* model.addDznString('y = 1;');
* // Add data from a JSON object
* model.addJSON({z: 2});
*
* const solve = model.solve({
* options: {
* solver: 'gecode',
* 'time-limit': 10000,
* statistics: true
* }
* });
*
* // You can listen for events
* solve.on('solution', solution => console.log(solution));
* solve.on('statistics', stats => console.log(stats.statistics));
*
* // And/or wait until complete
* solve.then(result => {
* console.log(result.solution);
* console.log(result.statistics);
* });
* ```
*/
export class Model {
/**
* Create a new model.
*
* @example
* ```js
* const model = new MiniZinc.Model();
* ```
*/
constructor();
/**
* Create a clone of this model.
*
* @example
* ```js
* const m1 = new MiniZinc.Model();
* m1.addFile('test.mzn', `
* var 1..3: x;
* int: y;
* `);
* const m2 = m1.clone();
* // Both m1 and m2 have test.mzn
*
* // Add different data to each model
* m1.addJson({
* y: 1
* });
* m2.addJson({
* y: 2
* });
* ```
*/
clone(): Model;
/**
* Add a snippet of code to the model.
*
* Note that each snippet is used as a complete model file.
*
* @example
* ```js
* model.addString("var 1..3: x;");
* model.addString("float: y;");
* ```
*
* @param model MiniZinc code as a string
* @returns The filename of the snippet (may be useful to identify sources of errors)
*/
addString(model: string): string;
/**
* Adds a snippet of data to the model.
*
* Note that each snippet is used as a complete data file.
*
* @example
* ```js
* model.addDznString("x = 1;");
* ```
*
* @param dzn DataZinc input as a string
* @returns The filename of the snippet (may be useful to identify sources of errors)
*/
addDznString(dzn: string): string;
/**
* Adds data to the model in JSON format.
*
* Note that each snippet is used as a complete JSON data file.
*
* @example
* ```js
* model.addJson({
* y: 1.5
* });
* ```
*
* @param data The data as an object in MiniZinc JSON data input format
* @returns The filename of the snippet (may be useful to identify sources of errors)
*/
addJson(data: object): string;
/**
* Makes the given string contents available to MiniZinc using the given
* filename.
*
* @example
* ```js
* /// Add this file to the MiniZinc command
* model.addFile("model.mzn", `
* include "foo.mzn";
* var 1..3: x;
* `);
* // Make this file available, but don't add it to the MiniZinc command
* model.addFile("foo.mzn", "var 1..3: y;", false);
* ```
*
* This method is generally only used from the browser.
*
* @param filename The file name to use
* @param contents The contents of the file
* @param use Whether to add this file as an argument to the MiniZinc command
*/
addFile(filename: string, contents: string, use?: boolean): void;
/**
* Adds the given file to the model.
*
* @example
* ```js
* model.addFile("./path/to/model.mzn");
* ```
*
* Only available using the native version of MiniZinc in NodeJS.
*
* @param filename The file name to use
*/
addFile(filename: string): void;
/**
* Check for errors in the model using `--model-check-only`.
*
* @example
* ```js
* const errors = model.check({
* options: {
* solver: 'gecode'
* }
* });
* for (const error of errors) {
* console.log(error.what, error.message);
* }
* ```
*
* @param config Configuration options
* @returns The errors in the model
*/
check(config: {
/** Options to pass to MiniZinc in parameter configuration file format */
options?: ParamConfig;
}): Promise<ErrorMessage[]>;
/**
* Get the model interface using `--model-interface-only`.
*
* @example
* ```js
* model.interface({
* options: {
* solver: 'gecode'
* }
* }).then(console.log);
* ```
*
* @param config Configuration options
* @returns The model interface
*/
interface(config: {
/** Options to pass to MiniZinc in parameter configuration file format */
options?: ParamConfig;
}): Promise<ModelInterface>;
/**
* Compile this model to FlatZinc.
*
* @example
* ```js
* const compile = model.compile({
* options: {
* solver: 'gecode',
* statistics: true
* }
* });
*
* // Print compilation statistics when received
* compile.on('statistics', e => console.log(e.statistics));
*
* // Wait for completion
* compile.then(console.log);
* ```
*
* @param config Configuration options
*/
compile(config: {
/** Options to pass to MiniZinc in parameter configuration file format */
options?: ParamConfig;
}): CompilationProgress;
/**
* Solve this model and retrieve the result.
*
* @example
* ```js
* // Begin solving
* const solve = model.solve({
* options: {
* solver: 'gecode',
* 'all-solutions': true
* }
* });
*
* // Print each solution as it is produced
* solve.on('solution', e => console.log(e.output));
*
* // Wait for completion
* solve.then(result => {
* console.log(result.status);
* });
* ```
*
* @param config Configuration options
*/
solve(config: {
/** Whether to use `--output-mode json` (`true` by default) */
jsonOutput?: boolean;
/** Options to pass to MiniZinc in parameter configuration file format */
options: ParamConfig;
}): SolveProgress;
}
/**
* Initialises MiniZinc.
*
* Calling this function is generally optional, but may be required if the
* library is unable to automatically find the `minizinc-worker.js` script in
* the browser, or the MiniZinc executable on NodeJS.
*
* @example
* ```js
* // In the browser
* MiniZinc.init({ workerURL: 'https://localhost:3000/minizinc-worker.js'} );
* // In NodeJS
* MiniZinc.init({ minizinc: '/path/to/minizinc' });
* ```
*
* It may also be useful to call in the browser to get a promise which resolves
* when the WebAssembly module has been loaded.
*
* @example
* ```js
* MiniZinc.init().then(() => {
* console.log('Ready to start solving');
* });
* ```
*
* @param config Configuration options for initialising MiniZinc
*/
export function init(
config?: BrowserInitConfig | NodeInitConfig
): Promise<void>;
/**
* Configuration options for initialising MiniZinc in the browser
*/
export interface BrowserInitConfig {
/** URL of the worker script */
workerURL?: string | URL;
/** URL of the minizinc.wasm file */
wasmURL?: string | URL;
/** URL of the minizinc.data file */
dataURL?: string | URL;
/** Size of web worker pool */
numWorkers?: number;
}
/**
* Configuration options for initialising MiniZinc in NodeJS
*/
export interface NodeInitConfig {
/** Name of, or path to the minizinc executable */
minizinc?: string;
/** Paths to search for the MiniZinc executable in */
minizincPaths?: string[];
}
/**
* Get the version of MiniZinc as returned by `minizinc --version`.
*/
export function version(): Promise<string>;
/**
* Get the list of solver configurations available using `minizinc --solvers-json`.
*/
export function solvers(): Promise<object[]>;
/**
* Get the contents of a text file in the MiniZinc share directory.
*
* Only files inside the MiniZinc share directory (or its children) may be used.
*
* @param file The path to the desired file relative to the MiniZinc share directory
* @returns The contents of the file, or null if it can't be read
*/
export function readStdlibFileContents(file: string): Promise<string>;
/**
* Get the contents of text files in the MiniZinc share directory.
*
* Only files inside the MiniZinc share directory (or its children) may be used.
*
* @param files An array of paths to the desired files relative to the MiniZinc share directory
* @returns An object whose keys are requested file names and whose values are the file contents, or null if it can't be read
*/
export function readStdlibFileContents(
files: string[]
): Promise<{ [key: string]: string }>;
/** Terminate any running MiniZinc processes and cleanup. */
export function shutdown(): void;
/**
* Options configuration in parameter configuration file ([`.mpc`](https://minizinc.dev/doc-latest/en/command_line.html#ch-param-files))
* format.
*
* @example
* ```js
* model.solve({
* options: {
* solver: 'gecode', // Maps to --solver gecode
* 'all-solutions': true, // Maps to --all-solutions
* 'output-objective': true, // maps to --output-objective
* }
* });
* ```
*/
export interface ParamConfig {
/** Solver tag to use. */
solver?: string;
/** MiniZinc command line options.
*
* The leading `--` can be omitted, and if the value is `true` it is treated
* as a flag).
*/
[arg: string]: any;
}
/**
* An error message from MiniZinc.
*
* @category Events
*/
export interface ErrorMessage {
/** Message type */
type: "error";
/** The kind of error which occurred */
what: string;
/** The error message */
message: string;
/** The file location if there is one */
location?: Location;
/** The stack trace is there is one */
stack?: StackItem[];
/** Other error-specific data */
[key: string]: any;
}
/**
* A warning message from MiniZinc.
*
* @category Events
*/
export interface WarningMessage {
/** Message type */
type: "warning";
/** The kind of warning which occurred */
what: string;
/** The error message */
message: string;
/** The file location if there is one */
location?: Location;
/** The stack trace is there is one */
stack?: StackItem[];
/** Other error-specific data */
[key: string]: any;
}
/**
* A location in a file.
*/
export interface Location {
/** Filename */
filename: string;
/** First line (starting from 1) */
firstLine: number;
/** First character (starting from 1) */
firstColumn: number;
/** Last line (inclusive) */
lastLine: number;
/** Last column (inclusive) */
lastColumn: number;
}
/**
* A stack trace item.
*/
export interface StackItem {
/** Location */
location: Location;
/** Whether this is a comprehension iteration item */
isCompIter: boolean;
/** String description of the stack trace item */
description: string;
}
/**
* Model interface output.
*
* @category Events
*/
export interface ModelInterface {
/** Message type */
type: "interface";
/** Model input parameters */
input: { [name: string]: VarType };
/** Model output variables */
output: { [name: string]: VarType };
/** Solve method */
method: "sat" | "min" | "max";
/** Whether there is an output item present */
has_output_item: boolean;
/** Files included */
included_files: string[];
/** Global constraints used */
globals: string[];
}
/**
* Type definition of a variable/parameter.
*/
export interface VarType {
/** Type of variable */
type: "int" | "float" | "bool" | "string";
/** Number of array dimensions if this is an array */
dim?: number;
/** Whether or not this variable is a set */
set?: true;
}
/**
* Model output mapping section names to their contents.
*
* The default mode will populate the `json` key with an object mapping
* variable names to their values (see the [MiniZinc documentation](https://minizinc.dev/doc-latest/en/spec.html#json-support)
* for details on the format).
*
* Note that sections ending with `_json` will be arrays.
*/
export interface Output {
/** The string output to the 'default' section (where no section was
* specified). */
default?: string;
/** The DZN output if produced.
*
* @example
* ```js
* model.solve({
* jsonOutput: false,
* options: {
* solver: 'gecode',
* 'output-mode': 'dzn'
* }
* });
* ```
*
*/
dzn?: string;
/** The output of all sections combined */
raw?: string;
/** The JSON output if produced */
json?: { [variable: string]: any };
/** Output to user-defined sections */
[section: string]: string | any[] | object | undefined;
}
/**
* Event emitted when a solution is found by the solver.
*
* @category Events
*/
export interface SolutionMessage {
/** Message type */
type: "solution";
/** Time in milliseconds (if run with `output-time: true`) */
time?: number;
/** Mapping between output section name and contents */
output: Output;
/** The sections output in order */
sections: string[];
}
/**
* Event emitted when a solution checker has been run.
*
* @category Events
*/
export interface CheckerMessage {
/** Message type */
type: "checker";
/** Time in milliseconds (if run with `output-time: true`) */
time?: number;
/** Messages produced by the checker */
messages: (SolutionMessage | TraceMessage | ErrorMessage | WarningMessage)[];
/** Mapping between output section name and contents
*
* @deprecated Use the `messages` list to retrieve the output instead.
*/
output: Output;
/** The sections output in order
*
* @deprecated Use the `messages` list to retrieve the output instead.
*/
sections: string[];
}
/**
* Solve status.
*/
export type Status =
/** All solutions found for a satisfaction problem */
| "ALL_SOLUTIONS"
/** Optimal solution found for an optimisation problem */
| "OPTIMAL_SOLUTION"
/** Problem is unsatisfiable */
| "UNSATISFIABLE"
/** Problem is unbounded */
| "UNBOUNDED"
/** Problem is unsatisfiable or unbounded */
| "UNSAT_OR_UNBOUNDED"
/** Problem is satisfied */
| "SATISFIED"
/** Status is unknown */
| "UNKNOWN"
/** An error occurred */
| "ERROR";
/**
* Event emitted when the final status is emitted by the solver.
*
* Note that this event may not be emitted at all.
*
* @category Events
*/
export interface StatusMessage {
/** Message type */
type: "status";
/** Status */
status: Status;
/** Time in milliseconds (if run with `output-time: true`) */
time?: number;
}
/**
* Event emitted when statistics are received from the compiler or the solver.
*
* @category Events
*/
export interface StatisticsMessage {
/** Message type */
type: "statistics";
/** Mapping between statistic name and value */
statistics: { [key: string]: any };
}
/**
* Event emitted when a timestamp message is received independently of a solution
* or status.
*
* @category Events
*/
export interface TimestampMessage {
/** Message type */
type: "time";
/** Time in milliseconds */
time: number;
}
/**
* Event emitted when MiniZinc encounters a call to `trace()`.
*
* @category Events
*/
export interface TraceMessage {
/** Message type */
type: "trace";
/** Output section */
section: string;
/** Trace message (usually a string, but may be an array if the section ends
* with `_json`). */
message: string | any[];
}
/** Event emitted when MiniZinc exits.
*
* If solving/compilation is cancelled with the `cancel()` method, then this
* event is still emitted, but with a `null` value for the `code`.
*
* @category Events
*/
export interface ExitMessage {
/** Message type */
type: "exit";
/** Exit code, or null if the process was interrupted */
code: number | null;
/** Possible message if there was an error */
message?: string;
}
/**
* Thenable controller for a compilation request.
*
* Used to listen to events during compilation, and can be awaited to retrieve
* the compiled FlatZinc.
*/
export interface CompilationProgress extends PromiseLike<string> {
/**
* Return whether or not compilation is still in progress.
*
* @example
* ```js
* const compile = model.compile({
* solver: 'gecode'
* });
* setInterval(() => {
* if (compile.isRunning()) {
* console.log('Still running');
* }
* }, 1000)
* ```
*/
isRunning(): boolean;
/** Cancel compilation.
*
* @example
* ```js
* const compile = model.compile({
* solver: 'gecode'
* });
* setTimeout(() => {
* if (compile.isRunning()) {
* compile.cancel();
* }
* }, 10000);
* ```
*/
cancel(): void;
/** Listen for an event.
*
* @example
* ```js
* const compile = model.compile({
* solver: 'gecode',
* statistics: true
* });
* solve.on('statistics', e => {
* console.log(e.statistics);
* });
* ```
*/
on(event: "statistics", callback: (e: StatisticsMessage) => void): void;
on(event: "trace", callback: (e: TraceMessage) => void): void;
on(event: "error", callback: (e: ErrorMessage) => void): void;
on(event: "warning", callback: (e: WarningMessage) => void): void;
on(event: "exit", callback: (e: ExitMessage) => void): void;
on(event: string, callback: (e: object) => void): void;
/** Stop listening for an event.
*
* @example
* ```js
* const compile = model.compile({
* solver: 'gecode',
* statistics: true
* });
* const onStat = e => {
* console.log(e.output);
* };
* // Start listening
* compile.on('statistics', onStat);
* setTimeout(() => {
* // Stop listening
* compile.off('statistics', onStat);
* }, 1000);
* ```
*/
off<T>(event: string, callback: (e: T) => void): void;
}
/**
* Thenable controller for a solve request.
*
* Used to listen to events during solving, and can be awaited to retrieve
* the final solution/statistics/status.
*/
export interface SolveProgress extends PromiseLike<SolveResult> {
/**
* Return whether or not solving is still in progress.
*
* @example
* ```js
* const solve = model.solve({
* solver: 'gecode'
* });
* setInterval(() => {
* if (solve.isRunning()) {
* console.log('Still running');
* }
* }, 1000)
* ```
*/
isRunning(): boolean;
/** Cancel solving.
*
* @example
* ```js
* const solve = model.solve({
* solver: 'gecode'
* });
* setTimeout(() => {
* if (solve.isRunning()) {
* solve.cancel();
* }
* }, 10000);
* ```
*/
cancel(): void;
/** Listen for an event.
*
* @example
* ```js
* const solve = model.solve({
* solver: 'gecode'
* });
* solve.on('solution', e => {
* console.log(e.output);
* });
* ```
*/
on(event: "solution", callback: (e: SolutionMessage) => void): void;
on(event: "checker", callback: (e: CheckerMessage) => void): void;
on(event: "status", callback: (e: StatusMessage) => void): void;
on(event: "statistics", callback: (e: StatisticsMessage) => void): void;
on(event: "timestamp", callback: (e: TimestampMessage) => void): void;
on(event: "trace", callback: (e: TraceMessage) => void): void;
on(event: "error", callback: (e: ErrorMessage) => void): void;
on(event: "warning", callback: (e: WarningMessage) => void): void;
on(event: "exit", callback: (e: ExitMessage) => void): void;
on(event: string, callback: (e: object) => void): void;
/** Stop listening for an event.
*
* @example
* ```js
* const solve = model.solve({
* solver: 'gecode'
* });
* const onSolution = e => {
* console.log(e.output);
* };
* // Start listening
* solve.on('solution', onSolution);
* setTimeout(() => {
* // Stop listening
* solve.off('solution', onSolution);
* }, 1000);
* ```
*/
off<T>(event: string, callback: (e: T) => void): void;
}
/**
* Result of solving the model.
*/
export interface SolveResult {
/** Solve status. */
status: Status;
/** The final solution if any was found. */
solution: SolutionMessage | null;
/** A combined statistics object with the latest value for each key if
* statistics output was enabled during solving.
*/
statistics: { [key: string]: any };
}