Skip to content

Commit 87f3e17

Browse files
committed
aggressive speedup for capacity pathing solver using greedy multiplier
1 parent a093aab commit 87f3e17

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

lib/solvers/CapacityMeshSolver/CapacityMeshNodeSolver2_NodesUnderObstacles.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ export class CapacityMeshNodeSolver2_NodeUnderObstacle extends CapacityMeshNodeS
181181
}
182182

183183
const childNodes = this.getChildNodes(nextNode)
184-
console.log("childNodes", childNodes)
185184

186185
const finishedNewNodes: CapacityMeshNode[] = []
187186
const unfinishedNewNodes: CapacityMeshNode[] = []
@@ -192,11 +191,6 @@ export class CapacityMeshNodeSolver2_NodeUnderObstacle extends CapacityMeshNodeS
192191
childNode.availableZ.length > 1 &&
193192
!shouldBeXYSubdivided &&
194193
childNode._containsObstacle
195-
console.log(
196-
childNode.capacityMeshNodeId,
197-
shouldBeXYSubdivided,
198-
shouldBeZSubdivided,
199-
)
200194
if (shouldBeXYSubdivided) {
201195
unfinishedNewNodes.push(childNode)
202196
} else if (!shouldBeXYSubdivided && !childNode._containsObstacle) {

lib/solvers/CapacityPathingSolver/CapacityPathingSolver.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export class CapacityPathingSolver extends BaseSolver {
4444

4545
activeCandidateStraightLineDistance?: number
4646

47+
debug_lastNodeCostMap: Map<
48+
CapacityMeshNodeId,
49+
{
50+
g: number
51+
h: number
52+
f: number
53+
}
54+
>
55+
4756
hyperParameters: Partial<CapacityHyperParameters>
4857

4958
constructor({
@@ -82,6 +91,7 @@ export class CapacityPathingSolver extends BaseSolver {
8291
this.maxDepthOfNodes = Math.max(
8392
...this.nodes.map((node) => node._depth ?? 0),
8493
)
94+
this.debug_lastNodeCostMap = new Map()
8595
}
8696

8797
getTotalCapacity(node: CapacityMeshNode): number {
@@ -245,6 +255,7 @@ export class CapacityPathingSolver extends BaseSolver {
245255
const [start, end] = nextConnection.nodes
246256
if (!this.candidates) {
247257
this.candidates = [{ prevCandidate: null, node: start, f: 0, g: 0, h: 0 }]
258+
this.debug_lastNodeCostMap = new Map()
248259
this.visitedNodes = new Set([start.capacityMeshNodeId])
249260
this.activeCandidateStraightLineDistance = distance(
250261
start.center,
@@ -300,6 +311,13 @@ export class CapacityPathingSolver extends BaseSolver {
300311
const g = this.computeG(currentCandidate, neighborNode, end)
301312
const h = this.computeH(currentCandidate, neighborNode, end)
302313
const f = g + h * this.GREEDY_MULTIPLIER
314+
315+
this.debug_lastNodeCostMap.set(neighborNode.capacityMeshNodeId, {
316+
f,
317+
g,
318+
h,
319+
})
320+
303321
const newCandidate = {
304322
prevCandidate: currentCandidate,
305323
node: neighborNode,
@@ -339,9 +357,17 @@ export class CapacityPathingSolver extends BaseSolver {
339357
}
340358

341359
for (const node of this.nodes) {
360+
const nodeCosts = this.debug_lastNodeCostMap.get(node.capacityMeshNodeId)
342361
graphics.rects!.push({
343362
...createRectFromCapacityNode(node),
344-
label: `${node.capacityMeshNodeId}\n${this.usedNodeCapacityMap.get(node.capacityMeshNodeId)}/${this.getTotalCapacity(node).toFixed(2)}\n${node.width.toFixed(2)}x${node.height.toFixed(2)}`,
363+
label: [
364+
`${node.capacityMeshNodeId}`,
365+
`${this.usedNodeCapacityMap.get(node.capacityMeshNodeId)}/${this.getTotalCapacity(node).toFixed(2)}`,
366+
`${node.width.toFixed(2)}x${node.height.toFixed(2)}`,
367+
`g: ${nodeCosts?.g !== undefined ? nodeCosts.g.toFixed(2) : "?"}`,
368+
`h: ${nodeCosts?.h !== undefined ? nodeCosts.h.toFixed(2) : "?"}`,
369+
`f: ${nodeCosts?.f !== undefined ? nodeCosts.f.toFixed(2) : "?"}`,
370+
].join("\n"),
345371
})
346372
}
347373

lib/solvers/CapacityPathingSolver/CapacityPathingSolver5.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export class CapacityPathingSolver5 extends CapacityPathingSolver {
66
NEGATIVE_CAPACITY_PENALTY_FACTOR = 1
77
REDUCED_CAPACITY_PENALTY_FACTOR = 1
88

9+
constructor(...args: ConstructorParameters<typeof CapacityPathingSolver>) {
10+
super(...args)
11+
this.GREEDY_MULTIPLIER = 2.5
12+
}
13+
914
get maxCapacityFactor() {
1015
return this.hyperParameters.MAX_CAPACITY_FACTOR ?? 1
1116
}
@@ -18,6 +23,7 @@ export class CapacityPathingSolver5 extends CapacityPathingSolver {
1823
* Penalty you pay for using this node
1924
*/
2025
getNodeCapacityPenalty(node: CapacityMeshNode): number {
26+
return 0.05
2127
if (node.availableZ.length === 1) {
2228
return 0
2329
}
@@ -66,12 +72,15 @@ export class CapacityPathingSolver5 extends CapacityPathingSolver {
6672
const dx = A.center.x - B.center.x
6773
const dy = A.center.y - B.center.y
6874

69-
const szx = Math.max(A.width, B.width)
70-
const szy = Math.max(A.height, B.height)
75+
return Math.sqrt(dx ** 2 + dy ** 2)
76+
77+
// REWARD BIG NODE TRAVEL
78+
// const szx = Math.max(A.width, B.width)
79+
// const szy = Math.max(A.height, B.height)
7180

72-
const dist = Math.sqrt(dx ** 2 + dy ** 2) / (szx * szy)
81+
// const dist = Math.sqrt(dx ** 2 + dy ** 2) / (szx * szy)
7382

74-
return dist
83+
// return dist
7584
}
7685

7786
computeG(
@@ -91,9 +100,6 @@ export class CapacityPathingSolver5 extends CapacityPathingSolver {
91100
node: CapacityMeshNode,
92101
endGoal: CapacityMeshNode,
93102
) {
94-
return (
95-
this.getDistanceBetweenNodes(node, endGoal) +
96-
this.getNodeCapacityPenalty(node)
97-
)
103+
return this.getDistanceBetweenNodes(node, endGoal)
98104
}
99105
}

lib/solvers/SingleLayerNodeMerger/SingleLayerNodeMergerSolver.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export class SingleLayerNodeMergerSolver extends BaseSolver {
3939
}
4040
}
4141
nodeWithArea.sort((a, b) => a[1] - b[1])
42-
console.log(nodeWithArea)
4342
this.currentBatchNodeIds = nodeWithArea.map((n) => n[0])
4443
this.nextBatchNodeIds = []
4544
this.batchHadModifications = false

0 commit comments

Comments
 (0)