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
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
00046 this->board_plan = new_cfg.board_plan;
00047
00048
00049 if (file_changed) {
00050 initializePlan();
00051 }
00052
00053
00054 this->config_=new_cfg;
00055
00056 this->unlock();
00057 }
00058
00059
00060 void PradaPlannerAlgorithm::initializePlan()
00061 {
00062
00063
00064
00065
00066
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
00075
00076 ROS_INFO("Reading reward");
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 reward = new relational::MaximizeReward(relational::Literal::get("numClean()"));
00087
00088
00089
00090
00091
00092
00093
00094
00095 ROS_INFO("Reading rules.");
00096
00097
00098 rules = new relational::RuleSet();
00099 relational::RuleSet::read(conf_file_rules.c_str(), *rules);
00100
00101 #if 0
00102
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
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
00133
00134
00135
00136 relational::RuleSet ground_rules;
00137
00138 relational::RuleSet::ground_with_filtering(ground_rules, *rules, relational::reason::getConstants(), *state);
00139
00140
00141
00142
00143
00144 if (planner!= NULL) {
00145 delete planner;
00146 }
00147 planner = new relational::A_PRADA();
00148
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
00163 relational::Literal* action;
00164
00165 ROS_INFO("Planning...");
00166
00167 planner->setHorizon(plan_length);
00168 action = planner->plan_action(*state);
00169
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
00177
00178
00179 this->unlock();
00180 return action;
00181 }
00182
00183 relational::LitL PradaPlannerAlgorithm::getCompletePlan(uint32_t plan_length) {
00184 this->lock();
00185
00186 relational::LitL plan;
00187 double value;
00188
00189 ROS_DEBUG_STREAM("PRADA new horizon " << plan_length);
00190 planner->setHorizon(plan_length);
00191
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
00204
00205 return std::min((int32_t)(ellipses_size*ellipses_size) + 4, this->max_prada_horizon);
00206 }
00207 else{
00208
00209
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