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/drat_proof_handler.h"
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 #include "ortools/base/strong_vector.h"
22 
23 namespace operations_research {
24 namespace sat {
25 
DratProofHandler()26 DratProofHandler::DratProofHandler()
27     : variable_index_(0), drat_checker_(new DratChecker()) {}
28 
DratProofHandler(bool in_binary_format,File * output,bool check)29 DratProofHandler::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 
ApplyMapping(const absl::StrongVector<BooleanVariable,BooleanVariable> & mapping)38 void DratProofHandler::ApplyMapping(
39     const absl::StrongVector<BooleanVariable, BooleanVariable>& mapping) {
40   absl::StrongVector<BooleanVariable, BooleanVariable> new_mapping;
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 
SetNumVariables(int num_variables)55 void 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 
AddOneVariable()62 void DratProofHandler::AddOneVariable() {
63   reverse_mapping_.push_back(BooleanVariable(variable_index_++));
64 }
65 
AddProblemClause(absl::Span<const Literal> clause)66 void DratProofHandler::AddProblemClause(absl::Span<const Literal> clause) {
67   if (drat_checker_ != nullptr) {
68     drat_checker_->AddProblemClause(clause);
69   }
70 }
71 
AddClause(absl::Span<const Literal> clause)72 void 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 
DeleteClause(absl::Span<const Literal> clause)82 void 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 
Check(double max_time_in_seconds)92 DratChecker::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 
MapClause(absl::Span<const Literal> clause)101 void 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
120