Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
table.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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
14#include "ortools/sat/table.h"
15
16#include <functional>
17#include <utility>
18#include <vector>
19
20#include "absl/container/flat_hash_map.h"
21#include "absl/log/check.h"
22#include "absl/types/span.h"
23#include "ortools/sat/model.h"
26
27namespace operations_research {
28namespace sat {
29
30std::function<void(Model*)> LiteralTableConstraint(
31 absl::Span<const std::vector<Literal>> literal_tuples,
32 absl::Span<const Literal> line_literals) {
33 return [=,
34 line_literals =
35 std::vector<Literal>(line_literals.begin(), line_literals.end()),
36 literal_tuples = std::vector<std::vector<Literal>>(
37 literal_tuples.begin(), literal_tuples.end())](Model* model) {
38 CHECK_EQ(literal_tuples.size(), line_literals.size());
39 const int num_tuples = line_literals.size();
40 if (num_tuples == 0) return;
41 const int tuple_size = literal_tuples[0].size();
42 if (tuple_size == 0) return;
43 for (int i = 1; i < num_tuples; ++i) {
44 CHECK_EQ(tuple_size, literal_tuples[i].size());
45 }
46
47 absl::flat_hash_map<LiteralIndex, std::vector<LiteralIndex>>
48 line_literals_per_literal;
49 for (int i = 0; i < num_tuples; ++i) {
50 const LiteralIndex selected_index = line_literals[i].Index();
51 for (const Literal l : literal_tuples[i]) {
52 line_literals_per_literal[l.Index()].push_back(selected_index);
53 }
54 }
55
56 // line_literals[i] == true => literal_tuples[i][j] == true.
57 // literal_tuples[i][j] == false => line_literals[i] == false.
58 for (int i = 0; i < num_tuples; ++i) {
59 const Literal line_is_selected = line_literals[i];
60 for (const Literal lit : literal_tuples[i]) {
61 model->Add(Implication(line_is_selected, lit));
62 }
63 }
64
65 // Exactly one selected literal is true.
66 model->Add(ExactlyOneConstraint(line_literals));
67
68 // If all selected literals of the lines containing a literal are false,
69 // then the literal is false.
70 for (const auto& p : line_literals_per_literal) {
71 std::vector<Literal> clause;
72 for (const auto& index : p.second) {
73 clause.push_back(Literal(index));
74 }
75 clause.push_back(Literal(p.first).Negated());
76 model->Add(ClauseConstraint(clause));
77 }
78 };
79}
80
81} // namespace sat
82} // namespace operations_research
std::function< void(Model *)> ClauseConstraint(absl::Span< const Literal > literals)
Definition sat_solver.h:947
std::function< void(Model *)> LiteralTableConstraint(absl::Span< const std::vector< Literal > > literal_tuples, absl::Span< const Literal > line_literals)
Definition table.cc:30
std::function< void(Model *)> Implication(absl::Span< const Literal > enforcement_literals, IntegerLiteral i)
Definition integer.h:1609
std::function< void(Model *)> ExactlyOneConstraint(absl::Span< const Literal > literals)
Definition sat_solver.h:917
In SWIG mode, we don't want anything besides these top-level includes.