PartialPolicyExecutor.cpp
Go to the documentation of this file.
00001 #include <actasp/executors/PartialPolicyExecutor.h>
00002 
00003 #include <actasp/AspKR.h>
00004 #include <actasp/MultiPlanner.h>
00005 #include <actasp/AspRule.h>
00006 #include <actasp/ActionSelector.h>
00007 #include <actasp/Action.h>
00008 #include <actasp/execution_observer_utils.h>
00009 #include <actasp/action_utils.h>
00010 
00011 #include <algorithm>
00012 #include <iterator>
00013 #include <functional>
00014 
00015 using namespace std;
00016 
00017 namespace actasp {
00018 
00019 PartialPolicyExecutor::PartialPolicyExecutor(AspKR* kr, MultiPlanner *planner, ActionSelector *selector, 
00020                       const std::map<std::string, Action * >& actionMap, double suboptimality) :
00021                     
00022                     isGoalReached(false),
00023                     hasFailed(false),
00024                     actionCounter(0),
00025                     newAction(true),
00026                     active(NULL),
00027                     kr(kr),
00028                     planner(planner),
00029                     goalRules(),
00030                     policy(NULL),
00031                     suboptimality(suboptimality),
00032                     selector(selector),
00033                     actionMap(),
00034                     executionObservers() {
00035 
00036   transform(actionMap.begin(),actionMap.end(),inserter(this->actionMap,this->actionMap.end()),ActionMapDeepCopy());
00037 }
00038 
00039 PartialPolicyExecutor::~PartialPolicyExecutor() {
00040   delete active;
00041   for_each(actionMap.begin(),actionMap.end(),ActionMapDelete());
00042   delete policy;
00043 }
00044   
00045   
00046 void  PartialPolicyExecutor::setGoal(const std::vector<actasp::AspRule>& goalRules) throw() {
00047 
00048   this->goalRules = goalRules;
00049 
00050   isGoalReached = kr->currentStateQuery(goalRules).isSatisfied();
00051 
00052   if (!isGoalReached) {
00053     delete policy;
00054     policy = planner->computePolicy(goalRules,suboptimality);
00055     //TODO do the same for the other notifications, and get rid of helper classes?
00056     for_each(executionObservers.begin(),executionObservers.end(),bind2nd(mem_fun(&ExecutionObserver::policyChanged),policy));
00057   }
00058 
00059   hasFailed = (policy!= NULL) && (policy->empty());
00060   delete active;
00061   active = NULL;
00062   actionCounter = 0;
00063   newAction = true;
00064   
00065   for_each(executionObservers.begin(),executionObservers.end(),NotifyGoalChanged(goalRules));
00066 
00067 }
00068 
00069 bool PartialPolicyExecutor::goalReached() const throw() {
00070   return isGoalReached;
00071 }
00072 bool PartialPolicyExecutor::failed() const throw() {
00073   return hasFailed;
00074 }
00075 
00076 static Action *instantiateAction(const std::map<std::string, Action * >& actionMap, const AspFluent &actionFluent) {
00077   map<string, Action * >::const_iterator action = actionMap.find(actionFluent.getName());
00078   
00079   if(action == actionMap.end())
00080     throw logic_error("MultiPolicyExecutor: no action with name " + actionFluent.getName());
00081   
00082   return action->second->cloneAndInit(actionFluent);
00083 }
00084 
00085 
00086 void PartialPolicyExecutor::executeActionStep() {
00087   if (isGoalReached || hasFailed)
00088     return;
00089   
00090   if (active != NULL && !active->hasFinished()) {
00091     
00092     if (newAction) {
00093       for_each(executionObservers.begin(),executionObservers.end(),NotifyActionStart(active->toFluent(actionCounter)));
00094       newAction = false;
00095     } 
00096   
00097     active->run();
00098 
00099   } else {
00100     
00101 
00102     if (active != NULL) {
00103       for_each(executionObservers.begin(),executionObservers.end(),NotifyActionTermination(active->toFluent(actionCounter++)));
00104     }
00105 
00106     isGoalReached = kr->currentStateQuery(goalRules).isSatisfied();
00107 
00108     if (isGoalReached) //well done!
00109       return;
00110 
00111     //choose the next action
00112     AnswerSet currentState = kr->currentStateQuery(vector<AspRule>());
00113     set<AspFluent> state(currentState.getFluents().begin(), currentState.getFluents().end());
00114     ActionSet options = policy->actions(state);
00115 
00116     if (options.empty() || (active != NULL &&  active->hasFailed())) {
00117       //there's no action for this state, computing more plans
00118 
00119       //if the last action failed, we may want to have some more option
00120 
00121       PartialPolicy *otherPolicy = planner->computePolicy(goalRules,suboptimality);
00122       policy->merge(otherPolicy);
00123       delete otherPolicy;
00124       for_each(executionObservers.begin(),executionObservers.end(),bind2nd(mem_fun(&ExecutionObserver::policyChanged),policy));
00125 
00126       options = policy->actions(state);
00127       if (options.empty()) { //no actions available from here!
00128         hasFailed = true;
00129         return;
00130       }
00131     }
00132 
00133     set<AspFluent>::const_iterator chosen = selector->choose(options);
00134 
00135     delete active;
00136     active = instantiateAction(actionMap,*chosen);
00137     actionCounter++;
00138     newAction = true;
00139 
00140 
00141   }
00142 
00143 }
00144 
00145 void PartialPolicyExecutor::addExecutionObserver(ExecutionObserver *observer) throw() {
00146   executionObservers.push_back(observer);
00147 }
00148 
00149 void PartialPolicyExecutor::removeExecutionObserver(ExecutionObserver *observer) throw() {
00150   executionObservers.remove(observer);
00151 }
00152 
00153 }


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Thu Jun 6 2019 17:57:37