FilteringReasoner.cpp
Go to the documentation of this file.
00001 #include <actasp/reasoners/FilteringReasoner.h>
00002 
00003 #include <actasp/FilteringQueryGenerator.h>
00004 
00005 #include <vector>
00006 #include <sstream>
00007 #include <boost/graph/graph_concepts.hpp>
00008 
00009 
00010 using namespace std;
00011 
00012 namespace actasp {
00013   
00014 
00015 FilteringReasoner::FilteringReasoner(FilteringQueryGenerator *actualReasoner,unsigned int max_n,const ActionSet& allActions) :
00016   Reasoner(actualReasoner,max_n,allActions),
00017   clingo(actualReasoner) {}
00018 
00019 
00020 GraphPolicy *FilteringReasoner::computePolicy(const std::vector<actasp::AspRule>& goal, double suboptimality) const throw (std::logic_error) {
00021 
00022   GraphPolicy *p = new GraphPolicy(allActions);
00023   computePolicyHelper(goal,suboptimality,p);
00024   return p;
00025 }
00026 
00027 
00028 struct AnswerSetStateComparator {
00029   bool operator()(const AnswerSet &first, const AnswerSet& second) const {
00030     return first.getFluentsAtTime(0).size() < second.getFluentsAtTime(0).size();
00031   }
00032 
00033 };
00034 
00035 //Filters the state fluents to only keep the ones necessary for the purpose of the plans:
00036 // plans = vector of list of actions (example: "north(1)" , "east(2)" ..)
00037 // goals = vector of fluents that have to be true at the final step (example: "not pos(3, 9, n)" to mean "be there"..)
00038 //domain needs to have #show for fluents and actions.
00039 // It is possible to add a "nofilter.txt" file in the domain directory to specify which fluents (name/arity) have to stay there no matter what.
00040 AnswerSet FilteringReasoner::filterState(const std::vector<actasp::AnswerSet>& plans, const std::vector<actasp::AspRule>& goals) {
00041 
00042   //input checking:
00043   if (plans.empty() || goals.empty()) {
00044     return AnswerSet();
00045   }
00046 
00047   //get all the known fluents of the current state:
00048   AnswerSet currentState = Reasoner::currentStateQuery(vector<AspRule>());
00049 
00050   set<AspFluent> result;
00051 
00052   std::vector<actasp::AnswerSet>::const_iterator planIt = plans.begin();
00053   for (; planIt != plans.end(); ++planIt) {
00054     //make a query that only uses the domain and what I created, not current.asp
00055     std::list<actasp::AnswerSet> listResult = clingo->filteringQuery(currentState,*planIt,goals);
00056     if (!listResult.empty()) {
00057       std::list<actasp::AnswerSet>::const_iterator best = min_element(listResult.begin(), listResult.end(),AnswerSetStateComparator());
00058       set<AspFluent> statezero = best->getFluentsAtTime(0);
00059       set<AspFluent>::const_iterator statezeroIt = statezero.begin();
00060       for (;statezeroIt != statezero.end(); ++statezeroIt) {
00061         result.insert(*statezeroIt);
00062       }
00063     }
00064 
00065   }
00066 
00067   return AnswerSet(result.begin(), result.end());
00068 
00069 } //end of Clingo::filterState method
00070 
00071 }


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