コード例 #1
0
 public void DryRunAlgorithm()
 {
     if (DryRunAlgo)
     {
         RaycastHit     hit;
         List <Vector3> pathList = new List <Vector3>();
         if (Physics.Raycast(new Ray(transform.position + (transform.up * 0.2f), -1 * transform.up), out hit))
         {
             g_Grid = hit.collider.GetComponent <NavGrid>();
             int succeed = 0;
             while (succeed < DryRunTests)
             {
                 int x = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1)),
                     y = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.y - 1));
                 g_FromNode = g_Grid.GetGridNode(x, y);
                 if (g_FromNode != null)
                 {
                     NavNode to = null;
                     do
                     {
                         x  = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1));
                         y  = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1));
                         to = g_Grid.GetGridNode(x, y);
                     } while (to == null);
                     g_FromNode.SetHighlightTile(true, Color.black, 1f);
                     FindPath(g_FromNode.Position, to.Position);
                     succeed++;
                 }
             }
         }
         g_NPCController.Debug("Finished algorithm dry run");
     }
     DryRunAlgo = false;
     g_FromNode = null;
 }
コード例 #2
0
    public void Benchmark()
    {
        if (BenchmarkAll)
        {
            List <float> times = new List <float>();
            for (int r = 0; r < 5; r++)
            {
                float globalBenchMarkTime           = Time.realtimeSinceStartup;
                Dictionary <string, int>   hCounter = new Dictionary <string, int>();
                Dictionary <string, float> timeAvg  = new Dictionary <string, float>();
                RaycastHit     hit;
                List <Vector3> pathList = new List <Vector3>();
                if (Physics.Raycast(new Ray(transform.position + (transform.up * 0.2f), -1 * transform.up), out hit))
                {
                    g_Grid = hit.collider.GetComponent <NavGrid>();
                    int succeed = 0;
                    while (succeed < DryRunTests)
                    {
                        int x = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1)),
                            y = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.y - 1));
                        g_FromNode = g_Grid.GetGridNode(x, y);
                        if (g_FromNode != null)
                        {
                            NavNode to = null;

                            do
                            {
                                x  = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1));
                                y  = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1));
                                to = g_Grid.GetGridNode(x, y);
                            } while (to == null);

                            NavNode from = g_FromNode;

                            // benchmark each path
                            for (int i = 0; i < 10; ++i)
                            {
                                string log = "";
                                float  w2  = 1f;
                                if (i == 0)
                                {
                                    foreach (NavSeqAStar n in g_Pathfinders)
                                    {
                                        n.g_SecondaryHeuristicsWeight = w2;
                                    }
                                }
                                else if (i == 1)
                                {
                                    foreach (NavSeqAStar n in g_Pathfinders)
                                    {
                                        w2 += 0.25f;
                                        n.g_SecondaryHeuristicsWeight = w2;
                                    }
                                }
                                else if (i == 2)
                                {
                                    foreach (NavSeqAStar n in g_Pathfinders)
                                    {
                                        w2 += 0.5f;
                                        n.g_SecondaryHeuristicsWeight = w2;
                                    }
                                }
                                else if (i == 3)
                                {
                                    foreach (NavSeqAStar n in g_Pathfinders)
                                    {
                                        w2 += 0.75f;
                                        n.g_SecondaryHeuristicsWeight = w2;
                                    }
                                }
                                else
                                {
                                    foreach (NavSeqAStar n in g_Pathfinders)
                                    {
                                        w2 += 1f;
                                        n.g_SecondaryHeuristicsWeight = w2;
                                    }
                                }

                                float now = Time.realtimeSinceStartup;
                                from.SetHighlightTile(true, Color.black, 1f);
                                FindPath(from.Position, to.Position);
                                if (hCounter.ContainsKey(g_Winner))
                                {
                                    hCounter[g_Winner] += 1;
                                }
                                else
                                {
                                    hCounter.Add(g_Winner, 1);
                                }
                                log += "W2: " + w2 + "\n";
                                float t = (Time.realtimeSinceStartup - now);
                                log += "Time: " + t + "ms \n";
                                if (timeAvg.ContainsKey(g_Winner))
                                {
                                    timeAvg[g_Winner] += t;
                                }
                                else
                                {
                                    timeAvg.Add(g_Winner, t);
                                }
                                UnityEngine.Debug.Log(log);
                                g_CurrentPathValue = 0f;
                                g_PathLengthNodes  = 0;
                            }
                            foreach (string s in hCounter.Keys)
                            {
                                Debug.Log(s + " average time: " + timeAvg[s] / hCounter[s] + "\n");
                            }
                            succeed++;
                            timeAvg.Clear();
                            hCounter.Clear();
                        }
                    }
                }
                times.Add((Time.realtimeSinceStartup - globalBenchMarkTime));
            }
            foreach (float f in times)
            {
                Debug.Log(f);
            }
        }
        DryRunAlgo = false;
        g_FromNode = null;
    }