-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmaxsat_mini.c
1490 lines (1413 loc) · 48.8 KB
/
zmaxsat_mini.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
/**************************************************************************************************
MaxSAT adopted clause learning based minisat framwork.
**************************************************************************************************/
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <limits.h>
typedef unsigned int uint32_t;
typedef unsigned long long int uint64_t;
#define WORD_LENGTH 1024
#define TRUE 1
#define FALSE 0
#define ACTIVE 1
#define PASSIVE 0
#define NONE -1
#define l_True 0
#define l_False 1
#define l_Undef 2
#define pop(stack) stack[--stack ## _fill_pointer]
#define push(stack,item) stack[stack ## _fill_pointer++] = item
#define top(stack) stack[stack ## _fill_pointer - 1]
#define neglit(p) p^1
#define parent(i) (i-1)>>1
#define left(i) i*2+1
#define right(i) (i+1)*2
#define ZEROLEN -1
#define NO_CONFLICT -2
#define NO_REASON -1
#define NEW_LCLAUSE 2
#define learntClen 1024
#define clausesRegion_init_cap 4194304
#define clauses_number_cap 8388608
#define my_clauses_number_cap 1048576
uint64_t HARD_WEIGHT = 0;
uint32_t NB_VAR,NB_CLAUSE,originalClauseN,BRANCH_NODE=0,NB_BRANCHE=0;//
uint32_t LB=0,UB,NB_BACK=0,conflict_number=0,prior_confsets;//backing number;valid conflict analyz number;store learnt clause's set
uint32_t instanceType,partial=0,NB_EMPTY=0;
//activities option
int var_inc; // Amount to bump next variable with.
int var_decay; // INVERSE decay factor for variable activity: stores 1/decay.
int cla_inc; // Amount to bump next clause with.
int cla_decay; // INVERSE decay factor for clause activity: stores 1/decay.
uint32_t maxLearntClause;
struct VarValue{int current; int rest;};
struct VarData { int Vstate; int reason; int level; };
struct Watcher {int cn; int blocker;}; //clause number and literal
struct VECW {uint32_t wsize; uint32_t wcap; struct Watcher * watchlit;};
struct Clstate{int Cstate;int involved;int weight;};
struct Vec{uint32_t vsize; uint32_t vcap; int* varindex;};
//clauses store and it's property
int * clausesRegion;
uint32_t clausesRegion_size=0;
uint32_t clausesRegion_cap;
uint32_t clauses_wasted=0;
int clauses_index[clauses_number_cap]; // List of clauses ,store clauses' address in clausesRegion
int clauses_index_fill_pointer=0;
struct Clstate clause_reduce_state[clauses_number_cap]; //claues's reason dl
uint32_t clauseActivity[clauses_number_cap];
//reduce clause
int clause_stack[clauses_number_cap]; //store passive clause such as satisfied clause, empty clause ,can not be used learnt clause
int clause_stack_fill_pointer=0;
int unitclause_stack[my_clauses_number_cap]; //store unit clause
int unitclause_stack_fill_pointer=0;
int * conSet_of_learnc; //reason clause reduced learnt clause
int conSet_of_learnc_fill_pointer=0;
int conSet_of_learnc_cap;
int addr_of_learncSet[clauses_number_cap]; //address of learn clause's conflict set
int clauseDelete[clauses_number_cap];
int clauseDelete_fill_pointer=0;
int clauseDelMark[clauses_number_cap];
struct Vec * Iclause_index_Lclause;//each clause have learnt clause
// int Iclause_index_fill_pointer[clauses_number_cap];
int my_unit_clause_stack[my_clauses_number_cap];
int my_unit_clause_stack_fill_pointer=0;
int learntclause[my_clauses_number_cap];
int learntclause_fill_pointer=0;
int* saved_clause_stack; //store for backtracking
int* saved_unitclause_stack;
int* saved_nb_empty;
int* saved_zeroLclause_stack;
int zero_learnt_clause[my_clauses_number_cap]; //parial assignment reduce learnt clause to empty
int zero_learnt_clause_fill_pointer=0;
int * reason_stack; //store conflict clause in inference graph
int reason_stack_fill_pointer=0;
int current_learnt_level;
int BCP_variable_index;
int temp_learnt_clause[learntClen]; //store new learnt clause
int temp_learnt_clause_fill_pointer=0;
int DelCount=0;
//watcher list and var index
struct VECW * watcherlist; // two literal watching
struct Vec * literal_in; //store the id of clause includes literal, works as neg_in pos_in in maxsatz
//variables store and it's property
int * varCandidate_STACK; //store undefy variable
int varCandidate_STACK_fill_pointer=0;
int * varIndices; //variable index in heap
uint32_t * varActivity;//variable score
struct VarValue * varAssigns; // Current and rest values of variables.
int * varBestValue; // problem solution
struct VarData * var_reduce_state; //state, reason, decisional level, it is equal to decision vector of MINISAT
int * seen;
int * trail;//store reduced literal
int trail_fill_pointer=0;
int * dirtyLiteral;
int isTrue(int p){return (varAssigns[p>>1].current)^(p&1);}
int pushVetw(struct VECW * wl, int crn, int li){
struct Watcher * ws; uint32_t place=wl->wsize;
if(place==wl->wcap){
wl->watchlit=(struct Watcher *)realloc(wl->watchlit,(wl->wcap)*2*sizeof(struct Watcher));
if(wl->watchlit==NULL) {printf("watchers space assignment erro\n"); return FALSE;}
wl->wcap*=2;
}
ws=wl->watchlit;
ws[place].cn=crn;
ws[place].blocker=li;
(*wl).wsize++;
}
int popVetw(struct VECW* wl, uint32_t crn, int li){
}
void percolateUP(int i){
int x=varCandidate_STACK[i]; int p=parent(i);
while(i!=0&&(varActivity[x]>varActivity[varCandidate_STACK[p]])){
varCandidate_STACK[i]=varCandidate_STACK[p];
varIndices[varCandidate_STACK[p]]=i;
i=p;p=parent(p);
}
varCandidate_STACK[i]=x;
varIndices[x]=i;
}
void percolateDown(int i){
int child,lvar,rvar,x;
x=varCandidate_STACK[i];
while(left(i)<varCandidate_STACK_fill_pointer){
child=(right(i)<varCandidate_STACK_fill_pointer
&&(varActivity[varCandidate_STACK[right(i)]]>varActivity[varCandidate_STACK[left(i)]]))?right(i):left(i);
if(varActivity[x]>=varActivity[varCandidate_STACK[child]])
break;
varCandidate_STACK[i]=varCandidate_STACK[child];
varIndices[varCandidate_STACK[child]]=i;
i=child;
}
varCandidate_STACK[i]=x;
varIndices[x]=i;
}
void updateVar(int var){
int i;
varActivity[var]++;
if(varActivity[var]>1048576){
for(i=0;i<NB_VAR;i++)
varActivity[i]=varActivity[i]/2;
}
if(varIndices[var]!=NONE) //if varialble in heap,update according it's new score
percolateUP(varIndices[var]);
// percolateDown(varIndices[var]);
}
//pick the max score variable from var heap
int removeMax_var(){
int x=varCandidate_STACK[0]; int var=varCandidate_STACK[varCandidate_STACK_fill_pointer-1];
varCandidate_STACK[0]=var;
varIndices[var]=0;
varIndices[x]=NONE;
varCandidate_STACK_fill_pointer--;
if(varCandidate_STACK_fill_pointer>1)
percolateDown(0);
return x;
}
//candidate variable insert the heap
void insertVar(int var){
varIndices[var]=varCandidate_STACK_fill_pointer;
push(varCandidate_STACK,var);
percolateUP(varIndices[var]);
}
//build two literal watchers
void attachClause(int cn,int cr,int size){
int * c=&clausesRegion[cr+1];
if(size==1){ //unit clause
pushVetw(&watcherlist[neglit(c[0])], cn,NONE);
if(unitclause_stack_fill_pointer>=my_clauses_number_cap){printf("unitclause overflow\n"); }
push(unitclause_stack,cn);
}
else{ //add clause to wathers list
pushVetw(&watcherlist[neglit(c[0])], cn, c[1]);
pushVetw(&watcherlist[neglit(c[1])], cn, c[0]);
}
}
//build pos_in ,neg_in for remove satisfied clauses
int attachLiteral(int cn, int * literal,int size){
int lit,i;
for(i=0;i<size;i++){
lit=literal[i];
if(literal_in[lit].vsize==0){
literal_in[lit].varindex=(int *)malloc(sizeof(int)*my_clauses_number_cap);
if(literal_in[lit].varindex==NULL){printf("literal_in malloc error\n"); return FALSE;}
literal_in[lit].vcap=my_clauses_number_cap;
}
if(literal_in[lit].vsize==literal_in[lit].vcap){
literal_in[lit].varindex=(int *)realloc(literal_in[lit].varindex,sizeof(int)*(literal_in[lit].vcap<<1));
literal_in[lit].vcap=literal_in[lit].vcap<<1;
if(literal_in[lit].varindex==NULL) {printf("literal_in realloc error\n");return FALSE;}
}
literal_in[lit].varindex[literal_in[lit].vsize++]=cn;
}
return TRUE;
}
int initStoreSpace(uint32_t varn,uint32_t clausesn) {
uint32_t litn,i;
if(clausesn*2>=clauses_number_cap)
printf("inputfile clauses number is big\n");
litn=varn<<1;
var_reduce_state=(struct VarData *)malloc(varn*sizeof(struct VarData));
seen=(int *)malloc(varn*sizeof(int));
varActivity=(uint32_t *)malloc(varn*sizeof(uint32_t));
varAssigns =(struct VarValue *)malloc(varn*sizeof(struct VarValue));
varBestValue=(int *)malloc(varn*sizeof(int));
varIndices=(int *)malloc(varn*sizeof(int));
trail=(int *)malloc(varn*sizeof(int));
varCandidate_STACK=(int *)malloc(varn*sizeof(int));
saved_clause_stack=(int *)malloc(varn*sizeof(int));
saved_unitclause_stack=(int *)malloc(varn*sizeof(int));
saved_nb_empty=(int *)malloc(varn*sizeof(int));
saved_zeroLclause_stack=(int *)malloc(varn*sizeof(int));
reason_stack=(int *)malloc(varn*sizeof(int));
dirtyLiteral=(int *)malloc(litn*sizeof(int));
if(var_reduce_state==NULL||seen==NULL||varActivity==NULL||varAssigns==NULL||saved_unitclause_stack==NULL
||varIndices==NULL||varCandidate_STACK==NULL||varBestValue==NULL||trail==NULL||saved_clause_stack==NULL
||saved_nb_empty==NULL||saved_zeroLclause_stack==NULL||dirtyLiteral==NULL)
return FALSE;
for(i=0;i<varn;i++)
varActivity[i]=0; //should init it here, input file changes the value
for(i=0;i<NB_CLAUSE;i++){ //only score the learnt clauses
clauseActivity[i]=0;
}
conSet_of_learnc=(int *)malloc((clausesRegion_init_cap)*sizeof(int));
if(conSet_of_learnc==NULL) return FALSE;
conSet_of_learnc_cap=clausesRegion_init_cap;
clausesRegion=(int *)malloc((clausesRegion_init_cap)*sizeof(int));
if(clausesRegion==NULL)
return FALSE;
clausesRegion_cap=clausesRegion_init_cap;
clausesRegion_size=0;
watcherlist=(struct VECW*)malloc(litn*sizeof(struct VECW));
for(i=0;i<litn;i++){
watcherlist[i].watchlit=(struct Watcher *)malloc(clauses_number_cap*sizeof(struct Watcher));
watcherlist[i].wsize=0;
watcherlist[i].wcap=clauses_number_cap;
}
Iclause_index_Lclause=(struct Vec*)malloc(clauses_number_cap*sizeof(struct Vec));
literal_in=(struct Vec *)malloc(litn*sizeof(struct Vec));
for(i=0;i<litn;i++){
literal_in[i].vsize=0; literal_in[i].vcap=0;
}
}
void freespace( ){
int i;
if(var_reduce_state!=NULL)
free(var_reduce_state);
free(seen);free(varActivity);free(varAssigns);free(varBestValue);free(varIndices);
free(trail);free(varCandidate_STACK);free(saved_clause_stack);free(saved_nb_empty);free(saved_unitclause_stack);
free(saved_zeroLclause_stack);free(reason_stack);free(dirtyLiteral);//about var 14
free(clausesRegion); free(conSet_of_learnc);
for(i=0;i<NB_VAR+NB_VAR;i++){
free(watcherlist[i].watchlit);
free(literal_in[i].varindex);
}
free(watcherlist);
free(literal_in);
for(i=0;i<NB_CLAUSE;i++){
if(Iclause_index_Lclause[i].vcap!=0)
free(Iclause_index_Lclause[i].varindex);
}
}
int setOptions(){
int i;
HARD_WEIGHT=NB_CLAUSE;
originalClauseN=NB_CLAUSE;
maxLearntClause=originalClauseN/3;
if(clauses_number_cap<originalClauseN+4*maxLearntClause)
{printf("reset store space \n"); return FALSE;}
for(i=0;i<NB_VAR;i++){
var_reduce_state[i].level=NONE;
var_reduce_state[i].reason=NO_REASON;
var_reduce_state[i].Vstate=ACTIVE;
}
for(i=0;i<NB_VAR;i++){
seen[i]=FALSE;
varAssigns[i].current =l_Undef;
varIndices[i]=NONE;
}
for(i=0;i<2*NB_VAR;i++)
dirtyLiteral[i]=FALSE;
for(i=0;i<NB_CLAUSE;i++){ //here, is better than in storeclause
clause_reduce_state[i].Cstate=ACTIVE;
clause_reduce_state[i].involved=FALSE;
}
for(i=0;i<NB_CLAUSE;i++){
Iclause_index_Lclause[i].vsize=0;
Iclause_index_Lclause[i].vcap=0;
}
for(i=0;i<NB_VAR;i++)
insertVar(i);
return TRUE;
}
char * skipWhitespace(char* sin) {
while ((*sin >= 9 && *sin <= 13) || *sin == 32)
sin++;
return sin;
}
char * parseInt(char* in, int * literal) {
int val = 0, _neg = 0;
in=skipWhitespace(in);
if (*in == '-'){
_neg = 1; in++;}
else if (*in == '+') in++;
if (*in < '0' || *in > '9'){
printf("PARSE ERROR! Unexpected char: %d\n", *in);}
while (*in >= '0' && *in <= '9'){
val = val*10 + (*in - '0');
in++;
}
*literal= _neg ? -val : val;
return in;
}
int storeClauses(int * lits, int Csize){
int cr,i;
if(clausesRegion_size+Csize+1>=clausesRegion_cap){
clausesRegion=(int *)realloc(clausesRegion,sizeof(int)*( clausesRegion_cap<<1));
if(clausesRegion==NULL) return FALSE;
clausesRegion_cap= clausesRegion_cap<<1;
}
//store clause according the format: size state, literal1, literal2 and so on.
cr=clausesRegion_size;
clausesRegion[cr++]=Csize;
for(i=0;i<Csize;i++){
clausesRegion[cr++]=lits[i];
}
attachClause(clauses_index_fill_pointer,clausesRegion_size,Csize); // add two literal watches by clause number and size
if(attachLiteral(clauses_index_fill_pointer,lits,Csize)==FALSE)
return FALSE;
push(clauses_index,clausesRegion_size); //store address of clause
clausesRegion_size+=(Csize+1);
return TRUE;
}
char * readClause(char *cin){
int parsed_lit, var,lit1,lit2,lits_stack[1024],lits_stack_fill_pointer=0;
int i,j,tautologie=FALSE;
for (;;){
cin = parseInt(cin, &parsed_lit);
// printf("%d \n",parsed_lit);
if (parsed_lit == 0) break; //the clause end by 0, get one clause
var = abs(parsed_lit)-1;
lit1=parsed_lit > 0 ? (var+var) :((var+var)^1);
push(lits_stack,lit1);
}
//preprocess the clause, such as sorting
for(i=0;i<lits_stack_fill_pointer-1;i++){
lit1=lits_stack[i];
for(j=i+1;j<lits_stack_fill_pointer;j++){
if(lit1>lits_stack[j]){
lit2=lit1;lit1=lits_stack[j];lits_stack[j]=lit2;}
else if (lit1==lits_stack[j]){
lits_stack[j]=lits_stack[lits_stack_fill_pointer-1];
j--;lits_stack_fill_pointer--;lits_stack[lits_stack_fill_pointer]=NONE;
}
else if(lit1==(lits_stack[j]^1)){
tautologie=TRUE; break; // clause is satisfied
}
}
if (tautologie == TRUE)
{NB_CLAUSE--; break;}
else lits_stack[i] = lit1;
}
//store clause to clauseRegion
if(tautologie==FALSE){
if(lits_stack_fill_pointer==2){ //binary clause weight
varActivity[lits_stack[0]>>1]++;
varActivity[lits_stack[1]>>1]++;
}
storeClauses(lits_stack,lits_stack_fill_pointer);}
return cin;
}
int buildInstance(char *input_file) {
FILE* fp_in;
char ch, word2[WORD_LENGTH],pLine[WORD_LENGTH];
int i=0,k=0;uint32_t lsize;
char * streambuffer;char * in;
fp_in=fopen(input_file, "rb");
if (fp_in == NULL) {
return FALSE;
}
fseek(fp_in,0,SEEK_END); //read file in buffer
lsize=ftell(fp_in);
rewind(fp_in);
streambuffer=(char *)malloc(sizeof(char)*(lsize+1));
if(streambuffer==NULL)
{printf("buffer store error \n"); return FALSE;}
if(fread(streambuffer,1,lsize,fp_in)!=lsize)
{printf("reading file to buffer error \n"); return FALSE;}
streambuffer[lsize]=EOF;
ch=streambuffer[k]; //remove useless information
while (ch!='p') {
while (ch!='\n') ch=streambuffer[++k];
ch=streambuffer[++k];
}
i = 0; //get useful information such as var number
while (ch!= '\n') {
pLine[i++] = ch;
ch=streambuffer[++k];
}
sscanf(pLine, "p %s %d %d %uint32_t",
word2, &NB_VAR, &NB_CLAUSE, &HARD_WEIGHT);
if (strcmp(word2, "cnf") == 0)
instanceType = 0; // cnf
else
instanceType = 1; // wcnf
if (HARD_WEIGHT > 0) // For partial
partial = 1;
if(initStoreSpace(NB_VAR,NB_CLAUSE)==FALSE){
printf("inite space malloc error \n");
return FALSE;
}
//store each clause;
in=&streambuffer[++k];
for (;;){
in=skipWhitespace(in);
if (*in==EOF)
break;
else{
in=readClause(in);}
}
fclose(fp_in);
free(streambuffer);
if(partial==0)
HARD_WEIGHT=NB_CLAUSE;
#if 0
k=0;
for(i=0;i<clausesRegion_size;i++){
k++;
printf("%d ",clausesRegion[i]);
if(k==12) {k=0;printf("\n");}
}
for(k=0;k<2*NB_VAR;k++){
for(i=0;i<watcherlist[k].wsize;i++)
printf("%d,%d ",watcherlist[k].watchlit[i].cn, watcherlist[k].watchlit[i].blocker);
printf("\n");
}
for(k=0;k<2*NB_VAR;k++){
printf("litreal_in %d \n",k);
for(i=0;i<literal_in[k].vsize;i++)
printf("%d, ",literal_in[k].varindex[i]);
printf("\n");
}
#endif
return TRUE;
}
uint32_t verifySolution( ) {
int i,j, cr,var,clause_truth;
int* c; uint32_t nb = 0;
for (i = 0; i<originalClauseN; i++) {
clause_truth = FALSE;
cr=clauses_index[i];
c=&clausesRegion[cr+1];
for(j=0;j<clausesRegion[cr];j++)
if (isTrue(c[j])==l_True) {
clause_truth = TRUE;
break;
}
if (clause_truth == FALSE) {
nb ++;
}
}
return nb;
}
void remove_satisfied_clauses(int lit) {
int i;int * clausesID;uint32_t size;
size=literal_in[lit].vsize;
clausesID=literal_in[lit].varindex;
for(i=0;i<size;i++) {
if(clause_stack_fill_pointer>clauses_number_cap)
printf("clause stack overflow\n");
if (clause_reduce_state[clausesID[i]].Cstate== ACTIVE) {
clause_reduce_state[clausesID[i]].Cstate= PASSIVE;
push(clause_stack,clausesID[i]);
}
}
}
int backtracking( ) {
int literal,var,index;
int *clause,addr;int i;
NB_BACK++;
while (trail_fill_pointer> 0) {
literal=pop(trail); var=literal>>1;
var_reduce_state[var].Vstate=ACTIVE;
NB_BRANCHE--;
varAssigns[var].current=l_Undef;
if (varAssigns[var].rest!= NONE){
for (index = saved_clause_stack[var];index < clause_stack_fill_pointer; index++){
addr=clauses_index[clause_stack[index]];
clause=&clausesRegion[addr+1];
if(var_reduce_state[clause[0]>>1].Vstate==ACTIVE||var_reduce_state[clause[1]>>1].Vstate==ACTIVE)
clause_reduce_state[clause_stack[index]].Cstate= ACTIVE;
else{
clause_reduce_state[clause_stack[index]].Cstate=ZEROLEN;
if(clause_stack[index]<originalClauseN) printf("zero back erro\n");
}
}
clause_stack_fill_pointer = saved_clause_stack[var];
unitclause_stack_fill_pointer=saved_unitclause_stack[var];
NB_EMPTY=saved_nb_empty[var];
for(index=saved_zeroLclause_stack[var];index<zero_learnt_clause_fill_pointer;index++){
clause_reduce_state[zero_learnt_clause[index]].Cstate=ACTIVE;
}
zero_learnt_clause_fill_pointer=saved_zeroLclause_stack[var];
#if 0
printf("backtracking %d \n",literal); printf("backtracking empty %d \n",NB_EMPTY);
printf("backtraking learnt clause state\n");
for(i=originalClauseN;i<NB_CLAUSE;i++){
printf(" %d ,",clause_reduce_state[i].Cstate);
}
#endif
if (NB_EMPTY<UB) {
varAssigns[var].current=varAssigns[var].rest;
varAssigns[var].rest=NONE;
var_reduce_state[var].level=++NB_BRANCHE;
var_reduce_state[var].Vstate= PASSIVE;
literal=var+var+varAssigns[var].current;
push(trail,literal);
if (reduce_clauses(literal)==NONE)
return NONE;
remove_satisfied_clauses(literal);
return TRUE;
//#if 0
for(i=0;i<clause_stack_fill_pointer;i++){
printf("%d ,", clause_stack[i]);
}printf("\n");
//#endif
}
}
insertVar(var);
}
return FALSE;
}
int remove_Leantc_of_Iclause(int Iclause){ //original clause is empty for patail assignments, remove it and learnt clauses containing it
int * listclause,i,position,clause,start;
start=clause_stack_fill_pointer;
push(clause_stack,Iclause);
clause_reduce_state[Iclause].Cstate=PASSIVE;
i=start;
while(i<clause_stack_fill_pointer){
Iclause=clause_stack[i++];
if(Iclause_index_Lclause[Iclause].vsize!=0){
listclause=Iclause_index_Lclause[Iclause].varindex;
for(position=0;position<Iclause_index_Lclause[Iclause].vsize;position++){
clause=listclause[position];
if(clause_reduce_state[clause].Cstate!=PASSIVE){
push(clause_stack,clause);
clause_reduce_state[clause].Cstate=PASSIVE;
}
}
}
}
return TRUE;
}
int reduce_clauses(int literal){
struct VECW ws; struct Watcher w; struct Watcher * i,* j,* end;
int blocker,false_lit,first, k,cn,Addr,changeflag=FALSE; //cn means clause number
int* clause; int a,b,Addr1,* clause1; int cn_stack[1000];int cn_stack_fill_pointer=0;
i=watcherlist[literal].watchlit;
j=watcherlist[literal].watchlit;
end=i+watcherlist[literal].wsize;
for(;i!=end;){
changeflag=FALSE;
blocker=i->blocker;
cn=i->cn;
#if 0
push(cn_stack,cn);
printf("cn is %d, blocker is %d \n",cn,blocker);
Addr1=clauses_index[cn];
clause1=&clausesRegion[Addr1+1];
for(b=0;b<clausesRegion[Addr1];b++)
printf("before rudecue %d ",clause1[b]);
printf("\n");
#endif
if(clause_reduce_state[cn].Cstate==ACTIVE){
if(blocker==NONE){ //unit clause becomes empty clause
if(cn<originalClauseN){
NB_EMPTY++;
if(NB_EMPTY>=UB){ while(i<end) *j++=*i++;watcherlist[literal].wsize-=(i-j);
return NONE;}
remove_Leantc_of_Iclause(cn);}
else{
clause_reduce_state[cn].Cstate=ZEROLEN;
push(zero_learnt_clause,cn); //learn clause is FALSE
}
*j++=*i++;continue;
}
false_lit=literal^1;
Addr=clauses_index[cn]; //inspect the clause in clauseRegion
clause=&clausesRegion[Addr+1];
if(clause[0]==false_lit){
clause[0]=clause[1];
clause[1]=false_lit;}
i++; //control the for cycle
w.blocker=clause[0];
w.cn=cn;
for(k=2;k<clausesRegion[Addr];k++){
if(isTrue(clause[k])!=l_False){
clause[1]=clause[k];
clause[k]=false_lit;
pushVetw(&watcherlist[clause[1]^1], cn, clause[0]);
changeflag=TRUE; //new literal come into watching region
}
}
if(changeflag==FALSE){
*j++=w; //reserve the current clause in this watch,update the blocker
if(isTrue(clause[0])==l_False){
if(cn<originalClauseN){
NB_EMPTY++;//printf("reduce empty clause %d \n",cn);
if(NB_EMPTY>=UB) { while(i<end) *j++=*i++;watcherlist[literal].wsize-=(i-j);
#if 0
for(a=0;a<watcherlist[literal].wsize;a++)
printf("%d,%d ",watcherlist[literal].watchlit[a].cn, watcherlist[literal].watchlit[a].blocker);
printf("\n");
#endif
return NONE;}
remove_Leantc_of_Iclause(cn);
}
else{
clause_reduce_state[cn].Cstate=ZEROLEN;
push(zero_learnt_clause,cn);
}
}
else
push(unitclause_stack,cn);
}
continue; //next clause,not reserve False clauses
}
*j++=*i++; //reserve satisfied clauses
}
watcherlist[literal].wsize=watcherlist[literal].wsize-(i-j);
#if 0
printf("current watcher wsize %d \n",watcherlist[literal].wsize);
for(a=0;a<watcherlist[literal].wsize;a++){
printf("%d, %d ",watcherlist[literal].watchlit[a].cn,watcherlist[literal].watchlit[a].blocker);
}
printf("\n");
#endif
//#if 0
printf("reduce clause part\n");
printf("involved watcher clause change \n");
for(a=0;a<cn_stack_fill_pointer;a++){
Addr=clauses_index[cn_stack[a]];
clause=&clausesRegion[Addr+1];
for(b=0;b<clausesRegion[Addr];b++)
printf("%d ",clause[b]);
printf("\n");
}
printf("unit clause now \n");
for(a=0;a<unitclause_stack_fill_pointer;a++){
printf("%d ,",unitclause_stack[a]);
}
printf("zero clause stack now \n");
for(a=0;a<zero_learnt_clause_fill_pointer;a++){
printf("%d ,%d ,",zero_learnt_clause[a],clause_reduce_state[zero_learnt_clause[a]].Cstate);
}
printf("\n");
//#endif
return TRUE;
}
int setpassive_Leantc_of_Iclause(int startpointer){
int * listclause,i=0,position,clause,lclause;
position=startpointer;
while(position<clause_stack_fill_pointer){
clause=clause_stack[position++];
clause_reduce_state[clause].Cstate=PASSIVE;
if(Iclause_index_Lclause[clause].vsize!=0){
listclause=Iclause_index_Lclause[clause].varindex;
for(i=0;i<Iclause_index_Lclause[clause].vsize;i++){
lclause=listclause[i];
if(clause_reduce_state[lclause].Cstate!=PASSIVE&&clause_reduce_state[lclause].Cstate!=NEW_LCLAUSE//eliminate NEW_LCLAUSE AND PASSIVE
&&clause_reduce_state[lclause].involved==FALSE){//clause may be removed before
push(clause_stack,lclause);
clause_reduce_state[lclause].involved=TRUE;
}
}
}
}
for(i=startpointer;i<clause_stack_fill_pointer;i++)
clause_reduce_state[clause_stack[i]].involved=FALSE;
return TRUE;
}
//repalce learnt clause with conflict set when removing happens
int remove_clauses_csets( ){
int Rclause,position,i,clause,ii;
int start=clause_stack_fill_pointer;
for(position=0;position<reason_stack_fill_pointer;position++){
Rclause=reason_stack[position];
if(clause_reduce_state[Rclause].Cstate!=PASSIVE&&clause_reduce_state[Rclause].involved==FALSE){//clause is satisfied
clause_reduce_state[Rclause].involved=TRUE;
push(clause_stack,Rclause);
if(Rclause>=originalClauseN){
i=addr_of_learncSet[Rclause-originalClauseN];
clause=conSet_of_learnc[i];
while(clause!=NONE){
if(clause_reduce_state[clause].Cstate!=PASSIVE
&&clause_reduce_state[clause].involved==FALSE){//some clause maybe PASSIVE because they are satisfied.
if(clause>=originalClauseN)
push(reason_stack,clause);
else{
push(clause_stack,clause);
// printf("%d, ",clause);
clause_reduce_state[clause].involved=TRUE;
}
}
clause=conSet_of_learnc[++i];
}
}
}
}
// printf("\n");
setpassive_Leantc_of_Iclause(start);
return TRUE;
}//not
int my_reduce_clauses(int p ) {
struct Watcher w;struct Watcher * i,* j,* end;
int blocker,false_lit,first,cn,Addr,k,changeflag=FALSE;
int* clause; int a;
end=watcherlist[p].watchlit+watcherlist[p].wsize;
for(i=j=watcherlist[p].watchlit;i!=end;){
changeflag=FALSE;
blocker=i->blocker;
cn=i->cn;
if(clause_reduce_state[cn].Cstate==ACTIVE){
if(blocker==NONE){ //unit clause becomes empty clause
while(i<end)
*j++=*i++;
watcherlist[p].wsize-=(i-j);
return cn;
}
if(isTrue(blocker)==l_True){
*j++=*i++;continue;
}
false_lit=p^1;
Addr=clauses_index[cn];
clause=&clausesRegion[Addr+1];
if(clause[0]==false_lit){
clause[0]=clause[1];
clause[1]=false_lit;}
i++;
first=clause[0];
w.blocker=clause[0];
w.cn=cn;
if(first!=blocker&&isTrue(first)==l_True){
*j++=w;continue;
}
for(k=2;k<clausesRegion[Addr];k++){
if(isTrue(clause[k])!=l_False){
clause[1]=clause[k];
clause[k]=false_lit;
pushVetw(&watcherlist[clause[1]^1], cn, clause[0]);
changeflag=TRUE;
}
}
if(changeflag==FALSE){
*j++=w;
if(isTrue(clause[0])==l_False){
while(i<end)
*j++=*i++;
watcherlist[p].wsize-=(i-j);
return cn;
}
else
push(my_unit_clause_stack,cn);
}
continue;
}
*j++=*i++; //reserve the satisfied clauses in watcherlist
}
watcherlist[p].wsize-=(i-j);
#if 0
printf("my_unit clause \n");
for(a=0;a<my_unit_clause_stack_fill_pointer;a++)
printf("%d ", my_unit_clause_stack[a]);
printf("\n");
printf("trai \n");
for(a=0;a<trail_fill_pointer;a++)
printf("%d %d %d,",trail[a],var_reduce_state[trail[a]>>1].Vstate,var_reduce_state[trail[a]>>1].reason);
printf("\n");
#endif
return NO_CONFLICT;
}
int satisfy_unitclause(int unitclause) {
int literal, cr,clause, temp;
cr =clauses_index[unitclause];
literal=clausesRegion[cr+1];
if(var_reduce_state[literal>>1].Vstate==PASSIVE){
if(var_reduce_state[clausesRegion[cr+2]>>1].Vstate==PASSIVE)
return NO_CONFLICT;
else{
temp=clausesRegion[cr+1]; clausesRegion[cr+1]=clausesRegion[cr+2];clausesRegion[cr+2]=temp;
literal=clausesRegion[cr+1];
}
}
push(trail,literal);
var_reduce_state[literal>>1].reason=unitclause;
var_reduce_state[literal>>1].Vstate=PASSIVE;
var_reduce_state[literal>>1].level=current_learnt_level;
varAssigns[literal>>1].current=literal&1;
if ((clause=my_reduce_clauses(literal))==NO_CONFLICT)
return NO_CONFLICT;
else
return clause;
}
int my_unitclause_process(int starting) {
int unitclause, unitclause_position,clause, my_unitclause_position, my_unitclause;
BCP_variable_index=trail_fill_pointer;
for (unitclause_position = starting;unitclause_position < unitclause_stack_fill_pointer;unitclause_position++) {
unitclause =unitclause_stack[unitclause_position];
if (clause_reduce_state[unitclause].Cstate== ACTIVE) {
my_unit_clause_stack_fill_pointer= 0;
if ((clause=satisfy_unitclause(unitclause)) != NO_CONFLICT)
return clause;
else {
for (my_unitclause_position = 0;my_unitclause_position <my_unit_clause_stack_fill_pointer;my_unitclause_position++) {
my_unitclause = my_unit_clause_stack[my_unitclause_position];
if (clause_reduce_state[my_unitclause].Cstate== ACTIVE) {
if ((clause=satisfy_unitclause(my_unitclause)) != NO_CONFLICT)
return clause;
}
}//end for
}
}
}
return NO_CONFLICT;
}
#define CLAUSE_INDEX_ISIZE 524288
int add_learntC_clause_index( ){
int i, * position,iclause;
for(i=prior_confsets;i<conSet_of_learnc_fill_pointer-1;i++){
iclause=conSet_of_learnc[i];
if(Iclause_index_Lclause[iclause].vcap==0){
Iclause_index_Lclause[iclause].varindex=(int *)malloc((CLAUSE_INDEX_ISIZE) * sizeof(int));
if(Iclause_index_Lclause[iclause].varindex==NULL) return FALSE;
Iclause_index_Lclause[iclause].vcap=CLAUSE_INDEX_ISIZE;
}
if(Iclause_index_Lclause[iclause].vsize>=Iclause_index_Lclause[iclause].vcap){
Iclause_index_Lclause[iclause].varindex=(int *)realloc(Iclause_index_Lclause[iclause].varindex,sizeof(int)*2*Iclause_index_Lclause[iclause].vcap);
if(Iclause_index_Lclause[iclause].varindex==NULL) return FALSE;
Iclause_index_Lclause[iclause].vcap*=2;
}
position=Iclause_index_Lclause[iclause].varindex;
position[Iclause_index_Lclause[iclause].vsize++]=NB_CLAUSE;//add lerant clause as first node
}
return TRUE;
}
int analyze_conflict(int cfclause){
int PathC=0,i=0,UIP_index,lclause,addr,conflict_clause,literal,var,j;
int flaglit=NONE,*clause;
int seen_variables_revert[1024];
int seen_variables_revert_fill_pointer=0;
conflict_clause=cfclause;
conflict_number++; //computer the valid analyze number
UIP_index=trail_fill_pointer-1;
prior_confsets=conSet_of_learnc_fill_pointer;
temp_learnt_clause_fill_pointer=1;
do{
addr=clauses_index[conflict_clause];
clause=&clausesRegion[addr+1];
for(j=(flaglit==NONE)?0:1;j<clausesRegion[addr];j++){
literal=clause[j]; var=literal>>1;
if(seen[var]!=TRUE&&var_reduce_state[var].level>=0) {
updateVar(var);
seen[var]=TRUE;
if(var_reduce_state[var].level>=current_learnt_level){
PathC++;
}
else{
push(temp_learnt_clause,literal);
push(seen_variables_revert,var);
}
}
}
// if(clause_weight[conflict_clause]<HARD_WEIGHT)
if(conSet_of_learnc_fill_pointer<conSet_of_learnc_cap-2)
push(conSet_of_learnc,conflict_clause);//record the FUIP conflictset
else{
conSet_of_learnc=(int *)realloc(conSet_of_learnc,conSet_of_learnc_cap*2*sizeof(int));
if(conSet_of_learnc==NULL) {printf("conset room is not enough\n"); return FALSE;}
conSet_of_learnc_cap*=2;
push(conSet_of_learnc,conflict_clause);
}
while(!seen[trail[UIP_index--]>>1]&&UIP_index>BCP_variable_index-2&&(UIP_index>=0));//next conflict clause
if(UIP_index<BCP_variable_index-2)
break;
flaglit=trail[UIP_index+1]; //get the next conflict variable
if(var_reduce_state[flaglit>>1].reason!=NO_REASON){
conflict_clause=var_reduce_state[flaglit>>1].reason;//conflict structure is chain,value maybe NONE
}
seen[flaglit>>1]=FALSE;
PathC--;
}while(PathC>0);
temp_learnt_clause[0]=flaglit^1; // FUIP must be 0 unit
push(conSet_of_learnc,NONE);
for(i=0;i<seen_variables_revert_fill_pointer;i++)
seen[seen_variables_revert[i]]=FALSE;
//special case:R3,R4
if(conSet_of_learnc_fill_pointer-prior_confsets==2){
conSet_of_learnc_fill_pointer=prior_confsets;
clause=&clausesRegion[clauses_index[cfclause]];;
for(i=0;i<clause[0];i++){
literal=clause[i+1];
seen[literal>>1]=FALSE;
}
return FALSE;
}
return TRUE;
}
void confirmDeleteC( ){
int i,j,cn,cr,count=0;int * clause;int saved;
saved=learntclause_fill_pointer;
clauseDelete_fill_pointer=0;
for(i=0;i<learntclause_fill_pointer;i++){
cn=learntclause[i];
if(count>=(saved/2))
break;
if(clause_reduce_state[cn].Cstate==ACTIVE){
push(clauseDelete,cn);
if(Iclause_index_Lclause[cn].vcap!=0){
free(Iclause_index_Lclause[cn].varindex);
Iclause_index_Lclause[cn].vcap=0;}
clauseDelMark[cn-originalClauseN]=TRUE;
learntclause[i]=learntclause[learntclause_fill_pointer-1];
learntclause_fill_pointer--;
i--;
count++;
}
}
}
int create_learnt_clause( ){
int i,literal,cr;int * clause; int k;
if(clausesRegion_size+temp_learnt_clause_fill_pointer+1>=clausesRegion_cap){
clausesRegion=(int *)realloc(clausesRegion,sizeof(int)*(clausesRegion_cap<<1));
if(clausesRegion==NULL) return FALSE;
clausesRegion_cap=clausesRegion_cap<<1;
}
cr=clausesRegion_size;
clausesRegion[cr++]=temp_learnt_clause_fill_pointer;
for(i=0;i<temp_learnt_clause_fill_pointer;i++){
literal=temp_learnt_clause[i];
clausesRegion[cr++]=literal;
//updateVar(literal>>1);
}