Grail (C++)  1.3.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
PlannerReasoner.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_PLANNER_REASONER_H
4 #define GRAIL_PLANNER_REASONER_H
5 
6 #include "../GoalSelector.hh"
7 #include "../../GrailCore/Behavior.hh"
8 #include "../../GrailCore/Plan.hh"
9 #include "../../GrailCore/Reasoner.hh"
10 #include "../../GrailData/DebugInfo/PlannerSnapshots.h"
11 
12 namespace grail
13 {
14 namespace planner
15 {
16  using PlannerCallback = std::function<void(const Plan&, const planner::Goal&)>;
17  class DomainTranslator;
18 
21  {
22  public:
23  struct Config
24  {
25  Config() noexcept
26  {
27  }
28 
29  std::size_t maxIterations = 1000;
30  int maxPlanLength = -1;
31  std::size_t iterationsPerFrame = 10;
32  double maxPlanCost = -1;
33  bool usePartialPlans = true;
34  };
35 
36  PlannerReasoner(std::unique_ptr<MemoryPool> memory);
37  PlannerReasoner(std::unique_ptr<MemoryPool> memory,
38  std::shared_ptr<planner::GoalSelector> goalSelector,
39  std::unique_ptr<planner::DomainTranslator> domainTranslator,
40  const Config& config);
41  virtual void StageBehavior(AIEntity& entity) override;
42  virtual void SetNewGoal(std::unique_ptr<planner::Goal> newGoal, AIEntity& entity) override;
43 
47  void SetupNewPlanner(std::unique_ptr<planner::DomainTranslator> domainTranslator, const Config& config);
48 
50  void SetFallbackBehavior(std::unique_ptr<Behavior> behavior);
51 
52  virtual const planner::Goal* GetCurrentGoal() const override;
53 
59  void BindPlanningSucceededCallback(PlannerCallback callback);
60 
68  void BindPartialPlanFoundCallback(PlannerCallback callback);
69 
75  void BindPlanningFailedCallback(std::function<void(const planner::Goal&)> callback);
76 
81  void BindPlanExecutedCallback(std::function<void()> callback);
82 
83  void SetSnapshotProduction(bool shouldProduce);
84  bool IsComputing() const;
85 
86  data::PlannerReasonerSnapshot ProduceDebugSnapshot();
87  virtual std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(std::size_t) override;
88  void ClearCurrentDebugSnapshot();
89 
90  void SetDebugSnapshotFirstIteration(std::size_t iterationNumber);
91  void SetDebugSnapshotLastIteration(std::size_t iterationNumber);
92  std::size_t GetDebugSnapshotFirstIteration() const;
93  std::size_t GetDebugSnapshotLastIteration() const;
94  private:
95  void Reset(AIEntity& entity, const class planner::Goal& goal);
96  void DevisePlan(AIEntity& entity);
97 
98  planner::Planner planner;
99  std::shared_ptr<planner::GoalSelector> goalSelector;
100  std::unique_ptr<planner::DomainTranslator> domainTranslator;
101  std::unique_ptr<MemoryPool> memory;
102 
103  Plan currentPlan;
104  std::size_t iterationsPerFrame = 0;
105  bool usePartialPlans = false;
106 
107  std::unique_ptr<planner::Goal> chosenGoal = nullptr;
108  std::unique_ptr<Behavior> fallbackBehavior = nullptr;
109 
110  PlannerCallback onPlanningSucceeded;
111  PlannerCallback onPartialPlanFound;
112  std::function<void(const planner::Goal&)> onPlanningFailed;
113  std::function<void()> onPlanExecuted;
114 
115  //TODO: revise this approach
116  //REVIEW: resolve todo
117  bool produceSnapshot = false;
119 
120  std::size_t debugSnapshotFirstIteration = 0;
121  std::size_t debugSnapshotLastIteration = std::numeric_limits<std::size_t>::max();
122  };
123 }
124 }
125 
126 #endif //GRAIL_PLANNER_REASONER_H
grail::AIEntity
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:50
grail::Reasoner
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:21
grail::planner::PlannerReasoner::BindPartialPlanFoundCallback
void BindPartialPlanFoundCallback(PlannerCallback callback)
BindPartialPlanFoundCallback - Bind a function to call when no sufficient plan could be found,...
Definition: PlannerReasoner.cpp:131
grail::planner::PlannerReasoner::BindPlanExecutedCallback
void BindPlanExecutedCallback(std::function< void()> callback)
BindPlanExecutedCallback - Bind a function to call when entity has successfully executed chosen plan.
Definition: PlannerReasoner.cpp:141
grail::planner::PlannerReasoner
A reasoner which uses planners to find optimal sequences of behaviors based on goals.
Definition: PlannerReasoner.hh:20
grail::planner::IGoalAcceptor
Definition: GoalSelector.hh:12
grail::planner::Planner
The main class responsible for finding paths in plan space.
Definition: Planner.hh:29
grail::planner::PlannerReasoner::BindPlanningFailedCallback
void BindPlanningFailedCallback(std::function< void(const planner::Goal &)> callback)
BindPlanningFailedCallback - Bind a function to call when no sufficient plan was found....
Definition: PlannerReasoner.cpp:136
grail::Plan
A data structure used by PlannerReasoner to execute a sequence of behaviors.
Definition: Plan.hh:14
grail::data::PlannerReasonerSnapshot
Definition: PlannerSnapshots.h:92
grail::planner::PlannerReasoner::SetupNewPlanner
void SetupNewPlanner(std::unique_ptr< planner::DomainTranslator > domainTranslator, const Config &config)
Definition: PlannerReasoner.cpp:104
grail::planner::PlannerReasoner::BindPlanningSucceededCallback
void BindPlanningSucceededCallback(PlannerCallback callback)
BindPlanningSucceededCallback - Binds a function handle to call if a sufficient (all goal conditions ...
Definition: PlannerReasoner.cpp:126
grail::planner::PlannerReasoner::SetFallbackBehavior
void SetFallbackBehavior(std::unique_ptr< Behavior > behavior)
Sets the behavior that should be performed during plan computation or if no valid plan is found.
Definition: PlannerReasoner.cpp:116
grail::planner::Goal
Represents a planner goal, used by PlannerReasoner.
Definition: Goal.hh:16
grail::planner::PlannerReasoner::StageBehavior
virtual void StageBehavior(AIEntity &entity) override
selectBehavior - Runs reasoner's selection algorithm and assigns chosen behavior to provided entity.
Definition: PlannerReasoner.cpp:34
grail::planner::PlannerReasoner::Config
Definition: PlannerReasoner.hh:23