我目前正在进行A*寻路,但我遇到了一些问题.在走最好的路径之前,它走错路.我究竟做错了什么?
源代码:http://basic.apayostudios.com/AStar.zip
线上:
Game.cs http://pastie.org/1656955
Node.cs http://pastie.org/1656956
枚举:
public enum NodeType
{
None,
Solid,
Start,
End
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在将用于Google AI挑战的机器人从Python重写为C++,我想使用boost的图形库来处理路径查找,而不是像以前在Python中那样滚动我自己的图形和搜索代码.
地图是一个简单的方形网格,包裹着边缘.
我之前没有使用过boost或C++(我非常了解C)而且我发现boost图文档真的很难理解,所以我需要一些帮助.
我遇到问题的具体文档:
这是工作python代码的片段:
def do_turn(self, ants):
# update path-finding graph
for row in range(ants.rows):
for col in range(ants.cols):
key = (row, col)
self.graph[key] = []
def append_if_unoccupied(coord):
if ants.passable(coord) and coord not in ants.my_hills():
self.graph[key].append(coord)
if row - 1 >= 0:
append_if_unoccupied((row - 1, col))
else:
append_if_unoccupied((ants.rows + (row - 1), col))
if col - 1 >= 0:
append_if_unoccupied((row, col - 1))
else:
append_if_unoccupied((row, ants.cols + (col - 1)))
if row + 1 < …Run Code Online (Sandbox Code Playgroud) 我正在准备在有障碍物的地形中寻找最佳路径的算法。到目前为止,我实现了 Dijsktra 和 A* 算法。现在我必须实现遗传算法,但我遇到了问题。
首先,我将向您展示我的地图表示形式的外观。有7种不同的地形(0-起点,7-终点,1-4正常可以通过,5-6不能通过)。下面是 Python 中的代码(在我看来,代码中最重要的部分是理解问题的函数neighbors):
class Graph():
def __init__(self, x=10, y=10):
self.width = x
self.height = y
self.board = ((1, 1, 1, 5, 1, 1, 1, 1, 1, 7),
(1, 1, 1, 5, 1, 1, 1, 1, 1, 1),
(1, 1, 1, 5, 1, 5, 1, 1, 1, 1),
(0, 1, 1, 1, 1, 5, 1, 1, 1, 1),
(1, 1, 1, 1, 1, 5, 1, 1, 1, 1),
(1, 1, 1, 1, 1, 1, …Run Code Online (Sandbox Code Playgroud) 我有一个基本的寻路程序,我希望矩形跟随鼠标移动。我正在使用这种特定的寻路方法,因为我想在将来对其进行扩展。我有2种方法:一种获取2个矩形之间的角度,另一种以x角度移动矩形。出于某种原因,该矩形仅在其前面时跟随我的鼠标,而不是在它后面时。
如果我不再Math.abs使用我的角度方法,问题仍然存在。
以下是我的两个基本课程。
PathfindingTest.java,这是主类,它初始化所有内容:
package pathfindingtest;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
*
* @author Preston Tang
*/
public class PathfindingTest extends Application {
private final long[] frameTimes = new long[100];
private int frameTimeIndex = 0;
private boolean arrayFilled = false;
private double mouseX, mouseY;
@Override
public void start(Stage stage) {
Pane base = new Pane();
base.setStyle("-fx-background-color: rgb(" + 40 + "," + 40 + ", …Run Code Online (Sandbox Code Playgroud) 当没有障碍物移动或从障碍物的顶部移动到侧面时,我的寻路工作正常,但是当它需要从障碍物的顶部到底部找到它时,它太慢了.
我很确定这是我排序每个循环或循环通过封闭集的方式,但我不知道如何避免这种情况,因为在Windows Phone 7上没有SortedLists
//This Vivid Destination means it doesn't have to find the exact location
//which is helpful as it is continuous environment
Rectangle vividDestination = new Rectangle((int)character.destination.X - 10, (int)character.destination.Y - 10, 20, 20);
while (!vividDestination.Contains(Maths.vectorToPoint(OpenNodes[0].position)))
{
Node Current = OpenNodes[0];
OpenNodes.RemoveAt(0);
ClosedNodes.Add(Current);
Current.calculateNeightbours();
foreach (Node neighbour in Current.neighbours)
{
neighbour.parents = Current.parents + 1;
//The cost of the node is the amount of parent nodes + the distance to destination
neighbour.cost = calculateCost(neighbour.position, character.destination, neighbour.parents);
for (int …Run Code Online (Sandbox Code Playgroud)