shahilsaha

Untitled

Jul 22nd, 2025
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | Gaming | 0 0
  1. public class PathfindingEvaluator : MonoBehaviour
  2. {
  3.     [SerializeField] private Grid3D mGrid;
  4.     [SerializeField] private PathfindingManager mPathManager;
  5.     private List<EvaluationResult> evaluationResults = new List<EvaluationResult>();
  6.  
  7.     public List<EvaluationResult> GetEvaluationResults() => evaluationResults;
  8.  
  9.     public EvaluationResult Evaluate(int evalSize)
  10.     {
  11.         // Pre evaluation
  12.         if (evalSize <= 0)
  13.         {
  14.             Debug.LogError("Evaluation size must be greater than 0.");
  15.             return null;
  16.         }
  17.  
  18.         // -- Clear data if exists.
  19.         ClearResults();
  20.  
  21.         var nodes = mGrid.GetStartEndNodes();
  22.  
  23.         // Evaluate the algorithms
  24.         StartEvaluation(evalSize, nodes.start, nodes.goal);
  25.  
  26.         // Post evaluation
  27.         if (evaluationResults.Count <= 0) return null;
  28.  
  29.         return evaluationResults[evalSize - 1];
  30.     }
  31.  
  32.     public void ClearResults()
  33.     {
  34.         if (evaluationResults != null && evaluationResults.Count > 0)
  35.             evaluationResults.Clear();
  36.     }
  37.  
  38.     private void StartEvaluation(int evalSize, Node start, Node goal)
  39.     {
  40.         int count = 0;
  41.  
  42.         while (count < evalSize)
  43.         {
  44.             var results = GatherEvaluationData(start, goal);
  45.             evaluationResults.Add(results);
  46.             count++;
  47.         }
  48.     }
  49.  
  50.     private EvaluationResult GatherEvaluationData(Node start, Node goal)
  51.     {
  52.         var aStar = EvaluationResult.FromPathResult(mPathManager.RunAStar(start, goal));
  53.         var ilsWithAStar = EvaluationResult.FromPathResult(mPathManager.RunILSWithAStar(start, goal));
  54.         var gbfs = EvaluationResult.FromPathResult(mPathManager.RunGBFS(start, goal));
  55.         var ilsWithGBFS = EvaluationResult.FromPathResult(mPathManager.RunILSWithGBFS(start, goal));
  56.  
  57.         return new EvaluationResult
  58.         {
  59.             AStar = aStar,
  60.             ILSWithAStar = ilsWithAStar,
  61.             GBFS = gbfs,
  62.             ILSWithGBFS = ilsWithGBFS,
  63.         };
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment