• C#,人工智能,机器人路径规划(Robotics Pathfinding)DStarLite(D* Lite Algorithm)优化算法与C#源程序


     文章与图片都是抄的,程序可是一脚一脚撸的。

    一、DStarLite算法摘要

    Dstar Lite是一种复杂的寻路算法,在机器人和游戏设计等许多领域都有实际应用。该算法能够解决最短路径问题,还集成了其他功能,如路径重新规划和路径校正。通过使用计算机模拟和分析该算法,我们测试了该算法的能力和处理速度,看看它是否可以成为高中机器人竞赛环境中的可行工具。最终,我们发现Dstar Lite的工作效率高于类似算法,因为它提供了一条从开始到结束的快速可靠的路径。我们建议机器人团队在自己的机器人代码中实现该算法,并将其用于自主运动。

    二、DStarLite算法介绍

    Dstar Lite是一种寻路算法,允许程序员在已知或部分已知的环境中找到起点和终点之间的最佳路径。该算法设计为在由边连接的节点组成的图数据结构上运行。指向探路器所在的当前位置的节点称为前置节点,而当前节点指向的节点称为其后续节点。

    Dstar Lite的工作原理是跟踪给定图形上每个节点的两个分数。第一个分数是G分数。该值跟踪到达图形上某个位置的成本。可以将其视为连接起点和当前点的最短路径。对于起点,G分数为零,因为探路者已经在那里了。另一方面,RHS分数是一个更有用的估计值。该分数作为一步向前看,在算法中,它用于找到探路者要前往的下一个最佳位置。如果下一个位置是障碍物,则连接成本将设置为无穷大,因为探路者永远无法到达该点。因此,RHS分数也是无穷大的。利用这两个分数,该算法将路径从终点传播到起点,最终找到从起点到终点的最优路径。

    1. Terminology:
    2. S = current node
    3. S’ = predecessor node
    4. C (s’, s) = edge cost for s’ ? s
    5. G(s) = current cost to arrive at that node
    6. G(s) = G(s’) + C (S’, S)
    7. RHS(s) = min value((G(S’) + C (S’, S))

    G分数和RHS分数作为Dstar Lite路径规划和重新规划的基础。如果Dstar Lite算法遇到不可预见的障碍并被迫重新规划路径,则只需使用所有受影响节点的新连接成本重新计算RHS分数。Koenig和Likhachev关于Dstar Lite算法的论文中有更详细的解释。

    该算法在工业上有许多实际应用。最著名的是,当蜂窝信号必须通过中继来自蜂窝塔网络的信号来找到从起点到目的地的最佳路径时,Dstar Lite用于电信行业。此外,该算法通常最适合机器人领域的路径规划。Dstar Lite允许在动态环境中快速重新种植。该功能对机器人很有用,因为在现实世界中,环境不断变化,机器人需要能够避开障碍物并快速重新规划其路径。

    在2002年开发Dstar Lite之前,其他算法(如Astar和Dijkstra算法)主导了路径搜索领域。Dstar Lite与这些路径优化算法主要有两个不同之处。首先,Dijkstra和Astar要求用户完全了解环境,因为它们不提供内置的重新规划功能。这种差异意味着,如果机器人遇到未知的东西,Astar和Dijkstra的算法将需要完全重新运行,以便创建新的路径。另一方面,Dstar Lite只快速重新计算受影响的区域,从而显著减少了必要的处理时间。当检测到节点是障碍物时,到达该节点的成本变为无穷大,因此RHS分数更新为无穷大。其次,Astar和Dijkstra的算法从生成一条从头到尾的路径开始。另一方面,Dstar Lite生成从目标到起点的路径。这种差异对于重新规划很有用,因为当需要重新生成路径时,向后遍历时需要的更改更少。

    在本文中,我们将讨论Dstar Lite如何成为机器人领域以及高中机器人比赛中使用的有效算法。我们相信,该算法的功能可以用于自主运动和辅助导航。

    三、DStarLite算法的计算结果和讨论

    我们使用模拟进行的第一个测试研究了运行时间如何随着网格大小的增加而变化。在我们的测试中,我们使用了网格长度和宽度,从10×10开始,然后慢慢增加大小,直到达到500×500。通过该测试,我们观察到,随着网格尺寸的增加,运行时间急剧增长。这是因为当网格的长度和宽度增加时,瓷砖的数量(等于长度乘以宽度)呈二次增长。当Dstar Lite穿过网格时,它必须检查与其直接相邻的8个磁贴。当总网格中有更多坐标时,Dstar Lite会整体检查更多的图块,因此运行时间会急剧增加。

    接下来,我们测试了场地上障碍物的数量如何影响运行时间。这个结果更令人惊讶,因为我们看到,随着网格上放置更多障碍,运行时间实际上减少了。我们认为,出现这种情况是因为网格上有更多障碍物减少了路径查找算法必须检查的点的数量。在我们的测试中,我们从10%的场地填充开始,慢慢地将充满障碍物的网格百分比增加到70%。我们注意到,随着障碍物的增加,运行时间不断减少。这一发现令人惊讶,因为我们认为更多的障碍意味着路径必须更加弯曲和不规则。相反,当存在大量障碍物时,Dstar Lite检查的点数会显著减少。

    最后,我们比较了Dstar-Lite和Astar的运行时间。当算法从头到尾遍历时随机引入障碍物时,我们发现Dstar Lite比Astar算法更快。由于Astar不支持重新规划,算法必须从当前点重新运行到终点,这意味着它必须做冗余工作。另一方面,Dstar Lite在这个过程中更有效,并使用过去的信息更快地重新规划。

    除了关于障碍物和场地大小的实验外,我们还测试了该算法是否能够进行路径校正。当机器人沿着一条路径行走,但由于摩擦和其他机械和物理错误,它偏离了规划的路径并最终到达其他地方时,需要进行路径校正。我们测试了Dstar-Lite算法是否可以用于重新规划路径并引导机器人返回正确的方向。我们通过在模拟机器人穿过网格时添加随机运动误差来测试这一点。每次机器人在路径中从一个瓷砖移动到下一个瓷砖时,机器人偏离正确位置并最终到达另一个相邻瓷砖的概率很小。发生这种情况时,我们使用Dstar Lite重新规划了路径,并继续沿着新生成的路径。在我们的实验中,我们看到这种行为与Dstar Lite在其路径被障碍物阻挡时的行为相同。有趣的是,当这种情况发生时,我们看到该算法要么生成一条到终点的全新路径,要么尝试修复自身并将机器人引导回原始路径。

    四、有关DStarLite算法的更多讨论

    Dstar Lite是一种广泛应用于机器人领域的算法。该路径规划算法可以通过里程计实现,并为真实机器人创建精确的运动轮廓功能。此外,该算法可以与传感器阵列结合使用,在机器人移动时动态避开障碍物。我们认为,该算法可以在竞争机器人中产生影响,因为该软件解决了许多问题,例如避障和精确的运动控制。目前,机器人团队在创建自主代码时面临的许多挑战可以通过将Dstar Lite算法应用到他们的软件中来解决。

    在我们的实验中,我们观察到在具有少量障碍物的开放网格上,Dstar Lite的效率低于在具有许多障碍物的拥挤网格上。我们认为这是因为在我们对Dstar Lite的改编中,当一个磁贴被障碍物占据时,Dstar Lite会俯瞰该点并继续前进。这意味着,如果网格有许多障碍,探路者将跳过更多的分片,算法的处理时间将减少。这种效果似乎是有益的,因为它可以用来降低必要的处理能力,并允许更快的路径生成。

    我们还发现,在每个测试期间,运行时通常有很多变化。这很可能是由测试计算机上运行的后台进程以及障碍物放置位置的可变性引起的。为了尽量减少这些影响,我们对每种情况进行了多次试验,然后平均运行时间以创建更准确的值。此外,每当在网格上放置随机障碍物时,它们产生障碍的可能性很小,因此在开始和结束之间没有可能的路径。如果出现这种情况,我们会重新设置所有障碍,然后重新进行试验。

    最后,由于我们使用具有离散瓦片的网格来跟踪机器人位置,因此Dstar Lite无法生成绝对最优路径。两点之间的绝对最优路径通常是一条从起点到终点完美环绕障碍物的直线。这样的网格有八个可能要遍历的相邻点,这将限制算法生成如下图所示的真正完美路径。

    可以使用视线算法解决此错误。视线算法不会仅将相邻图块视为后续图块,而是将具有直接视线的任何图块视为当前图块的后续图块。这将使生成的路径更直,调整更精细。同样,也可以通过提高图的分辨率来改善这个问题。在这种情况下,我们只需通过更多的行和列来扩大网格。这将提高路径的准确性,但运行时间也会增加。这两种方法都可以改善路径,但也意味着需要更多的处理能力和运行时间。

    五、结论

    在本文中,我们展示了Dstar Lite作为规划算法的有效性。我们比较了Dstar-Lite和Astar,发现前者的算法在重新规划时要快得多。我们认为,该算法在机器人领域非常有用,可以轻松地在高中机器人竞赛环境中实现。当前的高中比赛通常使用矩形网格的游戏场,在顶部放置已知和未知障碍物。Dstar Lite将能够避免这些障碍,并允许机器人快速找到最短路径。该算法不仅在创建路径方面非常有效,而且在路径重新规划和障碍回避方面比类似的算法(如Astar)更快。我们建议机器人在其自主代码中实现该算法。

    六、计算方法的实现

    虽然Dstar Lite最初设计为在具有特定节点的图上运行,但在我们的实验中,我们决定使用包含N行和N列的正方形网格。我们使用这种设置是因为网格可以很容易地对应于笛卡尔坐标系,并且可以在代码中表示为二维数组。这意味着(0,0)位置或网格上的原点对应于笛卡尔平面上的(0,0)位置(注意,在计算机渲染中,(0,0)点定义为左上角,下图显示了那里的原点)。此外,网格结构便于计算。在传统图中,需要隐式定义和存储每个节点之间的连接成本。对于具有许多节点和连接的图,这将快速累积并消耗内存。在我们的网格中,每个连接的成本可以自动定义为每个点之间的距离。这允许我们使用距离公式计算连接成本。类似地,需要隐式定义图的前导和后继。网格再一次优化了这个过程。网格上当前节点的前导节点和后继节点只是与其相邻的八个节点。

    在网格内,机器人在坐标(0,0)(字段左上角)处初始化,最终目标设置为(N-1,N-1)(网格右下角)。这个坐标被定义为(N-1,N-1),因为我们使用2D数组作为数据结构,数组从零开始。选择这两个点是为了确保路径尽可能长,因此在创建路径的过程中,探路者需要面对更多的障碍。

    最后,每次跑步前在场地上随机放置不同数量的障碍物。放置这些障碍物是为了模拟阻碍进入目标的障碍物和障碍物。此外,当机器人从起点移动到终点时,障碍物有10%的机会随机出现在机器人前面,阻塞其路径,并迫使算法重新调整路径。

    通过使用我们的模拟环境,我们进行了实验,以确定该算法是否足够有效,可以用于竞争机器人环境。在许多高中机器人比赛中,要求参赛者建造自主驱动的机器人,以在复杂环境中导航,完成某项任务。我们测试了该算法在这种情况下的使用情况。

    在整个实验过程中,我们研究了三个不同的因素:场大小的变化如何影响算法的运行时间,障碍物的数量如何影响运行时间,以及Dstar Lite算法与Astar算法相比如何再生最佳路径。我们使用Dstar Lite总共进行了84次试验,然后使用Astar进行了42次试验。对于每种不同的情况,我们进行了六次试验,然后取平均值。

    七、References

    (1) Choset, H. https://www.cs.cmu.edu/~motionplanning/lecture/AppH-astar-dstar_howie.pdf (accessed Jul 18, 2020).

    (2) Koenig, S.; Likhachev, M. D* Lite. http://idm-lab.org/bib/abstracts/papers/aaai02b.pdf (accessed Jul 18, 2020).

    (3) Buniyamin, N.; Sariff, N. An Overview of Autonomous Mobile Robot Path Planning Algorithms – IEEE Conference Publication. https://ieeexplore.ieee.org/abstract/document/4339335/authors#authors (accessed Jul 18, 2020).

    (4) Introduction to A*. http://theory.stanford.edu/~amitp/GameProgramming/AStarComparison.html (accessed Jul 19, 2020).

    (5) Konakalla, S. A Star Algorithm. http://cs.indstate.edu/~skonakalla/paper.pdf (accessed Jul 19, 2020).

    (6) Language Reference (API) \ Processing 3+. https://processing.org/reference/ (accessed Jul 19, 2020).

    (7) Kang, H.; Lee, B.; Kim, K. Path Planning Algorithm Using the Particle Swarm Optimization and the Improved Dijkstra Algorithm – IEEE Conference Publication. https://ieeexplore.ieee.org/abstract/document/4756927 (accessed Jul 19, 2020).

    八、DStarLite算法的C#源程序

    1、Point.cs

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace Legalsoft.Truffer
    5. {
    6. public class Point
    7. {
    8. public int x { get; set; } = 0;
    9. public int y { get; set; } = 0;
    10. public Point()
    11. {
    12. }
    13. public Point(int x, int y)
    14. {
    15. this.x = x;
    16. this.y = y;
    17. }
    18. }
    19. }

    2、K.cs

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace Legalsoft.Truffer
    5. {
    6. public class K
    7. {
    8. public double k1 { get; set; } = Double.PositiveInfinity;
    9. public double k2 { get; set; } = Double.PositiveInfinity;
    10. public K()
    11. {
    12. }
    13. public K(double K1, double K2)
    14. {
    15. k1 = K1;
    16. k2 = K2;
    17. }
    18. public int CompareTo(K that)
    19. {
    20. if (this.k1 < that.k1) return -1;
    21. else if (this.k1 > that.k1) return 1;
    22. if (this.k2 > that.k2) return 1;
    23. else if (this.k2 < that.k2) return -1;
    24. return 0;
    25. }
    26. public static bool operator <(K a, K b)
    27. {
    28. if (a.k1 < b.k1) return true;
    29. if (a.k2 < b.k2) return true;
    30. return false;
    31. }
    32. public static bool operator >(K a, K b)
    33. {
    34. if (a.k1 > b.k1) return true;
    35. if (a.k2 > b.k2) return true;
    36. return false;
    37. }
    38. public static bool operator ==(K a, K b)
    39. {
    40. return (Math.Abs(a.k1 - b.k1)<=float.Epsilon &&
    41. Math.Abs(a.k1 - b.k1)<=float.Epsilon);
    42. }
    43. public static bool operator !=(K a, K b)
    44. {
    45. return (Math.Abs(a.k1 - b.k1)>float.Epsilon ||
    46. Math.Abs(a.k1 - b.k1)>float.Epsilon);
    47. }
    48. public override bool Equals(object obj)
    49. {
    50. K a = (K)obj;
    51. return (this == a);
    52. }
    53. public override int GetHashCode()
    54. {
    55. return base.GetHashCode();
    56. }
    57. }
    58. }

    3、Heap.cs

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace Legalsoft.Truffer
    5. {
    6. public class Heap
    7. {
    8. private int n { get; set; }
    9. private Element[] heap;
    10. private Dictionary<Grid, int> hash { get; set; }
    11. public Heap(int cap)
    12. {
    13. n = 0;
    14. heap = new Element[cap];
    15. hash = new Dictionary<Grid, int>();
    16. }
    17. public K Peek()
    18. {
    19. if (n == 0)
    20. {
    21. return new K();
    22. }
    23. return heap[1].k;
    24. }
    25. public Grid Pop()
    26. {
    27. if (n == 0)
    28. {
    29. return null;
    30. }
    31. Grid s = heap[1].s;
    32. heap[1] = heap[n];
    33. hash[heap[1].s] = 1;
    34. hash[s] = 0;
    35. n--;
    36. Next(1);
    37. return s;
    38. }
    39. public void Insert(Grid s, K k)
    40. {
    41. Element e = new Element(s, k);
    42. n++;
    43. hash[s] = n;
    44. if (n == heap.Length)
    45. {
    46. Resize();
    47. }
    48. heap[n] = e;
    49. Last(n);
    50. }
    51. public void Update(Grid s, K k)
    52. {
    53. int i = hash[s];
    54. if (i == 0)
    55. {
    56. return;
    57. }
    58. K kold = heap[i].k;
    59. heap[i].k = k;
    60. if (kold.CompareTo(k) < 0)
    61. {
    62. Next(i);
    63. }
    64. else
    65. {
    66. Last(i);
    67. }
    68. }
    69. public void Remove(Grid s)
    70. {
    71. int i = hash[s];
    72. if (i == 0)
    73. {
    74. return;
    75. }
    76. hash[s] = 0;
    77. heap[i] = heap[n];
    78. hash[heap[i].s] = i;
    79. n--;
    80. Next(i);
    81. }
    82. public bool Contains(Grid s)
    83. {
    84. int i;
    85. if (!hash.TryGetValue(s, out i))
    86. {
    87. return false;
    88. }
    89. return (i != 0);
    90. }
    91. private void Next(int i)
    92. {
    93. int childL = i * 2;
    94. if (childL > n)
    95. {
    96. return;
    97. }
    98. int childR = i * 2 + 1;
    99. int smallerChild;
    100. if (childR > n)
    101. {
    102. smallerChild = childL;
    103. }
    104. else if (heap[childL].k.CompareTo(heap[childR].k) < 0)
    105. {
    106. smallerChild = childL;
    107. }
    108. else
    109. {
    110. smallerChild = childR;
    111. }
    112. if (heap[i].k.CompareTo(heap[smallerChild].k) > 0)
    113. {
    114. Swap(i, smallerChild);
    115. Next(smallerChild);
    116. }
    117. }
    118. private void Last(int i)
    119. {
    120. if (i == 1)
    121. {
    122. return;
    123. }
    124. int parent = i / 2;
    125. if (heap[parent].k.CompareTo(heap[i].k) > 0)
    126. {
    127. Swap(parent, i);
    128. Last(parent);
    129. }
    130. }
    131. private void Swap(int i, int j)
    132. {
    133. Element temp = heap[i];
    134. heap[i] = heap[j];
    135. hash[heap[j].s] = i;
    136. heap[j] = temp;
    137. hash[temp.s] = j;
    138. }
    139. private void Resize()
    140. {
    141. Array.Resize<Element>(ref heap, heap.Length * 2);
    142. }
    143. }
    144. }

    4、Grid.cs

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace Legalsoft.Truffer
    5. {
    6. public class Grid
    7. {
    8. public int x { get; set; } = 0;
    9. public int y { get; set; } = 0;
    10. public double g { get; set; } = Double.PositiveInfinity;
    11. public double rhs { get; set; } = Double.PositiveInfinity;
    12. public bool obstacle { get; set; } = false;
    13. public Grid(int x, int y)
    14. {
    15. this.x = x;
    16. this.y = y;
    17. }
    18. public override bool Equals(object that)
    19. {
    20. Grid a = that as Grid;
    21. return (this.x == a.x && this.y == a.y);
    22. }
    23. public static bool operator ==(Grid a, Grid b)
    24. {
    25. return a.Equals(b);
    26. }
    27. public static bool operator !=(Grid a, Grid b)
    28. {
    29. return !a.Equals(b);
    30. }
    31. public LinkedList<Grid> Succesors(Grid[,] map)
    32. {
    33. LinkedList<Grid> s = new LinkedList<Grid>();
    34. if ((x + 1) < map.GetLength(0))
    35. {
    36. s.AddFirst(map[x + 1, y]);
    37. }
    38. if ((y + 1) < map.GetLength(1))
    39. {
    40. s.AddFirst(map[x, y + 1]);
    41. }
    42. if ((x - 1) >= 0)
    43. {
    44. s.AddFirst(map[x - 1, y]);
    45. }
    46. if ((y - 1) >= 0)
    47. {
    48. s.AddFirst(map[x, y - 1]);
    49. }
    50. return s;
    51. }
    52. public LinkedList<Grid> Predecessors(Grid[,] map)
    53. {
    54. LinkedList<Grid> s = new LinkedList<Grid>();
    55. Grid tempState;
    56. if ((x + 1) < map.GetLength(0))
    57. {
    58. tempState = map[x + 1, y];
    59. if (tempState.obstacle == false)
    60. {
    61. s.AddFirst(tempState);
    62. }
    63. }
    64. if ((y + 1) < map.GetLength(1))
    65. {
    66. tempState = map[x, y + 1];
    67. if (tempState.obstacle == false)
    68. {
    69. s.AddFirst(tempState);
    70. }
    71. }
    72. if ((x - 1) >= 0)
    73. {
    74. tempState = map[x - 1, y];
    75. if (tempState.obstacle == false)
    76. {
    77. s.AddFirst(tempState);
    78. }
    79. }
    80. if ((y - 1) >= 0)
    81. {
    82. tempState = map[x, y - 1];
    83. if (tempState.obstacle == false)
    84. {
    85. s.AddFirst(tempState);
    86. }
    87. }
    88. return s;
    89. }
    90. public override int GetHashCode()
    91. {
    92. return base.GetHashCode();
    93. }
    94. }
    95. }

    5、Environment.cs

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace Legalsoft.Truffer
    5. {
    6. public interface DStarLiteEnvironment
    7. {
    8. void MoveTo(Point s);
    9. LinkedList<Point> GetObstaclesInVision();
    10. }
    11. }

    6、Element.cs

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace Legalsoft.Truffer
    5. {
    6. public class Element
    7. {
    8. public Grid s { get; set; }
    9. public K k { get; set; }
    10. public Element(Grid g, K k)
    11. {
    12. this.s = g;
    13. this.k = k;
    14. }
    15. }
    16. }

    7、DStarLite.cs

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace Legalsoft.Truffer
    5. {
    6. public class DStarLite
    7. {
    8. private int Row { get; set; }
    9. private int Column { get; set; }
    10. private Heap Path { get; set; }
    11. private Grid[,] Map { get; set; }
    12. private Grid Start { get; set; }
    13. private Grid Goal { get; set; }
    14. private double Factor { get; set; }
    15. public void Execute(int n, int m, int sx, int sy, int gx, int gy, DStarLiteEnvironment env)
    16. {
    17. Row = n;
    18. Column = m;
    19. Start = new Grid(sx, sy);
    20. Goal = new Grid(gx, gy);
    21. Grid slast = Start;
    22. Initialize();
    23. FindShortestPath();
    24. while (Start != Goal)
    25. {
    26. Start = FindMiniumGrid(Start);
    27. env.MoveTo(new Point(Start.x, Start.y));
    28. LinkedList<Point> obstacleCoord = env.GetObstaclesInVision();
    29. double old_factor = Factor;
    30. Grid oldslast = slast;
    31. Factor += Heuristic(Start, slast);
    32. slast = Start;
    33. bool change = false;
    34. foreach (Point c in obstacleCoord)
    35. {
    36. Grid s = Map[c.x, c.y];
    37. if (s.obstacle)
    38. {
    39. continue;
    40. }
    41. change = true;
    42. s.obstacle = true;
    43. foreach (Grid p in s.Predecessors(Map))
    44. {
    45. UpdatePath(p);
    46. }
    47. }
    48. if (change == false)
    49. {
    50. Factor = old_factor;
    51. slast = oldslast;
    52. }
    53. FindShortestPath();
    54. }
    55. }
    56. private K CalculateKey(Grid s)
    57. {
    58. return new K(Math.Min(s.g, s.rhs) + Heuristic(s, Start) + Factor,
    59. Math.Min(s.g, s.rhs));
    60. }
    61. private double Heuristic(Grid a, Grid b)
    62. {
    63. return Math.Abs(a.x - b.x) + Math.Abs(a.y - b.y);
    64. }
    65. private void Initialize()
    66. {
    67. Path = new Heap(Row * Column);
    68. Map = new Grid[Row, Column];
    69. Factor = 0.0;
    70. for (int i = 0; i < Row; i++)
    71. {
    72. for (int j = 0; j < Column; j++)
    73. {
    74. Map[i, j] = new Grid(i, j);
    75. Map[i, j].g = Double.PositiveInfinity;
    76. Map[i, j].rhs = Double.PositiveInfinity;
    77. }
    78. }
    79. Start = Map[Start.x, Start.y];
    80. Goal = Map[Goal.x, Goal.y];
    81. Goal.rhs = 0;
    82. Path.Insert(Goal, CalculateKey(Goal));
    83. }
    84. private void UpdatePath(Grid u)
    85. {
    86. if (u != Goal)
    87. {
    88. u.rhs = MiniumCost(u);
    89. }
    90. if (Path.Contains(u))
    91. {
    92. Path.Remove(u);
    93. }
    94. if (Math.Abs(u.g - u.rhs) > float.Epsilon)
    95. {
    96. Path.Insert(u, CalculateKey(u));
    97. }
    98. }
    99. private Grid FindMiniumGrid(Grid u)
    100. {
    101. double min = Double.PositiveInfinity;
    102. Grid n = null;
    103. foreach (Grid s in u.Succesors(Map))
    104. {
    105. double val = 1 + s.g;
    106. if (val <= min && s.obstacle == false)
    107. {
    108. min = val;
    109. n = s;
    110. }
    111. }
    112. return n;
    113. }
    114. private double MiniumCost(Grid u)
    115. {
    116. double min = Double.PositiveInfinity;
    117. foreach (Grid s in u.Succesors(Map))
    118. {
    119. double val = 1.0 + s.g;
    120. if (val < min && s.obstacle == false)
    121. {
    122. min = val;
    123. }
    124. }
    125. return min;
    126. }
    127. private void FindShortestPath()
    128. {
    129. while (Path.Peek().CompareTo(CalculateKey(Start)) < 0 ||
    130. Math.Abs(Start.rhs - Start.g) >= float.Epsilon)
    131. {
    132. K kold = Path.Peek();
    133. Grid u = Path.Pop();
    134. if (u == null)
    135. {
    136. break;
    137. }
    138. if (kold.CompareTo(CalculateKey(u)) < 0)
    139. {
    140. Path.Insert(u, CalculateKey(u));
    141. }
    142. else if (u.g > u.rhs)
    143. {
    144. u.g = u.rhs;
    145. foreach (Grid s in u.Predecessors(Map))
    146. {
    147. UpdatePath(s);
    148. }
    149. }
    150. else
    151. {
    152. u.g = Double.PositiveInfinity;
    153. UpdatePath(u);
    154. foreach (Grid s in u.Predecessors(Map))
    155. {
    156. UpdatePath(s);
    157. }
    158. }
    159. }
    160. }
    161. }
    162. }

    8、Drive.cs

    1. using System;
    2. using System.Collections.Generic;
    3. namespace Legalsoft.Truffer
    4. {
    5. public static class DStarLite_Drive
    6. {
    7. public static void Drive()
    8. {
    9. Test test = new Test();
    10. DStarLite dsl = new DStarLite();
    11. dsl.Execute(0, 1, 8, 6, test);
    12. }
    13. }
    14. }

    访问 深度混淆 的博客注意事项:写字是俺的弱项,请随意白嫖代码。

  • 相关阅读:
    HCIP(第十五天)
    JavaEE——Tomcat和servlet
    基于springboot的校园跑腿系统
    【Opencv4快速入门】轮廓检测findContours
    keepalived高可用学习 keepalived+nginx高可用负载均衡配置
    2023.09.10 学习周报
    Java医院绩效考核管理系统源码,设有手工录入功能(可以批量导入)
    使用yum install和reposync下载rpm安装包以及wget和curl下载文件
    R语言dplyr包基于嵌套的if_else语句对dataframe数据中的指定数据列进行编码处理
    异步复位的串联T触发器
  • 原文地址:https://blog.csdn.net/beijinghorn/article/details/125623296