//Meが動くとする。「Meのスコア - Enemyのスコア」の最大値を返す。 private int NegaMax(int deepness, SearchState state, int alpha, int beta, int count, PointEvaluator.Base evaluator) { var sw = System.Diagnostics.Stopwatch.StartNew(); if (deepness == 0) { return(evaluator.Calculate(ScoreBoard, state.MeBoard, 0, state.Me, state.Enemy) - evaluator.Calculate(ScoreBoard, state.EnemyBoard, 0, state.Enemy, state.Me)); } Ways ways = state.MakeMoves(AgentsCount, ScoreBoard); int i = 0; foreach (var way in ways.GetEnumerator(AgentsCount)) { i++; if (CancellationToken.IsCancellationRequested == true) { return(alpha); } //何を返しても良いのでとにかく返す SearchState backup = state; state = state.GetNextState(AgentsCount, way); state = state.ChangeTurn(); int res = -NegaMax(deepness - 1, state, -beta, -alpha, count + 1, evaluator); if (alpha < res) { alpha = res; dp[count].UpdateScore(alpha, way); if (alpha >= beta) { return(beta); //βcut } } state = backup; } sw.Stop(); Log("NODES : {0} nodes, elasped {1} ", i, sw.Elapsed); ways.End(); return(alpha); }
//Meが動くとする。「Meのスコア - Enemyのスコア」の最大値を返す。 private int NegaMax(int deepness, SearchState state, int alpha, int beta, int count, PointEvaluator.Base evaluator, Decision ngMove, int greedyDepth) { if (deepness == 0) { //for (int j = 0; j < greedyDepth; j++) //{ //Unsafe8Array<VelocityPoint> move = state.MakeGreedyMove(ScoreBoard, WayEnumerator, AgentsCount); //state.Move(move, AgentsCount); //Ways moves = state.MakeMoves(AgentsCount, ScoreBoard); //SortMoves(ScoreBoard, state, moves, 49, null); //state.Move(moves[0].Agent1Way, moves[1].Agent2Way); //} int score = evaluator.Calculate(ScoreBoard, state.MeBoard, 0, state.Me, state.Enemy) - evaluator.Calculate(ScoreBoard, state.EnemyBoard, 0, state.Enemy, state.Me); if (greedyDepth % 2 == 1) { return(-score); } return(score); } Ways ways = state.MakeMoves(AgentsCount, ScoreBoard); SmallWays sways = new SmallWays(AgentsCount); //ways => sways foreach (var way in ways.GetEnumerator(AgentsCount)) { sways.Add(new SmallWay(way)); } SortMoves(ScoreBoard, state, sways, count, ngMove); for (int i = 0; i < sways.Count; ++i) { if (CancellationToken.IsCancellationRequested == true) { return(alpha); } //何を返しても良いのでとにかく返す //if (count == 0 && !(ngMove is null) && new Decided(ways[i].Agent1Way, ways[i].Agent2Way).Equals(ngMove)) { continue; } //競合手を避ける場合 if (count == 0 && !(ngMove is null)) //2人とも競合手とは違う手を指す { int j; for (j = 0; j < AgentsCount; ++j) { if (sways[i].Equals(ngMove.Agents[j])) { break; } } if (j != AgentsCount) { continue; } } SearchState nextState = state; nextState = nextState.GetNextState(AgentsCount, sways[i].AgentsWay); nextState = nextState.ChangeTurn(); int res = -NegaMax(deepness - 1, nextState, -beta, -alpha, count + 1, evaluator, ngMove, greedyDepth); if (alpha < res) { alpha = res; if (ngMove is null) { dp1[count].UpdateScore(alpha, sways[i].AgentsWay); } else { dp2[count].UpdateScore(alpha, sways[i].AgentsWay); } if (alpha >= beta) { return(beta); //βcut } } } sways.Erase(); //WaysPool.Return(ways); return(alpha); }