コード例 #1
0
        // Phase 2: EXPANSION kết hợp dữ liệu BTMM trained
        public static Node ExpandChildBTMM(this Node node)
        {
            // Tính & Lưu lại child Strength, để ko cần tính lại khi expand
            // Cộng child strength để tính allChildStrength luôn
            if (node.ChildStrengths == null)
            {
                node.ChildStrengths = new Dictionary <ulong, double>(node.UntriedMoves.Count);
                double allChildStrength = 0;
                foreach (var bitMove in node.UntriedMoves)
                {
                    double strength = BTMMAlgorithm.StrongOfAction(node.State, bitMove);
                    node.ChildStrengths.Add(bitMove, strength);
                    allChildStrength += strength;
                }

                node.AllChildStrength = allChildStrength;
            }

            // Expand random child
            var i    = Constant.Random.Next(node.UntriedMoves.Count);
            var move = node.UntriedMoves[i];

            node.UntriedMoves.RemoveAt(i); // Untried -> Try

            var newState = node.State.Clone().NextState(move);
            var child    = new Node(newState, node, move);

            child.Strength = node.ChildStrengths[move]; // dùng lại child strength đã được tính trước đó
            node.ChildNodes.Add(child);

            return(child);
        }
コード例 #2
0
        // Distribute probability based on Roulette wheel method
        private static ulong RouletteWheelSelection(this State state)
        {
            // Nếu chỉ có 1 legal move => trả về legal move đó luôn
            if (state.BitLegalMoves.PopCount() == 1)
            {
                return(state.BitLegalMoves);
            }

            // Có từ 2 legal moves trở lên mới cần tính roulette wheel
            var listLegalMoves = state.GetArrayLegalMoves();
            var moveCount      = listLegalMoves.Length;
            var wheel          = new int[moveCount];
            var maxWheel       = 0;

            for (var i = 0; i < moveCount; i++)
            {
                // StrongOfAction trả về giá trị nằm trong khoảng [0.01, 100], nên nhân 1000 để cast int chính xác hơn
                // Câu trên SAI: Gamma nằm mới nằm trong khoảng [0.01, 100], còn strong thì được nhân bởi 1 hoặc nhiều gamma
                var temp = (int)(1000 * BTMMAlgorithm.StrongOfAction(state, listLegalMoves[i]));
                maxWheel += temp;
                wheel[i]  = maxWheel;
            }

            var selectPos = Constant.Random.Next(0, maxWheel);

            for (var i = 0; i < moveCount; i++)
            {
                if (selectPos <= wheel[i])
                {
                    return(listLegalMoves[i]);
                }
            }

            return(0UL);
        }
コード例 #3
0
 private static bool CheckModelReady()
 {
     if (BTMMAlgorithm.IsModelReady())
     {
         return(true);
     }
     Console.WriteLine("[!] You have to Train or Load trained data to be able to use BTMM Algorithm.");
     ConsoleUtil.WriteAndWaitKey("> Go to tab BTMM in MainMenu to Config BTMM.");
     return(false);
 }
コード例 #4
0
        private static void TrainMenu()
        {
            var userChoice = 0;

            while (true)
            {
                userChoice = GetUserChoice(
                    "Train BTMM Menu", new[]
                {
                    "<- Back",
                    $"Game records: {_gameRecordFilePath}",
                    $"Save to: {_saveTrainedFilePath}",
                    "Train now!"
                }, userChoice);

                if (userChoice == 0)
                {
                    return;
                }
                if (userChoice == 1)
                {
                    _gameRecordFilePath = (string)ConsoleUtil.Prompt("Game record", _gameRecordFilePath);
                }
                else if (userChoice == 2)
                {
                    _saveTrainedFilePath = (string)ConsoleUtil.Prompt("Save to", _saveTrainedFilePath);
                }
                else
                {
                    try
                    {
                        BTMMAlgorithm.TrainGameRecord(_gameRecordFilePath, _saveTrainedFilePath);
                        ConsoleUtil.WriteAndWaitKey("> Train End.");
                    }
                    catch (Exception e)
                    {
                        ConsoleUtil.WriteAndWaitKey($"[!] ERROR: {e}");
                    }
                }
            }
        }
コード例 #5
0
        private static void BTMMMenu()
        {
            var userChoice = 0;

            while (true)
            {
                userChoice = GetUserChoice(
                    "BTMM Menu", new[]
                {
                    "<- Back",
                    BTMMAlgorithm.IsModelReady() ? "* Status: READY" : "* Status: NOT READY!!",
                    "Train",
                    "Load",
                    "Read Log"
                }, userChoice);

                if (userChoice == 0)
                {
                    return;
                }
                if (userChoice == 1)
                {
                    CheckModelReady();
                }
                else if (userChoice == 2)
                {
                    TrainMenu();
                }
                else if (userChoice == 3)
                {
                    LoadMenu();
                }
                else
                {
                    ConsoleUtil.WriteAndWaitKey(GameLogger.ReadLog());
                }
            }
        }
コード例 #6
0
        private static void LoadMenu()
        {
            var userChoice = 0;

            while (true)
            {
                userChoice = GetUserChoice(
                    "Load BTMM Menu", new[]
                {
                    "<- Back",
                    $"Trained file: {_saveTrainedFilePath}",
                    "Load now!"
                }, userChoice);

                if (userChoice == 0)
                {
                    return;
                }
                if (userChoice == 1)
                {
                    _saveTrainedFilePath = (string)ConsoleUtil.Prompt("Trained data file", _saveTrainedFilePath);
                }
                else
                {
                    try
                    {
                        BTMMAlgorithm.LoadTrainedData(_saveTrainedFilePath);
                        ConsoleUtil.WriteAndWaitKey("> Load Done.");
                    }
                    catch (Exception e)
                    {
                        ConsoleUtil.WriteAndWaitKey($"[!] ERROR: {e}");
                    }
                }
            }
        }