首页 文章

如何在Boost图库捆绑属性中使用数组?

提问于
浏览
0

我有一个关于在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中指定提升算法,命名params以使用数组的特定索引作为成本函数?

提前致谢 .

干杯,帕布

1 回答

  • 1

    编译错误意味着以下内容:您尝试告诉编译器使用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具有正确的复制语义 .

相关问题