3 #ifndef GRAIL_ROULETTE_SELECTOR_H
4 #define GRAIL_ROULETTE_SELECTOR_H
6 #include "RouletteOptions.hh"
7 #include "../Selectors/SelectionOption.hh"
8 #include "../Selectors/SelectionResult.hh"
9 #include "../../GrailEvaluators/Consideration.hh"
10 #include "../ConfigHelpers/DefaultRandomNumberGenerator.h"
22 namespace roulette_selector
24 template <
typename ContextType>
38 std::function<
bool(
const std::size_t&)> validator)
40 std::vector<std::pair<int, float>> legalIndexWeightPairs(options.size());
42 for (
int i = 0; i < options.size(); ++i)
49 auto option = options[i];
50 int rank = option.rank;
51 evaluator::CacheConsiderationEvaluations(option.evaluator, option.context);
52 float weight = option.evaluator->EvaluateContext(option.context,
64 legalIndexWeightPairs.clear();
68 legalIndexWeightPairs.emplace_back(i, weight);
72 const int numberOfTopBehaviors = rouletteOptions.GetNumberOfTopBehaviors();
73 if (legalIndexWeightPairs.empty() || numberOfTopBehaviors == 0)
78 std::sort(legalIndexWeightPairs.begin(), legalIndexWeightPairs.end(), [](
auto first,
auto second) { return first.second > second.second; });
80 size_t topOptionsLimit;
81 if(numberOfTopBehaviors > 0)
83 topOptionsLimit = std::min<size_t>(numberOfTopBehaviors, legalIndexWeightPairs.size());
87 topOptionsLimit = legalIndexWeightPairs.size();
90 const float bestOptionWeight = legalIndexWeightPairs.front().second;
91 const float utilityThreshold = bestOptionWeight * rouletteOptions.GetRelativeUtilityThreshold();
92 float totalWeightOfBestRankOptions = bestOptionWeight;
93 for(
int i = 1; i < topOptionsLimit; ++i)
95 const float optionUtility = legalIndexWeightPairs[i].second;
96 if(optionUtility < utilityThreshold)
101 totalWeightOfBestRankOptions += optionUtility;
104 float thresholdValue = randomNumberGenerator.GetRandomPercentage() * totalWeightOfBestRankOptions;
105 for(
const auto legalIndexWeightPair : legalIndexWeightPairs)
107 if (thresholdValue <= legalIndexWeightPair.second)
113 thresholdValue -= legalIndexWeightPair.second;
120 #endif //GRAIL_ROULETTE_SELECTOR_H