Go to the documentation of this file.00001 #include "rule_learner_alg_node.h"
00002 
00003 RuleLearnerAlgNode::RuleLearnerAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<RuleLearnerAlgorithm>()
00005 {
00006   
00007   this->loop_rate_ = 0.2;
00008 
00009   
00010   
00011   
00012   
00013   
00014   this->learn_with_pasula_server_ = this->public_node_handle_.advertiseService("learn_with_pasula", &RuleLearnerAlgNode::learn_with_pasulaCallback, this);
00015   this->add_state_server_ = this->public_node_handle_.advertiseService("add_state", &RuleLearnerAlgNode::add_stateCallback, this);
00016   this->add_action_server_ = this->public_node_handle_.advertiseService("add_action", &RuleLearnerAlgNode::add_actionCallback, this);
00017   
00018   
00019   learn_pasula_client_ = this->public_node_handle_.serviceClient<iri_learning_msgs::LearnRulesFromTransitions>("learn_pasula");
00020   representation_to_string_client_ = this->public_node_handle_.serviceClient<estirabot_msgs::RepresentationToString>("representation_to_string");
00021   get_state_changes_client_ = this->public_node_handle_.serviceClient<estirabot_msgs::StateRepresentationChanges>("get_state_changes");
00022   
00023   
00024   
00025   
00026   
00027   pasula_time_ = ros::Duration(0);
00028   pasula_iter_ = 0;
00029 }
00030 
00031 RuleLearnerAlgNode::~RuleLearnerAlgNode(void)
00032 {
00033   
00034 }
00035 
00036 void RuleLearnerAlgNode::mainNodeThread(void)
00037 {
00038   
00039   
00040   
00041   
00042   
00043 
00044   
00045         std::vector<std::string> actions = this->alg_.actions_needing_pasula_learning();
00046         if (actions.size() > 0) {
00047                 ROS_INFO("RuleLearnerAlgNode:: Requesting pasula rules."); 
00048                 
00049                 this->alg_.lock();
00050                 TransitionList transitions = this->alg_.get_transitions_with_actions(actions);
00051                 this->alg_.write_transitions(remove_unused_predicates(transitions));
00052                 this->alg_.reset_new_transitions();
00053                 this->alg_.unlock();
00054                 
00055                 ros::Time begin = ros::Time::now();
00056                 
00057                 if (learn_pasula_client_.call(learn_pasula_srv_)) 
00058                 { 
00059                         ROS_INFO_STREAM("RuleLearnerAlgNode:: Response: " << learn_pasula_srv_.response.ok); 
00060                 } 
00061                 else 
00062                 { 
00063                         ROS_ERROR("RuleLearnerAlgNode:: Failed to Call Server on topic learn_pasula "); 
00064                         return;
00065                 }
00066                 this->alg_.lock();
00067                 std::vector<int> number_rules = this->alg_.update_with_pasula_rules(actions, transitions);
00068                 this->alg_.unlock();
00069                 
00070                 
00071                 pasula_time_ += ros::Time::now() - begin;
00072                 pasula_iter_++;
00073                 
00074                 std::string pasula_logs_file_path = "/tmp/pasula_logs.txt";
00075                 std::ofstream pasula_logs_file(pasula_logs_file_path.c_str());
00076                 if (pasula_logs_file.is_open())
00077                 {
00078                         pasula_logs_file << pasula_time_ << std::endl;
00079                         pasula_logs_file << pasula_iter_ << std::endl;
00080                         pasula_logs_file << number_rules[2] << std::endl;
00081                         pasula_logs_file << number_rules[0] << std::endl;
00082                         pasula_logs_file << number_rules[1] << std::endl;
00083                         
00084                         
00085                 }
00086                 else
00087                         ROS_ERROR_STREAM("Couldn't write pasula logs to " << pasula_logs_file_path);
00088                 pasula_logs_file.close();
00089         }
00090         
00091 }
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 bool RuleLearnerAlgNode::add_stateCallback(estirabot_msgs::AddState::Request &req, estirabot_msgs::AddState::Response &res) 
00133 { 
00134         ROS_DEBUG("RuleLearnerAlgNode::add_stateCallback: New Request Received!"); 
00135         
00136         
00137         
00138         
00139 
00140         res.rules_updated = this->alg_.add_state(req.state);
00141 
00142         
00143         
00144         
00145         
00146         return true; 
00147 }
00148 
00149 
00150 bool RuleLearnerAlgNode::add_actionCallback(estirabot_msgs::AddAction::Request &req, estirabot_msgs::AddAction::Response &res) 
00151 { 
00152         ROS_DEBUG("RuleLearnerAlgNode::add_actionCallback: New Request Received!"); 
00153         
00154         
00155         
00156         
00157         
00158         res.needs_learning = this->alg_.add_action(req.action, req.target_dirty_areas.indexes);
00159         
00160         
00161         
00162         
00163         
00164         return true; 
00165 }
00166 
00167 
00168 
00169 
00170 
00171 void RuleLearnerAlgNode::node_config_update(Config &config, uint32_t level)
00172 {
00173         this->alg_.lock();
00174 
00175         this->alg_.unlock();
00176 }
00177 
00178 void RuleLearnerAlgNode::addNodeDiagnostics(void)
00179 {
00180 }
00181 
00182 
00183 int main(int argc,char *argv[])
00184 {
00185         return algorithm_base::main<RuleLearnerAlgNode>(argc, argv, "rule_learner_alg_node");
00186 }