Beispiel #1
0
        private static void LoadFromFile_d()
        {
            if (!File.Exists("nodeMap_d.txt"))
            {
                return;
            }
            Console.WriteLine("Loading nodes from file nodeMap_d.txt...");
            using FileStream fs       = File.OpenRead("nodeMap_d.txt");
            using BinaryReader reader = new BinaryReader(fs);
            Global.nodeMapBaseline    = new ConcurrentDictionary <string, Infoset>();

            try
            {
                while (true)
                {
                    int     keyLength   = reader.ReadInt32();
                    byte[]  key         = reader.ReadBytes(keyLength);
                    string  keyString   = Encoding.ASCII.GetString(key);
                    int     valueLength = reader.ReadInt32();
                    byte[]  value       = reader.ReadBytes(valueLength);
                    Infoset infoset     = Deserialize(value);
                    Global.nodeMapBaseline.TryAdd(keyString, infoset);
                }
            }
            catch (EndOfStreamException e)
            {
                return;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Recursively update the strategy for the tree of player
        /// </summary>
        public void UpdateStrategy(State gs, int traverser)
        {
            if (gs.BettingRound() > 1 || gs is TerminalState || !gs.IsPlayerInHand(traverser))
            {
                return;
            }
            else if (gs is ChanceState)
            {
                gs = gs.DoRandomAction();
                UpdateStrategy(gs, traverser);
            }
            else if (gs.IsPlayerTurn(traverser))
            {
                Infoset infoset     = gs.GetInfoset();
                float[] sigma       = infoset.CalculateStrategy();
                int     randomIndex = Util.SampleDistribution(sigma);
                gs.CreateChildren();
                gs = gs.children[randomIndex];
                infoset.actionCounter[randomIndex]++;

                UpdateStrategy(gs, traverser);
            }
            else
            {
                gs.CreateChildren();
                foreach (State state in gs.children)
                {
                    UpdateStrategy(state, traverser);
                }
            }
        }
Beispiel #3
0
        public override Infoset GetInfoset()
        {
            // Betting history R, A, CH, C, F
            // Player whose turn it is // not needed?
            // Cards of player whose turn it is
            // community cards

            if (infosetStringGenerated == false)
            {
                string historyString = string.Join("", history);

                List <int> cards = new List <int>
                {
                    Card.GetIndexFromBitmask(playerCards[playerToMove].Item1),
                    Card.GetIndexFromBitmask(playerCards[playerToMove].Item2)
                };
                for (int i = 0; i < tableCards.Count; ++i)
                {
                    cards.Add(Card.GetIndexFromBitmask(tableCards[i]));
                }
                int[] cardArray = cards.ToArray();

                string cardString = "";
                if (tableCards.Count == 0)
                {
                    long index = Global.indexer_2.indexLast(cardArray);
                    cardString += "P" + index;
                }
                else if (tableCards.Count == 3)
                {
                    long index = EMDTable.flopIndices[Global.indexer_2_3.indexLast(cardArray)];
                    cardString += "F" + index;
                }
                else if (tableCards.Count == 4)
                {
                    long index = EMDTable.turnIndices[Global.indexer_2_4.indexLast(cardArray)];
                    cardString += "T" + index;
                }
                else
                {
                    long index = OCHSTable.riverIndices[Global.indexer_2_5.indexLast(cardArray)];
                    cardString += "R" + index;
                }
                infosetString          = historyString + cardString;
                infosetStringGenerated = true;
            }

            Global.nodeMap.TryGetValue(infosetString, out Infoset infoset);
            if (infoset != null)
            {
                return(infoset);
            }
            else
            {
                infoset = new Infoset(GetValidActionsCount());
                Infoset infosetRet = Global.nodeMap.GetOrAdd(infosetString, infoset);
                return(infosetRet);
            }
        }
Beispiel #4
0
        public void PrintStatistics(long iterations)
        {
            ResetGame();
            List <PlayState> gs = ((ChanceState)rootState).GetFirstActionStates();

            int maxOutput = -1; // todo

            foreach (PlayState ps in gs)
            {
                if (maxOutput < 0)
                {
                    break;
                }
                maxOutput--;

                Infoset infoset = ps.GetInfoset();

                Hand hand = new Hand();
                hand.Cards.Add(new Card(ps.playerCards[ps.playerToMove].Item1));
                hand.Cards.Add(new Card(ps.playerCards[ps.playerToMove].Item2));
                hand.PrintColoredCards("\n");
                List <ACTION> actions = ps.GetValidActions();

                for (int j = 0; j < actions.Count(); ++j)
                {
                    if (actions[j] == ACTION.FOLD)
                    {
                        Console.Write("FOLD: ");
                    }
                    if (actions[j] == ACTION.CALL)
                    {
                        Console.Write("CALL: ");
                    }
                    if (actions[j] == ACTION.RAISE1)
                    {
                        Console.Write(Global.raises[0] + "*POT ");
                    }
                    if (actions[j] == ACTION.RAISE2)
                    {
                        Console.Write(Global.raises[1] + "*POT ");
                    }
                    if (actions[j] == ACTION.RAISE3)
                    {
                        Console.Write(Global.raises[2] + "*POT ");
                    }
                    if (actions[j] == ACTION.ALLIN)
                    {
                        Console.Write("ALLIN: ");
                    }
                    Console.Write("Regret: " + infoset.regret[j].ToString("0.00") + " ");
                    Console.Write("ActionCounter: " + infoset.actionCounter[j].ToString("0.00") + " ");
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
            Console.WriteLine("Number of infosets: " + Global.nodeMap.Count);
            Console.WriteLine("Number of training iterations: " + iterations);
        }
Beispiel #5
0
        private static Infoset Deserialize(this byte[] byteArray)
        {
            if (byteArray == null)
            {
                return(null);
            }
            using var memStream = new MemoryStream();
            var binForm = new BinaryFormatter();

            memStream.Write(byteArray, 0, byteArray.Length);
            memStream.Seek(0, SeekOrigin.Begin);
            Infoset obj = (Infoset)binForm.Deserialize(memStream);

            return(obj);
        }
Beispiel #6
0
        private float TraverseMCCFR(State gs, int traverser, int iteration)
        {
            if (gs is TerminalState)
            {
                return(gs.GetReward(traverser));
            }
            else if (!gs.IsPlayerInHand(traverser)) // we cant get the reward because this function is not implemented
            {
                return(-gs.bets[traverser]);        // correct?
            }
            else if (gs is ChanceState)
            {
                // sample a from chance
                return(TraverseMCCFR(gs.DoRandomAction(), traverser, iteration));
            }
            else if (gs.IsPlayerTurn(traverser))
            {
                //Infoset of player i that corresponds to h
                Infoset infoset     = gs.GetInfoset();
                float[] sigma       = infoset.CalculateStrategy();
                float   expectedVal = 0.0f;

                gs.CreateChildren();
                List <float> expectedValsChildren = new List <float>();
                for (int i = 0; i < gs.children.Count; ++i)
                {
                    expectedValsChildren.Add(TraverseMCCFR(gs.children[i], traverser, iteration));
                    expectedVal += sigma[i] * expectedValsChildren.Last();
                }
                for (int i = 0; i < gs.children.Count; ++i)
                {
                    infoset.regret[i] += expectedValsChildren[i] - expectedVal;
                    infoset.regret[i]  = Math.Max(Global.regretFloor, infoset.regret[i]);
                }
                return(expectedVal);
            }
            else
            {
                Infoset infoset = gs.GetInfoset();
                float[] sigma   = infoset.CalculateStrategy();

                int randomIndex = Util.SampleDistribution(sigma);
                gs.CreateChildren();

                return(TraverseMCCFR(gs.children[randomIndex], traverser, iteration));
            }
        }
Beispiel #7
0
        private static void LoadFromFile()
        {
            if (!File.Exists("nodeMap.txt"))
            {
                return;
            }
            Console.WriteLine("Loading nodes from file nodeMap.txt...");
            using FileStream fs       = File.OpenRead("nodeMap.txt");
            using BinaryReader reader = new BinaryReader(fs);
            Global.nodeMap            = new ConcurrentDictionary <string, Infoset>();

            try
            {
                while (true)
                {
                    int    keyLength   = reader.ReadInt32();
                    byte[] key         = reader.ReadBytes(keyLength);
                    string keyString   = Encoding.ASCII.GetString(key);
                    int    valueLength = reader.ReadInt32();

                    Infoset infoset = new Infoset(valueLength);
                    for (int i = 0; i < valueLength; i++)
                    {
                        infoset.actionCounter[i] = reader.ReadInt32();
                    }
                    for (int i = 0; i < valueLength; i++)
                    {
                        infoset.regret[i] = reader.ReadInt32();
                    }
                    Global.nodeMap.TryAdd(keyString, infoset);
                }
            }
            catch (EndOfStreamException e)
            {
                return;
            }
        }
Beispiel #8
0
        private float TraverseMCCFRPruned(State gs, int traverser)
        {
            if (gs is TerminalState)
            {
                return(gs.GetReward(traverser));
            }
            else if (!gs.IsPlayerInHand(traverser)) // we cant get the reward because this function is not implemented
            {
                return(-gs.bets[traverser]);        // correct?
            }
            else if (gs is ChanceState)
            {
                // sample a from chance
                return(TraverseMCCFRPruned(gs.DoRandomAction(), traverser));
            }
            else if (gs.IsPlayerTurn(traverser))
            {
                // according to supp. mat. page 3, we do full MCCFR on the last betting round, otherwise skip low regret
                if (gs.bettingRound != 4)
                {
                    //Infoset of player i that corresponds to h
                    Infoset infoset     = gs.GetInfoset();
                    float[] sigma       = infoset.CalculateStrategy();
                    float   expectedVal = 0.0f;

                    gs.CreateChildren();
                    List <float> expectedValsChildren = new List <float>();
                    List <bool>  explored             = new List <bool>();
                    for (int i = 0; i < gs.children.Count; ++i)
                    {
                        if (infoset.regret[i] > Global.C)
                        {
                            expectedValsChildren.Add(TraverseMCCFRPruned(gs.children[i], traverser));
                            explored.Add(true);
                            expectedVal += sigma[i] * expectedValsChildren.Last();
                        }
                        else
                        {
                            explored.Add(false);
                        }
                    }
                    for (int i = 0; i < gs.children.Count; ++i)
                    {
                        if (explored[i])
                        {
                            infoset.regret[i] += expectedValsChildren[i] - expectedVal;
                            infoset.regret[i]  = Math.Max(Global.regretFloor, infoset.regret[i]);
                        }
                    }
                    return(expectedVal);
                }
                else
                {
                    // do the same as in normal MCCFR
                    //Infoset of player i that corresponds to h
                    Infoset infoset     = gs.GetInfoset();
                    float[] sigma       = infoset.CalculateStrategy();
                    float   expectedVal = 0.0f;

                    gs.CreateChildren();
                    List <float> expectedValsChildren = new List <float>();
                    for (int i = 0; i < gs.children.Count; ++i)
                    {
                        expectedValsChildren.Add(TraverseMCCFRPruned(gs.children[i], traverser));
                        expectedVal += sigma[i] * expectedValsChildren.Last();
                    }
                    for (int i = 0; i < gs.children.Count; ++i)
                    {
                        infoset.regret[i] += expectedValsChildren[i] - expectedVal;
                        infoset.regret[i]  = Math.Max(Global.regretFloor, infoset.regret[i]);
                    }
                    return(expectedVal);
                }
            }
            else
            {
                Infoset infoset = gs.GetInfoset();
                float[] sigma   = infoset.CalculateStrategy();

                int randomIndex = Util.SampleDistribution(sigma);
                gs.CreateChildren();

                return(TraverseMCCFRPruned(gs.children[randomIndex], traverser));
            }
        }
Beispiel #9
0
        public void PrintStartingHandsChart()
        {
            ResetGame();
            List <PlayState> gs = ((ChanceState)rootState).GetFirstActionStates();

            for (int i = 0; i < gs[0].GetValidActions().Count; ++i)
            {
                if (gs[0].GetValidActions()[i] == ACTION.FOLD)
                {
                    Console.WriteLine("FOLD Table");
                }
                if (gs[0].GetValidActions()[i] == ACTION.CALL)
                {
                    Console.WriteLine("CALL Table");
                }
                if (gs[0].GetValidActions()[i] == ACTION.RAISE1)
                {
                    Console.WriteLine(Global.raises[0] + "*POT RAISE " + "Table");
                }
                if (gs[0].GetValidActions()[i] == ACTION.RAISE2)
                {
                    Console.WriteLine(Global.raises[1] + "*POT RAISE " + "Table");
                }
                if (gs[0].GetValidActions()[i] == ACTION.RAISE3)
                {
                    Console.WriteLine(Global.raises[2] + "*POT RAISE " + "Table");
                }
                if (gs[0].GetValidActions()[i] == ACTION.ALLIN)
                {
                    Console.WriteLine("ALLIN Table");
                }

                Console.WriteLine("    2    3    4    5    6    7    8    9    T    J    Q    K    A (suited)");
                for (int j = 0; j < gs.Count; ++j)
                {
                    PlayState ps      = gs[j];
                    Infoset   infoset = ps.GetInfoset();
                    //List<float> sigma = infoset.CalculateStrategy();
                    float[] phi = infoset.GetFinalStrategy();

                    if (j % 13 == 0 && j + 1 < gs.Count)
                    {
                        if (j / 13 < 8)
                        {
                            Console.Write((j / 13 + 2) + " ");
                        }
                        else if (j / 13 == 8)
                        {
                            Console.Write("T ");
                        }
                        else if (j / 13 == 9)
                        {
                            Console.Write("J ");
                        }
                        else if (j / 13 == 10)
                        {
                            Console.Write("Q ");
                        }
                        else if (j / 13 == 11)
                        {
                            Console.Write("K ");
                        }
                        else
                        {
                            Console.Write("A ");
                        }
                    }

                    if (phi[i] <= 0.25)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    else if (phi[i] <= 0.5)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkRed;
                    }
                    else if (phi[i] <= 0.75)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                    }
                    else if (phi[i] <= 1.0)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }

                    Console.Write(phi[i].ToString("0.00") + " ");
                    Console.ResetColor();

                    if ((j + 1) % 13 == 0)
                    {
                        Console.WriteLine();
                    }
                }

                Console.WriteLine();
                Console.WriteLine();
            }
        }
Beispiel #10
0
        public void PlayOneGame()
        {
            ResetGame();
            State gs    = rootState;
            bool  first = true;

            while (!(gs is TerminalState))
            {
                if (gs is ChanceState)
                {
                    // sample a from chance
                    gs = gs.DoRandomAction();

                    Console.WriteLine();

                    if (first)
                    {
                        Console.Write("Player Cards: ");
                        for (int i = 0; i < Global.nofPlayers; ++i)
                        {
                            List <Card> playerCards = new List <Card>();
                            playerCards.Add(new Card(gs.playerCards[i].Item1));
                            playerCards.Add(new Card(gs.playerCards[i].Item2));
                            playerCards[0].PrintBeautifulString();
                            playerCards[1].PrintBeautifulString(" ");
                        }
                        first = false;
                    }
                    else
                    {
                        if (gs.tableCards.Count != 0)
                        {
                            Console.Write("Table Cards: ");
                        }
                        for (int i = 0; i < gs.tableCards.Count; ++i)
                        {
                            new Card(gs.tableCards[i]).PrintBeautifulString();
                        }
                    }
                }
                else if (gs is PlayState)
                {
                    Console.WriteLine();
                    Console.Write("Player {0}'s turn : ", gs.playerToMove);
                    Infoset infoset = gs.GetInfoset();
                    float[] sigma   = infoset.CalculateStrategy();

                    int randomIndex = Util.SampleDistribution(sigma);
                    gs.CreateChildren();
                    gs = gs.children[randomIndex];
                    Console.Write(gs.history[gs.history.Count - 1]);
                }
            }
            Console.WriteLine();
            Console.Write("Rewards: ");
            for (int i = 0; i < Global.nofPlayers; ++i)
            {
                Console.Write(gs.GetReward(i) + " ");
            }
            Console.WriteLine();
        }