this repo has no description
1#include <minizinc/algorithms/min_cut.h>
2#include <minizinc/config.hh>
3
4#include <stdexcept>
5
6#ifdef COMPILE_BOOST_MINCUT
7#include <boost/graph/adjacency_list.hpp>
8#include <boost/graph/graph_traits.hpp>
9#include <boost/graph/one_bit_color_map.hpp>
10#include <boost/graph/stoer_wagner_min_cut.hpp>
11#include <boost/property_map/property_map.hpp>
12#include <boost/typeof/typeof.hpp>
13#endif
14
15void Algorithms::MinCut::solve() {
16#ifndef COMPILE_BOOST_MINCUT
17 throw std::runtime_error(
18 "MIP/circuit: Subtour Elimination Constraints: MinCut::solve not compiled. "
19 "Compile with COMPILE_BOOST_MINCUT (needs boost), or use -D nSECcuts=0 for flattening.");
20#else
21 typedef std::pair<unsigned long, unsigned long> edge_t;
22
23 // A graphic of the min-cut is available at
24 // <http://www.boost.org/doc/libs/release/libs/graph/doc/stoer_wagner_imgs/stoer_wagner.cpp.gif>
25 using namespace std;
26
27 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property,
28 boost::property<boost::edge_weight_t, double> >
29 undirected_graph;
30 typedef boost::property_map<undirected_graph, boost::edge_weight_t>::type weight_map_type;
31 typedef boost::property_traits<weight_map_type>::value_type weight_type;
32
33 // define the 16 edges of the graph. {3, 4} means an undirected edge between vertices 3 and 4.
34 // edge_t edges[] = {{3, 4}, {3, 6}, {3, 5}, {0, 4}, {0, 1}, {0, 6}, {0, 7},
35 // {0, 5}, {0, 2}, {4, 1}, {1, 6}, {1, 5}, {6, 7}, {7, 5}, {5, 2}, {3, 4}};
36 std::vector<edge_t> edges_array; // = edges;
37 // std::vector< weight_type > ws_array;
38
39 edges_array.reserve(edges.size());
40 for (const auto& edg : edges)
41 edges_array.push_back(std::pair<unsigned long, unsigned long>(edg.first, edg.second));
42
43 // for each of the 16 edges, define the associated edge weight. ws[i] is the weight for the edge
44 // that is described by edges[i].
45 // weight_type ws[] =
46 // {0.2, 3.1, 1.3, 3.7, 1.5, 2.6, 6.44, 1.26, 8.77, 1.29, 1.95, 80.74, 2.23, 1.94, 1.23, 4.957};
47
48 // construct the graph object. 8 is the number of vertices, which are numbered from 0
49 // through 7, and 16 is the number of edges.
50 undirected_graph g(edges_array.data(), edges_array.data() + edges_array.size(), weights.data(),
51 nNodes, edges_array.size());
52
53 // define a property map, `parities`, that will store a boolean value for each vertex.
54 // Vertices that have the same parity after `stoer_wagner_min_cut` runs are on the same side of
55 // the min-cut.
56 BOOST_AUTO(parities00,
57 boost::make_one_bit_color_map(num_vertices(g), get(boost::vertex_index, g)));
58
59 // run the Stoer-Wagner algorithm to obtain the min-cut weight. `parities` is also filled in.
60 wMinCut =
61 boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g), boost::parity_map(parities00));
62
63 assert(nNodes == num_vertices(g));
64
65 // parities = parities00;
66 parities.clear();
67 parities.reserve(nNodes);
68 for (size_t i = 0; i < num_vertices(g); ++i) {
69 parities.push_back(get(parities00, i));
70 }
71#endif
72}