pomdp.cpp
Go to the documentation of this file.
00001 #include "pomdp.h"
00002 
00003 Pomdp::Pomdp(){
00004 
00005   int numstates, numactions, numobservations;
00006   last_action_ = -1;
00007   //init class attributes if necessary
00008   this->actioncount = 0;
00009 
00010   pomdpfile_ = std::string("data/pomdpfile.txt");
00011   if(node_handle_.getParam("pomdpfile", pomdpfile_)) {
00012     ROS_INFO("pomdpfile global path found, using %s", pomdpfile_.c_str());
00013   } else {
00014     std::ifstream pomdpdata(pomdpfile_.c_str());
00015     ROS_WARN("pomdpfile global path not found, using default %s", pomdpfile_.c_str());
00016     if(!pomdpdata){
00017       ROS_FATAL("File %s doesn't exist: aborting.",pomdpfile_.c_str()); 
00018     }
00019   }
00020 
00021   srand( time(NULL) ); //we do this at policy too...  
00022   GenericPolicy::getDimensions(pomdpfile_,&numstates,&numactions,&numobservations); 
00023   myPolicy = new GenericPolicy(pomdpfile_,numactions,numstates,numobservations);
00024 
00025   //init class attributes if necessary
00026   this->execution_flag = false;
00027 
00028   //string for port names
00029 
00030   // [init publishers]
00031   this->focused_obj_label_publisher = this->node_handle_.advertise<std_msgs::Int32>("/planner/focused_obj_label", 5);
00032   this->belief_summary_publisher = this->node_handle_.advertise<estirabot_msgs::belief_summary>("/planner/belief_summary", 5);
00033   this->belief_publisher = this->node_handle_.advertise<estirabot_msgs::belief>("/planner/belief", 5);
00034   
00035   // [init subscribers]
00036   
00037   // [init services]
00038   execution_flow_control_server_ = this->node_handle_.advertiseService("/planner/execution_flow_control", &Pomdp::execution_flow_controlCallback, this);
00039   obs2action_server_ = this->node_handle_.advertiseService("/planner/obs2action", &Pomdp::obs2actionCallback, this);
00040   resetPOMDP_server_ = this->node_handle_.advertiseService("/planner/resetPOMDP", &Pomdp::resetPOMDPCallback, this);
00041   
00042   // [init clients]
00043 
00044   // [init action servers]
00045   
00046   // [init action clients]
00047 
00048 }
00049 
00050 void Pomdp::mainLoop(void){
00051 
00052 }
00053 
00054 bool Pomdp::resetPOMDPCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res){
00055 
00056     myPolicy->resetToInitialBelief();
00057     publish_belief();
00058     return true; 
00059 }
00060 bool Pomdp::obs2actionCallback(estirabot_msgs::obs2action::Request &req, estirabot_msgs::obs2action::Response &res){
00061 
00062     int obs, state;
00063 
00064     if(!req.first){
00065         //myPolicy->logState(LOGFILE);
00066         //update belief
00067         //obs = myPolicy->domain.get_observation(req.num_objectsA, req.num_objectsB, MAXNUMB); 
00068         obs = req.observation;
00069         myPolicy->transformState(last_action_,obs);
00070         ROS_INFO("Belief Updated with observation %d and action %d",obs,last_action_); 
00071 
00072         //visualization
00073         state = myPolicy->mostProbableState();
00074         ROS_INFO("Most probable state %d with probability %f", state, myPolicy->getStateProbability(state));
00075     
00076 
00077         if(!myPolicy->isFinal(state) || myPolicy->getStateProbability(state) < 0.90){ 
00078             res.goal_reached = false;
00079         }else{
00080             ROS_INFO("Final state reached with probability %f", myPolicy->getStateProbability(myPolicy->mostProbableState()));
00081             res.goal_reached = true;
00082         }
00083     }
00084     //get action
00085     last_action_ = myPolicy->getBestAction();
00086     ROS_INFO("Best action is %d ", last_action_);
00087 
00088     res.action = last_action_;
00089 
00090     publish_belief();
00091 
00092     return true; 
00093 }
00094 
00095 bool Pomdp::execution_flow_controlCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res){
00096 
00097     //stop execution when called
00098     this->execution_flag = !this->execution_flag;
00099     return true; 
00100 }
00101 
00102 void Pomdp::publish_belief(){
00103     std::list<std::pair<double,int> > state_probability_list;
00104     std::vector<int> indexlist;
00105     std::list<std::pair<double,int> >::iterator it;
00106     std::vector<double> objInA, objInB;  
00107     double averageObjInA=0, averageObjInB=0;
00108     double uncertaintyA=0, uncertaintyB=0;
00109     int numA, numB, firstNumA, firstNumB;
00110 
00111     ROS_INFO("Belief (top four):");
00112     myPolicy->probableStatesList(state_probability_list, indexlist, 0.05);
00113     for(int state=0; state < 4;state++){
00114         if(myPolicy->getStateProbability(indexlist[state]) < 0.05)
00115             break;
00116         if((unsigned int)indexlist[state] > indexlist.size()) 
00117             continue;
00118         ROS_INFO("%f", myPolicy->getStateProbability(indexlist[state]));
00119     }
00120 
00121 //    myPolicy->getBeliefAverageAtPiles(objInA,objInB);
00122 //    for ( unsigned int i=0; i < objInA.size(); i++ )
00123 //        averageObjInA += i*objInA[i]; 
00124 //    for ( unsigned int i=0; i < objInB.size(); i++ )
00125 //        averageObjInB += i*objInB[i]; 
00128 //
00129 //    myPolicy->domain.get_num_obj(state_probability_list.front().second, firstNumA, firstNumB, MAXNUMB);
00130 //
00131 //    for ( it=state_probability_list.begin(); it != state_probability_list.end(); it++ ){
00132 //        std::pair<double,int> aux = *it;
00133 //        if(aux.first == 0)
00134 //            break;
00135 //        myPolicy->domain.get_num_obj(aux.second, numA, numB, MAXNUMB);
00136 //        if( numA == firstNumA) 
00137 //            uncertaintyA += aux.first;
00138 //        if( numB == firstNumB)
00139 //            uncertaintyB += aux.first;
00140 //    }
00141 //    
00142     this->belief_summary_msg.num_objects_A = 0; 
00143     this->belief_summary_msg.uncertainty_A = 0;
00144     this->belief_summary_msg.num_objects_B = 0; 
00145     this->belief_summary_msg.uncertainty_B = 0;
00146     this->belief_summary_msg.uncertainty_total = 1 - myPolicy->getStateProbability(indexlist[0]);
00147     ROS_INFO("Publishing belief and overall uncertainty %f", belief_summary_msg.uncertainty_total);
00148     belief_summary_publisher.publish(this->belief_summary_msg);
00149 
00150     this->belief_msg.states_probabilities.clear();
00151     myPolicy->probableStatesList(this->belief_msg.states_probabilities);
00152     this->belief_msg.action_values.clear();
00153     myPolicy->actionValueList(this->belief_msg.action_values);
00154 
00155     belief_publisher.publish(this->belief_msg);
00156 
00157 }
00158 
00159 /* main function */
00160 int main(int argc,char *argv[])
00161 {
00162     ros::init(argc, argv, "pomdp");
00163     Pomdp pomdp;
00164     ros::Rate loop_rate(10); 
00165     while(ros::ok()){
00166       pomdp.mainLoop();
00167       ros::spinOnce();
00168       loop_rate.sleep(); 
00169     }
00170 }


iri_2piles_pomdp
Author(s): pmonso
autogenerated on Fri Dec 6 2013 21:45:26