fss-satgen.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Kamil Dudka <xdudka00@stud.fit.vutbr.cz>
00003  *
00004  * This file is part of fss (Fast SAT Solver).
00005  *
00006  * fss is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * any later version.
00010  *
00011  * fss is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with fss.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <sys/types.h>
00021 #include <unistd.h>
00022 #include <sys/time.h>
00023 #include <time.h>
00024 #include <stdlib.h>
00025 #include <iostream>
00026 #include <vector>
00027 #include <string>
00028 #include <sstream>
00029 
00030 using namespace std;
00031 
00032 class RandGenerator {
00033   public:
00034     // Initialize random generator seed
00035     RandGenerator() {
00036       // Get system clock
00037       struct timeval tv;
00038       if (0 != gettimeofday(&tv, NULL))
00039         // Failed to read system time
00040         throw std::bad_alloc();
00041 
00042       // Convert to microseconds
00043       int clk = tv.tv_usec;
00044 
00045       // Initialize seed
00046       this->seed = clk * getpid();
00047 #if 0//ndef NDEBUG
00048       std::cerr << "RandGenerator::RandGenerator(): seed=" << seed << std::endl;
00049 #endif // NDEBUG
00050     }
00051     // Return random number in interval <0,1)
00052     float getRand() {
00053       float rnd = rand_r(&seed);
00054       return rnd/RAND_MAX;
00055     }
00056     // Return random float in interval <min,max)
00057     float getRand(float min, float max) {
00058       float rnd = this->getRand();
00059       rnd *= max-min;
00060       rnd += min;
00061       return rnd;
00062     }
00063     // Return random int in interval <min,max)
00064     int getRand(int min, int max) {
00065       float rnd = this->getRand(
00066           static_cast<float>(min),
00067           static_cast<float>(max)
00068           );
00069       return static_cast<int>(rnd);
00070     }
00071   private:
00072     unsigned int seed;
00073 };
00074 
00079 int main(int argc, char *argv[]) {
00080   if (argc<3) {
00081     std::cerr << "Usage: fss-satgen VARCOUNT FORMSCOUNT" << std::endl;
00082     return -1;
00083   }
00084 
00085   const int VARCOUNT = atoi(argv[1]);
00086   const int FORMSCOUNT = atoi(argv[2]);
00087   if (0==VARCOUNT || 0==FORMSCOUNT) {
00088     std::cerr << "Usage: fss-satgen VARCOUNT FORMSCOUNT" << std::endl;
00089     return -1;
00090   }
00091 
00092   // Random numbers generator
00093   RandGenerator rnd;
00094 
00095   // Variable names
00096   vector<string> varNames(VARCOUNT);
00097   for(int i=0; i<VARCOUNT; i++) {
00098     ostringstream stream;
00099     stream << "a" << i;
00100     varNames[i] = stream.str();
00101   }
00102 
00103   // Binary operators
00104   vector<string> binopSet;
00105   binopSet.push_back("AND");
00106   binopSet.push_back("OR");
00107   binopSet.push_back("XOR");
00108 
00109   // FA control state
00110   enum EState {
00111     S_EXPR,
00112     S_BINOP
00113   } state = S_EXPR;
00114 
00115   // count of opened parenthesis
00116   int lParCount = 0;
00117 
00118   // Generator main loop
00119   for (int fCount=0; fCount<FORMSCOUNT;) {
00120     switch (state) {
00121       case S_EXPR:
00122         // Unary NOT
00123         if (rnd.getRand() < 0.3) {
00124           std::cout << "~" << std::flush;
00125           break;
00126         }
00127         // Open a pair of brackets
00128         if (rnd.getRand(0.1f*lParCount, 1.0f) < 0.5) {
00129           std::cout << "(" << std::flush;
00130           lParCount++;
00131           break;
00132         }
00133         // Random variable
00134         std::cout << varNames[rnd.getRand(0, VARCOUNT)] << std::flush;
00135         state = S_BINOP;
00136         break;
00137 
00138       case S_BINOP:
00139         if (rnd.getRand() < 0.1) {
00140           // Close formula
00141           for(; lParCount; lParCount--)
00142             std::cout << ")";
00143           std::cout << ";" << std::endl;
00144           fCount++;
00145           state = S_EXPR;
00146           break;
00147         }
00148         if (rnd.getRand() < 0.05*lParCount) {
00149           // Close a pair of brackets
00150           std::cout << ")" << std::flush;
00151           lParCount--;
00152           break;
00153         }
00154         // Random binary operator
00155         std::cout << " " << binopSet[rnd.getRand(0, binopSet.size())] << " " << std::flush;
00156         state = S_EXPR;
00157         break;
00158     }
00159   }
00160   // new line at end of file generated
00161   std::cout << std::endl;
00162 }

Generated on Wed Nov 5 22:30:21 2008 for Fast SAT Solver by  doxygen 1.5.4