@reicek/neataptic-ts 0.1.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 (272) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +33 -0
  2. package/.github/ISSUE_TEMPLATE/feature_request.md +27 -0
  3. package/.github/PULL_REQUEST_TEMPLATE.md +28 -0
  4. package/.github/workflows/ci.yml +41 -0
  5. package/.github/workflows/deploy-pages.yml +29 -0
  6. package/.github/workflows/manual_release_pipeline.yml +62 -0
  7. package/.github/workflows/publish.yml +85 -0
  8. package/.github/workflows/release_dispatch.yml +38 -0
  9. package/.travis.yml +5 -0
  10. package/CONTRIBUTING.md +92 -0
  11. package/LICENSE +24 -0
  12. package/ONNX_EXPORT.md +87 -0
  13. package/README.md +1173 -0
  14. package/RELEASE.md +54 -0
  15. package/dist-docs/package.json +1 -0
  16. package/dist-docs/scripts/generate-docs.d.ts +2 -0
  17. package/dist-docs/scripts/generate-docs.d.ts.map +1 -0
  18. package/dist-docs/scripts/generate-docs.js +536 -0
  19. package/dist-docs/scripts/generate-docs.js.map +1 -0
  20. package/dist-docs/scripts/render-docs-html.d.ts +2 -0
  21. package/dist-docs/scripts/render-docs-html.d.ts.map +1 -0
  22. package/dist-docs/scripts/render-docs-html.js +148 -0
  23. package/dist-docs/scripts/render-docs-html.js.map +1 -0
  24. package/docs/FOLDERS.md +14 -0
  25. package/docs/README.md +1173 -0
  26. package/docs/architecture/README.md +1391 -0
  27. package/docs/architecture/index.html +938 -0
  28. package/docs/architecture/network/README.md +1210 -0
  29. package/docs/architecture/network/index.html +908 -0
  30. package/docs/assets/ascii-maze.bundle.js +16542 -0
  31. package/docs/assets/ascii-maze.bundle.js.map +7 -0
  32. package/docs/index.html +1419 -0
  33. package/docs/methods/README.md +670 -0
  34. package/docs/methods/index.html +477 -0
  35. package/docs/multithreading/README.md +274 -0
  36. package/docs/multithreading/index.html +215 -0
  37. package/docs/multithreading/workers/README.md +23 -0
  38. package/docs/multithreading/workers/browser/README.md +39 -0
  39. package/docs/multithreading/workers/browser/index.html +70 -0
  40. package/docs/multithreading/workers/index.html +57 -0
  41. package/docs/multithreading/workers/node/README.md +33 -0
  42. package/docs/multithreading/workers/node/index.html +66 -0
  43. package/docs/neat/README.md +1284 -0
  44. package/docs/neat/index.html +906 -0
  45. package/docs/src/README.md +2659 -0
  46. package/docs/src/index.html +1579 -0
  47. package/jest.config.ts +32 -0
  48. package/package.json +99 -0
  49. package/plans/HyperMorphoNEAT.md +293 -0
  50. package/plans/ONNX_EXPORT_PLAN.md +46 -0
  51. package/scripts/generate-docs.ts +486 -0
  52. package/scripts/render-docs-html.ts +138 -0
  53. package/scripts/types.d.ts +2 -0
  54. package/src/README.md +2659 -0
  55. package/src/architecture/README.md +1391 -0
  56. package/src/architecture/activationArrayPool.ts +135 -0
  57. package/src/architecture/architect.ts +635 -0
  58. package/src/architecture/connection.ts +148 -0
  59. package/src/architecture/group.ts +406 -0
  60. package/src/architecture/layer.ts +804 -0
  61. package/src/architecture/network/README.md +1210 -0
  62. package/src/architecture/network/network.activate.ts +223 -0
  63. package/src/architecture/network/network.connect.ts +157 -0
  64. package/src/architecture/network/network.deterministic.ts +167 -0
  65. package/src/architecture/network/network.evolve.ts +426 -0
  66. package/src/architecture/network/network.gating.ts +186 -0
  67. package/src/architecture/network/network.genetic.ts +247 -0
  68. package/src/architecture/network/network.mutate.ts +624 -0
  69. package/src/architecture/network/network.onnx.ts +463 -0
  70. package/src/architecture/network/network.prune.ts +216 -0
  71. package/src/architecture/network/network.remove.ts +96 -0
  72. package/src/architecture/network/network.serialize.ts +309 -0
  73. package/src/architecture/network/network.slab.ts +262 -0
  74. package/src/architecture/network/network.standalone.ts +246 -0
  75. package/src/architecture/network/network.stats.ts +59 -0
  76. package/src/architecture/network/network.topology.ts +86 -0
  77. package/src/architecture/network/network.training.ts +1278 -0
  78. package/src/architecture/network.ts +1302 -0
  79. package/src/architecture/node.ts +1288 -0
  80. package/src/architecture/onnx.ts +3 -0
  81. package/src/config.ts +83 -0
  82. package/src/methods/README.md +670 -0
  83. package/src/methods/activation.ts +372 -0
  84. package/src/methods/connection.ts +31 -0
  85. package/src/methods/cost.ts +347 -0
  86. package/src/methods/crossover.ts +63 -0
  87. package/src/methods/gating.ts +43 -0
  88. package/src/methods/methods.ts +8 -0
  89. package/src/methods/mutation.ts +300 -0
  90. package/src/methods/rate.ts +257 -0
  91. package/src/methods/selection.ts +65 -0
  92. package/src/multithreading/README.md +274 -0
  93. package/src/multithreading/multi.ts +339 -0
  94. package/src/multithreading/workers/README.md +23 -0
  95. package/src/multithreading/workers/browser/README.md +39 -0
  96. package/src/multithreading/workers/browser/testworker.ts +99 -0
  97. package/src/multithreading/workers/node/README.md +33 -0
  98. package/src/multithreading/workers/node/testworker.ts +72 -0
  99. package/src/multithreading/workers/node/worker.ts +70 -0
  100. package/src/multithreading/workers/workers.ts +22 -0
  101. package/src/neat/README.md +1284 -0
  102. package/src/neat/neat.adaptive.ts +544 -0
  103. package/src/neat/neat.compat.ts +164 -0
  104. package/src/neat/neat.constants.ts +20 -0
  105. package/src/neat/neat.diversity.ts +217 -0
  106. package/src/neat/neat.evaluate.ts +328 -0
  107. package/src/neat/neat.evolve.ts +1026 -0
  108. package/src/neat/neat.export.ts +249 -0
  109. package/src/neat/neat.helpers.ts +235 -0
  110. package/src/neat/neat.lineage.ts +220 -0
  111. package/src/neat/neat.multiobjective.ts +260 -0
  112. package/src/neat/neat.mutation.ts +718 -0
  113. package/src/neat/neat.objectives.ts +157 -0
  114. package/src/neat/neat.pruning.ts +190 -0
  115. package/src/neat/neat.selection.ts +269 -0
  116. package/src/neat/neat.speciation.ts +460 -0
  117. package/src/neat/neat.species.ts +151 -0
  118. package/src/neat/neat.telemetry.exports.ts +469 -0
  119. package/src/neat/neat.telemetry.ts +933 -0
  120. package/src/neat/neat.types.ts +275 -0
  121. package/src/neat.ts +1042 -0
  122. package/src/neataptic.ts +10 -0
  123. package/test/architecture/activationArrayPool.capacity.test.ts +19 -0
  124. package/test/architecture/activationArrayPool.test.ts +46 -0
  125. package/test/architecture/connection.test.ts +290 -0
  126. package/test/architecture/group.test.ts +950 -0
  127. package/test/architecture/layer.test.ts +1535 -0
  128. package/test/architecture/network.pruning.test.ts +65 -0
  129. package/test/architecture/node.test.ts +1602 -0
  130. package/test/examples/asciiMaze/asciiMaze.e2e.test.ts +499 -0
  131. package/test/examples/asciiMaze/asciiMaze.ts +41 -0
  132. package/test/examples/asciiMaze/browser-entry.ts +164 -0
  133. package/test/examples/asciiMaze/browserLogger.ts +221 -0
  134. package/test/examples/asciiMaze/browserTerminalUtility.ts +48 -0
  135. package/test/examples/asciiMaze/colors.ts +119 -0
  136. package/test/examples/asciiMaze/dashboardManager.ts +968 -0
  137. package/test/examples/asciiMaze/evolutionEngine.ts +1248 -0
  138. package/test/examples/asciiMaze/fitness.ts +136 -0
  139. package/test/examples/asciiMaze/index.html +128 -0
  140. package/test/examples/asciiMaze/index.ts +26 -0
  141. package/test/examples/asciiMaze/interfaces.ts +235 -0
  142. package/test/examples/asciiMaze/mazeMovement.ts +996 -0
  143. package/test/examples/asciiMaze/mazeUtils.ts +278 -0
  144. package/test/examples/asciiMaze/mazeVision.ts +402 -0
  145. package/test/examples/asciiMaze/mazeVisualization.ts +585 -0
  146. package/test/examples/asciiMaze/mazes.ts +245 -0
  147. package/test/examples/asciiMaze/networkRefinement.ts +76 -0
  148. package/test/examples/asciiMaze/networkVisualization.ts +901 -0
  149. package/test/examples/asciiMaze/terminalUtility.ts +73 -0
  150. package/test/methods/activation.test.ts +1142 -0
  151. package/test/methods/connection.test.ts +146 -0
  152. package/test/methods/cost.test.ts +1123 -0
  153. package/test/methods/crossover.test.ts +202 -0
  154. package/test/methods/gating.test.ts +144 -0
  155. package/test/methods/mutation.test.ts +451 -0
  156. package/test/methods/optimizers.advanced.test.ts +80 -0
  157. package/test/methods/optimizers.behavior.test.ts +105 -0
  158. package/test/methods/optimizers.formula.test.ts +89 -0
  159. package/test/methods/rate.cosineWarmRestarts.test.ts +44 -0
  160. package/test/methods/rate.linearWarmupDecay.test.ts +41 -0
  161. package/test/methods/rate.reduceOnPlateau.test.ts +45 -0
  162. package/test/methods/rate.test.ts +684 -0
  163. package/test/methods/selection.test.ts +245 -0
  164. package/test/multithreading/activations.functions.test.ts +54 -0
  165. package/test/multithreading/multi.test.ts +290 -0
  166. package/test/multithreading/worker.node.process.test.ts +39 -0
  167. package/test/multithreading/workers.coverage.test.ts +36 -0
  168. package/test/multithreading/workers.dynamic.import.test.ts +8 -0
  169. package/test/neat/neat.adaptive.complexityBudget.test.ts +34 -0
  170. package/test/neat/neat.adaptive.criterion.complexity.test.ts +50 -0
  171. package/test/neat/neat.adaptive.mutation.strategy.test.ts +37 -0
  172. package/test/neat/neat.adaptive.operator.decay.test.ts +31 -0
  173. package/test/neat/neat.adaptive.phasedComplexity.test.ts +25 -0
  174. package/test/neat/neat.adaptive.pruning.test.ts +25 -0
  175. package/test/neat/neat.adaptive.targetSpecies.test.ts +43 -0
  176. package/test/neat/neat.additional.coverage.test.ts +126 -0
  177. package/test/neat/neat.advanced.enhancements.test.ts +85 -0
  178. package/test/neat/neat.advanced.test.ts +589 -0
  179. package/test/neat/neat.diversity.autocompat.test.ts +47 -0
  180. package/test/neat/neat.diversity.metrics.test.ts +21 -0
  181. package/test/neat/neat.diversity.stats.test.ts +44 -0
  182. package/test/neat/neat.enhancements.test.ts +79 -0
  183. package/test/neat/neat.entropy.ancestorAdaptive.test.ts +133 -0
  184. package/test/neat/neat.entropy.compat.csv.test.ts +108 -0
  185. package/test/neat/neat.evolution.pruning.test.ts +39 -0
  186. package/test/neat/neat.fastmode.autotune.test.ts +42 -0
  187. package/test/neat/neat.innovation.test.ts +134 -0
  188. package/test/neat/neat.lineage.antibreeding.test.ts +35 -0
  189. package/test/neat/neat.lineage.entropy.test.ts +56 -0
  190. package/test/neat/neat.lineage.inbreeding.test.ts +49 -0
  191. package/test/neat/neat.lineage.pressure.test.ts +29 -0
  192. package/test/neat/neat.multiobjective.adaptive.test.ts +57 -0
  193. package/test/neat/neat.multiobjective.dynamic.schedule.test.ts +46 -0
  194. package/test/neat/neat.multiobjective.dynamic.test.ts +31 -0
  195. package/test/neat/neat.multiobjective.fastsort.delegation.test.ts +51 -0
  196. package/test/neat/neat.multiobjective.prune.test.ts +39 -0
  197. package/test/neat/neat.multiobjective.test.ts +21 -0
  198. package/test/neat/neat.mutation.undefined.pool.test.ts +24 -0
  199. package/test/neat/neat.objective.events.test.ts +26 -0
  200. package/test/neat/neat.objective.importance.test.ts +21 -0
  201. package/test/neat/neat.objective.lifetimes.test.ts +33 -0
  202. package/test/neat/neat.offspring.allocation.test.ts +22 -0
  203. package/test/neat/neat.operator.bandit.test.ts +17 -0
  204. package/test/neat/neat.operator.phases.test.ts +38 -0
  205. package/test/neat/neat.pruneInactive.behavior.test.ts +54 -0
  206. package/test/neat/neat.reenable.adaptation.test.ts +18 -0
  207. package/test/neat/neat.rng.state.test.ts +22 -0
  208. package/test/neat/neat.spawn.add.test.ts +123 -0
  209. package/test/neat/neat.speciation.test.ts +96 -0
  210. package/test/neat/neat.species.allocation.telemetry.test.ts +26 -0
  211. package/test/neat/neat.species.history.csv.test.ts +24 -0
  212. package/test/neat/neat.telemetry.advanced.test.ts +226 -0
  213. package/test/neat/neat.telemetry.csv.lineage.test.ts +19 -0
  214. package/test/neat/neat.telemetry.parity.test.ts +42 -0
  215. package/test/neat/neat.telemetry.stream.test.ts +19 -0
  216. package/test/neat/neat.telemetry.test.ts +16 -0
  217. package/test/neat/neat.test.ts +422 -0
  218. package/test/neat/neat.utilities.test.ts +44 -0
  219. package/test/network/__suppress_console.ts +9 -0
  220. package/test/network/acyclic.topoorder.test.ts +17 -0
  221. package/test/network/checkpoint.metricshook.test.ts +36 -0
  222. package/test/network/error.handling.test.ts +581 -0
  223. package/test/network/evolution.test.ts +285 -0
  224. package/test/network/genetic.test.ts +208 -0
  225. package/test/network/learning.capability.test.ts +244 -0
  226. package/test/network/mutation.effects.test.ts +492 -0
  227. package/test/network/network.activate.test.ts +115 -0
  228. package/test/network/network.activateBatch.test.ts +30 -0
  229. package/test/network/network.deterministic.test.ts +64 -0
  230. package/test/network/network.evolve.branches.test.ts +75 -0
  231. package/test/network/network.evolve.multithread.branches.test.ts +83 -0
  232. package/test/network/network.evolve.test.ts +100 -0
  233. package/test/network/network.gating.removal.test.ts +93 -0
  234. package/test/network/network.mutate.additional.test.ts +145 -0
  235. package/test/network/network.mutate.edgecases.test.ts +101 -0
  236. package/test/network/network.mutate.test.ts +101 -0
  237. package/test/network/network.prune.earlyexit.test.ts +38 -0
  238. package/test/network/network.remove.errors.test.ts +45 -0
  239. package/test/network/network.slab.fallbacks.test.ts +22 -0
  240. package/test/network/network.stats.test.ts +45 -0
  241. package/test/network/network.training.advanced.test.ts +149 -0
  242. package/test/network/network.training.basic.test.ts +228 -0
  243. package/test/network/network.training.helpers.test.ts +183 -0
  244. package/test/network/onnx.export.test.ts +310 -0
  245. package/test/network/onnx.import.test.ts +129 -0
  246. package/test/network/pruning.topology.test.ts +282 -0
  247. package/test/network/regularization.determinism.test.ts +83 -0
  248. package/test/network/regularization.dropconnect.test.ts +17 -0
  249. package/test/network/regularization.dropconnect.validation.test.ts +18 -0
  250. package/test/network/regularization.stochasticdepth.test.ts +27 -0
  251. package/test/network/regularization.test.ts +843 -0
  252. package/test/network/regularization.weightnoise.test.ts +30 -0
  253. package/test/network/setupTests.ts +2 -0
  254. package/test/network/standalone.test.ts +332 -0
  255. package/test/network/structure.serialization.test.ts +660 -0
  256. package/test/training/training.determinism.mixed-precision.test.ts +134 -0
  257. package/test/training/training.earlystopping.test.ts +91 -0
  258. package/test/training/training.edge-cases.test.ts +91 -0
  259. package/test/training/training.extensions.test.ts +47 -0
  260. package/test/training/training.gradient.features.test.ts +110 -0
  261. package/test/training/training.gradient.refinements.test.ts +170 -0
  262. package/test/training/training.gradient.separate-bias.test.ts +41 -0
  263. package/test/training/training.optimizer.test.ts +48 -0
  264. package/test/training/training.plateau.smoothing.test.ts +58 -0
  265. package/test/training/training.smoothing.types.test.ts +174 -0
  266. package/test/training/training.train.options.coverage.test.ts +52 -0
  267. package/test/utils/console-helper.ts +76 -0
  268. package/test/utils/jest-setup.ts +60 -0
  269. package/test/utils/test-helpers.ts +175 -0
  270. package/tsconfig.docs.json +12 -0
  271. package/tsconfig.json +21 -0
  272. package/webpack.config.js +49 -0
@@ -0,0 +1,99 @@
1
+ import Multi from '../../multi';
2
+
3
+ /**
4
+ * TestWorker class for handling network evaluations in a browser environment using Web Workers.
5
+ *
6
+ * This implementation aligns with the Instinct algorithm's emphasis on efficient evaluation of
7
+ * neural networks in parallel environments. The use of Web Workers allows for offloading
8
+ * computationally expensive tasks, such as network evaluation, to separate threads.
9
+ *
10
+ * @see Instinct Algorithm - Section 4 Constraints
11
+ * @see {@link https://medium.com/data-science/neuro-evolution-on-steroids-82bd14ddc2f6}
12
+ */
13
+ export class TestWorker {
14
+ private worker: Worker;
15
+ private url: string;
16
+
17
+ /**
18
+ * Creates a new TestWorker instance.
19
+ * @param {number[]} dataSet - The serialized dataset to be used by the worker.
20
+ * @param {any} cost - The cost function to evaluate the network.
21
+ */
22
+ constructor(dataSet: number[], cost: { name: string }) {
23
+ const blob = new Blob([TestWorker._createBlobString(cost)]);
24
+ this.url = window.URL.createObjectURL(blob);
25
+ this.worker = new Worker(this.url);
26
+
27
+ const data = { set: new Float64Array(dataSet).buffer };
28
+ this.worker.postMessage(data, [data.set]);
29
+ }
30
+
31
+ /**
32
+ * Evaluates a network using the worker process.
33
+ * @param {any} network - The network to evaluate.
34
+ * @returns {Promise<number>} A promise that resolves to the evaluation result.
35
+ */
36
+ evaluate(network: any): Promise<number> {
37
+ return new Promise((resolve, reject) => {
38
+ const serialized = network.serialize();
39
+
40
+ const data = {
41
+ activations: new Float64Array(serialized[0]).buffer,
42
+ states: new Float64Array(serialized[1]).buffer,
43
+ conns: new Float64Array(serialized[2]).buffer,
44
+ };
45
+
46
+ this.worker.onmessage = function (e: MessageEvent) {
47
+ const error = new Float64Array(e.data.buffer)[0];
48
+ resolve(error);
49
+ };
50
+
51
+ this.worker.postMessage(data, [
52
+ data.activations,
53
+ data.states,
54
+ data.conns,
55
+ ]);
56
+ });
57
+ }
58
+
59
+ /**
60
+ * Terminates the worker process and revokes the object URL.
61
+ */
62
+ terminate(): void {
63
+ this.worker.terminate();
64
+ window.URL.revokeObjectURL(this.url);
65
+ }
66
+
67
+ /**
68
+ * Creates a string representation of the worker's blob.
69
+ * @param {any} cost - The cost function to be used by the worker.
70
+ * @returns {string} The blob string.
71
+ */
72
+ private static _createBlobString(cost: any): string {
73
+ return `
74
+ const F = [${Multi.activations.toString()}];
75
+ const cost = ${cost.toString()};
76
+ const multi = {
77
+ deserializeDataSet: ${Multi.deserializeDataSet.toString()},
78
+ testSerializedSet: ${Multi.testSerializedSet.toString()},
79
+ activateSerializedNetwork: ${Multi.activateSerializedNetwork.toString()}
80
+ };
81
+
82
+ let set;
83
+
84
+ this.onmessage = function (e) {
85
+ if (typeof e.data.set === 'undefined') {
86
+ const A = new Float64Array(e.data.activations);
87
+ const S = new Float64Array(e.data.states);
88
+ const data = new Float64Array(e.data.conns);
89
+
90
+ const error = multi.testSerializedSet(set, cost, A, S, data, F);
91
+
92
+ const answer = { buffer: new Float64Array([error]).buffer };
93
+ postMessage(answer, [answer.buffer]);
94
+ } else {
95
+ set = multi.deserializeDataSet(new Float64Array(e.data.set));
96
+ }
97
+ };`;
98
+ }
99
+ }
@@ -0,0 +1,33 @@
1
+ # multithreading/workers/node
2
+
3
+ ## multithreading/workers/node/testworker.ts
4
+
5
+ ### TestWorker
6
+
7
+ TestWorker class for handling network evaluations in a Node.js environment using Worker Threads.
8
+
9
+ This implementation aligns with the Instinct algorithm's emphasis on efficient evaluation of
10
+ neural networks in parallel environments. The use of Worker Threads allows for offloading
11
+ computationally expensive tasks, such as network evaluation, to separate threads.
12
+
13
+ #### evaluate
14
+
15
+ `(network: any) => Promise<number>`
16
+
17
+ Evaluates a neural network using the worker process.
18
+
19
+ The network is serialized and sent to the worker for evaluation. The worker
20
+ sends back the evaluation result, which is returned as a promise.
21
+
22
+ Parameters:
23
+ - `` - - The neural network to evaluate. It must implement a `serialize` method.
24
+
25
+ Returns: A promise that resolves to the evaluation result.
26
+
27
+ #### terminate
28
+
29
+ `() => void`
30
+
31
+ Terminates the worker process.
32
+
33
+ This method ensures that the worker process is properly terminated to free up system resources.
@@ -0,0 +1,72 @@
1
+ import { fork, ChildProcess } from 'child_process';
2
+ import path from 'path';
3
+
4
+ /**
5
+ * TestWorker class for handling network evaluations in a Node.js environment using Worker Threads.
6
+ *
7
+ * This implementation aligns with the Instinct algorithm's emphasis on efficient evaluation of
8
+ * neural networks in parallel environments. The use of Worker Threads allows for offloading
9
+ * computationally expensive tasks, such as network evaluation, to separate threads.
10
+ *
11
+ * @see {@link https://medium.com/data-science/neuro-evolution-on-steroids-82bd14ddc2f6#4-constraints Instinct Algorithm - Section 4 Constraints}
12
+ *
13
+ * This class provides methods to evaluate neural networks and manage the worker process.
14
+ */
15
+ export class TestWorker {
16
+ private worker: ChildProcess;
17
+
18
+ /**
19
+ * Creates a new TestWorker instance.
20
+ *
21
+ * This initializes a new worker process and sends the dataset and cost function
22
+ * to the worker for further processing.
23
+ *
24
+ * @param {number[]} dataSet - The serialized dataset to be used by the worker.
25
+ * @param {{ name: string }} cost - The cost function to evaluate the network.
26
+ */
27
+ constructor(dataSet: number[], cost: { name: string }) {
28
+ this.worker = fork(path.join(__dirname, '/worker'));
29
+ this.worker.send({ set: dataSet, cost: cost.name });
30
+ }
31
+
32
+ /**
33
+ * Evaluates a neural network using the worker process.
34
+ *
35
+ * The network is serialized and sent to the worker for evaluation. The worker
36
+ * sends back the evaluation result, which is returned as a promise.
37
+ *
38
+ * @param {any} network - The neural network to evaluate. It must implement a `serialize` method.
39
+ * @returns {Promise<number>} A promise that resolves to the evaluation result.
40
+ */
41
+ evaluate(network: any): Promise<number> {
42
+ return new Promise((resolve) => {
43
+ const serialized = network.serialize();
44
+
45
+ const data = {
46
+ activations: serialized[0],
47
+ states: serialized[1],
48
+ conns: serialized[2],
49
+ };
50
+
51
+ const _that = this.worker;
52
+ this.worker.on('message', function callback(e: number) {
53
+ _that.removeListener('message', callback);
54
+ resolve(e);
55
+ });
56
+
57
+ this.worker.send(data);
58
+ });
59
+ }
60
+
61
+ /**
62
+ * Terminates the worker process.
63
+ *
64
+ * This method ensures that the worker process is properly terminated to free up system resources.
65
+ */
66
+ terminate(): void {
67
+ this.worker.kill();
68
+ }
69
+ }
70
+
71
+ // Add default export to match the original JavaScript implementation.
72
+ export default TestWorker;
@@ -0,0 +1,70 @@
1
+ import Multi from '../../../multithreading/multi';
2
+ import * as methods from '../../../methods/methods';
3
+
4
+ /**
5
+ * The dataset to be used by the worker.
6
+ * This is an array of objects where each object contains:
7
+ * - `input`: An array of input values for the network.
8
+ * - `output`: An array of expected output values for the network.
9
+ */
10
+ let set: Array<{ input: number[]; output: number[] }> = [];
11
+
12
+ /**
13
+ * The cost function to evaluate the network's performance.
14
+ * It takes the expected and actual output arrays and returns a numerical cost value.
15
+ */
16
+ let cost: (expected: number[], actual: number[]) => number;
17
+
18
+ /**
19
+ * The activation functions to be used by the worker.
20
+ * These are imported from the `Multi` module.
21
+ */
22
+ const F = Multi.activations;
23
+
24
+ /**
25
+ * Handles messages sent to the worker process.
26
+ *
27
+ * This function listens for messages sent to the worker process and performs one of two actions:
28
+ * 1. If the message contains serialized activations, states, and connections, it evaluates the network using the dataset.
29
+ * 2. If the message contains a dataset and cost function, it initializes the worker with the provided data.
30
+ *
31
+ * @param e - The message object sent to the worker process. It can contain:
32
+ * - `set`: Serialized dataset to initialize the worker. This is an array of objects with `input` and `output` properties.
33
+ * - `cost`: The name of the cost function to use. This should match a key in the `methods.Cost` object.
34
+ * - `activations`: Serialized activation values for the network.
35
+ * - `states`: Serialized state values for the network.
36
+ * - `conns`: Serialized connection data for the network.
37
+ */
38
+ process.on(
39
+ 'message',
40
+ (e: {
41
+ set?: any;
42
+ cost?: string;
43
+ activations?: any;
44
+ states?: any;
45
+ conns?: any;
46
+ }) => {
47
+ if (typeof e.set === 'undefined') {
48
+ // Deserialize the activations, states, and connections from the message
49
+ const { activations: A, states: S, conns: data } = e;
50
+
51
+ // Evaluate the network using the serialized dataset and send the result back
52
+ const result = Multi.testSerializedSet(set, cost, A, S, data, F);
53
+
54
+ // Send the evaluation result back to the parent process
55
+ if (process.send) {
56
+ process.send(result);
57
+ }
58
+ } else {
59
+ // Initialize the cost function using the provided name
60
+ // The cost function is retrieved from the `methods.Cost` object
61
+ cost = methods.Cost[e.cost as keyof typeof methods.Cost] as (
62
+ expected: number[],
63
+ actual: number[]
64
+ ) => number;
65
+
66
+ // Deserialize the dataset from the message and store it in the `set` variable
67
+ set = Multi.deserializeDataSet(e.set);
68
+ }
69
+ }
70
+ );
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Utility class for managing workers in both Node.js and browser environments.
3
+ */
4
+ export class Workers {
5
+ /**
6
+ * Loads the Node.js test worker dynamically.
7
+ * @returns {Promise<any>} A promise that resolves to the Node.js TestWorker class.
8
+ */
9
+ static async getNodeTestWorker(): Promise<any> {
10
+ const module = await import('./node/testworker');
11
+ return module.TestWorker;
12
+ }
13
+
14
+ /**
15
+ * Loads the browser test worker dynamically.
16
+ * @returns {Promise<any>} A promise that resolves to the browser TestWorker class.
17
+ */
18
+ static async getBrowserTestWorker(): Promise<any> {
19
+ const module = await import('./browser/testworker');
20
+ return module.TestWorker;
21
+ }
22
+ }