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

logopt.cc

00001 #include<scil/scil.h>
00002 #include<scil/core/boolfunction.h>
00003 #include<iostream>
00004 #include<sstream>
00005 #include<stack>
00006 #include<sstream>
00007 
00008 using namespace SCIL;
00009 using namespace std;
00010 
00011 
00012 struct bfElement{
00013    public:
00014    boolfunction* bf;
00015    boolOperator op;
00016    bfElement(var v) { 
00017       bf = new boolfunction(v);
00018       op=BASIC;
00019    };
00020    bfElement(boolfunction* _bf){
00021       bf = _bf;
00022       op = BASIC;
00023    };
00024 };
00025 
00026 struct bfStack {
00027    stack<bfElement> st;
00028    bool negated;
00029    bfStack(bool neg){
00030       negated=neg;
00031    };
00032    bfElement& top(){
00033       return st.top();
00034    };
00035    void pop(){
00036       st.pop();
00037    };
00038    void push(bfElement bfE){
00039       st.push(bfE);
00040    };
00041    bool empty(){
00042       return st.empty();
00043    };
00044 };
00045 
00046 //creates a boolfunction based on an input string buf
00047 boolfunction* read_boolfunction(string buf, ILP_Problem& IP, map<string,var>& varMap) {
00048    pair<map<string,var>::iterator, bool> newPair;
00049    map<string, var>::iterator mapIt;
00050    stack<bfStack> mainStack;
00051    mainStack.push(bfStack(false));
00052    istringstream iss(buf);
00053    string s;
00054    while(!iss.eof()) {
00055       iss >> s;
00056       switch (s[0]) {
00057          case '&':
00058             mainStack.top().top().op=AND;
00059             break;
00060          case '^':
00061             mainStack.top().top().op=XOR;
00062             break;
00063          case '|':
00064             mainStack.top().top().op=OR;
00065             break;
00066          case '=':
00067             mainStack.top().top().op=EQU;
00068             break;
00069          case '<':
00070             mainStack.top().top().op=PMI;
00071             break;
00072          case '>':
00073             mainStack.top().top().op=IMP;
00074             break;
00075          default:
00076             int front=0; 
00077             int back=s.size()-1;
00078             bool found = false;
00079             while(found == false) {
00080                switch (s[front]) {
00081                   case '!':
00082                      front++;
00083                      mainStack.push(bfStack(true));
00084                      break;
00085                   case '(':
00086                      front++;
00087                      mainStack.push(bfStack(false));
00088                      break;
00089                   default:
00090                      if(s[back] == ')'){
00091                         back--;
00092                      } else
00093                         found=true;
00094                      break;
00095                }
00096             }
00097             string varName = s.substr(front, back-front+1);
00098             mapIt=varMap.find(varName);
00099             if(mapIt == varMap.end()){
00100                varMap.insert(make_pair(varName, IP.add_binary_variable(.0)));
00101             }
00102             mainStack.top().push(bfElement(varMap[varName]));
00103             for(int k=0; k<s.size()-1-back; k++) {
00104                if(mainStack.top().negated)
00105                   k--;
00106                bfStack bfs = mainStack.top();
00107                mainStack.pop();
00108                boolfunction* bf = bfs.top().bf->copy();
00109                delete bfs.top().bf;
00110                bfs.pop();
00111                while(!bfs.empty()){
00112                   bf->add(bfs.top().bf,bfs.top().op);
00113                   delete bfs.top().bf;
00114                   bfs.pop();
00115                }
00116                if(bfs.negated)
00117                   bf->negate();
00118                mainStack.top().push(bfElement(bf));
00119             }
00120       }
00121    }
00122    boolfunction* bf;
00123    while(!mainStack.empty()){
00124       bfStack bfs = mainStack.top();
00125       mainStack.pop();
00126       bf = bfs.top().bf->copy();
00127       delete bfs.top().bf;
00128       bfs.pop();
00129       while(!bfs.empty()){
00130          bf->add(bfs.top().bf,bfs.top().op);
00131          delete bfs.top().bf;
00132          bfs.pop();
00133       }
00134       if(bfs.negated)
00135          bf->negate();
00136       if(!mainStack.empty())
00137          mainStack.top().push(bfElement(bf));
00138    }
00139    return bf;
00140 }
00141 
00142 void read_instance(char* infile, ILP_Problem &IP, map<string,var> &varMap) {
00143 
00144    string path(infile);
00145    ifstream file(path.c_str());
00146    if(file.fail()) {
00147       cerr<<"no file with name "<< infile<<" found!"<< endl;
00148       exit(1);
00149    }
00150    string s,t;
00151    char buf[1024];
00152    size_t pos, pos_alt;
00153    file >> s;
00154    while(s != "START")
00155       file >> s;
00156    while(s != "END")
00157    {
00158       file >> s;
00159       istringstream iss(s);
00160       double coeff;
00161       //if iss is a number, add a new boolfunction to objectivfunction
00162       if( iss >> coeff)
00163       {
00164          file.getline(buf, 1024);
00165          IP.add_boolfunction(read_boolfunction(buf, IP, varMap), coeff);
00166       }
00167       //if iss is a keyword add the corresponding constraint
00168       else {
00169          file.getline(buf, 1024);
00170          if (s == "C0")
00171             IP.add_bool_constraint(read_boolfunction(buf, IP, varMap), C0);
00172          else if ( s == "C1")
00173             IP.add_bool_constraint(read_boolfunction(buf, IP, varMap), C1);
00174          else if ( s == "CS"|| s == "CE") {
00175             list<boolfunction*> bf_list;
00176             t = buf;
00177             pos = 0;
00178             pos_alt = 0;
00179             while(pos != string::npos) {
00180                pos = t.find_first_of(';', pos);
00181                if ( pos != string::npos){
00182                   bf_list.push_back(read_boolfunction(t.substr(pos_alt, pos-pos_alt), IP, varMap));
00183                   pos++;
00184                   pos_alt=pos;
00185                } else 
00186                   bf_list.push_back(read_boolfunction(t.substr(pos_alt),IP, varMap));
00187             }
00188             if(s == "CS")
00189                IP.add_bool_constraint(bf_list, CS);
00190             else
00191                IP.add_bool_constraint(bf_list, CE);
00192          }
00193       }
00194    }
00195 }
00196 
00197 
00198 int main(int argc, char** argv) {
00199    //create a new ILP problem
00200    ILP_Problem IP(Optsense_Max);
00201    
00202    //read an inputfile
00203    map<string, var> varMap;
00204    read_instance(argv[1], IP, varMap);
00205 
00206    //optimize
00207    IP.optimize();
00208 
00209    //examine the solution
00210    cout<<"Optimal solution value: "<<IP.get_optimum()<<endl;
00211    for(map<string,var>::iterator it = varMap.begin(); it != varMap.end(); it++)
00212       cout<<it->first<<": "<<IP.get_solution(it->second)<<endl;
00213 }
00214 
Generated on Mon Mar 28 22:03:48 2011 for SCIL by  doxygen 1.6.3