OR-Tools  9.6
drat_proof_handler.h
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 
14 #ifndef OR_TOOLS_SAT_DRAT_PROOF_HANDLER_H_
15 #define OR_TOOLS_SAT_DRAT_PROOF_HANDLER_H_
16 
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 #if !defined(__PORTABLE_PLATFORM__)
22 #include "ortools/base/file.h"
23 #endif // !defined(__PORTABLE_PLATFORM__)
24 #include "absl/types/span.h"
28 #include "ortools/sat/sat_base.h"
29 
30 namespace operations_research {
31 namespace sat {
32 
33 // DRAT is a SAT proof format that allows a simple program to check that the
34 // problem is really UNSAT. The description of the format and a checker are
35 // available at: // http://www.cs.utexas.edu/~marijn/drat-trim/
36 //
37 // Note that DRAT proofs are often huge (can be GB), and take about as much time
38 // to check as it takes for the solver to find the proof in the first place!
39 //
40 // This class is used to build the SAT proof, and can either save it to disk,
41 // and/or store it in memory (in which case the proof can be checked when it is
42 // complete).
44  public:
45  // Use this constructor to store the DRAT proof in memory. The proof will not
46  // be written to disk, and can be checked with Check() when it is complete.
48  // Use this constructor to write the DRAT proof to disk, and to optionally
49  // store it in memory as well (in which case the proof can be checked with
50  // Check() when it is complete).
51  DratProofHandler(bool in_binary_format, File* output, bool check = false);
53 
54  // During the presolve step, variable get deleted and the set of non-deleted
55  // variable is remaped in a dense set. This allows to keep track of that and
56  // always output the DRAT clauses in term of the original variables. Must be
57  // called before adding or deleting clauses AddClause() or DeleteClause().
58  //
59  // TODO(user): This is exactly the same mecanism as in the SatPostsolver
60  // class. Factor out the code.
61  void ApplyMapping(
63 
64  // This need to be called when new variables are created.
65  void SetNumVariables(int num_variables);
66  void AddOneVariable();
67 
68  // Adds a clause of the UNSAT problem. This must be called before any call to
69  // AddClause() or DeleteClause(), in order to be able to check the DRAT proof
70  // with the Check() method when it is complete.
71  void AddProblemClause(absl::Span<const Literal> clause);
72 
73  // Writes a new clause to the DRAT output. The output clause is sorted so that
74  // newer variables always comes first. This is needed because in the DRAT
75  // format, the clause is checked for the RAT property with only its first
76  // literal. Must not be called after Check().
77  void AddClause(absl::Span<const Literal> clause);
78 
79  // Writes a "deletion" information about a clause that has been added before
80  // to the DRAT output. Note that it is also possible to delete a clause from
81  // the problem. Must not be called after Check().
82  //
83  // Because of a limitation a the DRAT-trim tool, it seems the order of the
84  // literals during addition and deletion should be EXACTLY the same. Because
85  // of this we get warnings for problem clauses.
86  void DeleteClause(absl::Span<const Literal> clause);
87 
88  // Returns VALID if the DRAT proof is correct, INVALID if it is not correct,
89  // or UNKNOWN if proof checking was not enabled (by choosing the right
90  // constructor) or timed out. This requires the problem clauses to be
91  // specified with AddProblemClause(), before the proof itself.
92  //
93  // WARNING: no new clause must be added or deleted after this method has been
94  // called.
95  DratChecker::Status Check(double max_time_in_seconds);
96 
97  private:
98  void MapClause(absl::Span<const Literal> clause);
99 
100  // We need to keep track of the variable newly created.
101  int variable_index_;
102 
103  // Temporary vector used for sorting the outputted clauses.
104  std::vector<Literal> values_;
105 
106  // This mapping will be applied to all clause passed to AddClause() or
107  // DeleteClause() so that they are in term of the original problem.
109 
110  std::unique_ptr<DratChecker> drat_checker_;
111  std::unique_ptr<DratWriter> drat_writer_;
112 };
113 
114 } // namespace sat
115 } // namespace operations_research
116 
117 #endif // OR_TOOLS_SAT_DRAT_PROOF_HANDLER_H_
Definition: base/file.h:33
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)
Collection of objects used to extend the Constraint Solver library.