-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract common search logic to base classes
- Loading branch information
Showing
7 changed files
with
166 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 14 additions & 113 deletions
127
BrainAI/Pathfinding/BreadthFirst/BreadthFirstPathfinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,149 +1,50 @@ | ||
namespace BrainAI.Pathfinding | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// Calculates paths given an IUnweightedGraph and start/goal positions | ||
/// </summary> | ||
public class BreadthFirstPathfinder<T> : IPathfinder<T>, ICoveragePathfinder<T> | ||
public class BreadthFirstPathfinder<T> : CoveragePathfinder<T> | ||
{ | ||
public Dictionary<T, T> VisitedNodes { get; } = new Dictionary<T, T>(); | ||
|
||
public List<T> ResultPath { get; set; } = new List<T>(); | ||
|
||
private readonly HashSet<T> tmpGoals = new HashSet<T>(); | ||
|
||
private T searchStart; | ||
|
||
private readonly IUnweightedGraph<T> graph; | ||
|
||
private readonly Queue<T> frontier = new Queue<T>(); | ||
|
||
private readonly List<T> neighbours = new List<T>(); | ||
|
||
public BreadthFirstPathfinder(IUnweightedGraph<T> graph) | ||
{ | ||
this.graph = graph; | ||
} | ||
|
||
public void Search(T start, T goal) | ||
{ | ||
this.PrepareSearch(); | ||
this.StartNewSearch(start); | ||
|
||
tmpGoals.Add(goal); | ||
|
||
ContinueSearch(); | ||
} | ||
|
||
public void Search(T start, HashSet<T> goals) | ||
{ | ||
this.PrepareSearch(); | ||
this.StartNewSearch(start); | ||
|
||
foreach (var goal in goals) | ||
{ | ||
this.tmpGoals.Add(goal); | ||
} | ||
|
||
ContinueSearch(); | ||
} | ||
|
||
public void Search(T start, int additionalDepth) | ||
{ | ||
this.PrepareSearch(); | ||
this.StartNewSearch(start); | ||
|
||
InternalSearch(additionalDepth); | ||
} | ||
|
||
public void Search(T start, T goal, int additionalDepth) | ||
{ | ||
this.PrepareSearch(); | ||
this.StartNewSearch(start); | ||
|
||
this.tmpGoals.Add(goal); | ||
|
||
ContinueSearch(additionalDepth); | ||
} | ||
|
||
public void Search(T start, HashSet<T> goals, int additionalDepth) | ||
{ | ||
this.PrepareSearch(); | ||
this.StartNewSearch(start); | ||
|
||
foreach (var goal in goals) | ||
{ | ||
this.tmpGoals.Add(goal); | ||
} | ||
|
||
ContinueSearch(additionalDepth); | ||
} | ||
|
||
public void ContinueSearch() | ||
{ | ||
this.ResultPath.Clear(); | ||
if (tmpGoals.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
ContinueSearch(int.MaxValue); | ||
} | ||
|
||
public void ContinueSearch(int additionalDepth) | ||
{ | ||
this.ResultPath.Clear(); | ||
var (target, isFound) = InternalSearch(additionalDepth); | ||
if (isFound) | ||
{ | ||
PathConstructor.RecontructPath(VisitedNodes, searchStart, target, this.ResultPath); | ||
} | ||
} | ||
|
||
private ValueTuple<T, bool> InternalSearch(int additionalDepth) | ||
internal override ValueTuple<T, bool> InternalSearch(int additionalDepth) | ||
{ | ||
while (frontier.Count > 0 && additionalDepth > 0) | ||
{ | ||
additionalDepth--; | ||
var current = frontier.Peek(); | ||
|
||
if (tmpGoals.Contains(current)) | ||
if (tmpGoals.Contains(current.Item2)) | ||
{ | ||
tmpGoals.Remove(current); | ||
return (current, true); | ||
tmpGoals.Remove(current.Item2); | ||
return (current.Item2, true); | ||
} | ||
|
||
frontier.Dequeue(); | ||
graph.GetNeighbors(current, neighbours); | ||
|
||
graph.GetNeighbors(current.Item2, neighbours); | ||
|
||
foreach (var next in neighbours) | ||
{ | ||
if (!VisitedNodes.ContainsKey(next)) | ||
var newCost = costSoFar[current.Item2] + 1; | ||
if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next]) | ||
{ | ||
frontier.Enqueue(next); | ||
VisitedNodes.Add(next, current); | ||
costSoFar[next] = newCost; | ||
var priority = newCost; | ||
frontier.Enqueue(new ValueTuple<int, T>(priority, next), priority); | ||
VisitedNodes[next] = current.Item2; | ||
} | ||
} | ||
} | ||
|
||
return (default(T), false); | ||
} | ||
|
||
private void PrepareSearch() | ||
{ | ||
this.frontier.Clear(); | ||
this.VisitedNodes.Clear(); | ||
this.tmpGoals.Clear(); | ||
} | ||
|
||
private void StartNewSearch(T start) | ||
{ | ||
this.searchStart = start; | ||
this.frontier.Enqueue(start); | ||
this.VisitedNodes.Add(start, start); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace BrainAI.Pathfinding | ||
{ | ||
public abstract class CoveragePathfinder<T> : Pathfinder<T>, ICoveragePathfinder<T> | ||
{ | ||
public void Search(T start, int additionalDepth) | ||
{ | ||
this.PrepareSearch(); | ||
this.StartNewSearch(start); | ||
|
||
InternalSearch(additionalDepth); | ||
} | ||
} | ||
} |
Oops, something went wrong.