condor-pathfinding-grid 0.4.0

Grid pathfinding, preprocessing, replanning, and multi-agent algorithms for Condor.
Documentation
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Dynamic-grid [`GridReplanner`]: Lifelong Planning A* (LPA*).
//!
//! It owns a grid snapshot and request after `initialize`, marks changes through
//! `update_cell` / `update_cost`, then resumes its priority queue at `replan`. Results
//! use the standard discrete invalid/found/no-path contract with per-cell
//! [`traversal_cost`](crate::Grid::traversal_cost), Manhattan keys, and one-step
//! lookahead. Prefer the product-facing
//! [`crate::algorithms::d_star_lite::DStarLite`] entrypoint unless direct LPA*
//! identity is required.
//!
//! Implementation notes: the open set is a **lazy** binary heap (stale keys skipped on pop).
//! Consistent vertices (`g == rhs`) must not re-enter the underconsistent branch. Incomplete
//! compute (iteration cap) forces a cold `initialize` on replan—paths are never reconstructed
//! from a non-converged state.

use std::cmp::Ordering;
use std::collections::{BTreeSet, BinaryHeap};

use crate::{
    grid::{Cell, Grid, GridEditError},
    path::Path,
    point::Point,
    replanning::GridReplanner,
    search::{SearchRequest, SearchResult},
};

/// Incremental LPA* [`GridReplanner`] for dynamic weighted grids.
///
/// Owns a grid copy and request; updates mark vertices, `replan` resumes the lazy
/// heap. Backing engine for [`super::d_star_lite::DStarLite`]. Incomplete compute
/// (iteration cap) forces a cold re-initialize — never reconstructs from non-converged state.
pub struct LifelongPlanningAStar {
    initialized: bool,
    grid: Grid,
    request: SearchRequest,
    /// g-values as f64 for LPA* keys (typical grid costs stay exact below 2^53).
    g_costs: Vec<f64>,
    rhs_costs: Vec<f64>,
    queue: BinaryHeap<PriorityEntry>,
    visited_nodes: usize,
}

/// Result of the main LPA* compute loop.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ComputeOutcome {
    /// Queue drained or top-key ≥ goal-key with consistent goal.
    Converged,
    /// Safety cap hit; search is incomplete (must not return soft success).
    HitIterationCap,
}

impl Default for LifelongPlanningAStar {
    fn default() -> Self {
        Self::new()
    }
}

impl LifelongPlanningAStar {
    /// Creates an uninitialized replanner; call [`GridReplanner::initialize`] before replan.
    #[must_use]
    pub fn new() -> Self {
        Self {
            initialized: false,
            grid: Grid::new(1, 1).expect("grid dimensions are valid"),
            request: SearchRequest::new(Point::new(0, 0), Point::new(0, 0)),
            g_costs: Vec::new(),
            rhs_costs: Vec::new(),
            queue: BinaryHeap::new(),
            visited_nodes: 0,
        }
    }

    fn calculate_key(&self, p: Point) -> [f64; 2] {
        let idx = self.grid.index_of(p).unwrap();
        let g = self.g_costs[idx];
        let rhs = self.rhs_costs[idx];
        let min = g.min(rhs);
        [min + self.heuristic(p), min]
    }

    fn heuristic(&self, p: Point) -> f64 {
        (p.x as f64 - self.request.goal.x as f64).abs()
            + (p.y as f64 - self.request.goal.y as f64).abs()
    }

    fn search_state_is_ready(&self) -> bool {
        let cell_count = self.grid.cell_count();
        self.g_costs.len() == cell_count && self.rhs_costs.len() == cell_count
    }

    fn update_vertex(&mut self, p: Point) {
        if !self.search_state_is_ready() {
            return;
        }
        let Some(idx) = self.grid.index_of(p) else {
            return;
        };
        if !self.grid.is_walkable(p) {
            self.rhs_costs[idx] = f64::INFINITY;
        } else if p == self.request.start {
            self.rhs_costs[idx] = 0.0;
        } else {
            let mut min_rhs = f64::INFINITY;
            let cost = self
                .grid
                .traversal_cost(p)
                .expect("walkable point has cost") as f64;
            for pred in self.grid.neighbors4(p) {
                let pred_idx = self.grid.index_of(pred).unwrap();
                min_rhs = min_rhs.min(self.g_costs[pred_idx] + cost);
            }
            self.rhs_costs[idx] = min_rhs;
        }

        if self.g_costs[idx] != self.rhs_costs[idx] {
            self.queue.push(PriorityEntry {
                point: p,
                key: self.calculate_key(p),
            });
        }
    }

    fn search_converged(&self) -> bool {
        if !self.search_state_is_ready() {
            return false;
        }
        let Some(goal_idx) = self.grid.index_of(self.request.goal) else {
            return false;
        };
        if self.g_costs[goal_idx] != self.rhs_costs[goal_idx] {
            return false;
        }
        match self.queue.peek() {
            None => true,
            Some(top) => top.key >= self.calculate_key(self.request.goal),
        }
    }

    fn compute_shortest_path(&mut self) -> ComputeOutcome {
        if !self.search_state_is_ready() {
            return ComputeOutcome::Converged;
        }
        let Some(goal_idx) = self.grid.index_of(self.request.goal) else {
            return ComputeOutcome::Converged;
        };

        let max_iterations = self.grid.cell_count().saturating_mul(64).max(4_096);
        let mut iterations = 0usize;

        while let Some(top) = self.queue.peek() {
            let top_key = top.key;
            let goal_key = self.calculate_key(self.request.goal);

            if top_key >= goal_key && self.rhs_costs[goal_idx] == self.g_costs[goal_idx] {
                return ComputeOutcome::Converged;
            }

            let entry = self.queue.pop().unwrap();
            let u = entry.point;
            let u_idx = self.grid.index_of(u).unwrap();

            if entry.key != self.calculate_key(u) {
                continue;
            }
            if self.g_costs[u_idx] == self.rhs_costs[u_idx] {
                continue;
            }

            iterations += 1;
            if iterations > max_iterations {
                return ComputeOutcome::HitIterationCap;
            }

            self.visited_nodes += 1;

            if self.g_costs[u_idx] > self.rhs_costs[u_idx] {
                // Overconsistent: g ← rhs, then refresh successors.
                self.g_costs[u_idx] = self.rhs_costs[u_idx];
                for s in self.grid.neighbors4(u) {
                    self.update_vertex(s);
                }
            } else {
                // Underconsistent: g ← ∞, then re-evaluate u and successors.
                self.g_costs[u_idx] = f64::INFINITY;
                self.update_vertex(u);
                for s in self.grid.neighbors4(u) {
                    self.update_vertex(s);
                }
            }
        }

        ComputeOutcome::Converged
    }

    fn finish_search(&mut self, outcome: ComputeOutcome) -> SearchResult {
        if !self.initialized {
            return crate::search::not_found(self.visited_nodes);
        }
        crate::search::validate_request(&self.grid, self.request)?;
        let Some(goal_idx) = self.grid.index_of(self.request.goal) else {
            unreachable!("validated goal has a grid index");
        };
        if !self.search_state_is_ready() {
            return crate::search::not_found(self.visited_nodes);
        }

        if outcome == ComputeOutcome::HitIterationCap || !self.search_converged() {
            return crate::search::not_found(self.visited_nodes);
        }

        if self.g_costs[goal_idx] == f64::INFINITY {
            return crate::search::not_found(self.visited_nodes);
        }

        let Some(path) = self.reconstruct_path() else {
            return crate::search::not_found(self.visited_nodes);
        };

        crate::search::found(path, self.visited_nodes)
    }

    fn reconstruct_path(&self) -> Option<Path> {
        if !self.search_state_is_ready() {
            return None;
        }
        if !self.grid.is_walkable(self.request.start) || !self.grid.is_walkable(self.request.goal) {
            return None;
        }

        let mut steps = Vec::new();
        let mut current = self.request.goal;
        steps.push(current);
        let mut seen = BTreeSet::from([current]);

        let mut total_cost: usize = 0;

        while current != self.request.start {
            let mut best_neighbor = None;
            let mut min_candidate_cost = f64::INFINITY;
            let step_cost_raw = self.grid.traversal_cost(current)?;
            let step_cost = step_cost_raw as f64;

            for neighbor in self.grid.neighbors4(current) {
                let neighbor_idx = self.grid.index_of(neighbor).unwrap();
                let candidate_cost = self.g_costs[neighbor_idx] + step_cost;
                if candidate_cost < min_candidate_cost {
                    min_candidate_cost = candidate_cost;
                    best_neighbor = Some(neighbor);
                }
            }

            let next = best_neighbor?;
            if !seen.insert(next) {
                return None;
            }
            total_cost = total_cost.saturating_add(step_cost_raw);
            current = next;
            steps.push(current);
        }
        steps.reverse();
        Some(
            Path::from_steps_with_cost(steps, total_cost)
                .expect("path contains at least one point"),
        )
    }
}

impl GridReplanner for LifelongPlanningAStar {
    fn name(&self) -> &'static str {
        "lpa-star"
    }

    fn initialize(&mut self, grid: &Grid, request: SearchRequest) -> SearchResult {
        crate::search::validate_request(grid, request)?;
        self.initialized = true;
        self.grid = grid.clone();
        self.request = request;
        self.g_costs.clear();
        self.rhs_costs.clear();
        self.queue.clear();
        self.visited_nodes = 0;

        let start_idx = self
            .grid
            .index_of(request.start)
            .expect("validated start has a grid index");
        let n = self.grid.cell_count();
        self.g_costs = vec![f64::INFINITY; n];
        self.rhs_costs = vec![f64::INFINITY; n];

        self.rhs_costs[start_idx] = 0.0;
        if request.start == request.goal {
            self.g_costs[start_idx] = 0.0;
            return crate::search::found(
                Path::from_steps(vec![request.start]).expect("path contains at least one point"),
                1,
            );
        }

        self.queue.push(PriorityEntry {
            point: request.start,
            key: self.calculate_key(request.start),
        });

        let outcome = self.compute_shortest_path();
        self.finish_search(outcome)
    }

    fn update_cell(&mut self, point: Point, cell: Cell) {
        if self.grid.set_cell(point, cell).is_err() {
            return;
        }
        self.update_vertex(point);
        for neighbor in self.grid.neighbors4(point) {
            self.update_vertex(neighbor);
        }
    }

    fn update_cost(&mut self, point: Point, cost: usize) -> Result<(), GridEditError> {
        self.grid.set_traversal_cost(point, cost)?;
        self.update_vertex(point);
        for neighbor in self.grid.neighbors4(point) {
            self.update_vertex(neighbor);
        }
        Ok(())
    }

    fn replan(&mut self) -> SearchResult {
        if !self.initialized {
            return crate::search::not_found(0);
        }
        self.visited_nodes = 0;
        let outcome = self.compute_shortest_path();
        let result = self.finish_search(outcome);
        if result.as_ref().is_ok_and(|outcome| outcome.is_found()) {
            return result;
        }

        let grid = self.grid.clone();
        let request = self.request;
        self.initialize(&grid, request)
    }
}

#[derive(Debug)]
struct PriorityEntry {
    point: Point,
    key: [f64; 2],
}

impl PartialEq for PriorityEntry {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
    }
}

impl Eq for PriorityEntry {}

impl Ord for PriorityEntry {
    fn cmp(&self, other: &Self) -> Ordering {
        for i in 0..2 {
            if self.key[i] < other.key[i] {
                return Ordering::Greater;
            }
            if self.key[i] > other.key[i] {
                return Ordering::Less;
            }
        }
        Ordering::Equal
    }
}

impl PartialOrd for PriorityEntry {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[cfg(test)]
mod tests {
    use super::LifelongPlanningAStar;
    use crate::{Cell, Grid, GridSearchError, Point, SearchRequest, replanning::GridReplanner};

    #[test]
    fn blocking_the_only_bridge_cell_removes_the_path() {
        let grid = Grid::new(3, 1).expect("grid dimensions are valid");
        let request = SearchRequest::new(Point::new(0, 0), Point::new(2, 0));
        let mut replanner = LifelongPlanningAStar::new();

        let initial = replanner.initialize(&grid, request);
        assert!(initial.as_ref().expect("valid search request").is_found());
        assert_eq!(
            initial.as_ref().expect("valid search request").cost(),
            Some(2)
        );

        replanner.update_cell(Point::new(1, 0), Cell::Blocked);
        let repaired = replanner.replan();
        assert!(!repaired.as_ref().expect("valid search request").is_found());
    }

    #[test]
    fn blocking_the_goal_removes_the_path() {
        let grid = Grid::new(3, 1).expect("grid dimensions are valid");
        let request = SearchRequest::new(Point::new(0, 0), Point::new(2, 0));
        let mut replanner = LifelongPlanningAStar::new();

        let initial = replanner.initialize(&grid, request);
        assert!(initial.as_ref().expect("valid search request").is_found());

        replanner.update_cell(Point::new(2, 0), Cell::Blocked);
        let repaired = replanner.replan();
        assert_eq!(
            repaired,
            Err(GridSearchError::InvalidGoal {
                point: Point::new(2, 0),
            })
        );
    }

    #[test]
    fn reopening_the_start_restores_the_path() {
        let grid = Grid::new(3, 1).expect("grid dimensions are valid");
        let request = SearchRequest::new(Point::new(0, 0), Point::new(2, 0));
        let mut replanner = LifelongPlanningAStar::new();

        let initial = replanner.initialize(&grid, request);
        assert!(initial.as_ref().expect("valid search request").is_found());

        replanner.update_cell(Point::new(0, 0), Cell::Blocked);
        let blocked = replanner.replan();
        assert_eq!(
            blocked,
            Err(GridSearchError::InvalidStart {
                point: Point::new(0, 0),
            })
        );

        replanner.update_cell(Point::new(0, 0), Cell::Open);
        let reopened = replanner.replan();
        assert!(reopened.as_ref().expect("valid search request").is_found());
        assert_eq!(
            reopened.as_ref().expect("valid search request").cost(),
            Some(2)
        );
    }

    #[test]
    fn unblocking_a_start_blocked_at_init_recovers_via_update_then_replan() {
        let mut grid = Grid::new(3, 1).expect("grid dimensions are valid");
        grid.set_cell(Point::new(0, 0), Cell::Blocked)
            .expect("valid grid edit");
        let request = SearchRequest::new(Point::new(0, 0), Point::new(2, 0));
        let mut replanner = LifelongPlanningAStar::new();

        let initial = replanner.initialize(&grid, request);
        assert_eq!(
            initial,
            Err(GridSearchError::InvalidStart {
                point: Point::new(0, 0),
            })
        );

        grid.set_cell(Point::new(0, 0), Cell::Open)
            .expect("valid grid edit");
        let recovered = replanner.initialize(&grid, request);
        assert!(recovered.as_ref().expect("valid search request").is_found());
        assert_eq!(
            recovered.as_ref().expect("valid search request").cost(),
            Some(2)
        );
    }

    #[test]
    fn unblocking_a_goal_blocked_at_init_recovers_via_update_then_replan() {
        let mut grid = Grid::new(3, 1).expect("grid dimensions are valid");
        grid.set_cell(Point::new(2, 0), Cell::Blocked)
            .expect("valid grid edit");
        let request = SearchRequest::new(Point::new(0, 0), Point::new(2, 0));
        let mut replanner = LifelongPlanningAStar::new();

        let initial = replanner.initialize(&grid, request);
        assert_eq!(
            initial,
            Err(GridSearchError::InvalidGoal {
                point: Point::new(2, 0),
            })
        );

        grid.set_cell(Point::new(2, 0), Cell::Open)
            .expect("valid grid edit");
        let recovered = replanner.initialize(&grid, request);
        assert!(recovered.as_ref().expect("valid search request").is_found());
        assert_eq!(
            recovered.as_ref().expect("valid search request").cost(),
            Some(2)
        );
    }

    #[test]
    fn reports_saturated_cost_on_large_traversal_cost() {
        let mut grid = Grid::new(3, 1).expect("grid dimensions are valid");
        assert_eq!(
            grid.set_traversal_cost(Point::new(1, 0), usize::MAX),
            Ok(())
        );

        let request = SearchRequest::new(Point::new(0, 0), Point::new(2, 0));
        let mut replanner = LifelongPlanningAStar::new();

        let initial = replanner.initialize(&grid, request);
        assert!(initial.as_ref().expect("valid search request").is_found());
        assert_eq!(
            initial.as_ref().expect("valid search request").cost(),
            Some(usize::MAX)
        );
        assert_eq!(
            initial
                .as_ref()
                .expect("valid search request")
                .path()
                .expect("path should exist")
                .cost(),
            usize::MAX
        );
    }

    #[test]
    fn cold_init_open_field_with_mid_wall_matches_astar() {
        use crate::{AStar, Pathfinder};

        let mut grid = Grid::new(24, 24).expect("grid");
        for k in 0..3 {
            grid.set_cell(Point::new(12, 8 + k), Cell::Blocked)
                .expect("valid grid edit");
        }
        let request = SearchRequest::new(Point::new(2, 9), Point::new(20, 9));
        let mut lpa = LifelongPlanningAStar::new();
        let cold = lpa.initialize(&grid, request);
        let astar = AStar.search(&grid, request);
        assert!(
            cold.as_ref().expect("valid search request").is_found()
                && astar.as_ref().expect("valid search request").is_found()
        );
        assert_eq!(
            cold.as_ref().expect("valid search request").cost(),
            astar.as_ref().expect("valid search request").cost()
        );
        let cap = grid.cell_count().saturating_mul(64);
        assert!(
            cold.as_ref()
                .expect("valid search request")
                .stats()
                .visited_nodes
                < cap,
            "must not thrash to iteration cap (visited={})",
            cold.as_ref()
                .expect("valid search request")
                .stats()
                .visited_nodes
        );
    }

    #[test]
    fn incremental_replan_after_local_wall_matches_astar() {
        use crate::{AStar, Pathfinder};

        let mut grid = Grid::new(32, 32).expect("grid");
        let request = SearchRequest::new(Point::new(2, 16), Point::new(28, 16));
        let mut lpa = LifelongPlanningAStar::new();
        let init = lpa.initialize(&grid, request);
        assert!(init.as_ref().expect("valid search request").is_found());
        assert_eq!(
            init.as_ref().expect("valid search request").cost(),
            AStar
                .search(&grid, request)
                .as_ref()
                .expect("valid search request")
                .cost()
        );

        for k in 0..5 {
            let p = Point::new(16, 14 + k);
            grid.set_cell(p, Cell::Blocked).expect("valid grid edit");
            lpa.update_cell(p, Cell::Blocked);
        }
        let repaired = lpa.replan();
        let restart = AStar.search(&grid, request);
        assert!(
            repaired.as_ref().expect("valid search request").is_found()
                && restart.as_ref().expect("valid search request").is_found()
        );
        assert_eq!(
            repaired.as_ref().expect("valid search request").cost(),
            restart.as_ref().expect("valid search request").cost()
        );
        let cap = grid.cell_count().saturating_mul(64);
        assert!(
            repaired
                .as_ref()
                .expect("valid search request")
                .stats()
                .visited_nodes
                < cap
        );
    }
}