Exemple #1
0
            /// <inheritdoc />
            public void DetermineSampleSizes(SearchContext <List <SabberStoneAction>, SabberStoneState, SabberStoneAction, object, SabberStoneAction> context, out int generationSamples, out int evaluationSamples)
            {
                long estimatedSamples;

                switch (BudgetType)
                {
                case BudgetType.Iterations:
                    // If we're running on an iterations budget, we know exactly how many we can spend.
                    estimatedSamples = BudgetAllowance;
                    break;

                case BudgetType.Time:
                    // If we're running on a time budget, we can check what the previous search was able to run.
                    if (PreviousSearchTime > 0 && PreviousSearchIterations > 0)
                    {
                        var sampleDuration = PreviousSearchTime / (PreviousSearchIterations * 1.0);
                        // We can now estimate how many samples go into the budget we are allowed to spend
                        estimatedSamples = (long)(BudgetAllowance / (sampleDuration * AverageSampleTimeBudgetEstimationStrategy.TIME_BUDGET_SAFETY_MARGIN));
                    }
                    else
                    {
                        // If we don't have any statistics on the previous search (maybe cause this is going to be the first)
                        // use an estimation strategy based on a couple playouts
                        estimatedSamples = AverageSampleTimeBudgetEstimationStrategy.EstimateBudgetWithTestPlayouts(context, Playout, BudgetAllowance);
                    }
                    break;

                default:
                    throw new InvalidEnumArgumentException($"BudgetType `{BudgetType}' is not supported.");
                }

                generationSamples = (int)(estimatedSamples * GenerationBudgetPercentage);
                evaluationSamples = (int)(estimatedSamples * (1 - GenerationBudgetPercentage) * Constants.DEFAULT_LSI_EVALUATION_SAMPLES_ADJUSTMENT_FACTOR);
            }
Exemple #2
0
        /// <summary>
        /// Constructs a new instance of LSIBot with default strategies.
        /// </summary>
        /// <param name="allowPerfectInformation">[Optional] Whether or not this bot is allowed perfect information about the game state (i.e. no obfuscation and therefore no determinisation). Default value is false.</param>
        /// <param name="ensembleSize">[Optional] The size of the ensemble to use. Default value is 1.</param>
        /// <param name="playoutBotType">[Optional] The type of playout bot to be used during playouts. Default value is <see cref="PlayoutBotType.MAST"/>.</param>
        /// <param name="mastSelectionType">[Optional] The type of selection strategy used by the M.A.S.T. playout. Default value is <see cref="MASTPlayoutBot.SelectionType.EGreedy"/>.</param>
        /// <param name="playoutTurnCutoff">[Optional] The amount of turns after which to stop a simulation. Default value is <see cref="Constants.DEFAULT_PLAYOUT_TURN_CUTOFF"/>.</param>
        /// <param name="budgetType">[Optional] The type of budget that this bot will use. Default value is <see cref="BudgetType.Iterations"/>.</param>
        /// <param name="samples">[Optional] The budget for the amount of iterations LSI can use. Default value is <see cref="Constants.DEFAULT_COMPUTATION_ITERATION_BUDGET"/>.</param>
        /// <param name="time">[Optional] The budget for the amount of milliseconds LSI can spend on searching. Default value is <see cref="Constants.DEFAULT_COMPUTATION_TIME_BUDGET"/>.</param>
        /// <param name="generationBudgetPercentage">[Optional] The percentage of the budget that should be spent during the generation phase. Default value is <see cref="Constants.DEFAULT_LSI_BUDGET_GENERATION_PERCENTAGE"/>.</param>
        /// <param name="budgetEstimationType">[Optional] The type of strategy used to estimate the budget for LSI. Default value is <see cref="BudgetEstimationType.AverageSampleTime"/>.</param>
        /// <param name="useHeuristicEvaluation">[Optional] Whether or not to use the HeuristicBot's evaluation function. Default value is false.</param>
        /// <param name="debugInfoToConsole">[Optional] Whether or not to write debug information to the console. Default value is false.</param>
        public LSIBot(bool allowPerfectInformation  = false,
                      int ensembleSize              = 1,
                      PlayoutBotType playoutBotType = PlayoutBotType.MAST,
                      MASTPlayoutBot.SelectionType mastSelectionType = MASTPlayoutBot.SelectionType.EGreedy,
                      int playoutTurnCutoff                     = Constants.DEFAULT_PLAYOUT_TURN_CUTOFF,
                      BudgetType budgetType                     = BudgetType.Iterations,
                      int samples                               = Constants.DEFAULT_COMPUTATION_ITERATION_BUDGET,
                      long time                                 = Constants.DEFAULT_COMPUTATION_TIME_BUDGET,
                      double generationBudgetPercentage         = Constants.DEFAULT_LSI_BUDGET_GENERATION_PERCENTAGE,
                      BudgetEstimationType budgetEstimationType = BudgetEstimationType.AverageSampleTime,
                      bool useHeuristicEvaluation               = false,
                      bool debugInfoToConsole                   = false)
        {
            PerfectInformation = allowPerfectInformation;
            EnsembleSize       = ensembleSize;
            PlayoutBotType     = playoutBotType;
            MASTSelectionType  = mastSelectionType;
            PlayoutTurnCutoff  = playoutTurnCutoff;
            BudgetType         = budgetType;
            Samples            = samples;
            Time = time;
            GenerationBudgetPercentage = generationBudgetPercentage;
            BudgetEstimation           = budgetEstimationType;
            _debug = debugInfoToConsole;

            // Create the ensemble search
            Ensemble = new EnsembleStrategySabberStone(enableStateObfuscation: true, enablePerfectInformation: PerfectInformation);

            // Adjust sample sizes for use in the Ensemble
            long budgetAllowance;

            switch (BudgetType)
            {
            case BudgetType.Iterations:
                Samples         = EnsembleSize > 0 ? Samples / EnsembleSize : Samples; // Note: Integer division by design.
                budgetAllowance = Samples;
                break;

            case BudgetType.Time:
                Time            = EnsembleSize > 0 ? Time / EnsembleSize : Time; // Note: Integer division by design.
                budgetAllowance = Time;
                break;

            default:
                throw new InvalidEnumArgumentException($"BudgetType `{BudgetType}' is not supported.");
            }

            // Simulation will be handled by the Playout.
            var sabberStoneStateEvaluation = new EvaluationStrategyHearthStone(useHeuristicEvaluation);

            Playout = new PlayoutStrategySabberStone();

            // Set the playout bots
            switch (PlayoutBotType)
            {
            case PlayoutBotType.Random:
                MyPlayoutBot       = new RandomBot(filterDuplicatePositionTasks: true);
                OpponentPlayoutBot = new RandomBot(filterDuplicatePositionTasks: true);
                break;

            case PlayoutBotType.Heuristic:
                MyPlayoutBot       = new HeuristicBot();
                OpponentPlayoutBot = new HeuristicBot();
                break;

            case PlayoutBotType.MAST:
                MyPlayoutBot       = new MASTPlayoutBot(MASTSelectionType, sabberStoneStateEvaluation);
                OpponentPlayoutBot = new MASTPlayoutBot(MASTSelectionType, sabberStoneStateEvaluation);
                break;

            default:
                throw new InvalidEnumArgumentException($"PlayoutBotType `{PlayoutBotType}' is not supported.");
            }

            // Set the budget estimation strategy.
            switch (BudgetEstimation)
            {
            case BudgetEstimationType.AverageSampleTime:
                BudgetEstimationStrategy = new AverageSampleTimeBudgetEstimationStrategy(BudgetType, budgetAllowance, GenerationBudgetPercentage, Playout);
                break;

            case BudgetEstimationType.PreviousSearchAverage:
                BudgetEstimationStrategy = new PreviousSearchAverageBudgetEstimationStrategy(BudgetType, budgetAllowance, GenerationBudgetPercentage, Playout);
                break;

            default:
                throw new InvalidEnumArgumentException($"BudgetEstimationType `{budgetEstimationType}' is not supported.");
            }

            // LSI will need a goal-strategy to determine when a simulation is done
            Goal = new GoalStrategyTurnCutoff(PlayoutTurnCutoff);

            // LSI will need an evaluation-strategy to evaluate the strength of samples
            Evaluation = new EvaluationStrategyHearthStone();

            // Application will be handled by the GameLogic
            // Hierarchical Expansion is set to TRUE because of incremental task validation during action sampling.
            GameLogic = new SabberStoneGameLogic(Goal, hierarchicalExpansion: true);

            // The side information strategy needs access to several of these.
            SideInformationStrategy = new SabberStoneSideInformationStrategy(Playout, Evaluation, GameLogic);

            // The sampling strategy used to sample actions during the generation phase.
            SamplingStrategy = new SabberStoneLSISamplingStrategy(GameLogic);
        }