prada_planner_alg.cpp
Go to the documentation of this file.
00001 #include "prada_planner_alg.h"
00002 
00003 PradaPlannerAlgorithm::PradaPlannerAlgorithm(void)
00004 {
00005         state = NULL;
00006         planner = NULL;
00007         rules = NULL;
00008         reward = NULL;
00009         srand (time(NULL));
00010         rnd.seed(rand());
00011 }
00012 
00013 PradaPlannerAlgorithm::~PradaPlannerAlgorithm(void)
00014 {
00015 }
00016 
00017 void PradaPlannerAlgorithm::config_update(Config& new_cfg, uint32_t level)
00018 {
00019         bool file_changed = false;
00020         this->lock();
00021 
00022         if (this->config_.conf_file_language.compare(new_cfg.conf_file_language) != 0) {
00023                 this->conf_file_language = new_cfg.conf_file_language;
00024                 file_changed = true;
00025         }
00026         if (this->config_.conf_file_reward.compare(new_cfg.conf_file_reward) != 0) {
00027                 this->conf_file_reward = new_cfg.conf_file_reward;
00028                 file_changed = true;
00029         }
00030         if (this->config_.conf_file_rules.compare(new_cfg.conf_file_rules) != 0) {
00031                 this->conf_file_rules = new_cfg.conf_file_rules;
00032                 file_changed = true;
00033         }
00034         if (this->config_.conf_file_state.compare(new_cfg.conf_file_state) != 0) {
00035                 this->conf_file_state = new_cfg.conf_file_state;
00036                 file_changed = true;
00037         }
00038         
00039         // prada
00040         this->max_prada_horizon = new_cfg.max_prada_horizon;
00041         this->prada_discount_factor = new_cfg.prada_discount_factor;
00042         this->prada_num_samples = new_cfg.prada_num_samples;
00043         this->prada_noise_softener = new_cfg.prada_noise_softener;
00044         
00045         // true if erasing a board
00046         this->board_plan = new_cfg.board_plan;
00047         
00048         // Get new config
00049         if (file_changed) {
00050                 initializePlan();
00051         }
00052         
00053         // save the current configuration
00054         this->config_=new_cfg;
00055         
00056         this->unlock();
00057 }
00058 
00059 // PradaPlannerAlgorithm Public API
00060 void PradaPlannerAlgorithm::initializePlan()
00061 {
00062         // -------------------------------------
00063         //  SET UP LOGIC
00064         // -------------------------------------
00065         
00066         // Set up logic
00067         ROS_INFO("Reading symbols.");
00068         relational::SymL symbols;
00069         relational::ArgumentTypeL types;
00070         relational::readSymbolsAndTypes(symbols, types, conf_file_language.c_str());
00071 
00072 
00073         // -------------------------------------
00074         // REWARD
00075         // -------------------------------------
00076         ROS_INFO("Reading reward");
00077         // TODO PRADA doesn't support yet reading rewards from a file
00078         // reward = relational::Reward::read(conf_file_reward.c_str());
00079         
00080         // Workaround for literal list
00081 //      relational::LitL lits_reward;
00082 //      relational::Literal::get(lits_reward, MT::String(conf_file_reward.c_str()));
00083 //      reward = new relational::LiteralListReward(lits_reward);
00084         
00085         // Workaround for maximize reward
00086         reward = new relational::MaximizeReward(relational::Literal::get("numClean()"));
00087            
00088         
00089         
00090         
00091         // -------------------------------------
00092         // RULES
00093         // -------------------------------------
00094         
00095         ROS_INFO("Reading rules.");
00096         // TODO check
00097         //delete rules;
00098         rules = new relational::RuleSet();
00099         relational::RuleSet::read(conf_file_rules.c_str(), *rules);
00100         
00101         #if  0
00102         // Manually create "doNothing"-rule if desired
00103         relational::Rule* rule_doNothing = relational::Rule::getDoNothingRule();
00104         rules.append(rule_doNothing);
00105         rule_doNothing->write();
00106         #endif
00107         
00108         ROS_INFO("Finished reading configuration.");
00109 }
00110 
00111 void PradaPlannerAlgorithm::setPlanStateFromFile()
00112 {
00113         if (state != NULL) {
00114                 delete state;
00115         }
00116         state = new relational::SymbolicState();
00117 //         relational::SymbolicState state;
00118         
00119         this->lock();
00120         ifstream in_state(this->conf_file_state.c_str());
00121         ROS_DEBUG("Reading state");
00122         state->read(in_state);
00123         relational::reason::setConstants(state->state_constants);
00124         ROS_DEBUG("State readed successfully");
00125         setupPlanner(state);
00126         
00127         this->unlock();
00128 }
00129 
00130 void PradaPlannerAlgorithm::setupPlanner(relational::SymbolicState *state) {
00131         // -------------------------------------
00132         // GROUND THE RULES
00133         // -------------------------------------
00134         
00135         //relational::RuleSet ground_rules;
00136         relational::RuleSet ground_rules;
00137 //      ground_rules = new relational::RuleSet;
00138         relational::RuleSet::ground_with_filtering(ground_rules, *rules, relational::reason::getConstants(), *state);
00139         
00140         
00141         // -------------------------------------
00142         // PLANNER
00143         // -------------------------------------
00144         if (planner!= NULL) {
00145                 delete planner;
00146         }
00147         planner = new relational::A_PRADA();
00148         // PRADA parameters
00149         ((relational::A_PRADA* ) planner)->setNumberOfSamples(prada_num_samples);
00150         ((relational::A_PRADA* ) planner)->setNoiseSoftener(prada_noise_softener);
00151         planner->setDiscount(prada_discount_factor);
00152         planner->setHorizon(max_prada_horizon);
00153         planner->setReward(reward);
00154         planner->setGroundRules(ground_rules);
00155         
00156         
00157 }
00158 
00159 
00160 relational::Literal* PradaPlannerAlgorithm::getSinglePlan(uint32_t plan_length) {
00161         this->lock();
00162 //      TL::Atom* action;
00163         relational::Literal* action;
00164         
00165         ROS_INFO("Planning...");
00166         
00167         planner->setHorizon(plan_length);
00168         action = planner->plan_action(*state);
00169 //      action = planner->generateAction(*state);
00170         if (action){
00171                 ROS_INFO_STREAM("The planner would like to kindly recommend the following action to you:"<<endl<<*action<<endl);
00172         }
00173         else{
00174                 ROS_INFO("Planning failed!");
00175         }
00176         //std::stringstream ss;
00177         //ss << *action;
00178         
00179         this->unlock();
00180         return action;
00181 }
00182 
00183 relational::LitL PradaPlannerAlgorithm::getCompletePlan(uint32_t plan_length) {
00184         this->lock();
00185 //      AtomA plan;
00186         relational::LitL plan;
00187         double value;
00188         
00189         ROS_DEBUG_STREAM("PRADA new horizon " << plan_length);
00190         planner->setHorizon(plan_length);
00191 //      ((relational::PRADA*) planner)->generatePlan(plan, value, *state);
00192         ((relational::PRADA_Planner*) planner)->plan_full(plan, value, *state);
00193         cout<<endl;
00194         cout<<"Also, it would like to kindly recommend the following plan with value "<<value<<" to you:"<<endl<<plan<<endl;
00195         
00196         this->unlock();
00197         return plan;
00198 }
00199 
00200 uint32_t PradaPlannerAlgorithm::getRecommendedPlanLength(uint32_t ellipses_size)
00201 {
00202         if (this->board_plan) {
00203                 // plan length is (#ellipses)^2 + 4
00204                 // max_planning_horizon is a parameter to define max plan length
00205                 return std::min((int32_t)(ellipses_size*ellipses_size) + 4, this->max_prada_horizon);
00206         }
00207         else{
00208                 // plan length is (#ellipses)*3
00209                 // max_planning_horizon is a parameter to define max plan length
00210                 return std::min((int32_t)(ellipses_size*3), this->max_prada_horizon);
00211         }
00212 }
00213 
00214 void PradaPlannerAlgorithm::reloadRules()
00215 {
00216         ROS_DEBUG("Reading rules.");
00217         delete rules;
00218         rules = new relational::RuleSet();
00219         relational::RuleSet::read(conf_file_rules.c_str(), *rules);
00220 }
00221 


iri_prada_planner
Author(s): dmartinez
autogenerated on Fri Dec 6 2013 23:36:33