Function.cpp
Go to the documentation of this file.
00001 // written by png shao wei
00002 #include <algorithm>
00003 #include "Function.h"
00004 
00005 Function::Function()
00006 {
00007 }
00008 void Function::setVNameCurr(string soa) {
00009     vnameCurr = soa;
00010 }
00011 
00012 void Function::setParents(vector<string> p) {
00013     parents = p;
00014 }
00015 
00016 string Function::getVNameCurr() const{
00017     return vnameCurr;
00018 }
00019 
00020 vector<string> Function::getParents() const{
00021     return parents;
00022 }
00023 
00024 void Function::initSparseTables(map<string, StateObsAct*>* mymap) {
00025     //if (parents.size() == 0)
00026     //sparseT->numCIValues.push_back(-999);
00027     vector<string> cIheader, uIheader;
00028     vector<int> numCIValues, numUIValues;
00029     for (unsigned int i = 0; i < parents.size(); i++) {
00030         cIheader.push_back(parents[i]);
00031         StateObsAct* soa = (*mymap)[parents[i]];
00032         if (soa != NULL)
00033             numCIValues.push_back(soa->getValueEnum().size());
00034         else
00035             numCIValues.push_back(1);
00036     }
00037     uIheader.push_back(vnameCurr);
00038     StateObsAct* soa = (*mymap)[vnameCurr];
00039     numUIValues.push_back(soa->getValueEnum().size());
00040 
00041     sparseT = SharedPointer<SparseTable>(new SparseTable(cIheader, uIheader, numCIValues, numUIValues));
00042 }
00043 
00044 
00045 void Function::fillTables(map<string, StateObsAct*>& mymap,
00046         const vector<string>& insttokens, const vector<double>& probttokensdouble) {
00047     bool simple = true;
00048     bool seestar = false;
00049 
00050     // this method should stop if a '-' or '*' is found.
00051     // in this case, it will call filLTables with the new parameters
00052     for (unsigned int i = 0; i < insttokens.size() && !seestar; i++) {
00053 
00054         if (insttokens[i] == "*" || insttokens[i] == "-") {
00055             seestar = true;
00056             simple = false;
00057             vector<string> allCombinations;
00058             if (i != insttokens.size() - 1) {
00059                 if (mymap.find(parents[i]) != mymap.end()) {
00060                     allCombinations = mymap[parents[i]]->getValueEnum(); // get all the various enumerations
00061                 } else {
00062                     cerr<<"Couldn't find mapping for:"<<parents[i]<<endl;
00063                     cerr<<"Please check XML file again."<<endl;
00064                     exit(-1);
00065                 }
00066             } else {
00067                 if (mymap.find(vnameCurr) != mymap.end()) {
00068                     allCombinations = mymap[vnameCurr]->getValueEnum();
00069                 } else {
00070                     cerr<<"Couldn't find mapping for:"<<vnameCurr<<endl;
00071                     cerr<<"Please check XML file again."<<endl;
00072                     exit(-1);
00073                 }
00074             }//end of if
00075 
00076             // multiple fillTables will be called, populating the table with entries
00077             for (unsigned int j = 0; j < allCombinations.size(); j++) {
00078 
00079                 // replace an '-' or '*' with a string
00080                 vector<string> temp = insttokens;
00081                 string s(allCombinations[j]);
00082                 temp.erase(temp.begin() + i);
00083                 temp.insert(temp.begin() + i, s);
00084 
00085                 // split the probtables if the input is a -
00086                 // recursive calls
00087                 if (insttokens[i] == "-") {
00088                     // splitTable will allocate the probability tokens to the correct parent
00089                     // note that one parent may still has more than one probability token
00090                     // assuming parents = action, X, parentA, parentB,
00091                     // and parentA has 2 elements a1,a2, while parentB has two elements,b1,2
00092                     // <Instance>action x - -</Instance>
00093                     // <ProbTable> x1 x2 x3 x4 </ProbTable>
00094                     // in this case,
00095                     // for the first iteration, when our program detects the first '-'
00096                     // x1 and x2 are allocated to the first element of the first '-', namely a1
00097                     // while x3 and x4 are allocated to the second element of the first '-,', namely a2
00098 
00099 
00100                     vector<double> st = splitTable(probttokensdouble, allCombinations.size(), j);
00101                     fillTables(mymap, temp, st);
00102                 } else {
00103                     fillTables(mymap, temp, probttokensdouble);
00104                 }
00105             }//end of for
00106         }//end of if
00107     }//end of for
00108 
00109     if (probttokensdouble.size() > 1)
00110         simple = false;
00111 
00112     //Base Case
00113     if (simple) {
00114         // simple means without * or -
00115         // separate the insttokens into the "tokens of parents" and "itself"
00116         // pass to simpleInsert
00117         string myself = insttokens.back();
00118 
00119         // Note new_insttokens size is now 1 less than insttokens
00120         vector<string> new_insttokens;
00121         new_insttokens.resize(insttokens.size() - 1);
00122         copy(insttokens.begin(), insttokens.end()-1, new_insttokens.begin());
00123 
00124         simpleSparseInsert(mymap, new_insttokens, myself, probttokensdouble.front());
00125     }
00126 }
00127 
00128 vector<double> Function::splitTable(const vector<double>& probttokensdouble, int size,
00129         int position) {
00130     vector<double> st;
00131 
00132     int factor = probttokensdouble.size() / size;
00133 
00134     for (int i = position * factor; i < position * factor + factor; i++) {
00135         st.push_back(probttokensdouble[i]);
00136     }
00137     return st;
00138 }
00139 
00140 void Function::simpleSparseInsert(map<string, StateObsAct*>& mymap, const vector<
00141         string>& insttokens, const string& myself, double prob) {
00142 
00143     SparseEntry st;
00144     int position = 0;
00145 
00146     vector<int> ci;
00147     for (unsigned int i = 0; i < insttokens.size(); i++) {
00148         position = mymap[parents[i]]->getPosition(insttokens[i]);
00149         ci.push_back(position);
00150     }
00151 
00152     UniqueIndex ui;
00153     ui.index = mymap[vnameCurr]->getPosition(myself);
00154     ui.value = prob;
00155     st.uniqueIndex.push_back(ui);
00156 
00157     sparseT->add(ci, st);
00158 }
00159 
00160 bool Function::checkNoMissingEntries(map<string, StateObsAct*> mymap, string& info) {
00161      stringstream ssinfo;
00162      vector<int> commonIndex;
00163      if (!sparseT->checkNoMissingEntries(commonIndex)) 
00164      {
00165           ssinfo << "  Transition not specified for instance ";
00166           for(int i=0;i<commonIndex.size();i++){
00167                if(getParents()[i]!="null"){
00168                     StateObsAct* soa = mymap[getParents()[i]];
00169                     ssinfo << soa->getValueEnum()[commonIndex[i]] << " ";
00170                }else{
00171                     ssinfo << "null" << endl;
00172                }
00173           }
00174           ssinfo << endl;
00175           info = ssinfo.str();
00176           return false;
00177      }
00178      else
00179           return true;
00180 }//end of checkNoMissingEntries(map<string, StateObsAct*> mymap)
00181 
00182 
00183 std::ostream& Function::write(std::ostream& out) {
00184     out << "\nvnameCurr :" << vnameCurr << endl;
00185     out << "parents: " << endl;
00186 
00187     for (unsigned int i = 0; i < parents.size(); i++) {
00188         out << parents[i] << ",";
00189     }
00190     sparseT->write(out);
00191     return out;
00192 }


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29