Go to the documentation of this file.00001 #include <actasp/executors/MultiPolicyExecutor.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
00014 using namespace std;
00015
00016 namespace actasp {
00017
00018 MultiPolicyExecutor::MultiPolicyExecutor(AspKR* kr, MultiPlanner *planner, ActionSelector *selector,
00019 const std::map<std::string, Action * >& actionMap, double suboptimality) :
00020
00021 isGoalReached(false),
00022 hasFailed(false),
00023 actionCounter(0),
00024 newAction(true),
00025 active(NULL),
00026 kr(kr),
00027 planner(planner),
00028 goalRules(),
00029 policy(actionMapToSet(actionMap)),
00030 suboptimality(suboptimality),
00031 selector(selector),
00032 actionMap(),
00033 executionObservers() {
00034
00035 transform(actionMap.begin(),actionMap.end(),inserter(this->actionMap,this->actionMap.end()),ActionMapDeepCopy());
00036 }
00037
00038 MultiPolicyExecutor::~MultiPolicyExecutor() {
00039 delete active;
00040 for_each(actionMap.begin(),actionMap.end(),ActionMapDelete());
00041 }
00042
00043
00044 void MultiPolicyExecutor::setGoal(const std::vector<actasp::AspRule>& goalRules) throw() {
00045
00046 this->goalRules = goalRules;
00047
00048 isGoalReached = kr->currentStateQuery(goalRules).isSatisfied();
00049
00050 if (!isGoalReached)
00051 policy = planner->computePolicy(goalRules,suboptimality);
00052
00053 hasFailed = policy.empty();
00054 delete active;
00055 active = NULL;
00056 actionCounter = 0;
00057 newAction = true;
00058
00059 }
00060
00061 bool MultiPolicyExecutor::goalReached() const throw() {
00062 return isGoalReached;
00063 }
00064 bool MultiPolicyExecutor::failed() const throw() {
00065 return hasFailed;
00066 }
00067
00068 static Action *instantiateAction(const std::map<std::string, Action * >& actionMap, const AspFluent &actionFluent) {
00069 map<string, Action * >::const_iterator action = actionMap.find(actionFluent.getName());
00070
00071 if(action == actionMap.end())
00072 throw logic_error("MultiPolicyExecutor: no action with name " + actionFluent.getName());
00073
00074 return action->second->cloneAndInit(actionFluent);
00075 }
00076
00077
00078 void MultiPolicyExecutor::executeActionStep() {
00079 if (isGoalReached || hasFailed)
00080 return;
00081
00082 if (active != NULL && !active->hasFinished()) {
00083
00084 if (newAction) {
00085 for_each(executionObservers.begin(),executionObservers.end(),NotifyActionStart(active->toFluent(actionCounter)));
00086 newAction = false;
00087 }
00088
00089 active->run();
00090
00091 } else {
00092
00093
00094 if (active != NULL) {
00095 for_each(executionObservers.begin(),executionObservers.end(),NotifyActionTermination(active->toFluent(actionCounter++)));
00096 }
00097
00098 isGoalReached = kr->currentStateQuery(goalRules).isSatisfied();
00099
00100 if (isGoalReached)
00101 return;
00102
00103
00104 AnswerSet currentState = kr->currentStateQuery(vector<AspRule>());
00105 set<AspFluent> state(currentState.getFluents().begin(), currentState.getFluents().end());
00106 ActionSet options = policy.actions(state);
00107
00108 if (options.empty() || (active != NULL && active->hasFailed())) {
00109
00110
00111
00112
00113
00114 MultiPolicy otherPolicy = planner->computePolicy(goalRules,suboptimality);
00115 policy.merge(otherPolicy);
00116
00117 options = policy.actions(state);
00118 if (options.empty()) {
00119 hasFailed = true;
00120 return;
00121 }
00122 }
00123
00124 set<AspFluent>::const_iterator chosen = selector->choose(options);
00125
00126 delete active;
00127 active = instantiateAction(actionMap,*chosen);
00128 actionCounter++;
00129 newAction = true;
00130
00131
00132 }
00133
00134 }
00135
00136 void MultiPolicyExecutor::addExecutionObserver(ExecutionObserver *observer) throw() {
00137 executionObservers.push_back(observer);
00138 }
00139
00140 void MultiPolicyExecutor::removeExecutionObserver(ExecutionObserver *observer) throw() {
00141 executionObservers.remove(observer);
00142 }
00143
00144 }