コード例 #1
0
ファイル: QAgentObsolete.cs プロジェクト: Armienn/Virus
        private Move GetMaxExplorationFunctionA(VirusBoard state)
        {
            double max    = -10;
            Move   action = default(Move);

            Move[] actions = state.GetPossibleMoves(playerNumber);
            foreach (Move a in actions)
            {
                double value = 0;
                if (!Q.ContainsKey(state))
                {
                    Q.Add(state, new Dictionary <Move, double>());
                }
                if (Q[state].ContainsKey(a))
                {
                    value = Q[state][a] + 1 / (double)N[state][a];
                }
                else
                {
                    value = 1;
                }
                if (value > max)
                {
                    max    = value;
                    action = a;
                }
            }
            return(action);
        }
コード例 #2
0
ファイル: QAgentObsolete.cs プロジェクト: Armienn/Virus
        private double GetMaxQ(VirusBoard state)
        {
            double max = -10;

            Move[] actions = state.GetPossibleMoves(playerNumber);
            foreach (Move a in actions)
            {
                double value = 0;
                if (!Q[state].ContainsKey(a))
                {
                    value = 0;
                }
                else
                {
                    value = Q[state][a];
                }
                if (value > max)
                {
                    max = value;
                }
            }
            if (Q[state].ContainsKey(default(Move)))
            {
                if (Q[state][default(Move)] > max)
                {
                    max = Q[state][default(Move)];
                }
            }
            return(max);
        }
コード例 #3
0
ファイル: QAgentSemiObsolete.cs プロジェクト: Armienn/Virus
        private double GetMaxQ(VirusBoard state)
        {
            double max = -10;

            Move[] actions = state.GetPossibleMoves(playerNumber);
            foreach (Move a in actions)
            {
                double value = 0;
                if (!Q[state.CustomHash()].ContainsKey(a.CustomHash()))
                {
                    value = 0;
                }
                else
                {
                    value = Q[state.CustomHash()][a.CustomHash()];
                }
                if (value > max)
                {
                    max = value;
                }
            }
            if (Q[state.CustomHash()].ContainsKey(0))
            {
                if (Q[state.CustomHash()][0] > max)
                {
                    max = Q[state.CustomHash()][0];
                }
            }
            return(max);
        }
コード例 #4
0
ファイル: MiniMaxMixAgent.cs プロジェクト: Armienn/Virus
        public override Move Move(Virus percept)
        {
            VirusBoard currentState = percept.GetBoardCopy();

            Move[] actions = currentState.GetPossibleMoves(playerNumber);
            Move   action  = actions[0];

            double max = double.NegativeInfinity;

            foreach (Move a in actions)
            {
                VirusBoard newState = currentState.GetUpdated(a);
                double     q        = 0;
                if (Q.ContainsKey(currentState.CustomHash()))
                {
                    if (Q[currentState.CustomHash()].ContainsKey(a.CustomHash()))
                    {
                        q = Q[currentState.CustomHash()][a.CustomHash()];
                    }
                }
                q += MinValue(newState, 0);
                if (q > max)
                {
                    max    = q;
                    action = a;
                }
                if (max == 1)
                {
                    break;
                }
            }

            return(action);
        }
コード例 #5
0
ファイル: MinimaxAgent.cs プロジェクト: Armienn/Virus
        public override Move Move(Virus percept)
        {
            //Stopwatch watch = new Stopwatch();
            //watch.Start();
            VirusBoard currentState = percept.GetBoardCopy();

            Move[] actions = currentState.GetPossibleMoves(playerNumber);
            Move   action  = actions[0];

            double max = double.NegativeInfinity;

            foreach (Move a in actions)
            {
                VirusBoard newState = currentState.GetUpdated(a);
                double     q        = Utility(currentState, newState);
                q += MinValue(newState, 0);
                if (q > max)
                {
                    max    = q;
                    action = a;
                }
                if (max == double.PositiveInfinity)
                {
                    break;
                }
            }
            //watch.Stop();

            //StreamWriter timeWriter = new StreamWriter("mmTimeLog",true);
            //timeWriter.WriteLine(watch.ElapsedMilliseconds); // + " ; " + watch.ElapsedTicks);
            //timeWriter.Close();
            return(action);
        }
コード例 #6
0
ファイル: MixedAgent.cs プロジェクト: Armienn/Virus
        public override Move Move(Virus percept)
        {
            VirusBoard state = percept.GetBoardCopy();

            Move[] actions = state.GetPossibleMoves(playerNumber);
            if (actions.Length < 1)
            {
                return(default(Move));
            }
            Move action;

            if (random.NextDouble() > randomRatio)               // bruteforce
            {
                List <Move> list     = new List <Move>();
                int         maxtaken = -1;

                foreach (Move a in actions)
                {
                    int temp = state.TakeablePieces(a);
                    if (a.IsLongMove)
                    {
                        temp--;
                    }

                    if (temp > maxtaken)
                    {
                        maxtaken = temp;
                        list.Clear();
                        list.Add(a);
                    }
                    else if (temp == maxtaken)
                    {
                        list.Add(a);
                    }
                }

                if (deterministic)
                {
                    action = list[0];
                }
                else
                {
                    action = list[random.Next(list.Count)];
                }
            }
            else               // random
            {
                if (deterministic)
                {
                    action = actions[0];
                }
                else
                {
                    action = actions[random.Next(actions.Length)];
                }
            }
            return(action);
        }
コード例 #7
0
        private Move GetMaxExplorationFunctionA(VirusBoard state)
        {
            double max    = double.NegativeInfinity;
            Move   action = default(Move);

            Move[] actions = state.GetPossibleMoves(playerNumber);

            bool berandom = random.NextDouble() < RandomRate;

            foreach (Move a in actions)
            {
                double value = 0;

                if (Q[state.CustomHash()].ContainsKey(a.CustomHash()))
                {
                    if (Q[state.CustomHash()][a.CustomHash()] >= 1)
                    {
                        value  = 1;
                        max    = value;
                        action = a;
                        break;
                    }
                    else if (Q[state.CustomHash()][a.CustomHash()] <= -1)
                    {
                        value = -1;
                    }
                    else
                    {
                        if (berandom)
                        {
                            value = random.NextDouble();
                        }
                        else
                        {
                            value = Q[state.CustomHash()][a.CustomHash()] + ((explore && !(RandomRate > 0)) ? ExplorationRate(N[state.CustomHash()][a.CustomHash()]) : 0);
                        }
                    }
                }
                else
                {
                    value = 1;
                }
                if (value > max)
                {
                    max    = value;
                    action = a;
                }
            }
            return(action);
        }
コード例 #8
0
ファイル: MiniMaxMixAgent.cs プロジェクト: Armienn/Virus
        //Calc maxValue
        private double MaxValue(VirusBoard state, int iteration)
        {
            iteration++;
            if (state.winner == playerNumber)
            {
                return(double.PositiveInfinity);
            }
            if (state.winner != playerNumber && state.winner != 0)
            {
                return(double.NegativeInfinity);
            }

            if (iteration < searchLength)
            {
                Move[] actions = state.GetPossibleMoves(playerNumber);

                double max = double.NegativeInfinity;
                foreach (Move a in actions)
                {
                    VirusBoard newState = state.GetUpdated(a);

                    double q = Utility(state, newState);
                    if (Q.ContainsKey(state.CustomHash()))
                    {
                        if (Q[state.CustomHash()].ContainsKey(a.CustomHash()))
                        {
                            q = Q[state.CustomHash()][a.CustomHash()];
                        }
                    }

                    q += MinValue(newState, iteration);
                    if (q > max)
                    {
                        max = q;
                    }
                    if (max == double.PositiveInfinity)
                    {
                        return(max);
                    }
                }

                return(max);
            }
            else
            {
                return(0);
            }
        }
コード例 #9
0
ファイル: MinimaxAgent.cs プロジェクト: Armienn/Virus
        // Calc minValue
        private double MinValue(VirusBoard state, int iteration)
        {
            iteration++;
            if (state.winner == playerNumber)
            {
                return(double.PositiveInfinity);
            }
            if (state.winner != playerNumber && state.winner != 0)
            {
                return(double.NegativeInfinity);
            }

            if (iteration < searchLength)
            {
                byte   opponent = (playerNumber == 1) ? (byte)2 : (byte)1;
                Move[] actions  = state.GetPossibleMoves(opponent);

                double min = double.PositiveInfinity;
                foreach (Move a in actions)
                {
                    VirusBoard newState = state.GetUpdated(a);
                    double     q        = Utility(state, newState);
                    q += MaxValue(newState, iteration);
                    if (q < min)
                    {
                        min = q;
                    }
                    if (min == double.NegativeInfinity)
                    {
                        return(min);
                    }
                }

                return(min);
            }
            else
            {
                return(0);
            }
        }