Skip to content
Merged
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 @@ -22,7 +22,10 @@ export class SingleTargetNecessaryCrampedPortPointSolver extends BaseSolver {
private queue: ExploredPortPoint[] = []
private resultExploredPortPoints: ExploredPortPoint[] = []
private currentExploredPortPoints: ExploredPortPoint | null = null
private visitedExploredPortPoints: SegmentPortPoint[] = []
private visitedExploredPortPoints = new Map<
SegmentPortPoint,
ExploredPortPoint
>()

constructor(private input: SingleTargetNecessaryCrampedPortPointSolverInput) {
super()
Expand All @@ -49,11 +52,16 @@ export class SingleTargetNecessaryCrampedPortPointSolver extends BaseSolver {
parent: null,
countOfCrampedPortPointsInPath: seedPort.cramped ? 1 : 0,
}
this.queue.push(initialCandidate)
const existingCandidate = this.visitedExploredPortPoints.get(seedPort)
if (
!existingCandidate ||
this.getCandidateCost(initialCandidate) <
this.getCandidateCost(existingCandidate)
) {
this.visitedExploredPortPoints.set(seedPort, initialCandidate)
this.queue.push(initialCandidate)
}
}
this.visitedExploredPortPoints = [
...new Set(this.queue.map((candidate) => candidate.port)),
]
}

override _step() {
Expand All @@ -65,6 +73,12 @@ export class SingleTargetNecessaryCrampedPortPointSolver extends BaseSolver {

while (this.queue.length > 0) {
this.currentExploredPortPoints = this.queue.shift()!
const bestKnownCandidate = this.visitedExploredPortPoints.get(
this.currentExploredPortPoints.port,
)
if (bestKnownCandidate !== this.currentExploredPortPoints) {
continue
}

if (this.currentExploredPortPoints.depth === this.input.depthLimit) {
this.resultExploredPortPoints.push(this.currentExploredPortPoints)
Expand Down Expand Up @@ -95,24 +109,34 @@ export class SingleTargetNecessaryCrampedPortPointSolver extends BaseSolver {
continue
}

this.queue.push({
const nextCandidate: ExploredPortPoint = {
port: nextPort,
depth: this.currentExploredPortPoints.depth + 1,
parent: this.currentExploredPortPoints,
countOfCrampedPortPointsInPath:
this.currentExploredPortPoints.countOfCrampedPortPointsInPath +
(nextPort.cramped ? 1 : 0),
})
}
const existingCandidate = this.visitedExploredPortPoints.get(nextPort)
if (
existingCandidate &&
this.getCandidateCost(existingCandidate) <=
this.getCandidateCost(nextCandidate)
) {
continue
}
this.visitedExploredPortPoints.set(nextPort, nextCandidate)
this.queue.push(nextCandidate)
}
}

this.visitedExploredPortPoints = [
...new Set(this.queue.map((candidate) => candidate.port)),
...this.visitedExploredPortPoints,
]
this.solved = true
}

private getCandidateCost(candidate: ExploredPortPoint): number {
return candidate.depth + candidate.countOfCrampedPortPointsInPath * 1000
}

getOutput() {
return this.resultExploredPortPoints
}
Expand All @@ -123,7 +147,7 @@ export class SingleTargetNecessaryCrampedPortPointSolver extends BaseSolver {
rects: [],
}

for (const candidate of this.visitedExploredPortPoints) {
for (const candidate of this.visitedExploredPortPoints.keys()) {
graphics.points!.push({
...candidate,
color: candidate.cramped ? "blue" : "green",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ test("necessary cramped port point solver does not explode duplicate candidates"
...singleTargetStats.map((stat) => stat.candidateCount),
)

expect(maxCandidateCount).toBeGreaterThan(500)
// currently we are at 14144
expect(maxCandidateCount).toBeLessThan(500)
SingleTargetNecessaryCrampedPortPointSolver.prototype._step = originalStep
})
Loading