void set_candidates_and_demands (const Graph& g, Vertices& candidates, Vertices& demands, ExistedCenterMap existed_center_map, CandidateMap candidate_map) { using boost::vertices; const typename boost::graph_traits<Graph>::vertices_size_type num_v = num_vertices(g); candidates.reserve(num_v); demands.reserve(num_v); typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end; for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { if (get(candidate_map, *vi) != false && get(existed_center_map, *vi) == false) { candidates.push_back(*vi); } demands.push_back(*vi); } // TODO copy_if or filter_iterator // if (candidates.empty()) { throw std::runtime_error("The number of candidate vertex is too small"); } }
std::string Rule::RightGraph::Vertex::printStereo(const graph::Printer &p) const { if(!r) throw LogicError("Can not get radical status on a null vertex."); const auto &graph = get_graph(get_labelled_right(r->getRule().getDPORule())); using boost::vertices; auto v = *std::next(vertices(graph).first, vId); return lib::IO::Stereo::Write::summary(r->getRule(), v, lib::Rules::Membership::Right, p.getOptions()); }
void add_middle_vertex_to_edges (const InputGraph& in_g, OutputGraph& out_g, WeightTag w_tag, IndexMap i_map, Orig2CopyMap orig_to_copy_map) { using boost::vertices; using boost::edges; using boost::num_edges; using boost::source; using boost::target; using boost::clear_vertex; using boost::add_vertex; using boost::add_edge; typedef typename boost::graph_traits<OutputGraph>::vertex_descriptor Vertex; typedef typename boost::property_map<OutputGraph, boost::edge_all_t>::type EdgePropertyMap; typedef typename boost::property_traits<EdgePropertyMap>::value_type EdgePropertyValue; typedef typename boost::property_map<OutputGraph, WeightTag>::type WeightMap; typedef typename boost::property_traits<WeightMap>::value_type WeightValue; typedef boost::tuple<Vertex, Vertex, EdgePropertyValue, WeightValue> EdgeTuple; typedef std::vector<EdgeTuple> EdgeTuples; boost::copy_graph(in_g, out_g, boost::vertex_index_map(i_map). orig_to_copy(orig_to_copy_map)); WeightMap w_map = get(w_tag, out_g); EdgePropertyMap e_all_map = get(boost::edge_all, out_g); EdgeTuples edge_tuples; edge_tuples.reserve(num_edges(out_g)); typename boost::graph_traits<OutputGraph>::edge_iterator ei, ei_end; for (boost::tie(ei, ei_end) = edges(out_g); ei != ei_end; ++ei) { edge_tuples.push_back (make_tuple(source(*ei, out_g), target(*ei, out_g), get(e_all_map, *ei), get(w_map, *ei) / 2)); } typename boost::graph_traits<OutputGraph>::vertex_iterator vi, vi_end; for (boost::tie(vi, vi_end) = vertices(out_g); vi != vi_end; ++vi) { clear_vertex(*vi, out_g); } // add_vertex must not invalid vertex_descriptor for (typename EdgeTuples::const_iterator eti = edge_tuples.begin(), eti_end = edge_tuples.end(); eti != eti_end; ++eti) { const Vertex new_v = add_vertex(out_g); put(w_map, add_edge(get<0>(*eti), new_v, get<2>(*eti), out_g).first, get<3>(*eti)); put(w_map, add_edge(new_v, get<1>(*eti), get<2>(*eti), out_g).first, get<3>(*eti)); } }
Rule::RightGraph::Edge Rule::Edge::getRight() const { if(isNull()) return RightGraph::Edge(); const auto &dpoRule = r->getRule().getDPORule(); const auto &graph = get_graph(dpoRule); using boost::vertices; const auto v = *(vertices(graph).first + vId); const auto e = *(out_edges(v, graph).first + eId); if(membership(dpoRule, e) != lib::Rules::Membership::Left) { const auto &vs = vertices(get_right(r->getRule().getDPORule())); auto iter = std::find(vs.first, vs.second, v); auto vSubId = std::distance(vs.first, iter); const auto &es = out_edges(v, get_right(r->getRule().getDPORule())); auto eIter = std::find(es.first, es.second, e); auto eSubId = std::distance(es.first, eIter); return RightGraph::Edge(r, vSubId, eSubId); } else return RightGraph::Edge(); }
inline void parallel_dijkstra_shortest_paths_no_init (const Graph& g, RootVertexMap root_vertex_map, PredecessorMap predecessor_map, DistanceMap distance_map, WeightMap weight_map, IndexMap index_map, Compare compare, Combine combine, DistZero zero, DijkstraVisitor vis, ColorMap color_map) { using boost::vertices; using boost::num_vertices; // TODO VertexList and IncidentGraph CenceptCheck typedef typename boost::property_traits<RootVertexMap>::value_type RootVertex; typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; boost::function_requires< boost::Convertible<RootVertex, Vertex> >(); typedef boost::indirect_cmp<DistanceMap, Compare> IndirectCmp; IndirectCmp icmp(distance_map, compare); #ifdef BOOST_GRAPH_DIJKSTRA_USE_RELAXED_HEAP typedef boost::relaxed_heap<Vertex, IndirectCmp, IndexMap> MutableQueue; MutableQueue Q(num_vertices(g), icmp, index_map); #else // Now the default: use a d-ary heap boost::scoped_array<std::size_t> index_in_heap_map_holder; typedef boost::detail::vertex_property_map_generator<Graph, IndexMap, std::size_t> IndexInHeapMapHelper; typedef typename IndexInHeapMapHelper::type IndexInHeapMap; IndexInHeapMap index_in_heap = IndexInHeapMapHelper::build(g, index_map, index_in_heap_map_holder); typedef boost::d_ary_heap_indirect<Vertex, 4, IndexInHeapMap, DistanceMap, Compare> MutableQueue; MutableQueue Q(distance_map, index_in_heap, compare); #endif // Relaxed heap typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end; for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { if (get(root_vertex_map, *vi) != false) { Q.push(*vi); } } if (Q.empty()) { // TODO throw std::runtime_error("not specified root vertices."); } boost::detail::dijkstra_bfs_visitor<DijkstraVisitor, MutableQueue, WeightMap, PredecessorMap, DistanceMap, Combine, Compare> bfs_vis(vis, Q, weight_map, predecessor_map, distance_map, combine, compare, zero); const Vertex s = Q.top(); Q.pop(); boost::breadth_first_visit(g, s, Q, bfs_vis, color_map); }
BondType Rule::RightGraph::Edge::getBondType() const { if(!r) throw LogicError("Can not get bond type on a null edge."); const auto &graph = get_right(r->getRule().getDPORule()); using boost::vertices; auto iter = vertices(graph).first; std::advance(iter, vId); auto v = *iter; auto e = *std::next(out_edges(v, graph).first, eId); return r->getRule().getMoleculeState().getRight()[e]; }
void set_distance_matrix (const Graph& g, DistanceMatrix& d_matrix, IndexMap i_map, ExistedCenterMap e_c_map) { using boost::vertices; typedef typename boost::graph_traits<Graph>::vertex_iterator VIter; VIter vi, vi_end; for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { if (get(e_c_map, *vi) != false) { VIter ui, ui_end; for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) { const std::size_t i = get(i_map, *ui); const WeightValue dist = d_matrix[i][get(i_map, *vi)]; std::replace_if(d_matrix[i].begin(), d_matrix[i].end(), dist < boost::lambda::_1, dist); // boost::replace_if(d_matrix[i], dist < boost::lambda::_1, dist); } } } }
Rule::Edge Rule::RightGraph::Edge::getCore() const { if(isNull()) return Rule::Edge(); const auto &graph = get_right(r->getRule().getDPORule()); using boost::vertices; auto v = *std::next(vertices(graph).first, vId); auto vCoreId = get(boost::vertex_index_t(), graph, v); auto e = *std::next(out_edges(v, graph).first, eId); const auto &es = out_edges(v, get_graph(r->getRule().getDPORule())); auto eCoreId = std::distance(es.first, std::find(es.first, es.second, e)); return Rule::Edge(r, vCoreId, eCoreId); }
Rule::RightGraph::Vertex Rule::Vertex::getRight() const { if(isNull()) return RightGraph::Vertex(); const auto &dpoRule = r->getRule().getDPORule(); const auto v = vertex(vId, get_graph(dpoRule)); if(membership(dpoRule, v) != lib::Rules::Membership::Left) { using boost::vertices; const auto &vs = vertices(get_right(r->getRule().getDPORule())); auto iter = std::find(vs.first, vs.second, v); auto vSubId = std::distance(vs.first, iter); return RightGraph::Vertex(r, vSubId); } else return RightGraph::Vertex(); }
inline typename boost::graph_traits<Graph>::vertex_descriptor get_start_vertex(const Graph& g) { using boost::vertices; using boost::out_degree; typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end; for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { if (out_degree(*vi, g) == 1) { return *vi; } } // TODO error process throw std::runtime_error("Graph is not linear."); }
void const_constraints(const G& cg) { #ifdef BOOST_VECTOR_AS_GRAPH_GRAPH_ADL_HACK // dwa 2003/7/11 -- This clearly shouldn't be necessary, but if // you want to use vector_as_graph, it is! I'm sure the graph // library leaves these out all over the place. Probably a // redesign involving specializing a template with a static // member function is in order :( using boost::vertices; #endif p = vertices(cg); v = *p.first; V = num_vertices(cg); }
void constraints() { function_requires< GraphConcept<G> >(); function_requires< MultiPassInputIteratorConcept<vertex_iterator> >(); function_requires< ConvertibleConcept<traversal_category, vertex_list_graph_tag> >(); #ifdef BOOST_VECTOR_AS_GRAPH_GRAPH_ADL_HACK // dwa 2003/7/11 -- This clearly shouldn't be necessary, but if // you want to use vector_as_graph, it is! I'm sure the graph // library leaves these out all over the place. Probably a // redesign involving specializing a template with a static // member function is in order :( using boost::vertices; #endif p = vertices(g); v = *p.first; const_constraints(g); }
inline void parallel_dijkstra_shortest_paths (const VertexListGraph& g, RootVertexMap root_vertex_map, PredecessorMap predecessor_map, DistanceMap distance_map, WeightMap weight_map, IndexMap index_map, Compare compare, Combine combine, DistInf inf, DistZero zero, DijkstraVisitor vis, ColorMap color_map) { using boost::vertices; // TODO VertexList and IncidentGraph CenceptCheck typedef typename boost::property_traits<RootVertexMap>::value_type RootVertex; typedef typename boost::graph_traits<VertexListGraph>::vertex_descriptor Vertex; boost::function_requires< boost::Convertible<RootVertex, Vertex> >(); typedef typename boost::property_traits<ColorMap>::value_type ColorValue; typedef boost::color_traits<ColorValue> Color; typename boost::graph_traits<VertexListGraph>::vertex_iterator vi, vi_end; for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { vis.initialize_vertex(*vi, g); if (get(root_vertex_map, *vi) != false) { put(distance_map, *vi, zero); } else { put(distance_map, *vi, inf); } put(predecessor_map, *vi, *vi); put(color_map, *vi, Color::white()); } parallel_dijkstra_shortest_paths_no_init (g, root_vertex_map, predecessor_map, distance_map, weight_map, index_map, compare, combine, zero, vis, color_map); }
typename AcyclicGraph<LinearGraph, WeightMap>::type convert_linear_graph_to_acyclic_graph (const LinearGraph& g, WeightMap weight, CandidateMap candidate, Combine combine, Multiple multiple) { using boost::vertices; using boost::out_edges; using boost::out_degree; using boost::target; typedef boost::graph_traits<LinearGraph> Traits; typedef typename Traits::vertex_descriptor Vertex; typedef typename Traits::vertex_iterator VIter; typedef typename boost::property_traits<WeightMap>::value_type WeightValue; typedef typename AcyclicGraph<LinearGraph, WeightMap>::type AcyclicGraph; const typename std::iterator_traits<VIter>::difference_type num_v = boost::distance(vertices(g)); const Vertex s = detail::get_start_vertex(g); std::vector<Vertex> new_vertices; new_vertices.reserve(num_v); std::vector<WeightValue> e_weight; e_weight.reserve(num_v - 1); // Collect info about a linear graph new_vertices.push_back(s); Vertex prev = s, now = s; typename Traits::out_edge_iterator ei, ei_end; do { boost::tie(ei, ei_end) = out_edges(now, g); if (target(*ei, g) == prev) { ++ei; } const Vertex next = target(*ei, g); if (get(candidate, next) || out_degree(next, g) == 1) { new_vertices.push_back(next); } if (get(candidate, now) || out_degree(now, g) == 1) { e_weight.push_back(get(weight, *ei)); } else { e_weight.back() = combine(e_weight.back(), get(weight, *ei)); } prev = now; now = next; } while (out_degree(now, g) != 1); // create acyclic graph's edge info const std::vector< std::pair<std::size_t, std::size_t> > new_edges(detail::create_new_edges(new_vertices.size())); const std::vector<WeightValue> new_weight(detail::convert_edge_weight(e_weight, combine, multiple)); AcyclicGraph acyclic_g( boost::edges_are_sorted, new_edges.begin(), new_edges.end(), new_weight.begin(), new_vertices.size()); // set vertex prop; typename boost::graph_traits<AcyclicGraph>::vertex_iterator vi, vi_end; for (boost::tie(vi, vi_end) = vertices(acyclic_g); vi != vi_end; ++vi) { acyclic_g[*vi] = new_vertices[get(boost::vertex_index, acyclic_g, *vi)]; } return acyclic_g; }
typename boost::property_traits<WeightMap>::value_type uneven_dist_center (const Graph& g, std::size_t k, CenterMap center_map, WeightMap weight_map, EdgeColorMap color_map, ExistedCenterMap existed_center_map, CandidateMap candidate_map, Compare compare, Combine combine, Multiple multiple, DistZero zero) { using boost::vertices; using boost::source; using boost::target; typedef linear_graphs<Graph> LinearGraphs; typedef typename LinearGraphs::value_type LinearGraph; typedef typename AcyclicGraph<LinearGraph, WeightMap>::type AcyclicGraph; typedef typename boost::property_map<AcyclicGraph, boost::edge_bundle_t>::type AcyclicWeightMap; typedef detail::graph_info<AcyclicGraph, AcyclicWeightMap, Compare, Combine> GraphInfo; typedef std::vector<GraphInfo> GraphInfos; const LinearGraphs lgraphs = split_to_linear_graphs(g, color_map, existed_center_map); /* TODO if (std::accumulate(lgraphs.begin(), lgraphs.end(), 0) < k) { throw std::runtime_error("The number of center is too large"); } */ // convert linear graph to acyclic graph GraphInfos graph_infos; graph_infos.reserve(lgraphs.size()); typename GraphInfo::OptimalSolutions optimal_solutions; typename GraphInfo::OptimalResouceContainers optimal_resource_containers; std::priority_queue< GraphInfo*, std::vector<GraphInfo*>, indirected_less<GraphInfo> > que; for (typename LinearGraphs::const_iterator it = lgraphs.begin(); it != lgraphs.end(); ++it) { #ifndef __GXX_EXPERIMENTAL_CXX0X__ graph_infos.push_back( GraphInfo( convert_linear_graph_to_acyclic_graph( *it, weight_map, candidate_map, combine, multiple), compare, combine, zero, optimal_solutions, optimal_resource_containers)); #else graph_infos.emplace_back( convert_linear_graph_to_acyclic_graph( *it, weight_map, candidate_map, combine, multiple), compare, combine, zero, optimal_solutions, optimal_resource_containers); #endif if (graph_infos.back().is_allocable()) { que.push(&graph_infos.back()); } } // allocate center number for (std::size_t i = 0; i < k; ++i) { if (que.empty()) { throw std::runtime_error("The number of center is too large"); } GraphInfo& info = *(que.top()); que.pop(); info.update(); if (info.is_allocable()) { que.push(&info); } } // set center_map // TODO Exclude existed center? typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end; boost::fill( vertices(g) | Canard::adaptors::mapped(center_map), false); for (typename GraphInfos::const_iterator it = graph_infos.begin(); it != graph_infos.end(); ++it) { const AcyclicGraph& graph = it->graph; typename GraphInfo::EdgeVector::const_iterator first = it->prev_solution.begin(), last = it->prev_solution.end(); put(center_map, graph[target(*first, graph)], true); for (; first != last; ++first) { put(center_map, graph[source(*first, graph)], true); } } return boost::accumulate( graph_infos | boost::adaptors::transformed( std::mem_fun_ref(&GraphInfo::get_value)), zero); }