【发布时间】:2016-03-03 11:34:11
【问题描述】:
我正在尝试实现自己的 A 星算法。但是,我只是想出了一种通过迭代器获取边缘权重值的方法。由于迭代器按顺序访问属性映射,因此我无法确定边的优先级,并且需要为每个顶点执行完整循环以查找邻居,这会使算法非常慢。
有没有什么方法可以在没有迭代器的情况下使用 get()、put()...,选择我要检查边缘的顶点?
这是我目前的程序:
struct Point {//struct point with vertex properties
int x, y;
int parentx, parenty;
double g;
double h;
friend std::ostream& operator<<(std::ostream& os, Point p) {
return os << "[" << p.x << "," << p.y << "]";
}
};
int main() {
//declarations
typedef property < edge_weight_t, double >Weight;
using std::vector;//?
using Graph = adjacency_list<setS, vecS, undirectedS, Point, Weight>;//graph includes our created point struct property<edge_weight_t
using vertex_descriptor = Graph::vertex_descriptor;
Graph lattuce;
//lattuce graph is created with weighted edges value 1 or 1,41 if diagonal. The functions used on a loop are:
//add_edge(nodes[p.x][p.y],nodes[neighbour.x][neighbour.y], Weight(1.0), lattuce);
//add_edge(nodes[p.x][p.y],nodes[neighbour.x][neighbour.y], Weight(1.4), lattuce);
typedef Graph::edge_iterator EdgeIterator;
std::pair<EdgeIterator, EdgeIterator> edges = boost::edges(lattuce);
typedef boost::property_map<Graph, boost::edge_weight_t>::type WeightMap;
WeightMap weights = boost::get(boost::edge_weight_t(), lattuce);
//cout<<get(weights,(1,2));
EdgeIterator edge;
for (edge = edges.first; edge != edges.second; ++edge) {
std::cout << boost::get(weights, *edge) <<" "<< *edge<< " ";
if (source(*edge, lattuce) == origin || target(*edge, lattuce)==origin ){
get(weights, *edge);
cout<<"Edge related to the origin ";
}
if (source(*edge, lattuce) == end_vertex || target(*edge, lattuce)==end_vertex ){
cout<<"Edge related to the end_vertex ";
}
if (get(weights, *edge)==1.41){
cout<<" Diagonal"<<endl;
}
else if (get(weights, *edge)==1){
cout<<" Unitary"<<endl;
}
else if (get(weights, *edge)==99999999.0){
cout<<"Infinite"<<endl;
}
}
}
另外,希望能够在 Point 结构中修改 f、h 和 parent 的值。
任何帮助或指导将不胜感激。
【问题讨论】:
标签: algorithm boost graph a-star boost-graph