- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Solvers/SAT/GATSAT/OpSAT/gen.cpp

    00001 //==============================================================================
    00002 // Copyright (c) 2001           Model-based Embedded and Robotic Systems Group
    00003 // All Rights Reserved          Massachusetts Institute of Technology
    00004 //
    00005 // This software is provided as is. Model-based Embedded and Robotic Systems
    00006 // (MERS) group does not assume any responsibility.
    00007 //==============================================================================
    00008 //# CHECKED MJP 08-07-2002
    00009
    00010 #ifdef _MSC_VER
    00011 #pragma warning( disable : 4786 )
    00012 #endif
    00013
    00014 #include <cstdlib>
    00015 #include <cstdio>
    00016 #include <cassert>
    00017 #include <iostream>
    00018 #include <fstream>
    00019 #include <string>
    00020 #include <cmath>
    00021 #include <ctime>
    00022 #include <list>
    00023 #include <vector>
    00024 //#include <sstream>
    00025
    00026 using namespace std;
    00027
    00028
    00029 class
    00030 DomainElement
    00031 {
    00032 public:
    00033         string value;
    00034         double probability;
    00035
    00036         DomainElement(string v, double p)
    00037         : value(v), probability(p)
    00038         {
    00039         }
    00040         DomainElement(DomainElement const & de)
    00041         : value(de.value), probability(de.probability)
    00042         {
    00043         }
    00044 };
    00045
    00046
    00047 class
    00048 ConstraintElement
    00049 {
    00050 public:
    00051         string variable;
    00052         string value;
    00053         bool sign;
    00054
    00055         ConstraintElement(string var, string val, bool s)
    00056         : variable(var), value(val), sign(s)
    00057         {
    00058         }
    00059         ConstraintElement(ConstraintElement const & ce)
    00060         : variable(ce.variable), value(ce.value), sign(ce.sign)
    00061         {
    00062         }
    00063 };
    00064
    00065
    00066 bool                        file_exist(char* filename);
    00067 list<unsigned int>          createCosts(unsigned int len, unsigned int maxCost);
    00068 vector<string>                createNames(unsigned int len, bool upperCase = true);
    00069 vector< DomainElement >       createDomain(unsigned int len);
    00070 vector< DomainElement >       createDomainRandom(unsigned int maxLen);
    00071 void                        printVariable(string name, vector< DomainElement > domain,
    00072                                           ostream & os = cout);
    00073 double                      randDouble();
    00074 int                         randRange(int low, int high);
    00075 int                         randRange(int high);
    00076 void                        printConstraintElement(ConstraintElement ce,
    00077                                                    ostream & os = cout);
    00078 void                        printConstraint(list< ConstraintElement > c,
    00079                                             ostream & os = cout);
    00080 list<ConstraintElement>     createConstraint(unsigned int maxLen);
    00081 ConstraintElement           createConstraintElement();
    00082 list< list<ConstraintElement> >   createMEDEConstraints();
    00083
    00084
    00085 vector<string>  varNames;
    00086 vector< vector<DomainElement> > varDomains;
    00087
    00088 double
    00089 randDouble()
    00090 {
    00091         int n = rand();
    00092         if (n)
    00093                 n--;
    00094         double res = (((double)n) / RAND_MAX);
    00095         assert(res >= 0.0);
    00096         assert(res < 1.0);
    00097         return res;
    00098 }
    00099
    00100 int
    00101 randRange(int low, int high)
    00102 {
    00103         int res = low + (int)( randDouble() * (high - low + 1) );
    00104         assert(res >= low);
    00105         assert(res <= high);
    00106         return res;
    00107 }
    00108
    00109 int
    00110 randRange(int high)
    00111 {
    00112         return randRange(0, high);
    00113 }
    00114
    00115
    00116 bool
    00117 file_exist(char const * filename)
    00118 {
    00119         FILE *file;
    00120         if ( (file = fopen(filename,"r")) == NULL )
    00121                 return false;
    00122         fclose(file);
    00123         return true;
    00124 }
    00125
    00126
    00127 list<double>
    00128 createProbabilities(unsigned int len)
    00129 {
    00130         int * weight = new int[len];
    00131         double totalWeight = 0;
    00132         for (unsigned int i = 0; i < len; i++)
    00133         {
    00134                 weight[i] = rand();
    00135                 totalWeight += weight[i];
    00136                 //cout << "   total: " << totalWeight << endl;
    00137         }
    00138         list<double> res;
    00139         //double check = 0;
    00140         for (unsigned int i2 = 0; i2 < len; i2++)
    00141         {
    00142                 //cout << "weight: " << weight[i2] << "; total: " << totalWeight << endl;
    00143                 res.push_back( ((double)weight[i2]) / totalWeight );
    00144                 //check += ((double)weight[i2]) / totalWeight;
    00145         }
    00146         //cout << "   check: " << check << endl;
    00147         delete[] weight;
    00148         return res;
    00149 }
    00150
    00151
    00152 list<unsigned int>
    00153 createCosts(unsigned int len, unsigned int maxCost)
    00154 {
    00155         list<unsigned int> res;
    00156         for (unsigned int i = 0; i < len; i++)
    00157         {
    00158                 res.push_back( (unsigned int)randRange(0, maxCost) );
    00159         }
    00160         return res;
    00161 }
    00162
    00163
    00164 vector<string>
    00165 createNames(unsigned int len, bool upperCase)
    00166 {
    00167         char name[3];
    00168         char base = upperCase ? 'A' : 'a';
    00169         name[1] = '\0';
    00170         vector<string> res;
    00171         for (unsigned int i = 0; i < len; i++)
    00172         {
    00173                 name[0] = base + i;
    00174                 string nameStr(name);
    00175                 res.push_back(nameStr);
    00176         }
    00177         return res;
    00178 }
    00179
    00180
    00181 vector< DomainElement >
    00182 createDomain(unsigned int len)
    00183 {
    00184         vector< DomainElement > res;
    00185         vector<string> vals  = createNames(len, false);
    00186         list<double> probs = createProbabilities(len);
    00187
    00188         vector<string>::const_iterator vIter;
    00189         list<double>::const_iterator pIter;
    00190         for (vIter = vals.begin(), pIter = probs.begin(); vIter != vals.end(); vIter++, pIter++)
    00191         {
    00192                 string v = *vIter;
    00193                 double p = *pIter;
    00194                 res.push_back(DomainElement(v,p));
    00195         }
    00196         return res;
    00197 }
    00198
    00199
    00200 vector< DomainElement >
    00201 createDomainRandom(unsigned int maxLen)
    00202 {
    00203         unsigned int domainSize = (unsigned int)randRange(2, maxLen);
    00204         return createDomain(domainSize);
    00205 }
    00206
    00207
    00208 list<ConstraintElement>
    00209 createConstraint(unsigned int maxLen)
    00210 {
    00211         unsigned int len = (unsigned int)randRange(2, maxLen);
    00212         list<ConstraintElement> res;
    00213         for (unsigned int i = 0; i < len; i++)
    00214         {
    00215                 res.push_back(createConstraintElement());
    00216         }
    00217         return res;
    00218 }
    00219
    00220
    00221 ConstraintElement
    00222 createConstraintElement()
    00223 {
    00224         bool sign = (randDouble() < 0.5);
    00225         unsigned int varIndex = (unsigned int)randRange(0, varNames.size() - 1);
    00226         vector<DomainElement> domain = varDomains[varIndex];
    00227         unsigned int valIndex = (unsigned int)randRange(0, domain.size() - 1);
    00228         string var = varNames[varIndex];
    00229         string val = domain[valIndex].value;
    00230         return ConstraintElement(var, val, sign);
    00231 }
    00232
    00233
    00234 void
    00235 printVariable(string name, vector< DomainElement > domain, ostream & os)
    00236 {
    00237         os << name << "  ";
    00238         vector<DomainElement>::const_iterator dIter;
    00239         for (dIter = domain.begin(); dIter != domain.end(); dIter++)
    00240         {
    00241                 os << (*dIter).value << " " << (*dIter).probability << " " ;
    00242         }
    00243         os << endl;
    00244 }
    00245
    00246
    00247 void
    00248 printConstraintElement(ConstraintElement ce, ostream & os)
    00249 {
    00250         if (!ce.sign)
    00251                 os << "!";
    00252         os << "[" << ce.variable << "=" << ce.value << "]";
    00253 }
    00254
    00255
    00256 void
    00257 printConstraint(list< ConstraintElement > c, ostream & os)
    00258 {
    00259         list<ConstraintElement>::const_iterator cIter;
    00260         for (cIter = c.begin(); cIter != c.end(); cIter++)
    00261         {
    00262                 printConstraintElement((*cIter), os);
    00263                 os << " " ;
    00264         }
    00265         os << endl;
    00266 }
    00267
    00268
    00269 list< list<ConstraintElement> >
    00270 createMEDEConstraints()
    00271 {
    00272 /*#
    00273         //unsigned int varIndex = (unsigned int)randRange(0, varNames.size() - 1);
    00274         //vector<DomainElement> domain = varDomains[varIndex];
    00275         //unsigned int valIndex = (unsigned int)randRange(0, domain.size() - 1);
    00276         //string var = varNames[varIndex];
    00277         //string val = domain[valIndex].value;
    00278         //ConstraintElement res = {var, val, sign};
    00279 */
    00280         list< list<ConstraintElement> >  res;
    00281         for (unsigned int i = 0; i < varNames.size(); i++)
    00282         {
    00283                 string var = varNames[i];
    00284                 vector<DomainElement> domain = varDomains[i];
    00285
    00286                 // domain exhaustion:
    00287                 list<ConstraintElement> con;
    00288                 for (unsigned int d = 0; d < domain.size(); d++)
    00289                 {
    00290                         ConstraintElement ce(var, domain[d].value, true);
    00291                         con.push_back(ce);
    00292                 }
    00293                 res.push_back(con);
    00294
    00295                 // mutual exclusion:
    00296                 for (unsigned int a1 = 0; a1 < domain.size(); a1++)
    00297                 {
    00298                         for (unsigned int a2 = a1+1; a2 < domain.size(); a2++)
    00299                         {
    00300                                 list<ConstraintElement> con2;
    00301                                 ConstraintElement ce1(var, domain[a1].value, false);
    00302                                 con2.push_back(ce1);
    00303                                 ConstraintElement ce2(var, domain[a2].value, false);
    00304                                 con2.push_back(ce2);
    00305                                 res.push_back(con2);
    00306                         }
    00307                 }
    00308         }
    00309         return res;
    00310 }
    00311
    00312
    00313
    00314 int
    00315 main(int argc, char** argv)
    00316 {
    00317         argc--;
    00318         argv++;
    00319
    00320         srand((unsigned int)time(NULL));
    00321
    00322         bool useProb = true;
    00323         unsigned int maxCost;
    00324
    00325         for (; argc > 0 && argv[argc-1][0] == '-'; argc--)
    00326         {
    00327                 if (argv[argc-1][1] == 'c' || argv[argc-1][1] == 'C')
    00328                 {
    00329                         cerr << "Using cost values instead of probabilities." << endl;
    00330                         useProb = false;
    00331                         if (argv[argc-1][2] == '\0')
    00332                         {
    00333                                 maxCost = 9;
    00334                         } else
    00335                         {
    00336                                 char * numStr = argv[argc-1]+2;
    00337                                 maxCost = (unsigned int)atoi(numStr);
    00338                         }
    00339                         cerr << "Values range from 0 to " << maxCost << "." << endl;
    00340                 } else
    00341                 if (argv[argc-1][1] == 'c' || argv[argc-1][1] == 'C')
    00342                 {
    00343                         cerr << "Using cost values instead of probabilities." << endl;
    00344                         useProb = false;
    00345                         if (argv[argc-1][2] == '\0')
    00346                         {
    00347                                 maxCost = 9;
    00348                         } else
    00349                         {
    00350                                 char * numStr = argv[argc-1]+2;
    00351                                 maxCost = (unsigned int)atoi(numStr);
    00352                         }
    00353                         cerr << "Values range from 0 to " << maxCost << "." << endl;
    00354                 } else
    00355                 {
    00356                         cerr << "Ignoring unrecognized option '" << argv[argc-1] << "'." << endl;
    00357                 }
    00358         }
    00359         if (argc != 3 && argc != 6 && argc != 5 && argc != 8)
    00360         {
    00361                 cout << "Arguments should be:" << endl << endl;
    00362                 cout << "   <num of variables> <max domain size> <filename> "
    00363                      << "[<num state vars> <filename>] "
    00364                      << "[<num constraints> <max constraint size> <filename>] "
    00365                      << "[-c[<max cost>]]" << endl;
    00366                 cout << endl;
    00367                 cout << "-c   uses costs instead of probabilities." << endl;
    00368                 cout << endl;
    00369                 exit(-1);
    00370         }
    00371         //cout << endl;
    00372
    00373         unsigned int numVars = (unsigned int)atoi(argv[0]);
    00374         unsigned int maxDomain = (unsigned int)atoi(argv[1]);
    00375         char const * varFilename = argv[2];
    00376         char const * constraintFilename = NULL;
    00377         char const * stateFilename = NULL;
    00378
    00379         unsigned int numState = 0;
    00380         if (argc == 5 || argc == 8)
    00381         {
    00382                 numState = (unsigned int)atoi(argv[3]);
    00383                 stateFilename = argv[4];
    00384         }
    00385
    00386         unsigned int numConstraints = 0;
    00387         unsigned int maxConstraint = 0;
    00388         if (argc == 6 || argc == 8)
    00389         {
    00390                 numConstraints = (unsigned int)atoi(argv[argc-3]);
    00391                 maxConstraint  = (unsigned int)atoi(argv[argc-2]);
    00392                 constraintFilename = argv[argc-1];
    00393         }
    00394
    00395         (void)useProb;
    00396         //cout << "Number of variables: " << numVars << ";  Maximum domain size: " << maxDomain
    00397         //     << endl << endl;
    00398
    00399         ofstream * out;
    00400         ofstream outdes(varFilename);
    00401         out = &outdes;
    00402         varNames = createNames(numVars, true);
    00403         unsigned int count = 0;
    00404         for (vector<string>::const_iterator vIter = varNames.begin(); vIter != varNames.end();
    00405              vIter++)
    00406         {
    00407                 if (stateFilename && (count == numVars - numState))  // switch from design file
    00408                 {
    00409                         out->close();
    00410                         out = new ofstream(stateFilename);
    00411                 }
    00412                 vector<DomainElement> domain = createDomainRandom(maxDomain);
    00413                 varDomains.push_back(domain);
    00414                 printVariable((*vIter), domain, *out);
    00415                 count++;
    00416         }
    00417         out->close();
    00418
    00419         //cout << endl;
    00420
    00421         if (numConstraints > 0)
    00422         {
    00423                 ofstream outc(constraintFilename);
    00424                 ofstream outc2((string(constraintFilename) + "2").c_str());
    00425                 list< list<ConstraintElement> > mede = createMEDEConstraints();
    00426                 list< list<ConstraintElement> >::const_iterator medeIter;
    00427                 for (medeIter = mede.begin(); medeIter != mede.end(); medeIter++)
    00428                 {
    00429                         printConstraint((*medeIter), outc);
    00430                 }
    00431                 outc << endl;
    00432                 for (unsigned int i = 0; i < numConstraints; i++)
    00433                 {
    00434                         list<ConstraintElement> con = createConstraint(maxConstraint);
    00435                         printConstraint(con, outc);
    00436                         printConstraint(con, outc2);
    00437                 }
    00438                 outc.close();
    00439                 outc2.close();
    00440         }
    00441
    00442         exit(0);
    00443         return 0; //# to make MSVC happy - yes, it's that stupid.
    00444 }
    00445