Go to the documentation of this file.00001
00002 #include "msgs_utils.h"
00003 #include "RemoteReasoner.h"
00004 #include "StaticFacts.h"
00005
00006 #include "learning/QLearningActionSelector.h"
00007 #include "learning/TimeReward.h"
00008 #include "learning/DefaultTimes.h"
00009
00010 #include "bwi_kr_execution/ExecutePlanAction.h"
00011
00012 #include "actasp/action_utils.h"
00013 #include "actasp/executors/MultiPolicyExecutor.h"
00014
00015 #include "actions/ActionFactory.h"
00016 #include "actions/LogicalNavigation.h"
00017
00018
00019 #include <actionlib/server/simple_action_server.h>
00020
00021 #include <ros/ros.h>
00022 #include <ros/package.h>
00023 #include <ros/console.h>
00024
00025 #include <boost/filesystem.hpp>
00026
00027 #include <string>
00028 #include <fstream>
00029
00030 const int MAX_N = 20;
00031 const std::string queryDirectory("/tmp/bwi_action_execution/");
00032 std::string valueDirectory;
00033
00034
00035 using namespace std;
00036 using namespace bwi_krexec;
00037 using namespace actasp;
00038
00039 typedef actionlib::SimpleActionServer<bwi_kr_execution::ExecutePlanAction> Server;
00040
00041
00042 ActionExecutor *executor;
00043 QLearningActionSelector *selector;
00044
00045 struct PrintFluent {
00046
00047 PrintFluent(ostream& stream) : stream(stream) {}
00048
00049 string operator()(const AspFluent& fluent) {
00050 stream << fluent.toString() << " ";
00051 }
00052
00053 ostream &stream;
00054
00055 };
00056
00057 struct Observer : public ExecutionObserver {
00058
00059 void actionStarted(const AspFluent& action) throw() {
00060 ROS_INFO_STREAM("Starting execution: " << action.toString());
00061 }
00062
00063 void actionTerminated(const AspFluent& action) throw() {
00064 ROS_INFO_STREAM("Terminating execution: " << action.toString());
00065 }
00066
00067 };
00068
00069 std::string rulesToFileName(const vector<AspRule> &rules) {
00070
00071 stringstream ruleString;
00072 vector<string> ruleStringVector;
00073
00074
00075
00076 vector<AspRule>::const_iterator ruleIt = rules.begin();
00077
00078 for(; ruleIt != rules.end(); ++ruleIt) {
00079
00080 vector<AspFluent>::const_iterator headIt = ruleIt->head.begin();
00081
00082 for(; headIt != ruleIt->head.end(); ++headIt)
00083 ruleString << headIt->toString(0);
00084
00085 ruleString << ":-";
00086
00087 vector<AspFluent>::const_iterator bodyIt = ruleIt->body.begin();
00088
00089 for(; bodyIt != ruleIt->body.end(); ++bodyIt)
00090 ruleString << bodyIt->toString(0);
00091
00092 ruleString << "--";
00093
00094 ruleStringVector.push_back(ruleString.str());
00095 ruleString.str(string());
00096 }
00097
00098 sort(ruleStringVector.begin(), ruleStringVector.end());
00099
00100 vector<string>::const_iterator stringIt = ruleStringVector.begin();
00101 for(; stringIt != ruleStringVector.end(); ++stringIt)
00102 ruleString << *stringIt;
00103
00104 return valueDirectory + ruleString.str();
00105 }
00106
00107 void executePlan(const bwi_kr_execution::ExecutePlanGoalConstPtr& plan, Server* as) {
00108
00109 vector<AspRule> goalRules;
00110
00111 transform(plan->aspGoal.begin(),plan->aspGoal.end(),back_inserter(goalRules),TranslateRule());
00112
00113 string valueFileName = rulesToFileName(goalRules);
00114 ifstream valueFileIn(valueFileName.c_str());
00115 selector->readFrom(valueFileIn);
00116 valueFileIn.close();
00117
00118 executor->setGoal(goalRules);
00119
00120 ros::Rate loop(10);
00121
00122 while (!executor->goalReached() && !executor->failed() && ros::ok()) {
00123
00124 if (!as->isPreemptRequested()) {
00125 executor->executeActionStep();
00126 } else {
00127 if (as->isNewGoalAvailable()) {
00128
00129 ofstream newValueFileOut(valueFileName.c_str());
00130 selector->writeTo(newValueFileOut);
00131 newValueFileOut.close();
00132
00133 goalRules.clear();
00134 const bwi_kr_execution::ExecutePlanGoalConstPtr& newGoal = as->acceptNewGoal();
00135 transform(newGoal->aspGoal.begin(),newGoal->aspGoal.end(),back_inserter(goalRules),TranslateRule());
00136
00137 valueFileName = rulesToFileName(goalRules);
00138 ifstream newValueFileIn(valueFileName.c_str());
00139 selector->readFrom(newValueFileIn);
00140 newValueFileIn.close();
00141
00142 executor->setGoal(goalRules);
00143 selector->episodeEnded();
00144 }
00145 }
00146 loop.sleep();
00147 }
00148
00149 selector->episodeEnded();
00150
00151 ofstream valueFileOut(valueFileName.c_str());
00152 selector->writeTo(valueFileOut);
00153 valueFileOut.close();
00154
00155 if (executor->goalReached()) {
00156 ROS_INFO("Execution succeded");
00157 as->setSucceeded();
00158 } else {
00159 ROS_INFO("Execution failed");
00160 as->setAborted();
00161 }
00162 }
00163
00164
00165
00166
00167
00168 int main(int argc, char**argv) {
00169 ros::init(argc, argv, "action_executor");
00170 ros::NodeHandle n;
00171
00172 if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug)) {
00173 ros::console::notifyLoggerLevelsChanged();
00174 }
00175
00176 ros::NodeHandle privateNode("~");
00177 string domainDirectory;
00178 n.param<std::string>("bwi_kr_execution/domain_directory", domainDirectory, ros::package::getPath("bwi_kr_execution")+"/domain/");
00179
00180 if (domainDirectory.at(domainDirectory.size()-1) != '/')
00181 domainDirectory += '/';
00182
00183
00184 LogicalNavigation setInitialState("noop");
00185 setInitialState.run();
00186
00187
00188
00189 bool simulating;
00190 privateNode.param<bool>("simulation",simulating,false);
00191
00192 valueDirectory = ros::package::getPath("bwi_kr_execution") +((simulating)? "/values_simulation/" : "/values/" ) ;
00193 boost::filesystem::create_directories(valueDirectory);
00194
00195 ActionFactory::setSimulation(simulating);
00196
00197 boost::filesystem::create_directories(queryDirectory);
00198
00199 AspKR *reasoner = new RemoteReasoner(MAX_N,queryDirectory,domainDirectory,actionMapToSet(ActionFactory::actions()),5);
00200 StaticFacts::retrieveStaticFacts(reasoner, domainDirectory);
00201
00202 TimeReward<QLearningActionSelector::State> *reward = new TimeReward<QLearningActionSelector::State>();
00203 DefaultActionValue *timeValue = new DefaultTimes();
00204
00205 selector = new QLearningActionSelector(0.3, reward , reasoner,timeValue);
00206
00207
00208 executor = new MultiPolicyExecutor(reasoner, reasoner,selector , ActionFactory::actions(),1.5);
00209
00210 executor->addExecutionObserver(selector);
00211 executor->addExecutionObserver(reward);
00212
00213 Observer observer;
00214 executor->addExecutionObserver(&observer);
00215
00216 Server server(privateNode, "execute_plan", boost::bind(&executePlan, _1, &server), false);
00217 server.start();
00218
00219 ros::spin();
00220
00221 server.shutdown();
00222
00223 delete executor;
00224 delete selector;
00225 delete timeValue;
00226 delete reward;
00227 delete reasoner;
00228
00229 return 0;
00230 }