Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
MaxSelector.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_MAX_SELECTOR_H
4 #define GRAIL_MAX_SELECTOR_H
5 
6 #include <functional>
7 #include <limits>
8 #include "SelectionOption.hh"
9 #include "SelectionResult.hh"
10 #include "../../GrailEvaluators/Consideration.hh"
11 
12 namespace grail
13 {
14 namespace utility
15 {
19 namespace max_selector
20 {
21  template <typename ContextType>
29  void GetBestOption(const std::vector<SelectionOption<ContextType>>& options,
30  SelectionResult& result,
31  std::function<bool(const std::size_t&)> validator)
32  {
33  float bestWeight = std::numeric_limits<float>::min();
34  int bestRank = std::numeric_limits<int>::min();
35 
36  for(std::size_t i = 0; i < options.size(); ++i)
37  {
38  if(!validator(i))
39  {
40  continue;
41  }
42 
43  auto& option = options[i];
44 
45  int rank = option.rank;
46  evaluator::CacheConsiderationEvaluations(option.evaluator, option.context);
47  float weight = option.evaluator->EvaluateContext(option.context,
48  result.
49  FetchNextEvaluatorSnapshot(option.
50  evaluatedObjectName,
51  option.evaluatedObjectMetadata,
52  rank));
53 
54  if(rank > bestRank || (rank == bestRank && weight > bestWeight))
55  {
56  bestRank = rank;
57  bestWeight = weight;
58  result.optionIndex = i;
59  }
60  }
61  }
62 }
63 }
64 }
65 
66 #endif //GRAIL_MAX_SELECTOR_H
grail::utility::max_selector::GetBestOption
void GetBestOption(const std::vector< SelectionOption< ContextType >> &options, SelectionResult &result, std::function< bool(const std::size_t &)> validator)
GetBestOption - Selects most suitable option. Provides different context for each evaluated objects.
Definition: MaxSelector.hh:29
grail::utility::SelectionOption
The SelectionOption struct - Helper structure describing one of the options being selected by Selecto...
Definition: SelectionOption.hh:19
grail::utility::SelectionResult::optionIndex
std::size_t optionIndex
optionIndex - Index of option selected by Selector.
Definition: SelectionResult.hh:67
grail::utility::SelectionResult
The SelectionResult struct - Structure containing results of operations done by Selector.
Definition: SelectionResult.hh:19