-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxsatz2013.c
3157 lines (2892 loc) · 99.4 KB
/
maxsatz2013.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Based on wpmsz-1.3.c, reformat some functions
*/
/* Based on wpmsz-1.4, with some optimizations
*/
/* Based on wpmsz-1.5, compute the minweight among clauses
involved in conflict, instead of among all clauses involved
in unit propagation
*/
/* Based on wpmsz-1.6, apply rule 1 and rule 2
Rule 1: 1 2, -1 2 ==> 1
Rule 2: 1, -1 ==> empty
*/
/* Based on wpmsz-2.0, merge binary clauses
*/
/* Based on wpmsz-2.2, apply cycle resolution
*/
/* Based on wpsmz-2.4, apply cycle resolution in an
inconsistent subset of clauses, when rules 5 and 6
are not applicable (i.e. other clauses implying
a cycle structure do not form a chain).
*/
/* Based on wpsmz, use arrays instead of linked lists for neg_in and pos_in
*/
/* before failed literal detection in lookahead(), do unit propagation
to fix all unit clauses. In this way, failed literal detection can obtain
smaller conflicting clause sets more easily
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <limits.h>
#include <unistd.h>
#include <sys/resource.h>
#include <math.h>
typedef signed char my_type;
typedef unsigned char my_unsigned_type;
typedef long long int lli_type;
// #define DEBUG
#define WORD_LENGTH 1024
#define TRUE 1
#define FALSE 0
#define NONE -1
#define WEIGHT 4
#define WEIGHT1 25
#define WEIGHT2 5
#define WEIGHT3 1
#define T 10
/* the tables of variables and clauses are statically allocated. Modify the
parameters tab_variable_size and tab_clause_size before compilation if
necessary */
#define tab_variable_size 30000
#define tab_clause_size 1000000
#define tab_unitclause_size ((tab_clause_size/4<2000) ? 2000 : tab_clause_size/4)
#define my_tab_variable_size ((tab_variable_size/2<1000) ? 1000 : tab_variable_size/2)
#define my_tab_clause_size ((tab_clause_size/2<2000) ? 2000 : tab_clause_size/2)
#define my_tab_unitclause_size ((tab_unitclause_size/2<1000) ? 1000 : tab_unitclause_size/2)
#define tab_literal_size 2*tab_variable_size
#define double_tab_clause_size 2*tab_clause_size
#define positive(literal) literal<NB_VAR
#define negative(literal) literal>=NB_VAR
#define get_var_from_lit(literal) \
((literal<NB_VAR) ? literal : literal-NB_VAR)
#define complement(lit1, lit2) \
((lit1<lit2) ? lit2-lit1 == NB_VAR : lit1-lit2 == NB_VAR)
#define get_lit(v, s) ((s == POSITIVE) ? v : v + NB_VAR)
#define tab_clause_size_XL (tab_clause_size * 10)
#define inverse_signe(signe) \
(signe == POSITIVE) ? NEGATIVE : POSITIVE
#define unsat(val) (val==0)?"UNS":"SAT"
#define pop(stack) stack[--stack ## _fill_pointer]
#define push(item, stack) stack[stack ## _fill_pointer++] = item
#define top(stack) stack[stack ## _fill_pointer - 1]
//#define satisfiable() CLAUSE_STACK_fill_pointer == NB_CLAUSE
#define min(a, b) (((a) < (b)) ? (a) : (b))
/*
#define debug_overflow(arrayIndex, maxSize) \
#ifdef DEBUG\
if (arrayIndex > maxSize) {\
printf("DEBUG: arrayIndex.\n"); \
exit(0);\
}\
#endif
*/
#define NEGATIVE 0
#define POSITIVE 1
#define PASSIVE 0
#define ACTIVE 1
my_type var_current_value[tab_variable_size]; // Current assignment of variables
my_type var_rest_value[tab_variable_size]; // Restore vaule of variables
my_type var_state[tab_variable_size]; // Variable status
int saved_lit_in_stack[tab_variable_size];
int saved_clause_stack[tab_variable_size];
int saved_reducedclause_stack[tab_variable_size];
int saved_unitclause_stack[tab_variable_size];
lli_type saved_nb_empty[tab_variable_size];
lli_type nb_neg_clause_of_length1[tab_variable_size];
lli_type nb_pos_clause_of_length1[tab_variable_size];
lli_type nb_neg_clause_of_length2[tab_variable_size];
lli_type nb_neg_clause_of_length3[tab_variable_size];
lli_type nb_pos_clause_of_length2[tab_variable_size];
lli_type nb_pos_clause_of_length3[tab_variable_size];
float reduce_if_negative[tab_variable_size];
float reduce_if_positive[tab_variable_size];
int *sat[tab_clause_size]; // Clauses [clause][literal]
int *var_sign[tab_clause_size]; // Clauses [clause][var,sign]
lli_type clause_weight[tab_clause_size]; // Clause weights
lli_type ini_clause_weight[tab_clause_size]; // Initial clause weights
my_type clause_state[tab_clause_size]; // Clause status
int clause_length[tab_clause_size]; // Clause length
int VARIABLE_STACK_fill_pointer = 0;
int CLAUSE_STACK_fill_pointer = 0;
int UNITCLAUSE_STACK_fill_pointer = 0;
int REDUCEDCLAUSE_STACK_fill_pointer = 0;
int VARIABLE_STACK[tab_variable_size];
int CLAUSE_STACK[tab_clause_size];
int UNITCLAUSE_STACK[tab_unitclause_size];
int REDUCEDCLAUSE_STACK[tab_clause_size];
int PREVIOUS_REDUCEDCLAUSE_STACK_fill_pointer = 0;
lli_type HARD_WEIGHT = 0;
int NB_VAR;
int NB_CLAUSE;
int INIT_NB_CLAUSE;
int INIT_NB_CLAUSE_PREPROC;
int REAL_NB_CLAUSE;
#define INIT_BASE_NB_CLAUSE (tab_clause_size / 2)
int BASE_NB_CLAUSE = INIT_BASE_NB_CLAUSE;
lli_type NB_MONO=0, NB_BRANCHE=0, NB_BACK = 0;
lli_type NB_EMPTY=0, UB;
int instance_type;
int partial;
#define NO_CONFLICT -3
#define NO_REASON -3
int reason[tab_variable_size];
int REASON_STACK[tab_variable_size];
int REASON_STACK_fill_pointer=0;
int MY_UNITCLAUSE_STACK[tab_unitclause_size];
int MY_UNITCLAUSE_STACK_fill_pointer=0;
int CANDIDATE_LITERALS[2*tab_variable_size];
int CANDIDATE_LITERALS_fill_pointer=0;
int NEW_CLAUSES[tab_clause_size][7];
int NEW_CLAUSES_fill_pointer=0;
int lit_to_fix[tab_clause_size];
int SAVED_CLAUSE_POSITIONS[tab_clause_size];
int SAVED_CLAUSES[tab_clause_size];
int SAVED_CLAUSES_fill_pointer=0;
int lit_involved_in_clause[2*tab_variable_size];
int INVOLVED_LIT_STACK[2*tab_variable_size];
int INVOLVED_LIT_STACK_fill_pointer=0;
int fixing_clause[2*tab_variable_size];
int saved_nb_clause[tab_variable_size];
int saved_saved_clauses[tab_variable_size];
int saved_new_clauses[tab_variable_size];
int CLAUSES_TO_REMOVE[tab_clause_size];
int CLAUSES_TO_REMOVE_fill_pointer=0;
lli_type WEIGHTS_TO_REMOVE[tab_clause_size];
int WEIGHTS_TO_REMOVE_fill_pointer=0;
lli_type CLAUSES_WEIGHTS_TO_REMOVE[tab_clause_size];
int CLAUSES_WEIGHTS_TO_REMOVE_fill_pointer=0;
my_type var_best_value[tab_variable_size]; // Best assignment of variables
int SAVED_WEIGHTS_CLAUSE[tab_clause_size];
int SAVED_WEIGHTS_CLAUSE_fill_pointer = 0;
lli_type SAVED_WEIGHTS_WEIGHT[tab_clause_size];
int SAVED_WEIGHTS_WEIGHT_fill_pointer = 0;
int saved_weights_nb[tab_variable_size];
int MARK_STACK[tab_variable_size * 2];
int MARK_STACK_fill_pointer = 0;
int mark[tab_variable_size * 2];
int IG_STACK[tab_unitclause_size];
int IG_STACK_fill_pointer;
int POST_UIP_LITS[tab_variable_size];
int POST_UIP_LITS_fill_pointer;
int NEW_CLAUSE_LITS[tab_variable_size];
int NEW_CLAUSE_LITS_fill_pointer;
int unit_of_var[tab_variable_size];
#define max_var_learned (tab_variable_size / 10)
int undo_learned[tab_variable_size][max_var_learned];
int nb_undo_learned[tab_variable_size];
#define MAX_LEN_LEARNED 20 // MAX_LEN_LEARNED = num_lits * 2 (best performance 20)
/* --------------------------*/
int *neg_in[tab_variable_size];
int *pos_in[tab_variable_size];
int neg_nb[tab_variable_size];
int pos_nb[tab_variable_size];
int saved_pos_nb[tab_variable_size];
int saved_neg_nb[tab_variable_size];
int POS_NEG_FOR_BACKTRACKING_STACK[tab_clause_size_XL];
int POS_NEG_FOR_BACKTRACKING_STACK_fill_pointer= 0;
int saved_saved_pos_neg[tab_variable_size];
//int clause_involved_fl[tab_clause_size];
lli_type simple_get_pos_clause_nb(int);
lli_type simple_get_neg_clause_nb(int);
//int avoid[tab_variable_size];
int in_conflict[tab_clause_size];
int CONFLICTCLAUSE_STACK[tab_clause_size];
int CONFLICTCLAUSE_STACK_fill_pointer=0;
int JOINT_CONFLICT;
int CREATED_UNITCLAUSE_STACK[tab_clause_size];
int CREATED_UNITCLAUSE_STACK_fill_pointer=0;
void add_newclause_in(int var, int sign){
if(sign== POSITIVE) {
pos_in[var][pos_nb[var]++]=NB_CLAUSE;
pos_in[var][pos_nb[var]]=NONE;
}
else{
neg_in[var][neg_nb[var]++]=NB_CLAUSE;
neg_in[var][neg_nb[var]]=NONE;
}
push(var, POS_NEG_FOR_BACKTRACKING_STACK);
push(sign, POS_NEG_FOR_BACKTRACKING_STACK);
}
void zz_print_structure(){
int i, var, clause, *clauses,k=0;
//~ printf("NB_VAR= %d, NB_CLAUSE-BASE_NB_CLAUSE= %d \n", NB_VAR, NB_CLAUSE-BASE_NB_CLAUSE);
//~ printf("-----var list : \n------");
for(i=0; i<NB_VAR; i++){
k=0;
//~ printf("var= %d : \n", i);
//~ printf("pos_in: ");
clauses =pos_in[i];
for(clause=*clauses; clause!=NONE; clause=*(++clauses)) {
//~ printf("%d, ", clause);
k++;
}
if(k!= pos_nb[i])
printf(" bizarre inprint structure \n");
//~ printf("\nneg_in: ");
//~ clauses =neg_in[i];
//~ for(clause=*clauses; clause!=NONE; clause=*(++clauses)) {
//~ printf("%d, ", clause);
//~ }
//~ printf("\n");
//~ }
//~ printf("-----clause list first10 : \n------");
//~ for(i= BASE_NB_CLAUSE; i<= NB_CLAUSE; i++){
//~ printf("%d, ", i);
}
//~ printf("\n");
}
// #include "input.c" //
/* test if the new clause is redundant or subsompted by another */
#define OLD_CLAUSE_REDUNDANT -77
#define NEW_CLAUSE_REDUNDANT -7
int smaller_than(int lit1, int lit2) {
return ((lit1<NB_VAR) ? lit1 : lit1-NB_VAR) <
((lit2<NB_VAR) ? lit2 : lit2-NB_VAR);
}
int redundant(int *new_clause, int *old_clause) {
int lit1, lit2, old_clause_diff=0, new_clause_diff=0;
lit1=*old_clause; lit2=*new_clause;
while ((lit1 != NONE) && (lit2 != NONE)) {
if (smaller_than(lit1, lit2)) {
lit1=*(++old_clause); old_clause_diff++;
} else if (smaller_than(lit2, lit1)) {
lit2=*(++new_clause); new_clause_diff++;
} else if (complement(lit1, lit2)) {
return FALSE; /* old_clause_diff++; new_clause_diff++; j1++; j2++; */
} else {
lit1=*(++old_clause); lit2=*(++new_clause);
}
}
if ((lit1 == NONE) && (old_clause_diff == 0))
/* la nouvelle clause est redondante ou subsumee */
return NEW_CLAUSE_REDUNDANT;
if ((lit2 == NONE) && (new_clause_diff == 0))
/* la old clause est redondante ou subsumee */
return OLD_CLAUSE_REDUNDANT;
return FALSE;
}
void remove_passive_clauses() {
int clause, put_in, first=NONE;
for (clause = BASE_NB_CLAUSE; clause < NB_CLAUSE; clause++) {
if (clause_state[clause]==PASSIVE) {
first=clause; break;
}
}
if (first!=NONE) {
put_in=first;
for(clause=first+1; clause<NB_CLAUSE; clause++) {
if (clause_state[clause]==ACTIVE) {
sat[put_in]=sat[clause]; var_sign[put_in]=var_sign[clause];
clause_state[put_in]=ACTIVE;
clause_length[put_in]=clause_length[clause];
clause_weight[put_in]=clause_weight[clause];
put_in++;
}
}
NB_CLAUSE=put_in;
}
}
void remove_passive_vars_in_clause(int clause) {
int *vars_signs, *vars_signs1, var, var1, first=NONE;
vars_signs=var_sign[clause];
for(var=*vars_signs; var!=NONE; var=*(vars_signs+=2)) {
if (var_state[var]!=ACTIVE) {
first=var; break;
}
}
if (first!=NONE) {
for(vars_signs1=vars_signs+2, var1=*vars_signs1;
var1!=NONE; var1=*(vars_signs1+=2)) {
if (var_state[var1]==ACTIVE) {
*vars_signs=var1; *(vars_signs+1) = *(vars_signs1+1);
vars_signs+=2;
}
}
*vars_signs=NONE;
}
}
/*
int clean_structure() {
int clause, var, *vars_signs;
remove_passive_clauses();
if (NB_CLAUSE == BASE_NB_CLAUSE)
return FALSE;
for (clause = BASE_NB_CLAUSE; clause < NB_CLAUSE; clause++)
remove_passive_vars_in_clause(clause);
LIT_IN_STACK_fill_pointer = BASE_LIT_IN_STACK;
for (var = 0; var < NB_VAR; var++) {
push(NONE, LIT_IN_STACK);
first_neg_in[var] = LIT_IN_STACK_fill_pointer;
last_neg_in[var] = LIT_IN_STACK_fill_pointer;
push(NONE, LIT_IN_STACK);
push(NONE, LIT_IN_STACK);
push(NONE, LIT_IN_STACK);
first_pos_in[var] = LIT_IN_STACK_fill_pointer;
last_pos_in[var] = LIT_IN_STACK_fill_pointer;
push(NONE, LIT_IN_STACK);
push(NONE, LIT_IN_STACK);
}
for (clause = BASE_NB_CLAUSE; clause < NB_CLAUSE; clause++) {
vars_signs=var_sign[clause];
for(var=*vars_signs; var!=NONE; var=*(vars_signs+=2)) {
if (*(vars_signs+1)==POSITIVE)
add_new_lit_in(&last_pos_in[var], clause);
else
add_new_lit_in(&last_neg_in[var], clause);
}
}
return TRUE;
}
*/
int clean_structure() {
int clause, var, *vars_signs;
remove_passive_clauses();
if (NB_CLAUSE == BASE_NB_CLAUSE)
return FALSE;
for (clause = BASE_NB_CLAUSE; clause < NB_CLAUSE; clause++)
remove_passive_vars_in_clause(clause);
for (var = 0; var < NB_VAR; var++) {
neg_nb[var] = 0;
pos_nb[var] = 0;
}
for (clause=BASE_NB_CLAUSE; clause<NB_CLAUSE; clause++) {
vars_signs=var_sign[clause];
for(var=*vars_signs; var!=NONE; var=*(vars_signs+=2)) {
if (*(vars_signs+1)==POSITIVE)
pos_in[var][pos_nb[var]++]=clause;
else neg_in[var][neg_nb[var]++]=clause;
}
}
for (var=0; var<NB_VAR; var++) {
neg_in[var][neg_nb[var]]=NONE;
pos_in[var][pos_nb[var]]=NONE;
}
return TRUE;
}
void lire_clauses(FILE *fp_in, int instance_type) {
int i, j, jj, ii, length, tautologie, lits[10000], lit, lit1;
lli_type weight;
partial = 0;
if (HARD_WEIGHT > 0) // For partial
partial = 1;
for (i = BASE_NB_CLAUSE; i < NB_CLAUSE; i++) {
length=0;
if (instance_type != 0)
fscanf(fp_in, "%lli", &weight);
else
weight = 1;
fscanf(fp_in, "%d", &lits[length]);
while (lits[length] != 0) {
length++;
fscanf(fp_in, "%d", &lits[length]);
}
tautologie = FALSE;
/* test if some literals are redundant and sort the clause */
for (ii=0; ii<length-1; ii++) {
lit = lits[ii];
for (jj=ii+1; jj<length; jj++) {
if (abs(lit)>abs(lits[jj])) { // swap
lit1=lits[jj]; lits[jj]=lit; lit=lit1;
} else if (lit == lits[jj]) { // x v x = x
lits[jj] = lits[length-1];
jj--; length--; lits[length] = 0;
printf("literal %d is redundant in clause %d \n",
lit, i+1);
} else if (abs(lit) == abs(lits[jj])) { // x v -x = T
tautologie = TRUE; break;
}
}
if (tautologie == TRUE) break;
else lits[ii] = lit;
}
if (tautologie == FALSE) {
sat[i]= (int *)malloc((length+1) * sizeof(int));
for (j=0; j<length; j++) {
if (lits[j] < 0)
sat[i][j] = abs(lits[j]) - 1 + NB_VAR ;
else
sat[i][j] = lits[j]-1;
}
sat[i][length]=NONE;
clause_length[i]=length;
clause_weight[i] = weight;
if (partial == 0)
HARD_WEIGHT += weight;
clause_state[i] = ACTIVE;
} else {
i--;
NB_CLAUSE--;
}
}
}
void build_structure() {
int i, j, var, *lits1, length, clause, *vars_signs, lit;
int *clique_set;
for (i=0; i<NB_VAR; i++) {
neg_nb[i] = 0; pos_nb[i] = 0;
}
for (i=BASE_NB_CLAUSE; i<NB_CLAUSE; i++) {
for(j=0; j<clause_length[i]; j++) {
if (sat[i][j]>=NB_VAR) {
var=sat[i][j]-NB_VAR; neg_nb[var]++;
}
else {
var=sat[i][j]; pos_nb[var]++;
}
}
if (sat[i][clause_length[i]] !=NONE)
printf("erreur ");
}
for(clause=BASE_NB_CLAUSE;clause<NB_CLAUSE;clause++) {
length = clause_length[clause];
var_sign[clause] = (int *)malloc((2*length+1)*sizeof(int));
//~ flag_in_stack[clause]= FALSE;
lits1 = sat[clause]; vars_signs = var_sign[clause];
for(lit=*lits1; lit!=NONE; lit=*(++lits1),(vars_signs+=2)) {
if (negative(lit)) {
*(vars_signs+1)= NEGATIVE;
*vars_signs = get_var_from_lit(lit);
}
else {
*(vars_signs+1)=POSITIVE;
*vars_signs = lit;
}
}
*vars_signs = NONE;
}
for (i=0; i<NB_VAR; i++) {
neg_in[i] = (int *)malloc((100*neg_nb[i]+1) * sizeof(int));
pos_in[i] = (int *)malloc((100*pos_nb[i]+1) * sizeof(int));
neg_in[i][neg_nb[i]]=NONE; pos_in[i][pos_nb[i]]=NONE;
neg_nb[i] = 0; pos_nb[i] = 0;
var_state[i] = ACTIVE;
}
for (i=BASE_NB_CLAUSE; i<NB_CLAUSE; i++) {
lits1 = sat[i];
for(lit=*lits1; lit!=NONE; lit=*(++lits1)) {
if (positive(lit))
pos_in[lit][pos_nb[lit]++] = i;
else
neg_in[get_var_from_lit(lit)]
[neg_nb[get_var_from_lit(lit)]++] = i;
}
}
}
void eliminate_redundance() {
int i;
for (i = BASE_NB_CLAUSE; i < NB_CLAUSE; i++) {
if (clause_state[i]==ACTIVE) {
if (clause_length[i]==1)
push(i, UNITCLAUSE_STACK);
}
}
}
int build_simple_sat_instance(char *input_file) {
FILE* fp_in=fopen(input_file, "r");
char ch, word2[WORD_LENGTH];
int i;
char pLine[WORD_LENGTH];
if (fp_in == NULL) {
return FALSE;
}
fscanf(fp_in, "%c", &ch);
while (ch!='p') {
while (ch!='\n') fscanf(fp_in, "%c", &ch);
fscanf(fp_in, "%c", &ch);
}
i = 0;
while (ch != '\n') {
pLine[i] = ch;
i++;
fscanf(fp_in, "%c", &ch);
}
sscanf(pLine, "p %s %d %d %lli",
word2, &NB_VAR, &NB_CLAUSE, &HARD_WEIGHT);
printf("c Instance info: p %s %d %d %lli\n",
word2, NB_VAR, NB_CLAUSE, HARD_WEIGHT);
if (NB_VAR > tab_variable_size ||
NB_CLAUSE > tab_clause_size - INIT_BASE_NB_CLAUSE) {
printf("ERROR: Out of memory.\n");
exit(0);
}
NB_CLAUSE = NB_CLAUSE + BASE_NB_CLAUSE;
INIT_NB_CLAUSE = NB_CLAUSE;
if (strcmp(word2, "cnf") == 0)
instance_type = 0; // cnf
else {
instance_type = 1; // wcnf
}
lire_clauses(fp_in, instance_type);
fclose(fp_in);
build_structure();
eliminate_redundance();
if (clean_structure()==FALSE)
return FALSE;
return TRUE;
}
// end of input.c
void print_clause(int clause) {
int *vars_signs, var;
printf("(%i, %i, %i) [%lli] ", clause, clause_length[clause],
clause_state[clause], clause_weight[clause]);
vars_signs = var_sign[clause];
for(var = *vars_signs; var != NONE; var = *(vars_signs += 2)) {
if (*(vars_signs + 1) == NEGATIVE)
printf("-");
printf("%i ", var + 1);
}
printf("0");
}
void remove_clauses(int var) {
register int clause, *clauses;
int p_clause;
if (var_current_value[var] == POSITIVE)
clauses =pos_in[var];
else
clauses =neg_in[var];
for(clause=*clauses; clause!=NONE; clause=*(++clauses)) {
if (clause_state[clause] == ACTIVE) {
clause_state[clause] = PASSIVE;
push(clause, CLAUSE_STACK);
}
}
}
int reduce_clauses(int var) {
register int clause, *clauses;
int p_clause;
if (var_current_value[var] == POSITIVE)
clauses =neg_in[var];
else
clauses =pos_in[var];
for(clause=*clauses; clause!=NONE; clause=*(++clauses)) {
if (clause_state[clause] == ACTIVE) {
clause_length[clause]--;
push(clause, REDUCEDCLAUSE_STACK);
switch (clause_length[clause]) {
case 0:
NB_EMPTY += clause_weight[clause];
if (UB<=NB_EMPTY) { /// Test if this check is needed
push(clause, IG_STACK);
push(var, MARK_STACK);
mark[var] = MARK_STACK_fill_pointer;
unit_of_var[var] = clause;
return NONE;
}
break;
case 1:
push(clause, UNITCLAUSE_STACK);
#ifdef DEBUG
if (UNITCLAUSE_STACK_fill_pointer > tab_unitclause_size - 5) {
printf("DEBUG: UNITCLAUSE_STACK.\n");
exit(0);
}
#endif
break;
}
}
}
return TRUE;
}
int my_reduce_clauses(int var) {
register int clause, *clauses;
int p_clause;
if (var_current_value[var] == POSITIVE)
clauses =neg_in[var];
else
clauses =pos_in[var];
for(clause=*clauses; clause!=NONE; clause=*(++clauses)) {
if (clause_state[clause] == ACTIVE) {
clause_length[clause]--;
push(clause, REDUCEDCLAUSE_STACK);
switch (clause_length[clause]) {
case 0:
return clause;
case 1:
push(clause, MY_UNITCLAUSE_STACK);
#ifdef DEBUG
if (MY_UNITCLAUSE_STACK_fill_pointer > tab_unitclause_size - 5) {
printf("DEBUG: MY_UNITCLAUSE_STACK.\n");
exit(0);
}
#endif
break;
}
}
}
return NO_CONFLICT;
}
int my_reduce_clauses_for_fl(int var) {
register int clause, *clauses;
int p_clause;
if (var_current_value[var] == POSITIVE)
clauses =neg_in[var];
else
clauses =pos_in[var];
for(clause=*clauses; clause!=NONE; clause=*(++clauses)) {
if (clause_state[clause] == ACTIVE) {
clause_length[clause]--;
push(clause, REDUCEDCLAUSE_STACK);
switch (clause_length[clause]) {
case 0:
return clause;
case 1:
push(clause, UNITCLAUSE_STACK);
break;
}
}
}
return NO_CONFLICT;
}
void print_values(int nb_var) {
FILE* fp_out;
int i;
fp_out = fopen("satx.sol", "w");
for (i=0; i<nb_var; i++) {
if (var_current_value[i] == 1)
fprintf(fp_out, "%d ", i+1);
else
fprintf(fp_out, "%d ", 0-i-1);
}
fprintf(fp_out, "\n");
fclose(fp_out);
}
int backtracking() {
int var, index,clause, saved;
int *vars_signs, var_s, sign, v;
NB_BACK++;
//~ printf("NB_BACK= %lld var= %d, before backtracking: \n", NB_BACK, var);
//~ zz_print_structure();
while (VARIABLE_STACK_fill_pointer > 0) {
var = pop(VARIABLE_STACK);
if (nb_undo_learned[var] > 0) {
for (index = 0; index < nb_undo_learned[var]; index++) {
clause = undo_learned[var][index];
clause_length[clause]++;
}
nb_undo_learned[var] = 0;
}
if (var_rest_value[var] == NONE)
var_state[var] = ACTIVE;
else {
for (index = saved_clause_stack[var];
index < CLAUSE_STACK_fill_pointer; index++)
clause_state[CLAUSE_STACK[index]] = ACTIVE;
CLAUSE_STACK_fill_pointer = saved_clause_stack[var];
for (index = saved_reducedclause_stack[var];
index < REDUCEDCLAUSE_STACK_fill_pointer; index++) {
//clause = REDUCEDCLAUSE_STACK[index];
clause_length[REDUCEDCLAUSE_STACK[index]]++;
}
REDUCEDCLAUSE_STACK_fill_pointer = saved_reducedclause_stack[var];
UNITCLAUSE_STACK_fill_pointer=saved_unitclause_stack[var];
NB_EMPTY=saved_nb_empty[var];
NB_CLAUSE=saved_nb_clause[var];
NEW_CLAUSES_fill_pointer=saved_new_clauses[var];
//~ saved=saved_saved_clauses[var];
//~ for (index = SAVED_CLAUSES_fill_pointer-1; index >= saved; index--)
//~ LIT_IN_STACK[SAVED_CLAUSE_POSITIONS[index]]=SAVED_CLAUSES[index];
//~ SAVED_CLAUSES_fill_pointer=saved;
saved= saved_saved_pos_neg[var];
for(index= POS_NEG_FOR_BACKTRACKING_STACK_fill_pointer -1; index>= saved; index-=2){
sign= POS_NEG_FOR_BACKTRACKING_STACK[index];
v= POS_NEG_FOR_BACKTRACKING_STACK[index-1];
if(sign==POSITIVE){
pos_nb[v]--; pos_in[v][pos_nb[v]]= NONE;
if(pos_nb[v]<=0)
printf("bizarre pos\n");
}
if(sign==NEGATIVE){
neg_nb[v]--; neg_in[v][neg_nb[v]]= NONE;
if(neg_nb[v]<=0)
printf("bizarre neg\n");
}
}
POS_NEG_FOR_BACKTRACKING_STACK_fill_pointer= saved;
saved = saved_weights_nb[var];
for (index = SAVED_WEIGHTS_CLAUSE_fill_pointer - 1;
index >= saved; index--)
clause_weight[SAVED_WEIGHTS_CLAUSE[index]] =
SAVED_WEIGHTS_WEIGHT[index];
SAVED_WEIGHTS_CLAUSE_fill_pointer = saved;
SAVED_WEIGHTS_WEIGHT_fill_pointer = saved;
/*
saved = saved_lit_in_stack[var];
for (index = LIT_IN_STACK_fill_pointer - 2;
index > saved; index -= 3) {
vars_signs = var_sign[LIT_IN_STACK[LIT_IN_STACK[index - 1]]];
for(var_s = *vars_signs; var_s != NONE;
var_s = *(vars_signs += 2)) {
if (last_pos_in[var_s] == index) {
last_pos_in[var_s] = LIT_IN_STACK[index - 1];
break;
}
if (last_neg_in[var_s] == index) {
last_neg_in[var_s] = LIT_IN_STACK[index - 1];
break;
}
}
LIT_IN_STACK[LIT_IN_STACK[index - 1] + 1] = NONE;
LIT_IN_STACK[LIT_IN_STACK[index - 1]] = NONE;
}
LIT_IN_STACK_fill_pointer = saved;
*/
if (NB_EMPTY<UB) {
var_current_value[var] = var_rest_value[var];
var_rest_value[var] = NONE;
push(var, VARIABLE_STACK);
if (reduce_clauses(var)==NONE)
return NONE;
remove_clauses(var);
return TRUE;
} else
var_state[var] = ACTIVE;
}
}
//~ printf("NB_BACK= %lld \n", NB_BACK);
//~ zz_print_structure();
return FALSE;
}
int verify_solution() {
int i, var, *vars_signs, clause_truth;
lli_type nb = 0;
for (i = INIT_BASE_NB_CLAUSE; i < REAL_NB_CLAUSE; i++) {
clause_truth = FALSE;
vars_signs = var_sign[i];
for(var=*vars_signs; var!=NONE; var=*(vars_signs+=2))
if (*(vars_signs+1) == var_current_value[var] ) {
clause_truth = TRUE;
break;
}
if (clause_truth == FALSE) {
nb += ini_clause_weight[i];
}
}
return nb;
}
void reset_context(int saved_clause_stack_fill_pointer,
int saved_reducedclause_stack_fill_pointer,
int saved_unitclause_stack_fill_pointer,
int saved_variable_stack_fill_pointer) {
int index, var, clause;
for (index = saved_clause_stack_fill_pointer;
index < CLAUSE_STACK_fill_pointer; index++)
clause_state[CLAUSE_STACK[index]] = ACTIVE;
CLAUSE_STACK_fill_pointer = saved_clause_stack_fill_pointer;
for (index = saved_reducedclause_stack_fill_pointer;
index < REDUCEDCLAUSE_STACK_fill_pointer; index++) {
clause = REDUCEDCLAUSE_STACK[index];
clause_length[REDUCEDCLAUSE_STACK[index]]++;
}
REDUCEDCLAUSE_STACK_fill_pointer =
saved_reducedclause_stack_fill_pointer;
for(index=saved_variable_stack_fill_pointer;
index<VARIABLE_STACK_fill_pointer; index++) {
var=VARIABLE_STACK[index];
reason[var]=NO_REASON;
var_state[var]=ACTIVE;
}
VARIABLE_STACK_fill_pointer=saved_variable_stack_fill_pointer;
UNITCLAUSE_STACK_fill_pointer=saved_unitclause_stack_fill_pointer;
}
void create_binaryclause(int var1, int sign1,
int var2, int sign2,
lli_type min_weight) {
int *vars_signs;
vars_signs=NEW_CLAUSES[NEW_CLAUSES_fill_pointer++];
if (var1<var2) {
vars_signs[0]=var1; vars_signs[1]=sign1;
vars_signs[2]=var2; vars_signs[3]=sign2;
} else {
vars_signs[0]=var2; vars_signs[1]=sign2;
vars_signs[2]=var1; vars_signs[3]=sign1;
}
vars_signs[4]=NONE;
var_sign[NB_CLAUSE]=vars_signs;
clause_state[NB_CLAUSE]=ACTIVE;
clause_length[NB_CLAUSE]=2;
clause_weight[NB_CLAUSE] = min_weight;
add_newclause_in(var1, sign1);
add_newclause_in(var2, sign2);
NB_CLAUSE++;
#ifdef DEBUG
if (NB_CLAUSE > tab_clause_size - 5) {
printf("DEBUG: NB_CLAUSE.\n");
exit(0);
}
#endif
}
int verify_binary_clauses(int *varssigns, int var1, int sign1,
int var2, int sign2) {
//int nb=0;
if (var1==*varssigns) {
if ((*(varssigns+1)!=1-sign1) || (var2!=*(varssigns+2)) ||
(*(varssigns+3)!=1-sign2)) {
printf("VBC problem..");
return FALSE;
}
}
else {
if ((var2 != *varssigns) ||
(*(varssigns+1)!=1-sign2) ||
(var1!=*(varssigns+2)) ||
(*(varssigns+3)!=1-sign1)) {
printf("VBC problem..");
return FALSE;
}
}
return TRUE;
}
int LINEAR_REASON_STACK1[tab_clause_size];
int LINEAR_REASON_STACK1_fill_pointer=0;
int LINEAR_REASON_STACK2[tab_clause_size];
int LINEAR_REASON_STACK2_fill_pointer=0;
int clause_involved[tab_clause_size];
int clause_entered[tab_clause_size];
int search_linear_reason1(int var) {
int *vars_signs, clause, fixed_var, index_var, new_fixed_var;
for(fixed_var=var; fixed_var!=NONE; fixed_var=new_fixed_var) {
clause=reason[fixed_var];
vars_signs = var_sign[clause];
new_fixed_var=NONE;
push(clause, LINEAR_REASON_STACK1);
clause_involved[clause]=TRUE;
for(index_var=*vars_signs; index_var!=NONE;
index_var=*(vars_signs+=2)) {
if ((index_var!=fixed_var) && (reason[index_var]!=NO_REASON)) {
if (new_fixed_var==NONE)
new_fixed_var=index_var;
else {
return FALSE;
}
}
}
}
return TRUE;
}
#define SIMPLE_NON_LINEAR_CASE 2
#define SIMPLE_RS1_NON_LINEAR_CASE 3
#define SIMPLE_RS2_NON_LINEAR_CASE 4
#define SIMPLE_CUB_NON_LINEAR_CASE 5
#define SIMPLE_RS1_3_NON_LINEAR_CASE 6
#define SIMPLE_RS2_3_NON_LINEAR_CASE 7
int search_linear_reason2(int var) {
int *vars_signs, clause, fixed_var, index_var, new_fixed_var;
for(fixed_var=var; fixed_var!=NONE; fixed_var=new_fixed_var) {
clause=reason[fixed_var];
if (clause_involved[clause]==TRUE) {
if (LINEAR_REASON_STACK2_fill_pointer == 2 &&
LINEAR_REASON_STACK1_fill_pointer > 2 &&
LINEAR_REASON_STACK1[ 2 ] == clause)
return SIMPLE_NON_LINEAR_CASE;
else if (LINEAR_REASON_STACK2_fill_pointer == 2 &&
LINEAR_REASON_STACK1_fill_pointer > 3 &&
LINEAR_REASON_STACK1[ 3 ] == clause)
return SIMPLE_RS2_NON_LINEAR_CASE;
else if (LINEAR_REASON_STACK2_fill_pointer == 3 &&
LINEAR_REASON_STACK1_fill_pointer > 2 &&
LINEAR_REASON_STACK1[ 2 ] == clause)
return SIMPLE_RS1_NON_LINEAR_CASE;
/*