Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,41 @@ export class SchematicTraceSingleLineSolver2 extends BaseSolver {
{ x: pin2.x, y: pin2.y },
)

// Seed search
this.queue.push({ path: this.baseElbow, collisionChipIds: new Set() })
this.visited.add(pathKey(this.baseElbow))
// Optimization: Use base elbow directly if collision-free
const baseCollision = findFirstCollision(this.baseElbow, this.obstacles)
if (!baseCollision && this.baseElbow.length >= 2) {
const first = this.baseElbow[0]!
const last = this.baseElbow[this.baseElbow.length - 1]!
const EPS = 1e-9
const samePoint = (p: Point, q: Point) =>
Math.abs(p.x - q.x) < EPS && Math.abs(p.y - q.y) < EPS

// Verify path is orthogonal and connects pins correctly
let isOrthogonal = true
for (let i = 0; i < this.baseElbow.length - 1; i++) {
const a = this.baseElbow[i]!
const b = this.baseElbow[i + 1]!
if (!isHorizontal(a, b) && !isVertical(a, b)) {
isOrthogonal = false
break
}
}

if (
isOrthogonal &&
samePoint(first, { x: pin1.x, y: pin1.y }) &&
samePoint(last, { x: pin2.x, y: pin2.y })
) {
this.solvedTracePath = this.baseElbow
this.solved = true
}
}

// Fallback: Use pathfinding if optimization didn't solve it
if (!this.solved) {
this.queue.push({ path: this.baseElbow, collisionChipIds: new Set() })
this.visited.add(pathKey(this.baseElbow))
}
}

override getConstructorParams(): ConstructorParameters<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,87 @@ test("SchematicTraceSingleLineSolver2 should solve problem correctly", () => {

expect(solver).toMatchSolverSnapshot(import.meta.path)
})

test("SchematicTraceSingleLineSolver2 collision-free optimization", () => {
const input = {
chipMap: {
chip1: {
chipId: "chip1",
center: { x: -2, y: -1 },
width: 1,
height: 1,
pins: [{ pinId: "pin1", x: -1.5, y: -1 }],
},
chip2: {
chipId: "chip2",
center: { x: 2, y: 1 },
width: 1,
height: 1,
pins: [{ pinId: "pin2", x: 1.5, y: 1 }],
},
chip3: {
chipId: "chip3",
center: { x: 0, y: -3 },
width: 1,
height: 1,
pins: [],
},
},
pins: [
{
pinId: "pin1",
x: -1.5,
y: -1,
chipId: "chip1",
_facingDirection: "x+" as const,
},
{
pinId: "pin2",
x: 1.5,
y: 1,
chipId: "chip2",
_facingDirection: "x-" as const,
},
],
inputProblem: {
chips: [
{
chipId: "chip1",
center: { x: -2, y: -1 },
width: 1,
height: 1,
pins: [{ pinId: "pin1", x: -1.5, y: -1 }],
},
{
chipId: "chip2",
center: { x: 2, y: 1 },
width: 1,
height: 1,
pins: [{ pinId: "pin2", x: 1.5, y: 1 }],
},
{
chipId: "chip3",
center: { x: 0, y: -3 },
width: 1,
height: 1,
pins: [],
},
],
directConnections: [],
netConnections: [],
availableNetLabelOrientations: {},
},
}

const solver = new SchematicTraceSingleLineSolver2(input as any)

// Should be solved immediately via collision-free optimization
expect(solver.solved).toBe(true)
expect(solver.solvedTracePath).not.toBeNull()

solver.solve()
expect(solver).toMatchSolverSnapshot(
import.meta.path,
"collision-free-optimization",
)
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.