@tscircuit/capacity-autorouter 0.0.59 → 0.0.61

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/dist/index.d.ts CHANGED
@@ -101,7 +101,7 @@ declare class BaseSolver {
101
101
  activeSubSolver?: BaseSolver | null;
102
102
  failedSubSolvers?: BaseSolver[];
103
103
  timeToSolve?: number;
104
- stats: Record<string, number>;
104
+ stats: Record<string, any>;
105
105
  /**
106
106
  * For cached solvers
107
107
  **/
@@ -1250,6 +1250,7 @@ interface CacheProvider {
1250
1250
  getCachedSolution(cacheKey: string): Promise<any>;
1251
1251
  setCachedSolutionSync(cacheKey: string, cachedSolution: any): void;
1252
1252
  setCachedSolution(cacheKey: string, cachedSolution: any): Promise<void>;
1253
+ getAllCacheKeys(): string[];
1253
1254
  clearCache(): void;
1254
1255
  }
1255
1256
 
@@ -1522,18 +1523,37 @@ declare class CapacityPathingMultiSectionSolver extends BaseSolver {
1522
1523
  nodeOptimizationAttemptCountMap: Map<CapacityMeshNodeId, number>;
1523
1524
  currentSection: CapacityPathingSection | null;
1524
1525
  sectionSolver?: CapacityPathingSingleSectionSolver | HyperCapacityPathingSingleSectionSolver | null;
1525
- MAX_ATTEMPTS_PER_NODE: number;
1526
- MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE: number;
1527
- MAX_EXPANSION_DEGREES: number;
1526
+ currentScheduleIndex: number;
1528
1527
  stats: {
1529
1528
  successfulOptimizations: number;
1530
1529
  failedOptimizations: number;
1530
+ failedSectionSolvers: number;
1531
+ scheduleScores: Array<{
1532
+ maxExpansionDegrees: number;
1533
+ sectionAttempts: number;
1534
+ endingScore: number;
1535
+ endingHighestNodePf: number;
1536
+ }>;
1537
+ };
1538
+ OPTIMIZATION_SCHEDULE: {
1539
+ MAX_ATTEMPTS_PER_NODE: number;
1540
+ MAX_EXPANSION_DEGREES: number;
1541
+ MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE: number;
1542
+ }[];
1543
+ get currentSchedule(): {
1544
+ MAX_ATTEMPTS_PER_NODE: number;
1545
+ MAX_EXPANSION_DEGREES: number;
1546
+ MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE: number;
1531
1547
  };
1532
1548
  constructor(params: ConstructorParameters<typeof CapacityPathingSolver>[0] & {
1533
1549
  initialPathingSolver?: CapacityPathingGreedySolver;
1534
1550
  });
1535
1551
  _stepInitialization(): void;
1536
1552
  _getNextNodeToOptimize(): CapacityMeshNodeId | null;
1553
+ getOverallScore(): {
1554
+ highestNodePf: number;
1555
+ score: number;
1556
+ };
1537
1557
  _stepSectionOptimization(): void;
1538
1558
  /**
1539
1559
  * Merges the paths found by a successful section solver back into the main
package/dist/index.js CHANGED
@@ -68,7 +68,7 @@ var BaseSolver = class {
68
68
  throw e;
69
69
  }
70
70
  if (!this.solved && this.iterations > this.MAX_ITERATIONS) {
71
- this.error = `${this.constructor.name} did not converge`;
71
+ this.error = `${this.constructor.name} ran out of iterations`;
72
72
  console.error(this.error);
73
73
  this.failed = true;
74
74
  }
@@ -10983,6 +10983,9 @@ var InMemoryCache = class {
10983
10983
  this.cacheHits = 0;
10984
10984
  this.cacheMisses = 0;
10985
10985
  }
10986
+ getAllCacheKeys() {
10987
+ return Array.from(this.cache.keys());
10988
+ }
10986
10989
  };
10987
10990
 
10988
10991
  // lib/cache/LocalStorageCache.ts
@@ -11089,6 +11092,16 @@ var LocalStorageCache = class {
11089
11092
  this.cacheMisses = 0;
11090
11093
  }
11091
11094
  }
11095
+ getAllCacheKeys() {
11096
+ const cacheKeys = [];
11097
+ for (let i = 0; i < 1e4; i++) {
11098
+ const keyName = localStorage.key(i);
11099
+ if (!keyName) break;
11100
+ if (!keyName.includes(CACHE_PREFIX)) continue;
11101
+ cacheKeys.push(keyName);
11102
+ }
11103
+ return cacheKeys;
11104
+ }
11092
11105
  };
11093
11106
 
11094
11107
  // lib/cache/setupGlobalCaches.ts
@@ -12718,7 +12731,7 @@ ${percent}% (Pf: ${(probabilityOfFailure * 100).toFixed(1)}%)`;
12718
12731
  return graphics;
12719
12732
  }
12720
12733
 
12721
- // lib/solvers/CapacityPathingSectionSolver/CapacityPathingSingleSectionPathingSolver.ts
12734
+ // lib/solvers/CapacityPathingSectionSolver/CapacityPathingSingleSectionSolver.ts
12722
12735
  var CapacityPathingSingleSectionSolver = class extends BaseSolver {
12723
12736
  GREEDY_MULTIPLIER = 1.5;
12724
12737
  sectionNodes;
@@ -12745,6 +12758,7 @@ var CapacityPathingSingleSectionSolver = class extends BaseSolver {
12745
12758
  // Default, similar to CapacityPathingSolver5
12746
12759
  constructor(params) {
12747
12760
  super();
12761
+ this.MAX_ITERATIONS = 1e4;
12748
12762
  this.centerNodeId = params.centerNodeId;
12749
12763
  this.sectionNodes = params.sectionNodes;
12750
12764
  this.sectionEdges = params.sectionEdges;
@@ -13053,7 +13067,7 @@ var HyperCapacityPathingSingleSectionSolver = class extends HyperParameterSuperv
13053
13067
  winningSolver;
13054
13068
  constructor(params) {
13055
13069
  super();
13056
- this.MAX_ITERATIONS = 1e4;
13070
+ this.MAX_ITERATIONS = 1e5;
13057
13071
  this.constructorParams = params;
13058
13072
  }
13059
13073
  computeG(solver) {
@@ -13217,15 +13231,37 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13217
13231
  nodeOptimizationAttemptCountMap = /* @__PURE__ */ new Map();
13218
13232
  currentSection = null;
13219
13233
  sectionSolver = null;
13220
- MAX_ATTEMPTS_PER_NODE = 1;
13221
- MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE = 0.05;
13222
- MAX_EXPANSION_DEGREES = 5;
13234
+ currentScheduleIndex = 0;
13223
13235
  stats;
13236
+ OPTIMIZATION_SCHEDULE = [
13237
+ {
13238
+ MAX_ATTEMPTS_PER_NODE: 1,
13239
+ MAX_EXPANSION_DEGREES: 5,
13240
+ MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE: 0.1
13241
+ },
13242
+ {
13243
+ MAX_ATTEMPTS_PER_NODE: 2,
13244
+ MAX_EXPANSION_DEGREES: 3,
13245
+ MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE: 0.05
13246
+ }
13247
+ ];
13248
+ get currentSchedule() {
13249
+ return this.OPTIMIZATION_SCHEDULE[this.currentScheduleIndex] ?? null;
13250
+ }
13224
13251
  constructor(params) {
13225
13252
  super();
13226
13253
  this.stats = {
13227
13254
  successfulOptimizations: 0,
13228
- failedOptimizations: 0
13255
+ failedOptimizations: 0,
13256
+ failedSectionSolvers: 0,
13257
+ scheduleScores: this.OPTIMIZATION_SCHEDULE.map(
13258
+ ({ MAX_EXPANSION_DEGREES }) => ({
13259
+ maxExpansionDegrees: MAX_EXPANSION_DEGREES,
13260
+ endingScore: 0,
13261
+ endingHighestNodePf: 0,
13262
+ sectionAttempts: 0
13263
+ })
13264
+ )
13229
13265
  };
13230
13266
  this.MAX_ITERATIONS = 1e7;
13231
13267
  this.simpleRouteJson = params.simpleRouteJson;
@@ -13287,7 +13323,7 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13287
13323
  node.availableZ.length
13288
13324
  );
13289
13325
  const nodePfDivAttempts = nodePf / (attemptCount + 1);
13290
- if (attemptCount < this.MAX_ATTEMPTS_PER_NODE && nodePfDivAttempts > highestNodePfDivAttempts && nodePf > this.MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE) {
13326
+ if (attemptCount < this.currentSchedule.MAX_ATTEMPTS_PER_NODE && nodePfDivAttempts > highestNodePfDivAttempts && nodePf > this.currentSchedule.MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE) {
13291
13327
  highestNodePfDivAttempts = nodePfDivAttempts;
13292
13328
  highestNodePf = nodePf;
13293
13329
  nodeWithHighestPercentCapacityUsed = node.capacityMeshNodeId;
@@ -13295,11 +13331,45 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13295
13331
  }
13296
13332
  return nodeWithHighestPercentCapacityUsed;
13297
13333
  }
13334
+ getOverallScore() {
13335
+ let highestNodePf = 0;
13336
+ for (const node of this.nodes) {
13337
+ if (node._containsTarget) continue;
13338
+ const totalCapacity = this.totalNodeCapacityMap.get(
13339
+ node.capacityMeshNodeId
13340
+ );
13341
+ const nodePf = calculateNodeProbabilityOfFailure2(
13342
+ this.usedNodeCapacityMap.get(node.capacityMeshNodeId) ?? 0,
13343
+ totalCapacity,
13344
+ node.availableZ.length
13345
+ );
13346
+ if (nodePf > highestNodePf) {
13347
+ highestNodePf = nodePf;
13348
+ }
13349
+ }
13350
+ return {
13351
+ highestNodePf,
13352
+ score: computeSectionScore({
13353
+ totalNodeCapacityMap: this.totalNodeCapacityMap,
13354
+ usedNodeCapacityMap: this.usedNodeCapacityMap,
13355
+ nodeMap: this.nodeMap,
13356
+ sectionNodeIds: new Set(
13357
+ this.nodes.map((node) => node.capacityMeshNodeId)
13358
+ )
13359
+ })
13360
+ };
13361
+ }
13298
13362
  _stepSectionOptimization() {
13299
13363
  if (!this.sectionSolver) {
13300
13364
  const centerNodeId = this._getNextNodeToOptimize();
13301
13365
  if (!centerNodeId) {
13302
- this.solved = true;
13366
+ const { highestNodePf, score } = this.getOverallScore();
13367
+ this.stats.scheduleScores[this.currentScheduleIndex].endingHighestNodePf = highestNodePf;
13368
+ this.stats.scheduleScores[this.currentScheduleIndex].endingScore = score;
13369
+ this.currentScheduleIndex++;
13370
+ if (!this.currentSchedule) {
13371
+ this.solved = true;
13372
+ }
13303
13373
  return;
13304
13374
  }
13305
13375
  const section = computeSectionNodesTerminalsAndEdges({
@@ -13307,16 +13377,18 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13307
13377
  connectionsWithNodes: this.connectionsWithNodes,
13308
13378
  nodeMap: this.nodeMap,
13309
13379
  edges: this.edges,
13310
- expansionDegrees: this.MAX_EXPANSION_DEGREES,
13380
+ expansionDegrees: this.currentSchedule.MAX_EXPANSION_DEGREES,
13381
+ // Corrected
13311
13382
  nodeEdgeMap: this.nodeEdgeMap
13312
13383
  });
13384
+ this.stats.scheduleScores[this.currentScheduleIndex].sectionAttempts++;
13313
13385
  this.currentSection = section;
13314
13386
  this.sectionSolver = new HyperCapacityPathingSingleSectionSolver({
13315
- sectionConnectionTerminals: section.sectionConnectionTerminals,
13316
- sectionEdges: section.sectionEdges,
13317
- sectionNodes: section.sectionNodes,
13387
+ sectionNodes: this.currentSection.sectionNodes,
13388
+ sectionEdges: this.currentSection.sectionEdges,
13389
+ sectionConnectionTerminals: this.currentSection.sectionConnectionTerminals,
13318
13390
  colorMap: this.colorMap,
13319
- centerNodeId: section.centerNodeId,
13391
+ centerNodeId: this.currentSection.centerNodeId,
13320
13392
  nodeEdgeMap: this.nodeEdgeMap
13321
13393
  });
13322
13394
  this.activeSubSolver = this.sectionSolver;
@@ -13330,6 +13402,8 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13330
13402
  console.warn(
13331
13403
  `Section solver failed for node ${this.currentSection.centerNodeId}. Error: ${this.sectionSolver.error}`
13332
13404
  );
13405
+ this.stats.failedSectionSolvers++;
13406
+ this.stats.failedOptimizations++;
13333
13407
  this.sectionSolver = null;
13334
13408
  this.activeSubSolver = null;
13335
13409
  return;
@@ -15758,7 +15832,7 @@ var AutoroutingPipelineSolver = class extends BaseSolver {
15758
15832
  super();
15759
15833
  this.srj = srj;
15760
15834
  this.opts = opts;
15761
- this.MAX_ITERATIONS = 1e7;
15835
+ this.MAX_ITERATIONS = 1e8;
15762
15836
  if (opts.capacityDepth === void 0) {
15763
15837
  const boundsWidth = srj.bounds.maxX - srj.bounds.minX;
15764
15838
  const boundsHeight = srj.bounds.maxY - srj.bounds.minY;