private void ResolveWithMemoization(PegNode pegNode) { foreach (var memoization in pegNode.GetMemoization().ToList()) { _probabilityByColumn[memoization.Column] += UpdateMemoizedProbability(pegNode.Peg, memoization); } _pegNodeStack.Clear(); }
public PegNode GetByPeg(Peg peg) { if (!_pegNodesCreated.ContainsKey(peg)) { _pegNodesCreated[peg] = new PegNode(peg); } return(_pegNodesCreated[peg]); }
private void SetupMemoization(PegNode peg) { var memoizationProbability = _pegNodeStack.PopPeg().Probability; while (_pegNodeStack.Count > 0) { var previousNode = _pegNodeStack.Pop(); if (previousNode.Peg.Row == _pegBoard.Rows) { continue; } var previousProbability = memoizationProbability * previousNode.Peg.Probability; memoizationProbability = previousProbability; previousNode.SetMemoization(peg.Peg.Column, previousProbability); } }
private void TraversePegTree(PegNode pegNode, double cumulativeProbability) { _pegNodeStack.Push(pegNode); if (pegNode.HasMemoization()) { ResolveWithMemoization(pegNode); return; } var pegNeighbours = _pegBoard.GetPegUpNeighboursFrom(pegNode.Peg); if (!pegNeighbours.Any() || pegNode.Peg.IsInitialPeg) { _probabilityByColumn[pegNode.Peg.Column] += cumulativeProbability; SetupMemoization(pegNode); return; } foreach (var neighbour in pegNeighbours) { var linkedNode = pegNode.AddLink(_pegNodesFactory.GetByPeg(neighbour)); TraversePegTree(linkedNode, cumulativeProbability * linkedNode.Peg.Probability); } }
public PegNode AddLink(PegNode peg) { _linkedPegs.Add(peg); return(peg); }