forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcp_model_postsolve_test.cc
356 lines (320 loc) · 11.2 KB
/
cp_model_postsolve_test.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2010-2025 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.
#include "ortools/sat/cp_model_postsolve.h"
#include <cstdint>
#include <vector>
#include "gtest/gtest.h"
#include "ortools/base/gmock.h"
#include "ortools/base/parse_test_proto.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/util/logging.h"
#include "ortools/util/sorted_interval_list.h"
namespace operations_research {
namespace sat {
namespace {
using ::google::protobuf::contrib::parse_proto::ParseTestProto;
// Note that the postsolve is already tested in many of our solver tests or
// random presolve tests. We just have a small unit test here.
TEST(PostsolveResponseTest, BasicExample) {
// Fixing z will allow the postsolve code to reconstruct all values.
const CpModelProto mapping_proto = ParseTestProto(R"pb(
variables { name: 'x' domain: 0 domain: 10 }
variables { name: 'y' domain: 0 domain: 10 }
variables { name: 'z' domain: 0 domain: 10 }
constraints {
linear {
vars: [ 0, 1, 2 ]
coeffs: [ 1, 2, -3 ]
domain: [ 5, 5 ]
}
}
constraints {
linear {
vars: [ 1, 2 ]
coeffs: [ 3, -1 ]
domain: [ 5, 5 ]
}
}
)pb");
std::vector<int64_t> solution = {1};
std::vector<int> postsolve_mapping = {2}; // The solution fix z.
PostsolveResponse(/*num_variables_in_original_model=*/3, mapping_proto,
postsolve_mapping, &solution);
// x + 2y - 3z = 5
// 3y - z = 5
// z = 1
EXPECT_THAT(solution, ::testing::ElementsAre(4, 2, 1));
}
TEST(PostsolveResponseTest, ExactlyOneExample1) {
const CpModelProto mapping_proto = ParseTestProto(R"pb(
variables { name: 'x' domain: 0 domain: 1 }
variables { name: 'y' domain: 0 domain: 1 }
variables { name: 'z' domain: 0 domain: 1 }
constraints { exactly_one { literals: [ 0, 1, 2 ] } }
)pb");
std::vector<int64_t> solution = {1};
std::vector<int> postsolve_mapping = {2}; // The solution fix z.
PostsolveResponse(/*num_variables_in_original_model=*/3, mapping_proto,
postsolve_mapping, &solution);
EXPECT_THAT(solution, ::testing::ElementsAre(0, 0, 1));
}
TEST(PostsolveResponseTest, ExactlyOneExample2) {
const CpModelProto mapping_proto = ParseTestProto(R"pb(
variables { name: 'x' domain: 0 domain: 1 }
variables { name: 'y' domain: 0 domain: 1 }
variables { name: 'z' domain: 0 domain: 1 }
constraints { exactly_one { literals: [ 0, 1, 2 ] } }
)pb");
std::vector<int64_t> solution = {0};
std::vector<int> postsolve_mapping = {2}; // The solution fix z.
PostsolveResponse(/*num_variables_in_original_model=*/3, mapping_proto,
postsolve_mapping, &solution);
// One variable is set to one.
EXPECT_THAT(solution, ::testing::ElementsAre(0, 1, 0));
}
TEST(PostsolveResponseTest, FixedTarget) {
// Fixing z will allow the postsolve code to reconstruct all values.
const CpModelProto mapping_proto = ParseTestProto(R"pb(
variables {
name: 'index'
domain: [ 0, 1 ]
}
variables {
name: 'a'
domain: [ 5, 5 ]
}
variables {
name: 'b'
domain: [ 7, 7 ]
}
variables {
name: 'target'
domain: [ 7, 7 ]
}
constraints {
element {
linear_index { vars: 0 coeffs: 1 }
linear_target { vars: 3 coeffs: 1 }
exprs { vars: 1, coeffs: 1 }
exprs { vars: 2, coeffs: 1 }
}
}
)pb");
std::vector<int64_t> solution;
std::vector<int> postsolve_mapping = {};
PostsolveResponse(/*num_variables_in_original_model=*/4, mapping_proto,
postsolve_mapping, &solution);
EXPECT_THAT(solution, ::testing::ElementsAre(1, 5, 7, 7));
}
TEST(PostsolveResponseTest, FixedIndex) {
const CpModelProto mapping_proto = ParseTestProto(R"pb(
variables { domain: [ 7, 7 ] }
variables { domain: [ 3, 3 ] }
variables { domain: [ 0, 129 ] }
variables { domain: [ 2, 2 ] }
constraints {
element {
linear_index { vars: 3 coeffs: 1 }
linear_target { vars: 2 coeffs: 1 }
exprs { vars: 0, coeffs: 1 }
exprs { vars: 1, coeffs: 1 }
exprs { vars: 0, coeffs: 1 }
}
}
)pb");
std::vector<int64_t> solution;
std::vector<int> postsolve_mapping = {};
PostsolveResponse(/*num_variables_in_original_model=*/4, mapping_proto,
postsolve_mapping, &solution);
EXPECT_THAT(solution, ::testing::ElementsAre(7, 3, 7, 2));
}
// Note that our postolve code is "limited" when it come to solving a single
// linear equation since we should only encounter "simple" case.
TEST(PostsolveResponseTest, TrickyLinearCase) {
// The equation is 2x + y = z
//
// It mostly work all the time, except if we decide to make z - y not a
// multiple of two. This is not necessarily detected by our presolve since
// 2 * [0, 124] is too complex to represent. Yet for any value of x and y
// there is a possible z, but the reverse is not true, since y = 1, z = 0 is
// not feasible.
//
// The presolve should deal with that by putting z first so that the
// postsolve code do not fail.
const CpModelProto mapping_proto = ParseTestProto(R"pb(
variables {
name: 'x'
domain: [ 0, 124 ]
}
variables {
name: 'y'
domain: [ 0, 1 ]
}
variables {
name: 'z'
domain: [ 0, 255 ]
}
constraints {
linear {
vars: [ 2, 0, 1 ]
coeffs: [ -1, 2, 1 ]
domain: [ 0, 0 ]
}
}
)pb");
// The likely response (there are many possible).
std::vector<int64_t> solution;
CpSolverResponse response;
response.set_status(OPTIMAL);
std::vector<int> postsolve_mapping;
PostsolveResponse(/*num_variables_in_original_model=*/3, mapping_proto,
postsolve_mapping, &solution);
EXPECT_THAT(solution, ::testing::ElementsAre(0, 0, 0));
}
// This used to fail because we where computing the EXACT domain atteignable
// by the sum of discrete domains, which have a lot of disjoint part.
//
// But our presolve was fine, because adding each of them to the loose rhs
// domain just result in a domain with a small complexity.
TEST(PostsolveResponseTest, ComplexityIssue) {
CpModelProto mapping_proto;
// N variables such that their sum can be and even number. If we try to
// compute the exact domains of their sum, we are quadratic in compexity.
const int num_variables = 30;
for (int i = 0; i < num_variables; ++i) {
IntegerVariableProto* var = mapping_proto.add_variables();
var->add_domain(0);
var->add_domain(0);
const int value = 1 << (1 + i);
var->add_domain(value);
var->add_domain(value);
}
// A linear constraint sum variable in [0, 1e9].
ConstraintProto* ct = mapping_proto.add_constraints();
ct->mutable_linear()->add_domain(0);
ct->mutable_linear()->add_domain(1e9);
for (int i = 0; i < num_variables; ++i) {
ct->mutable_linear()->add_vars(i);
ct->mutable_linear()->add_coeffs(1);
}
// The likely response (there are many possible).
std::vector<int64_t> solution;
std::vector<int> postsolve_mapping;
PostsolveResponse(num_variables, mapping_proto, postsolve_mapping, &solution);
ASSERT_EQ(solution.size(), num_variables);
}
TEST(FillTightenedDomainInResponseTest, BasicBehavior) {
// Original model.
const CpModelProto original_model = ParseTestProto(R"pb(
variables {
name: 'x'
domain: [ 0, 124 ]
}
variables { domain: [ 0, 1 ] }
variables { domain: [ 0, 255 ] }
)pb");
// We might have more variable there.
// Also the domains might be tighter.
const CpModelProto mapping_proto = ParseTestProto(R"pb(
variables { domain: [ 0, 100 ] }
variables { domain: [ 0, 1 ] }
variables { domain: [ 0, 255 ] }
variables { domain: [ 0, 17 ] }
variables { domain: [ 0, 18 ] }
)pb");
// Lets assume the presolved mode contains 3 variables, 2 in common.
std::vector<int> postsolve_mapping{0, 2, 4};
std::vector<Domain> search_bounds{Domain(0, 100), Domain(0, 0), Domain(3, 7)};
// Call the postsolving.
SolverLogger logger;
CpSolverResponse response;
FillTightenedDomainInResponse(original_model, mapping_proto,
postsolve_mapping, search_bounds, &response,
&logger);
// Lets test by constructing a model for easy comparison.
CpModelProto returned_model;
for (const IntegerVariableProto& var : response.tightened_variables()) {
*returned_model.add_variables() = var;
}
const CpModelProto expected_model = ParseTestProto(R"pb(
variables {
name: 'x'
domain: [ 0, 100 ]
} # presolve reduced the domain.
variables { domain: [ 0, 1 ] } # no info.
variables { domain: [ 0, 0 ] } # was fixed by search.
)pb");
EXPECT_THAT(returned_model, testing::EqualsProto(expected_model));
}
TEST(FillTightenedDomainInResponseTest, WithAffine) {
// Original model.
const CpModelProto original_model = ParseTestProto(R"pb(
variables { domain: [ 0, 124 ] }
variables { domain: [ 0, 50 ] }
variables { domain: [ 0, 255 ] }
)pb");
// We might have more variable there.
// Also the domains might be tighter.
const CpModelProto mapping_proto = ParseTestProto(R"pb(
variables { domain: [ 0, 100 ] }
variables { domain: [ 0, 50 ] }
variables { domain: [ 0, 100 ] }
variables { domain: [ 0, 17 ] }
variables { domain: [ 0, 18 ] }
variables { domain: [ 0, 19 ] }
constraints {
linear {
vars: [ 0, 3 ]
coeffs: [ 2, 1 ]
domain: [ 10, 10 ]
}
}
constraints {
linear {
vars: [ 1, 4 ]
coeffs: [ 1, 1 ]
domain: [ 10, 10 ]
}
}
constraints {
linear {
vars: [ 5, 2 ]
coeffs: [ 2, 1 ]
domain: [ 10, 10 ]
}
}
)pb");
std::vector<int> postsolve_mapping{3, 4, 5};
std::vector<Domain> search_bounds{Domain(0, 20), Domain(0, 20), Domain(3, 5)};
// Call the postsolving.
SolverLogger logger;
logger.EnableLogging(true);
CpSolverResponse response;
FillTightenedDomainInResponse(original_model, mapping_proto,
postsolve_mapping, search_bounds, &response,
&logger);
// Lets test by constructing a model for easy comparison.
CpModelProto returned_model;
for (const IntegerVariableProto& var : response.tightened_variables()) {
*returned_model.add_variables() = var;
}
const CpModelProto expected_model = ParseTestProto(R"pb(
variables { domain: [ 0, 5 ] } # 2 * v = 10 - [0, 17]
variables { domain: [ 0, 10 ] } # v = 10 - [0, 18]
variables { domain: [ 0, 4 ] } # v = 10 - 2 * [3, 5]
)pb");
EXPECT_THAT(returned_model, testing::EqualsProto(expected_model));
}
} // namespace
} // namespace sat
} // namespace operations_research