Skip to content

Commit 0d7cada

Browse files
author
Peter Vaiko
committed
perf: pathfinder penalty cache
Once calculated for a grid cell, cache the penalty in the pathfinder. The penalty depends currently only on the position and not the heading, so this requires a limited amount of memory. This should reduce the calls for getNodePenalty() by an order of magnitude, but needs to be tested on a bigger field with difficult situations.
1 parent db46a22 commit 0d7cada

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

scripts/pathfinder/HybridAStarWithAStarInTheMiddle.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function DummyAStar:init(vehicle, path)
232232
end
233233

234234
function DummyAStar:run()
235-
return true, self.path
235+
return PathfinderResult(true, self.path)
236236
end
237237

238238
--- Similar to HybridAStarWithAStarInTheMiddle, but the middle section is not calculated using the A*, instead

scripts/pathfinder/PathfinderConstraints.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function PathfinderConstraints:init(context)
7676
self:resetCounts()
7777
local areaToAvoidText = self.areaToAvoid and
7878
string.format('are to avoid %.1f x %.1f m', self.areaToAvoid.length, self.areaToAvoid.width) or 'none'
79-
self.logger:debug('off field penalty %.1f, max fruit percent: %.1f, field number %d, %s, ignore fruit %s, ignore off-field penalty %s',
79+
self.logger:debug(self.vehicle, 'off field penalty %.1f, max fruit percent: %.1f, field number %d, %s, ignore fruit %s, ignore off-field penalty %s',
8080
self.offFieldPenalty, self.maxFruitPercent, self.fieldNum, areaToAvoidText,
8181
self.areaToIgnoreFruit or 'none', self.areaToIgnoreOffFieldPenalty or 'none')
8282
end
@@ -172,7 +172,7 @@ function PathfinderConstraints:isValidAnalyticSolutionNode(node, log)
172172
local analyticLimit = self.maxFruitPercent * 2
173173
if hasFruit and fruitValue > analyticLimit then
174174
if log then
175-
self.logger:debug('isValidAnalyticSolutionNode: fruitValue %.1f, max %.1f @ %.1f, %.1f',
175+
self.logger:debug(self.vehicle, 'isValidAnalyticSolutionNode: fruitValue %.1f, max %.1f @ %.1f, %.1f',
176176
fruitValue, analyticLimit, node.x, -node.y)
177177
end
178178
return false
@@ -239,10 +239,10 @@ function PathfinderConstraints:resetStrictMode()
239239
end
240240

241241
function PathfinderConstraints:showStatistics()
242-
self.logger:debug('Nodes: %d, Penalties: fruit: %d, off-field: %d, not owned field: %d, collisions: %d, trailer collisions: %d, area to avoid: %d, preferred path: %d',
242+
self.logger:debug(self.vehicle, 'Nodes: %d, Penalties: fruit: %d, off-field: %d, not owned field: %d, collisions: %d, trailer collisions: %d, area to avoid: %d, preferred path: %d',
243243
self.totalNodeCount, self.fruitPenaltyNodeCount, self.offFieldPenaltyNodeCount, self.notOwnedFieldPenaltyNodeCount,
244244
self.collisionNodeCount, self.trailerCollisionNodeCount, self.areaToAvoidPenaltyCount, self.preferredPathPenaltyCount)
245-
self.logger:debug(' max fruit %.1f %%, off-field penalty: %.1f',
245+
self.logger:debug(self.vehicle, ' max fruit %.1f %%, off-field penalty: %.1f',
246246
self.maxFruitPercent, self.offFieldPenalty)
247247
end
248248

scripts/pathfinder/PathfinderUtil.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,12 @@ function PathfinderUtil.findPathForTurn(vehicle, startOffset, goalReferenceNode,
477477
local _, y, _ = getWorldTranslation(vehicle:getAIDirectionNode())
478478
local dx, _, dz = worldToLocal(vehicle:getAIDirectionNode(), headlandPath[1].x, y, -headlandPath[1].y)
479479
local dirDeg = math.deg(math.abs(math.atan2(dx, dz)))
480-
PathfinderUtil.logger:debug('First headland waypoint isn\'t in front of us (%.1f), remove first few waypoints to avoid making a circle %.1f %.1f', dirDeg, dx, dz)
480+
PathfinderUtil.logger:debug(vehicle, 'First headland waypoint isn\'t in front of us (%.1f), remove first few waypoints to avoid making a circle %.1f %.1f', dirDeg, dx, dz)
481481
pathfinder = HybridAStarWithPathInTheMiddle(vehicle, 200, headlandPath, true, analyticSolver)
482482
end
483483
end
484484
if pathfinder == nil then
485-
PathfinderUtil.logger:debug('No headland, or there is a headland but wasn\'t able to get the shortest path on the headland to the next row, falling back to hybrid A*')
485+
PathfinderUtil.logger:debug(vehicle, 'No headland, or there is a headland but wasn\'t able to get the shortest path on the headland to the next row, falling back to hybrid A*')
486486
pathfinder = HybridAStarWithAStarInTheMiddle(vehicle, 200, 10000, true, analyticSolver)
487487
end
488488

0 commit comments

Comments
 (0)