-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwalksat.c
executable file
·1473 lines (1269 loc) · 37.6 KB
/
walksat.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
/* walksat version 35 */
/* version 1 by Bram Cohen 7/93 */
/* versions 2 - 35 by Henry Kautz */
/************************************/
/* Compilation flags */
/************************************/
/* If the constant HUGE is set, then dynamic arrays are used instead of static arrays.
This allows very large or very small problems to be handled, without
having to adjust the constants MAXATOM and/or MAXCLAUSE. However, on some
architectures (e.g. SGI) the compiled program is about 25% slower, because not
as many optimizations can be performed. */
#define Huge 1
/* If the constant NT is set, then the program is modified to compile under Windows NT.
Currently, this eliminates the timing functionality. */
/* #define NT 1 */
/************************************/
/* Standard includes */
/************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <limits.h>
#include <signal.h>
#ifndef NT
#include <sys/times.h>
#include <sys/time.h>
#endif
#ifndef CLK_TCK
#define CLK_TCK 60
#endif
#ifdef NT
#define random() rand()
#define srandom(seed) srand(seed)
#endif
/************************************/
/* Constant parameters */
/************************************/
#define MAXATOM 1000000 /* maximum possible number of atoms */
#ifdef Huge
#define STOREBLOCK 2000000 /* size of block to malloc each time */
#else
#define STOREBLOCK 2000000 /* size of block to malloc each time */
#define MAXCLAUSE 500000 /* maximum possible number of clauses */
#endif
#define TRUE 1
#define FALSE 0
#define MAXLENGTH 500 /* maximum number of literals which can be in any clause */
/************************************/
/* Internal constants */
/************************************/
enum heuristics { RANDOM, BEST, TABU, NOVELTY, RNOVELTY };
#define NOVALUE -1
#define INIT_PARTIAL 1
#define HISTMAX 101 /* length of histogram of tail */
#define Var(CLAUSE, POSITION) (ABS(clause[CLAUSE][POSITION]))
static int scratch;
#define ABS(x) ((scratch=(x))>0?(scratch):(-scratch))
#define BIG 100000000
/************************************/
/* Main data structures */
/************************************/
/* Atoms start at 1 */
/* Not a is recorded as -1 * a */
/* One dimensional arrays are statically allocated. */
/* Two dimensional arrays are dynamically allocated in */
/* the second dimension only. */
int numatom;
int numclause;
int numliterals;
#ifdef Huge
int ** clause; /* clauses to be satisfied */
/* indexed as clause[clause_num][literal_num] */
int * size; /* length of each clause */
int * false; /* clauses which are false */
int * lowfalse;
int * wherefalse; /* where each clause is listed in false */
int * numtruelit; /* number of true literals in each clause */
#else
int * clause[MAXCLAUSE]; /* clauses to be satisfied */
/* indexed as clause[clause_num][literal_num] */
int size[MAXCLAUSE]; /* length of each clause */
int false[MAXCLAUSE]; /* clauses which are false */
int lowfalse[MAXCLAUSE];
int wherefalse[MAXCLAUSE]; /* where each clause is listed in false */
int numtruelit[MAXCLAUSE]; /* number of true literals in each clause */
#endif
int *occurence[2*MAXATOM+1]; /* where each literal occurs */
/* indexed as occurence[literal+MAXATOM][occurence_num] */
int numoccurence[2*MAXATOM+1]; /* number of times each literal occurs */
int atom[MAXATOM+1]; /* value of each atom */
int lowatom[MAXATOM+1];
int solution[MAXATOM+1];
int changed[MAXATOM+1]; /* step at which atom was last flipped */
int breakcount[MAXATOM+1]; /* number of clauses that become unsat if var if flipped */
int makecount[MAXATOM+1]; /* number of clauses that become sat if var if flipped */
int numfalse; /* number of false clauses */
/************************************/
/* Global flags and parameters */
/************************************/
int abort_flag;
int heuristic = BEST; /* heuristic to be used */
int numerator = NOVALUE; /* make random flip with numerator/denominator frequency */
int denominator = 100;
int tabu_length; /* length of tabu list */
long int numflip; /* number of changes so far */
long int numnullflip; /* number of times a clause was picked, but no */
/* variable from it was flipped */
int numrun = 10;
int cutoff = 100000;
int base_cutoff = 100000;
int target = 0;
int numtry = 0; /* total attempts at solutions */
int numsol = NOVALUE; /* stop after this many tries succeeds */
int superlinear = FALSE;
int makeflag = FALSE; /* set to true by heuristics that require the make values to be calculated */
/* Histogram of tail */
long int tailhist[HISTMAX]; /* histogram of num unsat in tail of run */
long histtotal;
int tail = 3;
int tail_start_flip;
/* Printing options */
int printonlysol = FALSE;
int printsolcnf = FALSE;
int printfalse = FALSE;
int printlow = FALSE;
int printhist = FALSE;
int printtrace = FALSE;
int trace_assign = FALSE;
/* Initialization options */
char initfile[100] = { 0 };
int initoptions = FALSE;
/* Randomization */
int seed; /* seed for random */
struct timeval tv;
struct timezone tzp;
/* Statistics */
double expertime;
long flips_this_solution;
long int lowbad; /* lowest number of bad clauses during try */
long int totalflip = 0; /* total number of flips in all tries so far */
long int totalsuccessflip = 0; /* total number of flips in all tries which succeeded so far */
int numsuccesstry = 0; /* total found solutions */
long x;
long integer_sum_x = 0;
double sum_x = 0.0;
double sum_x_squared = 0.0;
double mean_x;
double second_moment_x;
double variance_x;
double std_dev_x;
double std_error_mean_x;
double seconds_per_flip;
int r;
int sum_r = 0;
double sum_r_squared = 0.0;
double mean_r;
double variance_r;
double std_dev_r;
double std_error_mean_r;
double avgfalse;
double sumfalse;
double sumfalse_squared;
double second_moment_avgfalse, variance_avgfalse, std_dev_avgfalse, ratio_avgfalse;
double std_dev_avgfalse;
double f;
double sample_size;
double sum_avgfalse = 0.0;
double sum_std_dev_avgfalse = 0.0;
double mean_avgfalse;
double mean_std_dev_avgfalse;
int number_sampled_runs = 0;
double ratio_mean_avgfalse;
double suc_sum_avgfalse = 0.0;
double suc_sum_std_dev_avgfalse = 0.0;
double suc_mean_avgfalse;
double suc_mean_std_dev_avgfalse;
int suc_number_sampled_runs = 0;
double suc_ratio_mean_avgfalse;
double nonsuc_sum_avgfalse = 0.0;
double nonsuc_sum_std_dev_avgfalse = 0.0;
double nonsuc_mean_avgfalse;
double nonsuc_mean_std_dev_avgfalse;
int nonsuc_number_sampled_runs = 0;
double nonsuc_ratio_mean_avgfalse;
/* Hamming calcualations */
char hamming_target_file[512] = { 0 };
char hamming_data_file[512] = { 0 };
int hamming_sample_freq;
int hamming_flag = FALSE;
int hamming_distance;
int hamming_target[MAXATOM+1];
void read_hamming_file(char initfile[]);
void open_hamming_data(char initfile[]);
int calc_hamming_dist(int atom[], int hamming_target[], int numatom);
FILE * hamming_fp;
/* Noise level */
int samplefreq = 1;
/************************************/
/* Forward declarations */
/************************************/
void parse_parameters(int argc,char *argv[]);
void print_parameters(int argc, char * argv[]);
int pickrandom(void);
int pickbest(void);
int picktabu(void);
int picknovelty(void);
int pickrnovelty(void);
char * heuristic_names[] = { "random", "best", "tabu", "novelty", "rnovelty" };
static int (*pickcode[])(void) =
{pickrandom, pickbest, picktabu, picknovelty, pickrnovelty};
double elapsed_seconds(void);
int countunsat(void);
void scanone(int argc, char *argv[], int i, int *varptr);
void init(char initfile[], int initoptions);
void initprob(void); /* create a new problem */
void flipatom(int toflip); /* changes the assignment of the given literal */
void print_false_clauses(long int lowbad);
void save_false_clauses(long int lowbad);
void print_low_assign(long int lowbad);
void save_low_assign(void);
void save_solution(void);
void print_current_assign(void);
void handle_interrupt(int sig);
long super(int i);
void print_statistics_header(void);
void initialize_statistics(void);
void update_statistics_start_try(void);
void print_statistics_start_flip(void);
void update_and_print_statistics_end_try(void);
void update_statistics_end_flip(void);
void print_statistics_final(void);
void print_sol_cnf(void);
/************************************/
/* Main */
/************************************/
int main(int argc,char *argv[])
{
gettimeofday(&tv,&tzp);
seed = (( tv.tv_sec & 0177 ) * 1000000) + tv.tv_usec;
parse_parameters(argc, argv);
srandom(seed);
print_parameters(argc, argv);
initprob();
initialize_statistics();
print_statistics_header();
signal(SIGINT, (void *) handle_interrupt);
abort_flag = FALSE;
(void) elapsed_seconds();
while (! abort_flag && numsuccesstry < numsol && numtry < numrun) {
numtry++;
init(initfile, initoptions);
update_statistics_start_try();
numflip = 0;
if (superlinear) cutoff = base_cutoff * super(numtry);
while((numfalse > target) && (numflip < cutoff)) {
print_statistics_start_flip();
numflip++;
flipatom((pickcode[heuristic])());
update_statistics_end_flip();
}
update_and_print_statistics_end_try();
}
expertime = elapsed_seconds();
print_statistics_final();
return 0;
}
void parse_parameters(int argc,char *argv[])
{
int i;
int temp;
for (i=1;i < argc;i++)
{
if (strcmp(argv[i],"-seed") == 0)
scanone(argc,argv,++i,&seed);
else if (strcmp(argv[i],"-hist") == 0)
printhist = TRUE;
else if (strcmp(argv[i],"-cutoff") == 0)
scanone(argc,argv,++i,&cutoff);
else if (strcmp(argv[i],"-random") == 0)
heuristic = RANDOM;
else if (strcmp(argv[i],"-novelty") == 0){
heuristic = NOVELTY;
makeflag = TRUE;
}
else if (strcmp(argv[i],"-rnovelty") == 0){
heuristic = RNOVELTY;
makeflag = TRUE;
}
else if (strcmp(argv[i],"-best") == 0)
heuristic = BEST;
else if (strcmp(argv[i],"-noise") == 0){
scanone(argc,argv,++i,&numerator);
if (i < argc-1 && sscanf(argv[i+1],"%i",&temp)==1){
denominator = temp;
i++;
}
}
else if (strcmp(argv[i],"-init") == 0 && i < argc-1)
sscanf(argv[++i], " %s", initfile);
else if (strcmp(argv[i],"-hamming") == 0 && i < argc-3){
sscanf(argv[++i], " %s", hamming_target_file);
sscanf(argv[++i], " %s", hamming_data_file);
sscanf(argv[++i], " %i", &hamming_sample_freq);
hamming_flag = TRUE;
numrun = 1;
}
else if (strcmp(argv[i],"-partial") == 0)
initoptions = INIT_PARTIAL;
else if (strcmp(argv[i],"-super") == 0)
superlinear = TRUE;
else if (strcmp(argv[i],"-tries") == 0)
scanone(argc,argv,++i,&numrun);
else if (strcmp(argv[i],"-target") == 0)
scanone(argc,argv,++i,&target);
else if (strcmp(argv[i],"-tail") == 0)
scanone(argc,argv,++i,&tail);
else if (strcmp(argv[i],"-sample") == 0)
scanone(argc,argv,++i,&samplefreq);
else if (strcmp(argv[i],"-tabu") == 0)
{
scanone(argc,argv,++i,&tabu_length);
heuristic = TABU;
}
else if (strcmp(argv[i],"-low") == 0)
printlow = TRUE;
else if (strcmp(argv[i],"-sol") == 0)
{
printonlysol = TRUE;
printlow = TRUE;
}
else if (strcmp(argv[i],"-solcnf") == 0)
{
printsolcnf = TRUE;
if (numsol == NOVALUE) numsol = 1;
}
else if (strcmp(argv[i],"-bad") == 0)
printfalse = TRUE;
else if (strcmp(argv[i],"-numsol") == 0)
scanone(argc,argv,++i,&numsol);
else if (strcmp(argv[i],"-trace") == 0)
scanone(argc,argv,++i,&printtrace);
else if (strcmp(argv[i],"-assign") == 0){
scanone(argc,argv,++i,&printtrace);
trace_assign = TRUE;
}
else
{
if (strcmp(argv[i],"-help")!=0 && strcmp(argv[i],"-h")!=0 )
fprintf(stderr, "Bad argument %s\n", argv[i]);
fprintf(stderr, "General parameters:\n");
fprintf(stderr, " -seed N -cutoff N -tries N -target N\n");
fprintf(stderr, " -numsol N = stop after finding N solutions\n");
fprintf(stderr, " -init FILE = set vars not included in FILE to false\n");
fprintf(stderr, " -partial FILE = set vars not included in FILE randomly\n");
fprintf(stderr, "Heuristics:\n");
fprintf(stderr, " -random -best -tabu N -novelty -rnovelty\n");
fprintf(stderr, " -noise N or -noise N M (default M = 100)\n");
fprintf(stderr, "Printing:\n");
fprintf(stderr, " -hamming TARGET_FILE DATA_FILE SAMPLE_FREQUENCY\n");
fprintf(stderr, " -trace N = print statistics every N flips\n");
fprintf(stderr, " -assign N = print assignments at flip N, 2N, ...\n");
fprintf(stderr, " -sol = print satisfying assignments\n");
fprintf(stderr, " -low = print lowest assignment each try\n");
fprintf(stderr, " -bad = print unsat clauses each try\n");
fprintf(stderr, " -tail N = assume tail begins at nvars*N\n");
fprintf(stderr, " -solcnf = print sat assign in cnf format, and exit\n");
fprintf(stderr, " -sample N = sample noise level every N flips\n");
exit(-1);
}
}
base_cutoff = cutoff;
if (numsol==NOVALUE || numsol>numrun) numsol = numrun;
if (numerator==NOVALUE){
switch(heuristic) {
case BEST:
case NOVELTY:
case RNOVELTY:
numerator = 50;
break;
default:
numerator = 0;
break;
}
}
}
void print_parameters(int argc, char * argv[])
{
int i;
#ifdef Huge
printf("walksat version 35 (Huge)\n");
#else
printf("walksat version 35\n");
#endif
printf("command line =");
for (i=0;i < argc;i++){
printf(" %s", argv[i]);
}
printf("\n");
printf("seed = %i\n",seed);
printf("cutoff = %i\n",cutoff);
printf("tries = %i\n",numrun);
printf("heuristic = ");
switch(heuristic)
{
case TABU:
printf("tabu %d", tabu_length);
break;
default:
printf("%s", heuristic_names[heuristic]);
break;
}
if (numerator>0){
printf(", noise %d / %d", numerator, denominator);
}
printf("\n");
}
void print_statistics_header(void)
{
printf("numatom = %i, numclause = %i, numliterals = %i\n",numatom,numclause,numliterals);
printf("wff read in\n\n");
printf(" lowest final avg noise noise total avg mean mean\n");
printf(" #unsat #unsat noise std dev ratio flips length flips flips\n");
printf(" this this this this this this success success until std\n");
printf(" try try try try try try rate tries assign dev\n\n");
fflush(stdout);
}
void initialize_statistics(void)
{
x = 0; r = 0;
if (hamming_flag) {
read_hamming_file(hamming_target_file);
open_hamming_data(hamming_data_file);
}
tail_start_flip = tail * numatom;
printf("tail starts after flip = %i\n", tail_start_flip);
numnullflip = 0;
}
void update_statistics_start_try(void)
{
int i;
lowbad = numfalse;
sample_size = 0;
sumfalse = 0.0;
sumfalse_squared = 0.0;
for (i=0; i<HISTMAX; i++)
tailhist[i] = 0;
if (tail_start_flip == 0){
tailhist[numfalse < HISTMAX ? numfalse : HISTMAX - 1] ++;
}
if (printfalse) save_false_clauses(lowbad);
if (printlow) save_low_assign();
}
void print_statistics_start_flip(void)
{
if (printtrace && (numflip % printtrace == 0)){
printf(" %9i %9i %9li\n", lowbad,numfalse,numflip);
if (trace_assign)
print_current_assign();
fflush(stdout);
}
}
void update_and_print_statistics_end_try(void)
{
int i;
int j;
totalflip += numflip;
x += numflip;
r ++;
if (sample_size > 0){
avgfalse = sumfalse/sample_size;
second_moment_avgfalse = sumfalse_squared / sample_size;
variance_avgfalse = second_moment_avgfalse - (avgfalse * avgfalse);
if (sample_size > 1) { variance_avgfalse = (variance_avgfalse * sample_size)/(sample_size - 1); }
std_dev_avgfalse = sqrt(variance_avgfalse);
ratio_avgfalse = avgfalse / std_dev_avgfalse;
sum_avgfalse += avgfalse;
sum_std_dev_avgfalse += std_dev_avgfalse;
number_sampled_runs += 1;
if (numfalse == 0){
suc_number_sampled_runs += 1;
suc_sum_avgfalse += avgfalse;
suc_sum_std_dev_avgfalse += std_dev_avgfalse;
}
else {
nonsuc_number_sampled_runs += 1;
nonsuc_sum_avgfalse += avgfalse;
nonsuc_sum_std_dev_avgfalse += std_dev_avgfalse;
}
}
else{
avgfalse = 0;
variance_avgfalse = 0;
std_dev_avgfalse = 0;
ratio_avgfalse = 0;
}
if(numfalse == 0){
save_solution();
numsuccesstry++;
totalsuccessflip += numflip;
integer_sum_x += x;
sum_x = (double) integer_sum_x;
sum_x_squared += ((double)x)*((double)x);
mean_x = sum_x / numsuccesstry;
if (numsuccesstry > 1){
second_moment_x = sum_x_squared / numsuccesstry;
variance_x = second_moment_x - (mean_x * mean_x);
/* Adjustment for small small sample size */
variance_x = (variance_x * numsuccesstry)/(numsuccesstry - 1);
std_dev_x = sqrt(variance_x);
std_error_mean_x = std_dev_x / sqrt((double)numsuccesstry);
}
sum_r += r;
mean_r = ((double)sum_r)/numsuccesstry;
sum_r_squared += ((double)r)*((double)r);
x = 0;
r = 0;
}
printf(" %9i %9i %9.2f %9.2f %9.2f %9li %9li",
lowbad,numfalse,avgfalse, std_dev_avgfalse,ratio_avgfalse,numflip, (numsuccesstry*100)/numtry);
if (numsuccesstry > 0){
printf(" %9i", totalsuccessflip/numsuccesstry);
printf(" %11.2f", mean_x);
if (numsuccesstry > 1){
printf(" %11.2f", std_dev_x);
}
}
printf("\n");
if (printhist){
printf("histogram: ");
for (j=HISTMAX-1; tailhist[j] == 0; j--);
for (i=0; i<=j; i++){
printf(" %i(%i)", tailhist[i], i);
if ((i+1) % 10 == 0) printf("\n ");
}
if (j==HISTMAX-1) printf(" +++");
printf("\n");
}
if (numfalse>0 && printfalse)
print_false_clauses(lowbad);
if (printlow && (!printonlysol || numfalse >= target))
print_low_assign(lowbad);
if(numfalse == 0 && countunsat() != 0){
fprintf(stderr, "Program error, verification of solution fails!\n");
exit(-1);
}
fflush(stdout);
}
void update_statistics_end_flip(void)
{
if (numfalse < lowbad){
lowbad = numfalse;
if (printfalse) save_false_clauses(lowbad);
if (printlow) save_low_assign();
}
if (numflip >= tail_start_flip){
tailhist[(numfalse < HISTMAX) ? numfalse : (HISTMAX - 1)] ++;
if ((numflip % samplefreq) == 0){
sumfalse += numfalse;
sumfalse_squared += numfalse * numfalse;
sample_size ++;
}
}
}
void print_statistics_final(void)
{
seconds_per_flip = expertime / totalflip;
printf("\ntotal elapsed seconds = %f\n", expertime);
printf("average flips per second = %d\n", (long)(totalflip/expertime));
if (heuristic == TABU)
printf("proportion null flips = %f\n", ((double)numnullflip)/totalflip);
printf("number solutions found = %d\n", numsuccesstry);
printf("final success rate = %f\n", ((double)numsuccesstry * 100.0)/numtry);
printf("average length successful tries = %li\n", numsuccesstry ? (totalsuccessflip/numsuccesstry) : 0);
if (numsuccesstry > 0)
{
printf("mean flips until assign = %f\n", mean_x);
if (numsuccesstry>1){
printf(" variance = %f\n", variance_x);
printf(" standard deviation = %f\n", std_dev_x);
printf(" standard error of mean = %f\n", std_error_mean_x);
}
printf("mean seconds until assign = %f\n", mean_x * seconds_per_flip);
if (numsuccesstry>1){
printf(" variance = %f\n", variance_x * seconds_per_flip * seconds_per_flip);
printf(" standard deviation = %f\n", std_dev_x * seconds_per_flip);
printf(" standard error of mean = %f\n", std_error_mean_x * seconds_per_flip);
}
printf("mean restarts until assign = %f\n", mean_r);
if (numsuccesstry>1){
variance_r = (sum_r_squared / numsuccesstry) - (mean_r * mean_r);
if (numsuccesstry > 1) variance_r = (variance_r * numsuccesstry)/(numsuccesstry - 1);
std_dev_r = sqrt(variance_r);
std_error_mean_r = std_dev_r / sqrt((double)numsuccesstry);
printf(" variance = %f\n", variance_r);
printf(" standard deviation = %f\n", std_dev_r);
printf(" standard error of mean = %f\n", std_error_mean_r);
}
}
if (number_sampled_runs){
mean_avgfalse = sum_avgfalse / number_sampled_runs;
mean_std_dev_avgfalse = sum_std_dev_avgfalse / number_sampled_runs;
ratio_mean_avgfalse = mean_avgfalse / mean_std_dev_avgfalse;
if (suc_number_sampled_runs){
suc_mean_avgfalse = suc_sum_avgfalse / suc_number_sampled_runs;
suc_mean_std_dev_avgfalse = suc_sum_std_dev_avgfalse / suc_number_sampled_runs;
suc_ratio_mean_avgfalse = suc_mean_avgfalse / suc_mean_std_dev_avgfalse;
}
else {
suc_mean_avgfalse = 0;
suc_mean_std_dev_avgfalse = 0;
suc_ratio_mean_avgfalse = 0;
}
if (nonsuc_number_sampled_runs){
nonsuc_mean_avgfalse = nonsuc_sum_avgfalse / nonsuc_number_sampled_runs;
nonsuc_mean_std_dev_avgfalse = nonsuc_sum_std_dev_avgfalse / nonsuc_number_sampled_runs;
nonsuc_ratio_mean_avgfalse = nonsuc_mean_avgfalse / nonsuc_mean_std_dev_avgfalse;
}
else {
nonsuc_mean_avgfalse = 0;
nonsuc_mean_std_dev_avgfalse = 0;
nonsuc_ratio_mean_avgfalse = 0;
}
printf("final noise level statistics\n");
printf(" statistics over all runs:\n");
printf(" overall mean average noise level = %f\n", mean_avgfalse);
printf(" overall mean noise std deviation = %f\n", mean_std_dev_avgfalse);
printf(" overall ratio mean noise to mean std dev = %f\n", ratio_mean_avgfalse);
printf(" statistics on successful runs:\n");
printf(" successful mean average noise level = %f\n", suc_mean_avgfalse);
printf(" successful mean noise std deviation = %f\n", suc_mean_std_dev_avgfalse);
printf(" successful ratio mean noise to mean std dev = %f\n", suc_ratio_mean_avgfalse);
printf(" statistics on nonsuccessful runs:\n");
printf(" nonsuccessful mean average noise level = %f\n", nonsuc_mean_avgfalse);
printf(" nonsuccessful mean noise std deviation = %f\n", nonsuc_mean_std_dev_avgfalse);
printf(" nonsuccessful ratio mean noise to mean std dev = %f\n", nonsuc_ratio_mean_avgfalse);
}
if (hamming_flag){
close(hamming_fp);
printf("Final distance to hamming target = %i\n", calc_hamming_dist(atom, hamming_target, numatom));
printf("Hamming distance data stored in %s\n", hamming_data_file);
}
if (numsuccesstry > 0){
printf("ASSIGNMENT FOUND\n");
if(printsolcnf == TRUE) print_sol_cnf();
}
else
printf("ASSIGNMENT NOT FOUND\n");
}
long super(int i)
{
long power;
int k;
if (i<=0){
fprintf(stderr, "bad argument super(%d)\n", i);
exit(1);
}
/* let 2^k be the least power of 2 >= (i+1) */
k = 1;
power = 2;
while (power < (i+1)){
k += 1;
power *= 2;
}
if (power == (i+1)) return (power/2);
return (super(i - (power/2) + 1));
}
void handle_interrupt(int sig)
{
if (abort_flag) exit(-1);
abort_flag = TRUE;
}
void scanone(int argc, char *argv[], int i, int *varptr)
{
if (i>=argc || sscanf(argv[i],"%i",varptr)!=1){
fprintf(stderr, "Bad argument %s\n", i<argc ? argv[i] : argv[argc-1]);
exit(-1);
}
}
int calc_hamming_dist(int atom[], int hamming_target[], int numatom)
{
int i;
int dist = 0;
for (i=1; i<=numatom; i++){
if (atom[i] != hamming_target[i]) dist++;
}
return dist;
}
void open_hamming_data(char initfile[])
{
if ((hamming_fp = fopen(initfile, "w")) == NULL){
fprintf(stderr, "Cannot open %s for output\n", initfile);
exit(1);
}
}
void read_hamming_file(char initfile[])
{
int i; /* loop counter */
FILE * infile;
int lit;
printf("loading hamming target file %s ...", initfile);
if ((infile = fopen(initfile, "r")) == NULL){
fprintf(stderr, "Cannot open %s\n", initfile);
exit(1);
}
i=0;
for(i = 1;i < numatom+1;i++)
hamming_target[i] = 0;
while (fscanf(infile, " %d", &lit)==1){
if (ABS(lit)>numatom){
fprintf(stderr, "Bad hamming file %s\n", initfile);
exit(1);
}
if (lit>0) hamming_target[lit]=1;
}
printf("done\n");
}
void init(char initfile[], int initoptions)
{
int i;
int j;
int thetruelit;
FILE * infile;
int lit;
for(i = 0;i < numclause;i++)
numtruelit[i] = 0;
numfalse = 0;
for(i = 1;i < numatom+1;i++)
{
changed[i] = -BIG;
breakcount[i] = 0;
makecount[i] = 0;
}
if (initfile[0] && initoptions!=INIT_PARTIAL){
for(i = 1;i < numatom+1;i++)
atom[i] = 0;
}
else {
for(i = 1;i < numatom+1;i++)
atom[i] = random()%2;
}
if (initfile[0]){
if ((infile = fopen(initfile, "r")) == NULL){
fprintf(stderr, "Cannot open %s\n", initfile);
exit(1);
}
i=0;
while (fscanf(infile, " %d", &lit)==1){
i++;
if (ABS(lit)>numatom){
fprintf(stderr, "Bad init file %s\n", initfile);
exit(1);
}
if (lit<0) atom[-lit]=0;
else atom[lit]=1;
}
if (i==0){
fprintf(stderr, "Bad init file %s\n", initfile);
exit(1);
}
close(infile);
/* printf("read %d values\n", i); */
}
/* Initialize breakcount and makecount in the following: */
for(i = 0;i < numclause;i++)
{
for(j = 0;j < size[i];j++)
{
if((clause[i][j] > 0) == atom[ABS(clause[i][j])])
{
numtruelit[i]++;
thetruelit = clause[i][j];
}
}
if(numtruelit[i] == 0)
{
wherefalse[i] = numfalse;
false[numfalse] = i;
numfalse++;
for(j = 0;j < size[i];j++){
makecount[ABS(clause[i][j])]++;
}
}
else if (numtruelit[i] == 1)
{
breakcount[ABS(thetruelit)]++;
}
}
if (hamming_flag){
hamming_distance = calc_hamming_dist(atom, hamming_target, numatom);
fprintf(hamming_fp, "0 %i\n", hamming_distance);
}
}
void
print_false_clauses(long int lowbad)
{
int i, j;
int cl;
printf("Unsatisfied clauses:\n");
for (i=0; i<lowbad; i++){
cl = lowfalse[i];
for (j=0; j<size[cl]; j++){
printf("%d ", clause[cl][j]);
}
printf("0\n");
}
printf("End unsatisfied clauses\n");
}
void
save_false_clauses(long int lowbad)
{
int i;
for (i=0; i<lowbad; i++)
lowfalse[i] = false[i];
}
void initprob(void)
{
int i; /* loop counter */
int j; /* another loop counter */
int lastc;
int nextc;
int *storeptr;
int freestore;
int lit;
while ((lastc = getchar()) == 'c')
{
while ((nextc = getchar()) != EOF && nextc != '\n');
}
ungetc(lastc,stdin);
if (scanf("p cnf %i %i",&numatom,&numclause) != 2)
{
fprintf(stderr,"Bad input file: wrong p line\n");
exit(-1);
}
if(numatom > MAXATOM)
{
fprintf(stderr,"ERROR - too many atoms\n");
exit(-1);
}
#ifdef Huge
clause = (int **) malloc(sizeof(int *)*(numclause+1));
size = (int *) malloc(sizeof(int)*(numclause+1));
false = (int *) malloc(sizeof(int)*(numclause+1));
lowfalse = (int *) malloc(sizeof(int)*(numclause+1));
wherefalse = (int *) malloc(sizeof(int)*(numclause+1));
numtruelit = (int *) malloc(sizeof(int)*(numclause+1));
#else
if(numclause > MAXCLAUSE)
{
fprintf(stderr,"ERROR - too many clauses\n");
exit(-1);
}
#endif