Grail (C++)  1.1.1
A multi-platform, modular, universal engine for embedding advanced AI in games.
Selector.hh
1 #ifndef GRAIL_SELECTOR_H
2 #define GRAIL_SELECTOR_H
3 
4 #include "Blueprint.hh"
5 #include "../GrailData/DebugInfo/UtilityReasonerSnapshot.h"
6 #include "../GrailData/UtilityModel/UtilitySelectorModel.h"
7 #include "../GrailEvaluators/Evaluator.hh"
8 
9 #include <functional>
10 #include <limits>
11 #include <random>
12 #include <vector>
13 
14 namespace grail
15 {
16  namespace utility
17  {
18  static const std::string MAX = "Max";
19  static const std::string ROULETTE = "Roulette";
20 
21  template <typename ContextType>
26  struct Option
27  {
31  ContextType context{};
35  std::shared_ptr<Evaluator<ContextType>> evaluator{};
39  std::string evaluatedObjectName{};
43  std::string evaluatedObjectMetadata{};
47  int rank{};
48  };
49 
54  {
59  SelectionResult(bool isDebugging = false)
60  {
61  if(isDebugging)
62  {
63  snapshot = std::make_unique<UtilityReasonerSnapshot>();
64  }
65  }
66 
73  UtilityEvaluatorSnapshot* FetchNextEvaluatorSnapshot(const std::string& evaluatedObjectName,
74  const std::string& evaluatedObjectMetadata,
75  int rank)
76  {
77  if(snapshot != nullptr)
78  {
79  snapshot->evaluatorSnapshots.emplace_back(evaluatedObjectName, evaluatedObjectMetadata, rank);
80  return &snapshot->evaluatorSnapshots.back();
81  }
82  return nullptr;
83  }
84 
89  bool IsValid() const
90  {
91  return optionIndex != std::numeric_limits<std::size_t>::max();
92  }
93 
97  std::unique_ptr<UtilityReasonerSnapshot> snapshot{nullptr};
101  std::size_t optionIndex = std::numeric_limits<std::size_t>::max();
102  };
103 
107  namespace selector
108  {
109  template <typename ContextType>
110  void SelectMaxUtility(const std::vector<Option<ContextType>>& options,
111  SelectionResult& result,
112  std::function<bool(const std::size_t&)> validator)
113  {
114  float bestWeight = std::numeric_limits<float>::min();
115  int bestRank = std::numeric_limits<int>::min();
116 
117  for(std::size_t i = 0; i < options.size(); ++i)
118  {
119  if(!validator(i))
120  {
121  continue;
122  }
123 
124  auto& option = options[i];
125 
126  int rank = option.rank;
127  float weight = option.evaluator->EvaluateContext(option.context,
128  result.
129  FetchNextEvaluatorSnapshot(option.
130  evaluatedObjectName,
131  option.evaluatedObjectMetadata,
132  rank));
133 
134  if(rank > bestRank || (rank == bestRank && weight > bestWeight))
135  {
136  bestRank = rank;
137  bestWeight = weight;
138  result.optionIndex = i;
139  }
140  }
141  }
142 
143  template <typename ContextType>
152  void GetBestOption(SelectionMethod selectionMethod,
153  const std::vector<Option<ContextType>>& options,
154  SelectionResult& result,
155  std::function<bool(const std::size_t&)> validator = [](const std::size_t&) -> bool
156  {
157  return true;
158  })
159  {
160  switch(selectionMethod)
161  {
162  case SelectionMethod::MAX:
163  SelectMaxUtility<ContextType>(options, result, validator);
164  break;
165  default:
166  break; //TODO: crash?
167  //REVIEW: resolve todo above
168  }
169  }
170  }
171  }
172 }
173 #endif //GRAIL_SELECTOR_H
grail::utility::SelectionResult::FetchNextEvaluatorSnapshot
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:73
grail::utility::selector::GetBestOption
void GetBestOption(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:152
grail::UtilityEvaluatorSnapshot
The UtilityEvaluatorSnapshot class - debug snapshot of whole evaluator tree assigned to evaluated obj...
Definition: UtilityEvaluatorSnapshot.h:21
grail::utility::SelectionResult::snapshot
std::unique_ptr< UtilityReasonerSnapshot > snapshot
snapshot - If debugging mode was enabled in constructor, contains debug snapshot of whole UtilityReas...
Definition: Selector.hh:97
grail::utility::Option::evaluatedObjectName
std::string evaluatedObjectName
evaluatedObjectName - Name of the evaluated object.
Definition: Selector.hh:39
grail::utility::Option::evaluatedObjectMetadata
std::string evaluatedObjectMetadata
evaluatedObjectMetadata - additional data describing the evaluated object
Definition: Selector.hh:43
grail::utility::SelectionResult::optionIndex
std::size_t optionIndex
optionIndex - Index of option selected by Selector.
Definition: Selector.hh:101
grail::utility::SelectionResult::IsValid
bool IsValid() const
IsValid - Checks whether Selector already finished it's calculations and encountered no errors.
Definition: Selector.hh:89
grail::utility::SelectionResult
The SelectionResult struct - Structure containing results of operations done by Selector.
Definition: Selector.hh:53
grail::utility::Option
The Option struct - Helper structure describing one of the options being selected by Selector.
Definition: Selector.hh:26
grail::utility::Option::evaluator
std::shared_ptr< Evaluator< ContextType > > evaluator
evaluator - Object responsible for evaluation of provided context.
Definition: Selector.hh:35
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:47
grail::utility::SelectionResult::SelectionResult
SelectionResult(bool isDebugging=false)
SelectionResult - Constructor.
Definition: Selector.hh:59
grail::utility::Option::context
ContextType context
context - Context to be evaluated.
Definition: Selector.hh:31