OR-Tools  9.2
symmetry_util.cc
Go to the documentation of this file.
1// Copyright 2010-2021 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cstdint>
17
19
20namespace operations_research {
21namespace sat {
22
23std::vector<std::vector<int>> BasicOrbitopeExtraction(
24 const std::vector<std::unique_ptr<SparsePermutation>>& generators) {
25 // Count the number of permutations that are compositions of 2-cycle and
26 // regroup them according to the number of cycles.
27 std::vector<std::vector<int>> num_cycles_to_2cyclers;
28 for (int g = 0; g < generators.size(); ++g) {
29 const std::unique_ptr<SparsePermutation>& perm = generators[g];
30 bool contain_only_2cycles = true;
31 const int num_cycles = perm->NumCycles();
32 for (int i = 0; i < num_cycles; ++i) {
33 if (perm->Cycle(i).size() != 2) {
34 contain_only_2cycles = false;
35 break;
36 }
37 }
38 if (!contain_only_2cycles) continue;
39 if (num_cycles >= num_cycles_to_2cyclers.size()) {
40 num_cycles_to_2cyclers.resize(num_cycles + 1);
41 }
42 num_cycles_to_2cyclers[num_cycles].push_back(g);
43 }
44
45 // Heuristic: we try to grow the orbitope that has the most potential for
46 // fixing variables.
47 //
48 // TODO(user): We could grow each and keep the real maximum.
49 int best = -1;
50 int best_score = 0;
51 for (int i = 0; i < num_cycles_to_2cyclers.size(); ++i) {
52 if (num_cycles_to_2cyclers[i].size() > 1) {
53 const int num_perms = num_cycles_to_2cyclers[i].size() + 1;
54 VLOG(1) << "Potential orbitope: " << i << " x " << num_perms;
55 const int64_t score = std::min(i, num_perms);
56 if (score > best_score) {
57 best = i;
58 best_score = score;
59 }
60 }
61 }
62
63 std::vector<std::vector<int>> orbitope;
64 if (best == -1) return orbitope;
65
66 // We will track the element already added so we never have duplicates.
67 std::vector<bool> in_matrix;
68
69 // Greedily grow the orbitope.
70 orbitope.resize(best);
71 for (const int g : num_cycles_to_2cyclers[best]) {
72 // Start using the first permutation.
73 if (orbitope[0].empty()) {
74 const std::unique_ptr<SparsePermutation>& perm = generators[g];
75 const int num_cycles = perm->NumCycles();
76 for (int i = 0; i < num_cycles; ++i) {
77 for (const int x : perm->Cycle(i)) {
78 orbitope[i].push_back(x);
79 if (x >= in_matrix.size()) in_matrix.resize(x + 1, false);
80 in_matrix[x] = true;
81 }
82 }
83 continue;
84 }
85
86 // We want to find a column such that g sends it to variables not already
87 // in the orbitope matrix.
88 //
89 // Note(user): This relies on the cycle in each permutation to be ordered by
90 // smaller element first. This way we don't have to account any row
91 // permutation of the orbitope matrix. The code that detect the symmetries
92 // of the problem should already return permutation in this canonical
93 // format.
94 std::vector<int> grow;
95 int matching_column_index = -1;
96 const std::unique_ptr<SparsePermutation>& perm = generators[g];
97 const int num_cycles = perm->NumCycles();
98 for (int i = 0; i < num_cycles; ++i) {
99 // Extract the two elements of this transposition.
100 std::vector<int> tmp;
101 for (const int x : perm->Cycle(i)) tmp.push_back(x);
102 const int a = tmp[0];
103 const int b = tmp[1];
104
105 // We want one element to appear in matching_column_index and the other to
106 // not appear at all.
107 int num_matches_a = 0;
108 int num_matches_b = 0;
109 int last_match_index = -1;
110 for (int j = 0; j < orbitope[i].size(); ++j) {
111 if (orbitope[i][j] == a) {
112 ++num_matches_a;
113 last_match_index = j;
114 } else if (orbitope[i][j] == b) {
115 ++num_matches_b;
116 last_match_index = j;
117 }
118 }
119 if (last_match_index == -1) break;
120 if (matching_column_index == -1) {
121 matching_column_index = last_match_index;
122 }
123 if (matching_column_index != last_match_index) break;
124 if (num_matches_a == 0 && num_matches_b == 1) {
125 if (a >= in_matrix.size() || !in_matrix[a]) grow.push_back(a);
126 } else if (num_matches_a == 1 && num_matches_b == 0) {
127 if (b >= in_matrix.size() || !in_matrix[b]) grow.push_back(b);
128 } else {
129 break;
130 }
131 }
132
133 // If grow is of full size, we can extend the orbitope.
134 if (grow.size() == num_cycles) {
135 for (int i = 0; i < orbitope.size(); ++i) {
136 orbitope[i].push_back(grow[i]);
137 if (grow[i] >= in_matrix.size()) in_matrix.resize(grow[i] + 1, false);
138 in_matrix[grow[i]] = true;
139 }
140 }
141 }
142
143 return orbitope;
144}
145
146std::vector<int> GetOrbits(
147 int n, const std::vector<std::unique_ptr<SparsePermutation>>& generators) {
148 MergingPartition union_find;
149 union_find.Reset(n);
150 for (const std::unique_ptr<SparsePermutation>& perm : generators) {
151 const int num_cycles = perm->NumCycles();
152 for (int i = 0; i < num_cycles; ++i) {
153 // Note that there is currently no random access api like cycle[j].
154 int first;
155 bool is_first = true;
156 for (const int x : perm->Cycle(i)) {
157 if (is_first) {
158 first = x;
159 is_first = false;
160 } else {
161 union_find.MergePartsOf(first, x);
162 }
163 }
164 }
165 }
166
167 int num_parts = 0;
168 std::vector<int> orbits(n, -1);
169 for (int i = 0; i < n; ++i) {
170 if (union_find.NumNodesInSamePartAs(i) == 1) continue;
171 const int root = union_find.GetRootAndCompressPath(i);
172 if (orbits[root] == -1) orbits[root] = num_parts++;
173 orbits[i] = orbits[root];
174 }
175 return orbits;
176}
177
178std::vector<int> GetOrbitopeOrbits(
179 int n, const std::vector<std::vector<int>>& orbitope) {
180 std::vector<int> orbits(n, -1);
181 for (int i = 0; i < orbitope.size(); ++i) {
182 for (int j = 0; j < orbitope[i].size(); ++j) {
183 CHECK_EQ(orbits[orbitope[i][j]], -1);
184 orbits[orbitope[i][j]] = i;
185 }
186 }
187 return orbits;
188}
189
190} // namespace sat
191} // namespace operations_research
int64_t min
Definition: alldiff_cst.cc:139
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:702
#define VLOG(verboselevel)
Definition: base/logging.h:983
int MergePartsOf(int node1, int node2)
int64_t b
int64_t a
std::vector< int > GetOrbitopeOrbits(int n, const std::vector< std::vector< int > > &orbitope)
std::vector< int > GetOrbits(int n, const std::vector< std::unique_ptr< SparsePermutation > > &generators)
std::vector< std::vector< int > > BasicOrbitopeExtraction(const std::vector< std::unique_ptr< SparsePermutation > > &generators)
Collection of objects used to extend the Constraint Solver library.
const bool is_first