Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
vivification.h
Go to the documentation of this file.
1// Copyright 2010-2025 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 ORTOOLS_SAT_VIVIFICATION_H_
15#define ORTOOLS_SAT_VIVIFICATION_H_
16
17#include <cstdint>
18
19#include "absl/base/attributes.h"
20#include "ortools/sat/clause.h"
22#include "ortools/sat/model.h"
27
29
30// Helper class responsible for "vivifying" clauses.
31//
32// See for instance "Clause Vivification by Unit Propagation in CDCL SAT
33// Solvers" Chu-Min Li, Fan Xiao, Mao Luo, Felip Manyà, Zhipeng Lü, Yu Li.
34//
35// This is basically trying to minimize clauses using propagation by taking as
36// decisions the negation of some literals of that clause.
37class Vivifier {
38 public:
39 explicit Vivifier(Model* model)
40 : assignment_(model->GetOrCreate<Trail>()->Assignment()),
41 parameters_(*model->GetOrCreate<SatParameters>()),
42 time_limit_(model->GetOrCreate<TimeLimit>()),
43 logger_(model->GetOrCreate<SolverLogger>()),
44 sat_solver_(model->GetOrCreate<SatSolver>()),
45 trail_(model->GetOrCreate<Trail>()),
46 binary_clauses_(model->GetOrCreate<BinaryImplicationGraph>()),
47 clause_manager_(model->GetOrCreate<ClauseManager>()),
48 clause_id_generator_(model->GetOrCreate<ClauseIdGenerator>()),
49 lrat_proof_handler_(model->Mutable<LratProofHandler>()) {}
50
51 // Minimize a batch of clauses using propagation.
52 // Returns false on UNSAT.
53 ABSL_MUST_USE_RESULT bool MinimizeByPropagation(
54 bool log_info, double dtime_budget,
55 bool minimize_new_clauses_only = false);
56
57 // Values from the last MinimizeByPropagation() call.
58 int64_t last_num_vivified() const { return last_num_vivified_; }
59 int64_t last_num_literals_removed() const {
60 return last_num_literals_removed_;
61 }
62
63 // Sum of everything ever done by this class.
64 struct Counters {
66 int64_t num_decisions = 0;
67 int64_t num_true = 0;
68 int64_t num_subsumed = 0;
70 int64_t num_reused = 0;
71 int64_t num_conflicts = 0;
72 };
73 Counters counters() const { return counters_; }
74
75 private:
76 // Use propagation to try to minimize the given clause. This is really similar
77 // to MinimizeCoreWithPropagation(). Note that because this does a small tree
78 // search, it will impact the variable/clause activities and may add new
79 // conflicts.
80 ABSL_MUST_USE_RESULT bool TryToMinimizeClause(SatClause* clause);
81
82 // Returns true if variable is fixed in the current assignment due to
83 // non-removable clauses, plus at most one removable clause with size <=
84 // max_size.
85 bool SubsumptionIsInteresting(BooleanVariable variable, int max_size);
86 void KeepAllClausesUsedToInfer(BooleanVariable variable);
87
88 const VariablesAssignment& assignment_;
89 const SatParameters& parameters_;
90
91 TimeLimit* time_limit_;
92 SolverLogger* logger_;
93 SatSolver* sat_solver_;
94 Trail* trail_;
95 BinaryImplicationGraph* binary_clauses_;
96 ClauseManager* clause_manager_;
97 ClauseIdGenerator* clause_id_generator_;
98 LratProofHandler* lrat_proof_handler_ = nullptr;
99
100 Counters counters_;
101
102 int64_t last_num_vivified_ = 0;
103 int64_t last_num_literals_removed_ = 0;
104};
105
106} // namespace operations_research::sat
107#endif // ORTOOLS_SAT_VIVIFICATION_H_
int64_t last_num_literals_removed() const
ABSL_MUST_USE_RESULT bool MinimizeByPropagation(bool log_info, double dtime_budget, bool minimize_new_clauses_only=false)