public InitData(FictitiousPlayMc solver) { int playersCount = solver._at.PlayersCount; AtChildrenIndex = new UFTreeChildrenIndex(solver._at); CreatePlayerCt(solver.CardCount); RoundsCount = solver.CardCount.Length; }
private void PrepareHero() { _maxCard = new int[PLAYERS_COUNT][]; _chanceIndexSizes = new int[PLAYERS_COUNT][]; _heroVarsInLeaves = new int[ActionTree.NodesCount][]; _roundsCount = ChanceTree.CalculateRoundsCount(); for (int p = 0; p < PLAYERS_COUNT; ++p) { _maxCard[p] = new int[_roundsCount].Fill(int.MinValue); _chanceIndexSizes[p] = new int[_roundsCount].Fill(int.MinValue); } for (Int64 n = 1; n < ChanceTree.NodesCount; ++n) { int round = (ChanceTree.GetDepth(n) - 1) / PLAYERS_COUNT; if (_maxCard[ChanceTree.Nodes[n].Position][round] < ChanceTree.Nodes[n].Card) { _maxCard[ChanceTree.Nodes[n].Position][round] = ChanceTree.Nodes[n].Card; } } for (int p = 0; p < PLAYERS_COUNT; ++p) { _chanceIndexSizes[p][0] = _maxCard[p][0] + 1; for (int r = 1; r < _roundsCount; ++r) { _chanceIndexSizes[p][r] = _chanceIndexSizes[p][r - 1] * (_maxCard[p][r] + 1); } } _actionTreeIndex = new UFTreeChildrenIndex(ActionTree); for (Int64 n = 1; n < ActionTree.NodesCount; ++n) { if (_actionTreeIndex.GetChildrenCount(n) == 0) { // This is a leaf int round = ActionTree.Nodes[n].Round; _heroVarsInLeaves[n] = new int[_chanceIndexSizes[HeroPosition][round]].Fill(-1); } } // Create hero variables WalkUFTreePP <StrategyTree, PrepareHeroVarsContext> wt = new WalkUFTreePP <StrategyTree, PrepareHeroVarsContext>(); wt.OnNodeBegin = PrepareHero_OnNodeBegin; wt.Walk(Strategy, PLAYERS_COUNT); }
/// <summary> /// Finds a child of node with the given amount. A child pointed by hint is checked first. /// This allows to skip searching if the position of this child is known (for example from another /// tree for the same game). /// </summary> public long FindChildByAmount(long nodeIdx, double amount, UFTreeChildrenIndex index, int hintChildIdx) { int chBegin, chCount; index.GetChildrenBeginIdxAndCount(nodeIdx, out chBegin, out chCount); if (hintChildIdx < chCount) { int chIdx = index.GetChildIdx(chBegin + hintChildIdx); if (Nodes[chIdx].Amount == amount) { return(chIdx); } } return(FindChildByAmount(nodeIdx, amount, index)); }
/// <summary> /// Finds a child of node with the given amount by looking up all children using the index.. /// </summary> public long FindChildByAmount(long nodeIdx, double amount, UFTreeChildrenIndex index) { int childCount, chBegin; index.GetChildrenBeginIdxAndCount(nodeIdx, out chBegin, out childCount); for (int ch = 0; ch < childCount; ++ch) { int chIdx = index.GetChildIdx(chBegin + ch); if (Nodes[chIdx].Amount == amount) { return(chIdx); } } return(-1); }
/// <summary> /// Finds a node by an action path. Action path must not contain the root node and the blinds. /// This function is not speed-optimized. /// </summary> /// <param name="path"></param> /// <returns></returns> public Int64 FindNode(IStrategicAction[] path) { UFTreeChildrenIndex actionTreeIndex = new UFTreeChildrenIndex(this); Int64 curNode = PlayersCount; for (int a = 0; a < path.Length; ++a) { int childCount, chBegin; actionTreeIndex.GetChildrenBeginIdxAndCount(curNode, out chBegin, out childCount); for (int ch = 0; ch < childCount; ++ch) { int chIdx = actionTreeIndex.GetChildIdx(chBegin + ch); if (path[a].Position == Nodes[chIdx].Position) { if (Nodes[chIdx].IsDealerAction) { IDealerAction da = (IDealerAction)path[a]; if (da.Card == Nodes[chIdx].Card) { curNode = chIdx; goto found; } } else { IPlayerAction pa = (IPlayerAction)path[a]; if (pa.Amount == Nodes[chIdx].Amount) { curNode = chIdx; goto found; } } } } throw new ApplicationException(String.Format("Cannot find child at node {0} for action {1}", curNode, path[a])); found: ; } return(curNode); }
private void Prepare() { _playersCount = Strategies.Length; _maxCard = new int[_playersCount][]; _spArrayLengths = new int[_playersCount][]; _spArrays = new double[_playersCount][][].Fill(i => new double[ActionTree.NodesCount][]); if (PrepareVis) { _visLeaveValues = new double[_playersCount][][].Fill(i => new double[ActionTree.NodesCount][]); } _gameValues = new double[_playersCount]; _roundsCount = ChanceTree.CalculateRoundsCount(); for (int p = 0; p < _playersCount; ++p) { _maxCard[p] = new int[_roundsCount].Fill(int.MinValue); _spArrayLengths[p] = new int[_roundsCount].Fill(int.MinValue); } for (Int64 n = 1; n < ChanceTree.NodesCount; ++n) { int round = (ChanceTree.GetDepth(n) - 1) / _playersCount; if (_maxCard[ChanceTree.Nodes[n].Position][round] < ChanceTree.Nodes[n].Card) { _maxCard[ChanceTree.Nodes[n].Position][round] = ChanceTree.Nodes[n].Card; } } for (int p = 0; p < _playersCount; ++p) { _spArrayLengths[p][0] = _maxCard[p][0] + 1; for (int r = 1; r < _roundsCount; ++r) { _spArrayLengths[p][r] = _spArrayLengths[p][r - 1] * (_maxCard[p][r] + 1); } } _actionTreeIndex = new UFTreeChildrenIndex(ActionTree); for (Int64 n = 1; n < ActionTree.NodesCount; ++n) { if (_actionTreeIndex.GetChildrenCount(n) == 0) { // This is a leaf int round = ActionTree.Nodes[n].Round; for (int p = 0; p < _playersCount; ++p) { _spArrays[p][n] = new double[_spArrayLengths[p][round]]; if (PrepareVis) { _visLeaveValues[p][n] = new double[_spArrayLengths[p][round]]; } } } } for (_curPlayer = 0; _curPlayer < _playersCount; ++_curPlayer) { WalkUFTreePP <StrategyTree, PrepareContext> wt = new WalkUFTreePP <StrategyTree, PrepareContext>(); wt.OnNodeBegin = Prepare_OnNodeBegin; wt.Walk(Strategies[_curPlayer]); } }
void Initialize() { _playerInfoProps = new Props(); string strDir = _creationParams.Get("StrategyDir"); _playerInfoProps.Set("StrategyDir", strDir); string propsFile = Path.Combine(strDir, "props.xml"); Props props = XmlSerializerExt.Deserialize <Props>(propsFile); int rngSeed = int.Parse(_creationParams.GetDefault("RngSeed", "0")); if (rngSeed == 0) { rngSeed = (int)DateTime.Now.Ticks; } _checkBlinds = bool.Parse(_creationParams.GetDefault("CheckBlinds", "true")); string amountCompareMethodText = _creationParams.GetDefault("AmountSearchMethod", "Equal"); if (amountCompareMethodText == "Equal") { _amountSearchMethod = AmountSearchMethod.Equal; } else if (amountCompareMethodText == "Closest") { _amountSearchMethod = AmountSearchMethod.Closest; } else { throw new ApplicationException(string.Format("Unknown amount compare method {0}", amountCompareMethodText)); } _relProbabIgnoreLevel = double.Parse(_creationParams.GetDefault("RelProbabIgnoreLevel", "0.0"), CultureInfo.InvariantCulture); _playerInfoProps.Set("RelProbabIgnoreLevel", _relProbabIgnoreLevel.ToString()); // Use MersenneTwister because it is under our control on all platforms and // is probably better than System.Random. Random underlyingRng = new MersenneTwister(rngSeed); _moveSelector = new DiscreteProbabilityRng(underlyingRng); _playerInfoProps.Set("RngSeed", rngSeed.ToString()); _deckDescr = XmlSerializerExt.Deserialize <DeckDescriptor>(props.Get("DeckDescriptor")); _chanceAbsrtractions = new IChanceAbstraction[0]; // Create chance abstractions for (int pos = 0; ; pos++) { string caPropName = String.Format("ChanceAbstraction-{0}", pos); string relFileName = props.Get(caPropName); if (string.IsNullOrEmpty(relFileName)) { break; } Array.Resize(ref _chanceAbsrtractions, _chanceAbsrtractions.Length + 1); string absFileName = Path.Combine(strDir, relFileName); _chanceAbsrtractions[pos] = ChanceAbstractionHelper.CreateFromPropsFile(absFileName); _playerInfoProps.Set(caPropName + ".Name", _chanceAbsrtractions[pos].Name); } Dictionary <string, int> fileToPos = new Dictionary <string, int>(); _strategies = new StrategyTree[0]; _strIndexes = new UFTreeChildrenIndex[0]; // Load strategies, reuse if file is the same for (int pos = 0; ; pos++) { string strPropName = String.Format("Strategy-{0}", pos); string relFileName = props.Get(strPropName); if (string.IsNullOrEmpty(relFileName)) { break; } Array.Resize(ref _strategies, _strategies.Length + 1); Array.Resize(ref _strIndexes, _strIndexes.Length + 1); string absFileName = Path.Combine(strDir, relFileName); int existingPos; if (fileToPos.TryGetValue(absFileName, out existingPos)) { _strategies[pos] = _strategies[existingPos]; _strIndexes[pos] = _strIndexes[existingPos]; } else { fileToPos.Add(absFileName, pos); _strategies[pos] = StrategyTree.ReadFDA <StrategyTree>(absFileName); _strIndexes[pos] = new UFTreeChildrenIndex(_strategies[pos], Path.Combine(strDir, "strategy-idx.dat"), false); } _playerInfoProps.Set(strPropName + ".Version", _strategies[pos].Version.ToString()); } // Read blinds for (int strPos = 0; strPos < _strategies.Length; ++strPos) { StringBuilder sb = new StringBuilder(); for (int playerPos = 0; playerPos < _strategies.Length; ++playerPos) { StrategyTreeNode stNode = new StrategyTreeNode(); _strategies[strPos].GetNode(playerPos + 1, &stNode); sb.AppendFormat("{0:0.000000 }", stNode.Amount); } _playerInfoProps.Set("Blinds." + strPos.ToString(), sb.ToString()); } }
private void Prepare() { CreateHeroStrategy(); _playersCount = Strategies.Length; _maxCard = new int[_playersCount][]; _chanceIndexSizes = new int[_playersCount][]; _strategicProbabs = new double[_playersCount][][].Fill(i => new double[ActionTree.NodesCount][]); if (PrepareVis) { _visGameValues = new double[Strategies[HeroPosition].NodesCount]; } else { _visGameValues = null; } _roundsCount = ChanceTree.CalculateRoundsCount(); for (int p = 0; p < _playersCount; ++p) { _maxCard[p] = new int[_roundsCount].Fill(int.MinValue); _chanceIndexSizes[p] = new int[_roundsCount].Fill(int.MinValue); } for (Int64 n = 1; n < ChanceTree.NodesCount; ++n) { int round = (ChanceTree.GetDepth(n) - 1) / _playersCount; if (_maxCard[ChanceTree.Nodes[n].Position][round] < ChanceTree.Nodes[n].Card) { _maxCard[ChanceTree.Nodes[n].Position][round] = ChanceTree.Nodes[n].Card; } } for (int p = 0; p < _playersCount; ++p) { _chanceIndexSizes[p][0] = _maxCard[p][0] + 1; for (int r = 1; r < _roundsCount; ++r) { _chanceIndexSizes[p][r] = _chanceIndexSizes[p][r - 1] * (_maxCard[p][r] + 1); } } _actionTreeIndex = new UFTreeChildrenIndex(ActionTree); for (Int64 n = 1; n < ActionTree.NodesCount; ++n) { if (_actionTreeIndex.GetChildrenCount(n) == 0) { // This is a leaf int round = ActionTree.Nodes[n].Round; for (int p = 0; p < _playersCount; ++p) { if (p != HeroPosition) { _strategicProbabs[p][n] = new double[_chanceIndexSizes[p][round]]; } } } } // Fill strategic probability for each player except the hero. for (_curPlayer = 0; _curPlayer < _playersCount; ++_curPlayer) { if (_curPlayer == HeroPosition) { continue; } WalkUFTreePP <StrategyTree, PrepareStrategicProbabsContext> wt = new WalkUFTreePP <StrategyTree, PrepareStrategicProbabsContext>(); wt.OnNodeBegin = PrepareStrategicProbabs_OnNodeBegin; wt.Walk(Strategies[_curPlayer]); } _chanceTreeNodes = new int[_roundsCount][][]; for (int r = 0; r < _roundsCount; ++r) { int oppChanceIndexSize = 1; for (int p = 0; p < _playersCount; ++p) { if (p == HeroPosition) { continue; } oppChanceIndexSize *= _chanceIndexSizes[p][r]; } int size = _chanceIndexSizes[HeroPosition][r]; _chanceTreeNodes[r] = new int[size][]; for (int i = 0; i < size; ++i) { _chanceTreeNodes[r][i] = new int[oppChanceIndexSize]; } } WalkUFTreePP <ChanceTree, PrepareChanceIndexContext> wt1 = new WalkUFTreePP <ChanceTree, PrepareChanceIndexContext>(); wt1.OnNodeBegin = PrepareChanceIndex_OnNodeBegin; wt1.Walk(ChanceTree); }