private bool TryHandleSkipLevels(out double score) { if (this.skipLevelsHandled) { score = 0; return(false); } else { if (this.skipLevels <= 0) { return(this.HandleSkipLevelsFailed(out score)); } else if (this.skipLevels == 1) { MCTS <T> chosenChild; if (!TryChooseChildForExecute(out chosenChild) || 0 < chosenChild._visits ) { return(this.HandleSkipLevelsFailed(out score)); } else if (chosenChild.Execute(out score)) { return(true); } else { throw new InvalidOperationException(); } } else { while (true) { MCTS <T> chosenChild; if (!TryChooseChildForTryHandleSkipLevels(out chosenChild)) { return(this.HandleSkipLevelsFailed(out score)); } else if (chosenChild.TryHandleSkipLevels(out score)) { return(true); } else if (!chosenChild.skipLevelsHandled) { throw new InvalidOperationException( "Child didn't handle skip levels." ); } } } } }
private bool HandleSkipLevelsFailed(out double score) { this.skipLevelsHandled = true; score = 0; return(false); }
private bool Execute(out double score) { score = 0; bool result = false; if (this._complete) { return(false); } else if (!this.skipLevelsHandled) { Interlocked.Increment(ref this._visits); if (this.TryHandleSkipLevels(out score)) { if (score < 0) { throw new ArgumentOutOfRangeException( "IMCTSGame.Score must not be negative." ); } result = true; } else { Interlocked.Decrement(ref this._visits); } } if (!result) { if (Interlocked.CompareExchange(ref this._visits, 1, 0) == 0) { result = true; score = this.Node.Value; if (score < 0) { throw new ArgumentOutOfRangeException( "IMCTSGame.Score must not be negative." ); } } else { Interlocked.Increment(ref this._visits); while (!result) { MCTS <T> chosenChild; if (!TryChooseChildForExecute(out chosenChild)) { break; } result = chosenChild.Execute(out score); if (!result && !chosenChild.Complete) { throw new InvalidOperationException( "Node didn't execute and Complete is false." ); } } } } if (result) { this.score.Add(score); } else { Interlocked.Decrement(ref this._visits); this._complete = true; } return(result); }