使For循环并行

时间:2014-03-22 21:32:32

标签: c# parallel.for

我可以在这部分代码中使用并行编程吗?

我正在运行For循环1000次。  这个循环中的大量工作就是线(直到最后才玩游戏......):

**PocGraph.Player Winner = Expand(MCPlayout);**

如果我在这里使用并行编程,函数的最终结果评估 可能会受到影响吗?

public float Evaluate(PocGraph Current, Move move)
{
    int Value = 0;
    // value of move
    for (int Plays = 0; Plays < NumberOfPlayouts; Plays++)
    {
        PocGraph MCPlayout = Current.cloneGraph();
        MCPlayout.makeMoveMC(move.Position, Current.CurrentPlayer);
        PocGraph.Player Winner = Expand(MCPlayout);

        if (Winner == Current.CurrentPlayer)
            Value++;                                        // win means +1
        else if (Winner ==  PocGraph.GetOponentPlayer(Current.CurrentPlayer))
            Value--;                                        // loss means -1
    }
    return (Value / (float)NumberOfPlayouts);                // project into [-1,1]

}

protected PocGraph.Player Expand(PocGraph Game)
{
    // maybe the next move
    int MoveId;
    // until the game ends play it
    while (Game.testVictory() == PocGraph.Player.NONE)
    {
        List<Move> ValidMoves = Game.getValidMoves();
        MoveId = Rnd.Next(0, ValidMoves.Count - 1);               
        Game.makeMoveMC(ValidMoves[MoveId].Position, Game.CurrentPlayer);
    }
    // return the winner
    return Game.testVictory();
}

0 个答案:

没有答案
相关问题