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