controlpipe.cpp
Go to the documentation of this file.
00001 
00008 #include "ros/ros.h"
00009 #include "Controller.h"
00010 #include "GlobalResource.h"
00011 #include "MOMDP.h"
00012 #include "ParserSelector.h"
00013 #include "AlphaVectorPolicy.h"
00014 #include "appl/appl_request.h"
00015 #include <iostream>
00016 #include <sstream>
00017 #include <cstdlib>
00018 #include <cstdio>
00019 using namespace std;
00020 
00021 Controller *p_control;
00022 map<string, int> obsSymbolMapping, obsStateMapping;
00023 
00024 /*service callback function to process the command from client*/
00025 bool servicefn(appl::appl_request::Request&req,appl::appl_request::Response&res)
00026 {
00027         //command 0,1,2   ping init request
00028         int command=req.cmd;
00029         string xstate=req.xstate;
00030         string obs=req.obs;
00031         int action;
00032         if (command == 0) {
00033             res.action=-1;      //for testing
00034         } else if (command==1) {
00035             // format: ".init xstate"
00036             int idx_xstate = obsStateMapping[xstate];
00037             p_control->reset(idx_xstate);
00038             // dummy obs for first action
00039             action = p_control->nextAction(0, 0);
00040             cout << "action:" << action << endl;
00041             res.action=action;
00042         } else {
00043             int idx_xstate = obsStateMapping[xstate];
00044             int idx_obs = obsSymbolMapping[obs];
00045             action = p_control->nextAction(idx_obs, idx_xstate);
00046             res.action=action;
00047             cout << "action:" << action << endl;
00048         }
00049         cout << "Current belief:";
00050         p_control->currBelief()->bvec->write(cout);
00051         cout << endl;
00052         ROS_INFO("action service ended");
00053         return true;
00054 }
00055 
00056 int main(int argc, char **argv)
00057 {
00058     ros::init(argc, argv, "controlpipe");
00059     ros::NodeHandle nh;
00060     ros::ServiceServer service = nh.advertiseService("appl_request", servicefn);
00061     std::string problem_name, policy_name, default_param;
00062     nh.getParam("/controlpipe/appl_problem_name", problem_name);
00063     nh.getParam("/controlpipe/appl_policy_name", policy_name);
00064     ROS_INFO("%s!!!!!!!!!!!!!!!!!!",problem_name.c_str());
00065     
00066     
00067     SolverParams* p = &GlobalResource::getInstance()->solverParams;
00068     setvbuf(stdout,0, _IONBF,0);
00069     setvbuf(stderr,0, _IONBF,0);
00070     p->policyFile=policy_name;
00071     p->problemName=problem_name;
00072     
00073 
00074     cout<<"\nLoading the model ...\n   ";
00075     SharedPointer<MOMDP> problem = ParserSelector::loadProblem(p->problemName, *p);
00076 
00077     SharedPointer<AlphaVectorPolicy> policy = new AlphaVectorPolicy(problem);
00078 
00079     cout<<"\nLoading the policy ... input file : "<<p->policyFile<<"\n";
00080     bool policyRead = policy->readFromFile(p->policyFile);
00081 
00082     if (p->useLookahead) {
00083         cout<<"   action selection : one-step look ahead\n";
00084     }
00085 
00086     // TODO we assume the initial observed state is 0 for now
00087     p_control=new Controller(problem,policy,p,0);
00088 
00090 
00091     for(int i = 0 ; i < problem->XStates->size() ; i ++)
00092     {   
00093         //mapFile << "State : " << i <<  endl;
00094         //cout << "State : " << i <<  endl;
00095         map<string, string> obsState = problem->getFactoredObservedStatesSymbols(i);
00096         string state_str;
00097         for(map<string, string>::iterator iter = obsState.begin() ; iter != obsState.end() ; iter ++)
00098         {   
00099             //cout << iter->first << " : " << iter->second << endl ;
00100             state_str.append(iter->second);
00101         }
00102         obsStateMapping[state_str]=i;
00103     }
00104 
00105     for(int i = 0 ; i < problem->observations->size() ; i ++)
00106     {
00107         map<string, string> obsSym = problem->getObservationsSymbols(i);
00108         string obs_str;
00109         for(map<string, string>::iterator iter = obsSym.begin() ; iter != obsSym.end() ; iter ++)
00110         {
00111             obs_str.append(iter->second);
00112         }
00113         obsSymbolMapping[obs_str]=i;
00114     }
00115     
00116 
00117     cout<<"\nReady.\n";
00118      
00119     while(true)
00120     {
00121         ros::spin();
00122     }
00123 
00124     return 0;
00125 }


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