OR-Tools  9.6
drat_proof_handler.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 #include <cmath>
18 #include <memory>
19 #include <utility>
20 #include <vector>
21 
22 #include "ortools/base/logging.h"
23 #if !defined(__PORTABLE_PLATFORM__)
24 #include "ortools/base/file.h"
25 #endif // !defined(__PORTABLE_PLATFORM__)
26 #include "absl/types/span.h"
30 #include "ortools/sat/sat_base.h"
32 
33 namespace operations_research {
34 namespace sat {
35 
37  : variable_index_(0), drat_checker_(new DratChecker()) {}
38 
39 DratProofHandler::DratProofHandler(bool in_binary_format, File* output,
40  bool check)
41  : variable_index_(0),
42  drat_writer_(new DratWriter(in_binary_format, output)) {
43  if (check) {
44  drat_checker_ = std::make_unique<DratChecker>();
45  }
46 }
47 
51  for (BooleanVariable v(0); v < mapping.size(); ++v) {
52  const BooleanVariable image = mapping[v];
53  if (image != kNoBooleanVariable) {
54  if (image >= new_mapping.size())
55  new_mapping.resize(image.value() + 1, kNoBooleanVariable);
56  CHECK_EQ(new_mapping[image], kNoBooleanVariable);
57  new_mapping[image] =
58  v < reverse_mapping_.size() ? reverse_mapping_[v] : v;
59  CHECK_NE(new_mapping[image], kNoBooleanVariable);
60  }
61  }
62  std::swap(new_mapping, reverse_mapping_);
63 }
64 
65 void DratProofHandler::SetNumVariables(int num_variables) {
66  CHECK_GE(num_variables, reverse_mapping_.size());
67  while (reverse_mapping_.size() < num_variables) {
68  reverse_mapping_.push_back(BooleanVariable(variable_index_++));
69  }
70 }
71 
73  reverse_mapping_.push_back(BooleanVariable(variable_index_++));
74 }
75 
76 void DratProofHandler::AddProblemClause(absl::Span<const Literal> clause) {
77  if (drat_checker_ != nullptr) {
78  drat_checker_->AddProblemClause(clause);
79  }
80 }
81 
82 void DratProofHandler::AddClause(absl::Span<const Literal> clause) {
83  MapClause(clause);
84  if (drat_checker_ != nullptr) {
85  drat_checker_->AddInferedClause(values_);
86  }
87  if (drat_writer_ != nullptr) {
88  drat_writer_->AddClause(values_);
89  }
90 }
91 
92 void DratProofHandler::DeleteClause(absl::Span<const Literal> clause) {
93  MapClause(clause);
94  if (drat_checker_ != nullptr) {
95  drat_checker_->DeleteClause(values_);
96  }
97  if (drat_writer_ != nullptr) {
98  drat_writer_->DeleteClause(values_);
99  }
100 }
101 
102 DratChecker::Status DratProofHandler::Check(double max_time_in_seconds) {
103  if (drat_checker_ != nullptr) {
104  // The empty clause is not explicitly added by the solver.
105  drat_checker_->AddInferedClause({});
106  return drat_checker_->Check(max_time_in_seconds);
107  }
108  return DratChecker::Status::UNKNOWN;
109 }
110 
111 void DratProofHandler::MapClause(absl::Span<const Literal> clause) {
112  values_.clear();
113  for (const Literal l : clause) {
114  CHECK_LT(l.Variable(), reverse_mapping_.size());
115  const Literal original_literal =
116  Literal(reverse_mapping_[l.Variable()], l.IsPositive());
117  values_.push_back(original_literal);
118  }
119 
120  // The sorting is such that new variables appear first. This is important for
121  // BVA since DRAT-trim only check the RAT property with respect to the first
122  // variable of the clause.
123  std::sort(values_.begin(), values_.end(), [](Literal a, Literal b) {
124  return std::abs(a.SignedValue()) > std::abs(b.SignedValue());
125  });
126 }
127 
128 } // namespace sat
129 } // namespace operations_research
Definition: base/file.h:33
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)
int64_t b
int64_t a
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
Definition: id_map.h:269
const BooleanVariable kNoBooleanVariable(-1)
Collection of objects used to extend the Constraint Solver library.