Grail (C++)  1.2.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
Selector.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_SELECTOR_H
4 #define GRAIL_SELECTOR_H
5 
6 #include "Blueprint.hh"
7 #include "../GrailData/DebugInfo/UtilityReasonerSnapshot.h"
8 #include "../GrailData/UtilityModel/UtilitySelectorModel.h"
9 #include "../GrailEvaluators/Evaluator.hh"
10 
11 #include <functional>
12 #include <limits>
13 #include <random>
14 #include <vector>
15 
16 namespace grail
17 {
18 namespace utility
19 {
20  static const std::string MAX = "Max";
21  static const std::string ROULETTE = "Roulette";
22 
23  template <typename ContextType>
28  struct Option
29  {
33  ContextType context{};
37  std::shared_ptr<evaluator::Evaluator<ContextType>> evaluator{};
41  std::string evaluatedObjectName{};
45  std::string evaluatedObjectMetadata{};
49  int rank{};
50  };
51 
56  {
61  SelectionResult(bool isDebugging = false)
62  {
63  if(isDebugging)
64  {
65  snapshot = std::make_unique<data::UtilityReasonerSnapshot>();
66  }
67  }
68 
75  data::UtilityEvaluatorSnapshot* FetchNextEvaluatorSnapshot(const std::string& evaluatedObjectName,
76  const std::string& evaluatedObjectMetadata,
77  int rank)
78  {
79  if(snapshot != nullptr)
80  {
81  snapshot->evaluatorSnapshots.emplace_back(evaluatedObjectName, evaluatedObjectMetadata, rank);
82  return &snapshot->evaluatorSnapshots.back();
83  }
84  return nullptr;
85  }
86 
91  bool IsValid() const
92  {
93  return optionIndex != std::numeric_limits<std::size_t>::max();
94  }
95 
99  std::unique_ptr<data::UtilityReasonerSnapshot> snapshot{nullptr};
103  std::size_t optionIndex = std::numeric_limits<std::size_t>::max();
104  };
105 
109  namespace selector
110  {
111  template <typename ContextType>
112  void SelectMaxUtility(const std::vector<Option<ContextType>>& options,
113  SelectionResult& result,
114  std::function<bool(const std::size_t&)> validator)
115  {
116  float bestWeight = std::numeric_limits<float>::min();
117  int bestRank = std::numeric_limits<int>::min();
118 
119  for(std::size_t i = 0; i < options.size(); ++i)
120  {
121  if(!validator(i))
122  {
123  continue;
124  }
125 
126  auto& option = options[i];
127 
128  int rank = option.rank;
129  float weight = option.evaluator->EvaluateContext(option.context,
130  result.
131  FetchNextEvaluatorSnapshot(option.
132  evaluatedObjectName,
133  option.evaluatedObjectMetadata,
134  rank));
135 
136  if(rank > bestRank || (rank == bestRank && weight > bestWeight))
137  {
138  bestRank = rank;
139  bestWeight = weight;
140  result.optionIndex = i;
141  }
142  }
143  }
144 
145  template <typename ContextType>
154  void GetBestOption(data::SelectionMethod selectionMethod,
155  const std::vector<Option<ContextType>>& options,
156  SelectionResult& result,
157  std::function<bool(const std::size_t&)> validator = [](const std::size_t&) -> bool
158  {
159  return true;
160  })
161  {
162  switch(selectionMethod)
163  {
164  case data::SelectionMethod::MAX:
165  SelectMaxUtility<ContextType>(options, result, validator);
166  break;
167  default:
168  break; //TODO: crash?
169  //REVIEW: resolve todo above
170  }
171  }
172  }
173 }
174 }
175 #endif //GRAIL_SELECTOR_H
grail::utility::SelectionResult::FetchNextEvaluatorSnapshot
data::UtilityEvaluatorSnapshot * FetchNextEvaluatorSnapshot(const std::string &evaluatedObjectName, const std::string &evaluatedObjectMetadata, int rank)
FetchNextEvaluatorSnapshot - If debugging mode was enabled in constructor, creates an empty snapshot ...
Definition: Selector.hh:75
grail::utility::Option::evaluator
std::shared_ptr< evaluator::Evaluator< ContextType > > evaluator
evaluator - Object responsible for evaluation of provided context.
Definition: Selector.hh:37
grail::utility::Option::evaluatedObjectName
std::string evaluatedObjectName
evaluatedObjectName - Name of the evaluated object.
Definition: Selector.hh:41
grail::utility::Option::evaluatedObjectMetadata
std::string evaluatedObjectMetadata
evaluatedObjectMetadata - additional data describing the evaluated object
Definition: Selector.hh:45
grail::utility::SelectionResult::optionIndex
std::size_t optionIndex
optionIndex - Index of option selected by Selector.
Definition: Selector.hh:103
grail::utility::SelectionResult::IsValid
bool IsValid() const
IsValid - Checks whether Selector already finished it's calculations and encountered no errors.
Definition: Selector.hh:91
grail::utility::SelectionResult
The SelectionResult struct - Structure containing results of operations done by Selector.
Definition: Selector.hh:55
grail::utility::Option
The Option struct - Helper structure describing one of the options being selected by Selector.
Definition: Selector.hh:28
grail::utility::selector::GetBestOption
void GetBestOption(data::SelectionMethod selectionMethod, const std::vector< Option< ContextType >> &options, SelectionResult &result, std::function< bool(const std::size_t &)> validator=[](const std::size_t &) -> bool { return true;})
GetBestOption - Selects most suitable option. Provides different context for each evaluated objects.
Definition: Selector.hh:154
grail::utility::Option::rank
int rank
rank - Measure of importance of evaluated object. Only objects with highest available rank may be sel...
Definition: Selector.hh:49
grail::data::UtilityEvaluatorSnapshot
The UtilityEvaluatorSnapshot class - debug snapshot of whole evaluator tree assigned to evaluated obj...
Definition: UtilityEvaluatorSnapshot.h:25
grail::utility::SelectionResult::SelectionResult
SelectionResult(bool isDebugging=false)
SelectionResult - Constructor.
Definition: Selector.hh:61
grail::utility::SelectionResult::snapshot
std::unique_ptr< data::UtilityReasonerSnapshot > snapshot
snapshot - If debugging mode was enabled in constructor, contains debug snapshot of whole UtilityReas...
Definition: Selector.hh:99
grail::utility::Option::context
ContextType context
context - Context to be evaluated.
Definition: Selector.hh:33