Google OR-Tools v9.11
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-2024 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 "ortools/sat/model.h"
26
27namespace operations_research {
28namespace sat {
29
30std::function<void(Model*)> LiteralTableConstraint(
31 const std::vector<std::vector<Literal>>& literal_tuples,
32 const std::vector<Literal>& line_literals) {
33 return [=](Model* model) {
34 CHECK_EQ(literal_tuples.size(), line_literals.size());
35 const int num_tuples = line_literals.size();
36 if (num_tuples == 0) return;
37 const int tuple_size = literal_tuples[0].size();
38 if (tuple_size == 0) return;
39 for (int i = 1; i < num_tuples; ++i) {
40 CHECK_EQ(tuple_size, literal_tuples[i].size());
41 }
42
43 absl::flat_hash_map<LiteralIndex, std::vector<LiteralIndex>>
44 line_literals_per_literal;
45 for (int i = 0; i < num_tuples; ++i) {
46 const LiteralIndex selected_index = line_literals[i].Index();
47 for (const Literal l : literal_tuples[i]) {
48 line_literals_per_literal[l.Index()].push_back(selected_index);
49 }
50 }
51
52 // line_literals[i] == true => literal_tuples[i][j] == true.
53 // literal_tuples[i][j] == false => line_literals[i] == false.
54 for (int i = 0; i < num_tuples; ++i) {
55 const Literal line_is_selected = line_literals[i];
56 for (const Literal lit : literal_tuples[i]) {
57 model->Add(Implication(line_is_selected, lit));
58 }
59 }
60
61 // Exactly one selected literal is true.
62 model->Add(ExactlyOneConstraint(line_literals));
63
64 // If all selected literals of the lines containing a literal are false,
65 // then the literal is false.
66 for (const auto& p : line_literals_per_literal) {
67 std::vector<Literal> clause;
68 for (const auto& index : p.second) {
69 clause.push_back(Literal(index));
70 }
71 clause.push_back(Literal(p.first).Negated());
72 model->Add(ClauseConstraint(clause));
73 }
74 };
75}
76
77} // namespace sat
78} // namespace operations_research
IntegerValue size
GRBmodel * model
int lit
int index
std::function< void(Model *)> ClauseConstraint(absl::Span< const Literal > literals)
Definition sat_solver.h:933
std::function< void(Model *)> Implication(absl::Span< const Literal > enforcement_literals, IntegerLiteral i)
Definition integer.h:2025
std::function< void(Model *)> LiteralTableConstraint(const std::vector< std::vector< Literal > > &literal_tuples, const std::vector< Literal > &line_literals)
Definition table.cc:30
std::function< void(Model *)> ExactlyOneConstraint(const std::vector< Literal > &literals)
Definition sat_solver.h:905
In SWIG mode, we don't want anything besides these top-level includes.