Skip to content

Commit bc7305e

Browse files
authored
Merge pull request #4 from tscircuit/stats-display
Add stats display, cost function adjustment to penalize for high trace counts
2 parents 0ffef29 + 88909ce commit bc7305e

7 files changed

Lines changed: 51 additions & 17 deletions

File tree

lib/computeRegionCost.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const viaSize = 0.45
22
const viaSizeSq = viaSize ** 2
3+
const traceWidth = 0.1
34

45
export const computeRegionCost = (
56
regionWidth: number,
67
regionHeight: number,
78
numSameLayerIntersections: number,
89
numCrossLayerIntersections: number,
910
numEntryExitChanges: number,
11+
traceCount: number,
1012
) => {
1113
const area = regionWidth * regionHeight
1214

@@ -15,5 +17,7 @@ export const computeRegionCost = (
1517
numCrossLayerIntersections * 1 +
1618
numEntryExitChanges * 1
1719

18-
return (estViasRequired * viaSizeSq) / area
20+
const traceCountMult = 1 + traceCount / 5
21+
22+
return (estViasRequired * viaSizeSq * traceCountMult) / area
1923
}

lib/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const createEmptyRegionIntersectionCache = (): RegionIntersectionCache => ({
2626
existingSameLayerIntersections: 0,
2727
existingEntryExitLayerChanges: 0,
2828
existingRegionCost: 0,
29+
existingSegmentCount: 0,
2930
})
3031

3132
export interface TinyHyperGraphTopology {
@@ -138,7 +139,7 @@ export class TinyHyperGraphSolver extends BaseSolver {
138139

139140
RIP_THRESHOLD_START = 0.05
140141
RIP_THRESHOLD_END = 0.8
141-
RIP_THRESHOLD_RAMP_ATTEMPTS = 20
142+
RIP_THRESHOLD_RAMP_ATTEMPTS = 50
142143

143144
RIP_CONGESTION_REGION_COST_FACTOR = 0.1
144145

@@ -438,6 +439,7 @@ export class TinyHyperGraphSolver extends BaseSolver {
438439
newCrossLayerIntersections
439440
const existingEntryExitLayerChanges =
440441
regionCache.existingEntryExitLayerChanges + newEntryExitLayerChanges
442+
const existingSegmentCount = lesserAngles.length
441443

442444
state.regionIntersectionCaches[regionId] = {
443445
netIds,
@@ -447,12 +449,14 @@ export class TinyHyperGraphSolver extends BaseSolver {
447449
existingSameLayerIntersections,
448450
existingCrossingLayerIntersections,
449451
existingEntryExitLayerChanges,
452+
existingSegmentCount,
450453
existingRegionCost: computeRegionCost(
451454
topology.regionWidth[regionId],
452455
topology.regionHeight[regionId],
453456
existingSameLayerIntersections,
454457
existingCrossingLayerIntersections,
455458
existingEntryExitLayerChanges,
459+
existingSegmentCount,
456460
),
457461
}
458462
}
@@ -550,7 +554,10 @@ export class TinyHyperGraphSolver extends BaseSolver {
550554
ripCount: state.ripCount,
551555
}
552556

553-
if (regionIdsOverCostThreshold.length === 0) {
557+
if (
558+
regionIdsOverCostThreshold.length === 0 ||
559+
state.ripCount === this.RIP_THRESHOLD_RAMP_ATTEMPTS
560+
) {
554561
this.solved = true
555562
return
556563
}
@@ -639,6 +646,7 @@ export class TinyHyperGraphSolver extends BaseSolver {
639646
regionCache.existingCrossingLayerIntersections +
640647
newCrossLayerIntersections,
641648
regionCache.existingEntryExitLayerChanges + newEntryExitLayerChanges,
649+
regionCache.existingSegmentCount + 1,
642650
) - regionCache.existingRegionCost
643651

644652
return (

lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface RegionIntersectionCache extends DynamicAnglePairArrays {
2929
existingCrossingLayerIntersections: Integer
3030
existingEntryExitLayerChanges: Integer
3131
existingRegionCost: number
32+
existingSegmentCount: number
3233
}
3334

3435
export type SameLayerIntersectionCount = number

lib/visualizeTinyGraph.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const getRegionCostLabel = (
118118
regionId: RegionId,
119119
): string => {
120120
const regionCache = solver.state.regionIntersectionCaches[regionId]
121-
const regionCost = getRegionDisplayCost(solver, regionId)
121+
const regionCost = regionCache?.existingRegionCost ?? 0
122122
const congestionCost = solver.state.regionCongestionCost[regionId] ?? 0
123123
const regionNetId = solver.problem.regionNetId[regionId]
124124
const regionNetLabel = regionNetId === -1 ? "free" : `${regionNetId}`
@@ -134,13 +134,6 @@ const getRegionCostLabel = (
134134
)
135135
}
136136

137-
const getRegionDisplayCost = (
138-
solver: TinyHyperGraphSolver,
139-
regionId: RegionId,
140-
): number =>
141-
(solver.state.regionIntersectionCaches[regionId]?.existingRegionCost ?? 0) +
142-
(solver.state.regionCongestionCost[regionId] ?? 0)
143-
144137
const getBaseRegionFillColor = (
145138
solver: TinyHyperGraphSolver,
146139
regionId: RegionId,
@@ -171,7 +164,9 @@ const getRegionRectFill = (
171164
regionId: RegionId,
172165
): string => {
173166
const baseFill = getBaseRegionFillColor(solver, regionId)
174-
const cost = clamp01(getRegionDisplayCost(solver, regionId))
167+
const cost = clamp01(
168+
solver.state.regionIntersectionCaches[regionId]?.existingRegionCost ?? 0,
169+
)
175170
const redness = Math.pow(cost, 0.8)
176171

177172
return toRgbaString(mixColor(baseFill, HOT_REGION_FILL, redness))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"devDependencies": {
1010
"@biomejs/biome": "^2.4.8",
1111
"@tscircuit/hypergraph": "^0.0.71",
12-
"@tscircuit/solver-utils": "^0.0.16",
12+
"@tscircuit/solver-utils": "^0.0.17",
1313
"@types/bun": "latest",
1414
"cosmos": "^0.1.2",
1515
"dataset-hg07": "https://github.com/tscircuit/dataset-hg07",

scripts/profiling/hg07-first10.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ const computeDatasetMaxRegionCost = (
4747
]),
4848
)
4949

50-
serializedHyperGraph.regions.forEach((region, regionIndex) => {
51-
regionIdToIndex.set(region.regionId, regionIndex)
50+
topology.regionMetadata?.forEach((metadata, regionIndex) => {
51+
const serializedRegionId = metadata?.serializedRegionId
52+
if (typeof serializedRegionId === "string") {
53+
regionIdToIndex.set(serializedRegionId, regionIndex)
54+
}
5255
})
5356

54-
serializedHyperGraph.ports.forEach((port, portIndex) => {
55-
portIdToIndex.set(port.portId, portIndex)
57+
topology.portMetadata?.forEach((metadata, portIndex) => {
58+
const serializedPortId = metadata?.serializedPortId
59+
if (typeof serializedPortId === "string") {
60+
portIdToIndex.set(serializedPortId, portIndex)
61+
}
5662
})
5763

5864
for (let routeId = 0; routeId < problem.routeCount; routeId++) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expect, test } from "bun:test"
2+
3+
test("hg07 first10 profiling script runs successfully", () => {
4+
const result = Bun.spawnSync(
5+
["bun", "run", "scripts/profiling/hg07-first10.ts"],
6+
{
7+
cwd: process.cwd(),
8+
stdout: "pipe",
9+
stderr: "pipe",
10+
},
11+
)
12+
13+
const stdout = new TextDecoder().decode(result.stdout)
14+
const stderr = new TextDecoder().decode(result.stderr)
15+
16+
expect(result.exitCode).toBe(0)
17+
expect(stderr).toBe("")
18+
expect(stdout).toContain("hg-07 first 10 solve profile")
19+
expect(stdout).toContain("samples=10 failed=0")
20+
})

0 commit comments

Comments
 (0)