variableset.cpp
Go to the documentation of this file.
00001 
00007 #include <stdlib.h>
00008 #include <vector>
00009 #include <iostream>
00010 #include <string>
00011 #include <fstream>
00012 
00013 #include "variableset.h"
00014 
00015 using namespace cnf;
00016 
00017 
00018 VariableSet::VariableSet(unsigned int i, unsigned int j)
00019 {
00020         addVars(i,j);
00021 }
00022 
00023 
00024 VariableSet::VariableSet(unsigned int i, unsigned int j, unsigned int k)
00025 {
00026         addVars(i,j,k);
00027 }
00028 
00029 
00030 void VariableSet::addVars(unsigned int i, unsigned int j)
00031 {
00032         std::vector<unsigned int> itsDimensions;
00033         itsDimensions.push_back(i);
00034         itsDimensions.push_back(j);
00035         dim.push_back(itsDimensions);
00036         sizes.push_back(i*j);
00037         std::vector<bool> newVarValue(i*j,false);
00038         varValue.insert(varValue.end(), newVarValue.begin(),
00039                         newVarValue.end());
00040 }
00041 
00042 
00043 void VariableSet::addVars(unsigned int i, unsigned int j, unsigned int k)
00044 {
00045         std::vector<unsigned int> itsDimensions;
00046         itsDimensions.push_back(i);
00047         itsDimensions.push_back(j);
00048         itsDimensions.push_back(k);
00049         dim.push_back(itsDimensions);
00050         sizes.push_back(i*j*k);
00051         std::vector<bool> newVarValue(i*j*k,false);
00052         varValue.insert(varValue.end(), newVarValue.begin(),
00053                         newVarValue.end());
00054 }
00055 
00056 
00057 unsigned int VariableSet::getVarCode(unsigned int s, unsigned int i, unsigned int j)
00058 {
00059         if (s > sizes.size())
00060         {
00061                 std::cout<<"error in VariableSet: wrong subset index"<<" ("
00062                          <<s<<">="<<sizes.size()<<")"<< std::endl;
00063                 exit(100);
00064         }
00065         else if( (i>=dim[s][0]) ||
00066                  (j>=dim[s][1]) )
00067         {
00068                 std::cout<<"error in VariableSet: indices out of bound!"<<" ("
00069                          <<i<<">="<<dim[s][0]<<" or"
00070                          <<j<<">="<<dim[s][1]<<")"
00071                          << std::endl;
00072                 exit(100);
00073         }
00074         else
00075         {
00076                 unsigned int varCode=0;
00077                 for (unsigned int subset=0; subset<s; subset++)
00078                         varCode += sizes[subset];
00079                 varCode += i + (dim[s][0])*j;
00080                 return varCode +1;
00081         }
00082 }
00083 
00084 unsigned int VariableSet::getVarCode(unsigned int s, unsigned int i, unsigned int j, unsigned int k)
00085 {
00086         if (s > sizes.size())
00087         {
00088                 std::cout<<"error in VariableSet: wrong subset index"<<" ("
00089                          <<s<<">="<<sizes.size()<<")"<< std::endl;
00090                 exit(100);
00091         }
00092         else if ( (i>=dim[s][0]) || 
00093              (j>=dim[s][1]) ||
00094              (k>=dim[s][2]) )
00095         {
00096                 std::cout<<"error in VariableSet: indices out of bound!"<<" ("
00097                          <<i<<">="<<dim[s][0]<<" or"
00098                          <<j<<">="<<dim[s][1]<<" or"
00099                          <<k<<">="<<dim[s][2]<<")"<< std::endl;
00100                 exit(100);
00101         }
00102         else
00103         {
00104                 unsigned int varCode=0;
00105                 for (unsigned int subset=0; subset<s; subset++)
00106                         varCode += sizes[subset];
00107                 varCode += i + (dim[s][0])*(j + dim[s][1]*k);
00108 
00109 //                std::cout<<"("<<i<<","<<j<<","<<k<<" => "<<varCode+1<<")@t";
00110                 return varCode+1;
00111         }
00112 }
00113 
00114 
00115 bool VariableSet::getVarValue(unsigned int s, unsigned int i, unsigned int j)
00116 {
00117         return varValue[getVarCode(s,i,j) -1];
00118 }
00119 
00120 
00121 bool VariableSet::getVarValue(unsigned int s, unsigned int i, unsigned int j, unsigned int k)
00122 {
00123         return varValue[getVarCode(s,i,j,k) -1];
00124 }
00125 
00126 
00127 void VariableSet::parseDimacs(std::istream * input)
00128 {
00129         if (!(*input))
00130         {
00131                 std::cout<<"error 100: No such file!"<<std::endl;
00132                 exit(100);
00133         }
00134         else
00135         {
00136                 std::string res;
00137                 (*input) >> res;
00138                 if (res.compare("SAT") != 0)
00139                 {
00140                         std::cout<<"error 101: The file does not correspond "
00141                                  <<"to a satisfiable formula."<<std::endl;
00142                         exit(101);
00143                 }
00144                 else
00145                 {
00146                         int i=1;
00147                         while ( ((*input) >> i) && (i!=0) )
00148                                 if (i>0)
00149                                         varValue[i-1] = true;
00150                                 else
00151                                         varValue[-i+1] = false;
00152                 }
00153         }
00154 }
00155 
00156 
00157 unsigned int VariableSet::getCard()
00158 {
00159         unsigned int card=0;
00160         for (unsigned int s=0; s<sizes.size(); s++)
00161                 card += sizes[s];
00162         return card;
00163 }
00164 
00165 
00166 
00167 /*
00168 std::vector<unsigned int> VariableSet::getVarCoord(unsigned int code)
00169 {
00170         std::vector<unsigned int> coord;
00171         code -= 1;
00172         coord.push_back(code % dim[0]);
00173         code = (code - (code % dim[0]))/dim[0];
00174         coord.push_back(code % dim[1]);
00175         if (dim.size()>2)
00176         {
00177                 code = (code - (code % dim[1]))/dim[1];
00178                 coord.push_back(code % dim[2]);
00179         }
00180         return coord;
00181 }
00182 */
00183 
00184 
00185 
00186 // Thank you "1 to nvars" encoding, thank you. You make it SO easy.
00187 
00188 
00189 // I hate you.
 All Classes Namespaces Files Functions Variables Defines