-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathSingleTargetNecessaryCrampedPortPointSolver.ts
More file actions
159 lines (141 loc) · 4.7 KB
/
SingleTargetNecessaryCrampedPortPointSolver.ts
File metadata and controls
159 lines (141 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
CapacityMeshNode,
CapacityMeshNodeId,
} from "lib/types/capacity-mesh-types"
import { ExploredPortPoint } from "./types"
import { SegmentPortPoint } from "../AvailableSegmentPointSolver/AvailableSegmentPointSolver"
import { BaseSolver } from "@tscircuit/solver-utils"
import { GraphicsObject } from "graphics-debug"
type SingleTargetNecessaryCrampedPortPointSolverInput = {
target: CapacityMeshNode
mapOfCapacityMeshNodeIdToSegmentPortPoints: Map<
CapacityMeshNodeId,
SegmentPortPoint[]
>
mapOfCapacityMeshNodeIdToRef: Map<CapacityMeshNodeId, CapacityMeshNode>
depthLimit: number
shouldIgnoreCrampedPortPoints: boolean
}
export class SingleTargetNecessaryCrampedPortPointSolver extends BaseSolver {
private queue: ExploredPortPoint[] = []
private resultExploredPortPoints: ExploredPortPoint[] = []
private currentExploredPortPoints: ExploredPortPoint | null = null
private visitedExploredPortPoints = new Map<
SegmentPortPoint,
ExploredPortPoint
>()
constructor(private input: SingleTargetNecessaryCrampedPortPointSolverInput) {
super()
if (this.input.depthLimit < 1) {
throw new Error("Depth limit must be at least 1")
}
this._setup()
}
override getSolverName() {
return "singleTargetNecessaryCrampedPortPointSolver"
}
override _setup(): void {
const seedPorts =
this.input.mapOfCapacityMeshNodeIdToSegmentPortPoints.get(
this.input.target.capacityMeshNodeId,
) ?? []
for (const seedPort of seedPorts) {
if (this.input.shouldIgnoreCrampedPortPoints && seedPort.cramped) continue
const initialCandidate: ExploredPortPoint = {
port: seedPort,
depth: 1,
parent: null,
countOfCrampedPortPointsInPath: seedPort.cramped ? 1 : 0,
}
const existingCandidate = this.visitedExploredPortPoints.get(seedPort)
if (
!existingCandidate ||
this.getCandidateCost(initialCandidate) <
this.getCandidateCost(existingCandidate)
) {
this.visitedExploredPortPoints.set(seedPort, initialCandidate)
this.queue.push(initialCandidate)
}
}
}
override _step() {
if (this.queue.length === 0) {
this.currentExploredPortPoints = null
this.solved = true
return
}
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)
continue
}
const nextNodes = this.currentExploredPortPoints.port.nodeIds.map(
(nodeId) => {
const cmNode = this.input.mapOfCapacityMeshNodeIdToRef.get(nodeId)
if (!cmNode) {
throw new Error(
`Could not find capacity mesh node for id ${nodeId}`,
)
}
return cmNode
},
)
const nextPorts = nextNodes.flatMap(
(node) =>
this.input.mapOfCapacityMeshNodeIdToSegmentPortPoints.get(
node.capacityMeshNodeId,
) ?? [],
)
for (const nextPort of nextPorts) {
if (this.input.shouldIgnoreCrampedPortPoints && nextPort.cramped) {
continue
}
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.solved = true
}
private getCandidateCost(candidate: ExploredPortPoint): number {
return candidate.depth + candidate.countOfCrampedPortPointsInPath * 1000
}
getOutput() {
return this.resultExploredPortPoints
}
override visualize(): GraphicsObject {
const graphics: GraphicsObject = {
points: [],
rects: [],
}
for (const candidate of this.visitedExploredPortPoints.keys()) {
graphics.points!.push({
...candidate,
color: candidate.cramped ? "blue" : "green",
})
}
return graphics
}
}