Esempio n. 1
0
        /// <summary>
        /// Determines if this TreeSearchNode is an ancestor of the provided TreeSearchNode (i.e. when traversing the tree upwards, if this TreeSearchNode is encountered).
        /// </summary>
        /// <param name="descendant">TreeSearchNode that is a potential descendant of this TreeSearchNode.</param>
        /// <returns>Whether or not the argument TreeSearchNode is a descendant of this TreeSearchNode.</returns>
        public bool IsAncestorOf(TreeSearchNode <S, A> descendant)
        {
            while (!descendant.IsRoot())
            {
                if (descendant.Equals(this))
                {
                    return(true);
                }
                descendant = descendant.Parent;
            }

            return(false);
        }
        /// <summary>
        /// Propagate an evaluation value of the argument state starting from the argument node back up to the root node.
        /// </summary>
        /// <param name="context">The context of the search.</param>
        /// <param name="evaluation">The strategy used to evaluate the state.</param>
        /// <param name="node">The node from which the backpropagation starts.</param>
        /// <param name="state">The state that should be evaluated.</param>
        public void BackPropagate(SearchContext <D, P, A, S, Sol> context, IStateEvaluation <D, P, A, S, Sol, TreeSearchNode <P, A> > evaluation, TreeSearchNode <P, A> node, P state)
        {
            // Evaluate the state with respect to the argument node.
            var value = evaluation.Evaluate(context, node, state);

            // The root player is the current player in the search's source state.
            var rootPlayer = context.Source.CurrentPlayer();

            do
            {
                // Check whether or not this node is a root player's node.
                var targetPlayer = node.IsRoot() || rootPlayer == node.Payload.Player();

                // Visits the node with a coloured evaluation value.
                node.Visit(targetPlayer ? value : -value);

                // Keep moving up the tree while there is a valid parent.
            } while ((node = node.Parent) != null);
        }