예제 #1
0
    // On a knock-out, reduce the dimension of the joint distribution array and renormalize it
    public void KnockOutFilter(int SittingOrder, int DiscardedValue)
    {
        Debug.Assert(SittingOrder != MySittingOrder);
        Debug.Assert(DiscardedValue >= CardController.VALUE_GUARD && DiscardedValue <= CardController.VALUE_PRINCESS);
        int CardIndex       = DiscardedValue - 1;
        int HiddenHandIndex = GetHiddenHandIndex(SittingOrder);

        // Account for the discared card
        AccountForCard(DiscardedValue);
        // Merge down the current matrix to one dimension lower, taking the slice that corresponds to the knocked-out player's actual hand
        if (CurrentOpponentCount == 3)
        {
            TwoOpponentsHandsDistribution = ThreeOpponentsHandsDistribution.GetSlice(HiddenHandIndex, CardIndex);
            TwoOpponentsHandsDistribution.Renormalize();
            // Also, we move out the knocked-out player's sitting order to the last place of the HiddenHands array
            AIUtil.ShiftToLast(ref HiddenHands, HiddenHandIndex);
        }
        else if (CurrentOpponentCount == 2)
        {
            SingleOpponentHandDistribution = TwoOpponentsHandsDistribution.GetSlice(HiddenHandIndex, CardIndex);
            SingleOpponentHandDistribution.Renormalize();
            AIUtil.ShiftToLast(ref HiddenHands, HiddenHandIndex);
        }
        // Update player situation
        CurrentOpponentCount            -= 1;
        PlayerIsKnockedOut[SittingOrder] = true;
    }
예제 #2
0
 public KnowledgeState(int ThisPlayerSittingOrder, PlayerController[] AllPlayers)
 {
     // Save the player data
     PlayerCount    = AllPlayers.Length;
     MySittingOrder = ThisPlayerSittingOrder;
     HiddenHands    = new int[PlayerCount - 1];
     // Initialize distributions
     DeckDistribution = new Distribution1D(CARD_VECTOR_LENGTH);
     SingleOpponentHandDistribution  = new Distribution1D(CARD_VECTOR_LENGTH);
     TwoOpponentsHandsDistribution   = new Distribution2D(CARD_VECTOR_LENGTH, CARD_VECTOR_LENGTH);
     ThreeOpponentsHandsDistribution = new Distribution3D(CARD_VECTOR_LENGTH, CARD_VECTOR_LENGTH, CARD_VECTOR_LENGTH);
     HandDistribution = new Distribution1D[PlayerCount];
     for (int p = 0; p < PlayerCount; p++)
     {
         HandDistribution[p] = new Distribution1D(CARD_VECTOR_LENGTH);
     }
     CountUnaccountedForCards = new int[GameController.CARD_COUNT.Length];
     TheDeck = AllPlayers[ThisPlayerSittingOrder].Game.Deck;
     // Perform precomputations of static variables if necessary
     if (!PrecomputationsComplete)
     {
         PrecomputationsComplete = true;
         PrecomputeStaticArrays();
     }
     // Other opponent data
     PlayerIsKnockedOut      = new bool[PlayerCount];
     PlayerKnowsThatMyHandIs = new int[PlayerCount];
     PlayerHasTargetedMe     = new int[PlayerCount];
     PlayerHasKnockouts      = new int[PlayerCount];
     // Initialize memory
     Reset();
 }
예제 #3
0
        /// <summary>
        /// Returns a tensor product of this vector (on the X axis) with another vector (on the Y axis).
        /// </summary>
        /// <param name="other">The vector to multiply with.</param>
        /// <returns>A two-dimensional probability distribution representing the tensor product of the two vectors.</returns>
        public ProbabilityDistribution2D GetTensorProduct(ProbabilityDistribution1D other)
        {
            ProbabilityDistribution2D result = new ProbabilityDistribution2D(Length, other.Length);

            for (int x = 0; x < Length; x++)
            {
                for (int y = 0; y < other.Length; y++)
                {
                    result[x, y] = this[x] * other[y];
                }
            }
            return(result);
        }
예제 #4
0
    // Precomputes the static arrays for quicker access
    protected static void PrecomputeStaticArrays()
    {
        // Initialized base deck distribution
        BaseDeckDistribution = new Distribution1D(CARD_VECTOR_LENGTH);
        for (int CardIndex = 0; CardIndex < CARD_VECTOR_LENGTH; CardIndex++)
        {
            BaseDeckDistribution[CardIndex] = ((float)GameController.CARD_COUNT[CardIndex + 1]) / GameController.TOTAL_CARD_COUNT;
        }
        // Initialize base joint hand distribution
        BaseThreeOpponentsHandsDistribution = new Distribution3D(CARD_VECTOR_LENGTH, CARD_VECTOR_LENGTH, CARD_VECTOR_LENGTH);
        // Optimization for the following calculation:
        int[] tempRemainingCards = new int[CARD_VECTOR_LENGTH]; // We can't use virtualRemainingCards in a static method...
        Array.Copy(GameController.CARD_COUNT, 1, tempRemainingCards, 0, CARD_VECTOR_LENGTH);
        float SumOfArray = 0, ScalingFactor = 1f / GameController.TOTAL_CARD_COUNT / (GameController.TOTAL_CARD_COUNT - 1) / (GameController.TOTAL_CARD_COUNT - 2);

        // Calculate base joint hand distribution
        for (int Hand1Index = 0; Hand1Index < CARD_VECTOR_LENGTH; Hand1Index++)
        {
            // Account for this value of the first hand
            float Hand1Prob = ScalingFactor * tempRemainingCards[Hand1Index];
            tempRemainingCards[Hand1Index] -= 1;
            // And loop through all possible second and third hidden hands
            for (int Hand2Index = 0; Hand2Index < CARD_VECTOR_LENGTH; Hand2Index++)
            {
                // Account for this value of the second hand
                float Hand2JointProb = Hand1Prob * tempRemainingCards[Hand2Index];
                tempRemainingCards[Hand2Index] -= 1;
                // And loop through all possible third hidden hands
                for (int Hand3Index = 0; Hand3Index < CARD_VECTOR_LENGTH; Hand3Index++)
                {
                    // If this is an impossible case, just jump to the next iteration
                    if (Hand2JointProb <= 0 || tempRemainingCards[Hand3Index] <= 0)
                    {
                        continue;
                    }
                    // Otherwise, calculate the joint probability of this case and store it
                    float jointProb = Hand2JointProb * tempRemainingCards[Hand3Index];
                    BaseThreeOpponentsHandsDistribution[Hand1Index, Hand2Index, Hand3Index] = jointProb;
                    SumOfArray += jointProb;
                }
                // Reset card counts for the next iteration
                tempRemainingCards[Hand2Index] += 1;
            }
            // Reset card counts for the next iteration
            tempRemainingCards[Hand1Index] += 1;
        }
        Debug.Assert(AIUtil.Approx(SumOfArray, 1f));
    }
        /// <summary>
        /// Returns a tensor product of this matrix (on the X and Y axes) with another vector (on the Y axis).
        /// </summary>
        /// <param name="other">The vector to multiply with.</param>
        /// <returns>A 3D probability distribution representing the tensor product of the matrix and the vector.</returns>
        public ProbabilityDistribution3D GetTensorProduct(ProbabilityDistribution1D other)
        {
            ProbabilityDistribution3D result = new ProbabilityDistribution3D(GetLength(0), GetLength(1), other.Length);

            for (int x = 0; x < GetLength(0); x++)
            {
                for (int y = 0; y < GetLength(1); y++)
                {
                    for (int z = 0; z < other.Length; z++)
                    {
                        result[x, y, z] = this[x, y] * other[z];
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// Sums up all elements along the given dimension and returns a single projected vector.
        /// </summary>
        /// <param name="dim">Dimension to project onto (0 = x, 1 = y).</param>
        /// <returns>A 1D probability distribution.</returns>
        public ProbabilityDistribution1D Project(int dim)
        {
            if (dim != 0 && dim != 1)
            {
                throw new ArgumentOutOfRangeException("dim", dim, "Dimension must be 0 or 1!");
            }
            ProbabilityDistribution1D result = new ProbabilityDistribution1D(GetLength(dim));

            for (int x = 0; x < GetLength(0); x++)
            {
                for (int y = 0; y < GetLength(1); y++)
                {
                    int index = (dim == 0) ? x : y;
                    result[index] += this[x, y];
                }
            }
            return(result);
        }
예제 #7
0
 /// <summary>
 /// Adds another vector of same size to the current one, optionally weighting them.
 /// </summary>
 /// <param name="other">The vector to be added to this one.</param>
 /// <param name="ownWeight">A factor to multiply own values before addion (optional, default: 1).</param>
 /// <param name="otherWeight">A factor to multiply the other's values before addion (optional, default: 1).</param>
 public void Add(ProbabilityDistribution1D other, float ownWeight = 1f, float otherWeight = 1f)
 {
     if (other.Length != Length)
     {
         throw new ArgumentException("Cannot add distributions of different sizes!");
     }
     if (ownWeight < 0)
     {
         throw new ArgumentOutOfRangeException("ownWeight", ownWeight, "Cannot weight elements by a negative factor!");
     }
     if (otherWeight < 0)
     {
         throw new ArgumentOutOfRangeException("otherWeight", otherWeight, "Cannot multiply elements by a negative factor!");
     }
     for (int i = 0; i < Length; i++)
     {
         this[i] = this[i] * ownWeight + other[i] * otherWeight;
     }
 }
예제 #8
0
 // Convenience function for easier deck distribution calculation
 // Note that because it is CUMULATIVE, DeckDistributionBuffer contains intermediary results and may not be cleared!
 protected void CumulativeUpdateDeckDistribution(float Probability, int[] RemainingCards, ref Distribution1D DeckDistributionBuffer)
 {
     Debug.Assert(RemainingCards.Length == CARD_VECTOR_LENGTH);
     Debug.Assert(DeckDistributionBuffer.Length == CARD_VECTOR_LENGTH);
     if (Probability > 0)
     {
         int sum = AIUtil.SumUpArray(RemainingCards);
         if (sum > 0)
         {
             for (int i = 0; i < CARD_VECTOR_LENGTH; i++)
             {
                 if (RemainingCards[i] > 0)
                 {
                     DeckDistributionBuffer[i] += Probability * RemainingCards[i] / sum;
                 }
             }
         }
     }
 }
예제 #9
0
    /// <summary>
    /// Computes a partial hand distribution of the specified player,
    /// for an assumed world state and the observation of which card they played.
    /// </summary>
    /// <param name="PlayedCardIndex">The index of the card that was played by the opponent.</param>
    /// <param name="HandIndex">The index of the card that the other player is assumed to have in their hand.</param>
    /// <param name="OutDistribution">A reference to a probability distribution that containts return values.</param>
    protected void UpdatePartialHandDistribution(int PlayedCardIndex, int PlayerHand, ref Distribution1D OutDistribution)
    {
        // Don't continue if priori probability is zero
        float PrioriProbability = SingleOpponentHandDistribution[PlayerHand];

        if (PrioriProbability <= 0)
        {
            return;
        }
        // Make a copy of the unaccounted card counters and update it with the "virtually" accounted-for cards
        Array.Copy(CountUnaccountedForCards, 1, virtualRemainingCards, 0, CARD_VECTOR_LENGTH);
        if (--virtualRemainingCards[PlayerHand] < 0)
        {
            return; // If a counter goes below zero, this is an impossible case, so return without an update
        }
        // Prepare computation
        int remainingDeckSize = AIUtil.SumUpArray(virtualRemainingCards);

        Debug.Assert(remainingDeckSize > 0);
        // Now loop through each deck card and see if the card that was played could have been played from hand or from deck
        for (int dc = 0; dc < CARD_VECTOR_LENGTH; dc++)
        {
            if (virtualRemainingCards[dc] > 0 && (PlayedCardIndex == PlayerHand || PlayedCardIndex == dc))
            {
                // Compute which card the player has left in their hand, given PlayedCardIndex
                int otherCard = (PlayedCardIndex == PlayerHand) ? dc : PlayerHand;
                // Calculate the joint probability of such play
                OutDistribution[otherCard] += PrioriProbability * virtualRemainingCards[dc] / remainingDeckSize *
                                              PosterioriPerceptor.LikelihoodOfPlay[PlayedCardIndex + 1, otherCard + 1];
            }
        }
    }