1 /******************************************
2 Copyright (c) 2016, Mate Soos
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 ***********************************************/
22 
23 #ifndef __SUBSUMESTRENGTHEN_H__
24 #define __SUBSUMESTRENGTHEN_H__
25 
26 #include "cloffset.h"
27 #include "cryptominisat5/solvertypesmini.h"
28 #include "clabstraction.h"
29 #include "clause.h"
30 #include <vector>
31 using std::vector;
32 
33 namespace CMSat {
34 
35 class OccSimplifier;
36 class GateFinder;
37 class Solver;
38 
39 class SubsumeStrengthen
40 {
41 public:
42     SubsumeStrengthen(OccSimplifier* simplifier, Solver* solver);
43     size_t mem_used() const;
44 
45     void backw_sub_long_with_long();
46     bool backw_str_long_with_long();
47     bool backw_sub_str_long_with_bins();
48 
49     //Called from simplifier at resolvent-adding of var-elim
50     uint32_t subsume_and_unlink_and_markirred(const ClOffset offset);
51     bool backw_sub_str_long_with_bins_watch(
52         const Lit lit
53         , const bool redundant_too = false
54     );
55     bool handle_added_long_cl(int64_t* limit, const bool main_run);
56 
57     struct Sub0Ret {
58         ClauseStats stats;
59         bool subsumedIrred = 0;
60         uint32_t numSubsumed = 0;
61     };
62 
63     struct Sub1Ret {
64         Sub1Ret& operator+=(const Sub1Ret& other)
65         {
66             sub += other.sub;
67             str += other.str;
68 
69             return *this;
70         }
71 
72         size_t sub = 0;
73         size_t str = 0;
74         bool subsumedIrred = false;
75     };
76 
77     Sub1Ret backw_sub_str_long_with_implicit(const vector<Lit>& lits);
78     Sub1Ret strengthen_subsume_and_unlink_and_markirred(ClOffset offset);
79 
80     struct Stats
81     {
82         Stats& operator+=(const Stats& other);
83         void print_short(const Solver* solver) const;
84         void print() const;
85 
86         uint64_t subsumedBySub = 0;
87         uint64_t subsumedByStr = 0;
88         uint64_t litsRemStrengthen = 0;
89 
90         double subsumeTime = 0.0;
91         double strengthenTime = 0.0;
92     };
93 
94     void finishedRun();
95     const Stats& get_stats() const;
96     const Stats& getRunStats() const;
97 
98     template<class T>
99     void find_subsumed(
100         const ClOffset offset
101         , const T& ps
102         , const cl_abst_type abs
103         , vector<ClOffset>& out_subsumed
104         , const bool removeImplicit = false
105     );
106 
107 private:
108     Stats globalstats;
109     Stats runStats;
110 
111     OccSimplifier* simplifier;
112     Solver* solver;
113 
114     //Called from simplifier at resolvent-adding of var-elim
115     template<class T>
116     Sub0Ret subsume_and_unlink(
117         const ClOffset offset
118         , const T& ps
119         , const cl_abst_type abs
120         , const bool removeImplicit = false
121     );
122 
123     void randomise_clauses_order();
124     void remove_literal(ClOffset c, const Lit toRemoveLit);
125 
126     template<class T>
127     size_t find_smallest_watchlist_for_clause(const T& ps) const;
128 
129     template<class T>
130     void findStrengthened(
131         const ClOffset offset
132         , const T& ps
133         , const cl_abst_type abs
134         , vector<ClOffset>& out_subsumed
135         , vector<Lit>& out_lits
136     );
137 
138     template<class T>
139     void fillSubs(
140         const ClOffset offset
141         , const T& ps
142         , cl_abst_type abs
143         , vector<ClOffset>& out_subsumed
144         , vector<Lit>& out_lits
145         , const Lit lit
146     );
147 
148     template<class T1, class T2>
149     bool subset(const T1& A, const T2& B);
150 
151     template<class T1, class T2>
152     Lit subset1(const T1& A, const T2& B);
153     bool subsetAbst(const cl_abst_type A, const cl_abst_type B);
154 
155     vector<ClOffset> subs;
156     vector<Lit> subsLits;
157     vector<Lit> tmpLits;
158     size_t tried_bin_tri = 0;
159     uint64_t subsumedBin = 0;
160     uint64_t strBin = 0;
161 };
162 
getRunStats()163 inline const SubsumeStrengthen::Stats& SubsumeStrengthen::getRunStats() const
164 {
165     return runStats;
166 }
167 
get_stats()168 inline const SubsumeStrengthen::Stats& SubsumeStrengthen::get_stats() const
169 {
170     return globalstats;
171 }
172 
173 } //end namespace
174 
175 #endif //__SUBSUMESTRENGTHEN_H__
176