@@ -21,98 +21,37 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121THE SOFTWARE.
2222***********************************************/
2323
24- /* *******************************************************************/
25- /* Following tests set exactly one of the following flags to 1: */
26- /* BSD: BSD Unix */
27- /* OSX: Apple OS X */
28- /* LINUX: Linux Unix */
29- /* WINDOWS: Windows and DOS. Linking requires -l Winmm.lib */
30- /* POSIX: Other POSIX OS */
31- /* Platform dependent differences: */
32- /* WINDOWS and POSIX use rand() instead of random() */
33- /* Clock ticks per second determined by sysconf(_SC_CLK_TCK) */
34- /* for BSD, OSX, and LINUX */
35- /* Clock ticks per second fixed at 1000 for Windows */
36- /* Clock ticks per second fixed at 1 for POSIX */
37- /* *******************************************************************/
38-
3924#include " time_mem.h"
40-
41- #if __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __bsdi__ || _SYSTYPE_BSD
42- #define BSD 1
43- #elif __APPLE__ && __MACH__
44- #define OSX 1
45- #elif __unix__ || __unix || unix || __gnu_linux__ || linux || __linux
46- #define LINUX 1
47- #elif _WIN64 || _WIN23 || _WIN16 || __MSDOS__ || MSDOS || _MSDOS || __DOS__
48- #define NT 1
49- #else
50- #define POSIX 1
51- #endif
52-
53- // printing
54- #if BSD || OSX || LINUX
55- #define BIGFORMAT " li"
56- #elif WINDOWS
57- #define BIGFORMAT " I64d"
58- #endif
59-
60- /* ***********************************/
61- /* Standard includes */
62- /* ***********************************/
63-
6425#include < limits>
6526#include < cstdio>
6627#include < cmath>
6728#include < cstdlib>
6829#include " walksat.h"
6930
70- #ifdef WINDOWS
71- #define random () rand()
72- #define srandom (seed ) srand(seed)
73- #endif
74-
7531/* ***********************************/
7632/* Constant parameters */
7733/* ***********************************/
7834
79- #define BIG 1000000000 /* a number bigger that the possible number of violated clauses */
80- #define MAXATTEMPT 10 /* max number of times to attempt to find a non-tabu variable to flip */
8135#define denominator 100000 /* denominator used in fractions to represent probabilities */
82- #define ONE_PERCENT 1000 /* ONE_PERCENT / denominator = 0.01 */
8336
8437using namespace CMSat ;
8538
8639/* #define DEBUG */
8740
88- /* *************************************/
89- /* Inline utility functions */
90- /* *************************************/
91-
92- static inline int ABS (int x)
41+ uint32_t WalkSAT::RANDMOD (uint32_t x)
9342{
94- return x < 0 ? -x : x;
95- }
96-
97- static inline int RANDMOD (int x)
98- {
99- return x > 1 ? random () % x : 0 ;
43+ return x > 1 ? mtrand.randInt (x-1 ) : 0 ;
10044}
10145
10246static inline int MAX (int x, int y)
10347{
10448 return x > y ? x : y;
10549}
10650
107- /* ***********************************/
108- /* Main */
109- /* ***********************************/
110-
11151int WalkSAT::main ()
11252{
113- seed = 0 ;
11453 parse_parameters ();
115- srandom ( seed);
54+ mtrand. seed ( 1U );
11655 print_parameters ();
11756 initprob ();
11857 initialize_statistics ();
@@ -281,7 +220,7 @@ void WalkSAT::initprob()
281220 }
282221 ungetc (lastc, cnfStream);
283222 if (fscanf (cnfStream, " p cnf %i %i" , &numvars, &numclauses) != 2 ) {
284- fprintf (stderr, " Bad input file\n " ) ;
223+ cout << " Bad input file" << endl ;
285224 exit (-1 );
286225 }
287226
@@ -304,7 +243,7 @@ void WalkSAT::initprob()
304243 /* Read in the clauses and set number of occurrences of each literal */
305244 storesize = 1024 ;
306245 storeused = 0 ;
307- printf ( " Reading formula\n " ) ;
246+ cout << " Reading formula" << endl ;
308247 storebase = (Lit *)calloc (sizeof (Lit), 1024 );
309248
310249 for (i = 0 ; i < 2 * numvars; i++)
@@ -315,7 +254,7 @@ void WalkSAT::initprob()
315254 int lit;
316255 do {
317256 if (fscanf (cnfStream, " %i " , &lit) != 1 ) {
318- fprintf (stderr, " Bad input file\n " ) ;
257+ cout << " Bad input file" << endl ;
319258 exit (-1 );
320259 }
321260 if (lit != 0 ) {
@@ -337,37 +276,35 @@ void WalkSAT::initprob()
337276 } while (lit != 0 );
338277
339278 if (clsize[i] == 0 ) {
340- fprintf (stderr, " Bad input file\n " ) ;
279+ cout << " Bad input file" << endl ;
341280 exit (-1 );
342281 }
343282 longestclause = MAX (longestclause, clsize[i]);
344283 }
345284
346- printf ( " Creating data structures\n " ) ;
285+ cout << " Creating data structures" << endl ;
347286
348287 /* Have to wait to set the clause[i] ptrs to the end, since store might move */
349288 j = 0 ;
350289 for (i = 0 ; i < numclauses; i++) {
351290 clause[i] = &(storebase[j]);
352291 j += clsize[i];
353292 }
354- best = (int *) calloc (sizeof (int ), longestclause);
293+ best = (uint32_t *) calloc (sizeof (uint32_t ), longestclause);
355294
356295 /* Create the occurence lists for each literal */
357296
358297 /* First, allocate enough storage for occurrence lists */
359298 uint32_t * storebase2 = (uint32_t *)calloc (sizeof (uint32_t ), numliterals);
360299
361- /* printf( "numliterals = %d\n", numliterals); fflush(stdout); */
300+ /* cout << "numliterals = %d" << numliterals); fflush(stdout); */
362301
363302 /* Second, allocate occurence lists */
364303 i = 0 ;
365304 for (uint32_t i2 = 0 ; i2 < numvars*2 ; i2++) {
366305 const Lit lit = Lit::toLit (i2);
367- /* printf("lit = %d i = %d\n", lit, i); fflush(stdout); */
368-
369306 if (i > numliterals) {
370- fprintf (stderr, " Code error, allocating occurrence lists\n " ) ;
307+ cout << " Code error, allocating occurrence lists" << endl ;
371308 exit (-1 );
372309 }
373310 occurrence[lit.toInt ()] = &(storebase2[i]);
@@ -391,38 +328,38 @@ void WalkSAT::initprob()
391328
392329void WalkSAT::print_parameters ()
393330{
394- printf ( " WALKSAT v56\n " ) ;
395- printf ( " seed = %u \n " , seed) ;
396- printf ( " cutoff = % " BIGFORMAT " \n " , cutoff) ;
397- printf ( " tries = %i \n " , numrun);
398- printf ( " walk probabability = %5.3f \n " , walk_probability) ;
399- printf ( " \n " ) ;
331+ cout << " WALKSAT v56" << endl ;
332+ cout << " cutoff = %" << cutoff << endl ;
333+ cout << " tries = " << numrun << endl ;
334+ cout << " walk probabability = "
335+ << std::fixed << std::setprecision ( 2 ) << walk_probability << endl ;
336+ cout << endl ;
400337}
401338
402339void WalkSAT::initialize_statistics ()
403340{
404341 x = 0 ;
405342 r = 0 ;
406343 tail_start_flip = tail * numvars;
407- printf ( " tail starts after flip = %i \n " , tail_start_flip) ;
344+ cout << " tail starts after flip = " << tail_start_flip << endl ;
408345}
409346
410347void WalkSAT::print_statistics_header ()
411348{
412- printf ( " numvars = %i, numclauses = %i, numliterals = %i \n " , numvars, numclauses, numliterals);
413- printf ( " wff read in \n\n " ) ;
349+ cout << " numvars = " << numvars << " , numclauses = "
350+ << numclauses << " , numliterals = " << numliterals ;
414351
415- printf (
352+ cout << " wff read in\n " << endl;
353+ cout <<
416354 " lowbad unsat avg std dev sd/avg flips undo "
417- " length flips flips \n " ) ;
418- printf (
355+ " length flips" << endl ;
356+ cout <<
419357 " this end unsat avg ratio this flip success "
420- " success until std \n " ) ;
421- printf (
358+ " success until" << endl ;
359+ cout <<
422360 " try try tail unsat tail try fraction rate "
423- " tries assign dev\n\n " );
424-
425- fflush (stdout);
361+ " tries assign" << endl;
362+ cout << endl;
426363}
427364
428365void WalkSAT::update_statistics_start_try ()
@@ -497,17 +434,23 @@ void WalkSAT::update_and_print_statistics_end_try()
497434 // MSOOS: this has been removed, uses memory, only stats
498435 double undo_fraction = 0 ;
499436
500- printf (" %9i %9i %9.2f %9.2f %9.2f %9" BIGFORMAT " %9.6f %9i" , lowbad, numfalse, avgfalse,
501- std_dev_avgfalse, ratio_avgfalse, numflip, undo_fraction,
502- ((int )found_solution * 100 ) / numtry);
437+ cout
438+ << std::setw (9 ) << lowbad
439+ << std::setw (9 ) << numfalse
440+ << std::setw (9 +2 ) << avgfalse
441+ << std::setw (9 +2 ) << std_dev_avgfalse
442+ << std::setw (9 +2 ) << ratio_avgfalse
443+ << std::setw (9 ) << numflip
444+ << std::setw (9 ) << undo_fraction
445+ << std::setw (9 +2 ) << (((int )found_solution * 100 ) / numtry);
503446 if (found_solution) {
504- printf ( " %9 " BIGFORMAT, totalsuccessflip / (int )found_solution) ;
505- printf ( " %11.2f " , mean_x) ;
447+ cout << std::setw ( 9 + 2 ) << totalsuccessflip / (int )found_solution;
448+ cout << std::setw ( 9 + 2 ) << mean_x;
506449 }
507- printf ( " \n " ) ;
450+ cout << endl ;
508451
509452 if (numfalse == 0 && countunsat () != 0 ) {
510- fprintf (stderr, " Program error, verification of solution fails!\n " ) ;
453+ cout << " Program error, verification of solution fails!" << endl ;
511454 exit (-1 );
512455 }
513456
@@ -517,21 +460,21 @@ void WalkSAT::update_and_print_statistics_end_try()
517460void WalkSAT::print_statistics_final ()
518461{
519462 seconds_per_flip = expertime / totalflip;
520- printf ( " \n total elapsed seconds = %f \n " , expertime) ;
521- printf ( " num tries: %d \n " , numtry) ;
522- printf ( " average flips per second = %f \n " , ((double )totalflip) / expertime) ;
523- printf ( " number solutions found = %i \n " , found_solution) ;
524- printf ( " final success rate = %f \n " , ((double )found_solution * 100.0 ) / numtry) ;
525- printf ( " average length successful tries = %" BIGFORMAT " \n " ,
526- found_solution ? (totalsuccessflip / found_solution) : 0 );
463+ cout << " \n total elapsed seconds = " << expertime << endl ;
464+ cout << " num tries: " << numtry << endl ;
465+ cout << " average flips per second = " << ((double )totalflip) / expertime << endl ;
466+ cout << " number solutions found = " << found_solution << endl ;
467+ cout << " final success rate = " << ((double )found_solution * 100.0 ) / numtry << endl ;
468+ cout << " average length successful tries = %" <<
469+ ( found_solution ? (totalsuccessflip / found_solution) : 0 ) << endl ;
527470 if (found_solution) {
528- printf ( " average flips per assign (over all runs) = %f \n " ,
529- ((double )totalflip) / found_solution) ;
530- printf ( " average seconds per assign (over all runs) = %f \n " ,
531- (((double )totalflip) / found_solution) * seconds_per_flip) ;
532- printf ( " mean flips until assign = %f \n " , mean_x) ;
533- printf ( " mean seconds until assign = %f \n " , mean_x * seconds_per_flip) ;
534- printf ( " mean restarts until assign = %f \n " , mean_r) ;
471+ cout << " average flips per assign (over all runs) = " <<
472+ ((double )totalflip) / found_solution << endl ;
473+ cout << " average seconds per assign (over all runs) = " <<
474+ (((double )totalflip) / found_solution) * seconds_per_flip << endl ;
475+ cout << " mean flips until assign = " << mean_x << endl ;
476+ cout << " mean seconds until assign = " << mean_x * seconds_per_flip << endl ;
477+ cout << " mean restarts until assign = " << mean_r << endl ;
535478 }
536479
537480 if (number_sampled_runs) {
@@ -559,35 +502,38 @@ void WalkSAT::print_statistics_final()
559502 nonsuc_ratio_mean_avgfalse = 0 ;
560503 }
561504
562- printf ( " final numbad level statistics\n " ) ;
563- printf ( " statistics over all runs:\n " ) ;
564- printf ( " overall mean average numbad = %f \n " , mean_avgfalse) ;
565- printf ( " overall mean meanbad std deviation = %f \n " , mean_std_dev_avgfalse) ;
566- printf ( " overall ratio mean numbad to mean std dev = %f \n " , ratio_mean_avgfalse) ;
567- printf ( " statistics on successful runs:\n " ) ;
568- printf ( " successful mean average numbad = %f \n " , suc_mean_avgfalse) ;
569- printf ( " successful mean numbad std deviation = %f \n " , suc_mean_std_dev_avgfalse) ;
570- printf ( " successful ratio mean numbad to mean std dev = %f \n " ,
571- suc_ratio_mean_avgfalse) ;
572- printf ( " statistics on nonsuccessful runs:\n " ) ;
573- printf ( " nonsuccessful mean average numbad level = %f \n " , nonsuc_mean_avgfalse) ;
574- printf ( " nonsuccessful mean numbad std deviation = %f \n " ,
575- nonsuc_mean_std_dev_avgfalse) ;
576- printf ( " nonsuccessful ratio mean numbad to mean std dev = %f \n " ,
577- nonsuc_ratio_mean_avgfalse) ;
505+ cout << " final numbad level statistics" << endl ;
506+ cout << " statistics over all runs:" << endl ;
507+ cout << " overall mean average numbad = " << mean_avgfalse << endl ;
508+ cout << " overall mean meanbad std deviation = " << mean_std_dev_avgfalse << endl ;
509+ cout << " overall ratio mean numbad to mean std dev = " << ratio_mean_avgfalse << endl ;
510+ cout << " statistics on successful runs:" << endl ;
511+ cout << " successful mean average numbad = " << suc_mean_avgfalse << endl ;
512+ cout << " successful mean numbad std deviation = " << suc_mean_std_dev_avgfalse << endl ;
513+ cout << " successful ratio mean numbad to mean std dev = " <<
514+ suc_ratio_mean_avgfalse << endl ;
515+ cout << " statistics on nonsuccessful runs:" << endl ;
516+ cout << " nonsuccessful mean average numbad level = " << nonsuc_mean_avgfalse << endl ;
517+ cout << " nonsuccessful mean numbad std deviation = " <<
518+ nonsuc_mean_std_dev_avgfalse << endl ;
519+ cout << " nonsuccessful ratio mean numbad to mean std dev = " <<
520+ nonsuc_ratio_mean_avgfalse << endl ;
578521 }
579522
580523 if (found_solution) {
581- printf ( " ASSIGNMENT FOUND\n " ) ;
524+ cout << " ASSIGNMENT FOUND" << endl ;
582525 print_sol_cnf ();
583526 } else
584- printf ( " ASSIGNMENT NOT FOUND\n " ) ;
527+ cout << " ASSIGNMENT NOT FOUND" << endl ;
585528}
586529
587530void WalkSAT::print_sol_cnf ()
588531{
589- for (uint32_t i = 0 ; i < numvars; i++)
590- printf (" v %i\n " , assigns[i] == l_True? ((int )i+1 ) : -1 *((int )i+1 ));
532+ cout << " v " ;
533+ for (uint32_t i = 0 ; i < numvars; i++) {
534+ cout << (assigns[i] == l_True? ((int )i+1 ) : -1 *((int )i+1 )) << " " ;
535+ }
536+ cout << " 0" << endl;
591537}
592538
593539/* ******************************************************/
@@ -641,5 +587,5 @@ uint32_t WalkSAT::pickbest()
641587 if ((bestvalue > 0 ) && (RANDMOD (denominator) < numerator))
642588 return clause[tofix][RANDMOD (clausesize)].var ();
643589
644- return ABS ( best[RANDMOD (numbest)]) ;
590+ return best[RANDMOD (numbest)];
645591}
0 commit comments