Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
drat_proof_handler.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <cstdlib>
18#include <memory>
19#include <vector>
20#if !defined(__PORTABLE_PLATFORM__)
21#include "ortools/base/file.h"
22#endif // !defined(__PORTABLE_PLATFORM__)
23#include "absl/log/check.h"
24#include "absl/types/span.h"
30
31namespace operations_research {
32namespace sat {
33
35 : variable_index_(0), drat_checker_(new DratChecker()) {}
36
37DratProofHandler::DratProofHandler(bool in_binary_format, File* output,
38 bool check)
39 : variable_index_(0),
40 drat_writer_(new DratWriter(in_binary_format, output)) {
41 if (check) {
42 drat_checker_ = std::make_unique<DratChecker>();
43 }
44}
45
48 mapping) {
50 for (BooleanVariable v(0); v < mapping.size(); ++v) {
51 const BooleanVariable image = mapping[v];
52 if (image != kNoBooleanVariable) {
53 if (image >= new_mapping.size())
54 new_mapping.resize(image.value() + 1, kNoBooleanVariable);
55 CHECK_EQ(new_mapping[image], kNoBooleanVariable);
56 new_mapping[image] =
57 v < reverse_mapping_.size() ? reverse_mapping_[v] : v;
58 CHECK_NE(new_mapping[image], kNoBooleanVariable);
59 }
60 }
61 std::swap(new_mapping, reverse_mapping_);
62}
63
64void DratProofHandler::SetNumVariables(int num_variables) {
65 CHECK_GE(num_variables, reverse_mapping_.size());
66 while (reverse_mapping_.size() < num_variables) {
67 reverse_mapping_.push_back(BooleanVariable(variable_index_++));
68 }
69}
70
72 reverse_mapping_.push_back(BooleanVariable(variable_index_++));
73}
74
75void DratProofHandler::AddProblemClause(absl::Span<const Literal> clause) {
76 if (drat_checker_ != nullptr) {
77 drat_checker_->AddProblemClause(clause);
78 }
79}
80
81void DratProofHandler::AddClause(absl::Span<const Literal> clause) {
82 MapClause(clause);
83 if (drat_checker_ != nullptr) {
84 drat_checker_->AddInferedClause(values_);
85 }
86 if (drat_writer_ != nullptr) {
87 drat_writer_->AddClause(values_);
88 }
89}
90
91void DratProofHandler::DeleteClause(absl::Span<const Literal> clause) {
92 MapClause(clause);
93 if (drat_checker_ != nullptr) {
94 drat_checker_->DeleteClause(values_);
95 }
96 if (drat_writer_ != nullptr) {
97 drat_writer_->DeleteClause(values_);
98 }
99}
100
101DratChecker::Status DratProofHandler::Check(double max_time_in_seconds) {
102 if (drat_checker_ != nullptr) {
103 // The empty clause is not explicitly added by the solver.
104 drat_checker_->AddInferedClause({});
105 return drat_checker_->Check(max_time_in_seconds);
106 }
108}
109
110void DratProofHandler::MapClause(absl::Span<const Literal> clause) {
111 values_.clear();
112 for (const Literal l : clause) {
113 CHECK_LT(l.Variable(), reverse_mapping_.size());
114 const Literal original_literal =
115 Literal(reverse_mapping_[l.Variable()], l.IsPositive());
116 values_.push_back(original_literal);
117 }
118
119 // The sorting is such that new variables appear first. This is important for
120 // BVA since DRAT-trim only check the RAT property with respect to the first
121 // variable of the clause.
122 std::sort(values_.begin(), values_.end(), [](Literal a, Literal b) {
123 return std::abs(a.SignedValue()) > std::abs(b.SignedValue());
124 });
125}
126
127} // namespace sat
128} // namespace operations_research
Definition file.h:30
DratChecker::Status Check(double max_time_in_seconds)
void AddProblemClause(absl::Span< const Literal > clause)
void ApplyMapping(const util_intops::StrongVector< BooleanVariable, BooleanVariable > &mapping)
void AddClause(absl::Span< const Literal > clause)
void DeleteClause(absl::Span< const Literal > clause)
void SetNumVariables(int num_variables)
This need to be called when new variables are created.
void push_back(const value_type &val)
void resize(size_type new_size)
int64_t a
Definition table.cc:44
const BooleanVariable kNoBooleanVariable(-1)
In SWIG mode, we don't want anything besides these top-level includes.