@tscircuit/capacity-autorouter 0.0.60 → 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
  **/
@@ -1523,18 +1523,37 @@ declare class CapacityPathingMultiSectionSolver extends BaseSolver {
1523
1523
  nodeOptimizationAttemptCountMap: Map<CapacityMeshNodeId, number>;
1524
1524
  currentSection: CapacityPathingSection | null;
1525
1525
  sectionSolver?: CapacityPathingSingleSectionSolver | HyperCapacityPathingSingleSectionSolver | null;
1526
- MAX_ATTEMPTS_PER_NODE: number;
1527
- MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE: number;
1528
- MAX_EXPANSION_DEGREES: number;
1526
+ currentScheduleIndex: number;
1529
1527
  stats: {
1530
1528
  successfulOptimizations: number;
1531
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;
1532
1547
  };
1533
1548
  constructor(params: ConstructorParameters<typeof CapacityPathingSolver>[0] & {
1534
1549
  initialPathingSolver?: CapacityPathingGreedySolver;
1535
1550
  });
1536
1551
  _stepInitialization(): void;
1537
1552
  _getNextNodeToOptimize(): CapacityMeshNodeId | null;
1553
+ getOverallScore(): {
1554
+ highestNodePf: number;
1555
+ score: number;
1556
+ };
1538
1557
  _stepSectionOptimization(): void;
1539
1558
  /**
1540
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
  }
@@ -12731,7 +12731,7 @@ ${percent}% (Pf: ${(probabilityOfFailure * 100).toFixed(1)}%)`;
12731
12731
  return graphics;
12732
12732
  }
12733
12733
 
12734
- // lib/solvers/CapacityPathingSectionSolver/CapacityPathingSingleSectionPathingSolver.ts
12734
+ // lib/solvers/CapacityPathingSectionSolver/CapacityPathingSingleSectionSolver.ts
12735
12735
  var CapacityPathingSingleSectionSolver = class extends BaseSolver {
12736
12736
  GREEDY_MULTIPLIER = 1.5;
12737
12737
  sectionNodes;
@@ -12758,6 +12758,7 @@ var CapacityPathingSingleSectionSolver = class extends BaseSolver {
12758
12758
  // Default, similar to CapacityPathingSolver5
12759
12759
  constructor(params) {
12760
12760
  super();
12761
+ this.MAX_ITERATIONS = 1e4;
12761
12762
  this.centerNodeId = params.centerNodeId;
12762
12763
  this.sectionNodes = params.sectionNodes;
12763
12764
  this.sectionEdges = params.sectionEdges;
@@ -13066,7 +13067,7 @@ var HyperCapacityPathingSingleSectionSolver = class extends HyperParameterSuperv
13066
13067
  winningSolver;
13067
13068
  constructor(params) {
13068
13069
  super();
13069
- this.MAX_ITERATIONS = 1e4;
13070
+ this.MAX_ITERATIONS = 1e5;
13070
13071
  this.constructorParams = params;
13071
13072
  }
13072
13073
  computeG(solver) {
@@ -13230,15 +13231,37 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13230
13231
  nodeOptimizationAttemptCountMap = /* @__PURE__ */ new Map();
13231
13232
  currentSection = null;
13232
13233
  sectionSolver = null;
13233
- MAX_ATTEMPTS_PER_NODE = 1;
13234
- MINIMUM_PROBABILITY_OF_FAILURE_TO_OPTIMIZE = 0.05;
13235
- MAX_EXPANSION_DEGREES = 5;
13234
+ currentScheduleIndex = 0;
13236
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
+ }
13237
13251
  constructor(params) {
13238
13252
  super();
13239
13253
  this.stats = {
13240
13254
  successfulOptimizations: 0,
13241
- 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
+ )
13242
13265
  };
13243
13266
  this.MAX_ITERATIONS = 1e7;
13244
13267
  this.simpleRouteJson = params.simpleRouteJson;
@@ -13300,7 +13323,7 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13300
13323
  node.availableZ.length
13301
13324
  );
13302
13325
  const nodePfDivAttempts = nodePf / (attemptCount + 1);
13303
- 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) {
13304
13327
  highestNodePfDivAttempts = nodePfDivAttempts;
13305
13328
  highestNodePf = nodePf;
13306
13329
  nodeWithHighestPercentCapacityUsed = node.capacityMeshNodeId;
@@ -13308,11 +13331,45 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13308
13331
  }
13309
13332
  return nodeWithHighestPercentCapacityUsed;
13310
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
+ }
13311
13362
  _stepSectionOptimization() {
13312
13363
  if (!this.sectionSolver) {
13313
13364
  const centerNodeId = this._getNextNodeToOptimize();
13314
13365
  if (!centerNodeId) {
13315
- 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
+ }
13316
13373
  return;
13317
13374
  }
13318
13375
  const section = computeSectionNodesTerminalsAndEdges({
@@ -13320,16 +13377,18 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13320
13377
  connectionsWithNodes: this.connectionsWithNodes,
13321
13378
  nodeMap: this.nodeMap,
13322
13379
  edges: this.edges,
13323
- expansionDegrees: this.MAX_EXPANSION_DEGREES,
13380
+ expansionDegrees: this.currentSchedule.MAX_EXPANSION_DEGREES,
13381
+ // Corrected
13324
13382
  nodeEdgeMap: this.nodeEdgeMap
13325
13383
  });
13384
+ this.stats.scheduleScores[this.currentScheduleIndex].sectionAttempts++;
13326
13385
  this.currentSection = section;
13327
13386
  this.sectionSolver = new HyperCapacityPathingSingleSectionSolver({
13328
- sectionConnectionTerminals: section.sectionConnectionTerminals,
13329
- sectionEdges: section.sectionEdges,
13330
- sectionNodes: section.sectionNodes,
13387
+ sectionNodes: this.currentSection.sectionNodes,
13388
+ sectionEdges: this.currentSection.sectionEdges,
13389
+ sectionConnectionTerminals: this.currentSection.sectionConnectionTerminals,
13331
13390
  colorMap: this.colorMap,
13332
- centerNodeId: section.centerNodeId,
13391
+ centerNodeId: this.currentSection.centerNodeId,
13333
13392
  nodeEdgeMap: this.nodeEdgeMap
13334
13393
  });
13335
13394
  this.activeSubSolver = this.sectionSolver;
@@ -13343,6 +13402,8 @@ var CapacityPathingMultiSectionSolver = class extends BaseSolver {
13343
13402
  console.warn(
13344
13403
  `Section solver failed for node ${this.currentSection.centerNodeId}. Error: ${this.sectionSolver.error}`
13345
13404
  );
13405
+ this.stats.failedSectionSolvers++;
13406
+ this.stats.failedOptimizations++;
13346
13407
  this.sectionSolver = null;
13347
13408
  this.activeSubSolver = null;
13348
13409
  return;
@@ -15771,7 +15832,7 @@ var AutoroutingPipelineSolver = class extends BaseSolver {
15771
15832
  super();
15772
15833
  this.srj = srj;
15773
15834
  this.opts = opts;
15774
- this.MAX_ITERATIONS = 1e7;
15835
+ this.MAX_ITERATIONS = 1e8;
15775
15836
  if (opts.capacityDepth === void 0) {
15776
15837
  const boundsWidth = srj.bounds.maxX - srj.bounds.minX;
15777
15838
  const boundsHeight = srj.bounds.maxY - srj.bounds.minY;