ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
cadical_backward.cpp
Go to the documentation of this file.
1#include "global.h"
2
3#include "internal.hpp"
4
6
7namespace CaDiCaL {
8
9/*------------------------------------------------------------------------*/
10
11// Provide eager backward subsumption for resolved clauses.
12
13// The eliminator maintains a queue of clauses that are new and have to be
14// checked to subsume or strengthen other (longer or same size) clauses.
15
17 if (!internal->opts.elimbackward)
18 return;
19 if (c->enqueued)
20 return;
21 LOG (c, "backward enqueue");
22 backward.push (c);
23 c->enqueued = true;
24}
25
27 if (backward.empty ())
28 return 0;
29 Clause *res = backward.front ();
30 backward.pop ();
32 res->enqueued = false;
33 LOG (res, "backward dequeue");
34 return res;
35}
36
38 while (dequeue ())
39 ;
40}
41
42/*------------------------------------------------------------------------*/
43
45 CADICAL_assert (opts.elimbackward);
47 if (c->garbage)
48 return;
49 LOG (c, "attempting backward subsumption and strengthening with");
50 size_t len = UINT_MAX;
51 unsigned size = 0;
52 int best = 0;
53 bool satisfied = false;
54 CADICAL_assert (mini_chain.empty ());
55 for (const auto &lit : *c) {
56 const signed char tmp = val (lit);
57 if (tmp > 0) {
58 satisfied = true;
59 break;
60 }
61 if (tmp < 0)
62 continue;
63 size_t l = occs (lit).size ();
64 LOG ("literal %d occurs %zd times", lit, l);
65 if (l < len)
66 best = lit, len = l;
67 mark (lit);
68 size++;
69 }
70 if (satisfied) {
71 LOG ("clause actually already satisfied");
72 elim_update_removed_clause (eliminator, c);
73 mark_garbage (c);
74 } else if (len > (size_t) opts.elimocclim) {
75 LOG ("skipping backward subsumption due to too many occurrences");
76 } else {
77 CADICAL_assert (len);
78 LOG ("literal %d has smallest number of occurrences %zd", best, len);
79 LOG ("marked %d literals in clause of size %d", size, c->size);
80 for (auto &d : occs (best)) {
81 if (d == c)
82 continue;
83 if (d->garbage)
84 continue;
85 if ((unsigned) d->size < size)
86 continue;
87 int negated = 0;
88 unsigned found = 0;
89 satisfied = false;
90 for (const auto &lit : *d) {
91 signed char tmp = val (lit);
92 if (tmp > 0) {
93 satisfied = true;
94 break;
95 }
96 if (tmp < 0)
97 continue;
98 tmp = marked (lit);
99 if (!tmp)
100 continue;
101 if (tmp < 0) {
102 if (negated) {
103 size = UINT_MAX;
104 break;
105 } else
106 negated = lit;
107 }
108 if (++found == size)
109 break;
110 }
111 if (satisfied) {
112 LOG (d, "found satisfied clause");
113 elim_update_removed_clause (eliminator, d);
114 mark_garbage (d);
115 } else if (found == size) {
116 if (!negated) {
117 LOG (d, "found subsumed clause");
118 elim_update_removed_clause (eliminator, d);
119 mark_garbage (d);
120 stats.subsumed++;
121 stats.elimbwsub++;
122 } else {
123 int unit = 0;
125 CADICAL_assert (analyzed.empty ());
126 CADICAL_assert (lrat_chain.empty ());
127 // figure out wether we strengthen c or get a new unit
128 for (const auto &lit : *d) {
129 const signed char tmp = val (lit);
130 if (tmp < 0) {
131 if (!lrat)
132 continue;
133 Flags &f = flags (lit);
134 CADICAL_assert (!f.seen);
135 if (f.seen)
136 continue;
137 f.seen = true;
138 analyzed.push_back (lit);
139 continue;
140 }
141 if (tmp > 0) {
142 satisfied = true;
143 break;
144 }
145 if (lit == negated)
146 continue;
147 if (unit) {
148 unit = INT_MIN;
149 continue; // needed to guarantee d is not satsified
150 } else
151 unit = lit;
152 }
153 if (lrat && !satisfied) {
154 // if we found a unit we need to add all unit ids from
155 // {c\d}U{d\c} otherwise just the unit ids from {c\d}
156 for (const auto &lit : *c) {
157 const signed char tmp = val (lit);
158 CADICAL_assert (tmp <= 0);
159 if (tmp >= 0)
160 continue;
161 Flags &f = flags (lit);
162 if (f.seen && unit && unit == INT_MIN) {
163 f.seen = false;
164 continue;
165 } else if (!f.seen) {
166 f.seen = true;
167 analyzed.push_back (lit);
168 }
169 }
170 if (unit == INT_MIN) { // we do not need units from {d\c}
171 for (const auto &lit : *d) {
172 flags (lit).seen = false;
173 }
174 }
175 for (const auto &lit : analyzed) {
176 Flags &f = flags (lit);
177 if (!f.seen) {
178 f.seen = true;
179 continue;
180 }
181 int64_t id = unit_id (-lit);
182 lrat_chain.push_back (id);
183 }
185 lrat_chain.push_back (d->id);
186 lrat_chain.push_back (c->id);
187 } else if (lrat)
189 if (satisfied) {
190 CADICAL_assert (lrat_chain.empty ());
191 mark_garbage (d);
192 elim_update_removed_clause (eliminator, d);
193 } else if (unit && unit != INT_MIN) {
194 CADICAL_assert (unit);
195 LOG (d, "unit %d through hyper unary resolution with", unit);
196 assign_unit (unit);
197 elim_propagate (eliminator, unit);
198 lrat_chain.clear ();
199 break;
200 } else if (occs (negated).size () <= (size_t) opts.elimocclim) {
201 strengthen_clause (d, negated);
202 remove_occs (occs (negated), d);
203 elim_update_removed_lit (eliminator, negated);
204 stats.elimbwstr++;
205 CADICAL_assert (negated != best);
206 eliminator.enqueue (d);
207 }
208 lrat_chain.clear ();
209 }
210 }
211 }
212 }
213 mini_chain.clear ();
214 unmark (c);
215}
216
217/*------------------------------------------------------------------------*/
218
220 if (!opts.elimbackward) {
221 CADICAL_assert (eliminator.backward.empty ());
222 return;
223 }
224 START (backward);
225 LOG ("attempting backward subsumption and strengthening with %zd clauses",
226 eliminator.backward.size ());
227 Clause *c;
228 while (!unsat && (c = eliminator.dequeue ()))
229 elim_backward_clause (eliminator, c);
230 STOP (backward);
231}
232
233/*------------------------------------------------------------------------*/
234
235} // namespace CaDiCaL
236
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define CADICAL_assert(ignore)
Definition global.h:14
#define LOG(...)
const char * flags()
void remove_occs(Occs &os, Clause *c)
Definition occs.hpp:23
#define STOP(P)
Definition profile.hpp:158
#define START(P)
Definition profile.hpp:150
int lit
Definition satVec.h:130
Internal * internal
Definition elim.hpp:35
queue< Clause * > backward
Definition elim.hpp:43
void elim_update_removed_lit(Eliminator &, int lit)
void elim_backward_clause(Eliminator &, Clause *)
vector< int > analyzed
Definition internal.hpp:267
void mark_garbage(Clause *)
vector< int64_t > lrat_chain
Definition internal.hpp:210
void unmark(int lit)
Definition internal.hpp:490
signed char val(int lit) const
signed char marked(int lit) const
Definition internal.hpp:478
int64_t unit_id(int lit) const
Definition internal.hpp:434
void elim_update_removed_clause(Eliminator &, Clause *, int except=0)
void strengthen_clause(Clause *, int)
void elim_backward_clauses(Eliminator &)
vector< int64_t > minimize_chain
Definition internal.hpp:212
Occs & occs(int lit)
Definition internal.hpp:465
void elim_propagate(Eliminator &, int unit)
vector< int64_t > mini_chain
Definition internal.hpp:211
unsigned size
Definition vector.h:35
signed char mark
Definition value.h:8