# include # include # include using namespace std; # include "satisfy_brute.hpp" //****************************************************************************80 void i4_to_bvec ( int i4, int n, int bvec[] ) //****************************************************************************80 // // Purpose: // // i4_to_bvec() converts an integer into a binary vector. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 20 March 2009 // // Author: // // John Burkardt // // Input: // // int I4, the integer. // // int N, the dimension of the vector. // // Output: // // int BVEC[N], the vector of binary remainders. // { int i; for ( i = n - 1; 0 <= i; i-- ) { bvec[i] = i4 % 2; i4 = i4 / 2; } return; } //****************************************************************************80 void satisfy_brute ( int n, int formula ( int bvec[] ) ) //****************************************************************************80 // // Purpose: // // satisfy_brute() solves the satisfy problem using brute force. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 27 October 2022 // // Author: // // John Burkardt // // Reference: // // Michael Quinn, // Parallel Programming in C with MPI and OpenMP, // McGraw-Hill, 2004, // ISBN13: 978-0071232654, // LC: QA76.73.C15.Q55. // // Input: // // int n: the number of variables in the formula. // // int formula ( int bvec[] ): a function which evaluates the formula. // { int *bvec; int i; int ihi; int j; int solution_num; int value; bvec = new int[n]; cout << "\n"; cout << "satisfy_brute():\n"; cout << " We have a logical function of N logical arguments.\n"; cout << " We do an exhaustive search of all 2^N possibilities,\n"; cout << " seeking those inputs that make the function TRUE.\n"; // // Compute the number of binary vectors to check. // ihi = 1; for ( i = 1; i <= n; i++ ) { ihi = ihi * 2; } cout << "\n"; cout << " The number of logical variables is N = " << n << "\n";; cout << " The number of input vectors to check is " << ihi << "\n"; cout << "\n"; cout << " # Index ---------Input Values------------------------\n"; cout << "\n"; // // Check every possible input vector. // solution_num = 0; for ( i = 0; i < ihi; i++ ) { i4_to_bvec ( i, n, bvec ); value = formula ( bvec ); if ( value == 1 ) { solution_num = solution_num + 1; cout << " " << setw(2) << solution_num << " " << setw(10) << i << " "; for ( j = 0; j < n; j++ ) { cout << " " << setw(1) << bvec[j]; } cout << "\n"; } } // // Report. // cout << "\n"; cout << " Number of solutions found was " << solution_num << "\n"; delete [] bvec; return; }