2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2016-09-22 13:55:16 +02:00
|
|
|
// 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.
|
|
|
|
|
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/sat/table.h"
|
2016-09-22 13:55:16 +02:00
|
|
|
|
2022-02-15 18:00:11 +01:00
|
|
|
#include <functional>
|
2017-07-27 11:28:55 -07:00
|
|
|
#include <utility>
|
2022-02-15 18:00:11 +01:00
|
|
|
#include <vector>
|
2016-10-12 12:07:20 -07:00
|
|
|
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "absl/container/flat_hash_map.h"
|
2023-05-24 11:42:11 +02:00
|
|
|
#include "absl/log/check.h"
|
2024-11-28 15:34:01 +01:00
|
|
|
#include "absl/types/span.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/sat/model.h"
|
2019-04-16 09:25:34 -07:00
|
|
|
#include "ortools/sat/sat_base.h"
|
2017-07-27 11:28:55 -07:00
|
|
|
#include "ortools/sat/sat_solver.h"
|
2016-09-22 13:55:16 +02:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
namespace sat {
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
std::function<void(Model*)> LiteralTableConstraint(
|
2024-11-28 15:34:01 +01:00
|
|
|
absl::Span<const std::vector<Literal>> literal_tuples,
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<Literal>& line_literals) {
|
2024-11-28 15:34:01 +01:00
|
|
|
return [=, literal_tuples = std::vector<std::vector<Literal>>(
|
|
|
|
|
literal_tuples.begin(), literal_tuples.end())](Model* model) {
|
2020-10-22 23:36:58 +02:00
|
|
|
CHECK_EQ(literal_tuples.size(), line_literals.size());
|
2017-03-28 16:11:06 +02:00
|
|
|
const int num_tuples = line_literals.size();
|
2020-10-22 23:36:58 +02:00
|
|
|
if (num_tuples == 0) return;
|
2017-03-28 16:11:06 +02:00
|
|
|
const int tuple_size = literal_tuples[0].size();
|
2020-10-22 23:36:58 +02:00
|
|
|
if (tuple_size == 0) return;
|
2017-03-28 16:11:06 +02:00
|
|
|
for (int i = 1; i < num_tuples; ++i) {
|
|
|
|
|
CHECK_EQ(tuple_size, literal_tuples[i].size());
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 13:42:36 +01:00
|
|
|
absl::flat_hash_map<LiteralIndex, std::vector<LiteralIndex>>
|
2018-02-16 17:08:01 +01:00
|
|
|
line_literals_per_literal;
|
2017-03-28 16:11:06 +02:00
|
|
|
for (int i = 0; i < num_tuples; ++i) {
|
|
|
|
|
const LiteralIndex selected_index = line_literals[i].Index();
|
|
|
|
|
for (const Literal l : literal_tuples[i]) {
|
|
|
|
|
line_literals_per_literal[l.Index()].push_back(selected_index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// line_literals[i] == true => literal_tuples[i][j] == true.
|
|
|
|
|
// literal_tuples[i][j] == false => line_literals[i] == false.
|
|
|
|
|
for (int i = 0; i < num_tuples; ++i) {
|
|
|
|
|
const Literal line_is_selected = line_literals[i];
|
|
|
|
|
for (const Literal lit : literal_tuples[i]) {
|
|
|
|
|
model->Add(Implication(line_is_selected, lit));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exactly one selected literal is true.
|
|
|
|
|
model->Add(ExactlyOneConstraint(line_literals));
|
|
|
|
|
|
|
|
|
|
// If all selected literals of the lines containing a literal are false,
|
|
|
|
|
// then the literal is false.
|
2020-10-28 13:42:36 +01:00
|
|
|
for (const auto& p : line_literals_per_literal) {
|
2017-03-28 16:11:06 +02:00
|
|
|
std::vector<Literal> clause;
|
2020-10-28 13:42:36 +01:00
|
|
|
for (const auto& index : p.second) {
|
2017-03-28 16:11:06 +02:00
|
|
|
clause.push_back(Literal(index));
|
|
|
|
|
}
|
|
|
|
|
clause.push_back(Literal(p.first).Negated());
|
|
|
|
|
model->Add(ClauseConstraint(clause));
|
|
|
|
|
}
|
2020-10-22 23:36:58 +02:00
|
|
|
};
|
2017-03-28 16:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace sat
|
|
|
|
|
} // namespace operations_research
|