Grail (C++)  1.4.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:
24  struct Config
25  {
26  Config() noexcept
27  {
28  }
29 
31  std::size_t maxIterations = 1000;
32 
34  int maxPlanLength = -1;
35 
37  std::size_t iterationsPerFrame = 10;
38 
40  double maxPlanCost = -1;
41 
46  bool usePartialPlans = true;
47  };
48 
51  PlannerReasoner(std::unique_ptr<MemoryPool> memory);
52 
53 
61  PlannerReasoner(std::unique_ptr<MemoryPool> memory,
62  std::shared_ptr<planner::GoalSelector> goalSelector,
63  std::unique_ptr<planner::DomainTranslator> domainTranslator,
64  const Config& config);
65  virtual void StageBehavior(AIEntity& entity) override;
66  virtual void SetNewGoal(std::unique_ptr<planner::Goal> newGoal, AIEntity& entity) override;
67 
71  void SetupNewPlanner(std::unique_ptr<planner::DomainTranslator> domainTranslator, const Config& config);
72 
74  void SetFallbackBehavior(std::unique_ptr<Behavior> behavior);
75 
76  virtual const planner::Goal* GetCurrentGoal() const override;
77 
83  void BindPlanningSucceededCallback(PlannerCallback callback);
84 
92  void BindPartialPlanFoundCallback(PlannerCallback callback);
93 
99  void BindPlanningFailedCallback(std::function<void(const planner::Goal&)> callback);
100 
105  void BindPlanExecutedCallback(std::function<void()> callback);
106 
108  void SetSnapshotProduction(bool shouldProduce);
109 
111  bool IsComputing() const;
112 
113  data::PlannerReasonerSnapshot ProduceDebugSnapshot();
114  virtual std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(std::size_t) override;
115 
118 
120  void SetDebugSnapshotFirstIteration(std::size_t iterationNumber);
121 
123  void SetDebugSnapshotLastIteration(std::size_t iterationNumber);
124 
126  std::size_t GetDebugSnapshotFirstIteration() const;
127 
129  std::size_t GetDebugSnapshotLastIteration() const;
130 
131  protected:
132  virtual void ClearCache() override;
133 
134  private:
135  void Reset(AIEntity& entity, const class planner::Goal& goal);
136  void DevisePlan(AIEntity& entity);
137 
138  planner::Planner planner;
139  std::shared_ptr<planner::GoalSelector> goalSelector;
140  std::unique_ptr<planner::DomainTranslator> domainTranslator;
141  std::unique_ptr<MemoryPool> memory;
142 
143  Plan currentPlan;
144  std::size_t iterationsPerFrame = 0;
145  bool usePartialPlans = false;
146 
147  std::unique_ptr<planner::Goal> chosenGoal = nullptr;
148  std::unique_ptr<Behavior> fallbackBehavior = nullptr;
149 
150  PlannerCallback onPlanningSucceeded;
151  PlannerCallback onPartialPlanFound;
152  std::function<void(const planner::Goal&)> onPlanningFailed;
153  std::function<void()> onPlanExecuted;
154 
155  bool produceSnapshot = false;
157 
158  std::size_t debugSnapshotFirstIteration = 0;
159  std::size_t debugSnapshotLastIteration = std::numeric_limits<std::size_t>::max();
160  };
161 }
162 }
163 
164 #endif //GRAIL_PLANNER_REASONER_H
grail::planner::PlannerReasoner::SetNewGoal
virtual void SetNewGoal(std::unique_ptr< planner::Goal > newGoal, AIEntity &entity) override
Sets a new current goal for the given entity.
Definition: PlannerReasoner.cpp:79
grail::planner::PlannerReasoner::GetDebugSnapshotFirstIteration
std::size_t GetDebugSnapshotFirstIteration() const
Gets the number of the iteration starting from which debug snapshots will be generated if run in debu...
Definition: PlannerReasoner.cpp:177
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::Config::iterationsPerFrame
std::size_t iterationsPerFrame
The number of iterations to perform per one update of the reasoner. The total number of iterations wi...
Definition: PlannerReasoner.hh:37
grail::planner::PlannerReasoner::GetCurrentGoal
virtual const planner::Goal * GetCurrentGoal() const override
Definition: PlannerReasoner.cpp:121
grail::planner::PlannerReasoner::IsComputing
bool IsComputing() const
Definition: PlannerReasoner.cpp:151
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::PlannerReasoner::ClearCurrentDebugSnapshot
void ClearCurrentDebugSnapshot()
Clears any debug data that have been accumulated during iterations so far.
Definition: PlannerReasoner.cpp:162
grail::planner::PlannerReasoner::Config::maxPlanLength
int maxPlanLength
The maximum number of actions (steps) a plan can take. Set -1 for unlimited plans.
Definition: PlannerReasoner.hh:34
grail::planner::IGoalAcceptor
Definition: GoalSelector.hh:16
grail::planner::PlannerReasoner::SetDebugSnapshotLastIteration
void SetDebugSnapshotLastIteration(std::size_t iterationNumber)
Sets the number of the iteration up to which debug snapshots will be generated if run in debug mode.
Definition: PlannerReasoner.cpp:172
grail::planner::Planner
Definition: Planner.hh:39
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::PlannerReasoner::GetDebugSnapshotLastIteration
std::size_t GetDebugSnapshotLastIteration() const
Gets the number of the iteration up to which debug snapshots will be generated if run in debug mode.
Definition: PlannerReasoner.cpp:182
grail::planner::Goal
Definition: Goal.hh:19
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::ClearCache
virtual void ClearCache() override
ClearCache - called when assigning this reasoner to an entity. Clears internally stored data,...
Definition: PlannerReasoner.cpp:187
grail::planner::PlannerReasoner::SetDebugSnapshotFirstIteration
void SetDebugSnapshotFirstIteration(std::size_t iterationNumber)
Sets the number of the iteration starting from which debug snapshots will be generated if run in debu...
Definition: PlannerReasoner.cpp:167
grail::planner::PlannerReasoner::SetSnapshotProduction
void SetSnapshotProduction(bool shouldProduce)
Enable or disable snapshot production for debugging.
Definition: PlannerReasoner.cpp:146
grail::planner::PlannerReasoner::Config::maxPlanCost
double maxPlanCost
An upper bound on the plan cost measured as the sum of costs of actions it consists of....
Definition: PlannerReasoner.hh:40
grail::planner::PlannerReasoner::PlannerReasoner
PlannerReasoner(std::unique_ptr< MemoryPool > memory)
Definition: PlannerReasoner.cpp:13
grail::planner::PlannerReasoner::Config::maxIterations
std::size_t maxIterations
The maximum number of iterations to find the best plan in a given state.
Definition: PlannerReasoner.hh:31
grail::planner::PlannerReasoner::Config::usePartialPlans
bool usePartialPlans
Definition: PlannerReasoner.hh:46
grail::planner::PlannerReasoner::Config
The Planner's run-time configuration parameters.
Definition: PlannerReasoner.hh:24