BuDDy是一个BDD包
下载地址:http://vlsicad.eecs.umich.edu/BK/Slots/cache/www.itu.dk/research/buddy/index.html
BuDDy22的安装过程:
make
sudo make install
注意:需要安装g++
一个能将CNF(DIMACS格式)转化为BDD的小程序如下:
#include <bdd.h> #include <iostream> #include <fstream> #include <vector> using namespace std; #define MAX_VAR 10000 #define MAX_LINE 1024 struct Clause { int *lits; int len; Clause( int * literals, int length ) { lits = new int [length]; len = length; for ( int i = 0; i < length; i++ ) lits[i] = literals[i]; } }; void CNF_Display( ostream & fout, int nv, vector<Clause> & cnf ) { fout << "p cnf " << nv << ' ' << cnf.size() << endl; for ( vector<Clause>::iterator itr = cnf.begin(); itr < cnf.end(); itr++ ) { for ( int i = 0; i < itr->len; i++ ) fout << itr->lits[i] << ' '; fout << '0' << endl; } } // argv[1] is the name of CNF file, argv[2] is the name of BDD file. int main( int argc, char *argv[] ) { if ( argc != 3 ) { cerr << "ERROR: argc == " << argc << '!' << endl; return 0; } ifstream fin(argv[1]); if ( !fin ) { cerr << "ERROR: no input file!" << argc << endl; return 0; } char line[MAX_LINE]; do { if (fin.eof()) { cerr << "ERROR: empty file!" << endl; return 0; } fin.getline( line, MAX_LINE ); } while (line[0] == '#'); int nv; long nc; if ( sscanf( line, "p cnf %d %ld", &nv, &nc ) != 2 ) { cerr << "ERROR: wrong head!" << endl; return 0; } if ( nv <= 0 || nv > MAX_VAR) { cerr << "ERROR: nv are not in (0, " << MAX_VAR <<"]!" << endl; return 0; } if ( nv < 0 ) { cerr << "ERROR: the number of clauses must be not negative!" << endl; return 0; } int lits[MAX_VAR]; int len; vector<Clause> cnf; for( long i = 0; i < nc; i++ ) { do { if (fin.eof()) { cerr << "ERROR: there are only " << i << " clauses!" << endl; return 0; } fin.getline( line, MAX_LINE ); } while (line[0] == '#'); len = 0; char * p = line; while ( *p == ' ' || *p == '/t' ) p++; while ( 1 ) { int tmp; if ( sscanf( p, "%d", &tmp) != 1 ) { cerr << "ERROR: wrong literal format!" << endl; return 0; } if ( tmp > nv || tmp < -nv ) { cerr << "ERROR: literal are not in [" << -nv << ", " << nv << "]" << endl; return 0; } if ( tmp == 0 ) break; lits[len++] = tmp; if( *p == '-' ) p++; while ( *p >= '0' && *p <= '9' ) p++; while ( *p == ' ' || *p == '/t' ) p++; if ( *p == '/0' ) { cerr << "ERROR: no end of clause!" << endl; return 0; } } cnf.push_back( Clause( lits, len ) ); } fin.close(); CNF_Display(cout, nv, cnf); bdd result; bdd_init(1000, 100); bdd_setvarnum(nv + 1); vector<Clause>::iterator itr = cnf.begin(), end = cnf.end(); if ( itr->lits[0] > 0 ) result = bdd_ithvar( itr->lits[0] ); else result = bdd_nithvar( -itr->lits[0] ); for ( int j = 1; j < itr->len; j++ ) { if ( itr->lits[j] > 0 ) result |= bdd_ithvar( itr->lits[j] ); else result = result | bdd_nithvar( -itr->lits[j] ); } cout << bddtable << result << endl; for ( itr++; itr < cnf.end(); itr++ ) { bdd x; if ( itr->lits[0] > 0 ) x = bdd_ithvar( itr->lits[0] ); else x = bdd_nithvar( -itr->lits[0] ); for ( int j = 1; j < itr->len; j++ ) { if ( itr->lits[j] > 0 ) x = x | bdd_ithvar( itr->lits[j] ); else x = x | bdd_nithvar( -itr->lits[j] ); } result &= x; cout << bddtable << result << endl; } ofstream fout( argv[2] ); fout << bddtable << result << endl; cout << bddtable << result << endl; bdd_done(); return 1; }