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 AnswerSet& plan) throw(logic_error) {
00026 
00027 
00028   //ignore the last time step becuase it's the final state and has no actions
00029   unsigned int planLength = plan.maxTimeStep()-1;
00030 
00031         for (int timeStep = 0; timeStep <= planLength; ++timeStep) {
00032                 
00033                 set<AspFluent> fluents = plan.getFluentsAtTime(timeStep);
00034     
00035     //find the action
00036     set<AspFluent>::iterator actionIt = find_if(fluents.begin(),fluents.end(),IsAnAction(allActions));
00037     
00038     if(actionIt == fluents.end())
00039       throw logic_error("MultiPolicy: no action for some state");
00040     
00041     AspFluent action = *actionIt;
00042                 
00043                 //remove the action from there
00044                 fluents.erase(actionIt);
00045                 
00046                 ActionSet &stateActions = policy[fluents]; //creates an empty vector if not present
00047 
00048                 stateActions.insert(action);
00049     
00050         }
00051 
00052 }
00053 
00054 struct MergeActions {
00055   MergeActions( std::map<std::set<AspFluent>, ActionSet, StateComparator<AspFluent> > &policy) : policy(policy) {}
00056  
00057  void operator()(const std::pair<set<AspFluent>, ActionSet >& stateActions) {
00058   
00059    map<set<AspFluent>, ActionSet >::iterator found = policy.find(stateActions.first);
00060    if(found == policy.end())
00061      policy.insert(stateActions);
00062    
00063    else {
00064      found->second.insert(stateActions.second.begin(),stateActions.second.end());
00065    }
00066      
00067    
00068  }
00069  
00070   std::map<std::set<AspFluent>, ActionSet, StateComparator<AspFluent> > &policy;
00071 };
00072 
00073 void MultiPolicy::merge(const MultiPolicy& otherPolicy) {
00074   
00075   set_union(otherPolicy.allActions.begin(),otherPolicy.allActions.end(),
00076                  allActions.begin(),allActions.end(),
00077                  inserter(allActions,allActions.begin()));
00078   
00079   for_each(otherPolicy.policy.begin(),otherPolicy.policy.end(),MergeActions(policy));
00080 }
00081 
00082 bool MultiPolicy::empty() const throw() {
00083         return policy.empty();
00084 }
00085 
00086 
00087 
00088 }


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Fri Aug 28 2015 10:14:46