@elaraai/east-py-datascience 0.0.2-beta.9 → 1.0.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/CLA.md +26 -0
- package/CONTRIBUTING.md +28 -0
- package/LICENSE.md +2 -2
- package/README.md +108 -15
- package/dist/src/alns/alns.d.ts +528 -0
- package/dist/src/alns/alns.d.ts.map +1 -0
- package/dist/src/alns/alns.js +238 -0
- package/dist/src/alns/alns.js.map +1 -0
- package/dist/src/google_or/google_or.d.ts +2457 -0
- package/dist/src/google_or/google_or.d.ts.map +1 -0
- package/dist/src/google_or/google_or.js +547 -0
- package/dist/src/google_or/google_or.js.map +1 -0
- package/dist/{gp → src/gp}/gp.d.ts +185 -136
- package/dist/src/gp/gp.d.ts.map +1 -0
- package/dist/{gp → src/gp}/gp.js +64 -12
- package/dist/src/gp/gp.js.map +1 -0
- package/dist/src/index.d.ts +34 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +57 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lightgbm/lightgbm.d.ts +575 -0
- package/dist/src/lightgbm/lightgbm.d.ts.map +1 -0
- package/dist/{lightgbm → src/lightgbm}/lightgbm.js +104 -18
- package/dist/src/lightgbm/lightgbm.js.map +1 -0
- package/dist/src/lightning/lightning.d.ts +1594 -0
- package/dist/src/lightning/lightning.d.ts.map +1 -0
- package/dist/src/lightning/lightning.js +468 -0
- package/dist/src/lightning/lightning.js.map +1 -0
- package/dist/{mads → src/mads}/mads.d.ts +109 -112
- package/dist/src/mads/mads.d.ts.map +1 -0
- package/dist/{mads → src/mads}/mads.js +6 -8
- package/dist/src/mads/mads.js.map +1 -0
- package/dist/src/mapie/mapie.d.ts +3680 -0
- package/dist/src/mapie/mapie.d.ts.map +1 -0
- package/dist/src/mapie/mapie.js +616 -0
- package/dist/src/mapie/mapie.js.map +1 -0
- package/dist/{ngboost → src/ngboost}/ngboost.d.ts +192 -142
- package/dist/src/ngboost/ngboost.d.ts.map +1 -0
- package/dist/{ngboost → src/ngboost}/ngboost.js +67 -14
- package/dist/src/ngboost/ngboost.js.map +1 -0
- package/dist/src/optimization/optimization.d.ts +700 -0
- package/dist/src/optimization/optimization.d.ts.map +1 -0
- package/dist/src/optimization/optimization.js +411 -0
- package/dist/src/optimization/optimization.js.map +1 -0
- package/dist/{optuna → src/optuna}/optuna.d.ts +374 -314
- package/dist/src/optuna/optuna.d.ts.map +1 -0
- package/dist/{optuna → src/optuna}/optuna.js +2 -0
- package/dist/src/optuna/optuna.js.map +1 -0
- package/dist/src/pymc/pymc.d.ts +2932 -0
- package/dist/src/pymc/pymc.d.ts.map +1 -0
- package/dist/src/pymc/pymc.js +688 -0
- package/dist/src/pymc/pymc.js.map +1 -0
- package/dist/src/scipy/scipy.d.ts +2205 -0
- package/dist/src/scipy/scipy.d.ts.map +1 -0
- package/dist/src/scipy/scipy.js +884 -0
- package/dist/src/scipy/scipy.js.map +1 -0
- package/dist/src/shap/shap.d.ts +4611 -0
- package/dist/src/shap/shap.d.ts.map +1 -0
- package/dist/src/shap/shap.js +552 -0
- package/dist/src/shap/shap.js.map +1 -0
- package/dist/{simanneal → src/simanneal}/simanneal.d.ts +257 -160
- package/dist/src/simanneal/simanneal.d.ts.map +1 -0
- package/dist/{simanneal → src/simanneal}/simanneal.js +105 -8
- package/dist/src/simanneal/simanneal.js.map +1 -0
- package/dist/src/simulation/simulation.d.ts +224 -0
- package/dist/src/simulation/simulation.d.ts.map +1 -0
- package/dist/src/simulation/simulation.js +189 -0
- package/dist/src/simulation/simulation.js.map +1 -0
- package/dist/src/sklearn/sklearn.d.ts +6362 -0
- package/dist/src/sklearn/sklearn.d.ts.map +1 -0
- package/dist/src/sklearn/sklearn.js +1508 -0
- package/dist/src/sklearn/sklearn.js.map +1 -0
- package/dist/src/torch/torch.d.ts +1205 -0
- package/dist/src/torch/torch.d.ts.map +1 -0
- package/dist/{torch → src/torch}/torch.js +109 -18
- package/dist/src/torch/torch.js.map +1 -0
- package/dist/src/types.d.ts +43 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +44 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/xgboost/xgboost.d.ts +1424 -0
- package/dist/src/xgboost/xgboost.d.ts.map +1 -0
- package/dist/src/xgboost/xgboost.js +432 -0
- package/dist/src/xgboost/xgboost.js.map +1 -0
- package/package.json +21 -42
- package/dist/gp/gp.d.ts.map +0 -1
- package/dist/gp/gp.js.map +0 -1
- package/dist/index.d.ts +0 -27
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -41
- package/dist/index.js.map +0 -1
- package/dist/lightgbm/lightgbm.d.ts +0 -494
- package/dist/lightgbm/lightgbm.d.ts.map +0 -1
- package/dist/lightgbm/lightgbm.js.map +0 -1
- package/dist/mads/mads.d.ts.map +0 -1
- package/dist/mads/mads.js.map +0 -1
- package/dist/ngboost/ngboost.d.ts.map +0 -1
- package/dist/ngboost/ngboost.js.map +0 -1
- package/dist/optuna/optuna.d.ts.map +0 -1
- package/dist/optuna/optuna.js.map +0 -1
- package/dist/scipy/scipy.d.ts +0 -1260
- package/dist/scipy/scipy.d.ts.map +0 -1
- package/dist/scipy/scipy.js +0 -413
- package/dist/scipy/scipy.js.map +0 -1
- package/dist/shap/shap.d.ts +0 -657
- package/dist/shap/shap.d.ts.map +0 -1
- package/dist/shap/shap.js +0 -241
- package/dist/shap/shap.js.map +0 -1
- package/dist/simanneal/simanneal.d.ts.map +0 -1
- package/dist/simanneal/simanneal.js.map +0 -1
- package/dist/sklearn/sklearn.d.ts +0 -2691
- package/dist/sklearn/sklearn.d.ts.map +0 -1
- package/dist/sklearn/sklearn.js +0 -524
- package/dist/sklearn/sklearn.js.map +0 -1
- package/dist/torch/torch.d.ts +0 -1081
- package/dist/torch/torch.d.ts.map +0 -1
- package/dist/torch/torch.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types.d.ts +0 -80
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -81
- package/dist/types.js.map +0 -1
- package/dist/xgboost/xgboost.d.ts +0 -504
- package/dist/xgboost/xgboost.d.ts.map +0 -1
- package/dist/xgboost/xgboost.js +0 -177
- package/dist/xgboost/xgboost.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simanneal.d.ts","sourceRoot":"","sources":["../../../src/simanneal/simanneal.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;GAWG;AAEH,OAAO,EAEH,UAAU,EACV,WAAW,EACX,UAAU,EACV,WAAW,EACX,SAAS,EACT,WAAW,EACX,YAAY,EACZ,UAAU,EACb,MAAM,eAAe,CAAC;AAMvB;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB;IAC1B,sDAAsD;;IAEtD,6CAA6C;;EAE/C,CAAC;AAMH;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB;IAhB3B,sDAAsD;;IAEtD,6CAA6C;;eAiBhD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB;IA3BzB,sDAAsD;;IAEtD,6CAA6C;;;IAF7C,sDAAsD;;IAEtD,6CAA6C;;GA4BhD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,oDAGjC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,oDAG5B,CAAC;AAMF;;GAEG;AACH,eAAO,MAAM,gBAAgB;IACzB,8CAA8C;;IAE9C,wCAAwC;;IAExC,wCAAwC;;IAExC,sDAAsD;;IAEtD,mDAAmD;;IAEnD,sCAAsC;;EAExC,CAAC;AAMH;;GAEG;AACH,eAAO,MAAM,gBAAgB;IACzB,uBAAuB;;QAlFvB,sDAAsD;;QAEtD,6CAA6C;;;IAkF7C,2BAA2B;;IAE3B,kCAAkC;;IAElC,kDAAkD;;EAEpD,CAAC;AAMH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB;IA3G3B,sDAAsD;;IAEtD,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wK7C,2BAA2B;;YA1K3B,sDAAsD;;YAEtD,6CAA6C;;;QA0K7C,yBAAyB;;YA5KzB,sDAAsD;;YAEtD,6CAA6C;;;YAF7C,sDAAsD;;YAEtD,6CAA6C;;;QA4K7C,uCAAuC;;QAEvC,kCAAkC;;QAElC,yBAAyB;;YAtHzB,8CAA8C;;YAE9C,wCAAwC;;YAExC,wCAAwC;;YAExC,sDAAsD;;YAEtD,mDAAmD;;YAEnD,sCAAsC;;;QA8GtC,kBAAkB;;YAlGlB,uBAAuB;;gBAlFvB,sDAAsD;;gBAEtD,6CAA6C;;;YAkF7C,2BAA2B;;YAE3B,kCAAkC;;YAElC,kDAAkD;;;;CA6P5C,CAAC"}
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*
|
|
15
15
|
* @packageDocumentation
|
|
16
16
|
*/
|
|
17
|
-
import { East, StructType, VariantType, OptionType,
|
|
17
|
+
import { East, StructType, VariantType, OptionType, IntegerType, FloatType, BooleanType, FunctionType, VectorType, } from "@elaraai/east";
|
|
18
18
|
// ===========================================
|
|
19
19
|
// State Types
|
|
20
20
|
// ===========================================
|
|
@@ -27,9 +27,9 @@ import { East, StructType, VariantType, OptionType, ArrayType, IntegerType, Floa
|
|
|
27
27
|
*/
|
|
28
28
|
export const DiscreteStateType = VariantType({
|
|
29
29
|
/** Integer array state (permutations, assignments) */
|
|
30
|
-
int_array:
|
|
30
|
+
int_array: VectorType(IntegerType),
|
|
31
31
|
/** Boolean array state (subset selection) */
|
|
32
|
-
bool_array:
|
|
32
|
+
bool_array: VectorType(BooleanType),
|
|
33
33
|
});
|
|
34
34
|
// ===========================================
|
|
35
35
|
// Function Types
|
|
@@ -53,13 +53,13 @@ export const MoveFunctionType = FunctionType([DiscreteStateType], DiscreteStateT
|
|
|
53
53
|
*
|
|
54
54
|
* Specialized for permutation-based optimization.
|
|
55
55
|
*/
|
|
56
|
-
export const PermutationEnergyType = FunctionType([
|
|
56
|
+
export const PermutationEnergyType = FunctionType([VectorType(IntegerType)], FloatType);
|
|
57
57
|
/**
|
|
58
58
|
* Subset energy function type.
|
|
59
59
|
*
|
|
60
60
|
* Specialized for subset selection optimization.
|
|
61
61
|
*/
|
|
62
|
-
export const SubsetEnergyType = FunctionType([
|
|
62
|
+
export const SubsetEnergyType = FunctionType([VectorType(BooleanType)], FloatType);
|
|
63
63
|
// ===========================================
|
|
64
64
|
// Configuration Types
|
|
65
65
|
// ===========================================
|
|
@@ -128,7 +128,7 @@ export const simanneal_optimize = East.platform("simanneal_optimize", [
|
|
|
128
128
|
* @returns Result with best permutation and energy
|
|
129
129
|
*/
|
|
130
130
|
export const simanneal_optimize_permutation = East.platform("simanneal_optimize_permutation", [
|
|
131
|
-
|
|
131
|
+
VectorType(IntegerType),
|
|
132
132
|
PermutationEnergyType,
|
|
133
133
|
AnnealConfigType,
|
|
134
134
|
], AnnealResultType);
|
|
@@ -144,7 +144,7 @@ export const simanneal_optimize_permutation = East.platform("simanneal_optimize_
|
|
|
144
144
|
* @returns Result with best selection and energy
|
|
145
145
|
*/
|
|
146
146
|
export const simanneal_optimize_subset = East.platform("simanneal_optimize_subset", [
|
|
147
|
-
|
|
147
|
+
VectorType(BooleanType),
|
|
148
148
|
SubsetEnergyType,
|
|
149
149
|
AnnealConfigType,
|
|
150
150
|
], AnnealResultType);
|
|
@@ -190,7 +190,7 @@ export const SimAnnealTypes = {
|
|
|
190
190
|
*
|
|
191
191
|
* // TSP: minimize total route distance
|
|
192
192
|
* const energy = East.function(
|
|
193
|
-
* [
|
|
193
|
+
* [VectorType(IntegerType)],
|
|
194
194
|
* FloatType,
|
|
195
195
|
* ($, route) => {
|
|
196
196
|
* // Calculate total route distance
|
|
@@ -213,14 +213,111 @@ export const SimAnnealTypes = {
|
|
|
213
213
|
export const SimAnneal = {
|
|
214
214
|
/**
|
|
215
215
|
* Run simulated annealing with custom energy and move functions.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { East, FloatType, IntegerType, VectorType, variant } from "@elaraai/east";
|
|
220
|
+
* import { SimAnneal, AnnealConfigType, DiscreteStateType } from "@elaraai/east-py-datascience";
|
|
221
|
+
*
|
|
222
|
+
* const solve = East.function([], SimAnneal.Types.ResultType, ($) => {
|
|
223
|
+
* const energy = East.function([DiscreteStateType], FloatType, ($, state) => {
|
|
224
|
+
* const cost = $.let(0.0);
|
|
225
|
+
* $.match(state, {
|
|
226
|
+
* int_array: ($, arr) => {
|
|
227
|
+
* const a0 = $.let(arr.get(0n));
|
|
228
|
+
* $.if(East.equal(a0, 2n), $ => {}).else($ => { $.assign(cost, cost.add(1.0)); });
|
|
229
|
+
* },
|
|
230
|
+
* bool_array: ($) => { $.assign(cost, 999.0); },
|
|
231
|
+
* });
|
|
232
|
+
* return $.return(cost);
|
|
233
|
+
* });
|
|
234
|
+
* const move = East.function([DiscreteStateType], DiscreteStateType, ($, state) => {
|
|
235
|
+
* const result = $.let(state);
|
|
236
|
+
* $.match(state, {
|
|
237
|
+
* int_array: ($, arr) => {
|
|
238
|
+
* const new_arr = $.let(East.Vector.fromArray([arr.get(0n).add(1n).remainder(3n), arr.get(1n), arr.get(2n)]));
|
|
239
|
+
* $.assign(result, variant("int_array", new_arr));
|
|
240
|
+
* },
|
|
241
|
+
* bool_array: ($, arr) => { $.assign(result, variant("bool_array", arr)); },
|
|
242
|
+
* });
|
|
243
|
+
* return $.return(result);
|
|
244
|
+
* });
|
|
245
|
+
* const initial = $.let(variant("int_array", new BigInt64Array([0n, 0n, 0n])), DiscreteStateType);
|
|
246
|
+
* const config = $.let({
|
|
247
|
+
* t_max: variant("some", 100.0), t_min: variant("some", 0.01),
|
|
248
|
+
* steps: variant("some", 1000n), updates: variant("none", null),
|
|
249
|
+
* auto_schedule: variant("none", null), random_state: variant("some", 42n),
|
|
250
|
+
* }, AnnealConfigType);
|
|
251
|
+
* return $.return(SimAnneal.optimize(initial, energy, move, config));
|
|
252
|
+
* });
|
|
253
|
+
* ```
|
|
216
254
|
*/
|
|
217
255
|
optimize: simanneal_optimize,
|
|
218
256
|
/**
|
|
219
257
|
* Run simulated annealing on a permutation with swap moves.
|
|
258
|
+
*
|
|
259
|
+
* Convenience function for permutation-based problems (TSP, scheduling).
|
|
260
|
+
* Automatically uses swap moves to generate neighbors.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```ts
|
|
264
|
+
* import { East, FloatType, IntegerType, VectorType, variant } from "@elaraai/east";
|
|
265
|
+
* import { SimAnneal, AnnealConfigType } from "@elaraai/east-py-datascience";
|
|
266
|
+
*
|
|
267
|
+
* const solve = East.function([], SimAnneal.Types.ResultType, ($) => {
|
|
268
|
+
* const energy = East.function([VectorType(IntegerType)], FloatType, ($, route) => {
|
|
269
|
+
* const total = $.let(0.0);
|
|
270
|
+
* $.for(East.Array.range(0n, route.length()), ($, i) => {
|
|
271
|
+
* const from = $.let(route.get(i));
|
|
272
|
+
* const to = $.let(route.get(i.add(1n).remainder(route.length())));
|
|
273
|
+
* const diff = $.let(from.subtract(to).abs());
|
|
274
|
+
* $.if(East.equal(diff, 1n).or(() => East.equal(diff, 3n)), $ => {
|
|
275
|
+
* $.assign(total, total.add(1.0));
|
|
276
|
+
* }).else($ => { $.assign(total, total.add(1.414)); });
|
|
277
|
+
* });
|
|
278
|
+
* return $.return(total);
|
|
279
|
+
* });
|
|
280
|
+
* const initial = $.let(new BigInt64Array([0n, 1n, 2n, 3n]));
|
|
281
|
+
* const config = $.let({
|
|
282
|
+
* t_max: variant("some", 1000.0), t_min: variant("some", 0.1),
|
|
283
|
+
* steps: variant("some", 5000n), updates: variant("none", null),
|
|
284
|
+
* auto_schedule: variant("none", null), random_state: variant("some", 42n),
|
|
285
|
+
* }, AnnealConfigType);
|
|
286
|
+
* return $.return(SimAnneal.optimizePermutation(initial, energy, config));
|
|
287
|
+
* });
|
|
288
|
+
* ```
|
|
220
289
|
*/
|
|
221
290
|
optimizePermutation: simanneal_optimize_permutation,
|
|
222
291
|
/**
|
|
223
292
|
* Run simulated annealing on a subset selection with bit-flip moves.
|
|
293
|
+
*
|
|
294
|
+
* Convenience function for subset selection problems (feature selection, knapsack).
|
|
295
|
+
* Automatically uses bit-flip moves to generate neighbors.
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```ts
|
|
299
|
+
* import { East, FloatType, BooleanType, VectorType, variant } from "@elaraai/east";
|
|
300
|
+
* import { SimAnneal, AnnealConfigType } from "@elaraai/east-py-datascience";
|
|
301
|
+
*
|
|
302
|
+
* const solve = East.function([], SimAnneal.Types.ResultType, ($) => {
|
|
303
|
+
* const items = $.let(new Float64Array([3.0, 7.0, 1.0, 8.0, 4.0]));
|
|
304
|
+
* const target = $.let(12.0);
|
|
305
|
+
* const energy = East.function([VectorType(BooleanType)], FloatType, ($, selection) => {
|
|
306
|
+
* const sum = $.let(0.0);
|
|
307
|
+
* $.for(East.Array.range(0n, selection.length()), ($, i) => {
|
|
308
|
+
* $.if(selection.get(i), $ => { $.assign(sum, sum.add(items.get(i))); });
|
|
309
|
+
* });
|
|
310
|
+
* return $.return(sum.subtract(target).abs());
|
|
311
|
+
* });
|
|
312
|
+
* const initial = $.let(East.Vector.fromArray([true, true, true, true, true]));
|
|
313
|
+
* const config = $.let({
|
|
314
|
+
* t_max: variant("some", 500.0), t_min: variant("some", 0.1),
|
|
315
|
+
* steps: variant("some", 2000n), updates: variant("none", null),
|
|
316
|
+
* auto_schedule: variant("none", null), random_state: variant("some", 123n),
|
|
317
|
+
* }, AnnealConfigType);
|
|
318
|
+
* return $.return(SimAnneal.optimizeSubset(initial, energy, config));
|
|
319
|
+
* });
|
|
320
|
+
* ```
|
|
224
321
|
*/
|
|
225
322
|
optimizeSubset: simanneal_optimize_subset,
|
|
226
323
|
/**
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simanneal.js","sourceRoot":"","sources":["../../../src/simanneal/simanneal.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;GAWG;A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ACvC,qBAAqB;IACrB,kCAAkC;IAClC,gBAAgB;IAChB,yBAAyB;IACzB,UAAU,EAAE,gBAAgB;IAC5B,kBAAkB;IAClB,UAAU,EAAE,gBAAgB;CACtB,CAAC;AAEX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwCG;IACH,QAAQ,EAAE,kBAAkB;IAE5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,mBAAmB,EAAE,8BAA8B;IAEnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,cAAc,EAAE,yBAAyB;IAEzC;;OAEG;IACH,KAAK,EAAE,cAAc;CACf,CAAC"}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Discrete Event Simulation (DES) — Economic Ontology.
|
|
7
|
+
*
|
|
8
|
+
* Provides a generic priority-queue DES engine grounded in an economic ontology:
|
|
9
|
+
* - R (Resources): user-defined struct representing business state
|
|
10
|
+
* - E (Events): user-defined variant where each case is an economic activity
|
|
11
|
+
* - Process handler: defines how events affect resources (match dispatch)
|
|
12
|
+
*
|
|
13
|
+
* Entry point:
|
|
14
|
+
* - `simulation_run`: single deterministic run
|
|
15
|
+
*
|
|
16
|
+
* @packageDocumentation
|
|
17
|
+
*/
|
|
18
|
+
import { StructType, OptionType, ArrayType, IntegerType, DateTimeType, FunctionType } from "@elaraai/east";
|
|
19
|
+
/**
|
|
20
|
+
* Configuration for a single simulation run.
|
|
21
|
+
*/
|
|
22
|
+
export declare const SimulationConfigType: StructType<{
|
|
23
|
+
/** Safety limit on number of events processed (default: 100000) */
|
|
24
|
+
readonly max_events: OptionType<IntegerType>;
|
|
25
|
+
/** Stop processing events after this date */
|
|
26
|
+
readonly end_date: OptionType<DateTimeType>;
|
|
27
|
+
}>;
|
|
28
|
+
/**
|
|
29
|
+
* Result of a single simulation run.
|
|
30
|
+
*/
|
|
31
|
+
export declare const SimulationResultType: StructType<{
|
|
32
|
+
/** Final resource state after all events processed */
|
|
33
|
+
readonly final_state: "R";
|
|
34
|
+
/** Number of events processed */
|
|
35
|
+
readonly events_processed: IntegerType;
|
|
36
|
+
/** Date of the last event processed */
|
|
37
|
+
readonly final_date: DateTimeType;
|
|
38
|
+
}>;
|
|
39
|
+
/**
|
|
40
|
+
* Run a single deterministic discrete event simulation.
|
|
41
|
+
*
|
|
42
|
+
* Events are processed in chronological order from a priority queue.
|
|
43
|
+
* The handler function dispatches on event type (match) and can
|
|
44
|
+
* mutate state and schedule new events.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { East, StructType, VariantType, FloatType, DateTimeType, variant } from "@elaraai/east";
|
|
49
|
+
* import { Simulation } from "@elaraai/east-py-datascience";
|
|
50
|
+
*
|
|
51
|
+
* const Resources = StructType({ cash: FloatType });
|
|
52
|
+
* const Events = VariantType({ income: FloatType, expense: FloatType });
|
|
53
|
+
*
|
|
54
|
+
* const process = East.function(
|
|
55
|
+
* [Resources, DateTimeType, Events],
|
|
56
|
+
* StructType({ state: Resources, events: ArrayType(StructType({ date: DateTimeType, event: Events })) }),
|
|
57
|
+
* ($, state, date, event) => {
|
|
58
|
+
* return $.return(event.match({
|
|
59
|
+
* income: ($, amount) => $.return({ state: state.spread({ cash: state.cash.add(amount) }), events: [] }),
|
|
60
|
+
* expense: ($, amount) => $.return({ state: state.spread({ cash: state.cash.subtract(amount) }), events: [] }),
|
|
61
|
+
* }));
|
|
62
|
+
* }
|
|
63
|
+
* );
|
|
64
|
+
*
|
|
65
|
+
* const result = $.let(Simulation.run([Resources, Events], initialState, initialEvents, process, config));
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare const simulation_run: import("@elaraai/east").GenericPlatformDefinition<readonly ["R", "E"], readonly ["R", ArrayType<StructType<{
|
|
69
|
+
readonly date: DateTimeType;
|
|
70
|
+
readonly event: "E";
|
|
71
|
+
}>>, FunctionType<["R", DateTimeType, "E"], StructType<{
|
|
72
|
+
readonly state: "R";
|
|
73
|
+
readonly events: ArrayType<StructType<{
|
|
74
|
+
readonly date: DateTimeType;
|
|
75
|
+
readonly event: "E";
|
|
76
|
+
}>>;
|
|
77
|
+
}>>, StructType<{
|
|
78
|
+
/** Safety limit on number of events processed (default: 100000) */
|
|
79
|
+
readonly max_events: OptionType<IntegerType>;
|
|
80
|
+
/** Stop processing events after this date */
|
|
81
|
+
readonly end_date: OptionType<DateTimeType>;
|
|
82
|
+
}>], StructType<{
|
|
83
|
+
/** Final resource state after all events processed */
|
|
84
|
+
readonly final_state: "R";
|
|
85
|
+
/** Number of events processed */
|
|
86
|
+
readonly events_processed: IntegerType;
|
|
87
|
+
/** Date of the last event processed */
|
|
88
|
+
readonly final_date: DateTimeType;
|
|
89
|
+
}>>;
|
|
90
|
+
/**
|
|
91
|
+
* Type definitions for simulation functions.
|
|
92
|
+
*/
|
|
93
|
+
export declare const SimulationTypes: {
|
|
94
|
+
/** Single run configuration */
|
|
95
|
+
readonly ConfigType: StructType<{
|
|
96
|
+
/** Safety limit on number of events processed (default: 100000) */
|
|
97
|
+
readonly max_events: OptionType<IntegerType>;
|
|
98
|
+
/** Stop processing events after this date */
|
|
99
|
+
readonly end_date: OptionType<DateTimeType>;
|
|
100
|
+
}>;
|
|
101
|
+
/** Single run result */
|
|
102
|
+
readonly ResultType: StructType<{
|
|
103
|
+
/** Final resource state after all events processed */
|
|
104
|
+
readonly final_state: "R";
|
|
105
|
+
/** Number of events processed */
|
|
106
|
+
readonly events_processed: IntegerType;
|
|
107
|
+
/** Date of the last event processed */
|
|
108
|
+
readonly final_date: DateTimeType;
|
|
109
|
+
}>;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Discrete Event Simulation (DES) — REA Economic Ontology.
|
|
113
|
+
*
|
|
114
|
+
* Generic over:
|
|
115
|
+
* - R: Resources (state struct — the economic resource ontology)
|
|
116
|
+
* - E: Events (variant type — the economic event ontology)
|
|
117
|
+
*
|
|
118
|
+
* The handler function uses match dispatch to define economic processes.
|
|
119
|
+
* Each match branch IS a separate economic process. Process-from-process
|
|
120
|
+
* triggering works by returning new events.
|
|
121
|
+
*/
|
|
122
|
+
export declare const Simulation: {
|
|
123
|
+
/**
|
|
124
|
+
* Run a single deterministic discrete event simulation.
|
|
125
|
+
*
|
|
126
|
+
* Events are processed chronologically from a priority queue.
|
|
127
|
+
* The handler function dispatches on event type via match and can
|
|
128
|
+
* mutate state and schedule new events (creating the directed
|
|
129
|
+
* cyclic economic graph).
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* import { East, StructType, VariantType, ArrayType, FloatType, DateTimeType, variant } from "@elaraai/east";
|
|
134
|
+
* import { Simulation, SimulationConfigType } from "@elaraai/east-py-datascience";
|
|
135
|
+
*
|
|
136
|
+
* const Resources = StructType({ cash: FloatType });
|
|
137
|
+
* const Events = VariantType({ income: FloatType, expense: FloatType });
|
|
138
|
+
* const ScheduledEvent = StructType({ date: DateTimeType, event: Events });
|
|
139
|
+
* const ProcessResult = StructType({ state: Resources, events: ArrayType(ScheduledEvent) });
|
|
140
|
+
*
|
|
141
|
+
* const simulate = East.function([], Simulation.Types.ResultType, ($) => {
|
|
142
|
+
* const process = East.function(
|
|
143
|
+
* [Resources, DateTimeType, Events],
|
|
144
|
+
* ProcessResult,
|
|
145
|
+
* ($, state, date, event) => {
|
|
146
|
+
* const empty = $.let([] as const, ArrayType(ScheduledEvent));
|
|
147
|
+
* return $.return(event.match({
|
|
148
|
+
* income: ($, amount) => ({
|
|
149
|
+
* state: { cash: state.cash.add(amount) },
|
|
150
|
+
* events: empty,
|
|
151
|
+
* }),
|
|
152
|
+
* expense: ($, amount) => ({
|
|
153
|
+
* state: { cash: state.cash.subtract(amount) },
|
|
154
|
+
* events: empty,
|
|
155
|
+
* }),
|
|
156
|
+
* }));
|
|
157
|
+
* }
|
|
158
|
+
* );
|
|
159
|
+
*
|
|
160
|
+
* const initialState = $.let({ cash: 1000.0 });
|
|
161
|
+
* const initialEvents = $.let([
|
|
162
|
+
* { date: $.let(new Date("2025-01-01")), event: $.let(variant("income", 500.0), Events) },
|
|
163
|
+
* { date: $.let(new Date("2025-01-15")), event: $.let(variant("expense", 200.0), Events) },
|
|
164
|
+
* ], ArrayType(ScheduledEvent));
|
|
165
|
+
* const config = $.let({
|
|
166
|
+
* max_events: variant("none", null),
|
|
167
|
+
* end_date: variant("none", null),
|
|
168
|
+
* }, SimulationConfigType);
|
|
169
|
+
*
|
|
170
|
+
* const result = $.let(Simulation.run(
|
|
171
|
+
* [Resources, Events],
|
|
172
|
+
* initialState, initialEvents, process, config,
|
|
173
|
+
* ));
|
|
174
|
+
* // result.final_state.cash => 1300.0
|
|
175
|
+
* // result.events_processed => 2n
|
|
176
|
+
* return $.return(result);
|
|
177
|
+
* });
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
readonly run: import("@elaraai/east").GenericPlatformDefinition<readonly ["R", "E"], readonly ["R", ArrayType<StructType<{
|
|
181
|
+
readonly date: DateTimeType;
|
|
182
|
+
readonly event: "E";
|
|
183
|
+
}>>, FunctionType<["R", DateTimeType, "E"], StructType<{
|
|
184
|
+
readonly state: "R";
|
|
185
|
+
readonly events: ArrayType<StructType<{
|
|
186
|
+
readonly date: DateTimeType;
|
|
187
|
+
readonly event: "E";
|
|
188
|
+
}>>;
|
|
189
|
+
}>>, StructType<{
|
|
190
|
+
/** Safety limit on number of events processed (default: 100000) */
|
|
191
|
+
readonly max_events: OptionType<IntegerType>;
|
|
192
|
+
/** Stop processing events after this date */
|
|
193
|
+
readonly end_date: OptionType<DateTimeType>;
|
|
194
|
+
}>], StructType<{
|
|
195
|
+
/** Final resource state after all events processed */
|
|
196
|
+
readonly final_state: "R";
|
|
197
|
+
/** Number of events processed */
|
|
198
|
+
readonly events_processed: IntegerType;
|
|
199
|
+
/** Date of the last event processed */
|
|
200
|
+
readonly final_date: DateTimeType;
|
|
201
|
+
}>>;
|
|
202
|
+
/**
|
|
203
|
+
* Type definitions for simulation functions.
|
|
204
|
+
*/
|
|
205
|
+
readonly Types: {
|
|
206
|
+
/** Single run configuration */
|
|
207
|
+
readonly ConfigType: StructType<{
|
|
208
|
+
/** Safety limit on number of events processed (default: 100000) */
|
|
209
|
+
readonly max_events: OptionType<IntegerType>;
|
|
210
|
+
/** Stop processing events after this date */
|
|
211
|
+
readonly end_date: OptionType<DateTimeType>;
|
|
212
|
+
}>;
|
|
213
|
+
/** Single run result */
|
|
214
|
+
readonly ResultType: StructType<{
|
|
215
|
+
/** Final resource state after all events processed */
|
|
216
|
+
readonly final_state: "R";
|
|
217
|
+
/** Number of events processed */
|
|
218
|
+
readonly events_processed: IntegerType;
|
|
219
|
+
/** Date of the last event processed */
|
|
220
|
+
readonly final_date: DateTimeType;
|
|
221
|
+
}>;
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
//# sourceMappingURL=simulation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.d.ts","sourceRoot":"","sources":["../../../src/simulation/simulation.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;GAYG;AAEH,OAAO,EAEH,UAAU,EACV,UAAU,EACV,SAAS,EACT,WAAW,EACX,YAAY,EACZ,YAAY,EACf,MAAM,eAAe,CAAC;AAmCvB;;GAEG;AACH,eAAO,MAAM,oBAAoB;IAC7B,mEAAmE;;IAEnE,6CAA6C;;EAE/C,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,oBAAoB;IAC7B,sDAAsD;;IAEtD,iCAAiC;;IAEjC,uCAAuC;;EAEzC,CAAC;AAMH;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;IAnDvB,mEAAmE;;IAEnE,6CAA6C;;;IAQ7C,sDAAsD;;IAEtD,iCAAiC;;IAEjC,uCAAuC;;GA+C1C,CAAC;AAMF;;GAEG;AACH,eAAO,MAAM,eAAe;IACxB,+BAA+B;;QAvE/B,mEAAmE;;QAEnE,6CAA6C;;;IAuE7C,wBAAwB;;QA/DxB,sDAAsD;;QAEtD,iCAAiC;;QAEjC,uCAAuC;;;CA6DjC,CAAC;AAEX;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU;IACnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwDG;;;;;;;;;;;QAjJH,mEAAmE;;QAEnE,6CAA6C;;;QAQ7C,sDAAsD;;QAEtD,iCAAiC;;QAEjC,uCAAuC;;;IAsIvC;;OAEG;;QA/EH,+BAA+B;;YAvE/B,mEAAmE;;YAEnE,6CAA6C;;;QAuE7C,wBAAwB;;YA/DxB,sDAAsD;;YAEtD,iCAAiC;;YAEjC,uCAAuC;;;;CA0IjC,CAAC"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Discrete Event Simulation (DES) — Economic Ontology.
|
|
7
|
+
*
|
|
8
|
+
* Provides a generic priority-queue DES engine grounded in an economic ontology:
|
|
9
|
+
* - R (Resources): user-defined struct representing business state
|
|
10
|
+
* - E (Events): user-defined variant where each case is an economic activity
|
|
11
|
+
* - Process handler: defines how events affect resources (match dispatch)
|
|
12
|
+
*
|
|
13
|
+
* Entry point:
|
|
14
|
+
* - `simulation_run`: single deterministic run
|
|
15
|
+
*
|
|
16
|
+
* @packageDocumentation
|
|
17
|
+
*/
|
|
18
|
+
import { East, StructType, OptionType, ArrayType, IntegerType, DateTimeType, FunctionType, } from "@elaraai/east";
|
|
19
|
+
// ============================================================================
|
|
20
|
+
// Internal Helper Types
|
|
21
|
+
// ============================================================================
|
|
22
|
+
/**
|
|
23
|
+
* Scheduled event — a date + event pair.
|
|
24
|
+
* Not exported; users construct these inline.
|
|
25
|
+
*/
|
|
26
|
+
const ScheduledEventType = StructType({
|
|
27
|
+
date: DateTimeType,
|
|
28
|
+
event: "E",
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* Handler return type — new state + new events to schedule.
|
|
32
|
+
*/
|
|
33
|
+
const ProcessResultType = StructType({
|
|
34
|
+
state: "R",
|
|
35
|
+
events: ArrayType(ScheduledEventType),
|
|
36
|
+
});
|
|
37
|
+
/**
|
|
38
|
+
* Handler function type — (state, date, event) -> ProcessResult.
|
|
39
|
+
*/
|
|
40
|
+
const ProcessFnType = FunctionType(["R", DateTimeType, "E"], ProcessResultType);
|
|
41
|
+
// ============================================================================
|
|
42
|
+
// Single Run Types
|
|
43
|
+
// ============================================================================
|
|
44
|
+
/**
|
|
45
|
+
* Configuration for a single simulation run.
|
|
46
|
+
*/
|
|
47
|
+
export const SimulationConfigType = StructType({
|
|
48
|
+
/** Safety limit on number of events processed (default: 100000) */
|
|
49
|
+
max_events: OptionType(IntegerType),
|
|
50
|
+
/** Stop processing events after this date */
|
|
51
|
+
end_date: OptionType(DateTimeType),
|
|
52
|
+
});
|
|
53
|
+
/**
|
|
54
|
+
* Result of a single simulation run.
|
|
55
|
+
*/
|
|
56
|
+
export const SimulationResultType = StructType({
|
|
57
|
+
/** Final resource state after all events processed */
|
|
58
|
+
final_state: "R",
|
|
59
|
+
/** Number of events processed */
|
|
60
|
+
events_processed: IntegerType,
|
|
61
|
+
/** Date of the last event processed */
|
|
62
|
+
final_date: DateTimeType,
|
|
63
|
+
});
|
|
64
|
+
// ============================================================================
|
|
65
|
+
// Platform Functions
|
|
66
|
+
// ============================================================================
|
|
67
|
+
/**
|
|
68
|
+
* Run a single deterministic discrete event simulation.
|
|
69
|
+
*
|
|
70
|
+
* Events are processed in chronological order from a priority queue.
|
|
71
|
+
* The handler function dispatches on event type (match) and can
|
|
72
|
+
* mutate state and schedule new events.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { East, StructType, VariantType, FloatType, DateTimeType, variant } from "@elaraai/east";
|
|
77
|
+
* import { Simulation } from "@elaraai/east-py-datascience";
|
|
78
|
+
*
|
|
79
|
+
* const Resources = StructType({ cash: FloatType });
|
|
80
|
+
* const Events = VariantType({ income: FloatType, expense: FloatType });
|
|
81
|
+
*
|
|
82
|
+
* const process = East.function(
|
|
83
|
+
* [Resources, DateTimeType, Events],
|
|
84
|
+
* StructType({ state: Resources, events: ArrayType(StructType({ date: DateTimeType, event: Events })) }),
|
|
85
|
+
* ($, state, date, event) => {
|
|
86
|
+
* return $.return(event.match({
|
|
87
|
+
* income: ($, amount) => $.return({ state: state.spread({ cash: state.cash.add(amount) }), events: [] }),
|
|
88
|
+
* expense: ($, amount) => $.return({ state: state.spread({ cash: state.cash.subtract(amount) }), events: [] }),
|
|
89
|
+
* }));
|
|
90
|
+
* }
|
|
91
|
+
* );
|
|
92
|
+
*
|
|
93
|
+
* const result = $.let(Simulation.run([Resources, Events], initialState, initialEvents, process, config));
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export const simulation_run = East.genericPlatform("simulation_run", ["R", "E"], [
|
|
97
|
+
"R", // initial_state
|
|
98
|
+
ArrayType(ScheduledEventType), // initial_events
|
|
99
|
+
ProcessFnType, // process handler
|
|
100
|
+
SimulationConfigType, // config
|
|
101
|
+
], SimulationResultType);
|
|
102
|
+
// ============================================================================
|
|
103
|
+
// Grouped Export
|
|
104
|
+
// ============================================================================
|
|
105
|
+
/**
|
|
106
|
+
* Type definitions for simulation functions.
|
|
107
|
+
*/
|
|
108
|
+
export const SimulationTypes = {
|
|
109
|
+
/** Single run configuration */
|
|
110
|
+
ConfigType: SimulationConfigType,
|
|
111
|
+
/** Single run result */
|
|
112
|
+
ResultType: SimulationResultType,
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Discrete Event Simulation (DES) — REA Economic Ontology.
|
|
116
|
+
*
|
|
117
|
+
* Generic over:
|
|
118
|
+
* - R: Resources (state struct — the economic resource ontology)
|
|
119
|
+
* - E: Events (variant type — the economic event ontology)
|
|
120
|
+
*
|
|
121
|
+
* The handler function uses match dispatch to define economic processes.
|
|
122
|
+
* Each match branch IS a separate economic process. Process-from-process
|
|
123
|
+
* triggering works by returning new events.
|
|
124
|
+
*/
|
|
125
|
+
export const Simulation = {
|
|
126
|
+
/**
|
|
127
|
+
* Run a single deterministic discrete event simulation.
|
|
128
|
+
*
|
|
129
|
+
* Events are processed chronologically from a priority queue.
|
|
130
|
+
* The handler function dispatches on event type via match and can
|
|
131
|
+
* mutate state and schedule new events (creating the directed
|
|
132
|
+
* cyclic economic graph).
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* import { East, StructType, VariantType, ArrayType, FloatType, DateTimeType, variant } from "@elaraai/east";
|
|
137
|
+
* import { Simulation, SimulationConfigType } from "@elaraai/east-py-datascience";
|
|
138
|
+
*
|
|
139
|
+
* const Resources = StructType({ cash: FloatType });
|
|
140
|
+
* const Events = VariantType({ income: FloatType, expense: FloatType });
|
|
141
|
+
* const ScheduledEvent = StructType({ date: DateTimeType, event: Events });
|
|
142
|
+
* const ProcessResult = StructType({ state: Resources, events: ArrayType(ScheduledEvent) });
|
|
143
|
+
*
|
|
144
|
+
* const simulate = East.function([], Simulation.Types.ResultType, ($) => {
|
|
145
|
+
* const process = East.function(
|
|
146
|
+
* [Resources, DateTimeType, Events],
|
|
147
|
+
* ProcessResult,
|
|
148
|
+
* ($, state, date, event) => {
|
|
149
|
+
* const empty = $.let([] as const, ArrayType(ScheduledEvent));
|
|
150
|
+
* return $.return(event.match({
|
|
151
|
+
* income: ($, amount) => ({
|
|
152
|
+
* state: { cash: state.cash.add(amount) },
|
|
153
|
+
* events: empty,
|
|
154
|
+
* }),
|
|
155
|
+
* expense: ($, amount) => ({
|
|
156
|
+
* state: { cash: state.cash.subtract(amount) },
|
|
157
|
+
* events: empty,
|
|
158
|
+
* }),
|
|
159
|
+
* }));
|
|
160
|
+
* }
|
|
161
|
+
* );
|
|
162
|
+
*
|
|
163
|
+
* const initialState = $.let({ cash: 1000.0 });
|
|
164
|
+
* const initialEvents = $.let([
|
|
165
|
+
* { date: $.let(new Date("2025-01-01")), event: $.let(variant("income", 500.0), Events) },
|
|
166
|
+
* { date: $.let(new Date("2025-01-15")), event: $.let(variant("expense", 200.0), Events) },
|
|
167
|
+
* ], ArrayType(ScheduledEvent));
|
|
168
|
+
* const config = $.let({
|
|
169
|
+
* max_events: variant("none", null),
|
|
170
|
+
* end_date: variant("none", null),
|
|
171
|
+
* }, SimulationConfigType);
|
|
172
|
+
*
|
|
173
|
+
* const result = $.let(Simulation.run(
|
|
174
|
+
* [Resources, Events],
|
|
175
|
+
* initialState, initialEvents, process, config,
|
|
176
|
+
* ));
|
|
177
|
+
* // result.final_state.cash => 1300.0
|
|
178
|
+
* // result.events_processed => 2n
|
|
179
|
+
* return $.return(result);
|
|
180
|
+
* });
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
run: simulation_run,
|
|
184
|
+
/**
|
|
185
|
+
* Type definitions for simulation functions.
|
|
186
|
+
*/
|
|
187
|
+
Types: SimulationTypes,
|
|
188
|
+
};
|
|
189
|
+
//# sourceMappingURL=simulation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.js","sourceRoot":"","sources":["../../../src/simulation/simulation.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;GAYG;AAEH,OAAO,EACH,IAAI,EACJ,UAAU,EACV,UAAU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