public override void CalculateTree(MiniMaxNode <TicTacGameState> root, bool maximizer)
        {
            if (root.children == null || root.children.Length == 0)
            {
                if (((TicTacGameState)root.gameState).Tie)
                {
                    root.Value = 0;
                }
                else if (((TicTacGameState)root.gameState).XerVictory)
                {
                    root.Value = 1;
                }
                else
                {
                    root.Value = -1;
                }
                return;
            }

            CalculateTree(root.children[0], !maximizer);
            root.Value = root.children[0].Value;
            foreach (MiniMaxNode <TicTacGameState> child in root.children)
            {
                CalculateTree(child, !maximizer);
                if (maximizer && child.Value > root.Value)
                {
                    root.Value = child.Value;
                }
                else if (!maximizer && child.Value < root.Value)
                {
                    root.Value = child.Value;
                }
            }
        }
示例#2
0
        public void GenerateTree(MiniMaxNode <CheckersGS> root, bool maximizer, int layerDepth)
        {
            Stack <(bool maximizer, MiniMaxNode <CheckersGS> node, int depth)> miniMaxNodes = new Stack <(bool maximizer, MiniMaxNode <CheckersGS> node, int depth)>();
            List <CheckersGS> states = new List <CheckersGS>(layerDepth * 8);

            miniMaxNodes.Push((maximizer, root, 0));
            while (miniMaxNodes.Count > 0)
            {
                (bool maximizer, MiniMaxNode <CheckersGS> node, int depth)currentNode = miniMaxNodes.Pop();
                if (currentNode.depth == layerDepth)
                {
                    continue;
                }
                currentNode.node.gameState.GetPossibleStates(currentNode.maximizer, states);
                currentNode.node.children = new MiniMaxNode <CheckersGS> [states.Count];
                for (int i = 0; i < states.Count; i++)
                {
                    currentNode.node.children[i] = new MiniMaxNode <CheckersGS>(states[i]);
                    if (!states[i].gameFinished)
                    {
                        miniMaxNodes.Push((!currentNode.maximizer, currentNode.node.children[i], currentNode.depth + 1));
                    }
                }
            }
        }
示例#3
0
        public static void MonteCarlo(MiniMaxNode <CheckersGS> leaf, bool maximizer)
        {
            if (leaf.gameState.gameFinished)
            {
                if (leaf.gameState.Tie)
                {
                    leaf.Value = 0;
                    return;
                }
                if (leaf.gameState.XerVictory)
                {
                    leaf.Value = double.MaxValue;
                    return;
                }
                else
                {
                    leaf.Value = double.MinValue;
                    return;
                }
            }


            List <MiniMaxNode <CheckersGS> > roots = new List <MiniMaxNode <CheckersGS> >();

            roots.Add(leaf);

            Random random         = new Random();
            var    carloNodes     = new (int wins, MiniMaxNode <CheckersGS> node)[1000];
示例#4
0
        public override void CalculateTree(MiniMaxNode <CheckersGS> root, bool maximizer)
        {
            if (root.children == null || root.children.Length == 0)
            {
                //if game over set values
                if (root.gameState.gameFinished)
                {
                    if (root.gameState.Tie)
                    {
                        root.Value = 0.5;
                    }
                    else if (root.gameState.XerVictory)
                    {
                        root.Value = int.MaxValue;
                    }
                    else
                    {
                        root.Value = int.MinValue;
                    }
                }
                return;
            }

            CalculateTree(root.children[0], !maximizer);
            root.Value = root.children[0].Value;
            foreach (MiniMaxNode <CheckersGS> child in root.children)
            {
                CalculateTree(child, !maximizer);
                if (child.pruned)
                {
                    continue;
                }
                if (maximizer && child.Value > root.Value)
                {
                    root.Value = child.Value;
                }
                else if (!maximizer && child.Value < root.Value)
                {
                    root.Value = child.Value;
                }
            }
        }
示例#5
0
        private double AlphaBetaMonteCarlo(MiniMaxNode <CheckersGS> root, bool maximizer, double alpha, double beta)
        {
            double bestVal = maximizer ? double.MinValue : double.MaxValue;

            if (root.children == null || root.children.Length == 0)
            {
                if (monteNet == null)
                {
                    MonteCarlo(root, maximizer);
                }
                else
                {
                    root.Value = monteNet.Compute(root.gameState.toDoubles())[0];
                }
                return(root.Value);
            }
            else
            {
                for (int i = 0; i < root.children.Length; i++)
                {
                    double value = AlphaBetaMonteCarlo(root.children[i], !maximizer, alpha, beta);
                    if (maximizer)
                    {
                        bestVal = dMax(bestVal, value);
                        alpha   = dMax(alpha, value);
                    }
                    else
                    {
                        bestVal = dMin(bestVal, value);
                        beta    = dMin(beta, value);
                    }
                    if (beta <= alpha)
                    {
                        root.pruned = true;
                        break;
                    }
                }
            }
            return(bestVal);
        }
示例#6
0
        public override void GenerateTree(MiniMaxNode <CheckersGS> root, bool maximizer)
        {
            Stack <(bool maximizer, MiniMaxNode <CheckersGS> node)> miniMaxNodes = new Stack <(bool maximizer, MiniMaxNode <CheckersGS> node)>();

            List <CheckersGS> states = new List <CheckersGS>();

            miniMaxNodes.Push((maximizer, root));
            while (miniMaxNodes.Count > 0)
            {
                (bool maximizer, MiniMaxNode <CheckersGS> node)currentNode = miniMaxNodes.Pop();
                currentNode.node.gameState.GetPossibleStates(currentNode.maximizer, states);
                currentNode.node.children = new MiniMaxNode <CheckersGS> [states.Count];
                for (int i = 0; i < states.Count; i++)
                {
                    currentNode.node.children[i] = new MiniMaxNode <CheckersGS>(states[i]);
                    if (!states[i].gameFinished)
                    {
                        miniMaxNodes.Push((!currentNode.maximizer, currentNode.node.children[i]));
                    }
                }
            }
        }
示例#7
0
        private double AlphaBetaMonteCarlo(MiniMaxNode <Connect4GS> root, bool maximizer, double alpha, double beta)
        {
            loops++;
            if (loops > 20)
            {
                Console.SetCursorPosition(0, 0);
                root.gameState.ConsoleWrite(true);
                loops = 0;
            }
            double bestVal = maximizer ? double.MinValue : double.MaxValue;

            if (root.children == null || root.children.Length == 0)
            {
                MonteCarlo(root, maximizer);
                return(root.Value);
            }
            else
            {
                for (int i = 0; i < root.children.Length; i++)
                {
                    double value = AlphaBetaMonteCarlo(root.children[i], !maximizer, alpha, beta);
                    if (maximizer)
                    {
                        bestVal = dMax(bestVal, value);
                        alpha   = dMax(alpha, value);
                    }
                    else
                    {
                        bestVal = dMin(bestVal, value);
                        beta    = dMin(beta, value);
                    }
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
            }
            return(bestVal);
        }
示例#8
0
        static void ChxMonteCarloTrain()
        {
            using (StreamWriter fs = new StreamWriter(Path.Combine(@"\\GMRDC1\Folder Redirection\Ryan.Alameddine\Documents\Visual Studio 2017\Projects\NeuralNet\MiniMaxTree\MiniMaxTree\bin\Debug\netcoreapp2.1", "CHX.txt"), true))
            {
                Random rand = new Random();
                for (int i = 0; i < 10000; i++)
                {
                    CheckersGS gs = new CheckersGS();
                    gs.Xer = rand.Next(0, 2) == 1;
                    int xCount = rand.Next(1, 13);
                    int oCount = rand.Next(1, 13);
                    for (int x = 0; x < xCount; x++)
                    {
                        int f = rand.Next(0, 32) * 2;
                        if (f % 16 < 8)
                        {
                            f++;
                        }
                        if (gs.marks[f] != '\0')
                        {
                            x--;
                            continue;
                        }
                        else
                        {
                            if (rand.Next(0, 10) == 0)
                            {
                                gs.marks[f] = 'X';
                            }
                            else
                            {
                                gs.marks[f] = 'x';
                            }
                        }
                    }

                    for (int o = 0; o < oCount; o++)
                    {
                        int f = rand.Next(0, 32) * 2;
                        if (f % 16 < 8)
                        {
                            f++;
                        }
                        if (gs.marks[f] != '\0')
                        {
                            o--;
                            continue;
                        }
                        else
                        {
                            if (rand.Next(0, 10) == 0)
                            {
                                gs.marks[f] = 'O';
                            }
                            else
                            {
                                gs.marks[f] = 'o';
                            }
                        }
                    }
                    var node = new MiniMaxNode <CheckersGS>(gs);
                    Console.SetCursorPosition(0, 0);
                    gs.ConsoleWrite();
                    Console.WriteLine(i);
                    CheckersGM.MonteCarlo(node, gs.Xer);
                    string ln = node.Value + ":" + gs.ToCompact();
                    fs.WriteLine(ln);
                    fs.Flush();
                }
            }
            //File.AppendAllLines("", lines);
        }
示例#9
0
 public double AlphaBetaMonteCarlo(MiniMaxNode <CheckersGS> root, bool maximizer)
 {
     return(AlphaBetaMonteCarlo(root, maximizer, double.MinValue, double.MaxValue));
 }
示例#10
0
 public abstract void GenerateTree(MiniMaxNode <T> root, bool maximizer);
示例#11
0
 public abstract void CalculateTree(MiniMaxNode <T> root, bool maximizer);
        public MiniMaxTree(GameManager <T> GameManager, bool maximizerFirst, T state)
        {
            this.GameManager = GameManager;

            Root = new MiniMaxNode <T>(state);
        }