Main Page   Class Hierarchy   Compound List   File List   Contact   Download   Symbolic Constraints   Examples  

maxsat.cc

00001 #include<scil/scil.h>
00002 #include<scil/core/boolfunction.h>
00003 #include<iostream>
00004 
00005 using namespace SCIL;
00006 using namespace std;
00007 
00008 void read_instance(char* infile, ILP_Problem &IP, vector<var>& vars, list<boolfunction*>& bf_list) {
00009 
00010    string path(infile);
00011    ifstream file(path.c_str());
00012    if(file.fail()) {
00013       cerr<<"no file with name "<< infile<<" found!"<< endl;
00014       exit(1);
00015    }
00016    char buf[1024];
00017    while(file.peek() != 'p')
00018       file.getline(buf, 1024);
00019 
00020    char c;
00021    string type;
00022    int nv,nc, v, weight =1;
00023 
00024    file>>c;
00025    file>>type;
00026    file>>nv;
00027    file>>nc;
00028    cout<<c<<" "<<type<<" "<<nv<<" "<<nc<<endl;
00029    vars.resize(nv+1); 
00030    for(int i=1; i<=nv; i++){
00031       vars[i] = IP.add_binary_variable(0);
00032    }
00033    for(int j=0; j<nc; j++){
00034       if(type == "wcnf")
00035         file >> weight;
00036       boolfunction* bf = NULL;
00037       file >> v;
00038       while(v != 0){
00039          if(bf){
00040             if(v < 0)
00041                bf->add(OR, vars[-v], true);
00042             else
00043                bf->add(OR, vars[v]);
00044          } else {
00045             if(v < 0)
00046                bf = new boolfunction(vars[-v], true);
00047             else
00048                bf = new boolfunction(vars[v]);
00049          }
00050          file >> v;
00051       }
00052       cout<<weight<<"   "<<j<<": "<<bf<<endl;
00053       bf_list.push_back(bf);
00054       IP.add_boolfunction(bf, weight);
00055    }
00056 
00057 }
00058 
00059 int main(int argc, char** argv) {
00060    
00061    vector<var> vars;
00062    list<boolfunction*> bf_list;
00063    ILP_Problem IP(Optsense_Max);
00064    
00065    read_instance(argv[1], IP,vars,bf_list);
00066 
00067    IP.optimize();
00068 
00069    solution s = IP.get_solution();
00070    cout<<"-----------Solution-----------"<<endl;
00071    for( int i = 1; i < vars.size(); i++)
00072       cout<<"x"<<i-1<<": "<<s.value(vars[i])<<endl;
00073    int num=0;
00074    for( list<boolfunction*>::iterator it = bf_list.begin(); it != bf_list.end(); it++)
00075       if (!((*it)->evaluate(s))){
00076          cout<<(*it)<<"  value: "<<(*it)->evaluate(s)<<endl;
00077          num++;
00078       }
00079    cout<<"O = "<<num<<endl;
00080 }
00081 
Generated on Mon Mar 28 22:03:49 2011 for SCIL by  doxygen 1.6.3