Skip to content

Commit ff5a2c1

Browse files
committed
[CP-SAT] tune the code; improve lrat; work on encodings
1 parent 27ebae0 commit ff5a2c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3486
-2651
lines changed

ortools/sat/BUILD.bazel

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,14 @@ cc_library(
177177
":presolve_context",
178178
":sat_base",
179179
":sat_parameters_cc_proto",
180-
"//ortools/base:logging",
181180
"//ortools/base:protobuf_util",
182181
"//ortools/util:sorted_interval_list",
182+
"@abseil-cpp//absl/cleanup",
183183
"@abseil-cpp//absl/container:flat_hash_map",
184184
"@abseil-cpp//absl/container:flat_hash_set",
185185
"@abseil-cpp//absl/log",
186186
"@abseil-cpp//absl/log:check",
187+
"@abseil-cpp//absl/memory",
187188
"@abseil-cpp//absl/strings",
188189
"@abseil-cpp//absl/types:span",
189190
"@protobuf",
@@ -394,6 +395,7 @@ cc_library(
394395
hdrs = ["presolve_encoding.h"],
395396
deps = [
396397
":cp_model_utils",
398+
":integer_base",
397399
":presolve_context",
398400
"//ortools/base:stl_util",
399401
"//ortools/util:bitset",
@@ -404,6 +406,7 @@ cc_library(
404406
"@abseil-cpp//absl/container:inlined_vector",
405407
"@abseil-cpp//absl/log",
406408
"@abseil-cpp//absl/log:check",
409+
"@abseil-cpp//absl/strings",
407410
"@protobuf",
408411
],
409412
)
@@ -1225,6 +1228,7 @@ cc_library(
12251228
"//ortools/util:sorted_interval_list",
12261229
"//ortools/util:time_limit",
12271230
"@abseil-cpp//absl/base:core_headers",
1231+
"@abseil-cpp//absl/base:log_severity",
12281232
"@abseil-cpp//absl/container:btree",
12291233
"@abseil-cpp//absl/container:flat_hash_map",
12301234
"@abseil-cpp//absl/container:flat_hash_set",
@@ -1375,7 +1379,6 @@ cc_library(
13751379
":util",
13761380
":var_domination",
13771381
":variable_expand",
1378-
"//ortools/base",
13791382
"//ortools/base:mathutil",
13801383
"//ortools/base:protobuf_util",
13811384
"//ortools/base:stl_util",
@@ -1396,7 +1399,6 @@ cc_library(
13961399
"@abseil-cpp//absl/container:btree",
13971400
"@abseil-cpp//absl/container:flat_hash_map",
13981401
"@abseil-cpp//absl/container:flat_hash_set",
1399-
"@abseil-cpp//absl/container:inlined_vector",
14001402
"@abseil-cpp//absl/flags:flag",
14011403
"@abseil-cpp//absl/hash",
14021404
"@abseil-cpp//absl/log",
@@ -1685,7 +1687,6 @@ cc_library(
16851687
"@abseil-cpp//absl/cleanup",
16861688
"@abseil-cpp//absl/container:btree",
16871689
"@abseil-cpp//absl/container:flat_hash_set",
1688-
"@abseil-cpp//absl/container:inlined_vector",
16891690
"@abseil-cpp//absl/log",
16901691
"@abseil-cpp//absl/log:check",
16911692
"@abseil-cpp//absl/log:vlog_is_on",
@@ -2455,6 +2456,7 @@ cc_test(
24552456
"//ortools/base:logging",
24562457
"//ortools/util:sorted_interval_list",
24572458
"//ortools/util:strong_integers",
2459+
"@abseil-cpp//absl/log",
24582460
"@abseil-cpp//absl/log:check",
24592461
"@abseil-cpp//absl/types:span",
24602462
"@google_benchmark//:benchmark",
@@ -2599,6 +2601,7 @@ cc_library(
25992601
"//ortools/util:bitset",
26002602
"//ortools/util:sort",
26012603
"//ortools/util:strong_integers",
2604+
"//ortools/util:time_limit",
26022605
"@abseil-cpp//absl/algorithm:container",
26032606
"@abseil-cpp//absl/container:btree",
26042607
"@abseil-cpp//absl/container:flat_hash_map",
@@ -2917,6 +2920,7 @@ cc_library(
29172920
":sat_solver",
29182921
":scheduling_cuts",
29192922
":scheduling_helpers",
2923+
":synchronization",
29202924
":util",
29212925
"//ortools/base:logging",
29222926
"//ortools/base:mathutil",
@@ -2925,10 +2929,12 @@ cc_library(
29252929
"//ortools/util:logging",
29262930
"//ortools/util:sorted_interval_list",
29272931
"//ortools/util:strong_integers",
2932+
"@abseil-cpp//absl/algorithm:container",
29282933
"@abseil-cpp//absl/base:core_headers",
29292934
"@abseil-cpp//absl/container:btree",
29302935
"@abseil-cpp//absl/container:flat_hash_map",
29312936
"@abseil-cpp//absl/container:flat_hash_set",
2937+
"@abseil-cpp//absl/log",
29322938
"@abseil-cpp//absl/log:check",
29332939
"@abseil-cpp//absl/types:span",
29342940
"@protobuf",
@@ -4369,10 +4375,10 @@ cc_library(
43694375
":model",
43704376
":sat_base",
43714377
":synchronization",
4372-
":util",
43734378
"//ortools/base:strong_vector",
43744379
"//ortools/util:bitset",
43754380
"@abseil-cpp//absl/algorithm:container",
4381+
"@abseil-cpp//absl/container:btree",
43764382
"@abseil-cpp//absl/container:flat_hash_map",
43774383
"@abseil-cpp//absl/container:flat_hash_set",
43784384
"@abseil-cpp//absl/log",
@@ -4399,18 +4405,13 @@ cc_library(
43994405
srcs = ["lrat_proof_handler.cc"],
44004406
hdrs = ["lrat_proof_handler.h"],
44014407
deps = [
4402-
":drat_checker",
4403-
":drat_writer",
44044408
":lrat_cc_proto",
44054409
":lrat_checker",
44064410
":model",
44074411
":recordio",
44084412
":sat_base",
44094413
":synchronization",
4410-
":util",
4411-
"//ortools/base:file",
44124414
"//ortools/base:intops",
4413-
"//ortools/base:timer",
44144415
"@abseil-cpp//absl/container:flat_hash_map",
44154416
"@abseil-cpp//absl/container:flat_hash_set",
44164417
"@abseil-cpp//absl/flags:flag",
@@ -4442,6 +4443,8 @@ cc_test(
44424443
":model",
44434444
":sat_base",
44444445
"//ortools/base:gmock_main",
4446+
"@abseil-cpp//absl/memory",
4447+
"@abseil-cpp//absl/strings",
44454448
"@abseil-cpp//absl/types:span",
44464449
],
44474450
)

ortools/sat/all_different.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "ortools/sat/sat_solver.h"
3434
#include "ortools/util/sort.h"
3535
#include "ortools/util/strong_integers.h"
36+
#include "ortools/util/time_limit.h"
3637

3738
namespace operations_research {
3839
namespace sat {
@@ -405,7 +406,7 @@ bool AllDifferentConstraint::Propagate() {
405406
}
406407
}
407408

408-
return trail_->EnqueueWithStoredReason(kNoClauseId, x_lit.Negated());
409+
return trail_->EnqueueWithStoredReason(x_lit.Negated(), kNullClausePtr);
409410
}
410411
}
411412
}
@@ -417,6 +418,7 @@ AllDifferentBoundsPropagator::AllDifferentBoundsPropagator(
417418
absl::Span<const Literal> enforcement_literals,
418419
absl::Span<const AffineExpression> expressions, Model* model)
419420
: integer_trail_(*model->GetOrCreate<IntegerTrail>()),
421+
time_limit_(model->GetOrCreate<TimeLimit>()),
420422
enforcement_helper_(*model->GetOrCreate<EnforcementHelper>()) {
421423
CHECK(!expressions.empty());
422424

@@ -489,8 +491,15 @@ bool AllDifferentBoundsPropagator::PropagateLowerBounds() {
489491
entry.lb = integer_trail_.LowerBound(entry.expr);
490492
entry.ub = integer_trail_.UpperBound(entry.expr);
491493
}
492-
IncrementalSort(bounds_.begin(), bounds_.end(),
493-
[](CachedBounds a, CachedBounds b) { return a.lb < b.lb; });
494+
495+
// TODO(user): The running time can be dominated by this sort. For the
496+
// "permutation" case where we have as many bounds has possible lb, maybe a
497+
// radix sort is more efficient.
498+
time_limit_->AdvanceDeterministicTime(static_cast<double>(bounds_.size()) *
499+
1e-8);
500+
IncrementalSort(
501+
bounds_.begin(), bounds_.end(),
502+
[](const CachedBounds& a, const CachedBounds& b) { return a.lb < b.lb; });
494503

495504
// We will split the affine epressions in vars sorted by lb in contiguous
496505
// subset with index of the form [start, start + num_in_window).

ortools/sat/all_different.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "ortools/sat/util.h"
3232
#include "ortools/util/bitset.h"
3333
#include "ortools/util/strong_integers.h"
34+
#include "ortools/util/time_limit.h"
3435

3536
namespace operations_research {
3637
namespace sat {
@@ -215,6 +216,8 @@ class AllDifferentBoundsPropagator : public PropagatorInterface {
215216
IntegerValue GetValue(int index) const { return base_ + IntegerValue(index); }
216217

217218
const IntegerTrail& integer_trail_;
219+
TimeLimit* time_limit_;
220+
218221
EnforcementHelper& enforcement_helper_;
219222
EnforcementId enforcement_id_;
220223

ortools/sat/circuit.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ bool CircuitPropagator::Propagate() {
308308
std::vector<Literal>* reason = trail_.GetEmptyVectorToStoreReason();
309309
FillReasonForPath(start_node, reason);
310310
enforcement_helper_.AddEnforcementReason(enforcement_id_, reason);
311-
if (!trail_.EnqueueWithStoredReason(kNoClauseId, literal.Negated())) {
311+
if (!trail_.EnqueueWithStoredReason(literal.Negated(),
312+
kNullClausePtr)) {
312313
return false;
313314
}
314315
}
@@ -358,7 +359,7 @@ bool CircuitPropagator::Propagate() {
358359
reason->push_back(Literal(extra_reason));
359360
}
360361
const bool ok =
361-
trail_.EnqueueWithStoredReason(kNoClauseId, literal.Negated());
362+
trail_.EnqueueWithStoredReason(literal.Negated(), kNullClausePtr);
362363
if (!ok) return false;
363364
continue;
364365
}
@@ -399,7 +400,8 @@ bool CircuitPropagator::Propagate() {
399400
std::vector<Literal>* reason = trail_.GetEmptyVectorToStoreReason();
400401
FillReasonForPath(start_node, reason);
401402
enforcement_helper_.AddEnforcementReason(enforcement_id_, reason);
402-
const bool ok = trail_.EnqueueWithStoredReason(kNoClauseId, literal);
403+
const bool ok =
404+
trail_.EnqueueWithStoredReason(literal, kNullClausePtr);
403405
if (!ok) return false;
404406
} else {
405407
trail_.EnqueueWithSameReasonAs(literal, variable_with_same_reason);
@@ -671,7 +673,7 @@ bool CircuitCoveringPropagator::Propagate() {
671673
auto* reason = trail_->GetEmptyVectorToStoreReason();
672674
FillFixedPathInReason(start, end, reason);
673675
const bool ok = trail_->EnqueueWithStoredReason(
674-
kNoClauseId, graph_[end][start].Negated());
676+
graph_[end][start].Negated(), kNullClausePtr);
675677
if (!ok) return false;
676678
}
677679
}

0 commit comments

Comments
 (0)