OR-Tools  8.2
drat_proof_handler.cc
Go to the documentation of this file.
1// Copyright 2010-2018 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
15
16#include <algorithm>
17
18#include "absl/memory/memory.h"
22
23namespace operations_research {
24namespace sat {
25
27 : variable_index_(0), drat_checker_(new DratChecker()) {}
28
29DratProofHandler::DratProofHandler(bool in_binary_format, File* output,
30 bool check)
31 : variable_index_(0),
32 drat_writer_(new DratWriter(in_binary_format, output)) {
33 if (check) {
34 drat_checker_ = absl::make_unique<DratChecker>();
35 }
36}
37
41 for (BooleanVariable v(0); v < mapping.size(); ++v) {
42 const BooleanVariable image = mapping[v];
43 if (image != kNoBooleanVariable) {
44 if (image >= new_mapping.size())
45 new_mapping.resize(image.value() + 1, kNoBooleanVariable);
46 CHECK_EQ(new_mapping[image], kNoBooleanVariable);
47 new_mapping[image] =
48 v < reverse_mapping_.size() ? reverse_mapping_[v] : v;
49 CHECK_NE(new_mapping[image], kNoBooleanVariable);
50 }
51 }
52 std::swap(new_mapping, reverse_mapping_);
53}
54
55void DratProofHandler::SetNumVariables(int num_variables) {
56 CHECK_GE(num_variables, reverse_mapping_.size());
57 while (reverse_mapping_.size() < num_variables) {
58 reverse_mapping_.push_back(BooleanVariable(variable_index_++));
59 }
60}
61
63 reverse_mapping_.push_back(BooleanVariable(variable_index_++));
64}
65
66void DratProofHandler::AddProblemClause(absl::Span<const Literal> clause) {
67 if (drat_checker_ != nullptr) {
68 drat_checker_->AddProblemClause(clause);
69 }
70}
71
72void DratProofHandler::AddClause(absl::Span<const Literal> clause) {
73 MapClause(clause);
74 if (drat_checker_ != nullptr) {
75 drat_checker_->AddInferedClause(values_);
76 }
77 if (drat_writer_ != nullptr) {
78 drat_writer_->AddClause(values_);
79 }
80}
81
82void DratProofHandler::DeleteClause(absl::Span<const Literal> clause) {
83 MapClause(clause);
84 if (drat_checker_ != nullptr) {
85 drat_checker_->DeleteClause(values_);
86 }
87 if (drat_writer_ != nullptr) {
88 drat_writer_->DeleteClause(values_);
89 }
90}
91
92DratChecker::Status DratProofHandler::Check(double max_time_in_seconds) {
93 if (drat_checker_ != nullptr) {
94 // The empty clause is not explicitly added by the solver.
95 drat_checker_->AddInferedClause({});
96 return drat_checker_->Check(max_time_in_seconds);
97 }
98 return DratChecker::Status::UNKNOWN;
99}
100
101void DratProofHandler::MapClause(absl::Span<const Literal> clause) {
102 values_.clear();
103 for (const Literal l : clause) {
104 CHECK_LT(l.Variable(), reverse_mapping_.size());
105 const Literal original_literal =
106 Literal(reverse_mapping_[l.Variable()], l.IsPositive());
107 values_.push_back(original_literal);
108 }
109
110 // The sorting is such that new variables appear first. This is important for
111 // BVA since DRAT-trim only check the RAT property with respect to the first
112 // variable of the clause.
113 std::sort(values_.begin(), values_.end(), [](Literal a, Literal b) {
114 return std::abs(a.SignedValue()) > std::abs(b.SignedValue());
115 });
116}
117
118} // namespace sat
119} // namespace operations_research
#define CHECK_LT(val1, val2)
Definition: base/logging.h:700
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:697
#define CHECK_GE(val1, val2)
Definition: base/logging.h:701
#define CHECK_NE(val1, val2)
Definition: base/logging.h:698
Definition: base/file.h:32
void resize(size_type new_size)
size_type size() const
void push_back(const value_type &x)
void DeleteClause(absl::Span< const Literal > clause)
DratChecker::Status Check(double max_time_in_seconds)
void AddProblemClause(absl::Span< const Literal > clause)
void ApplyMapping(const absl::StrongVector< BooleanVariable, BooleanVariable > &mapping)
void AddClause(absl::Span< const Literal > clause)
const BooleanVariable kNoBooleanVariable(-1)
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...