using System.Diagnostics; using HeuristicLab.Algorithms.Bandits; using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base; using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation; using HeuristicLab.Problems.GrammaticalOptimization; using System; using System.Linq; using System.Threading; namespace HeuristicLab.Algorithms.MonteCarloTreeSearch { public class MonteCarloTreeSearch_PruneLeaves : MonteCarloTreeSearch { public MonteCarloTreeSearch_PruneLeaves(ISymbolicExpressionTreeProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy, ISimulation simulationPolicy) : base(problem, maxLen, random, behaviourPolicy, simulationPolicy) { } public override void Run(int maxIterations) { Reset(); int selections = 0; TreeNode currentNode; string phrase; string simulatedPhrase; double quality; for (int i = 0; !StopRequested && i < maxIterations; i++) { currentNode = rootNode; while (!currentNode.IsLeaf()) { int currentActionIndex = behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos()); currentNode = currentNode.children[currentActionIndex]; selections++; CheckSelection(currentNode, selections); } phrase = currentNode.phrase; if (phrase.Length <= maxLen) { // Version 2: if (currentNode.children != null && !currentNode.children.Any()) { // already removed all child nodes so remove it too.. currentNode.parent.RemoveChildren(currentNode); i--; continue; } ExpandTreeNode(currentNode); if (currentNode.children.Any()) { currentNode = currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos()) ]; selections++; CheckSelection(currentNode, selections); } else { // Version 2: // remove currentNode from tree.. currentNode.parent.RemoveChildren(currentNode); } } if (currentNode.phrase.Length <= maxLen) { quality = simulation.Simulate(currentNode, out simulatedPhrase); OnSolutionEvaluated(simulatedPhrase, quality); OnIterationFinishedChanged(goodSelections, selections); Propagate(currentNode, quality); } else { i--; } } } } }