predicates.cpp
Go to the documentation of this file.
00001 #include <predicates.h>
00002 
00003 // Operators
00004 bool operator==(Predicate &p1, Predicate &p2)
00005 {
00006     return ((*static_cast<Symbol*>(&p1) == p2) && (p1.positive_ == p2.positive_));
00007 //     return (( *static_cast<const Symbol*>(&p1)==*static_cast<const Symbol*>(&p2) ) && (p1.positive_ == p2.positive_));
00008 }
00009 
00010 std::ostream& operator<<(std::ostream &out, const Predicate& p)
00011 {
00012     if (not p.positive_)
00013         out << "-";
00014     out << *static_cast<const Symbol*>(&p);
00015     return out;
00016 }
00017 
00018 std::ostream& operator<<(std::ostream &out, const PredicateGroup& p)
00019 {
00020     for ( size_t i = 0; i < p.predicates_.size(); i++ ) {
00021         out << p.predicates_[i];
00022         if ((i+1) < p.predicates_.size())
00023             out << ",";
00024     }
00025     return out;
00026 }
00027 
00028 std::ostream& operator<<(std::ostream &out, const Outcome& o)
00029 {
00030     out << o.current_probability_ << " ";
00031     if (o.noisy_) {
00032         out << "<noise>";
00033     }
00034     else {
00035         out << *static_cast<const PredicateGroup*>(&o);
00036     }
00037     return out;
00038 }
00039 
00040 // Predicate
00041 
00042 // PredicateGroup
00043 PredicateGroup::PredicateGroup(PredicateList predicates) :
00044         predicates_(predicates)
00045 { }
00046         
00047 PredicateGroup::PredicateGroup(std::string predicates) :
00048         predicates_(get_predicates_from_string(predicates))
00049 { }
00050 
00051 bool PredicateGroup::is_satisfied(PredicateList predicates)
00052 {   
00053         bool satisfied = true;
00054         for ( PredicateList::iterator it_pred=predicates_.begin() ; it_pred != predicates_.end(); it_pred++ ) {
00055         bool pred_satisfied = false;
00056         for ( PredicateList::iterator it_pred2=predicates.begin() ; it_pred2 != predicates.end(); it_pred2++ ) {
00057                 if (*it_pred == *it_pred2) {
00058                 pred_satisfied = true;
00059                 }
00060         }
00061         if (!pred_satisfied) {
00062                 satisfied = false;
00063         }
00064         }
00065         ROS_DEBUG_STREAM("Checking predicates\nPreconditions:" << *this << "\nState:" << PredicateGroup(predicates) << "\nResult: " << satisfied);
00066         return satisfied;
00067 }
00068 
00069 int PredicateGroup::count_satisfied(PredicateList predicates)
00070 {
00071         int n_satisfied = 0;
00072         for ( PredicateList::iterator it_pred=predicates_.begin() ; it_pred != predicates_.end(); it_pred++ ) {
00073                 bool pred_satisfied = false;
00074                 for ( PredicateList::iterator it_pred2=predicates.begin() ; it_pred2 != predicates.end(); it_pred2++ ) {
00075                         if (*it_pred == *it_pred2) {
00076                                 pred_satisfied = true;
00077                         }
00078                 }
00079                 if (pred_satisfied) {
00080                         n_satisfied++;
00081                 }
00082         }
00083         ROS_DEBUG_STREAM("Counting predicates\nPreconditions:" << *this << "\nState:" << PredicateGroup(predicates) << "\nResult: " << n_satisfied);
00084         return n_satisfied;
00085 }
00086 
00087 int PredicateGroup::number_parameters()
00088 {
00089         std::set<std::string> parameters;
00090         
00091         for ( PredicateList::iterator it_pred=predicates_.begin() ; it_pred != predicates_.end(); it_pred++ ) {
00092                 for ( std::vector<std::string>::iterator it_param = it_pred->param_names_.begin(); it_param != it_pred->param_names_.end(); it_param++) {
00093                         if (parameters.count(*it_param) == 0)
00094                                 parameters.insert(*it_param);
00095                 }
00096         }
00097         
00098         return parameters.size();
00099 }
00100 
00101 void PredicateGroup::change_variable(std::string orig_var, std::string new_var)
00102 {
00103         for ( PredicateList::iterator it_pred=predicates_.begin() ; it_pred != predicates_.end(); it_pred++ ) {
00104                 for ( std::vector<std::string>::iterator it_param = it_pred->param_names_.begin(); it_param != it_pred->param_names_.end(); it_param++) {
00105                         if (it_param->compare(orig_var) == 0) {
00106                                 *it_param = new_var;
00107                                 // TODO look replace if
00108                         }
00109                 }
00110         }
00111 }
00112 
00113 PredicateGroup PredicateGroup::get_predicates_with_vars(std::vector< std::string > variables) 
00114 {
00115         PredicateList preds_with_vars;
00116         for ( PredicateList::iterator it_pred=predicates_.begin() ; it_pred != predicates_.end(); it_pred++ ) {
00117                 for ( std::vector<std::string>::iterator it_param = it_pred->param_names_.begin(); it_param != it_pred->param_names_.end(); it_param++) {
00118                         for ( std::vector<std::string>::iterator it_var = variables.begin(); it_var != variables.end(); it_var++) {
00119                                 if (it_param->compare(*it_var) == 0) {
00120                                         preds_with_vars.push_back(*it_pred);
00121                                 }
00122                         }
00123                 }
00124         }
00125         
00126         PredicateGroup result(preds_with_vars);
00127         result.remove_duplicated();
00128         return result;
00129 }
00130 
00131 void PredicateGroup::add_positive_differences(PredicateGroup compared_group) 
00132 {
00133         PredicateGroup differences = compared_group.get_differences(*this);
00134         for ( PredicateList::iterator it_pred=differences.predicates_.begin() ; it_pred != differences.predicates_.end(); it_pred++ ) {
00135                 if (it_pred->positive_) {
00136                         this->predicates_.push_back(*it_pred);
00137                 }
00138         }
00139 //         this->predicates_.insert( this->predicates_.end(), differences.predicates_.begin(), differences.predicates_.end() );
00140         remove_duplicated();
00141 }
00142 
00143 PredicateGroup PredicateGroup::get_differences(const PredicateGroup compared_group) 
00144 {
00145 //             std::set<Predicate> s1(predicates_.begin(), predicates_.end());
00146 //             std::set<Predicate> s2(compared_preds.predicates_.begin(), compared_preds.predicates_.end());
00147 //             std::vector<Predicate> diffs;
00148 //             std::set_difference (s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(diffs));
00149 //             return PredicateGroup(diffs);
00150         
00151         PredicateList compared_preds(compared_group.predicates_);
00152         PredicateGroup differences;
00153         bool is_present;
00154         for ( PredicateList::iterator it_pred=predicates_.begin() ; it_pred != predicates_.end(); it_pred++ ) {
00155                 is_present = false;
00156                 for ( PredicateList::iterator it_pred2=compared_preds.begin() ; it_pred2 != compared_preds.end();  ) {
00157                         if (*it_pred == *it_pred2) {
00158                                 is_present = true;
00159                                 it_pred2 = compared_preds.erase(it_pred2);
00160                         }
00161                         else {
00162                                 ++it_pred2;
00163                         }
00164                 }
00165                 if (!is_present) {
00166                         differences.predicates_.push_back(it_pred->get_negated());
00167                 }
00168         }
00169         
00170         differences.predicates_.insert( differences.predicates_.end(), compared_preds.begin(), compared_preds.end() );
00171         
00172         differences.remove_duplicated();
00173         ROS_DEBUG_STREAM("Differences: " << differences);
00174         return differences;
00175 }
00176 
00177 void PredicateGroup::remove_duplicated()
00178 {
00179         for ( PredicateList::iterator it_pred=predicates_.begin() ; it_pred != predicates_.end(); it_pred++ ) {
00180                 for ( PredicateList::iterator it_pred2=(it_pred+1) ; it_pred2 != predicates_.end(); ) {
00181                         if (*it_pred == *it_pred2) {
00182                                 it_pred2 = predicates_.erase(it_pred2);
00183                         }
00184                         else {
00185                                 ++it_pred2;
00186                         }
00187                 }
00188         }
00189 }
00190 
00191 void PredicateGroup::remove_variables_not_present(std::vector<std::string> variables)
00192 {
00193         for ( PredicateList::iterator it_pred=predicates_.begin() ; it_pred != predicates_.end(); ) {
00194                 if (it_pred->has_param_names_in(variables)) {
00195                         ++it_pred;
00196                 }
00197                 else {
00198                         it_pred = predicates_.erase(it_pred);
00199                 }
00200         }
00201 }
00202 
00203 
00204 // Outcome
00205 
00206 // Other
00207 
00208 
00209 
00210 
00211 PredicateList get_predicates_from_string(std::string line)
00212 {
00213     std::string item, aux;
00214     bool positive;
00215     PredicateList pred_list;
00216     
00217     // remove spaces
00218 //     line.erase(remove_if(line.begin(), line.end(), isspace), line.end());
00219     // remove leading spaces
00220 //     line = line.substr(line.find_first_not_of(" \t"));
00221     // remove leading and trailing spaces
00222     line = trim(line);
00223     
00224     std::stringstream ss(line);
00225     
00226     // case of empty string
00227     if ( (line=="") || (line=="--") ) {
00228         return pred_list;
00229     }
00230     
00231     while (std::getline(ss, item, ')')) {
00232         //std::cout << "Generating predicate: " << std::endl;
00233         
00234         // add ) to the end to complete it
00235         item = item + ')';
00236         
00237         // if begins with , remove it
00238         aux = item.substr(0,1);
00239         if (aux.compare(",") == 0)
00240             item = item.substr(1);
00241         
00242         aux = item.substr(0,1);
00243         if (aux.compare("-") == 0) {
00244             item = item.substr(1);
00245             positive = false;
00246         }
00247         else
00248             positive = true;
00249         
00250         pred_list.push_back(Predicate(get_symbol_from_string(item), positive));
00251     }
00252     return pred_list;
00253 }
00254 
00255 Outcome get_outcome_from_string(std::string line)
00256 {
00257     std::string item;
00258     float probability;
00259     
00260     // remove leading spaces
00261     line = line.substr(line.find_first_not_of(" \t"));
00262     std::stringstream ss(line);
00263     
00264     if(std::getline(ss, item, ' ')) {
00265         //std::cout << "Probability: " << item << std::endl;
00266     }
00267     probability = atof(item.c_str());
00268     
00269     if (ss.eof())
00270         return Outcome(probability);
00271     
00272     // get rest of the line
00273     std::getline(ss, item);
00274     if (item.find("<noise>")!=std::string::npos) {
00275         return Outcome(probability, true);
00276     }
00277     else if (item.find("<no-change>")!=std::string::npos) {
00278         return Outcome(probability);
00279     }
00280     else {
00281         return Outcome(get_predicates_from_string(item), probability);
00282     }
00283 } 


iri_rule_learner
Author(s): dmartinez
autogenerated on Fri Dec 6 2013 20:43:48