我正试图在C#中实现A *算法。我已经建立街道(egdes)和streetcorners(节点),我有一个像坐标的大图:用C#实现A *算法(了解伪代码)
10 50 Streetname 50 70
这里10 50是一个节点,具有定向边缘(Streetname)到节点50 70。你可以找到两个节点与毕达哥拉斯之间的距离。
我有问题的理解8号线和13〜15。希望有人能告诉这是什么意思。
节点
public class Node
{
public string name { get; private set; } // Name of the node
public Node parent { get; set; } // parent to the current node
public bool visited { get; set; } // visited, to check if node has been vsisited
public int x { get; set; } // x cordinate of the node (street corner)
public int y { get; set; } // y cordinate of the node (street corner)
public double g { get; set; } // Distance from start to this node
public double h { get; set; } // Heuristic (guess) distance from this node to goal
public double f { get; set; } // Disstance from start + heuristic distance
public List<Edge> neighbors = new List<Edge>(); // All succesors to node
public Node(string name, int x, int y)
{
this.name = name;
this.x = x;
this.y = y;
}
public void calculate_G_Cost(Node other)
{
g = other.getG() + Math.Sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
public double getG()
{
return g;
}
public double GetH()
{
return h;
}
}
边缘
public class Edge
{
public Node node { get; set; }
public string streetName { get; set; }
public Edge(Node v, string street)
{
node = v;
streetName = street;
}
}
A *搜索
public void search(Node root, Node goal)
{
// Open list
SimplePriorityQueue<Node> openList = new SimplePriorityQueue<Node>();
// Closed list
List<Node> closedList = new List<Node>();
// Put starting node into openList
openList.Enqueue(root, Convert.ToSingle(root.f));
// Inizialize currentNode object
Node currentNode;
while (openList.Count > 0)
{
currentNode = openList.Dequeue();
currentNode.visited = true;
if (currentNode.Equals(goal))
{
// Stop algorithm
Console.WriteLine("Goal node has been found");
break;
}
// Calculate g cost from current and set parent
foreach (var neighbor in currentNode.neighbors)
{
neighbor.node.calculate_G_Cost(currentNode); // Calculate G
neighbor.node.parent = currentNode; // Set parent
neighbor.node.h = 0; // Calculate H
neighbor.node.f = neighbor.node.g + neighbor.node.h; // Calculate F
// If neighbor node is in openList
if (openList.Contains(neighbor.node)) {
}
// If neighbor node is in closedList
if (closedList.Contains(neighbor.node))
{
}
}
closedList.Add(currentNode);
Console.WriteLine(currentNode.name + " has been added to closeList");
}
}
我知道我使用的0启发式值,whic小时,这使得这是一个最好的首先搜索的方法,但会增加我的估计。
难道我做错了什么要downvoted? – Pixel
你的问题很不清楚。你说你不懂某些行,然后提供你当前的代码。你不明白什么?你的哪部分代码不起作用? – Rob
对不起,这个例子中只有一些部分我不清楚,但理查德做了一个很好的友好的解释,所以现在我试图解决它:) – Pixel