25 #include <boost/graph/adjacency_list.hpp>
26 #include <boost/range/iterator_range.hpp>
29 #include <unordered_map>
31 namespace ir = paal::ir;
33 template <
typename Graph,
typename CostMap,
typename OutputIter>
36 vertex_cover(
const Graph & g, CostMap cost_map, OutputIter cover) :
37 m_g(g), m_cost_map(cost_map), m_cover(cover) {}
39 using Vertex =
typename boost::graph_traits<Graph>::vertex_descriptor;
40 using VertexMap = std::unordered_map<Vertex, paal::lp::col_id>;
42 const Graph &get_graph()
const {
return m_g; }
44 auto get_cost(Vertex v)->decltype(std::declval<CostMap>()(v)) {
49 m_vertex_map.insert(
typename VertexMap::value_type(v, col));
54 void add_to_cover(Vertex v) {
63 VertexMap m_vertex_map;
69 template <
typename Problem,
typename LP>
70 void operator()(Problem &problem,
LP &lp) {
71 lp.set_optimization_type(paal::lp::MINIMIZE);
75 boost::make_iterator_range(vertices(problem.get_graph()))) {
76 problem.bind_col_to_vertex(lp.
add_column(problem.get_cost(v)), v);
80 for (
auto e : boost::make_iterator_range(edges(problem.get_graph()))) {
81 auto x_u = problem.vertex_to_column(source(e, problem.get_graph()));
82 auto x_v = problem.vertex_to_column(target(e, problem.get_graph()));
89 template <
typename Problem,
typename GetSolution>
90 void operator()(Problem &problem,
const GetSolution &solution) {
93 boost::make_iterator_range(vertices(problem.get_graph()))) {
94 if (m_compare.
e(solution(problem.vertex_to_column(v)), 1)) {
95 problem.add_to_cover(v);
104 using vertex_cover_ir_components =
111 using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
112 boost::no_property, boost::no_property>;
113 using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
116 std::vector<std::pair<int, int>> edges{ { 0, 1 }, { 0, 2 }, { 1, 2 },
117 { 1, 3 }, { 1, 4 }, { 1, 5 },
118 { 5, 0 }, { 3, 4 } };
119 std::vector<int> costs{ 1, 2, 1, 2, 1, 5 };
121 Graph g(edges.begin(), edges.end(), 6);
123 std::vector<Vertex> result_cover;
124 auto insert_iter = std::back_inserter(result_cover);
127 g, vertex_costs, insert_iter);
134 if (result.first == paal::lp::OPTIMAL) {
135 std::cout <<
"Vertices in the cover:" << std::endl;
136 for (
auto v : result_cover) {
137 std::cout <<
"Vertex " << v << std::endl;
139 std::cout <<
"Cost of the solution: " << *(result.second) << std::endl;
141 std::cout <<
"The instance is infeasible" << std::endl;
143 paal::lp::glp::free_env();
[Iterative Rounding Problem Example]
The common LP solvers base class. Responsible for:
col_id add_column(double cost_coef=0, double lb=0., double ub=lp_traits::PLUS_INF, const std::string &name="")
This file contains set of simple useful functors or functor adapters.
auto make_array_to_functor(const Array &a, int offset=0)
make function for array_to_functor
Column rounding component. A variable is rounded up to 1, if it has value at least half in the soluti...
typename components::type< Args...> IRcomponents
Iterative rounding components.
row_id add_row(const double_bounded_expression &constraint=double_bounded_expression{}, const std::string &name="")
IRResult solve_iterative_rounding(Problem &problem, IRcomponents components, Visitor visitor=Visitor())
Solves an Iterative Rounding problem.
bool e(T a, T b) const
equals
[Iterative Rounding Problem Example]
int main()
[Iterative Rounding Components Example]