summaryrefslogtreecommitdiff
path: root/src/prop/cryptominisat/Solver/XorFinder.cpp
blob: 1b82430e960e869e2fd4ddac85775197cbf213f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/***********************************************************************************
CryptoMiniSat -- Copyright (c) 2009 Mate Soos

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************************************/

#include "XorFinder.h"

#include <algorithm>
#include <utility>
#include <iostream>
#include "Solver.h"
#include "VarReplacer.h"
#include "ClauseCleaner.h"
#include "time_mem.h"

//#define VERBOSE_DEBUG

using namespace CMSat;
using std::make_pair;

XorFinder::XorFinder(Solver& _solver, vec<Clause*>& _cls) :
    cls(_cls)
    , solver(_solver)
{
}

bool XorFinder::fullFindXors(const uint32_t minSize, const uint32_t maxSize)
{
    uint32_t sumLengths = 0;
    double time = cpuTime();
    foundXors = 0;
    solver.clauseCleaner->cleanClauses(solver.clauses, ClauseCleaner::clauses);
    if (!solver.ok) return false;

    toRemove.clear();
    toRemove.resize(cls.size(), false);
    toLeaveInPlace.clear();
    toLeaveInPlace.resize(cls.size(), false);

    table.clear();
    table.reserve(cls.size());

    for (Clause **it = cls.getData(), **end = cls.getDataEnd(); it != end; it ++) {
        if (it+1 != end) __builtin_prefetch(*(it+1));
        Clause& c = (**it);
        assert((*it)->size() > 2);
        bool sorted = true;
        for (uint32_t i = 0, size = c.size(); i+1 < size ; i++) {
            sorted = (c[i].var() <= c[i+1].var());
            if (!sorted) break;
        }
        if (!sorted) {
            solver.detachClause(c);
            std::sort(c.getData(), c.getDataEnd());
            solver.attachClause(c);
        }
    }

    uint32_t i = 0;
    for (Clause **it = cls.getData(), **end = cls.getDataEnd(); it != end; it++, i++) {
        const uint32_t size = (*it)->size();
        if ( size > maxSize || size < minSize) {
            toLeaveInPlace[i] = true;
            continue;
        }
        table.push_back(make_pair(*it, i));
    }
    std::sort(table.begin(), table.end(), clause_sorter_primary());

    if (!findXors(sumLengths)) goto end;
    solver.ok = (solver.propagate<true>().isNULL());

end:

    if (solver.conf.verbosity >= 1 || (solver.conf.verbosity >= 1 && foundXors > 0)) {
        printf("c Finding non-binary XORs:    %5.2f s (found: %7d, avg size: %3.1f)\n", cpuTime()-time, foundXors, (double)sumLengths/(double)foundXors);
    }

    i = 0;
    uint32_t j = 0;
    uint32_t toSkip = 0;
    for (uint32_t end = cls.size(); i != end; i++) {
        if (toLeaveInPlace[i]) {
            cls[j] = cls[i];
            j++;
            toSkip++;
            continue;
        }
        if (!toRemove[table[i-toSkip].second]) {
            cls[j] = table[i-toSkip].first;
            j++;
        }
    }
    cls.shrink(i-j);

    return solver.ok;
}


/**
@brief Finds xors in clauseTable -- datastructures must already be set up

Identifies sets of clauses of the same length and variable content, and then
tries to merge them into an XOR.
*/
bool XorFinder::findXors(uint32_t& sumLengths)
{
    #ifdef VERBOSE_DEBUG
    cout << "Finding Xors started" << endl;
    #endif

    sumLengths = 0;

    ClauseTable::iterator begin = table.begin();
    ClauseTable::iterator end = table.begin();
    vec<Lit> lits;
    bool impair;
    while (getNextXor(begin,  end, impair)) {
        const Clause& c = *(begin->first);
        lits.clear();
        for (const Lit *it = &c[0], *cend = it+c.size() ; it != cend; it++) {
            lits.push(Lit(it->var(), false));
        }

        #ifdef VERBOSE_DEBUG
        cout << "- Found clauses:" << endl;
        #endif

        for (ClauseTable::iterator it = begin; it != end; it++) {
            //This clause belongs to the xor we found?
            //(i.e. does it have the correct number of inverted literals?)
            if (impairSigns(*it->first) == impair){
                #ifdef VERBOSE_DEBUG
                it->first->plainPrint();
                #endif
                toRemove[it->second] = true;
                solver.removeClause(*it->first);
            }
        }

        assert(lits.size() > 2);
        XorClause* x = solver.addXorClauseInt(lits, impair);
        if (x != NULL) solver.xorclauses.push(x);
        if (!solver.ok) return false;

        #ifdef VERBOSE_DEBUG
        cout << "- Final xor-clause: " << x << std::endl;;
        #endif

        foundXors++;
        sumLengths += lits.size();
    }

    return solver.ok;
}

/**
@brief Moves to the next set of begin&end pointers that contain an xor

@p begin[inout] start searching here in XorFinder::table
@p end[inout] end iterator of XorFinder::table until which the xor spans
*/
bool XorFinder::getNextXor(ClauseTable::iterator& begin, ClauseTable::iterator& end, bool& impair)
{
    ClauseTable::iterator tableEnd = table.end();

    while(begin != tableEnd && end != tableEnd) {
        begin = end;
        end++;
        uint32_t size = (end == tableEnd ? 0:1);
        while(end != tableEnd && clause_vareq(begin->first, end->first)) {
            size++;
            end++;
        }
        if (size > 0 && isXor(size, begin, end, impair))
            return true;
    }

    return false;
}

/**
@brief Returns if the two clauses are equal

NOTE: assumes that the clauses are of equal lenght AND contain the same
variables (but the invertedness of the literals might differ)
*/
bool XorFinder::clauseEqual(const Clause& c1, const Clause& c2) const
{
    assert(c1.size() == c2.size());
    for (uint32_t i = 0, size = c1.size(); i < size; i++)
        if (c1[i].sign() !=  c2[i].sign()) return false;

    return true;
}

/**
@brief Returns whether the number of inverted literals in the clause is pair or impair
*/
bool XorFinder::impairSigns(const Clause& c) const
{
    uint32_t num = 0;
    for (const Lit *it = &c[0], *end = it + c.size(); it != end; it++)
        num += it->sign();

    return num % 2;
}

/**
@brief Gets as input a set of clauses of equal size and variable content, decides if there is an XOR in them

@param impair If there is an XOR, this tells if that XOR contains an impair
number of inverted literal or not
@return True, if ther is an XOR, and False if not
*/
bool XorFinder::isXor(const uint32_t size, const ClauseTable::iterator& begin, const ClauseTable::iterator& end, bool& impair)
{
    const uint32_t requiredSize = 1 << (begin->first->size()-1);

    //Note: "size" can be larger than requiredSize, since there might be
    //a mix of imparied and paired num. inverted literals, and furthermore,
    //clauses might be repeated
    if (size < requiredSize) return false;

    #ifdef DEBUG_XORFIND2
    {
        vec<Var> vars;
        Clause& c = *begin->first;
        for (uint32_t i = 0; i < c.size(); i++)
            vars.push(c[i].var());
        for (ClauseTable::iterator it = begin; it != end; it++) {
            Clause& c = *it->first;
            for (uint32_t i = 0; i < c.size(); i++)
                assert(vars[i] == c[i].var());
        }
        clause_sorter_primary sorter;

        for (ClauseTable::iterator it = begin; it != end; it++) {
            ClauseTable::iterator it2 = it;
            it2++;
            if (it2 == end) break;
            assert(!sorter(*it2, *it));
        }
    }
    #endif //DEBUG_XORFIND

    //We now sort them according to literal content
    std::sort(begin, end, clause_sorter_secondary());

    uint32_t numPair = 0;
    uint32_t numImpair = 0;
    countImpairs(begin, end, numImpair, numPair);

    //if there are two XORs with equal variable sets, but different invertedness
    //that leads to direct UNSAT result.
    if (numImpair == requiredSize && numPair == requiredSize) {
        solver.ok = false;
        impair = true;
        return true;
    }

    if (numImpair == requiredSize) {
        impair = true;
        return true;
    }

    if (numPair == requiredSize) {
        impair = false;
        return true;
    }

    return false;
}

/**
@brief Counts number of negations in the literals in clauses between begin&end, and returns the number of clauses with pair, and impair literals
*/
void XorFinder::countImpairs(const ClauseTable::iterator& begin, const ClauseTable::iterator& end, uint32_t& numImpair, uint32_t& numPair) const
{
    numImpair = 0;
    numPair = 0;

    ClauseTable::const_iterator it = begin;
    ClauseTable::const_iterator it2 = begin;
    it2++;

    bool impair = impairSigns(*it->first);
    numImpair += impair;
    numPair += !impair;

    for (; it2 != end;) {
        if (!clauseEqual(*it->first, *it2->first)) {
            bool impair = impairSigns(*it2->first);
            numImpair += impair;
            numPair += !impair;
        }
        it++;
        it2++;
    }
}

/**
@brief Converts all xor clauses to normal clauses

Sometimes it's not worth the hassle of having xor clauses and normal clauses.
This function converts xor clauses to normal clauses, and removes the normal
clauses.

\todo It currently only works for 3- and 4-long clauses. Larger clauses should
also be handled.
*/
void XorFinder::addAllXorAsNorm()
{
    uint32_t added = 0;
    XorClause **i = solver.xorclauses.getData(), **j = i;
    for (XorClause **end = solver.xorclauses.getDataEnd(); i != end; i++) {
        if ((*i)->size() > 3) {
            *j++ = *i;
            continue;
        }
        added++;
        if ((*i)->size() == 3) addXorAsNormal3(**i);
        //if ((*i)->size() == 4) addXorAsNormal4(**i);
        solver.removeClause(**i);
    }
    solver.xorclauses.shrink(i-j);
    if (solver.conf.verbosity >= 1) {
        std::cout << "c Added XOR as norm:" << added << std::endl;
    }
}

/**
@brief Utility function for addAllXorAsNorm() for converting 3-long xor clauses to normal clauses

\todo clean this up, it's ugly
*/
void XorFinder::addXorAsNormal3(XorClause& c)
{
    assert(c.size() == 3);
    Clause *tmp;
    vec<Var> vars;
    const bool inverted = c.xorEqualFalse();

    for (uint32_t i = 0; i < c.size(); i++) {
        vars.push(c[i].var());
    }

    vec<Lit> vars2;
    vars2.growTo(3);
    vars2[0] = Lit(vars[0], false ^ inverted);
    vars2[1] = Lit(vars[1], false ^ inverted);
    vars2[2] = Lit(vars[2], false ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2.growTo(3);
    vars2[0] = Lit(vars[0], true ^ inverted);
    vars2[1] = Lit(vars[1], true ^ inverted);
    vars2[2] = Lit(vars[2], false ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2.growTo(3);
    vars2[0] = Lit(vars[0], true ^ inverted);
    vars2[1] = Lit(vars[1], false ^ inverted);
    vars2[2] = Lit(vars[2], true ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2.growTo(3);
    vars2[0] = Lit(vars[0], false ^ inverted);
    vars2[1] = Lit(vars[1], true ^ inverted);
    vars2[2] = Lit(vars[2], true ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);
}

/**
@brief Utility function for addAllXorAsNorm() for converting 4-long xor clauses to normal clauses

\todo clean this up, it's ugly
*/
void XorFinder::addXorAsNormal4(XorClause& c)
{
    assert(c.size() == 4);
    Clause *tmp;
    vec<Var> vars;
    vec<Lit> vars2(c.size());
    const bool inverted = !c.xorEqualFalse();

    for (uint32_t i = 0; i < c.size(); i++) {
        vars.push(c[i].var());
    }

    vars2[0] = Lit(vars[0], false ^ inverted);
    vars2[1] = Lit(vars[1], false ^ inverted);
    vars2[2] = Lit(vars[2], false ^ inverted);
    vars2[3] = Lit(vars[3], true ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2[0] = Lit(vars[0], false ^ inverted);
    vars2[1] = Lit(vars[1], true ^ inverted);
    vars2[2] = Lit(vars[2], false ^ inverted);
    vars2[3] = Lit(vars[3], false ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2[0] = Lit(vars[0], false ^ inverted);
    vars2[1] = Lit(vars[1], false ^ inverted);
    vars2[2] = Lit(vars[2], true ^ inverted);
    vars2[3] = Lit(vars[3], false ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2[0] = Lit(vars[0], false ^ inverted);
    vars2[1] = Lit(vars[1], false ^ inverted);
    vars2[2] = Lit(vars[2], false ^ inverted);
    vars2[3] = Lit(vars[3], true ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2[0] = Lit(vars[0], false ^ inverted);
    vars2[1] = Lit(vars[1], true ^ inverted);
    vars2[2] = Lit(vars[2], true ^ inverted);
    vars2[3] = Lit(vars[3], true ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2[0] = Lit(vars[0], true ^ inverted);
    vars2[1] = Lit(vars[1], false ^ inverted);
    vars2[2] = Lit(vars[2], true ^ inverted);
    vars2[3] = Lit(vars[3], true ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2[0] = Lit(vars[0], true ^ inverted);
    vars2[1] = Lit(vars[1], true ^ inverted);
    vars2[2] = Lit(vars[2], false ^ inverted);
    vars2[3] = Lit(vars[3], true ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);

    vars2[0] = Lit(vars[0], true ^ inverted);
    vars2[1] = Lit(vars[1], true ^ inverted);
    vars2[2] = Lit(vars[2], true ^ inverted);
    vars2[3] = Lit(vars[3], false ^ inverted);
    tmp = solver.addClauseInt(vars2);
    if (tmp) solver.clauses.push(tmp);
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback