 |
Grail (C++)
1.1.1
A multi-platform, modular, universal engine for embedding advanced AI in games.
|
1 #ifndef GRAIL_SELECTOR_H
2 #define GRAIL_SELECTOR_H
4 #include "Blueprint.hh"
5 #include "../GrailData/DebugInfo/UtilityReasonerSnapshot.h"
6 #include "../GrailData/UtilityModel/UtilitySelectorModel.h"
7 #include "../GrailEvaluators/Evaluator.hh"
18 static const std::string MAX =
"Max";
19 static const std::string ROULETTE =
"Roulette";
21 template <
typename ContextType>
35 std::shared_ptr<Evaluator<ContextType>>
evaluator{};
63 snapshot = std::make_unique<UtilityReasonerSnapshot>();
74 const std::string& evaluatedObjectMetadata,
79 snapshot->evaluatorSnapshots.emplace_back(evaluatedObjectName, evaluatedObjectMetadata, rank);
80 return &
snapshot->evaluatorSnapshots.back();
91 return optionIndex != std::numeric_limits<std::size_t>::max();
97 std::unique_ptr<UtilityReasonerSnapshot>
snapshot{
nullptr};
101 std::size_t
optionIndex = std::numeric_limits<std::size_t>::max();
109 template <
typename ContextType>
112 std::function<
bool(
const std::size_t&)> validator)
114 float bestWeight = std::numeric_limits<float>::min();
115 int bestRank = std::numeric_limits<int>::min();
117 for(std::size_t i = 0; i < options.size(); ++i)
124 auto& option = options[i];
126 int rank = option.rank;
127 float weight = option.evaluator->EvaluateContext(option.context,
129 FetchNextEvaluatorSnapshot(option.
131 option.evaluatedObjectMetadata,
134 if(rank > bestRank || (rank == bestRank && weight > bestWeight))
143 template <
typename ContextType>
155 std::function<
bool(
const std::size_t&)> validator = [](
const std::size_t&) ->
bool
160 switch(selectionMethod)
162 case SelectionMethod::MAX:
163 SelectMaxUtility<ContextType>(options, result, validator);
173 #endif //GRAIL_SELECTOR_H
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
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
The UtilityEvaluatorSnapshot class - debug snapshot of whole evaluator tree assigned to evaluated obj...
Definition: UtilityEvaluatorSnapshot.h:21
std::unique_ptr< UtilityReasonerSnapshot > snapshot
snapshot - If debugging mode was enabled in constructor, contains debug snapshot of whole UtilityReas...
Definition: Selector.hh:97
std::string evaluatedObjectName
evaluatedObjectName - Name of the evaluated object.
Definition: Selector.hh:39
std::string evaluatedObjectMetadata
evaluatedObjectMetadata - additional data describing the evaluated object
Definition: Selector.hh:43
std::size_t optionIndex
optionIndex - Index of option selected by Selector.
Definition: Selector.hh:101
bool IsValid() const
IsValid - Checks whether Selector already finished it's calculations and encountered no errors.
Definition: Selector.hh:89
The SelectionResult struct - Structure containing results of operations done by Selector.
Definition: Selector.hh:53
The Option struct - Helper structure describing one of the options being selected by Selector.
Definition: Selector.hh:26
std::shared_ptr< Evaluator< ContextType > > evaluator
evaluator - Object responsible for evaluation of provided context.
Definition: Selector.hh:35
int rank
rank - Measure of importance of evaluated object. Only objects with highest available rank may be sel...
Definition: Selector.hh:47
SelectionResult(bool isDebugging=false)
SelectionResult - Constructor.
Definition: Selector.hh:59
ContextType context
context - Context to be evaluated.
Definition: Selector.hh:31