Ejemplo n.º 1
0
        /// <summary>
        /// Perform the search. Note: should set the Solution in the SearchContext and update its Status.
        /// </summary>
        /// <param name="context">The context within which the search happens.</param>
        public override void Search(SearchContext <D, P, A, S, Sol> context)
        {
            var clone     = context.Cloner;
            var rootState = context.Source;
            var apply     = context.Application;

            var endTime = DateTime.Now.AddMilliseconds(Time);
            var it      = 0;

            // Setup for when we might be continuing a search from a specific node.
            var root = (TreeSearchNode <P, A>)context.StartNode;

            if (root == null)
            {
                root = new TreeSearchNode <P, A>(clone.Clone(rootState));
                context.StartNode = root;
            }

            while ((Time == Constants.NO_LIMIT_ON_THINKING_TIME || DateTime.Now < endTime) &&
                   (Iterations == Constants.NO_LIMIT_ON_ITERATIONS || it < Iterations))
            {
                it++;

                var worldState = clone.Clone(rootState);
                var target     = root;

                // Check if we have to expand
                if (!target.IsFullyExpanded())
                {
                    target = ExpansionStrategy.Expand(context, root, worldState);
                }

                // Select a node
                if (target == null || target == root)
                {
                    target = SelectionStrategy.SelectNextNode(context, root);
                }

                // Apply action in selected node
                worldState = apply.Apply(context, worldState, target.Payload);
                // Simulate
                var endState = PlayoutStrategy.Playout(context, worldState);

                // Backpropagation
                BackPropagationStrategy.BackPropagate(context, EvaluationStrategy, target, endState);
            }

            var finalNode = FinalNodeSelectionStrategy.SelectFinalNode(context, root);

            context.Solution    = SolutionStrategy.Solution(context, finalNode);
            context.BudgetSpent = it;
            context.Status      = SearchContext <D, P, A, S, Sol> .SearchStatus.Success;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Perform the search. Note: should set the Solution in the SearchContext and update its Status.
        /// </summary>
        /// <param name="context">The context within which the search happens.</param>
        public override void Search(SearchContext <D, P, A, S, Sol> context)
        {
            var clone     = context.Cloner;
            var rootState = context.Source;

            var endTime = DateTime.Now.AddMilliseconds(Time);
            var it      = 0;

            // Setup for when we might be continuing a search from a specific node.
            var root = (TreeSearchNode <P, A>)context.StartNode;

            if (root == null)
            {
                root = new TreeSearchNode <P, A>(clone.Clone(rootState));
                context.StartNode = root;
            }

            // Set up a global MAB, to hold the value combinations created during the naïve-sampling process.
            var gMAB = new Dictionary <long, Dictionary <int, LocalArm> >();

            while ((Time == Constants.NO_LIMIT_ON_THINKING_TIME || DateTime.Now < endTime) && (Iterations == Constants.NO_LIMIT_ON_ITERATIONS || it < Iterations))
            {
                it++;

                // SelectAndExpand, reference the iteration counter because it might be updated in the recursive call
                var selectedNode = NaïveSelectAndExpand(context, root, gMAB, endTime, ref it);

                // Keep track of the maximum depth we reach
                var nodeDepth = selectedNode.CalculateDepth();
                if (nodeDepth > MaxDepth)
                {
                    MaxDepth = nodeDepth;
                }

                // Simulate
                var endState = PlayoutStrategy.Playout(context, (P)selectedNode.State.Copy());

                // Backpropagation
                BackPropagationStrategy.BackPropagate(context, EvaluationStrategy, selectedNode, endState);
            }

            var finalNode = FinalNodeSelectionStrategy.SelectFinalNode(context, root);

            context.Solution    = SolutionStrategy.Solution(context, finalNode);
            context.BudgetSpent = it;
            context.Status      = SearchContext <D, P, A, S, Sol> .SearchStatus.Success;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Perform the search. Note: should set the Solution in the SearchContext and update its Status.
        /// </summary>
        /// <param name="context">The context within which the search happens.</param>
        public override void Search(SearchContext <D, P, A, S, Sol> context)
        {
            var clone     = context.Cloner;
            var rootState = context.Source;
            var apply     = context.Application;
            var goal      = context.Goal;

            var endTime = DateTime.Now.AddMilliseconds(Time);
            var it      = 0;

            // Setup for when we might be continuing a search from a specific node.
            var root = (TreeSearchNode <P, A>)context.StartNode;

            if (root == null)
            {
                root = new TreeSearchNode <P, A>(clone.Clone(rootState));
                context.StartNode = root;
            }

            while ((Time == Constants.NO_LIMIT_ON_THINKING_TIME || DateTime.Now < endTime) && (Iterations == Constants.NO_LIMIT_ON_ITERATIONS || it < Iterations))
            {
                it++;

                var worldState = clone.Clone(rootState);

                // Selection
                bool done;
                var  target = root;
                while (!(done = goal.Done(context, worldState)) && target.IsFullyExpanded())
                {
                    target     = SelectionStrategy.SelectNextNode(context, target);
                    worldState = apply.Apply(context, worldState, target.Payload);
                }

                // Expansion
                var endState = worldState;
                if (!done)
                {
                    var result = ExpansionStrategy.Expand(context, target, endState);
                    if (result != target)
                    {
                        endState = apply.Apply(context, endState, result.Payload);
                        target   = result;
                    }

                    // Simulation
                    endState = PlayoutStrategy.Playout(context, endState);
                }

                // Keep track of the maximum depth we reach
                var nodeDepth = target.CalculateDepth();
                if (nodeDepth > MaxDepth)
                {
                    MaxDepth = nodeDepth;
                }

                // Backpropagation
                BackPropagationStrategy.BackPropagate(context, EvaluationStrategy, target, endState);
            }

            var finalNode = FinalNodeSelectionStrategy.SelectFinalNode(context, root);

            context.Solution    = SolutionStrategy.Solution(context, finalNode);
            context.BudgetSpent = it;
            context.Status      = SearchContext <D, P, A, S, Sol> .SearchStatus.Success;
        }