// 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; }
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(); }
/// <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); }
/// <summary> /// Adds another matrix of same size to the current one, optionally weighting them. /// </summary> /// <param name="other">The matrix 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(ProbabilityDistribution2D other, float ownWeight = 1f, float otherWeight = 1f) { if (other.GetLength(0) != GetLength(0) || other.GetLength(1) != GetLength(1)) { 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 x = 0; x < GetLength(0); x++) { for (int y = 0; y < GetLength(1); y++) { this[x, y] = this[x, y] * ownWeight + other[x, y] * otherWeight; } } }
/// <summary> /// Computes a partial hand distribution of the specified player's new hand after being targeted by the Prince, /// for an assumed world state and the observation of which card they previously discarded. /// </summary> /// <param name="PlayerIndex">Index of the player for whom the computation is performed.</param> /// <param name="Hand0">The index of the card the first of the two remaining opponents is asssumed to hold.</param> /// <param name="Hand1">The index of the card the second of the two remaining opponents is asssumed to hold.</param> /// <param name="DiscardedIndex">The observed index of the card that the player had discarded.</param> /// <param name="OutDistribution">A reference to a probability distribution that containts return values.</param> protected void PrinceDiscardAndDrawPartialUpdate(int PlayerIndex, int Hand0, int Hand1, int DiscardedIndex, ref Distribution2D OutDistribution) { // Don't continue if priori probability is zero float PrioriProbability = TwoOpponentsHandsDistribution[Hand0, Hand1]; if (PrioriProbability <= 0) { return; } // Determine which card in the current data belongs to PlayerIndex int[] idx = new int[] { Hand0, Hand1 }; int playerHand = idx[PlayerIndex]; if (playerHand != DiscardedIndex) { 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[Hand0] < 0 || --virtualRemainingCards[Hand1] < 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); if (remainingDeckSize < 1) { return; // The game is over, anyway. } 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) { // Calculate the joint probability of such play and increment the output array idx[PlayerIndex] = dc; OutDistribution[idx[0], idx[1]] += PrioriProbability * virtualRemainingCards[dc] / remainingDeckSize; } } }
/// <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="TurnData">Complete data of the turn being analyzed.</param> /// <param name="Hand0">The index of the card the first of the two remaining opponents is asssumed to hold.</param> /// <param name="Hand1">The index of the card the second of the two remaining opponents is asssumed to hold.</param> /// <param name="OutDistribution">A reference to a probability distribution that containts return values.</param> protected void UpdatePartialHandDistribution(MoveData TurnData, int Hand0, int Hand1, ref Distribution2D OutDistribution) { // Don't continue if priori probability is zero float PrioriProbability = TwoOpponentsHandsDistribution[Hand0, Hand1]; 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[Hand0] < 0 || --virtualRemainingCards[Hand1] < 0) { return; // If a counter goes below zero, this is an impossible case, so return without an update } // Parse the turn data int PlayerIndex = GetHiddenHandIndex(TurnData.Player.SittingOrder), PlayedCardIndex = TurnData.Card.Value - 1, TargetIndex = (TurnData.Target == null || TurnData.Target.SittingOrder == MySittingOrder) ? -1 : GetHiddenHandIndex(TurnData.Target.SittingOrder); // Determine which card in the current data belongs to PlayerIndex int[] idx = new int[] { Hand0, Hand1 }; int playerHand = idx[PlayerIndex], targetHand = TargetIndex != -1 ? idx[TargetIndex] : -1; // 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; // Prepare the index values for the update idx[PlayerIndex] = otherCard; // Calculate the joint probability of such play and increment the output array OutDistribution[idx[0], idx[1]] += PrioriProbability * virtualRemainingCards[dc] / remainingDeckSize * GetLikelihoodOfPlay(TurnData, otherCard + 1, targetHand + 1); } } }