![](/img/trans.png)
[英]Boost Graph Library Polymorphic Bundled Properties
[英]How to use arrays in Boost graph library bundled properties ?
我對在Boost圖形庫的捆綁屬性中使用數組有疑問。 下面我嘗試並最終導致編譯錯誤。
下面是我的圖聲明。
struct PT_EdgeProperties{
PT_EdgeId edge_id;
double weights[10];
};
typedef boost::property<boost::vertex_name_t, std::string> PT_VertexProperties;
typedef boost::adjacency_list<boost::vecS,
boost::vecS,
boost::directedS,
PT_VertexProperties,
PT_EdgeProperties> PublicTransitGraph;
vector<sim_mob::StreetDirectory::PT_EdgeId>
以下是我使用astar_search boost算法查找最短路徑的代碼。
sim_mob::A_StarPublicTransitShortestPathImpl::searchShortestPath(StreetDirectory::PT_VertexId fromNode,StreetDirectory::PT_VertexId toNode,int cost)
{
vector<StreetDirectory::PT_EdgeId> res;
StreetDirectory::PT_Vertex from=vertexMap.find(fromNode)->second;;
StreetDirectory::PT_Vertex to=vertexMap.find(toNode)->second;;
StreetDirectory::PublicTransitGraph& graph = publictransitMap_;
list<StreetDirectory::PT_Vertex> partialRes;
vector<StreetDirectory::PT_Vertex> p(boost::num_vertices(graph)); //Output variable
vector<double> d(boost::num_vertices(graph)); //Output variable
try {
boost::astar_search(
graph,
from,
distance_heuristic_graph_PT(&graph, to),
boost::weight_map(get(&StreetDirectory::PT_EdgeProperties::weights[cost],graph)).predecessor_map(&p[0]).distance_map(&d[0])
.visitor(astar_pt_goal_visitor(to)));
}
catch (pt_found_goal& goal) {
for (StreetDirectory::PT_Vertex v=to;;v=p[v]) {
partialRes.push_front(v);
if(p[v] == v) {
break;
}
}
std::list<StreetDirectory::PT_Vertex>::const_iterator prev = partialRes.end();
for (std::list<StreetDirectory::PT_Vertex>::const_iterator it=partialRes.begin(); it!=partialRes.end(); it++) {
if (prev!=partialRes.end()) {
std::pair<StreetDirectory::PT_Edge, bool> edge = boost::edge(*prev, *it, graph);
if (!edge.second) {
Warn() <<"ERROR: Boost can't find an edge that it should know about." <<std::endl;
return vector<StreetDirectory::PT_EdgeId>();
}
StreetDirectory::PT_EdgeId edge_id = get(&StreetDirectory::PT_EdgeProperties::edge_id, graph,edge.first);
res.push_back(edge_id);
}
prev = it;
}
}
return res;
}
編譯錯誤:
In file included from /home/smart-nus/code/simmobility/dev/Basic/shared/geospatial/streetdir/A_StarPublicTransitShortestPathImpl.hpp:22:0,
from /home/smart-nus/code/simmobility/dev/Basic/shared/geospatial/streetdir/A_StarPublicTransitShortestPathImpl.cpp:5:
/home/smart-nus/code/simmobility/dev/Basic/shared/geospatial/streetdir/StreetDirectory.hpp: In member function ‘virtual std::vector<int> sim_mob::A_StarPublicTransitShortestPathImpl::searchShortestPath(sim_mob::StreetDirectory::PT_VertexId, sim_mob::StreetDirectory::PT_VertexId, int)’:
/home/smart-nus/code/simmobility/dev/Basic/shared/geospatial/streetdir/StreetDirectory.hpp:208:20: error: invalid use of non-static data member ‘sim_mob::StreetDirectory::PT_EdgeProperties::weights’
double weights[10];
^
/home/smart-nus/code/simmobility/dev/Basic/shared/geospatial/streetdir/A_StarPublicTransitShortestPathImpl.cpp:223:64: error: from this location
boost::weight_map(get(&StreetDirectory::PT_EdgeProperties::weights[cost],graph)).predecessor_map(&p[0]).distance_map(&d[0])
^
make[2]: *** [CMakeFiles/SimMob_Shared.dir/shared/geospatial/streetdir/A_StarPublicTransitShortestPathImpl.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/SimMob_Shared.dir/all] Error 2
make: *** [all] Error 2
問題:您能否幫助我在BGL的捆綁屬性中找到使用數組的方法。 以及如何在weight_map boost指定的params中指定提升算法以將數組的特定索引用作成本函數?
提前致謝 。
干杯,普拉布
編譯錯誤的含義如下:您嘗試告訴編譯器使用edge屬性weight [cost]作為權重圖。 這里的整數“ cost”在編譯時未知,它是函數參數。
它有些不標准,但是可行。 您應該定義自己的屬性圖
#include <boost/graph/properties.hpp>
#include <boost/graph/properties.hpp>
#include <boost/property_map/property_map.hpp>
struct variable_weight_map //maps "PublicTransit" edge to its weight
{
typedef boost::readable_property_map_tag category;
typedef double value_type;
typedef const value_type & reference;
typedef PublicTransitGraph::edge_descriptor key_type;
variable_weight_map(const PublicTransitGraph* graph, size_t cost_idx_)
: cost_idx(cost_idx_), g(graph)
{
}
size_t cost_idx;
const PublicTransitGraph* g;
};
//ReadablePropertyMapConcept
variable_weight_map::value_type
get( variable_weight_map pmap,
variable_weight_map::key_type edge)
{
//access edge_property, then extract specific element of the array
return (*pmap.g)[edge].weights[pmap.cost_idx];
}
現在,當您需要傳遞此類屬性映射時,可以在指定成本索引的地方調用其構造函數:
boost::astar_search(
graph,
from,
distance_heuristic_graph_PT(&graph, to),
boost::weight_map( variable_weight_map(&graph, cost) )
...
順便說一句,我還建議用Boost數組替換邊緣屬性中的C數組:boost :: array weights; 與C-array不同,Boost.Array具有正確的復制語義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.