MultiPolicy.cpp
Go to the documentation of this file.
00001 #include <actasp/MultiPolicy.h>
00002 
00003 #include <actasp/Action.h>
00004 #include <actasp/action_utils.h>
00005 
00006 #include <algorithm>
00007 
00008 using namespace std;
00009 
00010 namespace actasp {
00011   
00012 MultiPolicy::MultiPolicy(const ActionSet& actions) : policy(), allActions(actions) {}
00013         
00014 ActionSet MultiPolicy::actions(const std::set<AspFluent>& state) const throw() {
00015   
00016         std::map<set<AspFluent>, ActionSet >::const_iterator acts = policy.find(state);
00017 
00018         if(acts != policy.end()) {
00019                 return acts->second;
00020         }
00021         
00022         return ActionSet();
00023 }
00024 
00025 void MultiPolicy::merge(const PartialPolicy* otherPolicy) {
00026     const MultiPolicy *other = dynamic_cast<const MultiPolicy*>(otherPolicy);
00027     if(other != NULL)
00028       merge(other);
00029     else 
00030       throw runtime_error("method not implemented for a partial policy other than MultiPolicy");
00031 }
00032 
00033 void MultiPolicy::merge(const AnswerSet& plan) throw(logic_error) {
00034 
00035 
00036   //ignore the last time step becuase it's the final state and has no actions
00037   unsigned int planLength = plan.maxTimeStep();
00038 
00039   set<AspFluent> state = plan.getFluentsAtTime(0);
00040   
00041         for (int timeStep = 1; timeStep <=planLength; ++timeStep) {
00042                 
00043                 set<AspFluent> stateWithAction = plan.getFluentsAtTime(timeStep);
00044     
00045     //find the action
00046     set<AspFluent>::iterator actionIt = find_if(stateWithAction.begin(),stateWithAction.end(),IsAnAction(allActions));
00047     
00048     if(actionIt == stateWithAction.end())
00049       throw logic_error("MultiPolicy: no action for some state");
00050     
00051     AspFluent action = *actionIt;
00052                 
00053                 //remove the action from there
00054                 stateWithAction.erase(actionIt);
00055                 
00056                 ActionSet &stateActions = policy[state]; //creates an empty vector if not present
00057 
00058                 stateActions.insert(action);
00059     
00060     //TODO check this
00061     state = stateWithAction;
00062     
00063         }
00064 
00065 }
00066 
00067 struct MergeActions {
00068   MergeActions( std::map<std::set<AspFluent>, ActionSet, StateComparator<AspFluent> > &policy) : policy(policy) {}
00069  
00070  void operator()(const std::pair<set<AspFluent>, ActionSet >& stateActions) {
00071   
00072    map<set<AspFluent>, ActionSet >::iterator found = policy.find(stateActions.first);
00073    if(found == policy.end())
00074      policy.insert(stateActions);
00075    
00076    else {
00077      found->second.insert(stateActions.second.begin(),stateActions.second.end());
00078    }
00079      
00080    
00081  }
00082  
00083   std::map<std::set<AspFluent>, ActionSet, StateComparator<AspFluent> > &policy;
00084 };
00085 
00086 void MultiPolicy::merge(const MultiPolicy* otherPolicy) {
00087   
00088   set_union(otherPolicy->allActions.begin(),otherPolicy->allActions.end(),
00089                  allActions.begin(),allActions.end(),
00090                  inserter(allActions,allActions.begin()));
00091   
00092   for_each(otherPolicy->policy.begin(),otherPolicy->policy.end(),MergeActions(policy));
00093 }
00094 
00095 bool MultiPolicy::empty() const throw() {
00096         return policy.empty();
00097 }
00098 
00099 
00100 
00101 }


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