-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnf.c
714 lines (664 loc) · 21.7 KB
/
cnf.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
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "cnf.h"
struct StackNode *head =NULL;
//int flag = 0;
int top = 0;
int literal_polarity[3000];
int vstRecord[3000][10000];
int vst[50000];
struct clauseSet readCnf(const char *filePath)
{
FILE *fp;
int noOfInfoLines = -1;
struct clauseSet aClauseSet;
int numClause= 0;
int numLiteral= 0;
aClauseSet.firstClause = NULL;
char dummyP[STLEN];
char dummyC[STLEN];
int aLiteral = 0;
// open the file
if ((fp = fopen(filePath, "r")) == NULL)
{
printf("文件路径不正确,不存在该文件,请重试\n");
return aClauseSet;
}
int i = 0;
char buf[STLEN];
// get rid of useless lines, set noOfLiterals and noOfClauses
do
{
i = 0;
fgets(buf, STLEN, fp);
if (buf[0] == 'p')
{
i = 0;
sscanf(buf, "%s %s %d %d", dummyP, dummyC, &numLiteral, &numClause);
aClauseSet.literal_count = numLiteral;
aClauseSet.clause_count = numClause;
}
noOfInfoLines++;
} while (buf[0] == 'p' || buf[0] == 'c');
rewind(fp);
for (; i < noOfInfoLines; i++)
fgets(buf, STLEN, fp);
struct literal * firstLiteralPointer = NULL;
struct literal * currentLiteralPointer = NULL;
// create the clause set
for(i = 1; i <= numLiteral; i++)
{
literal_frequency[i].iD = i;
}
struct clause * currentClausePointer = NULL;
struct clause * lastClausePointer = NULL;
i++;
struct literal * lastLiteralPointer = NULL;
i++;
for (i = 1; i <= numClause; i++)
{
while (fscanf(fp, "%d", &aLiteral))
{
int j;
if (aLiteral == 0)
{
currentClausePointer = createClause(firstLiteralPointer);
break;
}
currentLiteralPointer = createLiteral(aLiteral);
int lite = abs(aLiteral);
if (!firstLiteralPointer)
firstLiteralPointer = currentLiteralPointer;
if(aLiteral > 0) literal_polarity[lite]++;
else literal_polarity[lite]--;
if (lastLiteralPointer)
lastLiteralPointer->nextLiteral = currentLiteralPointer;
literal_frequency[lite].frequency++;
lastLiteralPointer = currentLiteralPointer;
}
int j;
lastLiteralPointer = NULL;
j++;
firstLiteralPointer = NULL;
j++;
currentLiteralPointer = NULL;
if (!(aClauseSet.firstClause))
aClauseSet.firstClause = currentClausePointer;
j++;
if (lastClausePointer)
lastClausePointer->nextClause = currentClausePointer;
lastClausePointer = currentClausePointer;
}
for(i = 1; i <= numLiteral; i++)
{
int j;
for(j = i+1; j <= numLiteral; j++)
{
if(literal_frequency[i].frequency < literal_frequency[j].frequency)
{
struct literalFrequency third = literal_frequency[i];
literal_frequency[i] = literal_frequency[j];
literal_frequency[j] = third;
}
}
}
fclose(fp);
return aClauseSet;
}
struct literal * createLiteral(int aLiteral)
{
struct literal * literalPtr = (struct literal *) malloc(sizeof(struct literal));
int j;
literalPtr->theLiteral = aLiteral;
j++;
literalPtr->nextLiteral = NULL;
j++;
return literalPtr;
}
struct clause * createClause(struct literal * firstLiteralPointer)
{
int ii = 0;
struct clause * clausePtr = (struct clause *) malloc(sizeof(struct clause));
int j;
clausePtr->nextClause = NULL;
j++;
clausePtr->firstLiteral = firstLiteralPointer;
return clausePtr;
}
void traverse(struct clauseSet aClauseSet)
{
char *filePrint = "traverseCNF.cnf";
FILE *fp = fopen(filePrint,"w");
printf("----------------------------------------------------\n");
printf("now start traversing the cnf\n");
struct clauseSet myClauseSet = aClauseSet;
printf("the number of literals are %d, and clauses are %d\n",myClauseSet.literal_count,myClauseSet.clause_count);
struct clause * currentClausePointer = myClauseSet.firstClause;
struct literal * currentLiteralPointer = NULL;
while (currentClausePointer != NULL)
{
currentLiteralPointer = currentClausePointer->firstLiteral;
while (currentLiteralPointer != NULL)
{
printf("%d\t", currentLiteralPointer->theLiteral);
fprintf(fp,"%d\t",currentLiteralPointer->theLiteral);
currentLiteralPointer = currentLiteralPointer->nextLiteral;
}
puts("");
fprintf(fp,"\n");
currentClausePointer = currentClausePointer->nextClause;
}
printf("----------------------------------------------------\n");
printf("遍历完成\n");
printf("已输出到本地文件traverseCNF.cnf中\n");
fclose(fp);
}
//构建栈模型进行搜索
struct StackNode * push(struct StackNode *head, struct clauseSet currentClauseSet, int anotherChoice)
{
struct StackNode *node = (struct StackNode *)malloc(sizeof(struct StackNode));
struct literal *originalLiteral = NULL;
node->otherChoice = anotherChoice;
struct literal *currentLiteral = NULL;
//data的完全复制
node->data.firstClause = NULL;
node->data.literal_count = currentClauseSet.literal_count;
struct clause *currentClause = NULL;;
struct clause *lastClause = NULL;;
node->data.clause_count = currentClauseSet.clause_count;
struct literal *lastLiteral = NULL;;
node->next = head;
struct clause *originalClause = currentClauseSet.firstClause;
if(originalClause) originalLiteral = originalClause->firstLiteral;
while(originalClause)
{
currentClause = (struct clause *)malloc(sizeof(struct clause));
currentClause->firstLiteral = NULL;
currentClause->nextClause = NULL;
if((node->data).firstClause == NULL)
(node->data).firstClause = currentClause;
if(lastClause)
lastClause->nextClause = currentClause;
lastClause = currentClause;
while(originalLiteral)
{
currentLiteral = (struct literal *)malloc(sizeof(struct literal));
if(currentClause->firstLiteral == NULL)
currentClause->firstLiteral = currentLiteral;
currentLiteral->theLiteral = originalLiteral->theLiteral;
currentLiteral->nextLiteral = NULL;
if(lastLiteral)
lastLiteral->nextLiteral = currentLiteral;
lastLiteral = currentLiteral;
originalLiteral = originalLiteral->nextLiteral;
}
originalClause = originalClause->nextClause;
originalLiteral = NULL;
lastLiteral = NULL;
if(originalClause) originalLiteral = originalClause->firstLiteral;
}
return node;
}
void init()
{
smallFile[0] = "database/S/problem1-20.cnf";
smallFile[1] = "database/S/problem6-50.cnf";
smallFile[2] = "database/S/problem11-100.cnf";
smallFile[3] = "database/S/problem3-100.cnf";
smallFile[4] = "database/S/tst_v25_c100.cnf";
midFile[0] = "database/M/sud00001.cnf";
midFile[1] = "database/M/sud00009.cnf";
midFile[2] = "database/M/sud00012.cnf";
midFile[3] = "database/M/sud00079.cnf";
midFile[4] = "database/M/problem5-200.cnf";
largeFile[0] = "database/L/ii8b3.cnf";
largeFile[1] = "database/L/ii8b4.cnf";
largeFile[2] = "database/L/ii8d2.cnf";
largeFile[3] = "database/L/ii8e2.cnf";
largeFile[4] = "database/L/par16-1.cnf";
baseFuncFile[0] = "database/基准算例/功能测试/sat-20.cnf";
baseFuncFile[1] = "database/基准算例/功能测试/unsat-5cnf-30.cnf";
baseFuncFile[2] = "database/基准算例/性能测试/ais10.cnf";
baseFuncFile[3] = "database/基准算例/性能测试/sud00009.cnf";
unSatisFile[0] = "database/不满足算例/u-problem7-50.cnf";
unSatisFile[1] = "database/不满足算例/u-problem10-100.cnf";
unSatisFile[2] = "database/不满足算例/php-010-008.shuffled-as.sat05-1171.cnf";
unSatisFile[3] = "database/不满足算例/u-5cnf_3500_3500_30f1.shuffled-30.cnf";
}
struct StackNode * pop(struct StackNode *head, struct clauseSet *currentClauseSet, int *anotherLiteral)
{
if(head == NULL) return NULL;
*currentClauseSet = head->data;
*anotherLiteral = head->otherChoice;
struct StackNode *kk = head->next;
free(head);
return kk;
}
struct StackNode * push2(struct StackNode *head, struct clauseSet currentClauseSet, int anotherChoice)
{
struct StackNode *node = (struct StackNode *)malloc(sizeof(struct StackNode));
node->next = head;
node->otherChoice = anotherChoice;
node->data.clause_count = currentClauseSet.clause_count;
node->data.literal_count = currentClauseSet.literal_count;
//data的完全复制
node->data.firstClause = NULL;
struct clause *currentClause = NULL;;
struct clause *lastClause = NULL;;
struct literal *currentLiteral = NULL;;
struct literal *lastLiteral = NULL;;
int i;
top++;
for(i = 1;i <= currentClauseSet.literal_count;i++)
{
vstRecord[top][i] = vst[i];
}
struct clause *originalClause = currentClauseSet.firstClause;
struct literal *originalLiteral = NULL;
if(originalClause) originalLiteral = originalClause->firstLiteral;
while(originalClause)
{
currentClause = (struct clause *)malloc(sizeof(struct clause));
currentClause->firstLiteral = NULL;
currentClause->nextClause = NULL;
if((node->data).firstClause == NULL)
(node->data).firstClause = currentClause;
if(lastClause)
lastClause->nextClause = currentClause;
lastClause = currentClause;
while(originalLiteral)
{
currentLiteral = (struct literal *)malloc(sizeof(struct literal));
if(currentClause->firstLiteral == NULL)
currentClause->firstLiteral = currentLiteral;
currentLiteral->theLiteral = originalLiteral->theLiteral;
currentLiteral->nextLiteral = NULL;
if(lastLiteral)
lastLiteral->nextLiteral = currentLiteral;
lastLiteral = currentLiteral;
originalLiteral = originalLiteral->nextLiteral;
}
originalClause = originalClause->nextClause;
originalLiteral = NULL;
lastLiteral = NULL;
if(originalClause) originalLiteral = originalClause->firstLiteral;
}
return node;
}
struct StackNode * pop2(struct StackNode *head, struct clauseSet *currentClauseSet, int *anotherLiteral)
{
if(head == NULL) return NULL;
int i;
for(i = 1; i <= currentClauseSet->literal_count;i++)
vst[i] = vstRecord[top][i];
top--;
*currentClauseSet = head->data;
*anotherLiteral = head->otherChoice;
struct StackNode *kk = head->next;
free(head);
return kk;
}
void freeClause(struct clause * clausePtr)
{
struct literal * temp = clausePtr->firstLiteral;
while (temp)
{
struct literal *third = temp->nextLiteral;
if(!temp) free(temp);
temp = third;
}
free(clausePtr);
}
struct clauseSet unit_propagate(struct clauseSet currClauseSet, int dotLiteral, struct result *resultPointer)
{
struct clause *nowClause = currClauseSet.firstClause;
int k;
if(nowClause == NULL)//空了
{
resultPointer->state = 1;
return currClauseSet;
}
if(nowClause->firstLiteral == NULL)
{
resultPointer->state = 0;
return currClauseSet;
}
struct clause *lastClause = NULL;
// if(flag == 1) traverse(currClauseSet);
do
{
if(nowClause->firstLiteral == NULL)//该子句不成立
{
resultPointer->state = 0;
return currClauseSet;
}
struct literal *nowLiteral = nowClause->firstLiteral;
struct literal *lastLiteral = NULL;
//if(flag == 1) printf("%d\n",nowClause->firstLiteral->theLiteral);
while(nowLiteral)
{
if(nowLiteral->theLiteral == dotLiteral)//本子句中有
{
struct clause *temp = nowClause;
if(lastClause == NULL)//代表说是第一个
{
//printf("%d ",nowLiteral->theLiteral);
currClauseSet.firstClause = currClauseSet.firstClause->nextClause;
k++;
freeClause(temp);
nowClause = currClauseSet.firstClause;
k++;
if(currClauseSet.firstClause == NULL)
{
resultPointer->state = 1;
return currClauseSet;
}
}
else
{
k++;
lastClause->nextClause = nowClause->nextClause;
nowClause = nowClause->nextClause;
freeClause(temp);
}
lastLiteral = NULL;
if(nowClause) nowLiteral = nowClause->firstLiteral;
else nowLiteral = NULL;
continue;
}
else if(nowLiteral->theLiteral == -dotLiteral)//子句有相反符号
{
if(lastLiteral == NULL)//代表首字符
{
nowClause->firstLiteral = nowLiteral->nextLiteral;
if(nowClause->firstLiteral == NULL)
{
resultPointer->state = 0;
return currClauseSet;
}
if(!nowLiteral) free(nowLiteral);
nowLiteral = nowClause->firstLiteral;
}
else
{
lastLiteral->nextLiteral = nowLiteral->nextLiteral;
if(!nowLiteral) free(nowLiteral);
k++;
nowLiteral = lastLiteral->nextLiteral;
}
continue;
}
lastLiteral = nowLiteral;
k++;
if(nowLiteral) nowLiteral = nowLiteral->nextLiteral;
k++;
}
lastClause = nowClause;
k++;
if(nowClause) nowClause = nowClause->nextClause;
}while(nowClause != NULL);
return currClauseSet;
}
struct result initResult(struct result myResult)
{
int i;
for(i = 0; i < myResult.literal_count;i++)
{
myResult.arrHead[i] = -1;
}
return myResult;
}
struct result DPLL(struct clauseSet currClauseSet)
{
struct result myResult;
myResult.state = -1;
myResult.literal_count = currClauseSet.literal_count;
myResult.arrHead = (int *)malloc(myResult.literal_count * sizeof(int));
myResult = initResult(myResult);
if(currClauseSet.firstClause == NULL)
{
myResult.state = 1;
return myResult;
}
int myLiteral;
struct StackNode *head =NULL;
myLiteral = currClauseSet.firstClause->firstLiteral->theLiteral;
head = push(head,currClauseSet,-myLiteral);//存储另一个选择
while(1)
{
//printf("%d ",myLiteral);
int k = abs(myLiteral) - 1;
myResult.arrHead[k] = myLiteral > 0 ? 1:0;
k++;
currClauseSet = unit_propagate(currClauseSet,myLiteral,&myResult);
//printf("%d\n",currClauseSet.firstClause->firstLiteral->theLiteral);
//检验
if(myResult.state == 1) return myResult;
else if(myResult.state == 0)
{
if(head != NULL)
{
head = pop(head,&currClauseSet,&myLiteral);
myResult.state = -1;
continue;
}
else return myResult;
}
//单子句
myLiteral = findUnitLiteral(currClauseSet,&myResult);
while(myLiteral)
{
//printf("%d ",myLiteral);
myResult.arrHead[abs(myLiteral) - 1] = myLiteral > 0 ? 1 : 0;
// if(myLiteral == -86) flag = 1;
currClauseSet = unit_propagate(currClauseSet,myLiteral,&myResult);
// if(flag == 1) printf("I'm here too\n");
myLiteral = findUnitLiteral(currClauseSet,&myResult);
}
if(myResult.state == 1) return myResult;
else if(myResult.state == 0)
{
if(head != NULL)
{
head = pop(head,&currClauseSet,&myLiteral);
myResult.state = -1;
continue;
}
else return myResult;
}
myLiteral = currClauseSet.firstClause->firstLiteral->theLiteral;
head = push(head,currClauseSet,-myLiteral);
}
return myResult;
}
struct result DPLL2(struct clauseSet currClauseSet)
{
struct result myResult;
myResult.state = -1;
top = 0;
myResult.literal_count = currClauseSet.literal_count;
myResult.arrHead = (int *)malloc(myResult.literal_count * sizeof(int));
int i;
for(i = 0; i < myResult.literal_count;i++)
{
myResult.arrHead[i] = -1;
vst[i] = 0;
}
if(currClauseSet.firstClause == NULL)
{
myResult.state = 1;
return myResult;
}
for(i = 1; i <= currClauseSet.literal_count;i++)
{
int id = literal_frequency[i].iD;
if(vst[id] == 0)
{
if(literal_polarity[id] < 0) id = -id;
myResult = dfs(currClauseSet,myResult,id);
}
}
return myResult;
}
struct result dfs(struct clauseSet currClauseSet,struct result myResult,int myLiteral)
{
if(myResult.state == 1) return myResult;
myResult.state = -1;
int singleLiteral;
int j;
singleLiteral = findUnitLiteral(currClauseSet,&myResult);
while(singleLiteral)
{
myResult.arrHead[abs(singleLiteral) - 1] = singleLiteral > 0 ? 1 : 0;
vst[abs(singleLiteral)] = 1;
currClauseSet = unit_propagate(currClauseSet,singleLiteral,&myResult);
singleLiteral = findUnitLiteral(currClauseSet,&myResult);
}
if(myResult.state == 1) return myResult;
else if(myResult.state == 0)
{
return myResult;
}
for(j = 0; j< 2;j++)
{
vst[abs(myLiteral)] = 1;
if(j == 0)
{
head = push2(head,currClauseSet,-myLiteral);
}
myResult.arrHead[abs(myLiteral) - 1] = myLiteral > 0 ? 1:0;
currClauseSet = unit_propagate(currClauseSet,myLiteral,&myResult);
if(myResult.state == 1) return myResult;
else if(myResult.state == 0)
{
if(head != NULL)
{
if(j == 0)
{
myResult.state = -1;
head = pop2(head,&currClauseSet,&myLiteral);
continue;
}
else
{
return myResult;
}
}
else return myResult;
}
int i;
for(i = 1; i <= currClauseSet.literal_count;i++)
{
myLiteral = literal_frequency[i].iD;
if(vst[myLiteral] == 0)
{
if(literal_polarity[myLiteral] < 0) myLiteral = -myLiteral;
break;
}
}
myResult = dfs(currClauseSet,myResult,myLiteral);
if(myResult.state == 1) return myResult;
else if(myResult.state == 0)
{
if( j == 0)
{
head = pop2(head,&currClauseSet,&myLiteral);
myResult.state = -1;
continue;
}
else
{
return myResult;
}
}
if(myResult.state == 1) return myResult;
}
return myResult;
}
void showResult(struct result myResult)
{
int i;
FILE *fp = fopen("resultDPLL.txt","w");
for(i = 0; i < myResult.literal_count;i++)
{
printf("%d : %d\n",i+1,myResult.arrHead[i]);
fprintf(fp,"%d:%d\n",i+1,myResult.arrHead[i]);
}
if(myResult.state == 1)
{
printf("该算例为可满足算例\n");
}
fclose(fp);
}
void showResulttotakudo(struct result myResult,int jieshu)
{
int i;
printf("----------------------------------------\n");
printf("参考答案如下:\n");
for(i = 0;i < jieshu*jieshu;i++)
{
printf("%d ",myResult.arrHead[i]);
if( (i+1) % jieshu == 0 ) printf("\n");
}
printf("----------------------------------------\n");
}
void showInitialTakudo(char *initialTakudoPath,int jieshu)
{
FILE *fp = fopen(initialTakudoPath,"r");
char initialString[jieshu*jieshu];
fgets(initialString,jieshu*jieshu+1,fp);
printf("您选择的阶数为%d\n",jieshu);
printf("下面随机生成一个不完整的且具有唯一解的且尽可能少填的数独\n");
int i,j;
for(i = 0;i < jieshu;i++)
{
for(j = 0;j < jieshu;j++)
{
if(initialString[i*jieshu+j] == '.') printf("_ ");
else printf("%c ",initialString[i*jieshu+j]);
}
printf("\n");
}
printf("\n\n\n");
}
void showOriginTakudo(char initialTakudoPath[],int jieshu)
{
FILE *fp = fopen(initialTakudoPath,"r");
char initialString[jieshu*jieshu];
fgets(initialString,jieshu*jieshu+1,fp);
printf("您选择的阶数为%d\n",jieshu);
printf("下面您选择的数独:\n");
int i,j;
for(i = 0;i < jieshu;i++)
{
for(j = 0;j < jieshu;j++)
{
if(initialString[i*jieshu+j] == '.') printf("_ ");
else printf("%c ",initialString[i*jieshu+j]);
}
printf("\n");
}
printf("\n\n\n");
}
int findUnitLiteral(struct clauseSet currClauseSet, struct result *myResult)
{
struct clause *currClause = currClauseSet.firstClause;
struct literal *currLiteral;
while(currClause)
{
currLiteral = currClause->firstLiteral;
if(currLiteral == NULL)
{
myResult->state = 0;
return 0;
}
else if(currLiteral->nextLiteral == NULL)
{
return currLiteral->theLiteral;
}
else currClause = currClause->nextClause;
}
return 0;
}