@elaraai/east-py-datascience 0.0.2-beta.84 → 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.
@@ -19,6 +19,8 @@ import { StructType, VariantType, OptionType, ArrayType, VectorType, IntegerType
19
19
  export declare const ParameterVectorType: VectorType<IntegerType>;
20
20
  /** Objective function: Vector<Integer> -> Float */
21
21
  export declare const IterativeObjectiveType: FunctionType<[VectorType<IntegerType>], FloatType>;
22
+ /** Per-element contribution function: (Vector<Integer>, Integer) -> Float */
23
+ export declare const ElementObjectiveType: FunctionType<[VectorType<IntegerType>, IntegerType], FloatType>;
22
24
  /** Per-element candidate spaces: Array<Vector<Integer>> */
23
25
  export declare const ParameterSpacesType: ArrayType<VectorType<IntegerType>>;
24
26
  /**
@@ -168,6 +170,136 @@ export declare const optimization_iterative: import("@elaraai/east").PlatformDef
168
170
  /** Whether optimization succeeded */
169
171
  readonly success: BooleanType;
170
172
  }>>;
173
+ /**
174
+ * Incremental iterative optimization over integer parameter vectors.
175
+ *
176
+ * Like {@link optimization_iterative}, but takes a **per-element contribution function**
177
+ * instead of a full objective. The optimizer maintains a running sum and only
178
+ * recomputes contributions for changed indices — dramatically faster when
179
+ * individual moves affect only a small part of the total cost.
180
+ *
181
+ * The total objective is `sum(elementObjective(vector, i) for all i)`.
182
+ *
183
+ * Two modes are available:
184
+ * - **coordinate** (default): When changing element `i`, recomputes 1 contribution.
185
+ * - **swap**: When swapping elements `i` and `j`, recomputes 2 contributions.
186
+ *
187
+ * @example Incremental rostering
188
+ * ```ts
189
+ * // Per-person cost: only recomputes the changed person
190
+ * const elementObjective = East.function(
191
+ * [VectorType(IntegerType), IntegerType], FloatType,
192
+ * ($, assignments, personIdx) => {
193
+ * const role = $.let(assignments.get(personIdx));
194
+ * return $.return(salaryLookup.get(personIdx).get(role).negate());
195
+ * }
196
+ * );
197
+ * const result = $.let(Optimization.iterativeIncremental(
198
+ * elementObjective, spaces, config
199
+ * ));
200
+ * ```
201
+ */
202
+ export declare const optimization_iterative_incremental: import("@elaraai/east").PlatformDefinition<[FunctionType<[VectorType<IntegerType>, IntegerType], FloatType>, ArrayType<VectorType<IntegerType>>, StructType<{
203
+ /** Maximum coordinate descent iterations per sample (default: 100) */
204
+ readonly iterations: OptionType<IntegerType>;
205
+ /** Number of independent restarts (default: 1) */
206
+ readonly samples: OptionType<IntegerType>;
207
+ /** How to initialize parameter values (default: first) */
208
+ readonly initial: OptionType<VariantType<{
209
+ readonly first: NullType;
210
+ readonly random: NullType;
211
+ }>>;
212
+ /** Order to evaluate candidates (default: sequential) */
213
+ readonly order: OptionType<VariantType<{
214
+ readonly sequential: NullType;
215
+ readonly random: NullType;
216
+ }>>;
217
+ /** Random seed for reproducibility */
218
+ readonly random_state: OptionType<IntegerType>;
219
+ /** Optimization mode: coordinate (default) or swap for permutations */
220
+ readonly mode: OptionType<VariantType<{
221
+ readonly coordinate: NullType;
222
+ readonly swap: NullType;
223
+ }>>;
224
+ }>], StructType<{
225
+ /** Best parameter values found */
226
+ readonly best_parameters: VectorType<IntegerType>;
227
+ /** Objective value at best parameters */
228
+ readonly best_objective: FloatType;
229
+ /** Total coordinate descent iterations across all samples */
230
+ readonly iterations: IntegerType;
231
+ /** Total number of objective evaluations */
232
+ readonly evaluations: IntegerType;
233
+ /** Whether optimization succeeded */
234
+ readonly success: BooleanType;
235
+ }>>;
236
+ /**
237
+ * Group-based incremental iterative optimization.
238
+ *
239
+ * Like {@link optimization_iterative_incremental}, but contributions are grouped
240
+ * by **value** rather than by index. The group objective receives the full vector
241
+ * and a group key (a value that appears in the vector), and returns the total
242
+ * contribution of all elements assigned to that group.
243
+ *
244
+ * When element `i` changes from value A to value B, only 2 groups are recomputed:
245
+ * `groupObjective(vector, A)` and `groupObjective(vector, B)`.
246
+ *
247
+ * Use this when cost is associated with values (e.g., employees, bins, vehicles)
248
+ * rather than positions (e.g., slots, items, stops).
249
+ *
250
+ * @example Rostering — slot → employee assignment
251
+ * ```ts
252
+ * const groupObjective = East.function(
253
+ * [VectorType(IntegerType), IntegerType], FloatType,
254
+ * ($, slotAssignments, employeeId) => {
255
+ * const cost = $.let(0.0);
256
+ * $.for(East.Array.range(0n, nSlots), ($, slot) => {
257
+ * $.if(East.equal(slotAssignments.get(slot), employeeId), $ => {
258
+ * $.assign(cost, cost.add(shiftRates.get(slot)));
259
+ * });
260
+ * });
261
+ * return $.return(cost.negate());
262
+ * }
263
+ * );
264
+ * const result = $.let(Optimization.iterativeGrouped(
265
+ * groupObjective, spaces, config
266
+ * ));
267
+ * ```
268
+ */
269
+ export declare const optimization_iterative_grouped: import("@elaraai/east").PlatformDefinition<[FunctionType<[VectorType<IntegerType>, IntegerType], FloatType>, ArrayType<VectorType<IntegerType>>, StructType<{
270
+ /** Maximum coordinate descent iterations per sample (default: 100) */
271
+ readonly iterations: OptionType<IntegerType>;
272
+ /** Number of independent restarts (default: 1) */
273
+ readonly samples: OptionType<IntegerType>;
274
+ /** How to initialize parameter values (default: first) */
275
+ readonly initial: OptionType<VariantType<{
276
+ readonly first: NullType;
277
+ readonly random: NullType;
278
+ }>>;
279
+ /** Order to evaluate candidates (default: sequential) */
280
+ readonly order: OptionType<VariantType<{
281
+ readonly sequential: NullType;
282
+ readonly random: NullType;
283
+ }>>;
284
+ /** Random seed for reproducibility */
285
+ readonly random_state: OptionType<IntegerType>;
286
+ /** Optimization mode: coordinate (default) or swap for permutations */
287
+ readonly mode: OptionType<VariantType<{
288
+ readonly coordinate: NullType;
289
+ readonly swap: NullType;
290
+ }>>;
291
+ }>], StructType<{
292
+ /** Best parameter values found */
293
+ readonly best_parameters: VectorType<IntegerType>;
294
+ /** Objective value at best parameters */
295
+ readonly best_objective: FloatType;
296
+ /** Total coordinate descent iterations across all samples */
297
+ readonly iterations: IntegerType;
298
+ /** Total number of objective evaluations */
299
+ readonly evaluations: IntegerType;
300
+ /** Whether optimization succeeded */
301
+ readonly success: BooleanType;
302
+ }>>;
171
303
  /**
172
304
  * Type definitions for iterative optimization.
173
305
  */
@@ -176,6 +308,8 @@ export declare const OptimizationTypes: {
176
308
  readonly ParameterVectorType: VectorType<IntegerType>;
177
309
  /** Objective function type */
178
310
  readonly ObjectiveType: FunctionType<[VectorType<IntegerType>], FloatType>;
311
+ /** Per-element contribution function type */
312
+ readonly ElementObjectiveType: FunctionType<[VectorType<IntegerType>, IntegerType], FloatType>;
179
313
  /** Parameter spaces type */
180
314
  readonly SpacesType: ArrayType<VectorType<IntegerType>>;
181
315
  /** Initial value strategy variant */
@@ -353,6 +487,150 @@ export declare const Optimization: {
353
487
  /** Whether optimization succeeded */
354
488
  readonly success: BooleanType;
355
489
  }>>;
490
+ /**
491
+ * Incremental iterative optimization with per-element contributions.
492
+ *
493
+ * `Optimization.iterativeIncremental(elementObjective, spaces, config)`
494
+ *
495
+ * Takes a per-element contribution function `(Vector<Integer>, Integer) -> Float`
496
+ * instead of a full objective. The total objective is the sum of all element
497
+ * contributions. Only recomputes contributions for changed indices during search.
498
+ *
499
+ * Use this when individual moves (coordinate or swap) affect only a small
500
+ * part of the total cost — e.g., rostering where changing one person's
501
+ * allocation only changes that person's salary.
502
+ *
503
+ * @example Incremental task-worker assignment
504
+ * ```ts
505
+ * // Per-task skill score: only recomputes the changed task
506
+ * const skill = $.let([[3.0, 1.0], [1.0, 3.0], [2.0, 2.0]]);
507
+ * const elementObjective = East.function(
508
+ * [VectorType(IntegerType), IntegerType], FloatType,
509
+ * ($, assignments, taskIdx) => {
510
+ * const worker = $.let(assignments.get(taskIdx));
511
+ * return $.return(skill.get(taskIdx).get(worker));
512
+ * }
513
+ * );
514
+ * const spaces = $.let([
515
+ * new BigInt64Array([0n, 1n]),
516
+ * new BigInt64Array([0n, 1n]),
517
+ * new BigInt64Array([0n, 1n]),
518
+ * ]);
519
+ * const config = $.let({
520
+ * iterations: variant('some', 10n),
521
+ * samples: variant('some', 3n),
522
+ * initial: variant('some', variant('random', null)),
523
+ * order: variant('some', variant('sequential', null)),
524
+ * random_state: variant('some', 42n),
525
+ * mode: variant('none', null),
526
+ * });
527
+ * const result = $.let(Optimization.iterativeIncremental(
528
+ * elementObjective, spaces, config
529
+ * ));
530
+ * // result.best_objective = 8.0 (same result, fewer evaluations)
531
+ * ```
532
+ */
533
+ readonly iterativeIncremental: import("@elaraai/east").PlatformDefinition<[FunctionType<[VectorType<IntegerType>, IntegerType], FloatType>, ArrayType<VectorType<IntegerType>>, StructType<{
534
+ /** Maximum coordinate descent iterations per sample (default: 100) */
535
+ readonly iterations: OptionType<IntegerType>;
536
+ /** Number of independent restarts (default: 1) */
537
+ readonly samples: OptionType<IntegerType>;
538
+ /** How to initialize parameter values (default: first) */
539
+ readonly initial: OptionType<VariantType<{
540
+ readonly first: NullType;
541
+ readonly random: NullType;
542
+ }>>;
543
+ /** Order to evaluate candidates (default: sequential) */
544
+ readonly order: OptionType<VariantType<{
545
+ readonly sequential: NullType;
546
+ readonly random: NullType;
547
+ }>>;
548
+ /** Random seed for reproducibility */
549
+ readonly random_state: OptionType<IntegerType>;
550
+ /** Optimization mode: coordinate (default) or swap for permutations */
551
+ readonly mode: OptionType<VariantType<{
552
+ readonly coordinate: NullType;
553
+ readonly swap: NullType;
554
+ }>>;
555
+ }>], StructType<{
556
+ /** Best parameter values found */
557
+ readonly best_parameters: VectorType<IntegerType>;
558
+ /** Objective value at best parameters */
559
+ readonly best_objective: FloatType;
560
+ /** Total coordinate descent iterations across all samples */
561
+ readonly iterations: IntegerType;
562
+ /** Total number of objective evaluations */
563
+ readonly evaluations: IntegerType;
564
+ /** Whether optimization succeeded */
565
+ readonly success: BooleanType;
566
+ }>>;
567
+ /**
568
+ * Group-based incremental optimization with per-value contributions.
569
+ *
570
+ * `Optimization.iterativeGrouped(groupObjective, spaces, config)`
571
+ *
572
+ * Takes a group objective `(Vector<Integer>, Integer) -> Float` where the
573
+ * second argument is a **value** (group key), not an index. Returns the total
574
+ * contribution of all elements assigned to that value.
575
+ *
576
+ * When element `i` changes from value A to B, recomputes only groups A and B.
577
+ * Use this when cost is per-value (employee, bin, vehicle) not per-position.
578
+ *
579
+ * @example Group-based task assignment
580
+ * ```ts
581
+ * // 6 tasks assigned to workers 0-2. Cost = per-worker total.
582
+ * const taskCosts = $.let([10.0, 20.0, 15.0, 25.0, 30.0, 5.0]);
583
+ * const groupObjective = East.function(
584
+ * [VectorType(IntegerType), IntegerType], FloatType,
585
+ * ($, assignments, workerId) => {
586
+ * const total = $.let(0.0);
587
+ * $.for(East.Array.range(0n, East.value(6n)), ($, task) => {
588
+ * $.if(East.equal(assignments.get(task), workerId), $ => {
589
+ * $.assign(total, total.add(taskCosts.get(task)));
590
+ * });
591
+ * });
592
+ * return $.return(total.negate());
593
+ * }
594
+ * );
595
+ * const result = $.let(Optimization.iterativeGrouped(
596
+ * groupObjective, spaces, config
597
+ * ));
598
+ * ```
599
+ */
600
+ readonly iterativeGrouped: import("@elaraai/east").PlatformDefinition<[FunctionType<[VectorType<IntegerType>, IntegerType], FloatType>, ArrayType<VectorType<IntegerType>>, StructType<{
601
+ /** Maximum coordinate descent iterations per sample (default: 100) */
602
+ readonly iterations: OptionType<IntegerType>;
603
+ /** Number of independent restarts (default: 1) */
604
+ readonly samples: OptionType<IntegerType>;
605
+ /** How to initialize parameter values (default: first) */
606
+ readonly initial: OptionType<VariantType<{
607
+ readonly first: NullType;
608
+ readonly random: NullType;
609
+ }>>;
610
+ /** Order to evaluate candidates (default: sequential) */
611
+ readonly order: OptionType<VariantType<{
612
+ readonly sequential: NullType;
613
+ readonly random: NullType;
614
+ }>>;
615
+ /** Random seed for reproducibility */
616
+ readonly random_state: OptionType<IntegerType>;
617
+ /** Optimization mode: coordinate (default) or swap for permutations */
618
+ readonly mode: OptionType<VariantType<{
619
+ readonly coordinate: NullType;
620
+ readonly swap: NullType;
621
+ }>>;
622
+ }>], StructType<{
623
+ /** Best parameter values found */
624
+ readonly best_parameters: VectorType<IntegerType>;
625
+ /** Objective value at best parameters */
626
+ readonly best_objective: FloatType;
627
+ /** Total coordinate descent iterations across all samples */
628
+ readonly iterations: IntegerType;
629
+ /** Total number of objective evaluations */
630
+ readonly evaluations: IntegerType;
631
+ /** Whether optimization succeeded */
632
+ readonly success: BooleanType;
633
+ }>>;
356
634
  /**
357
635
  * Type definitions for optimization functions.
358
636
  */
@@ -361,6 +639,8 @@ export declare const Optimization: {
361
639
  readonly ParameterVectorType: VectorType<IntegerType>;
362
640
  /** Objective function type */
363
641
  readonly ObjectiveType: FunctionType<[VectorType<IntegerType>], FloatType>;
642
+ /** Per-element contribution function type */
643
+ readonly ElementObjectiveType: FunctionType<[VectorType<IntegerType>, IntegerType], FloatType>;
364
644
  /** Parameter spaces type */
365
645
  readonly SpacesType: ArrayType<VectorType<IntegerType>>;
366
646
  /** Initial value strategy variant */
@@ -1 +1 @@
1
- {"version":3,"file":"optimization.d.ts","sourceRoot":"","sources":["../../../src/optimization/optimization.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;GAWG;AAEH,OAAO,EAEH,UAAU,EACV,WAAW,EACX,UAAU,EACV,SAAS,EACT,UAAU,EACV,WAAW,EACX,WAAW,EACX,SAAS,EACT,QAAQ,EACR,YAAY,EACf,MAAM,eAAe,CAAC;AAMvB,wCAAwC;AACxC,eAAO,MAAM,mBAAmB,yBAA0B,CAAC;AAE3D,mDAAmD;AACnD,eAAO,MAAM,sBAAsB,oDAAiD,CAAC;AAErF,2DAA2D;AAC3D,eAAO,MAAM,mBAAmB,oCAAiC,CAAC;AAElE;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB;;;EAG9B,CAAC;AAEH;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB;;;EAG9B,CAAC;AAEH;;;;;GAKG;AACH,eAAO,MAAM,QAAQ;;;EAGnB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,mBAAmB;IAC5B,sEAAsE;;IAEtE,kDAAkD;;IAElD,0DAA0D;;;;;IAE1D,yDAAyD;;;;;IAEzD,sCAAsC;;IAEtC,u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,uEAAuE;;;;;;QAoLvE,kBAAkB;;YA5KlB,kCAAkC;;YAElC,yCAAyC;;YAEzC,6DAA6D;;YAE7D,4CAA4C;;YAE5C,qCAAqC;;;;CAsV/B,CAAC"}
@@ -22,6 +22,8 @@ import { East, StructType, VariantType, OptionType, ArrayType, VectorType, Integ
22
22
  export const ParameterVectorType = VectorType(IntegerType);
23
23
  /** Objective function: Vector<Integer> -> Float */
24
24
  export const IterativeObjectiveType = FunctionType([ParameterVectorType], FloatType);
25
+ /** Per-element contribution function: (Vector<Integer>, Integer) -> Float */
26
+ export const ElementObjectiveType = FunctionType([ParameterVectorType, IntegerType], FloatType);
25
27
  /** Per-element candidate spaces: Array<Vector<Integer>> */
26
28
  export const ParameterSpacesType = ArrayType(ParameterVectorType);
27
29
  /**
@@ -136,6 +138,78 @@ export const optimization_iterative = East.platform("optimization_iterative", [
136
138
  ParameterSpacesType, // parameter_spaces: Array<Vector<Integer>>
137
139
  IterativeConfigType, // config
138
140
  ], IterativeResultType);
141
+ /**
142
+ * Incremental iterative optimization over integer parameter vectors.
143
+ *
144
+ * Like {@link optimization_iterative}, but takes a **per-element contribution function**
145
+ * instead of a full objective. The optimizer maintains a running sum and only
146
+ * recomputes contributions for changed indices — dramatically faster when
147
+ * individual moves affect only a small part of the total cost.
148
+ *
149
+ * The total objective is `sum(elementObjective(vector, i) for all i)`.
150
+ *
151
+ * Two modes are available:
152
+ * - **coordinate** (default): When changing element `i`, recomputes 1 contribution.
153
+ * - **swap**: When swapping elements `i` and `j`, recomputes 2 contributions.
154
+ *
155
+ * @example Incremental rostering
156
+ * ```ts
157
+ * // Per-person cost: only recomputes the changed person
158
+ * const elementObjective = East.function(
159
+ * [VectorType(IntegerType), IntegerType], FloatType,
160
+ * ($, assignments, personIdx) => {
161
+ * const role = $.let(assignments.get(personIdx));
162
+ * return $.return(salaryLookup.get(personIdx).get(role).negate());
163
+ * }
164
+ * );
165
+ * const result = $.let(Optimization.iterativeIncremental(
166
+ * elementObjective, spaces, config
167
+ * ));
168
+ * ```
169
+ */
170
+ export const optimization_iterative_incremental = East.platform("optimization_iterative_incremental", [
171
+ ElementObjectiveType, // elementObjective: (Vector<Integer>, Integer) -> Float
172
+ ParameterSpacesType, // parameter_spaces: Array<Vector<Integer>>
173
+ IterativeConfigType, // config
174
+ ], IterativeResultType);
175
+ /**
176
+ * Group-based incremental iterative optimization.
177
+ *
178
+ * Like {@link optimization_iterative_incremental}, but contributions are grouped
179
+ * by **value** rather than by index. The group objective receives the full vector
180
+ * and a group key (a value that appears in the vector), and returns the total
181
+ * contribution of all elements assigned to that group.
182
+ *
183
+ * When element `i` changes from value A to value B, only 2 groups are recomputed:
184
+ * `groupObjective(vector, A)` and `groupObjective(vector, B)`.
185
+ *
186
+ * Use this when cost is associated with values (e.g., employees, bins, vehicles)
187
+ * rather than positions (e.g., slots, items, stops).
188
+ *
189
+ * @example Rostering — slot → employee assignment
190
+ * ```ts
191
+ * const groupObjective = East.function(
192
+ * [VectorType(IntegerType), IntegerType], FloatType,
193
+ * ($, slotAssignments, employeeId) => {
194
+ * const cost = $.let(0.0);
195
+ * $.for(East.Array.range(0n, nSlots), ($, slot) => {
196
+ * $.if(East.equal(slotAssignments.get(slot), employeeId), $ => {
197
+ * $.assign(cost, cost.add(shiftRates.get(slot)));
198
+ * });
199
+ * });
200
+ * return $.return(cost.negate());
201
+ * }
202
+ * );
203
+ * const result = $.let(Optimization.iterativeGrouped(
204
+ * groupObjective, spaces, config
205
+ * ));
206
+ * ```
207
+ */
208
+ export const optimization_iterative_grouped = East.platform("optimization_iterative_grouped", [
209
+ ElementObjectiveType, // groupObjective: (Vector<Integer>, Integer) -> Float
210
+ ParameterSpacesType, // parameter_spaces: Array<Vector<Integer>>
211
+ IterativeConfigType, // config
212
+ ], IterativeResultType);
139
213
  // ============================================================================
140
214
  // Grouped Export
141
215
  // ============================================================================
@@ -147,6 +221,8 @@ export const OptimizationTypes = {
147
221
  ParameterVectorType,
148
222
  /** Objective function type */
149
223
  ObjectiveType: IterativeObjectiveType,
224
+ /** Per-element contribution function type */
225
+ ElementObjectiveType,
150
226
  /** Parameter spaces type */
151
227
  SpacesType: ParameterSpacesType,
152
228
  /** Initial value strategy variant */
@@ -249,6 +325,84 @@ export const Optimization = {
249
325
  * ```
250
326
  */
251
327
  iterative: optimization_iterative,
328
+ /**
329
+ * Incremental iterative optimization with per-element contributions.
330
+ *
331
+ * `Optimization.iterativeIncremental(elementObjective, spaces, config)`
332
+ *
333
+ * Takes a per-element contribution function `(Vector<Integer>, Integer) -> Float`
334
+ * instead of a full objective. The total objective is the sum of all element
335
+ * contributions. Only recomputes contributions for changed indices during search.
336
+ *
337
+ * Use this when individual moves (coordinate or swap) affect only a small
338
+ * part of the total cost — e.g., rostering where changing one person's
339
+ * allocation only changes that person's salary.
340
+ *
341
+ * @example Incremental task-worker assignment
342
+ * ```ts
343
+ * // Per-task skill score: only recomputes the changed task
344
+ * const skill = $.let([[3.0, 1.0], [1.0, 3.0], [2.0, 2.0]]);
345
+ * const elementObjective = East.function(
346
+ * [VectorType(IntegerType), IntegerType], FloatType,
347
+ * ($, assignments, taskIdx) => {
348
+ * const worker = $.let(assignments.get(taskIdx));
349
+ * return $.return(skill.get(taskIdx).get(worker));
350
+ * }
351
+ * );
352
+ * const spaces = $.let([
353
+ * new BigInt64Array([0n, 1n]),
354
+ * new BigInt64Array([0n, 1n]),
355
+ * new BigInt64Array([0n, 1n]),
356
+ * ]);
357
+ * const config = $.let({
358
+ * iterations: variant('some', 10n),
359
+ * samples: variant('some', 3n),
360
+ * initial: variant('some', variant('random', null)),
361
+ * order: variant('some', variant('sequential', null)),
362
+ * random_state: variant('some', 42n),
363
+ * mode: variant('none', null),
364
+ * });
365
+ * const result = $.let(Optimization.iterativeIncremental(
366
+ * elementObjective, spaces, config
367
+ * ));
368
+ * // result.best_objective = 8.0 (same result, fewer evaluations)
369
+ * ```
370
+ */
371
+ iterativeIncremental: optimization_iterative_incremental,
372
+ /**
373
+ * Group-based incremental optimization with per-value contributions.
374
+ *
375
+ * `Optimization.iterativeGrouped(groupObjective, spaces, config)`
376
+ *
377
+ * Takes a group objective `(Vector<Integer>, Integer) -> Float` where the
378
+ * second argument is a **value** (group key), not an index. Returns the total
379
+ * contribution of all elements assigned to that value.
380
+ *
381
+ * When element `i` changes from value A to B, recomputes only groups A and B.
382
+ * Use this when cost is per-value (employee, bin, vehicle) not per-position.
383
+ *
384
+ * @example Group-based task assignment
385
+ * ```ts
386
+ * // 6 tasks assigned to workers 0-2. Cost = per-worker total.
387
+ * const taskCosts = $.let([10.0, 20.0, 15.0, 25.0, 30.0, 5.0]);
388
+ * const groupObjective = East.function(
389
+ * [VectorType(IntegerType), IntegerType], FloatType,
390
+ * ($, assignments, workerId) => {
391
+ * const total = $.let(0.0);
392
+ * $.for(East.Array.range(0n, East.value(6n)), ($, task) => {
393
+ * $.if(East.equal(assignments.get(task), workerId), $ => {
394
+ * $.assign(total, total.add(taskCosts.get(task)));
395
+ * });
396
+ * });
397
+ * return $.return(total.negate());
398
+ * }
399
+ * );
400
+ * const result = $.let(Optimization.iterativeGrouped(
401
+ * groupObjective, spaces, config
402
+ * ));
403
+ * ```
404
+ */
405
+ iterativeGrouped: optimization_iterative_grouped,
252
406
  /**
253
407
  * Type definitions for optimization functions.
254
408
  */
@@ -1 +1 @@
1
- {"version":3,"file":"optimization.js","sourceRoot":"","sources":["../../../src/optimization/optimization.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;GAWG;AAEH,OAAO,EACH,IAAI,EACJ,UAAU,EACV,WAAW,EACX,UAAU,EACV,SAAS,EACT,UAAU,EACV,WAAW,EACX,WAAW,EACX,SAAS,EACT,QAAQ,EACR,YAAY,GACf,MAAM,eAAe,CAAC;AAEvB,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E,wCAAwC;AACxC,MAAM,CAAC,MAAM,mBAAmB,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;AAE3D,mDAAmD;AAC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WAAW;IACxB,qCAAqC;IACrC,OAAO,EAAE,WAAW;CACvB,CAAC,CAAC;AAEH,+EAA+E;AAC/E,qBAAqB;AACrB,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,IAAI,CAAC,QAAQ,CAC/C,wBAAwB,EACxB;IACI,sBAAsB,EAAI,sCAAsC;IAChE,mBAAmB,EAAO,2CAA2C;IACrE,mBAAmB,EAAO,SAAS;CACtC,EACD,mBAAmB,CACtB,CAAC;AAEF,+EAA+E;AAC/E,iBAAiB;AACjB,+EAA+E;AAE/E;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC7B,4BAA4B;IAC5B,mBAAmB;IACnB,8BAA8B;IAC9B,aAAa,EAAE,sBAAsB;IACrC,4BAA4B;IAC5B,UAAU,EAAE,mBAAmB;IAC/B,qCAAqC;IACrC,mBAAmB;IACnB,+BAA+B;IAC/B,mBAAmB;IACnB,gCAAgC;IAChC,QAAQ;IACR,yBAAyB;IACzB,UAAU,EAAE,mBAAmB;IAC/B,kBAAkB;IAClB,UAAU,EAAE,mBAAmB;CACzB,CAAC;AAEX;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IACxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuEG;IACH,SAAS,EAAE,sBAAsB;IAEjC;;OAEG;IACH,KAAK,EAAE,iBAAiB;CAClB,CAAC"}
1
+ {"version":3,"file":"optimization.js","sourceRoot":"","sources":["../../../src/optimization/optimization.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;GAWG;AAEH,OAAO,EACH,IAAI,EACJ,UAAU,EACV,WAAW,EACX,UAAU,EACV,SAAS,EACT,UAAU,EACV,WAAW,EACX,WAAW,EACX,SAAS,EACT,QAAQ,EACR,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