dwave-preprocessing 0.6.7__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dwave/__init__.py +16 -0
- dwave/preprocessing/__init__.py +42 -0
- dwave/preprocessing/composites/__init__.py +19 -0
- dwave/preprocessing/composites/clip.py +114 -0
- dwave/preprocessing/composites/connected_components.py +113 -0
- dwave/preprocessing/composites/fix_variables.py +153 -0
- dwave/preprocessing/composites/scale.py +144 -0
- dwave/preprocessing/composites/spin_reversal_transform.py +221 -0
- dwave/preprocessing/cyfix_variables.cp313-win_amd64.pyd +0 -0
- dwave/preprocessing/cyfix_variables.cpp +29498 -0
- dwave/preprocessing/cyfix_variables.pyx +58 -0
- dwave/preprocessing/include/dwave/exceptions.hpp +28 -0
- dwave/preprocessing/include/dwave/flags.hpp +63 -0
- dwave/preprocessing/include/dwave/presolve.hpp +91 -0
- dwave/preprocessing/include/dwave-preprocessing/fix_variables.hpp +119 -0
- dwave/preprocessing/include/dwave-preprocessing/helper_data_structures.hpp +152 -0
- dwave/preprocessing/include/dwave-preprocessing/helper_graph_algorithms.hpp +476 -0
- dwave/preprocessing/include/dwave-preprocessing/implication_network.hpp +690 -0
- dwave/preprocessing/include/dwave-preprocessing/mapping_policy.hpp +124 -0
- dwave/preprocessing/include/dwave-preprocessing/posiform_info.hpp +384 -0
- dwave/preprocessing/include/dwave-preprocessing/push_relabel.hpp +775 -0
- dwave/preprocessing/libcpp.pxd +59 -0
- dwave/preprocessing/lower_bounds.py +94 -0
- dwave/preprocessing/presolve/__init__.pxd +13 -0
- dwave/preprocessing/presolve/__init__.py +16 -0
- dwave/preprocessing/presolve/cypresolve.cp313-win_amd64.pyd +0 -0
- dwave/preprocessing/presolve/cypresolve.cpp +39255 -0
- dwave/preprocessing/presolve/cypresolve.pxd +45 -0
- dwave/preprocessing/presolve/cypresolve.pyi +50 -0
- dwave/preprocessing/presolve/cypresolve.pyx +340 -0
- dwave/preprocessing/presolve/exceptions.py +21 -0
- dwave/preprocessing/presolve/pypresolve.py +173 -0
- dwave/preprocessing/src/exceptions.cpp +21 -0
- dwave/preprocessing/src/presolve.cpp +105 -0
- dwave/preprocessing/src/presolveimpl.hpp +1060 -0
- dwave_preprocessing-0.6.7.dist-info/DELVEWHEEL +2 -0
- dwave_preprocessing-0.6.7.dist-info/LICENSE +203 -0
- dwave_preprocessing-0.6.7.dist-info/METADATA +114 -0
- dwave_preprocessing-0.6.7.dist-info/RECORD +42 -0
- dwave_preprocessing-0.6.7.dist-info/WHEEL +5 -0
- dwave_preprocessing-0.6.7.dist-info/top_level.txt +1 -0
- dwave_preprocessing.libs/msvcp140-c5f1b59771da57851103ca2011763553.dll +0 -0
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
// Copyright 2021 D-Wave Systems Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
#ifndef IMPLICATION_NETWORK_HPP_INCLUDED
|
|
16
|
+
#define IMPLICATION_NETWORK_HPP_INCLUDED
|
|
17
|
+
|
|
18
|
+
#include <assert.h>
|
|
19
|
+
#include "helper_graph_algorithms.hpp"
|
|
20
|
+
#include "mapping_policy.hpp"
|
|
21
|
+
#include "push_relabel.hpp"
|
|
22
|
+
|
|
23
|
+
// Edge type for implication network. An implication network is formed from a
|
|
24
|
+
// posiform. If there is a term Coeff * X_i * X_j, we will have two edges in the
|
|
25
|
+
// network one X_i to X_j' and another X_j to X_i', the directions will depend
|
|
26
|
+
// on the the sign of the coefficient. The index of the symmetric edge for
|
|
27
|
+
// X_i connecting to X_j' is the index to the edge in the edge list of X_j that
|
|
28
|
+
// connects to X_i'. It is needed for the step where we average the residual
|
|
29
|
+
// capacities of the edges to get the symmetric residual graph.
|
|
30
|
+
// For more details see : Boros, Endre & Hammer, Peter & Tavares, Gabriel.
|
|
31
|
+
// (2006). Preprocessing of unconstrained quadratic binary optimization. RUTCOR
|
|
32
|
+
// Research Report.
|
|
33
|
+
template <typename capacity_t> class ImplicationEdge {
|
|
34
|
+
public:
|
|
35
|
+
typedef capacity_t capacity_type;
|
|
36
|
+
|
|
37
|
+
ImplicationEdge(int from_vertex, int to_vertex, capacity_t capacity,
|
|
38
|
+
capacity_t reverse_capacity, int reverse_edge_index,
|
|
39
|
+
int symmetric_edge_index)
|
|
40
|
+
: from_vertex(from_vertex), to_vertex(to_vertex),
|
|
41
|
+
reverse_edge_index(reverse_edge_index),
|
|
42
|
+
symmetric_edge_index(symmetric_edge_index), residual(capacity) {
|
|
43
|
+
assert((!capacity || !reverse_capacity) &&
|
|
44
|
+
"Either capacity or reverse edge capacity must be zero.");
|
|
45
|
+
_encoded_capacity = (!capacity) ? -reverse_capacity : capacity;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
void print() {
|
|
49
|
+
std::cout << std::endl;
|
|
50
|
+
std::cout << from_vertex << " --> " << to_vertex << std::endl;
|
|
51
|
+
std::cout << "Capacity : " << getCapacity() << std::endl;
|
|
52
|
+
std::cout << "Residual : " << residual << std::endl;
|
|
53
|
+
std::cout << "Reverse Edge Capacity : " << getReverseEdgeCapacity()
|
|
54
|
+
<< std::endl;
|
|
55
|
+
std::cout << "Reverse Edge Residual : " << getReverseEdgeResidual()
|
|
56
|
+
<< std::endl;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The from_vertex is redundant, since we use an adjacency list, but we are
|
|
60
|
+
// not wasting any space as of now, since the compiler uses the same amount of
|
|
61
|
+
// storage for padding when residual is of a type that takes 8 bytes and in
|
|
62
|
+
// our implmementation we use long long int (8 byte type).
|
|
63
|
+
int from_vertex;
|
|
64
|
+
int to_vertex;
|
|
65
|
+
int reverse_edge_index;
|
|
66
|
+
int symmetric_edge_index;
|
|
67
|
+
capacity_t residual;
|
|
68
|
+
|
|
69
|
+
private:
|
|
70
|
+
// The value of capacity is not needed per se to compute a max-flow but is
|
|
71
|
+
// needed for the purpose of verifying it. We use the encoded capacity to
|
|
72
|
+
// store the capacity of an edge, but when it is a residual/reverse edge,
|
|
73
|
+
// instead of saving 0 we save the negative of the original edge's capacity,
|
|
74
|
+
// this way we can both verify max-flow and also return the residual capacity
|
|
75
|
+
// of the edge itself and also its reverse/residual without hopping through
|
|
76
|
+
// memory. This is not the best software engineering practice, but is needed
|
|
77
|
+
// to save memory. Current size is 32 byte when long long int is used for
|
|
78
|
+
// capacity and 2 such edges fit in a typical cache line, adding 8 bytes will
|
|
79
|
+
// not allow that to be possible.
|
|
80
|
+
capacity_t _encoded_capacity;
|
|
81
|
+
|
|
82
|
+
public:
|
|
83
|
+
inline capacity_t getFlow() {
|
|
84
|
+
return ((_encoded_capacity > 0) ? (_encoded_capacity - residual)
|
|
85
|
+
: -residual);
|
|
86
|
+
}
|
|
87
|
+
inline capacity_t getCapacity() {
|
|
88
|
+
return ((_encoded_capacity > 0) ? _encoded_capacity : 0);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
inline capacity_t getReverseEdgeCapacity() {
|
|
92
|
+
return ((_encoded_capacity > 0) ? 0 : -_encoded_capacity);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
inline capacity_t getReverseEdgeResidual() {
|
|
96
|
+
return ((_encoded_capacity > 0) ? (_encoded_capacity - residual)
|
|
97
|
+
: (-_encoded_capacity - residual));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Needed for the purpose of making residual network symmetric.
|
|
101
|
+
void scaleCapacity(int scale) { _encoded_capacity *= scale; }
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// Needed for binary search when edges are in sorted order.
|
|
105
|
+
class ImplicationEdgeComparator {
|
|
106
|
+
public:
|
|
107
|
+
template <class capacity_t>
|
|
108
|
+
bool operator()(const ImplicationEdge<capacity_t> &a, const int &vertex) {
|
|
109
|
+
return a.to_vertex < vertex;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// Structure for holding various information regarding strongly connected
|
|
114
|
+
// components of a implication network.
|
|
115
|
+
class stronglyConnectedComponentsInfo {
|
|
116
|
+
public:
|
|
117
|
+
stronglyConnectedComponentsInfo(int num_components,
|
|
118
|
+
std::vector<int> vertex_to_component_map,
|
|
119
|
+
mapper_t &mapper)
|
|
120
|
+
: num_vertices(mapper.num_vertices()),num_components(num_components),
|
|
121
|
+
vertex_to_component_map(vertex_to_component_map) {
|
|
122
|
+
components.resize(num_components);
|
|
123
|
+
std::vector<int> component_sizes(num_components, 0);
|
|
124
|
+
source_component = vertex_to_component_map[mapper.source()];
|
|
125
|
+
sink_component = vertex_to_component_map[mapper.sink()];
|
|
126
|
+
|
|
127
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
128
|
+
component_sizes[vertex_to_component_map[vertex]]++;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (int component = 0; component < num_components; component++) {
|
|
132
|
+
components.reserve(component_sizes[component]);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
136
|
+
components[vertex_to_component_map[vertex]].push_back(vertex);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
complement_map.resize(num_components);
|
|
140
|
+
|
|
141
|
+
for (int component = 0; component < num_components; component++) {
|
|
142
|
+
assert(components[component].size() &&
|
|
143
|
+
"Each strongly connected component must have one element.");
|
|
144
|
+
// According to the paper each strongly connected component of the
|
|
145
|
+
// residual graph should have either a set of vertices and its complements
|
|
146
|
+
// in it or there will be another component with the complement vertices.
|
|
147
|
+
// So we can decide the complement of a strongly connected component by
|
|
148
|
+
// using only one vertex.
|
|
149
|
+
complement_map[component] =
|
|
150
|
+
vertex_to_component_map[mapper.complement(components[component][0])];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Since the assumption that a strongly connected component should self
|
|
154
|
+
// contain the complements of its vertices, or there will be a complement of
|
|
155
|
+
// the component is a very strong one, we keep a check for that.
|
|
156
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
157
|
+
if (complement_map[vertex_to_component_map[vertex]] !=
|
|
158
|
+
vertex_to_component_map[mapper.complement(vertex)]) {
|
|
159
|
+
std::cout
|
|
160
|
+
<< "The assumption that each strongly connected component in the "
|
|
161
|
+
"residual graph containing edges with positive capacities, must "
|
|
162
|
+
"contain vertices and their complements, or there will be "
|
|
163
|
+
"another "
|
|
164
|
+
"component with exactly the complementary vertices, did not "
|
|
165
|
+
"hold."
|
|
166
|
+
<< std::endl;
|
|
167
|
+
exit(1);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public:
|
|
173
|
+
int num_vertices;
|
|
174
|
+
int num_components;
|
|
175
|
+
int source_component;
|
|
176
|
+
int sink_component;
|
|
177
|
+
// Component to its complement component.
|
|
178
|
+
std::vector<int> complement_map;
|
|
179
|
+
std::vector<int> vertex_to_component_map;
|
|
180
|
+
std::vector<std::vector<int>> components;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// The implication graph used in the paper Boros, Endre & Hammer, Peter &
|
|
184
|
+
// Tavares, Gabriel. (2006). Preprocessing of unconstrained quadratic binary
|
|
185
|
+
// optimization. RUTCOR Research Report. See the class mappingPolicy to
|
|
186
|
+
// understand the mapping between posiform variables and implication network
|
|
187
|
+
// vertices.
|
|
188
|
+
template <class capacity_t> class ImplicationNetwork {
|
|
189
|
+
|
|
190
|
+
public:
|
|
191
|
+
template <class PosiformInfo> ImplicationNetwork(PosiformInfo &posiform);
|
|
192
|
+
|
|
193
|
+
int getSource() { return _source; }
|
|
194
|
+
|
|
195
|
+
int getSink() { return _sink; }
|
|
196
|
+
|
|
197
|
+
std::vector<std::vector<ImplicationEdge<capacity_t>>> &getAdjacencyList() {
|
|
198
|
+
checkAdjacencyListValidity();
|
|
199
|
+
return _adjacency_list;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
capacity_t fixVariables(std::vector<std::pair<int, int>> &fixed_variables,
|
|
203
|
+
bool only_trivially_strong = false);
|
|
204
|
+
|
|
205
|
+
void print();
|
|
206
|
+
|
|
207
|
+
private:
|
|
208
|
+
// We may free the memory of adjacency list after crucial operations have
|
|
209
|
+
// completed. Thus public functions may need to check the validity of the
|
|
210
|
+
// adjacency list before operating.
|
|
211
|
+
void checkAdjacencyListValidity() {
|
|
212
|
+
if (!_adjacency_list_valid) {
|
|
213
|
+
std::cout << std::endl;
|
|
214
|
+
std::cout << "Function requiring adjacency list of implication network "
|
|
215
|
+
"called after releasing its memory."
|
|
216
|
+
<< std::endl;
|
|
217
|
+
exit(1);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
void makeResidualSymmetric();
|
|
222
|
+
|
|
223
|
+
void extractResidualNetworkWithoutSourceInSinkOut(
|
|
224
|
+
std::vector<std::vector<int>> &adjacency_list_residual,
|
|
225
|
+
bool free_original_adjacency_list = false);
|
|
226
|
+
|
|
227
|
+
capacity_t fixTriviallyStrongVariables(
|
|
228
|
+
std::vector<std::pair<int, int>> &fixed_variables);
|
|
229
|
+
|
|
230
|
+
void fixStronglyConnectedComponentVariables(
|
|
231
|
+
int component, stronglyConnectedComponentsInfo &scc_info,
|
|
232
|
+
std::vector<std::vector<int>> &adjacency_list_components_transposed,
|
|
233
|
+
std::vector<int> &out_degrees,
|
|
234
|
+
std::vector<std::pair<int, int>> &fixed_variables,
|
|
235
|
+
vector_based_queue<int> &component_queue, bool enqueue);
|
|
236
|
+
|
|
237
|
+
capacity_t
|
|
238
|
+
fixStrongAndWeakVariables(std::vector<std::pair<int, int>> &fixed_variables);
|
|
239
|
+
|
|
240
|
+
void createImplicationNetworkEdges(int from_vertex, int to_vertex,
|
|
241
|
+
capacity_t capacity);
|
|
242
|
+
|
|
243
|
+
int _num_variables;
|
|
244
|
+
int _num_vertices;
|
|
245
|
+
int _source;
|
|
246
|
+
int _sink;
|
|
247
|
+
bool _adjacency_list_valid;
|
|
248
|
+
mapper_t _mapper;
|
|
249
|
+
std::vector<std::vector<ImplicationEdge<capacity_t>>> _adjacency_list;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
template <class capacity_t>
|
|
253
|
+
template <class PosiformInfo>
|
|
254
|
+
ImplicationNetwork<capacity_t>::ImplicationNetwork(PosiformInfo &posiform) {
|
|
255
|
+
assert(std::is_integral<capacity_t>::value &&
|
|
256
|
+
std::is_signed<capacity_t>::value &&
|
|
257
|
+
"Implication Network must have signed, integral type coefficients");
|
|
258
|
+
assert(
|
|
259
|
+
(std::numeric_limits<capacity_t>::max() >=
|
|
260
|
+
std::numeric_limits<typename PosiformInfo::coefficient_type>::max()) &&
|
|
261
|
+
"Implication Network must have capacity type with larger maximum "
|
|
262
|
+
"value than the type of coefficients in source posiform.");
|
|
263
|
+
_num_variables = posiform.getNumVariables();
|
|
264
|
+
_mapper = mapper_t(_num_variables);
|
|
265
|
+
_num_vertices = _mapper.num_vertices();
|
|
266
|
+
_source = _mapper.source();
|
|
267
|
+
_sink = _mapper.sink();
|
|
268
|
+
_adjacency_list.resize(2 * _num_variables + 2);
|
|
269
|
+
|
|
270
|
+
// For efficiency we preallocate the vectors first.
|
|
271
|
+
int num_linear = posiform.getNumLinear();
|
|
272
|
+
_adjacency_list[_source].reserve(num_linear);
|
|
273
|
+
_adjacency_list[_sink].reserve(num_linear);
|
|
274
|
+
|
|
275
|
+
// There are reverse edges for each edge created in the implication graph.
|
|
276
|
+
// Depending on the sign of the bias, an edge may start from v or v' but
|
|
277
|
+
// reverse edges makes the number of edges coming out of v and v' the same and
|
|
278
|
+
// are equal to 1/0 + number of quadratic biases in which v contributes. The +
|
|
279
|
+
// 1 is due to the linear term when it is present.
|
|
280
|
+
for (int variable = 0; variable < _num_variables; variable++) {
|
|
281
|
+
int from_vertex = _mapper.variable_to_vertex(variable);
|
|
282
|
+
int from_vertex_complement = _mapper.complement(from_vertex);
|
|
283
|
+
int num_out_edges = posiform.getNumQuadratic(variable);
|
|
284
|
+
auto linear = posiform.getLinear(variable);
|
|
285
|
+
if (linear) {
|
|
286
|
+
num_out_edges++;
|
|
287
|
+
}
|
|
288
|
+
_adjacency_list[from_vertex].reserve(num_out_edges);
|
|
289
|
+
_adjacency_list[from_vertex_complement].reserve(num_out_edges);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
for (int variable = 0; variable < _num_variables; variable++) {
|
|
293
|
+
int from_vertex = _mapper.variable_to_vertex(variable);
|
|
294
|
+
// int from_vertex_complement = _mapper.complement(from_vertex);
|
|
295
|
+
auto quadratic_span = posiform.getQuadratic(variable);
|
|
296
|
+
auto it = quadratic_span.first;
|
|
297
|
+
auto it_end = quadratic_span.second;
|
|
298
|
+
for (; it != it_end; it++) {
|
|
299
|
+
// The quadratic iterators, in the posiform, belong to the original
|
|
300
|
+
// bqm, thus the variables, must be mapped to the posiform variables,
|
|
301
|
+
// and the biases should be ideally converted to the same type the
|
|
302
|
+
// posiform represens them in.
|
|
303
|
+
// IMPORTANT NOTE : We skip dividing by 2 when calculating the implication
|
|
304
|
+
// netowrk edge capacities to avoid rounding errors, but when we compute
|
|
305
|
+
// the max flow and convert it back to a lower bound for the bqm, we must
|
|
306
|
+
// take this into account and divide the max flow by 2.
|
|
307
|
+
// See bottom of page 5 after equation 5 of the following paper.
|
|
308
|
+
// Boros, Endre & Hammer, Peter & Tavares, Gabriel. (2006). Preprocessing of
|
|
309
|
+
// unconstrained quadratic binary optimization. RUTCOR Research Report.
|
|
310
|
+
auto coefficient = posiform.convertToPosiformCoefficient(it->bias);
|
|
311
|
+
int variable_2 = posiform.mapVariableQuboToPosiform(it->v);
|
|
312
|
+
int to_vertex = _mapper.variable_to_vertex(variable_2);
|
|
313
|
+
if (coefficient > 0) {
|
|
314
|
+
createImplicationNetworkEdges(
|
|
315
|
+
from_vertex, _mapper.complement(to_vertex), coefficient);
|
|
316
|
+
} else if (coefficient < 0) {
|
|
317
|
+
createImplicationNetworkEdges(from_vertex, to_vertex, -coefficient);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// We separate out the creation of edges with source and sink, as if the
|
|
323
|
+
// mapping of variables to vertices is ordered such that if variable x <
|
|
324
|
+
// variable y, then vertices corresponding to x and x' both will be less than
|
|
325
|
+
// vertices y and y' then we can keep the order of edges sorted by mapping
|
|
326
|
+
// source and sink to the highest numbered vertices, and creating the
|
|
327
|
+
// corresponding edges last. This ordering lets us optimize the process of
|
|
328
|
+
// making the residual network symmetric.
|
|
329
|
+
for (int variable = 0; variable < _num_variables; variable++) {
|
|
330
|
+
int from_vertex = _mapper.variable_to_vertex(variable);
|
|
331
|
+
int from_vertex_complement = _mapper.complement(from_vertex);
|
|
332
|
+
auto linear = posiform.getLinear(variable);
|
|
333
|
+
if (linear > 0) {
|
|
334
|
+
createImplicationNetworkEdges(_source, from_vertex_complement, linear);
|
|
335
|
+
} else if (linear < 0) {
|
|
336
|
+
createImplicationNetworkEdges(_source, from_vertex, -linear);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
_adjacency_list_valid = true;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Each term in posiform produces four edges in implication network
|
|
343
|
+
// the reverse edges and the symmetric edges.
|
|
344
|
+
template <class capacity_t>
|
|
345
|
+
void ImplicationNetwork<capacity_t>::createImplicationNetworkEdges(
|
|
346
|
+
int from_vertex, int to_vertex, capacity_t capacity) {
|
|
347
|
+
int from_vertex_complement = _mapper.complement(from_vertex);
|
|
348
|
+
int to_vertex_complement = _mapper.complement(to_vertex);
|
|
349
|
+
int from_vertex_edge_index = _adjacency_list[from_vertex].size();
|
|
350
|
+
int to_vertex_edge_index = _adjacency_list[to_vertex].size();
|
|
351
|
+
int from_vertex_complement_edge_index =
|
|
352
|
+
_adjacency_list[from_vertex_complement].size();
|
|
353
|
+
int to_vertex_complement_edge_index =
|
|
354
|
+
_adjacency_list[to_vertex_complement].size();
|
|
355
|
+
|
|
356
|
+
// edge
|
|
357
|
+
_adjacency_list[from_vertex].emplace_back(ImplicationEdge<capacity_t>(
|
|
358
|
+
from_vertex, to_vertex, capacity, 0, to_vertex_edge_index,
|
|
359
|
+
to_vertex_complement_edge_index));
|
|
360
|
+
|
|
361
|
+
// reverse edge
|
|
362
|
+
_adjacency_list[to_vertex].emplace_back(ImplicationEdge<capacity_t>(
|
|
363
|
+
to_vertex, from_vertex, 0, capacity, from_vertex_edge_index,
|
|
364
|
+
from_vertex_complement_edge_index));
|
|
365
|
+
|
|
366
|
+
// symmetric edge
|
|
367
|
+
_adjacency_list[to_vertex_complement].emplace_back(
|
|
368
|
+
ImplicationEdge<capacity_t>(
|
|
369
|
+
to_vertex_complement, from_vertex_complement, capacity, 0,
|
|
370
|
+
from_vertex_complement_edge_index, from_vertex_edge_index));
|
|
371
|
+
|
|
372
|
+
// reverse symmetric edge
|
|
373
|
+
_adjacency_list[from_vertex_complement].emplace_back(
|
|
374
|
+
ImplicationEdge<capacity_t>(from_vertex_complement, to_vertex_complement,
|
|
375
|
+
0, capacity, to_vertex_complement_edge_index,
|
|
376
|
+
to_vertex_edge_index));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Make the residual network symmetric, by summing the residual capacities and
|
|
380
|
+
// original capacities of the implicaiton network. Here we multiply the
|
|
381
|
+
// capacities by 2, since the symmetric edges have same capacity.
|
|
382
|
+
template <class capacity_t>
|
|
383
|
+
void ImplicationNetwork<capacity_t>::makeResidualSymmetric() {
|
|
384
|
+
checkAdjacencyListValidity();
|
|
385
|
+
// If the edges are sorted even if we create edges to/from vertices
|
|
386
|
+
// corresponding to variables and their complements.
|
|
387
|
+
bool edges_sorted = _mapper.complement_maintains_order();
|
|
388
|
+
#pragma omp parallel for schedule(dynamic)
|
|
389
|
+
for (int vertex = 0; vertex < _num_vertices; vertex++) {
|
|
390
|
+
int from_vertex_base = _mapper.non_complemented_vertex(vertex);
|
|
391
|
+
auto eit = edges_sorted ? std::lower_bound(_adjacency_list[vertex].begin(),
|
|
392
|
+
_adjacency_list[vertex].end(),
|
|
393
|
+
from_vertex_base,
|
|
394
|
+
ImplicationEdgeComparator())
|
|
395
|
+
: _adjacency_list[vertex].begin();
|
|
396
|
+
auto eit_end = _adjacency_list[vertex].end();
|
|
397
|
+
for (; eit != eit_end; eit++) {
|
|
398
|
+
int to_vertex = eit->to_vertex;
|
|
399
|
+
int to_vertex_complement = _mapper.complement(to_vertex);
|
|
400
|
+
int to_vertex_base = _mapper.non_complemented_vertex(to_vertex);
|
|
401
|
+
// We don not want to process the symmetric edges twice, we pick
|
|
402
|
+
// the one that starts from the smaller vertex number when
|
|
403
|
+
// complementation is not taken into account.
|
|
404
|
+
if (to_vertex_base > from_vertex_base) {
|
|
405
|
+
auto symmetric_eit = _adjacency_list[to_vertex_complement].begin() +
|
|
406
|
+
eit->symmetric_edge_index;
|
|
407
|
+
capacity_t edge_residual = eit->residual;
|
|
408
|
+
capacity_t symmetric_edge_residual = symmetric_eit->residual;
|
|
409
|
+
// The paper states that we should average the residuals and
|
|
410
|
+
// assign the average, but to avoid underflow we do not divide
|
|
411
|
+
// by two but to keep the flow valid we multiply the
|
|
412
|
+
// capacities by 2. The doubling of capacity is not needed for
|
|
413
|
+
// the later steps in the algorithm, but will help us verify
|
|
414
|
+
// if the symmetric flow is a valid flow or not.
|
|
415
|
+
capacity_t residual_sum = edge_residual + symmetric_edge_residual;
|
|
416
|
+
eit->residual = residual_sum;
|
|
417
|
+
eit->scaleCapacity(2);
|
|
418
|
+
symmetric_eit->residual = residual_sum;
|
|
419
|
+
symmetric_eit->scaleCapacity(2);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// We extract the adjacency list that contains only the edges in the
|
|
426
|
+
// implication network with positive residual capacity, excluding the one going
|
|
427
|
+
// into the source and coming out of the sink. We will run later algorithms on
|
|
428
|
+
// the extracted adjacency list. For a single thread this may or may not be
|
|
429
|
+
// slower when compared to working with the original network due to the extra
|
|
430
|
+
// step but if the graph is large and if we take advantage of parallelism we are
|
|
431
|
+
// supposed to get better performance ase there will be less page faults and
|
|
432
|
+
// more vertices/edges will fit into the cache due to smaller memory footprint
|
|
433
|
+
// of the extracted graph and also due to the fact that we can reduce the cost
|
|
434
|
+
// of extraction using multiple threads.
|
|
435
|
+
template <class capacity_t>
|
|
436
|
+
void ImplicationNetwork<capacity_t>::
|
|
437
|
+
extractResidualNetworkWithoutSourceInSinkOut(
|
|
438
|
+
std::vector<std::vector<int>> &adjacency_list_residual,
|
|
439
|
+
bool free_original_adjacency_list) {
|
|
440
|
+
checkAdjacencyListValidity();
|
|
441
|
+
adjacency_list_residual.resize(_num_vertices);
|
|
442
|
+
#pragma omp parallel
|
|
443
|
+
{
|
|
444
|
+
std::vector<int> temp_buffer(_num_vertices);
|
|
445
|
+
#pragma omp for
|
|
446
|
+
for (int vertex = 0; vertex < _num_vertices; vertex++) {
|
|
447
|
+
if (vertex != _sink) {
|
|
448
|
+
int num_residual_out_edges = 0;
|
|
449
|
+
auto eit = _adjacency_list[vertex].begin();
|
|
450
|
+
auto eit_end = _adjacency_list[vertex].end();
|
|
451
|
+
for (; eit != eit_end; eit++) {
|
|
452
|
+
if (eit->residual > 0) {
|
|
453
|
+
if (eit->to_vertex != _source) {
|
|
454
|
+
temp_buffer[num_residual_out_edges++] = eit->to_vertex;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
adjacency_list_residual[vertex].assign(
|
|
459
|
+
temp_buffer.begin(), temp_buffer.begin() + num_residual_out_edges);
|
|
460
|
+
}
|
|
461
|
+
if (free_original_adjacency_list) {
|
|
462
|
+
std::vector<ImplicationEdge<capacity_t>>().swap(
|
|
463
|
+
_adjacency_list[vertex]);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (free_original_adjacency_list) {
|
|
469
|
+
std::vector<std::vector<ImplicationEdge<capacity_t>>>().swap(
|
|
470
|
+
_adjacency_list);
|
|
471
|
+
_adjacency_list_valid = false;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Fix only variables relevant to a strongly connected component.
|
|
476
|
+
template <class capacity_t>
|
|
477
|
+
void ImplicationNetwork<capacity_t>::fixStronglyConnectedComponentVariables(
|
|
478
|
+
int component, stronglyConnectedComponentsInfo &scc_info,
|
|
479
|
+
std::vector<std::vector<int>> &adjacency_list_components_transposed,
|
|
480
|
+
std::vector<int> &out_degrees,
|
|
481
|
+
std::vector<std::pair<int, int>> &fixed_variables,
|
|
482
|
+
vector_based_queue<int> &component_queue, bool enqueue) {
|
|
483
|
+
|
|
484
|
+
auto &components = scc_info.components;
|
|
485
|
+
auto &complement_map = scc_info.complement_map;
|
|
486
|
+
|
|
487
|
+
int complement_component = complement_map[component];
|
|
488
|
+
out_degrees[component] = -1;
|
|
489
|
+
out_degrees[complement_component] = -1;
|
|
490
|
+
|
|
491
|
+
auto vit = components[component].begin();
|
|
492
|
+
auto vit_end = components[component].end();
|
|
493
|
+
for (; vit != vit_end; vit++) {
|
|
494
|
+
int vertex = *vit;
|
|
495
|
+
int base_vertex = _mapper.non_complemented_vertex(vertex);
|
|
496
|
+
int variable = _mapper.non_complemented_vertex_to_variable(base_vertex);
|
|
497
|
+
fixed_variables.push_back({variable, (vertex == base_vertex) ? 1 : 0});
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
auto eit = adjacency_list_components_transposed[component].begin();
|
|
501
|
+
auto eit_end = adjacency_list_components_transposed[component].end();
|
|
502
|
+
for (; eit != eit_end; eit++) {
|
|
503
|
+
// This is the transpose of the adjacency matrix of components, so edges
|
|
504
|
+
// are coming from the other components.
|
|
505
|
+
int from_component = *eit;
|
|
506
|
+
if (out_degrees[from_component] > 0) {
|
|
507
|
+
out_degrees[from_component]--;
|
|
508
|
+
if (enqueue) {
|
|
509
|
+
component_queue.push(from_component);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
eit = adjacency_list_components_transposed[complement_component].begin();
|
|
515
|
+
eit_end = adjacency_list_components_transposed[complement_component].end();
|
|
516
|
+
for (; eit != eit_end; eit++) {
|
|
517
|
+
int from_component = *eit;
|
|
518
|
+
if (out_degrees[from_component] > 0) {
|
|
519
|
+
out_degrees[from_component]--;
|
|
520
|
+
if (enqueue) {
|
|
521
|
+
component_queue.push(from_component);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// Fix both strong and weak pesistencies. This implementation is a bit different
|
|
528
|
+
// from the implementation mentioned in the paper : Boros, Endre & Hammer, Peter
|
|
529
|
+
// & Tavares, Gabriel. (2006). Preprocessing of unconstrained quadratic binary
|
|
530
|
+
// optimization. RUTCOR Research Report. Here after making the residual network
|
|
531
|
+
// symmetric and extracting the residual network, we specifically remove the
|
|
532
|
+
// edges going into the source and the ones coming out of the sink, this way
|
|
533
|
+
// when we generate the graph of strongly connected components, the source and
|
|
534
|
+
// sink will belong to different strongly connected components of their own,
|
|
535
|
+
// where they will be the sole members. Thus a breadth first search from the
|
|
536
|
+
// source will still find the strong persistencies and fix the variables that
|
|
537
|
+
// the algorithm from the above mentioned paper was supposed to find, since they
|
|
538
|
+
// will be in different components from the source now. We ignore fixing the
|
|
539
|
+
// component where the source is, that component should have only source as its
|
|
540
|
+
// member. Whenever we fix a component we reduce the outdegree of components
|
|
541
|
+
// which had edges into the component just fixed. Whenever a component has zero
|
|
542
|
+
// outdegree and is of not self complementing type, that is vertices and their
|
|
543
|
+
// complements both do not exist in the same component, it becomes a candidate
|
|
544
|
+
// for the fixing process.
|
|
545
|
+
template <class capacity_t>
|
|
546
|
+
capacity_t ImplicationNetwork<capacity_t>::fixStrongAndWeakVariables(
|
|
547
|
+
std::vector<std::pair<int, int>> &fixed_variables) {
|
|
548
|
+
|
|
549
|
+
PushRelabelSolver<ImplicationEdge<capacity_t>> push_relabel_solver(
|
|
550
|
+
_adjacency_list, _source, _sink);
|
|
551
|
+
capacity_t max_flow = push_relabel_solver.computeMaximumFlow(false);
|
|
552
|
+
assert(isMaximumFlow(_adjacency_list, _source, _sink).second &&
|
|
553
|
+
"Maximum flow is not valid.");
|
|
554
|
+
|
|
555
|
+
makeResidualSymmetric();
|
|
556
|
+
assert(isMaximumFlow(_adjacency_list, _source, _sink).second &&
|
|
557
|
+
"Maximum flow is not valid.");
|
|
558
|
+
|
|
559
|
+
// The removal of certain edges will create a component with the source only
|
|
560
|
+
// and another component with the sink only.
|
|
561
|
+
std::vector<std::vector<int>> adjacency_list_residual;
|
|
562
|
+
extractResidualNetworkWithoutSourceInSinkOut(adjacency_list_residual, true);
|
|
563
|
+
|
|
564
|
+
std::vector<int> vertex_to_component_map;
|
|
565
|
+
int num_components = stronglyConnectedComponents(adjacency_list_residual,
|
|
566
|
+
vertex_to_component_map);
|
|
567
|
+
|
|
568
|
+
stronglyConnectedComponentsInfo scc_info(num_components,
|
|
569
|
+
vertex_to_component_map, _mapper);
|
|
570
|
+
|
|
571
|
+
std::vector<std::vector<int>> adjacency_list_components;
|
|
572
|
+
createGraphOfStronglyConnectedComponents(
|
|
573
|
+
scc_info.vertex_to_component_map, scc_info.components,
|
|
574
|
+
adjacency_list_residual, adjacency_list_components);
|
|
575
|
+
|
|
576
|
+
std::vector<std::vector<int>> adjacency_list_components_transposed;
|
|
577
|
+
getTransposedAdjacencyList(adjacency_list_components,
|
|
578
|
+
adjacency_list_components_transposed);
|
|
579
|
+
|
|
580
|
+
std::vector<int> bfs_depth_values;
|
|
581
|
+
int UNVISITED = breadthFirstSearch(
|
|
582
|
+
adjacency_list_components, scc_info.source_component, bfs_depth_values);
|
|
583
|
+
|
|
584
|
+
auto &complement_map = scc_info.complement_map;
|
|
585
|
+
fixed_variables.reserve(_num_variables);
|
|
586
|
+
vector_based_queue<int> component_queue(num_components);
|
|
587
|
+
std::vector<int> out_degrees(num_components, -1);
|
|
588
|
+
|
|
589
|
+
for (int component = 0; component < num_components; component++) {
|
|
590
|
+
if (complement_map[component] != component) {
|
|
591
|
+
out_degrees[component] = adjacency_list_components[component].size();
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
out_degrees[scc_info.source_component] = -1;
|
|
596
|
+
out_degrees[scc_info.sink_component] = -1;
|
|
597
|
+
|
|
598
|
+
for (int component = 0; component < num_components; component++) {
|
|
599
|
+
// Skipping the source component and the other unvisited ones.
|
|
600
|
+
if ((bfs_depth_values[component] != 0) &&
|
|
601
|
+
(bfs_depth_values[component] != UNVISITED)) {
|
|
602
|
+
fixStronglyConnectedComponentVariables(
|
|
603
|
+
component, scc_info, adjacency_list_components_transposed,
|
|
604
|
+
out_degrees, fixed_variables, component_queue, false);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
for (int component = 0; component < num_components; component++) {
|
|
609
|
+
if (out_degrees[component] == 0) {
|
|
610
|
+
component_queue.push(component);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
while (!component_queue.empty()) {
|
|
615
|
+
int component = component_queue.pop();
|
|
616
|
+
fixStronglyConnectedComponentVariables(
|
|
617
|
+
component, scc_info, adjacency_list_components_transposed, out_degrees,
|
|
618
|
+
fixed_variables, component_queue, true);
|
|
619
|
+
}
|
|
620
|
+
return max_flow;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// Fix only the strong variables which can be trivially found. That is fix the
|
|
624
|
+
// variables which can be reached from the source in the residual graph. This is
|
|
625
|
+
// a subset or say the initial part of the algorithm mentioned in the paper :
|
|
626
|
+
// Boros, Endre & Hammer, Peter & Tavares, Gabriel. (2006). Preprocessing of
|
|
627
|
+
// unconstrained quadratic binary optimization. RUTCOR Research Report.
|
|
628
|
+
template <class capacity_t>
|
|
629
|
+
capacity_t ImplicationNetwork<capacity_t>::fixTriviallyStrongVariables(
|
|
630
|
+
std::vector<std::pair<int, int>> &fixed_variables) {
|
|
631
|
+
|
|
632
|
+
PushRelabelSolver<ImplicationEdge<capacity_t>> push_relabel_solver(
|
|
633
|
+
_adjacency_list, _source, _sink);
|
|
634
|
+
capacity_t max_flow = push_relabel_solver.computeMaximumFlow(false);
|
|
635
|
+
assert(isMaximumFlow(_adjacency_list, _source, _sink).second &&
|
|
636
|
+
"Maximum flow is not valid.");
|
|
637
|
+
|
|
638
|
+
std::vector<int> bfs_depth_values;
|
|
639
|
+
int UNVISITED = breadthFirstSearchResidual(_adjacency_list, _source,
|
|
640
|
+
bfs_depth_values, false, false);
|
|
641
|
+
|
|
642
|
+
fixed_variables.reserve(_num_variables);
|
|
643
|
+
std::vector<bool> variable_fixed(_num_variables, false);
|
|
644
|
+
|
|
645
|
+
for (int vertex = 0; vertex < _num_vertices; vertex++) {
|
|
646
|
+
if (bfs_depth_values[vertex] != UNVISITED) {
|
|
647
|
+
int base_vertex = _mapper.non_complemented_vertex(vertex);
|
|
648
|
+
if (base_vertex == _source) {
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
int variable = _mapper.non_complemented_vertex_to_variable(base_vertex);
|
|
652
|
+
fixed_variables.push_back({variable, (vertex == base_vertex) ? 1 : 0});
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return max_flow;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// Parent function for fixing posiform based variables.
|
|
659
|
+
template <class capacity_t>
|
|
660
|
+
capacity_t ImplicationNetwork<capacity_t>::fixVariables(
|
|
661
|
+
std::vector<std::pair<int, int>> &fixed_variables,
|
|
662
|
+
bool only_trivially_strong) {
|
|
663
|
+
if (only_trivially_strong) {
|
|
664
|
+
return fixTriviallyStrongVariables(fixed_variables);
|
|
665
|
+
} else {
|
|
666
|
+
return fixStrongAndWeakVariables(fixed_variables);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// For debugging.
|
|
671
|
+
template <class capacity_t> void ImplicationNetwork<capacity_t>::print() {
|
|
672
|
+
checkAdjacencyListValidity();
|
|
673
|
+
std::cout << std::endl;
|
|
674
|
+
std::cout << "Implication Graph Information : " << std::endl;
|
|
675
|
+
std::cout << "Num Variables : " << _num_variables << std::endl;
|
|
676
|
+
std::cout << "Num Vertices : " << _num_vertices << std::endl;
|
|
677
|
+
std::cout << "Source : " << _source << " Sink : " << _sink << std::endl;
|
|
678
|
+
std::cout << std::endl;
|
|
679
|
+
for (int i = 0; i < _adjacency_list.size(); i++) {
|
|
680
|
+
for (int j = 0; j < _adjacency_list[i].size(); j++) {
|
|
681
|
+
auto &node = _adjacency_list[i][j];
|
|
682
|
+
std::cout << "{ " << i << " --> " << node.to_vertex << " "
|
|
683
|
+
<< node.residual << " ";
|
|
684
|
+
std::cout << node.reverse_edge_index << " " << node.symmetric_edge_index
|
|
685
|
+
<< " } " << std::endl;
|
|
686
|
+
}
|
|
687
|
+
std::cout << std::endl;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
#endif // IMPLICATION_NETWORK_INCLUDED
|