src/GameAI/Algorithms/MonteCarlo/RandomSimulation.cs
/// <summary>
/// Return the move with the highest win-rate after performing in parallel the specified number of simulations on the game.
/// </summary>
/// <param name="game">The starting gamestate.</param>
/// <param name="simulations">The number of simulations to perform.</param>
Return the move with the highest win-rate after performing in parallel the specified number of simulations on the game.
Return the move with the highest win - rate after performing in parallel the specified number of simulations on the game .
public static TMove ParallelSearch(TGame game, int simulations)
        {
            TPlayer aiPlayer = game.CurrentPlayer;
            List<TMove> legalMoves = game.GetLegalMoves();
            int count = legalMoves.Count;
            MoveStats[] moveStats = new MoveStats[count];
            Parallel.For(0, simulations,
                () => RandomFactory.Create(),
                (i, loop, localRandom) =>
                {
                    int moveIndex = localRandom.Next(0, count);
                    TGame copy = game.DeepCopy();
                    copy.DoMove(legalMoves[moveIndex]);
                    while (!copy.IsGameOver())
                        copy.DoMove(
                            copy.GetLegalMoves().RandomItem(localRandom));
                    Interlocked.Add(ref moveStats[moveIndex].executions, 1);
                    if (copy.IsWinner(aiPlayer))
                        Interlocked.Add(ref moveStats[moveIndex].victories, 1);
                    return localRandom;
                },
                x => { }
            );
            int bestMoveFound = 0;
            double bestScoreFound = 0f;
            for (int i = 0; i < count; i++)
            {
                double score = moveStats[i].Score();
                if (score > bestScoreFound)
                {
                    bestScoreFound = score;
                    bestMoveFound = i;
                }
            }
            return legalMoves[bestMoveFound];
        }
public static TMove ParallelSearch ( TGame game , int simulations ) { TPlayer aiPlayer = game . CurrentPlayer ; List < TMove > legalMoves = game . GetLegalMoves ( ) ; int count = legalMoves . Count ; MoveStats [ ] moveStats = new MoveStats [ count ] ; Parallel . For ( 0 , simulations , ( ) => RandomFactory . Create ( ) , ( i , loop , localRandom ) => { int moveIndex = localRandom . Next ( 0 , count ) ; TGame copy = game . DeepCopy ( ) ; copy . DoMove ( legalMoves [ moveIndex ] ) ; while ( ! copy . IsGameOver ( ) ) copy . DoMove ( copy . GetLegalMoves ( ) . RandomItem ( localRandom ) ) ; Interlocked . Add ( ref moveStats [ moveIndex ] . executions , 1 ) ; if ( copy . IsWinner ( aiPlayer ) ) Interlocked . Add ( ref moveStats [ moveIndex ] . victories , 1 ) ; return localRandom ; } , x => { } ) ; int bestMoveFound = 0 ; double bestScoreFound = 0f ; for ( int i = 0 ; i < count ; i ++ ) { double score = moveStats [ i ] . Score ( ) ; if ( score > bestScoreFound ) { bestScoreFound = score ; bestMoveFound = i ; } } return legalMoves [ bestMoveFound ] ; }
Return the move with the highest win-rate after performing in parallel the specified number of simulations on the game.
Return the move with the highest win - rate after performing in parallel the specified number of simulations on the game .
//for (int i = 0; i < legalMoves.Count; i++) //    Console.WriteLine("Move " + legalMoves[i] + " has " + moveStats[i].victories + " victories / " + moveStats[i].executions + " executions.");
game
TGame
The starting gamestate.
The starting gamestate .
simulations
int
The number of simulations to perform.
The number of simulations to perform .