OR-Tools  9.2
sat/table.cc
Go to the documentation of this file.
1// Copyright 2010-2021 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 <algorithm>
17#include <cstdint>
18#include <limits>
19#include <memory>
20#include <set>
21#include <utility>
22
23#include "absl/container/flat_hash_map.h"
24#include "absl/container/flat_hash_set.h"
25#include "absl/strings/str_cat.h"
26#include "absl/strings/str_join.h"
33#include "ortools/sat/util.h"
35
36namespace operations_research {
37namespace sat {
38
39std::function<void(Model*)> LiteralTableConstraint(
40 const std::vector<std::vector<Literal>>& literal_tuples,
41 const std::vector<Literal>& line_literals) {
42 return [=](Model* model) {
43 CHECK_EQ(literal_tuples.size(), line_literals.size());
44 const int num_tuples = line_literals.size();
45 if (num_tuples == 0) return;
46 const int tuple_size = literal_tuples[0].size();
47 if (tuple_size == 0) return;
48 for (int i = 1; i < num_tuples; ++i) {
49 CHECK_EQ(tuple_size, literal_tuples[i].size());
50 }
51
52 absl::flat_hash_map<LiteralIndex, std::vector<LiteralIndex>>
53 line_literals_per_literal;
54 for (int i = 0; i < num_tuples; ++i) {
55 const LiteralIndex selected_index = line_literals[i].Index();
56 for (const Literal l : literal_tuples[i]) {
57 line_literals_per_literal[l.Index()].push_back(selected_index);
58 }
59 }
60
61 // line_literals[i] == true => literal_tuples[i][j] == true.
62 // literal_tuples[i][j] == false => line_literals[i] == false.
63 for (int i = 0; i < num_tuples; ++i) {
64 const Literal line_is_selected = line_literals[i];
65 for (const Literal lit : literal_tuples[i]) {
66 model->Add(Implication(line_is_selected, lit));
67 }
68 }
69
70 // Exactly one selected literal is true.
71 model->Add(ExactlyOneConstraint(line_literals));
72
73 // If all selected literals of the lines containing a literal are false,
74 // then the literal is false.
75 for (const auto& p : line_literals_per_literal) {
76 std::vector<Literal> clause;
77 for (const auto& index : p.second) {
78 clause.push_back(Literal(index));
79 }
80 clause.push_back(Literal(p.first).Negated());
81 model->Add(ClauseConstraint(clause));
82 }
83 };
84}
85
86} // namespace sat
87} // namespace operations_research
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:702
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
GRBmodel * model
std::function< void(Model *)> ClauseConstraint(absl::Span< const Literal > literals)
Definition: sat_solver.h:906
std::function< void(Model *)> LiteralTableConstraint(const std::vector< std::vector< Literal > > &literal_tuples, const std::vector< Literal > &line_literals)
Definition: sat/table.cc:39
std::function< void(Model *)> Implication(const std::vector< Literal > &enforcement_literals, IntegerLiteral i)
Definition: integer.h:1724
std::function< void(Model *)> ExactlyOneConstraint(const std::vector< Literal > &literals)
Definition: sat_solver.h:878
Collection of objects used to extend the Constraint Solver library.
int index
Definition: pack.cc:509