@iyulab/u-metaheur 0.3.0 → 0.3.2

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 CHANGED
@@ -85,6 +85,19 @@ cargo bench # criterion benchmarks
85
85
 
86
86
  MIT License — see [LICENSE](LICENSE).
87
87
 
88
+ ## npm (WebAssembly)
89
+
90
+ ```bash
91
+ npm install @iyulab/u-metaheur
92
+ ```
93
+
94
+ The package resolves per environment via a conditional `exports` map:
95
+
96
+ | Environment | Entry |
97
+ |---|---|
98
+ | Bundlers (webpack, Vite, …) | ESM + WebAssembly ESM-integration (`default` condition) |
99
+ | Node.js — `require()`, ESM `import`, CJS TS runners (`tsx`, `ts-node`) | CJS glue loading the wasm from the filesystem (`node` condition) — no loader hooks or flags |
100
+
88
101
  ## Related
89
102
 
90
103
  - [u-numflow](https://github.com/iyulab/u-numflow) — Mathematical primitives
@@ -0,0 +1,448 @@
1
+ /* @ts-self-types="./u_metaheur.d.cts" */
2
+
3
+ /**
4
+ * Runs a Genetic Algorithm for TSP.
5
+ *
6
+ * # Arguments
7
+ * `config` — native JS object with fields:
8
+ * - `nodes`: `[[x, y], ...]` (required)
9
+ * - `population_size`: integer (default 100)
10
+ * - `generations`: integer (default 200)
11
+ * - `mutation_rate`: float 0–1 (default 0.05)
12
+ *
13
+ * # Returns
14
+ * JS object with `best_distance`, `best_tour`, `generations_run`.
15
+ * @param {any} config
16
+ * @returns {any}
17
+ */
18
+ function run_ga(config) {
19
+ const ret = wasm.run_ga(config);
20
+ if (ret[2]) {
21
+ throw takeFromExternrefTable0(ret[1]);
22
+ }
23
+ return takeFromExternrefTable0(ret[0]);
24
+ }
25
+ exports.run_ga = run_ga;
26
+
27
+ /**
28
+ * Runs Simulated Annealing for TSP.
29
+ *
30
+ * Uses geometric cooling (`T *= cooling_rate` per iteration).
31
+ * Neighbour move: 2-opt segment reversal between two random positions.
32
+ *
33
+ * # Arguments
34
+ * `config` — native JS object with fields:
35
+ * - `nodes`: `[[x, y], ...]` (required)
36
+ * - `initial_temp`: float (default 1000.0)
37
+ * - `cooling_rate`: float in (0, 1) (default 0.995)
38
+ * - `iterations`: integer (default 5000)
39
+ *
40
+ * # Returns
41
+ * JS object with `best_distance`, `best_tour`, `iterations_run`.
42
+ * @param {any} config
43
+ * @returns {any}
44
+ */
45
+ function run_sa(config) {
46
+ const ret = wasm.run_sa(config);
47
+ if (ret[2]) {
48
+ throw takeFromExternrefTable0(ret[1]);
49
+ }
50
+ return takeFromExternrefTable0(ret[0]);
51
+ }
52
+ exports.run_sa = run_sa;
53
+
54
+ function __wbg_get_imports() {
55
+ const import0 = {
56
+ __proto__: null,
57
+ __wbg_Error_8c4e43fe74559d73: function(arg0, arg1) {
58
+ const ret = Error(getStringFromWasm0(arg0, arg1));
59
+ return ret;
60
+ },
61
+ __wbg_String_8f0eb39a4a4c2f66: function(arg0, arg1) {
62
+ const ret = String(arg1);
63
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
64
+ const len1 = WASM_VECTOR_LEN;
65
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
66
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
67
+ },
68
+ __wbg___wbindgen_bigint_get_as_i64_8fcf4ce7f1ca72a2: function(arg0, arg1) {
69
+ const v = arg1;
70
+ const ret = typeof(v) === 'bigint' ? v : undefined;
71
+ getDataViewMemory0().setBigInt64(arg0 + 8 * 1, isLikeNone(ret) ? BigInt(0) : ret, true);
72
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
73
+ },
74
+ __wbg___wbindgen_boolean_get_bbbb1c18aa2f5e25: function(arg0) {
75
+ const v = arg0;
76
+ const ret = typeof(v) === 'boolean' ? v : undefined;
77
+ return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
78
+ },
79
+ __wbg___wbindgen_debug_string_0bc8482c6e3508ae: function(arg0, arg1) {
80
+ const ret = debugString(arg1);
81
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
82
+ const len1 = WASM_VECTOR_LEN;
83
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
84
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
85
+ },
86
+ __wbg___wbindgen_in_47fa6863be6f2f25: function(arg0, arg1) {
87
+ const ret = arg0 in arg1;
88
+ return ret;
89
+ },
90
+ __wbg___wbindgen_is_bigint_31b12575b56f32fc: function(arg0) {
91
+ const ret = typeof(arg0) === 'bigint';
92
+ return ret;
93
+ },
94
+ __wbg___wbindgen_is_function_0095a73b8b156f76: function(arg0) {
95
+ const ret = typeof(arg0) === 'function';
96
+ return ret;
97
+ },
98
+ __wbg___wbindgen_is_object_5ae8e5880f2c1fbd: function(arg0) {
99
+ const val = arg0;
100
+ const ret = typeof(val) === 'object' && val !== null;
101
+ return ret;
102
+ },
103
+ __wbg___wbindgen_jsval_eq_11888390b0186270: function(arg0, arg1) {
104
+ const ret = arg0 === arg1;
105
+ return ret;
106
+ },
107
+ __wbg___wbindgen_jsval_loose_eq_9dd77d8cd6671811: function(arg0, arg1) {
108
+ const ret = arg0 == arg1;
109
+ return ret;
110
+ },
111
+ __wbg___wbindgen_number_get_8ff4255516ccad3e: function(arg0, arg1) {
112
+ const obj = arg1;
113
+ const ret = typeof(obj) === 'number' ? obj : undefined;
114
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
115
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
116
+ },
117
+ __wbg___wbindgen_string_get_72fb696202c56729: function(arg0, arg1) {
118
+ const obj = arg1;
119
+ const ret = typeof(obj) === 'string' ? obj : undefined;
120
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
121
+ var len1 = WASM_VECTOR_LEN;
122
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
123
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
124
+ },
125
+ __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) {
126
+ throw new Error(getStringFromWasm0(arg0, arg1));
127
+ },
128
+ __wbg_call_389efe28435a9388: function() { return handleError(function (arg0, arg1) {
129
+ const ret = arg0.call(arg1);
130
+ return ret;
131
+ }, arguments); },
132
+ __wbg_done_57b39ecd9addfe81: function(arg0) {
133
+ const ret = arg0.done;
134
+ return ret;
135
+ },
136
+ __wbg_entries_58c7934c745daac7: function(arg0) {
137
+ const ret = Object.entries(arg0);
138
+ return ret;
139
+ },
140
+ __wbg_getRandomValues_1c61fac11405ffdc: function() { return handleError(function (arg0, arg1) {
141
+ globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1));
142
+ }, arguments); },
143
+ __wbg_get_9b94d73e6221f75c: function(arg0, arg1) {
144
+ const ret = arg0[arg1 >>> 0];
145
+ return ret;
146
+ },
147
+ __wbg_get_b3ed3ad4be2bc8ac: function() { return handleError(function (arg0, arg1) {
148
+ const ret = Reflect.get(arg0, arg1);
149
+ return ret;
150
+ }, arguments); },
151
+ __wbg_instanceof_ArrayBuffer_c367199e2fa2aa04: function(arg0) {
152
+ let result;
153
+ try {
154
+ result = arg0 instanceof ArrayBuffer;
155
+ } catch (_) {
156
+ result = false;
157
+ }
158
+ const ret = result;
159
+ return ret;
160
+ },
161
+ __wbg_instanceof_Map_53af74335dec57f4: function(arg0) {
162
+ let result;
163
+ try {
164
+ result = arg0 instanceof Map;
165
+ } catch (_) {
166
+ result = false;
167
+ }
168
+ const ret = result;
169
+ return ret;
170
+ },
171
+ __wbg_instanceof_Uint8Array_9b9075935c74707c: function(arg0) {
172
+ let result;
173
+ try {
174
+ result = arg0 instanceof Uint8Array;
175
+ } catch (_) {
176
+ result = false;
177
+ }
178
+ const ret = result;
179
+ return ret;
180
+ },
181
+ __wbg_isArray_d314bb98fcf08331: function(arg0) {
182
+ const ret = Array.isArray(arg0);
183
+ return ret;
184
+ },
185
+ __wbg_isSafeInteger_bfbc7332a9768d2a: function(arg0) {
186
+ const ret = Number.isSafeInteger(arg0);
187
+ return ret;
188
+ },
189
+ __wbg_iterator_6ff6560ca1568e55: function() {
190
+ const ret = Symbol.iterator;
191
+ return ret;
192
+ },
193
+ __wbg_length_32ed9a279acd054c: function(arg0) {
194
+ const ret = arg0.length;
195
+ return ret;
196
+ },
197
+ __wbg_length_35a7bace40f36eac: function(arg0) {
198
+ const ret = arg0.length;
199
+ return ret;
200
+ },
201
+ __wbg_new_361308b2356cecd0: function() {
202
+ const ret = new Object();
203
+ return ret;
204
+ },
205
+ __wbg_new_3eb36ae241fe6f44: function() {
206
+ const ret = new Array();
207
+ return ret;
208
+ },
209
+ __wbg_new_dd2b680c8bf6ae29: function(arg0) {
210
+ const ret = new Uint8Array(arg0);
211
+ return ret;
212
+ },
213
+ __wbg_next_3482f54c49e8af19: function() { return handleError(function (arg0) {
214
+ const ret = arg0.next();
215
+ return ret;
216
+ }, arguments); },
217
+ __wbg_next_418f80d8f5303233: function(arg0) {
218
+ const ret = arg0.next;
219
+ return ret;
220
+ },
221
+ __wbg_prototypesetcall_bdcdcc5842e4d77d: function(arg0, arg1, arg2) {
222
+ Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
223
+ },
224
+ __wbg_set_3f1d0b984ed272ed: function(arg0, arg1, arg2) {
225
+ arg0[arg1] = arg2;
226
+ },
227
+ __wbg_set_f43e577aea94465b: function(arg0, arg1, arg2) {
228
+ arg0[arg1 >>> 0] = arg2;
229
+ },
230
+ __wbg_value_0546255b415e96c1: function(arg0) {
231
+ const ret = arg0.value;
232
+ return ret;
233
+ },
234
+ __wbindgen_cast_0000000000000001: function(arg0) {
235
+ // Cast intrinsic for `F64 -> Externref`.
236
+ const ret = arg0;
237
+ return ret;
238
+ },
239
+ __wbindgen_cast_0000000000000002: function(arg0) {
240
+ // Cast intrinsic for `I64 -> Externref`.
241
+ const ret = arg0;
242
+ return ret;
243
+ },
244
+ __wbindgen_cast_0000000000000003: function(arg0, arg1) {
245
+ // Cast intrinsic for `Ref(String) -> Externref`.
246
+ const ret = getStringFromWasm0(arg0, arg1);
247
+ return ret;
248
+ },
249
+ __wbindgen_cast_0000000000000004: function(arg0) {
250
+ // Cast intrinsic for `U64 -> Externref`.
251
+ const ret = BigInt.asUintN(64, arg0);
252
+ return ret;
253
+ },
254
+ __wbindgen_init_externref_table: function() {
255
+ const table = wasm.__wbindgen_externrefs;
256
+ const offset = table.grow(4);
257
+ table.set(0, undefined);
258
+ table.set(offset + 0, undefined);
259
+ table.set(offset + 1, null);
260
+ table.set(offset + 2, true);
261
+ table.set(offset + 3, false);
262
+ },
263
+ };
264
+ return {
265
+ __proto__: null,
266
+ "./u_metaheur_bg.js": import0,
267
+ };
268
+ }
269
+
270
+ function addToExternrefTable0(obj) {
271
+ const idx = wasm.__externref_table_alloc();
272
+ wasm.__wbindgen_externrefs.set(idx, obj);
273
+ return idx;
274
+ }
275
+
276
+ function debugString(val) {
277
+ // primitive types
278
+ const type = typeof val;
279
+ if (type == 'number' || type == 'boolean' || val == null) {
280
+ return `${val}`;
281
+ }
282
+ if (type == 'string') {
283
+ return `"${val}"`;
284
+ }
285
+ if (type == 'symbol') {
286
+ const description = val.description;
287
+ if (description == null) {
288
+ return 'Symbol';
289
+ } else {
290
+ return `Symbol(${description})`;
291
+ }
292
+ }
293
+ if (type == 'function') {
294
+ const name = val.name;
295
+ if (typeof name == 'string' && name.length > 0) {
296
+ return `Function(${name})`;
297
+ } else {
298
+ return 'Function';
299
+ }
300
+ }
301
+ // objects
302
+ if (Array.isArray(val)) {
303
+ const length = val.length;
304
+ let debug = '[';
305
+ if (length > 0) {
306
+ debug += debugString(val[0]);
307
+ }
308
+ for(let i = 1; i < length; i++) {
309
+ debug += ', ' + debugString(val[i]);
310
+ }
311
+ debug += ']';
312
+ return debug;
313
+ }
314
+ // Test for built-in
315
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
316
+ let className;
317
+ if (builtInMatches && builtInMatches.length > 1) {
318
+ className = builtInMatches[1];
319
+ } else {
320
+ // Failed to match the standard '[object ClassName]'
321
+ return toString.call(val);
322
+ }
323
+ if (className == 'Object') {
324
+ // we're a user defined class or Object
325
+ // JSON.stringify avoids problems with cycles, and is generally much
326
+ // easier than looping through ownProperties of `val`.
327
+ try {
328
+ return 'Object(' + JSON.stringify(val) + ')';
329
+ } catch (_) {
330
+ return 'Object';
331
+ }
332
+ }
333
+ // errors
334
+ if (val instanceof Error) {
335
+ return `${val.name}: ${val.message}\n${val.stack}`;
336
+ }
337
+ // TODO we could test for more things here, like `Set`s and `Map`s.
338
+ return className;
339
+ }
340
+
341
+ function getArrayU8FromWasm0(ptr, len) {
342
+ ptr = ptr >>> 0;
343
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
344
+ }
345
+
346
+ let cachedDataViewMemory0 = null;
347
+ function getDataViewMemory0() {
348
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
349
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
350
+ }
351
+ return cachedDataViewMemory0;
352
+ }
353
+
354
+ function getStringFromWasm0(ptr, len) {
355
+ ptr = ptr >>> 0;
356
+ return decodeText(ptr, len);
357
+ }
358
+
359
+ let cachedUint8ArrayMemory0 = null;
360
+ function getUint8ArrayMemory0() {
361
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
362
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
363
+ }
364
+ return cachedUint8ArrayMemory0;
365
+ }
366
+
367
+ function handleError(f, args) {
368
+ try {
369
+ return f.apply(this, args);
370
+ } catch (e) {
371
+ const idx = addToExternrefTable0(e);
372
+ wasm.__wbindgen_exn_store(idx);
373
+ }
374
+ }
375
+
376
+ function isLikeNone(x) {
377
+ return x === undefined || x === null;
378
+ }
379
+
380
+ function passStringToWasm0(arg, malloc, realloc) {
381
+ if (realloc === undefined) {
382
+ const buf = cachedTextEncoder.encode(arg);
383
+ const ptr = malloc(buf.length, 1) >>> 0;
384
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
385
+ WASM_VECTOR_LEN = buf.length;
386
+ return ptr;
387
+ }
388
+
389
+ let len = arg.length;
390
+ let ptr = malloc(len, 1) >>> 0;
391
+
392
+ const mem = getUint8ArrayMemory0();
393
+
394
+ let offset = 0;
395
+
396
+ for (; offset < len; offset++) {
397
+ const code = arg.charCodeAt(offset);
398
+ if (code > 0x7F) break;
399
+ mem[ptr + offset] = code;
400
+ }
401
+ if (offset !== len) {
402
+ if (offset !== 0) {
403
+ arg = arg.slice(offset);
404
+ }
405
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
406
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
407
+ const ret = cachedTextEncoder.encodeInto(arg, view);
408
+
409
+ offset += ret.written;
410
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
411
+ }
412
+
413
+ WASM_VECTOR_LEN = offset;
414
+ return ptr;
415
+ }
416
+
417
+ function takeFromExternrefTable0(idx) {
418
+ const value = wasm.__wbindgen_externrefs.get(idx);
419
+ wasm.__externref_table_dealloc(idx);
420
+ return value;
421
+ }
422
+
423
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
424
+ cachedTextDecoder.decode();
425
+ function decodeText(ptr, len) {
426
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
427
+ }
428
+
429
+ const cachedTextEncoder = new TextEncoder();
430
+
431
+ if (!('encodeInto' in cachedTextEncoder)) {
432
+ cachedTextEncoder.encodeInto = function (arg, view) {
433
+ const buf = cachedTextEncoder.encode(arg);
434
+ view.set(buf);
435
+ return {
436
+ read: arg.length,
437
+ written: buf.length
438
+ };
439
+ };
440
+ }
441
+
442
+ let WASM_VECTOR_LEN = 0;
443
+
444
+ const wasmPath = `${__dirname}/u_metaheur_bg.wasm`;
445
+ const wasmBytes = require('fs').readFileSync(wasmPath);
446
+ const wasmModule = new WebAssembly.Module(wasmBytes);
447
+ const wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports;
448
+ wasm.__wbindgen_start();
@@ -0,0 +1,35 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Runs a Genetic Algorithm for TSP.
6
+ *
7
+ * # Arguments
8
+ * `config` — native JS object with fields:
9
+ * - `nodes`: `[[x, y], ...]` (required)
10
+ * - `population_size`: integer (default 100)
11
+ * - `generations`: integer (default 200)
12
+ * - `mutation_rate`: float 0–1 (default 0.05)
13
+ *
14
+ * # Returns
15
+ * JS object with `best_distance`, `best_tour`, `generations_run`.
16
+ */
17
+ export function run_ga(config: any): any;
18
+
19
+ /**
20
+ * Runs Simulated Annealing for TSP.
21
+ *
22
+ * Uses geometric cooling (`T *= cooling_rate` per iteration).
23
+ * Neighbour move: 2-opt segment reversal between two random positions.
24
+ *
25
+ * # Arguments
26
+ * `config` — native JS object with fields:
27
+ * - `nodes`: `[[x, y], ...]` (required)
28
+ * - `initial_temp`: float (default 1000.0)
29
+ * - `cooling_rate`: float in (0, 1) (default 0.995)
30
+ * - `iterations`: integer (default 5000)
31
+ *
32
+ * # Returns
33
+ * JS object with `best_distance`, `best_tour`, `iterations_run`.
34
+ */
35
+ export function run_sa(config: any): any;
Binary file
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "iyulab"
6
6
  ],
7
7
  "description": "Domain-agnostic metaheuristic optimization framework: GA, BRKGA, SA, ALNS.",
8
- "version": "0.3.0",
8
+ "version": "0.3.2",
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
@@ -15,18 +15,31 @@
15
15
  "u_metaheur_bg.wasm",
16
16
  "u_metaheur.js",
17
17
  "u_metaheur_bg.js",
18
- "u_metaheur.d.ts"
18
+ "u_metaheur.d.ts",
19
+ "node"
19
20
  ],
20
21
  "main": "u_metaheur.js",
21
22
  "types": "u_metaheur.d.ts",
22
23
  "sideEffects": [
23
24
  "./u_metaheur.js",
24
- "./snippets/*"
25
+ "./snippets/*",
26
+ "./node/u_metaheur.cjs"
25
27
  ],
26
28
  "keywords": [
27
29
  "optimization",
28
30
  "genetic-algorithm",
29
31
  "metaheuristic",
30
32
  "evolutionary"
31
- ]
33
+ ],
34
+ "exports": {
35
+ ".": {
36
+ "node": {
37
+ "types": "./node/u_metaheur.d.cts",
38
+ "default": "./node/u_metaheur.cjs"
39
+ },
40
+ "types": "./u_metaheur.d.ts",
41
+ "default": "./u_metaheur.js"
42
+ },
43
+ "./package.json": "./package.json"
44
+ }
32
45
  }
Binary file