-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
677 lines (556 loc) · 16.4 KB
/
parser.c
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
MARCH Satisfiability Solver
Copyright (C) 2001-2005 M.J.H. Heule, J.E. van Zwieten, and M. Dufour.
Copyright (C) 2005-2011 M.J.H. Heule.
*/
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
#include "common.h"
#include "parser.h"
#include "memory.h"
#include "equivalence.h"
#ifndef __APPLE__
#include <malloc.h>
#endif
int *simplify_stack, *simplify_stackp;
#define PUSH_PARSER_NA( __a ) \
{ \
if( timeAssignments[ __a ] >= VARMAX ) \
{ \
if( FIXED_ON_COMPLEMENT( __a ) )\
return UNSAT; \
} \
else \
{ \
timeAssignments[ __a ] = VARMAX; \
timeAssignments[ -__a ] = VARMAX + 1; \
*( simplify_stackp++ ) = __a; \
} \
}
/*
************************************************************************
* CNF init and deinit, parsing and unary clause removal. *
************************************************************************
*/
/*
MALLOCS: -
REALLOCS: -
FREES: -
*/
int initFormula( FILE* in )
{
int result;
/*
initialize global data structure.
*/
original_nrofvars = 0;
original_nrofclauses = 0;
nrofvars = 0;
activevars = 0;
nrofclauses = 0;
nrofceq = 0;
initial_freevars = 0;
non_tautological_equivalences = 0;
lookaheadArrayLength = 0;
forced_literals = 0;
Cv = NULL;
Clength = NULL;
timeAssignments = NULL;
VeqDepends = NULL;
/*
initialization done.
*/
// printf( "c initFormula():: searching for DIMACS p-line....\n" );
/*
search for p-line in DIMACS format
*/
do
{
result = fscanf( in, " p cnf %i %i \n", &( original_nrofvars ), &( original_nrofclauses ) );
if( result > 0 && result != EOF )
break;
result = fscanf( in, "%*s\n" );
}
while( result != 2 && result != EOF );
if( result == EOF || result != 2 )
{
return 0;
}
nrofvars = original_nrofvars;
activevars = original_nrofvars;
nrofclauses = original_nrofclauses;
freevars = nrofvars;
if( (nrofclauses / nrofvars) > 10 )
{
printf("c full lookahead due to high density!\n");
percent = 100;
}
else
percent = PERCENT;
printf( "c the DIMACS p-line indicates a CNF of %i variables and %i clauses.\n", nrofvars, nrofclauses );
return 1;
}
/*
MALLOCS: -
REALLOCS: -
FREES: Cv[ * ], Cv, Clength, timeAssignments, VeqDepends
*/
void disposeFormula()
{
int i;
/*
Can also be used to delete a partial formula, because Cv[ i ]
is initialized to NULL. IMPORTANT: according to 'man free',
free( void *ptr ) does nothing iff ( ptr == NULL ). This
behaviour is vital to disposeFormula() and other parts of
the solver where memory is freed.
*/
if( Cv != NULL )
{
for( i = 0; i < nrofclauses; i++ )
free( Cv[ i ] );
free( Cv );
Cv = NULL;
}
/*
IMPORTANT: timeAssignments should be corrected before
attempting this. (In the lookahead, nrofvars is added to both
pointers to speed up indexing.) Neglecting this correction
means Segfault Suicide!
*/
FREE_OFFSET( timeAssignments );
FREE( VeqDepends );
FREE( Clength );
/*
Update cnf structure.
*/
original_nrofvars = 0;
original_nrofclauses = 0;
nrofvars = 0;
nrofclauses = 0;
dispose_equivalence();
}
/*
MALLOCS: _clause, Cv, Cv[], Clength, timeAssignments
REALLOCS: -
FREES: _clause
*/
int parseCNF( FILE* in )
{
int *_clause, clen, _lit;
int i, j, error, unary;
unary = 0;
#ifndef PRINT_FORMULA
// printf( "c parseCNF():: parsing....\n" );
#endif
/*
Allocate buffer to hold clause. A clause can never
be longer than nrofvars, for obvious reasons.
*/
_clause = (int*) malloc( sizeof( int ) * nrofvars );
/* INIT GLOBAL DATASTRUCTURES!! */
Cv = (int**) malloc( sizeof( int* ) * nrofclauses );
for( i = 0; i < nrofclauses; i++ )
Cv[ i ] = NULL;
/* Clength: length of clause i */
Clength = (int*) malloc( sizeof( int ) * nrofclauses );
/* BinaryImp: implication clause table */
BinaryImp = (int**) malloc( sizeof( int* ) * ( 2 * nrofvars + 1) );
BinaryImpLength = (int* ) malloc( sizeof( int ) * ( 2 * nrofvars + 1) );
for( i = 0; i < (2 * nrofvars + 1); i++ )
{
BinaryImp [ i ] = (int*) malloc( sizeof( int ) * INITIAL_ARRAY_SIZE );
BinaryImpLength[ i ] = INITIAL_ARRAY_SIZE - 1;
BinaryImp [ i ][ 0 ] = 2;
BinaryImp [ i ][ 1 ] = 0; //Moet nog weggewerkt worden...
}
BinaryImp += nrofvars;
BinaryImpLength += nrofvars;
/* timeAssignments & VeqDepends */
timeAssignments = (tstamp*) malloc( sizeof( tstamp ) * ( 2 * nrofvars + 1 ) );
VeqDepends = (int* ) malloc( sizeof( int ) * ( nrofvars + 1 ) );
timeAssignments += nrofvars;
for( i = 0; i < ( nrofvars + 1 ); i++ )
{
VeqDepends [ i ] = 0;
timeAssignments[ i ] = 0;
timeAssignments[ -i ] = 0;
}
i = clen = error = 0;
while( i < nrofclauses && !error )
{
error = ( fscanf( in, " %i ", &_lit ) != 1 );
if( !error )
{
if( _lit == 0 )
{
if( clen == 0 )
{
/* a zero-length clause is not good! */
printf( "c parseCNF():: zero length clause found in input!\n" );
error = 1;
}
else
{
if( clen == 1 )
unary++;
Cv[ i ] = (int*) malloc( sizeof( int ) * clen );
Clength[ i ] = clen;
for( j = 0; j < clen; j++ ) Cv[ i ][ j ] = _clause[ j ];
clen = 0;
i++;
}
}
else
{
if( clen < nrofvars )
_clause[ clen++ ] = _lit;
else
{
printf( "c parseCNF():: clause length exceeds total number of variables in this CNF.\n" );
error = 1;
}
}
}
}
/* free clause buffer */
free( _clause );
// if( !error )
// printf( "c parseCNF():: the CNF contains %i unary clauses.\n", unary );
if( error )
disposeFormula();
return !error;
}
int simplify_formula()
{
int _iterCounter, tautologies, satisfied, duplicates, bi_equivalences;
tautologies = 0;
satisfied = 0;
duplicates = 0;
bi_equivalences = 0;
do
{
_iterCounter = bi_equivalences + nrofvars - freevars;
tautologies += sort_literals();
if( find_and_propagate_unary_clauses() == UNSAT )
return UNSAT;
satisfied += compactCNF();
duplicates += sort_clauses();
bi_equivalences += find_and_propagate_binary_equivalences();
if( check_vadility_equivalences() == UNSAT )
return UNSAT;
}
while( (bi_equivalences + nrofvars - freevars) > _iterCounter );
// printf("c simplify_formula():: removed %i tautological, %i satisfied and %i duplicate clauses\n",
// tautologies, satisfied - tautologies, duplicates);
return SAT;
}
/************************************************************************************
sort_literals bubble sorts all _variables_ in all clauses of the CNF
and returns the number of tautological clauses (clauses that contain
both a literal xi and literals ~xi) found while sorting.
*************************************************************************************/
int sort_literals()
{
int i, j, k, tmp, _result = 0;
for( i = 0; i < nrofclauses; i++ )
for( k = 0; k < Clength[ i ] - 1; k++ )
for( j = 0; j < Clength[ i ] - k - 1; j++ )
{
if( NR(Cv[ i ][ j ]) > NR(Cv[ i ][ j + 1 ]) )
{
tmp = Cv[ i ][ j ];
Cv[ i ][ j ] = Cv[ i ][ j + 1 ];
Cv[ i ][ j + 1 ] = tmp;
}
else if( NR(Cv[ i ][ j ]) == NR(Cv[ i ][ j + 1 ]) )
{
/*
Double literal? -> swap it out of the clause.
*/
if( Cv[ i ][ j ] == Cv[ i ][ j + 1 ] )
Cv[ i ][ j-- ] = Cv[ i ][ --Clength[ i ] ] ;
else
{
/*
The same literal positive and negative.
So a tautology. -> eliminate clause.
*/
Clength[ i ] = 0;
_result++;
}
}
}
return _result;
}
int find_and_propagate_unary_clauses()
{
int i, _result;
simplify_stack = (int*) malloc( sizeof(int) * ( nrofvars + 1) );
simplify_stackp = simplify_stack;
for( i = 0; i < nrofclauses; i++ )
if( Clength[ i ] == 1 )
{ PUSH_PARSER_NA( Cv[ i ][ 0 ] ); }
for( i = 0; i < nrofceq; i++ )
if( CeqSizes[ i ] == 1 )
{ PUSH_PARSER_NA( Ceq[ i ][ 0 ] * CeqValues[ i ] ); }
_result = propagate_unary_clauses();
free( simplify_stack );
return _result;
}
/*
MALLOCS: _Vc, _Vc[], _VcTemp
REALLOCS: -
FREES: _VcTemp, _Vc[ * ], _Vc
*/
int propagate_unary_clauses()
{
int i, j, nrval, clsidx, *_simplify_stackp;
int **_variableArray;
_simplify_stackp = simplify_stack;
allocateSmallVc( &_variableArray , 1 );
_variableArray += nrofvars;
/* fix monotone variables that do not occur in equivalence clauses */
#ifdef FIX_MONOTONE
for( i = 1; i <= nrofvars; i++ )
if( Veq[ i ][ 0 ] == 1 )
{
if( (_variableArray[ i ][ 0 ] == 0) && (_variableArray[ -i ][ 0 ] > 0) )
{
PUSH_PARSER_NA( -i );
}
else if( (_variableArray[ i ][ 0 ] > 0) && (_variableArray[ -i ][ 0 ] == 0) )
{
PUSH_PARSER_NA( i );
}
}
#endif
while( _simplify_stackp < simplify_stackp )
{
nrval = *( _simplify_stackp++ );
freevars--;
/*
All clauses containing nrval are satisfied.
They are removed from the CNF by setting Clength = 0.
*/
for( i = 1; i <= _variableArray[ nrval ][ 0 ] ; i++ )
Clength[ _variableArray[ nrval ][ i ] ] = 0;
/*
All clauses containing ~nrval are shortened by removing ~nrval
from the clause. If this operation results in a unary clause,
then this clause is removed from the CNF by setting Clength = 0
and the literal is pushed on the fix stack to be fixed later.
*/
for( i = 1; i <= _variableArray[ -nrval ][ 0 ]; i++ )
{
clsidx = _variableArray[ -nrval ][ i ];
for( j = 0; j < Clength[ clsidx ]; j++ )
{
if( Cv[ clsidx ][ j ] == -nrval )
{
/*
Swap literal to the front of the clause.
*/
Cv[ clsidx ][ j-- ] = Cv[ clsidx ][ --( Clength[ clsidx ] ) ];
if( Clength[ clsidx ] == 1 )
{
PUSH_PARSER_NA( Cv[ clsidx ][ 0 ] );
Clength[ clsidx ] = 0;
}
}
}
}
while( Veq[ NR(nrval) ][ 0 ] > 1 )
{
clsidx = Veq[ NR(nrval) ][ 1 ];
fixEq( NR(nrval), 1, SGN(nrval));
if( CeqSizes[ clsidx ] == 1 )
{
PUSH_PARSER_NA( Ceq[ clsidx ][ 0 ] * CeqValues[ clsidx ] );
}
}
}
/*
Free temporary allocated space.
*/
_variableArray -= nrofvars;
freeSmallVc( _variableArray );
return 1;
}
int sort_clauses()
{
int i, j, clen, *tmpcls, flag, _nrofclauses, _result;
//printFormula( Cv );
for( i = 0; i < nrofclauses; i++ )
{
clen = Clength[ i ];
tmpcls = (int*) malloc( sizeof( int ) * ( clen + 1 ) );
tmpcls[ 0 ] = clen;
for( j = 0; j < clen; j++ )
tmpcls[ j + 1 ] = Cv[ i ][ j ];
free( Cv[ i ] );
Cv[ i ] = tmpcls;
}
free( Clength );
Clength = NULL;
/*
Quick sort all clauses in the CNF.
*/
qsort( Cv, nrofclauses, sizeof( int* ), clsCompare );
/*
Remove all identical clauses.
*/
for( i = 0; i < nrofclauses - 1; i++ )
{
flag = 1;
for( j = 0; j <= Cv[ i ][ 0 ]; j++ )
if( Cv[ i ][ j ] != Cv[ i + 1 ][ j ] )
{
flag = 0;
break;
}
if( flag ) Cv[ i ][ 0 ] = 0;
}
/*
Restore Clength and Cv.
*/
Clength = (int*) malloc( sizeof( int ) * nrofclauses );
_nrofclauses = 0;
for( i = 0; i < nrofclauses; i++ )
{
clen = Cv[ i ][ 0 ];
if( clen == 0 )
{
free( Cv[ i ] );
Cv[ i ] = NULL;
}
else
{
tmpcls = (int*) malloc( sizeof( int ) * clen );
Clength[ _nrofclauses ] = clen;
for( j = 0; j < clen; j++ )
tmpcls[ j ] = Cv[ i ][ j + 1 ];
free( Cv[ i ] );
Cv[ _nrofclauses ] = tmpcls;
_nrofclauses++;
}
}
Cv = (int**) realloc( Cv , sizeof( int* ) * _nrofclauses );
Clength = (int* ) realloc( Clength, sizeof( int ) * _nrofclauses );
_result = nrofclauses - _nrofclauses;
nrofclauses = _nrofclauses;
compactCNF();
return _result;
}
int find_and_propagate_binary_equivalences()
{
int i, new_bieq = 0;
for( i = 0; i < ( nrofclauses - 1 ); i++ )
{
if( (Clength[ i ] == 2) && (Clength[ i + 1 ] == 2) &&
(Cv[ i ][ 0 ] == -Cv[ i + 1 ][ 0 ]) &&
(Cv[ i ][ 1 ] == -Cv[ i + 1 ][ 1 ]) &&
(NR(Cv[ i ][ 0 ]) != NR(Cv[ i ][ 1 ])) )
{
/*
We found a new bi-equivalency.
*/
new_bieq++;
add_binary_equivalence( Cv[ i ][ 0 ], Cv[ i ][ 1 ] );
//printf("c found binary equivalence %i %i in parser\n", Cv[ i ][ 0 ], Cv[ i ][ 1 ]);
/*
Remove clauses;
*/
Clength[ i ] = 0;
Clength[ i + 1 ] = 0;
}
}
new_bieq += find_and_propagate_bieq();
return new_bieq;
}
/*
MALLOCS: -
REALLOCS: Cv[ .. ], Cv, Clength
FREES: Cv[ .. ]
*/
int compactCNF()
{
int i, _nrofclauses, _result;
_nrofclauses = 0;
for( i = 0; i < nrofclauses; i++ )
{
if( Clength[ i ] == 0 )
{
free( Cv[ i ] );
Cv[ i ] = NULL;
}
else
{
if( i != _nrofclauses )
{
Cv [ _nrofclauses ] = Cv [ i ];
Clength[ _nrofclauses ] = Clength[ i ];
}
_nrofclauses++;
}
}
if( _nrofclauses > 0 )
{
Cv = (int**) realloc( Cv, sizeof( int* ) * _nrofclauses );
Clength = (int* ) realloc( Clength, sizeof( int ) * _nrofclauses );
}
_result = nrofclauses - _nrofclauses;
nrofclauses = _nrofclauses;
return _result;
}
/*
MALLOCS: -
REALLOCS: -
FREES: -
*/
int clsCompare( const void *ptrA, const void *ptrB )
{
int i;
if( NR( *( *(int **) ( ptrA ) + 1 ) ) != NR( *( *(int **) ( ptrB ) + 1 ) ) )
return ( NR( *( *(int **) ( ptrA ) + 1 ) ) - NR( *( *(int **) ( ptrB ) + 1 ) ) > 0 ? -1 : 1 );
if( NR( *( *(int **) ( ptrA ) + 2 ) ) != NR( *( *(int **) ( ptrB ) + 2 ) ) )
return ( NR( *( *(int **) ( ptrA ) + 2 ) ) - NR( *( *(int **) ( ptrB ) + 2 ) ) > 0 ? -1 : 1 );
/*
Now compare the lengths of the clauses.
*/
if( **(int **) ptrA != **(int **) ptrB )
return ( **(int **) ptrA - **(int **) ptrB > 0 ? -1 : 1 );
/*
The lengths of A and B are the same and the first 2 _variables_ are also
the same. So now we take a look at the other _variables_ in the clauses.
*/
for( i = 3; i <= **(int **) ptrA; i++ )
if( NR( *( *(int **) ( ptrA ) + i ) ) != NR( *( *(int **) ( ptrB ) + i ) ) )
return ( NR( *( *(int **) ( ptrA ) + i ) ) - NR( *( *(int **) ( ptrB ) + i ) ) > 0 ? -1 : 1 );
/*
If the two clauses contain the same _variables_, we then consider them as
literals and compare again. ( So, no NR() is used here ). This is done to make
removal of duplets easy.
*/
for( i = 1; i <= **(int **) ptrA; i++ )
if( *( *(int **) ( ptrA ) + i ) != *( *(int **) ( ptrB ) + i ) )
return ( *( *(int **) ( ptrA ) + i ) - *( *(int **) ( ptrB ) + i ) > 0 ? -1 : 1 );
/*
Default value if all is equal. ( Thus a duplet... )
*/
return 1;
}
void printFormula( int** _Cv )
{
int i, j;
for( i = 0; i < nrofclauses; i++ )
{
// printf("clause %i ( %i ): ( ", i, Clength[ i ] );
for( j = 0; j < Clength[ i ]; j++ )
printf("%i ", _Cv[ i ][ j ] );
printf("0\n" );
// printf(")\n" );
}
}