MultiAgentDecisionProcess  Release 0.2.1
PlanningUnitMADPDiscrete.h
Go to the documentation of this file.
1 
28 /* Only include this header file once. */
29 #ifndef _PLANNINGUNITMADPDISCRETE_H_
30 #define _PLANNINGUNITMADPDISCRETE_H_ 1
31 
32 /* the include directives */
33 #include <iostream>
34 #include <queue>
35 #include <cmath>
36 #include "Globals.h"
37 #include "PlanningUnit.h"
41 #include "Referrer.h"
42 
43 // forward declarations, very important in this class, as it will be
44 // included by many many others
45 class JointBelief;
46 class JointBeliefSparse;
49 
50 class ActionHistory;
51 class JointActionHistory;
52 class ObservationHistory;
60 
61 #include "ActionHistoryTree.h"
62 #include "JointActionHistoryTree.h"
63 
69  public PlanningUnit,
71  public Referrer<MultiAgentDecisionProcessDiscreteInterface>
72 {
73 private:
75  void Initialize();
76 
78  void Deinitialize();
79 
82 
85 
93 
101 
110 
113 
121 
137 
140 
155  agentI);
159 
169 
172 
173 protected:
174 
176  virtual bool SanityCheck() const;
177 
178  //Counters that keep track of the number of histories
179 
182  std::vector<size_t> _m_nrObservationHistories;
187  std::vector< std::vector<size_t> > _m_nrObservationHistoriesT;
189  std::vector<size_t> _m_nrJointObservationHistoriesT;
192  std::vector<std::vector<LIndex> > _m_firstOHIforT;
195  std::vector<LIndex> _m_firstJOHIforT;
196 
199  std::vector<size_t> _m_nrActionHistories;
204  std::vector< std::vector<size_t> > _m_nrActionHistoriesT;
206  std::vector<size_t> _m_nrJointActionHistoriesT;
209  std::vector<std::vector<LIndex> > _m_firstAHIforT;
212  std::vector<LIndex> _m_firstJAHIforT;
213 
216  std::vector<size_t> _m_nrActionObservationHistories;
221  std::vector< std::vector<size_t> > _m_nrActionObservationHistoriesT;
226  std::vector<std::vector<LIndex> > _m_firstAOHIforT;
229  std::vector<LIndex> _m_firstJAOHIforT;
230 
231  //Storage of the actual histories:
232 
235  std::vector<ObservationHistoryTree *> _m_observationHistoryTreeRootPointers;
239  std::vector<std::vector<ObservationHistoryTree*> > _m_observationHistoryTreeVectors;
246  std::vector<JointObservationHistoryTree*> _m_jointObservationHistoryTreeVector;
247 
250  std::vector<ActionHistoryTree *> _m_actionHistoryTreeRootPointers;
255  std::vector<std::vector<ActionHistoryTree*> > _m_actionHistoryTreeVectors;
262  std::vector<JointActionHistoryTree*> _m_jointActionHistoryTreeVector;
263 
266  std::vector< ActionObservationHistoryTree *>
273  std::vector<std::vector<ActionObservationHistoryTree*> >
275 
283  std::vector<JointActionObservationHistoryTree*>
285 
289  std::map< Index, JointActionObservationHistoryTree*>
291 
292  //Storage of joint beliefs:
298  std::vector<const JointBeliefInterface*> _m_jBeliefCache;
300  std::vector<double> _m_jaohConditionalProbs;
307  std::vector<double> _m_jaohProbs;
308 
309 public:
310  // Constructor, destructor and copy assignment.
312 
316  size_t horizon=3,
318  );
320 
324  size_t horizon=3,
326  );
327 
330 
331  //operators:
332 
334  { return GetReferred(); }
336  { return GetReferred(); }
337 
338  //data manipulation (set) functions:
339 
341 
344 
345  void SetHorizon(size_t h);
346 
347  //get (data) functions:
348 
349  //general
350 
352  size_t GetNrAgents() const { return(PlanningUnit::GetNrAgents()); }
353 
356  {return(GetReferred());};
358  {return(GetReferred());};
360  size_t GetNrStates() const;
361 
363  const State* GetState(Index i) const
364  { return(GetReferred()->GetState(i)); }
365 
368  {return(GetReferred()->GetInitialStateProbability(sI));};
373 
376  { return(_m_params); }
377 
379 
380  void SetParams(const PlanningUnitMADPDiscreteParameters &params);
381 
382  //related to getting (info of) actions
383 
385  const std::vector<size_t>& GetNrActions() const
386  {return (GetReferred()->GetNrActions());}
388  size_t GetNrActions(Index agentI) const
389  {return (GetReferred()->GetNrActions(agentI));}
391  size_t GetNrJointActions() const
392  {return (GetReferred()->GetNrJointActions());}
393 
395  const Action* GetAction(Index agentI, Index a) const
396  {return(GetReferred()->GetAction(agentI, a));}
398  const JointAction* GetJointAction(Index jaI) const
399  {return (GetReferred()->GetJointAction(jaI));}
403  indivActionIndices) const
405  indivActionIndices));}
408  Index IndividualToJointActionIndices(const std::vector<Index>&
409  indivActionIndices) const
411  indivActionIndices));}
412 
415  std::vector<Index> JointToIndividualActionIndices(Index jaI) const;
416 
417 
418  //related to getting (info of) observations
419 
420 
422  const std::vector<size_t>& GetNrObservations() const
423  {return (GetReferred()->GetNrObservations());}
425  size_t GetNrObservations(Index agentI) const
426  {return (GetReferred()->GetNrObservations(agentI));};
428  size_t GetNrJointObservations() const
429  {return (GetReferred()->GetNrJointObservations());};
431  const Observation* GetObservation(Index agentI, Index o) const
432  {return(GetReferred()->GetObservation(agentI, o));}
435  const {return (GetReferred()->GetJointObservation(joI));}
438  Index IndividualToJointObservationIndices(std::vector<Index>& inObs) const
443  std::vector<Index> JointToIndividualObservationIndices(Index joI) const
445 
446  //related to getting histories:
459  Index ComputeHistoryIndex( Index t, Index t_offset,
460  //const std::vector<Index>& indices,
461  const Index indices [],
462  size_t indexDomainSize
463  ) const;
464 
479  void ComputeHistoryArrays(Index hI, Index t, Index t_offset,
480  Index Indices[], //output
481  size_t indexDomainSize
482  ) const;
483 
484 
485 
486  //related to getting (info of) observation histories
487 
491  const std::vector<Index>& JointToIndividualObservationHistoryIndicesRef(
492  Index johI) const;
495  std::vector<Index> JointToIndividualObservationHistoryIndices(Index johI)
496  const;
498 
506  const std::vector<Index>& indivIs) const;
507 
511  size_t GetNrObservationHistories(Index agentI) const;
514  const std::vector<size_t> GetNrObservationHistoriesVector() const;
519  size_t GetNrObservationHistories(Index agentI, Index ts) const
520  {return _m_nrObservationHistoriesT.at(agentI).at(ts);}
523  const std::vector<size_t> GetNrObservationHistoriesVector(Index ts) const;
529  {return _m_nrJointObservationHistoriesT.at(ts);}
532  {return _m_firstOHIforT.at(agI).at(ts);}
536  {return _m_firstJOHIforT.at(ts);}
538  Index GetTimeStepForOHI(Index agentI, Index ohI) const;
540  Index GetTimeStepForJOHI(Index johI) const;
544  Index GetSuccessorJOHI(Index johI, Index joI) const;
548  Index GetSuccessorOHI(Index agentI, Index ohI, Index oI) const;
549 
550 
553  Index ohI) const;
556  Index johI) const;
559  const;
567  const std::vector<Index>& observations) const;
576  const std::vector<Index>& jointObservations) const;
585  const Index jointObservations[] ) const;
586 
598  (Index johI, Index t, Index joIs[]) const;
599 
613  (Index agentI, Index ohI, Index t, Index oIs[]) const;
614 
615 
616 
617 
618  //related to getting (info of) action histories
619 
625  std::vector<Index> JointToIndividualActionHistoryIndices(Index JAHistI)
626  const;
633  const std::vector<Index>& JointToIndividualActionHistoryIndicesRef(Index
634  JAHistI) const;
636 
644  const std::vector<Index>& indivIs) const;
654  (Index agentI, Index ahI, Index t, Index aIs[]) const;
663  const std::vector<Index>& jointActions) const;
672  const Index jointActions[] ) const;
676  size_t GetNrActionHistories(Index agentI) const;
679  Index ohI) const;
682  Index jahI) const;
686  size_t GetNrJointActionHistories() const;
690  {return _m_firstJAOHIforT.at(ts);}
692  Index GetTimeStepForAHI(Index agentI, Index ohI) const;
694  Index GetTimeStepForJAHI(Index johI) const;
695  Index GetSuccessorJAHI(Index johI, Index joI) const;
696  Index GetSuccessorAHI(Index agentI, Index ohI, Index oI) const;
697 
698  //related to getting (info of) action-observation histories
699 
700 
704  const std::vector<Index>&
706  const;
711  Index jaohI) const;
712 
717  Index jaohI,
718  std::vector< std::vector<Index> >& indivO_vec,
719  std::vector< std::vector<Index> >& indivA_vec
720  ) const;
725  Index jaohI,
726  std::vector< std::vector<Index> >& indivAO_vec
727  ) const;
729 
737  const std::vector<Index>& indivIs) const;
738 
744  {return _m_nrActionObservationHistories.at(agentI);}
747  Index agentI, Index aohI) const
748  {return _m_actionObservationHistoryTreeVectors.at(agentI).at(aohI);}
749 
756 
761  Index agentI,
762  Index t,
763  const std::vector<Index>& actions,
764  const std::vector<Index>& observations
765  ) const;
766 
768  Index GetTimeStepForAOHI(Index agentI, Index aohI) const;
770  Index GetTimeStepForJAOHI(Index jaohI) const;
775  Index GetSuccessorAOHI(Index agI, Index aohI, Index aI, Index oI) const;
780  Index GetSuccessorJAOHI(Index jaohI, Index jaI, Index joI) const;
781 
785 
799  (Index agentI, Index aohI, Index t, Index aIs[], Index oIs[]) const;
800 
804 
811  Index t,
812  const std::vector<Index>& Jactions,
813  const std::vector<Index>& Jobservations
814  ) const;
815 
831  (Index jaohI, Index t, Index jaIs[], Index joIs[]) const;
840  std::vector<Index> &jaIs,
841  std::vector<Index> &joIs) const;
842 
843  //related to joint beliefs
844  //
860  virtual JointBeliefInterface * GetNewJointBeliefInterface(size_t size)
861  const;
862 
877 
884  double GetJAOHProbGivenPred(Index jaohI) const;
885 
890  double GetJAOHProb(
891  Index jaohI, // the jaohI for which we want to know the probs
892  Index p_jaohI = 0, // the GIVEN predecessor
893  const JointBeliefInterface* p_jb = NULL,// the corresponding GIVEN
894  // jb of p
895  const JointPolicyDiscrete * jpol = NULL // the policy followed in
896  ) const;
923  double GetJAOHProbs(
924  //output args:
926  //input args:
927  Index jaohI, // the jaohI for which we want to know the probs
928  Index p_jaohI = 0, // the GIVEN predecessor
929  const JointBeliefInterface* p_jb = NULL,// the corresponding GIVEN
930  // jb of p
931  const JointPolicyDiscrete * jpol = NULL // the policy followed in
932  ) const;
949  //input/output args:
951  //input args:
952  const Index jaIs[],
953  const Index joIs[],
954  Index t_p,
955  Index t,
956 // const Index jaohI, //the jaohI for which we want to know the probs
957  Index p_jaohI = 0, // the GIVEN predecessor
958 // const JointBelief* p_jb = NULL, // the corresponding GIVEN jb of p
959  const JointPolicyDiscrete * jpol = NULL // the policy followed in
960  ) const;
961 
962  //related to policies
964  LIndex GetNrPolicies(Index agentI) const;
965 
967  LIndex GetNrJointPolicies() const;
968 
969  //Getting probabilities and rewards
970 
972  double GetTransitionProbability(Index sI, Index jaI, Index sucSI)
973  const
974  { return(GetReferred()->GetTransitionProbability(sI, jaI, sucSI));};
976  double GetObservationProbability(Index jaI, Index sucSI, Index joI)
977  const
978  { return(GetReferred()->GetObservationProbability(jaI, sucSI, joI));};
979 
980  //other
982  void PrintActionHistories();
988  void Print();
989 
990  //functions for Interface_ProblemToPolicyDiscretePure
992  std::string SoftPrintObservationHistory(Index agentI, Index ohIndex) const;
993 
995  std::string SoftPrintAction(Index agentI, Index actionI) const;
996 
997  virtual bool AreCachedJointToIndivIndices(
999 
1001  {
1002 // std::cerr << "GetTransitionModelDiscretePtr deprecated, use GetProblem->GetTGet()"<<std::endl;
1004 
1007 
1008  //functions that implement the Interface_ProblemToPolicyDiscretePure
1010  std::vector<Index> JointToIndividualPolicyDomainIndices(Index jdI,
1012  const std::vector<Index>& JointToIndividualPolicyDomainIndicesRef(
1013  Index jdI, PolicyGlobals::IndexDomainCategory cat) const;
1014  std::string SoftPrintPolicyDomainElement(Index agentI, Index dI,
1016  size_t GetNrPolicyDomainElements(Index agentI,
1018  size_t depth=MAXHORIZON) const;
1019 
1020  /*\brief the default IndexDomainCategory for the planning unit.
1021  *
1022  * as specified by Interface_ProblemToPolicyDiscrete. This can be
1023  * overriden in derived classes.
1024  */
1026 };
1027 
1028 
1029 #endif /* !_PLANNINGUNITMADPDISCRETE_H_ */
1030 
1031 
1032 // Local Variables: ***
1033 // mode:c++ ***
1034 // End: ***