-
Notifications
You must be signed in to change notification settings - Fork 1
/
walksat.c
1809 lines (1553 loc) · 47.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 */
/* Maintained by Henry Kautz <kautz@cs.rochester.edu> */
#define VERSION "walksat-v51"
/************************************/
/* Compilation flags */
/************************************/
/* If the constant DYNAMIC 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 the compiled program is about 15% slower, because not
as many optimizations can be performed. */
#define DYNAMIC 1
/********************************************************************/
/* Please select only one of following flags: UNIX, OSX, ANSI or NT */
/* Description: */
/* UNIX: uses some non-POSIX unix time routines, compiles */
/* (at least) under Linux and Mac OS X */
/* ANSI: use POSIX time routines, can compile under all */
/* unix and NT, time accuracy is 1sec */
/* NT: use NT/Win32 multimedia routines, compile under */
/* NT only, time accuracy is 1ms */
/* Uses NT "timeGetTime" function: */
/* Header: Decl. in Mmsystem.h; include Windows.h. */
/* Library: Use Winmm.lib. */
/********************************************************************/
#define UNIX 1
#define ANSI 0
#define NT 0
/* Define BIGINT to be the type used for the "cutoff" variable.
Under gcc "long long int" gives a 64 bit integer.
Program will still function using a 32-bit value, but it
limit size of cutoffs that can be specified. */
#if UNIX
#define BIGINT long long int
#define BIGFORMAT "lli"
#endif
#if NT
#define BIGINT __int64
#define BIGFORMAT "I64d"
#endif
#if ANSI
#define BIGINT long int
#define BIGFORMAT "li"
#endif
/************************************/
/* Standard includes */
/************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <limits.h>
#include <signal.h>
#if UNIX
#include <sys/times.h>
#include <sys/time.h>
#include <unistd.h>
#endif
#if NT
#include <time.h>
#include <windows.h>
#include <mmsystem.h>
#endif
#if ANSI
#include <sys/time.h>
#endif
#if ANSI || NT
#define random() rand()
#define srandom(seed) srand(seed)
#endif
/* No longer using constant CLK_TCK - it is deprecated! */
/* Instead: */
long ticks_per_second;
/************************************/
/* Constant parameters */
/************************************/
#define MAXFILENAME 256
#define MAXATOM 150000 /* maximum possible number of atoms */
#ifdef DYNAMIC
#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, NOVELTY_PLUS,
RNOVELTY_PLUS};
#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 DYNAMIC
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 *occurrence[2*MAXATOM+1]; /* where each literal occurs */
/* indexed as occurrence[literal+MAXATOM][occurrence_num] */
int numoccurrence[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 */
/************************************/
FILE * cnfStream;
int status_flag = 0; /* value returned from main procedure */
int abort_flag;
int heuristic = BEST; /* heuristic to be used */
int numerator = NOVALUE; /* make random flip with numerator/denominator frequency */
int denominator = 100;
#define RANDOM_MASK 0xFF
int adjusted_numerator;
int tabu_length; /* length of tabu list */
int wp_numerator = NOVALUE; /* walk probability numerator/denominator */
int wp_denominator = 100;
BIGINT numflip; /* number of changes so far */
BIGINT numnullflip; /* number of times a clause was picked, but no */
/* variable from it was flipped */
int numrun = 10;
BIGINT cutoff = 100000;
BIGINT 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;
char outfile[MAXFILENAME] = { 0 };
/* Initialization options */
char initfile[MAXFILENAME] = { 0 };
int initoptions = FALSE;
/* Randomization */
unsigned int seed; /* Sometimes defined as an unsigned long int */
#if UNIX
struct timeval tv;
struct timezone tzp;
#endif
#if NT
DWORD win_time; /* elapsed time in ms, since windows boot up */
#endif
/* Statistics */
double expertime;
BIGINT flips_this_solution;
int lowbad; /* lowest number of bad clauses during try */
BIGINT totalflip = 0; /* total number of flips in all tries so far */
BIGINT totalsuccessflip = 0; /* total number of flips in all tries which succeeded so far */
int numsuccesstry = 0; /* total found solutions */
BIGINT x;
BIGINT 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 calculations */
char hamming_target_file[MAXFILENAME] = { 0 };
char hamming_data_file[MAXFILENAME] = { 0 };
int hamming_sample_freq;
int hamming_flag = FALSE;
int hamming_distance;
int hamming_target[MAXATOM+1];
FILE * hamming_fp;
/* How frequently to sample the tail for calculating statistics */
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);
int picknoveltyplus(void);
int pickrnoveltyplus(void);
char * heuristic_names[] = { "random", "best", "tabu", "novelty", "rnovelty",
"novelty+", "rnovelty+"};
static int (*pickcode[])(void) =
{pickrandom, pickbest, picktabu, picknovelty, pickrnovelty,
picknoveltyplus, pickrnoveltyplus};
double elapsed_seconds(void);
int countunsat(void);
void scanone(int argc, char *argv[], int i, int *varptr);
void scanonell(int argc, char *argv[], int i, BIGINT *varptr);
void scanoneu(int argc, char *argv[], int i, unsigned int *varptr);
void init(char initfile[], int initoptions);
void initprob(void);
void flipatom(int toflip);
void print_false_clauses(int lowbad);
void save_false_clauses(int lowbad);
void print_low_assign(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_sol_file(char * filename);
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);
void read_hamming_file(char initfile[]);
void open_hamming_data(char initfile[]);
int calc_hamming_dist(int atom[], int hamming_target[], int numatom);
/************************************/
/* Main */
/************************************/
int main(int argc,char *argv[])
{
#if UNIX
ticks_per_second = sysconf(_SC_CLK_TCK);
gettimeofday(&tv,&tzp);
seed = (unsigned int)((( tv.tv_sec & 0177 ) * 1000000) + tv.tv_usec);
#endif
#if NT
seed = (unsigned int)(timeGetTime());
#endif
#if ANSI
seed = (unsigned int)(time());
#endif
parse_parameters(argc, argv);
srandom(seed);
print_parameters(argc, argv);
initprob();
initialize_statistics();
print_statistics_header();
signal(SIGINT, 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 status_flag;
}
void parse_parameters(int argc,char *argv[])
{
int i;
int temp;
cnfStream = stdin;
for (i=1;i < argc;i++)
{
if (argv[i][0] != '-' && cnfStream == stdin){
cnfStream = fopen(argv[i],"r");
if (cnfStream == NULL){
fprintf(stderr, "Cannot open file named %s\n", argv[i]);
exit(-1);
}
++i;
}
else if (strcmp(argv[i],"-seed") == 0){
scanone(argc,argv,++i,&temp);
seed = (unsigned int)temp;
}
else if (strcmp(argv[i],"-out") == 0 && i<argc-1)
strcpy(outfile, argv[++i]);
else if (strcmp(argv[i],"-hist") == 0)
printhist = TRUE;
else if (strcmp(argv[i],"-status") == 0)
status_flag = 1;
else if (strcmp(argv[i],"-cutoff") == 0)
scanonell(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],"-novelty+") == 0){
heuristic = NOVELTY_PLUS;
makeflag = TRUE;
}
else if (strcmp(argv[i],"-rnovelty+") == 0){
heuristic = RNOVELTY_PLUS;
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],"-wp") == 0){
scanone(argc,argv,++i,&wp_numerator);
if (i < argc-1 && sscanf(argv[i+1],"%i",&temp)==1){
wp_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 || strcmp(argv[i],"-restart") == 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
{
fprintf(stderr, "General parameters:\n");
fprintf(stderr, " filename = CNF file, if not specified read stdin\n");
fprintf(stderr, " -seed N = use N to initialize random()\n");
fprintf(stderr, " -cutoff N = bound on the number of flips per trial\n");
fprintf(stderr, " -restart N = bound on the number of trials\n");
fprintf(stderr, " -numsol N = stop after finding N solutions\n");
fprintf(stderr, " -super N = use the series 1,1,2,1,1,2,4,... times cutoff for bound on flips per trial\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, " -status = return fail status if solution not found\n");
fprintf(stderr, " -target N = succeed if N or fewer clauses unsatisfied\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, " -novelty+ -rnovelty+\n");
fprintf(stderr, " -wp N or -wp N M (default M = 100) = cycle breaking for (r)novelty+\n");
fprintf(stderr, "Printing:\n");
fprintf(stderr, " -out FILE = print solution as a list of literals to FILE\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 to stdout\n");
fprintf(stderr, " -solcnf = print sat assign to stdout in DIMACS format, and exit\n");
fprintf(stderr, " -low = print lowest assignment each try\n");
fprintf(stderr, " -bad = print unsat clauses each try\n");
fprintf(stderr, " -hist = print histogram of tail\n");
fprintf(stderr, " -tail N = assume tail begins at nvars*N\n");
fprintf(stderr, " -sample N = sample noise level every N flips\n");
fprintf(stderr, " -hamming TARGET_FILE DATA_FILE SAMPLE_FREQUENCY\n");
exit(-1);
}
}
base_cutoff = cutoff;
if (numsol==NOVALUE || numsol>numrun) numsol = numrun;
if (numerator==NOVALUE){
switch(heuristic) {
case BEST:
case NOVELTY:
case RNOVELTY:
case NOVELTY_PLUS:
case RNOVELTY_PLUS:
numerator = 50;
break;
default:
numerator = 0;
break;
}
}
if (wp_numerator==NOVALUE){
switch(heuristic) {
case NOVELTY_PLUS:
case RNOVELTY_PLUS:
wp_numerator = 1;
break;
default:
wp_numerator = 0;
break;
}
}
adjusted_numerator = numerator * (RANDOM_MASK + 1) / denominator;
}
void print_parameters(int argc, char * argv[])
{
int i;
printf("%s\n", VERSION);
printf("command line =");
for (i=0;i < argc;i++){
printf(" %s", argv[i]);
}
printf("\n");
printf("seed = %u\n",seed);
printf("cutoff = %" BIGFORMAT "\n",cutoff);
printf("tries = %i\n",numrun);
printf("heuristic = ");
switch(heuristic)
{
case TABU:
printf("tabu %i", tabu_length);
break;
default:
printf("%s", heuristic_names[heuristic]);
break;
}
if (numerator>0){
printf(", noise %i / %i", numerator, denominator);
}
if (wp_numerator>0){
printf(", wp %i / %i", wp_numerator, wp_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 dist dist total avg mean mean\n");
printf(" #unsat #unsat dist 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 %9" BIGFORMAT "\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 <= target){
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 <= target){
status_flag = 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 %9" BIGFORMAT " %9i",
lowbad,numfalse,avgfalse, std_dev_avgfalse,ratio_avgfalse,numflip, (numsuccesstry*100)/numtry);
if (numsuccesstry > 0){
printf(" %9" BIGFORMAT, 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(" %li(%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 = %li\n", (long)(totalflip/expertime));
if (heuristic == TABU)
printf("proportion null flips = %f\n", ((double)numnullflip)/totalflip);
printf("number solutions found = %i\n", numsuccesstry);
printf("final success rate = %f\n", ((double)numsuccesstry * 100.0)/numtry);
printf("average length successful tries = %" BIGFORMAT "\n", numsuccesstry ? (totalsuccessflip/numsuccesstry) : 0);
if (numsuccesstry > 0)
{
printf("average flips per assign (over all runs) = %f\n", ((double)totalflip)/numsuccesstry);
printf("average seconds per assign (over all runs) = %f\n", (((double)totalflip)/numsuccesstry)*seconds_per_flip);
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 numbad level statistics\n");
printf(" statistics over all runs:\n");
printf(" overall mean average numbad = %f\n", mean_avgfalse);
printf(" overall mean meanbad std deviation = %f\n", mean_std_dev_avgfalse);
printf(" overall ratio mean numbad to mean std dev = %f\n", ratio_mean_avgfalse);
printf(" statistics on successful runs:\n");
printf(" successful mean average numbad = %f\n", suc_mean_avgfalse);
printf(" successful mean numbad std deviation = %f\n", suc_mean_std_dev_avgfalse);
printf(" successful ratio mean numbad to mean std dev = %f\n", suc_ratio_mean_avgfalse);
printf(" statistics on nonsuccessful runs:\n");
printf(" nonsuccessful mean average numbad level = %f\n", nonsuc_mean_avgfalse);
printf(" nonsuccessful mean numbad std deviation = %f\n", nonsuc_mean_std_dev_avgfalse);
printf(" nonsuccessful ratio mean numbad to mean std dev = %f\n", nonsuc_ratio_mean_avgfalse);
}
if (hamming_flag){
fclose(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();
if (outfile[0]) print_sol_file(outfile);
}
else
printf("ASSIGNMENT NOT FOUND\n");
}
long super(int i)
{
long power;
int k;
if (i<=0){
fprintf(stderr, "bad argument super(%i)\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);
}
}
void scanoneu(int argc, char *argv[], int i, unsigned int *varptr)
{
if (i>=argc || sscanf(argv[i],"%u",varptr)!=1){
fprintf(stderr, "Bad argument %s\n", i<argc ? argv[i] : argv[argc-1]);
exit(-1);
}
}
void scanonell(int argc, char *argv[], int i, BIGINT *varptr)
{
if (i>=argc || sscanf(argv[i],"%lli",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, " %i", &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");
}