Skip to content

Commit d346e1c

Browse files
authored
Merge pull request #9 from tscircuit/section-solver-2
Section Solver (Attempt 2) gets 93% on tscircuit-autorouter
2 parents de534c1 + 9327f34 commit d346e1c

20 files changed

Lines changed: 4705 additions & 914 deletions

lib/compat/loadSerializedHyperGraph.ts

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,18 @@ export const loadSerializedHyperGraph = (
354354
return netIndex
355355
}
356356

357+
const regionNetCandidates = Array.from(
358+
{ length: regionCount },
359+
() => new Set<number>(),
360+
)
361+
357362
const assignRegionNet = (regionId: string, netIndex: number) => {
358363
const regionIndex = regionIdToIndex.get(regionId)
359364
if (regionIndex === undefined) {
360365
throw new Error(`Connection references missing region "${regionId}"`)
361366
}
362367

363-
const existingNetIndex = regionNetId[regionIndex]
364-
if (existingNetIndex !== -1 && existingNetIndex !== netIndex) {
365-
throw new Error(
366-
`Region "${regionId}" is assigned to multiple nets (${existingNetIndex}, ${netIndex})`,
367-
)
368-
}
369-
370-
regionNetId[regionIndex] = netIndex
368+
regionNetCandidates[regionIndex]!.add(netIndex)
371369
}
372370

373371
connections.forEach((connection) => {
@@ -376,6 +374,12 @@ export const loadSerializedHyperGraph = (
376374
assignRegionNet(connection.endRegionId, netIndex)
377375
})
378376

377+
regionNetCandidates.forEach((candidateNetIndexes, regionIndex) => {
378+
if (candidateNetIndexes.size === 1) {
379+
regionNetId[regionIndex] = [...candidateNetIndexes][0]!
380+
}
381+
})
382+
379383
const routableConnections = connections
380384
.map((connection) => {
381385
const solvedRoute = solvedRouteByConnectionId.get(connection.connectionId)
@@ -469,27 +473,58 @@ export const loadSerializedHyperGraph = (
469473
regionNetId,
470474
}
471475

476+
const solvedRoutePathSegments: TinyHyperGraphSolution["solvedRoutePathSegments"] =
477+
[]
478+
const solvedRoutePathRegionIds: NonNullable<
479+
TinyHyperGraphSolution["solvedRoutePathRegionIds"]
480+
> = []
481+
482+
for (const { solvedRoute: route } of routableConnections) {
483+
if (!route) {
484+
solvedRoutePathSegments.push([])
485+
solvedRoutePathRegionIds.push([])
486+
continue
487+
}
488+
489+
const segments: Array<[number, number]> = []
490+
const segmentRegionIds: Array<number | undefined> = []
491+
492+
for (let i = 1; i < route.path.length; i++) {
493+
const fromCandidate = route.path[i - 1]
494+
const toCandidate = route.path[i]
495+
const fromPortId = fromCandidate?.portId
496+
const toPortId = toCandidate?.portId
497+
const fromPortIndex =
498+
fromPortId !== undefined ? portIdToIndex.get(fromPortId) : undefined
499+
const toPortIndex =
500+
toPortId !== undefined ? portIdToIndex.get(toPortId) : undefined
501+
502+
if (fromPortIndex === undefined || toPortIndex === undefined) {
503+
continue
504+
}
505+
506+
const serializedRegionId =
507+
typeof fromCandidate?.nextRegionId === "string"
508+
? fromCandidate.nextRegionId
509+
: typeof toCandidate?.lastRegionId === "string"
510+
? toCandidate.lastRegionId
511+
: undefined
512+
513+
segments.push([fromPortIndex, toPortIndex])
514+
segmentRegionIds.push(
515+
serializedRegionId !== undefined
516+
? regionIdToIndex.get(serializedRegionId)
517+
: undefined,
518+
)
519+
}
520+
521+
solvedRoutePathSegments.push(segments)
522+
solvedRoutePathRegionIds.push(segmentRegionIds)
523+
}
524+
472525
const solution: TinyHyperGraphSolution = {
473-
solvedRoutePathSegments: routableConnections.map(
474-
({ connection, solvedRoute: route }) => {
475-
if (!route) return []
476-
477-
const segments: Array<[number, number]> = []
478-
for (let i = 1; i < route.path.length; i++) {
479-
const fromPortId = route.path[i - 1]?.portId
480-
const toPortId = route.path[i]?.portId
481-
const fromPortIndex =
482-
fromPortId !== undefined ? portIdToIndex.get(fromPortId) : undefined
483-
const toPortIndex =
484-
toPortId !== undefined ? portIdToIndex.get(toPortId) : undefined
485-
486-
if (fromPortIndex !== undefined && toPortIndex !== undefined) {
487-
segments.push([fromPortIndex, toPortIndex])
488-
}
489-
}
490-
return segments
491-
},
492-
),
526+
solvedRoutePathSegments,
527+
solvedRoutePathRegionIds,
493528
}
494529

495530
return { topology, problem, solution }

0 commit comments

Comments
 (0)