forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
costas_array_sat.cc
328 lines (279 loc) · 9.96 KB
/
costas_array_sat.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Costas Array Problem
//
// Finds an NxN matrix of 0s and 1s, with only one 1 per row,
// and one 1 per column, such that all displacement vectors
// between each pair of 1s are distinct.
//
// This example contains two separate implementations. CostasHard()
// uses hard constraints, whereas CostasSoft() uses a minimizer to
// minimize the number of duplicates.
#include <cstdint>
#include <ctime>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/types/span.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/base/types.h"
#include "ortools/sat/cp_model.h"
#include "ortools/sat/model.h"
ABSL_FLAG(int, minsize, 0, "Minimum problem size.");
ABSL_FLAG(int, maxsize, 0, "Maximum problem size.");
ABSL_FLAG(int, model, 1,
"Model type: 1 integer variables hard, 2 boolean variables, 3 "
"boolean_variable soft");
ABSL_FLAG(std::string, params, "", "Sat parameters.");
namespace operations_research {
namespace sat {
// Checks that all pairwise distances are unique and returns all violators
void CheckConstraintViolators(absl::Span<const int64_t> vars,
std::vector<int>* const violators) {
int dim = vars.size();
// Check that all indices are unique
for (int i = 0; i < dim; ++i) {
for (int k = i + 1; k < dim; ++k) {
if (vars[i] == vars[k]) {
violators->push_back(i);
violators->push_back(k);
}
}
}
// Check that all differences are unique for each level
for (int level = 1; level < dim; ++level) {
for (int i = 0; i < dim - level; ++i) {
const int difference = vars[i + level] - vars[i];
for (int k = i + 1; k < dim - level; ++k) {
if (difference == vars[k + level] - vars[k]) {
violators->push_back(k + level);
violators->push_back(k);
violators->push_back(i + level);
violators->push_back(i);
}
}
}
}
}
// Check that all pairwise differences are unique
bool CheckCostas(absl::Span<const int64_t> vars) {
std::vector<int> violators;
CheckConstraintViolators(vars, &violators);
return violators.empty();
}
// Computes a Costas Array.
void CostasHard(const int dim) {
CpModelBuilder cp_model;
// create the variables
std::vector<IntVar> vars;
Domain domain(1, dim);
for (int i = 0; i < dim; ++i) {
vars.push_back(
cp_model.NewIntVar(domain).WithName(absl::StrCat("var_", i)));
}
cp_model.AddAllDifferent(vars);
// Check that the pairwise difference is unique
for (int i = 1; i < dim; ++i) {
std::vector<IntVar> subset;
Domain difference_domain(-dim, dim);
for (int j = 0; j < dim - i; ++j) {
IntVar diff = cp_model.NewIntVar(difference_domain);
subset.push_back(diff);
cp_model.AddEquality(diff, vars[j + i] - vars[j]);
}
cp_model.AddAllDifferent(subset);
}
Model model;
if (!absl::GetFlag(FLAGS_params).empty()) {
model.Add(NewSatParameters(absl::GetFlag(FLAGS_params)));
}
const CpSolverResponse response = SolveCpModel(cp_model.Build(), &model);
if (response.status() == CpSolverStatus::OPTIMAL) {
std::vector<int64_t> costas_matrix;
std::string output;
for (int n = 0; n < dim; ++n) {
const int64_t v = SolutionIntegerValue(response, vars[n]);
costas_matrix.push_back(v);
absl::StrAppendFormat(&output, "%3lld", v);
}
LOG(INFO) << output << " (" << response.wall_time() << " s)";
CHECK(CheckCostas(costas_matrix))
<< ": Solution is not a valid Costas Matrix.";
} else {
LOG(INFO) << "No solution found.";
}
}
// Computes a Costas Array.
void CostasBool(const int dim) {
CpModelBuilder cp_model;
// create the variables
std::vector<std::vector<BoolVar>> vars(dim);
std::vector<std::vector<BoolVar>> transposed_vars(dim);
for (int i = 0; i < dim; ++i) {
for (int j = 0; j < dim; ++j) {
const BoolVar var = cp_model.NewBoolVar();
vars[i].push_back(var);
transposed_vars[j].push_back(var);
}
}
for (int i = 0; i < dim; ++i) {
cp_model.AddEquality(LinearExpr::Sum(vars[i]), 1);
cp_model.AddEquality(LinearExpr::Sum(transposed_vars[i]), 1);
}
// Check that the pairwise difference is unique
for (int step = 1; step < dim; ++step) {
for (int diff = 1; diff < dim - 1; ++diff) {
std::vector<BoolVar> positive_diffs;
std::vector<BoolVar> negative_diffs;
for (int var = 0; var < dim - step; ++var) {
for (int value = 0; value < dim - diff; ++value) {
const BoolVar pos = cp_model.NewBoolVar();
const BoolVar neg = cp_model.NewBoolVar();
positive_diffs.push_back(pos);
negative_diffs.push_back(neg);
cp_model.AddBoolOr(
{~vars[var][value], ~vars[var + step][value + diff], pos});
cp_model.AddBoolOr(
{~vars[var][value + diff], ~vars[var + step][value], neg});
}
}
cp_model.AddLessOrEqual(LinearExpr::Sum(positive_diffs), 1);
cp_model.AddLessOrEqual(LinearExpr::Sum(negative_diffs), 1);
}
}
Model model;
if (!absl::GetFlag(FLAGS_params).empty()) {
model.Add(NewSatParameters(absl::GetFlag(FLAGS_params)));
}
const CpSolverResponse response = SolveCpModel(cp_model.Build(), &model);
if (response.status() == CpSolverStatus::OPTIMAL) {
std::vector<int64_t> costas_matrix;
std::string output;
for (int n = 0; n < dim; ++n) {
for (int v = 0; v < dim; ++v) {
if (SolutionBooleanValue(response, vars[n][v])) {
costas_matrix.push_back(v + 1);
absl::StrAppendFormat(&output, "%3lld", v + 1);
break;
}
}
}
LOG(INFO) << output << " (" << response.wall_time() << " s)";
CHECK(CheckCostas(costas_matrix))
<< ": Solution is not a valid Costas Matrix.";
} else {
LOG(INFO) << "No solution found.";
}
}
// Computes a Costas Array with a minimization objective.
void CostasBoolSoft(const int dim) {
CpModelBuilder cp_model;
// create the variables
std::vector<std::vector<BoolVar>> vars(dim);
std::vector<std::vector<BoolVar>> transposed_vars(dim);
for (int i = 0; i < dim; ++i) {
for (int j = 0; j < dim; ++j) {
const BoolVar var = cp_model.NewBoolVar();
vars[i].push_back(var);
transposed_vars[j].push_back(var);
}
}
for (int i = 0; i < dim; ++i) {
cp_model.AddEquality(LinearExpr::Sum(vars[i]), 1);
cp_model.AddEquality(LinearExpr::Sum(transposed_vars[i]), 1);
}
std::vector<IntVar> all_violations;
// Check that the pairwise difference is unique
for (int step = 1; step < dim; ++step) {
for (int diff = 1; diff < dim - 1; ++diff) {
std::vector<BoolVar> positive_diffs;
std::vector<BoolVar> negative_diffs;
for (int var = 0; var < dim - step; ++var) {
for (int value = 0; value < dim - diff; ++value) {
const BoolVar pos = cp_model.NewBoolVar();
const BoolVar neg = cp_model.NewBoolVar();
positive_diffs.push_back(pos);
negative_diffs.push_back(neg);
cp_model.AddBoolOr(
{~vars[var][value], ~vars[var + step][value + diff], pos});
cp_model.AddBoolOr(
{~vars[var][value + diff], ~vars[var + step][value], neg});
}
}
const IntVar pos_var =
cp_model.NewIntVar(Domain(0, positive_diffs.size()));
const IntVar neg_var =
cp_model.NewIntVar(Domain(0, negative_diffs.size()));
cp_model.AddGreaterOrEqual(pos_var, LinearExpr::Sum(positive_diffs) - 1);
cp_model.AddGreaterOrEqual(neg_var, LinearExpr::Sum(negative_diffs) - 1);
all_violations.push_back(pos_var);
all_violations.push_back(neg_var);
}
}
cp_model.Minimize(LinearExpr::Sum(all_violations));
Model model;
if (!absl::GetFlag(FLAGS_params).empty()) {
model.Add(NewSatParameters(absl::GetFlag(FLAGS_params)));
}
const CpSolverResponse response = SolveCpModel(cp_model.Build(), &model);
if (response.status() == CpSolverStatus::OPTIMAL) {
std::vector<int64_t> costas_matrix;
std::string output;
for (int n = 0; n < dim; ++n) {
for (int v = 0; v < dim; ++v) {
if (SolutionBooleanValue(response, vars[n][v])) {
costas_matrix.push_back(v + 1);
absl::StrAppendFormat(&output, "%3lld", v + 1);
break;
}
}
}
LOG(INFO) << output << " (" << response.wall_time() << " s)";
CHECK(CheckCostas(costas_matrix))
<< ": Solution is not a valid Costas Matrix.";
} else {
LOG(INFO) << "No solution found.";
}
}
} // namespace sat
} // namespace operations_research
int main(int argc, char** argv) {
absl::SetFlag(&FLAGS_stderrthreshold, 0);
InitGoogle(argv[0], &argc, &argv, true);
int min = 1;
int max = 10;
if (absl::GetFlag(FLAGS_minsize) != 0) {
min = absl::GetFlag(FLAGS_minsize);
if (absl::GetFlag(FLAGS_maxsize) != 0) {
max = absl::GetFlag(FLAGS_maxsize);
} else {
max = min;
}
}
for (int size = min; size <= max; ++size) {
LOG(INFO) << "Computing Costas Array for dim = " << size;
if (absl::GetFlag(FLAGS_model) == 1) {
operations_research::sat::CostasHard(size);
} else if (absl::GetFlag(FLAGS_model) == 2) {
operations_research::sat::CostasBool(size);
} else if (absl::GetFlag(FLAGS_model) == 3) {
operations_research::sat::CostasBoolSoft(size);
}
}
return EXIT_SUCCESS;
}