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